os/security/crypto/weakcrypto/test/tasymmetric/script_gen/gen_dsakey.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
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 a DSA 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/dsa.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 printDSAKey(DSA* key)
sl@0
    40
    {
sl@0
    41
    printf("static DSA* createDSAKey()\n");
sl@0
    42
    printf("\t{\n");
sl@0
    43
sl@0
    44
    printCBN("p_data", key->p);
sl@0
    45
    printCBN("q_data", key->q);
sl@0
    46
    printCBN("g_data", key->g);
sl@0
    47
    printCBN("priv_key_data", key->priv_key);
sl@0
    48
    printCBN("pub_key_data", key->pub_key);
sl@0
    49
sl@0
    50
    printf("\tDSA* key = DSA_new();\n");
sl@0
    51
    printf("\tkey->p = BN_new();\n");
sl@0
    52
    printf("\tkey->q = BN_new();\n");
sl@0
    53
    printf("\tkey->g = BN_new();\n");
sl@0
    54
    printf("\tkey->priv_key = BN_new();\n");
sl@0
    55
    printf("\tkey->pub_key = BN_new();\n\n");
sl@0
    56
sl@0
    57
    printf("\tBN_bin2bn(p_data, p_data_len, key->p);\n");
sl@0
    58
    printf("\tBN_bin2bn(q_data, q_data_len, key->q);\n");
sl@0
    59
    printf("\tBN_bin2bn(g_data, g_data_len, key->g);\n");
sl@0
    60
    printf("\tBN_bin2bn(pub_key_data, pub_key_data_len, key->pub_key);\n");
sl@0
    61
    printf("\tBN_bin2bn(priv_key_data, priv_key_data_len, key->priv_key);\n\n");
sl@0
    62
     
sl@0
    63
    printf("\treturn key;\n");
sl@0
    64
sl@0
    65
    printf("\t}\n");
sl@0
    66
    }
sl@0
    67
sl@0
    68
/*
sl@0
    69
 * This is the seed used in the openssl test code.  Using it (by
sl@0
    70
 * specifying the -use_seed option) makes this program generate the
sl@0
    71
 * same key used in the openssl test code
sl@0
    72
 *
sl@0
    73
 * It comes from the updated Appendix 5 to FIPS PUB 186.
sl@0
    74
 */
sl@0
    75
sl@0
    76
static unsigned char seed[20]={
sl@0
    77
	0xd5,0x01,0x4e,0x4b,0x60,0xef,0x2b,0xa8,0xb6,0x21,0x1b,0x40,
sl@0
    78
	0x62,0xba,0x32,0x24,0xe0,0x42,0x7d,0xd3,
sl@0
    79
	};
sl@0
    80
sl@0
    81
static const char rnd_seed[] = "string to make the random number generator think it has entropy";
sl@0
    82
sl@0
    83
static void badUsage()
sl@0
    84
    {
sl@0
    85
    printf("usage: gen_dsakey [ -use_seed ]\n");
sl@0
    86
    exit(1);
sl@0
    87
    }
sl@0
    88
sl@0
    89
int main(int argc, char **argv)
sl@0
    90
	{
sl@0
    91
	DSA *dsa=NULL;
sl@0
    92
	int counter,ret=0,i,j;
sl@0
    93
	unsigned char buf[256];
sl@0
    94
	unsigned long h;
sl@0
    95
	unsigned char sig[256];
sl@0
    96
	unsigned int siglen;
sl@0
    97
    BOOL useSeed = FALSE;
sl@0
    98
sl@0
    99
    if (argc > 2)
sl@0
   100
        badUsage();
sl@0
   101
    else if (argc == 2)
sl@0
   102
        {
sl@0
   103
        if (strcmp(argv[1], "-use_seed") != 0)
sl@0
   104
            badUsage();
sl@0
   105
        useSeed = TRUE;
sl@0
   106
        }
sl@0
   107
    
sl@0
   108
	RAND_seed(rnd_seed, sizeof rnd_seed);
sl@0
   109
sl@0
   110
	dsa=DSA_generate_parameters(512,useSeed ? seed : NULL,20,&counter,&h,NULL,NULL);
sl@0
   111
sl@0
   112
	DSA_generate_key(dsa);
sl@0
   113
sl@0
   114
    printDSAKey(dsa);
sl@0
   115
sl@0
   116
	return 0;
sl@0
   117
	}