00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00030 #include <fstream>
00031
00032 #include <openssl/pem.h>
00033 #include <openssl/ssl.h>
00034
00035 #include "CODEX_Ciphers/RSA.h"
00036 #include "CODEX_Ciphers/ElGamal.h"
00037 #include "CODEX_Ciphers/SHA1HashFunction.h"
00038 #include "CODEX_Client/Message.h"
00039
00040 int main()
00041 {
00042 SSLeay_add_ssl_algorithms();
00043 CODEX_Ciphers::RSAPrivateKey privKey;
00044
00045 privKey.fromPEMFile( "/users/mmarsh/src/CODEX/scripts/service.private.pem",
00046 0 );
00047
00048 CODEX_Ciphers::ElGamalKeyPairGenerator gen(1024);
00049 CODEX_Ciphers::ElGamalPublicKey* pubEGKey = 0;
00050 CODEX_Ciphers::ElGamalPrivateKey* privEGKey = 0;
00051 cout << "generating ElGamal key pair (may take awhile)..." << flush;
00052 gen(pubEGKey,privEGKey);
00053 cout << "done" << endl;
00054 BIGNUM * h = BN_new();
00055 BIGNUM * temp = BN_new();
00056 BN_CTX * ctx = BN_CTX_new();
00057 BIGNUM * q = (BIGNUM*)pubEGKey->q();
00058 BIGNUM * p = (BIGNUM*)pubEGKey->p().value();
00059 bool flag;
00060 cout << "generating auxiliary generator..." << flush;
00061 do
00062 {
00063 flag = false;
00064 BN_rand_range(h,p);
00065 if ( 0 >= BN_cmp(h,BN_value_one()) )
00066 {
00067 flag = true;
00068 }
00069 else
00070 {
00071 BN_mod_mul(temp,h,h,p,ctx);
00072 if ( 0 == BN_cmp( temp, BN_value_one() ) )
00073 {
00074 flag = true;
00075 }
00076 else
00077 {
00078 BN_mod_exp(temp,h,q,p,ctx);
00079 if ( 0 == BN_cmp( temp, BN_value_one() ) )
00080 {
00081 flag = true;
00082 }
00083 }
00084 }
00085 } while ( flag );
00086 cout << "done" << endl;
00087 BN_free(temp);
00088 CODEX_Server::AugmentedEGPublicKey pkMsg( *pubEGKey,
00089 CODEX_ASN1::BigNumber(h) );
00090 CODEX_Ciphers::SHA1HashFunction hashFunc;
00091 BIGNUM * digest = pkMsg.digest( hashFunc );
00092 CODEX_Ciphers::RSASignature* signature = privKey.sign( digest );
00093 BN_free( digest );
00094 CODEX_Server::SignedAugmentedEGPublicKey signedPKMsg( pkMsg, *signature );
00095 delete signature;
00096 privEGKey->toFile("/users/mmarsh/src/CODEX/elgamal.priv");
00097 int length = signedPKMsg.marshal(0);
00098 unsigned char* buff = new unsigned char[length];
00099 unsigned char* pBuff = buff;
00100 signedPKMsg.marshal(&pBuff);
00101 ofstream os("/users/mmarsh/src/CODEX/elgamal.pub");
00102 for ( int i = 0 ; i < length ; ++i )
00103 {
00104 os << buff[i];
00105 }
00106 os.close();
00107 delete [] buff;
00108 return 0;
00109 }