os/security/crypto/weakcrypto/test/tasymmetric/script_gen/gen_random.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.
     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 * Generates C code that initialises a variable to hold some random data.
    16 *
    17 */
    18 
    19 
    20 
    21 
    22 /**
    23  @file
    24 */
    25 
    26 #include <openssl/rand.h>
    27 
    28 static void gen_random(int len)
    29     {
    30     unsigned char data[len];
    31     int i, j;
    32 
    33     RAND_bytes(data, len);
    34 
    35     printf("unsigned char random[] =");
    36     for (i = 0 ; i < sizeof(data) ; i += 16)
    37         {
    38         printf("\n\t\"");
    39         for (j = i ; j < sizeof(data) && j < (i + 16) ; ++j)
    40             {
    41             printf("\\x%02x", data[j]);
    42             }
    43         printf("\"");
    44         }
    45 
    46     printf(";\n");
    47     }
    48 
    49 static void badUsage()
    50     {
    51     printf("gen_random [BYTES]\n");
    52     exit(1);
    53     }
    54 
    55 int main(int argc, char* argv[])
    56     {
    57     int bytes = 256;
    58 
    59     if (argc > 2)
    60         badUsage();
    61     else if (argc == 2)
    62         {
    63         bytes = atoi(argv[1]);
    64         if (bytes < 1)
    65             badUsage();
    66         }
    67 
    68     gen_random(bytes);
    69     return 0;
    70     }