1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tasymmetric/script_gen/gen_dsakey.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,117 @@
1.4 +/*
1.5 +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* Generate a DSA key.
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +
1.24 +
1.25 +/**
1.26 + @file
1.27 +*/
1.28 +
1.29 +#include <stdio.h>
1.30 +#include <openssl/crypto.h>
1.31 +#include <openssl/rand.h>
1.32 +#include <openssl/dsa.h>
1.33 +#include <openssl/bn.h>
1.34 +#include "utils.h"
1.35 +
1.36 +#ifndef BOOL
1.37 +#define BOOL int
1.38 +#define TRUE 1
1.39 +#define FALSE 0
1.40 +#endif
1.41 +
1.42 +static void printDSAKey(DSA* key)
1.43 + {
1.44 + printf("static DSA* createDSAKey()\n");
1.45 + printf("\t{\n");
1.46 +
1.47 + printCBN("p_data", key->p);
1.48 + printCBN("q_data", key->q);
1.49 + printCBN("g_data", key->g);
1.50 + printCBN("priv_key_data", key->priv_key);
1.51 + printCBN("pub_key_data", key->pub_key);
1.52 +
1.53 + printf("\tDSA* key = DSA_new();\n");
1.54 + printf("\tkey->p = BN_new();\n");
1.55 + printf("\tkey->q = BN_new();\n");
1.56 + printf("\tkey->g = BN_new();\n");
1.57 + printf("\tkey->priv_key = BN_new();\n");
1.58 + printf("\tkey->pub_key = BN_new();\n\n");
1.59 +
1.60 + printf("\tBN_bin2bn(p_data, p_data_len, key->p);\n");
1.61 + printf("\tBN_bin2bn(q_data, q_data_len, key->q);\n");
1.62 + printf("\tBN_bin2bn(g_data, g_data_len, key->g);\n");
1.63 + printf("\tBN_bin2bn(pub_key_data, pub_key_data_len, key->pub_key);\n");
1.64 + printf("\tBN_bin2bn(priv_key_data, priv_key_data_len, key->priv_key);\n\n");
1.65 +
1.66 + printf("\treturn key;\n");
1.67 +
1.68 + printf("\t}\n");
1.69 + }
1.70 +
1.71 +/*
1.72 + * This is the seed used in the openssl test code. Using it (by
1.73 + * specifying the -use_seed option) makes this program generate the
1.74 + * same key used in the openssl test code
1.75 + *
1.76 + * It comes from the updated Appendix 5 to FIPS PUB 186.
1.77 + */
1.78 +
1.79 +static unsigned char seed[20]={
1.80 + 0xd5,0x01,0x4e,0x4b,0x60,0xef,0x2b,0xa8,0xb6,0x21,0x1b,0x40,
1.81 + 0x62,0xba,0x32,0x24,0xe0,0x42,0x7d,0xd3,
1.82 + };
1.83 +
1.84 +static const char rnd_seed[] = "string to make the random number generator think it has entropy";
1.85 +
1.86 +static void badUsage()
1.87 + {
1.88 + printf("usage: gen_dsakey [ -use_seed ]\n");
1.89 + exit(1);
1.90 + }
1.91 +
1.92 +int main(int argc, char **argv)
1.93 + {
1.94 + DSA *dsa=NULL;
1.95 + int counter,ret=0,i,j;
1.96 + unsigned char buf[256];
1.97 + unsigned long h;
1.98 + unsigned char sig[256];
1.99 + unsigned int siglen;
1.100 + BOOL useSeed = FALSE;
1.101 +
1.102 + if (argc > 2)
1.103 + badUsage();
1.104 + else if (argc == 2)
1.105 + {
1.106 + if (strcmp(argv[1], "-use_seed") != 0)
1.107 + badUsage();
1.108 + useSeed = TRUE;
1.109 + }
1.110 +
1.111 + RAND_seed(rnd_seed, sizeof rnd_seed);
1.112 +
1.113 + dsa=DSA_generate_parameters(512,useSeed ? seed : NULL,20,&counter,&h,NULL,NULL);
1.114 +
1.115 + DSA_generate_key(dsa);
1.116 +
1.117 + printDSAKey(dsa);
1.118 +
1.119 + return 0;
1.120 + }