Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   Related Pages  

generate_elgamal_key.cc

00001 /*
00002  * Copyright 2003 Michael A. Marsh, Cornell University. All rights reserved.
00003  * This software is released under the modified BSD license.
00004  * See the file LICENSE in the top-level directory for details.
00005  */
00006 //
00007 // $Id: generate_elgamal_key.cc,v 1.4 2004/05/19 15:56:46 mmarsh Exp $
00008 //
00009 // $Log: generate_elgamal_key.cc,v $
00010 // Revision 1.4  2004/05/19 15:56:46  mmarsh
00011 // *** empty log message ***
00012 //
00013 // Revision 1.3  2003/11/06 18:11:51  mmarsh
00014 // Cleaned up doxygen page label and title.
00015 //
00016 // Revision 1.2  2003/11/04 22:07:35  mmarsh
00017 // General code cleanup and reorganization.
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    // use cmdline switch
00045    privKey.fromPEMFile( "/users/mmarsh/src/CODEX/scripts/service.private.pem",
00046                          0 );
00047    // use cmdline switch
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 }

Generated on Wed Jun 2 16:32:55 2004 for COrnell Data EXchange (CODEX) by doxygen1.2.18