crypto/weakcrypto/test/tasymmetric/script_gen/utils.c
changeset 72 de46a57f75fb
equal deleted inserted replaced
65:970c0057d9bc 72:de46a57f75fb
       
     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 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 /**
       
    22  @file
       
    23 */
       
    24 
       
    25 #include "utils.h"
       
    26 #include <openssl/crypto.h>
       
    27 #include <openssl/err.h>
       
    28 #include <openssl/rand.h>
       
    29 
       
    30 void printBN(BIGNUM* bn)
       
    31     {
       
    32     char* text = BN_bn2hex(bn);
       
    33     printf("%s", text);
       
    34     OPENSSL_free(text);
       
    35     }
       
    36 
       
    37 void printBin(char* data, int len)
       
    38     {
       
    39     BIGNUM* bn = BN_new();
       
    40     bn = BN_bin2bn(data, len, bn);
       
    41     printBN(bn);
       
    42     BN_free(bn);
       
    43     }
       
    44 
       
    45 static int vectorNumber = 1;
       
    46 
       
    47 /**
       
    48  * Print the first few lines of the action block.
       
    49  */
       
    50 
       
    51 void printActionHeader(char* name, char* type)
       
    52     {
       
    53     printf("<action>\n");
       
    54     printf("\t<actionname>%s %i (%s)</actionname>\n", name, vectorNumber++, type);
       
    55     printf("\t<actiontype>%s</actiontype>\n", type);
       
    56     printf("\t<actionbody>\n");
       
    57     } 
       
    58 
       
    59 /**
       
    60  * Print the last few lines of the action block.
       
    61  */
       
    62 
       
    63 void printActionFooter(BOOL passes)
       
    64     {
       
    65     printf("\t</actionbody>\n");
       
    66     printf("\t<actionresult>\n");
       
    67     printf("\t\t<result>%s</result>\n", passes ? "ETrue" : "EFalse");
       
    68     printf("\t</actionresult>\n");
       
    69     printf("</action>\n");
       
    70     }
       
    71 
       
    72 /**
       
    73  * Print an element containg hex data.
       
    74  */
       
    75 
       
    76 void printHexElement(char* name, unsigned char* data, int len)
       
    77     {
       
    78     printf("\t\t<%s>", name);
       
    79     printBin(data, len);
       
    80     printf("</%s>\n", name);
       
    81     }
       
    82 
       
    83 /**
       
    84  * Print an element containg hex data.
       
    85  */
       
    86 
       
    87 void printBNElement(char* name, BIGNUM* num)
       
    88     {
       
    89     printf("\t\t<%s>", name);
       
    90     printBN(num);
       
    91     printf("</%s>\n", name);
       
    92     }
       
    93 
       
    94 /**
       
    95  * Scramble some data - used for generating tests that we expect to fail.
       
    96  */
       
    97 
       
    98 void scramble(unsigned char* data, int len)
       
    99     {
       
   100     int i;
       
   101     for (i = 0 ; i < len ; ++ i)
       
   102         data[i] ^= i;
       
   103     }
       
   104 
       
   105 /**
       
   106  * Print an openssl error and exit.
       
   107  */
       
   108 
       
   109 void processError()
       
   110     {
       
   111     unsigned long err = ERR_get_error();
       
   112     ERR_load_crypto_strings();
       
   113     printf("Openssl error: %s\n", ERR_error_string(err, NULL));
       
   114     exit(1);
       
   115     }
       
   116 
       
   117 ////////////////////////////////////////////////////////////////////////////////
       
   118 // Random stuff
       
   119 ////////////////////////////////////////////////////////////////////////////////
       
   120 
       
   121 int random_value = 1;
       
   122 
       
   123 void random_seed(const void* buf, int num)
       
   124     {
       
   125     }
       
   126 
       
   127 int random_bytes(unsigned char *buf, int num)
       
   128     {
       
   129     int i;
       
   130     for (i=0; i<num; i++)
       
   131 			buf[i]=random_value++;
       
   132 
       
   133     return(1);
       
   134     }
       
   135 
       
   136 void random_cleanup(void)
       
   137     {
       
   138     }
       
   139 
       
   140 void random_add(const void *buf, int num, double entropy)
       
   141     {
       
   142     }
       
   143 
       
   144 int random_pseudorand(unsigned char *buf, int num)
       
   145     {
       
   146     return 1;
       
   147     }
       
   148 
       
   149 int random_status(void)
       
   150     {
       
   151     return 1;
       
   152     }
       
   153 
       
   154 RAND_METHOD ourRNG = {random_seed, random_bytes, random_cleanup, random_add, random_pseudorand, random_status};
       
   155 
       
   156 void testOurRandom()
       
   157     {
       
   158     char data[16];
       
   159     int i;
       
   160 
       
   161     RAND_bytes(data, 16);
       
   162 
       
   163     for (i = 0 ; i < 16 ; ++i)
       
   164         {
       
   165         if (data[i] != i + 1)
       
   166             {
       
   167             printf("Random number generator not crippled\n");
       
   168             exit(1);
       
   169             }
       
   170         }
       
   171     }
       
   172 
       
   173 void setOurRandom()
       
   174     {
       
   175     random_value = 1;
       
   176     RAND_set_rand_method(&ourRNG);
       
   177     }
       
   178 
       
   179 /** Print C source for assinging binary data to a variable. */
       
   180 void printCBin(char* varname, unsigned char* data, int len)
       
   181     {
       
   182     int i, j;
       
   183 
       
   184     printf("\tunsigned char %s[] =", varname);
       
   185     for (i = 0 ; i < len ; i += 16)
       
   186         {
       
   187         printf("\n\t\t\"");
       
   188         for (j = i ; j < len && j < (i + 16) ; ++j)
       
   189             {
       
   190             printf("\\x%02x", data[j]);
       
   191             }
       
   192         printf("\"");
       
   193         }
       
   194 
       
   195     printf(";\n\n");
       
   196 
       
   197     printf("\tint %s_len = %i;\n\n", varname, len);
       
   198     }
       
   199 
       
   200 /** Print C source for assigning the binary form of a BIGNUM to a variable. */
       
   201 void printCBN(char* varname, BIGNUM* bignum)
       
   202     {
       
   203     int len = BN_num_bytes(bignum);
       
   204     unsigned char buffer[len];
       
   205 
       
   206     BN_bn2bin(bignum, buffer);
       
   207     printCBin(varname, buffer, len);
       
   208     }