Game Development Community

dev|Pro Game Development Curriculum

Implementing Crypto++ into TGEA

by Robert Fritzen · 02/03/2011 (5:31 pm) · 8 comments

Some of you may have read my (first) resource on implementing openSSL into TGEA:
http://www.garagegames.com/community/resources/view/20795

Well, now I'll show you how to implement another popular Cryptography library, Crypto++ into TGEA. I'm also going to be using this cryptography library for my authentication system, for which another resource will be coming when it's done.

Crypto++ is another open source cryptography library developed by Wei Dai. You can download new and older versions of it here, as well as get some general info on Crypto++:

http://www.cryptopp.com/

Likewise with the openSSL version of this system, I have only compiled this on VS 9.0(2008), but there are tutorials online for usage. I have compiled this library as a static library (.lib), so if my memory serves me correctly, you can distribute without requiring any special .dll files which can serve as an advantage. You can download the static library here:

http://www.phantomdev.net/staff/phantom139/public/CryptoPP_5_6_1.zip

The files you want are the cryptlib.lib (for release) and cryptlibd.lib(for debug). I am not really experienced in knowing which files are "required" when building a static library (I only included the .lib files), so all of the crypto++ source files are included in that .zip as well.

What you need to do to get this implemented is sort of on the exact same lines as following the TGEA documentation on getting DirectX to compile with TGEA. So unzip that library to some location on your drive, and have your compiler point to the Crypto++ folder.

If my library files do not work, don't hesitate, I have already found a tutorial that shows you how to do it (I followed this one, so I know it works :) ):

http://www.codeproject.com/KB/tips/CryptoPPIntegration.aspx

Once it's all set to go, let's try out the same little bit of code I showed for openSSL, the SHA1 algorithm:

// Crypto++ Library  
    //include this in the header file you plan on using as your "Class" of cryptos  
    #ifdef _DEBUG  
    #  pragma comment ( lib, "cryptlibd" )  
    #else  
    #  pragma comment ( lib, "cryptlib" )  
    #endif  
      
    #include <modes.h>  
    #include <filters.h>  
    #include <sha.h>  
    #include <hex.h>  
    //End  
      
    using namespace CryptoPP;  
      
    std::string dosha1(std::string text) {  
       CryptoPP::SHA1 hash;    
      
       std::string digest;  
       
       CryptoPP::StringSource foo(text, true,  
          new CryptoPP::HashFilter(hash,  
             new CryptoPP::HexEncoder(  
                new CryptoPP::StringSink(digest), false)));  
      
       return digest;  
    }  
      
    ConsoleFunction(sha1, const char *, 2, 2, "(string) returns the SHA1 hash of a string") {  
       argc;  
       std::string output_hash = dosha1(argv[1]);  
       const char * done = (const char *)malloc(256);  
       strcpy((char *)done, output_hash.c_str());  
       return done;  
    }  
      
    //correct mistakes if you see them again, I can be a lousy coder at times :)

So now tell me, which one do you like better, Crypto++, or OpenSSL :)

I should mention, what I did was created a class that uses all of my cryptography functions, I'd recommend you do the same for your own.

==========================================
For T3D Users, Read Below:
==========================================

T3D makes use of a class named Singleton. This conflicts with the Crypto++ Library. To fix this, you will need to download the library itself (which also means you will be building it following the instructions here.

I recommend you use MSVC 2008, as it has the find/replace in solution tool. Simple open the Crypto++ solution, find all instances of Singleton and replace it with SingletonCPP. If there are any additional errors, you can address those by adjusting the namespace values to match SingletonCPP, but I'm sure the find/replace all will handle this. From there, re-build the library files and modify your engine includes to use those headers and lib file, and you'll be good to go!

#1
02/03/2011 (6:13 pm)
Awesome resource, thanks for sharing it, Robert. I always played with the idea of adding an (optional) SecurID token to our authentication process a'la WoW. :)
#2
02/04/2011 (6:07 am)
cool resource, ty Robert.
#3
02/04/2011 (7:27 am)
No problem.

I am currently working out the PHP side of my authentication system. Unless if anyone here knows how to install phpseclib to save me a bunch of time trying to figure out how to install it :). All that's left it the RSA signature on the auth. server for client accounts, and then I'll be all set to test the system, and ultimately, resource it for your usage.
#4
02/04/2011 (7:23 pm)
I was under the impression that mcrypt has RSA support, but - it does not. :)

Anyway, I found this: RSA encryption in pure PHP It could be easier to bundle with your solution than having to install phpseclib.
#5
02/05/2011 (8:34 am)
Thanks for the link, I'll try it out. Currently just trying to figure out what format CryptoPP wants the CA (PHP Generated) key in.
#6
10/05/2011 (8:11 pm)
Added the libraries and created a quick cpp file with your code snippet in it. I'm on T3D 1.1 and am getting the following 4 errors:

error C2065: 'sha1' : undeclared identifier

error C2059: syntax error : 'const'

error C2143: syntax error : missing ';' before '{'

error C2447: '{' : missing function header (old-style formal list?)

Any help would be appreciated.

-Walker
#7
10/06/2011 (7:36 am)
Turns out the bit of coding that you had to test SHA1 didn't have all the includes:

#include "platform/platform.h"
#include "console/console.h"
#include "console/consoleInternal.h"
#include "console/ast.h"
#include "core/resManager.h"
#include "core/stream/fileStream.h"
#include "console/compiler.h"
#include "platform/event.h"
#include "platform/platformInput.h"
#include "core/util/journal/journal.h"

I took those from your old OpenSSL resource. Hopefully, this helps people behind me, but I think that I am the only one that has tested that code so far.
#8
10/06/2011 (4:07 pm)
Haha, I've managed to solve this problem as well. Turns out that your lib files weren't working for some unknown issue. I compiled them myself from the source (as you suggested in the beginning of your post), and the errors went away. Hopefully this helps anyone behind me.