os/security/crypto/weakcrypto/test/tasymmetric/script_gen/gen_dsakey.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 * Generate a DSA key.
    16 *
    17 */
    18 
    19 
    20 
    21 
    22 /**
    23  @file
    24 */
    25 
    26 #include <stdio.h>
    27 #include <openssl/crypto.h>
    28 #include <openssl/rand.h>
    29 #include <openssl/dsa.h>
    30 #include <openssl/bn.h>
    31 #include "utils.h"
    32 
    33 #ifndef BOOL
    34 #define BOOL int
    35 #define TRUE 1
    36 #define FALSE 0
    37 #endif
    38 
    39 static void printDSAKey(DSA* key)
    40     {
    41     printf("static DSA* createDSAKey()\n");
    42     printf("\t{\n");
    43 
    44     printCBN("p_data", key->p);
    45     printCBN("q_data", key->q);
    46     printCBN("g_data", key->g);
    47     printCBN("priv_key_data", key->priv_key);
    48     printCBN("pub_key_data", key->pub_key);
    49 
    50     printf("\tDSA* key = DSA_new();\n");
    51     printf("\tkey->p = BN_new();\n");
    52     printf("\tkey->q = BN_new();\n");
    53     printf("\tkey->g = BN_new();\n");
    54     printf("\tkey->priv_key = BN_new();\n");
    55     printf("\tkey->pub_key = BN_new();\n\n");
    56 
    57     printf("\tBN_bin2bn(p_data, p_data_len, key->p);\n");
    58     printf("\tBN_bin2bn(q_data, q_data_len, key->q);\n");
    59     printf("\tBN_bin2bn(g_data, g_data_len, key->g);\n");
    60     printf("\tBN_bin2bn(pub_key_data, pub_key_data_len, key->pub_key);\n");
    61     printf("\tBN_bin2bn(priv_key_data, priv_key_data_len, key->priv_key);\n\n");
    62      
    63     printf("\treturn key;\n");
    64 
    65     printf("\t}\n");
    66     }
    67 
    68 /*
    69  * This is the seed used in the openssl test code.  Using it (by
    70  * specifying the -use_seed option) makes this program generate the
    71  * same key used in the openssl test code
    72  *
    73  * It comes from the updated Appendix 5 to FIPS PUB 186.
    74  */
    75 
    76 static unsigned char seed[20]={
    77 	0xd5,0x01,0x4e,0x4b,0x60,0xef,0x2b,0xa8,0xb6,0x21,0x1b,0x40,
    78 	0x62,0xba,0x32,0x24,0xe0,0x42,0x7d,0xd3,
    79 	};
    80 
    81 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
    82 
    83 static void badUsage()
    84     {
    85     printf("usage: gen_dsakey [ -use_seed ]\n");
    86     exit(1);
    87     }
    88 
    89 int main(int argc, char **argv)
    90 	{
    91 	DSA *dsa=NULL;
    92 	int counter,ret=0,i,j;
    93 	unsigned char buf[256];
    94 	unsigned long h;
    95 	unsigned char sig[256];
    96 	unsigned int siglen;
    97     BOOL useSeed = FALSE;
    98 
    99     if (argc > 2)
   100         badUsage();
   101     else if (argc == 2)
   102         {
   103         if (strcmp(argv[1], "-use_seed") != 0)
   104             badUsage();
   105         useSeed = TRUE;
   106         }
   107     
   108 	RAND_seed(rnd_seed, sizeof rnd_seed);
   109 
   110 	dsa=DSA_generate_parameters(512,useSeed ? seed : NULL,20,&counter,&h,NULL,NULL);
   111 
   112 	DSA_generate_key(dsa);
   113 
   114     printDSAKey(dsa);
   115 
   116 	return 0;
   117 	}