os/security/crypto/weakcrypto/test/tasymmetric/script_gen/gen_rsakey.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* Generate an RSA key.
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
sl@0
    21
sl@0
    22
/**
sl@0
    23
 @file
sl@0
    24
*/
sl@0
    25
sl@0
    26
#include <stdio.h>
sl@0
    27
#include <openssl/crypto.h>
sl@0
    28
#include <openssl/rand.h>
sl@0
    29
#include <openssl/rsa.h>
sl@0
    30
#include <openssl/bn.h>
sl@0
    31
#include "utils.h"
sl@0
    32
sl@0
    33
#ifndef BOOL
sl@0
    34
#define BOOL int
sl@0
    35
#define TRUE 1
sl@0
    36
#define FALSE 0
sl@0
    37
#endif
sl@0
    38
sl@0
    39
static void printRSAKey(RSA* key)
sl@0
    40
    {
sl@0
    41
    printf("static RSA* createRSAKey()\n");
sl@0
    42
    printf("\t{\n");
sl@0
    43
sl@0
    44
    printCBN("n_data", key->n);
sl@0
    45
    printCBN("e_data", key->e);
sl@0
    46
    printCBN("d_data", key->d);
sl@0
    47
sl@0
    48
    printf("\tRSA* key = RSA_new();\n");
sl@0
    49
    printf("\tkey->n = BN_new();\n");
sl@0
    50
    printf("\tkey->e = BN_new();\n");
sl@0
    51
    printf("\tkey->d = BN_new();\n");
sl@0
    52
sl@0
    53
    printf("\tBN_bin2bn(n_data, n_data_len, key->n);\n");
sl@0
    54
    printf("\tBN_bin2bn(e_data, e_data_len, key->e);\n");
sl@0
    55
    printf("\tBN_bin2bn(d_data, d_data_len, key->d);\n");
sl@0
    56
     
sl@0
    57
    printf("\treturn key;\n");
sl@0
    58
sl@0
    59
    printf("\t}\n");
sl@0
    60
    }
sl@0
    61
sl@0
    62
static const char rnd_seed[] = "string to make the random number generator think it has entropy";
sl@0
    63
sl@0
    64
static void badUsage()
sl@0
    65
    {
sl@0
    66
    printf("usage: gen_rsakey\n");
sl@0
    67
    exit(1);
sl@0
    68
    }
sl@0
    69
sl@0
    70
int main(int argc, char **argv)
sl@0
    71
	{
sl@0
    72
	RSA *rsa;
sl@0
    73
	int modulus_size = 1024;
sl@0
    74
	int exponent = 65537;	
sl@0
    75
sl@0
    76
    if (argc > 1)
sl@0
    77
        badUsage();
sl@0
    78
    
sl@0
    79
	RAND_seed(rnd_seed, sizeof rnd_seed);
sl@0
    80
sl@0
    81
	rsa = RSA_generate_key(modulus_size, exponent, NULL, NULL);
sl@0
    82
sl@0
    83
    printRSAKey(rsa);
sl@0
    84
sl@0
    85
	return 0;
sl@0
    86
	}