sl@0: /*
sl@0: * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: * All rights reserved.
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of the License "Eclipse Public License v1.0"
sl@0: * which accompanies this distribution, and is available
sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: *
sl@0: * Initial Contributors:
sl@0: * Nokia Corporation - initial contribution.
sl@0: *
sl@0: * Contributors:
sl@0: *
sl@0: * Description: 
sl@0: * Generate a DSA key.
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: 
sl@0: 
sl@0: /**
sl@0:  @file
sl@0: */
sl@0: 
sl@0: #include <stdio.h>
sl@0: #include <openssl/crypto.h>
sl@0: #include <openssl/rand.h>
sl@0: #include <openssl/dsa.h>
sl@0: #include <openssl/bn.h>
sl@0: #include "utils.h"
sl@0: 
sl@0: #ifndef BOOL
sl@0: #define BOOL int
sl@0: #define TRUE 1
sl@0: #define FALSE 0
sl@0: #endif
sl@0: 
sl@0: static void printDSAKey(DSA* key)
sl@0:     {
sl@0:     printf("static DSA* createDSAKey()\n");
sl@0:     printf("\t{\n");
sl@0: 
sl@0:     printCBN("p_data", key->p);
sl@0:     printCBN("q_data", key->q);
sl@0:     printCBN("g_data", key->g);
sl@0:     printCBN("priv_key_data", key->priv_key);
sl@0:     printCBN("pub_key_data", key->pub_key);
sl@0: 
sl@0:     printf("\tDSA* key = DSA_new();\n");
sl@0:     printf("\tkey->p = BN_new();\n");
sl@0:     printf("\tkey->q = BN_new();\n");
sl@0:     printf("\tkey->g = BN_new();\n");
sl@0:     printf("\tkey->priv_key = BN_new();\n");
sl@0:     printf("\tkey->pub_key = BN_new();\n\n");
sl@0: 
sl@0:     printf("\tBN_bin2bn(p_data, p_data_len, key->p);\n");
sl@0:     printf("\tBN_bin2bn(q_data, q_data_len, key->q);\n");
sl@0:     printf("\tBN_bin2bn(g_data, g_data_len, key->g);\n");
sl@0:     printf("\tBN_bin2bn(pub_key_data, pub_key_data_len, key->pub_key);\n");
sl@0:     printf("\tBN_bin2bn(priv_key_data, priv_key_data_len, key->priv_key);\n\n");
sl@0:      
sl@0:     printf("\treturn key;\n");
sl@0: 
sl@0:     printf("\t}\n");
sl@0:     }
sl@0: 
sl@0: /*
sl@0:  * This is the seed used in the openssl test code.  Using it (by
sl@0:  * specifying the -use_seed option) makes this program generate the
sl@0:  * same key used in the openssl test code
sl@0:  *
sl@0:  * It comes from the updated Appendix 5 to FIPS PUB 186.
sl@0:  */
sl@0: 
sl@0: static unsigned char seed[20]={
sl@0: 	0xd5,0x01,0x4e,0x4b,0x60,0xef,0x2b,0xa8,0xb6,0x21,0x1b,0x40,
sl@0: 	0x62,0xba,0x32,0x24,0xe0,0x42,0x7d,0xd3,
sl@0: 	};
sl@0: 
sl@0: static const char rnd_seed[] = "string to make the random number generator think it has entropy";
sl@0: 
sl@0: static void badUsage()
sl@0:     {
sl@0:     printf("usage: gen_dsakey [ -use_seed ]\n");
sl@0:     exit(1);
sl@0:     }
sl@0: 
sl@0: int main(int argc, char **argv)
sl@0: 	{
sl@0: 	DSA *dsa=NULL;
sl@0: 	int counter,ret=0,i,j;
sl@0: 	unsigned char buf[256];
sl@0: 	unsigned long h;
sl@0: 	unsigned char sig[256];
sl@0: 	unsigned int siglen;
sl@0:     BOOL useSeed = FALSE;
sl@0: 
sl@0:     if (argc > 2)
sl@0:         badUsage();
sl@0:     else if (argc == 2)
sl@0:         {
sl@0:         if (strcmp(argv[1], "-use_seed") != 0)
sl@0:             badUsage();
sl@0:         useSeed = TRUE;
sl@0:         }
sl@0:     
sl@0: 	RAND_seed(rnd_seed, sizeof rnd_seed);
sl@0: 
sl@0: 	dsa=DSA_generate_parameters(512,useSeed ? seed : NULL,20,&counter,&h,NULL,NULL);
sl@0: 
sl@0: 	DSA_generate_key(dsa);
sl@0: 
sl@0:     printDSAKey(dsa);
sl@0: 
sl@0: 	return 0;
sl@0: 	}