os/security/crypto/weakcrypto/test/tasymmetric/script_gen/gen_rsakey.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 an RSA 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/rsa.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 printRSAKey(RSA* key)
    40     {
    41     printf("static RSA* createRSAKey()\n");
    42     printf("\t{\n");
    43 
    44     printCBN("n_data", key->n);
    45     printCBN("e_data", key->e);
    46     printCBN("d_data", key->d);
    47 
    48     printf("\tRSA* key = RSA_new();\n");
    49     printf("\tkey->n = BN_new();\n");
    50     printf("\tkey->e = BN_new();\n");
    51     printf("\tkey->d = BN_new();\n");
    52 
    53     printf("\tBN_bin2bn(n_data, n_data_len, key->n);\n");
    54     printf("\tBN_bin2bn(e_data, e_data_len, key->e);\n");
    55     printf("\tBN_bin2bn(d_data, d_data_len, key->d);\n");
    56      
    57     printf("\treturn key;\n");
    58 
    59     printf("\t}\n");
    60     }
    61 
    62 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
    63 
    64 static void badUsage()
    65     {
    66     printf("usage: gen_rsakey\n");
    67     exit(1);
    68     }
    69 
    70 int main(int argc, char **argv)
    71 	{
    72 	RSA *rsa;
    73 	int modulus_size = 1024;
    74 	int exponent = 65537;	
    75 
    76     if (argc > 1)
    77         badUsage();
    78     
    79 	RAND_seed(rnd_seed, sizeof rnd_seed);
    80 
    81 	rsa = RSA_generate_key(modulus_size, exponent, NULL, NULL);
    82 
    83     printRSAKey(rsa);
    84 
    85 	return 0;
    86 	}