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 an RSA 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/rsa.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 printRSAKey(RSA* key)
sl@0:     {
sl@0:     printf("static RSA* createRSAKey()\n");
sl@0:     printf("\t{\n");
sl@0: 
sl@0:     printCBN("n_data", key->n);
sl@0:     printCBN("e_data", key->e);
sl@0:     printCBN("d_data", key->d);
sl@0: 
sl@0:     printf("\tRSA* key = RSA_new();\n");
sl@0:     printf("\tkey->n = BN_new();\n");
sl@0:     printf("\tkey->e = BN_new();\n");
sl@0:     printf("\tkey->d = BN_new();\n");
sl@0: 
sl@0:     printf("\tBN_bin2bn(n_data, n_data_len, key->n);\n");
sl@0:     printf("\tBN_bin2bn(e_data, e_data_len, key->e);\n");
sl@0:     printf("\tBN_bin2bn(d_data, d_data_len, key->d);\n");
sl@0:      
sl@0:     printf("\treturn key;\n");
sl@0: 
sl@0:     printf("\t}\n");
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_rsakey\n");
sl@0:     exit(1);
sl@0:     }
sl@0: 
sl@0: int main(int argc, char **argv)
sl@0: 	{
sl@0: 	RSA *rsa;
sl@0: 	int modulus_size = 1024;
sl@0: 	int exponent = 65537;	
sl@0: 
sl@0:     if (argc > 1)
sl@0:         badUsage();
sl@0:     
sl@0: 	RAND_seed(rnd_seed, sizeof rnd_seed);
sl@0: 
sl@0: 	rsa = RSA_generate_key(modulus_size, exponent, NULL, NULL);
sl@0: 
sl@0:     printRSAKey(rsa);
sl@0: 
sl@0: 	return 0;
sl@0: 	}