os/security/crypto/weakcrypto/test/tasymmetric/script_gen/utils.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcrypto/test/tasymmetric/script_gen/utils.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,208 @@
     1.4 +/*
     1.5 +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +
    1.23 +
    1.24 +/**
    1.25 + @file
    1.26 +*/
    1.27 +
    1.28 +#include "utils.h"
    1.29 +#include <openssl/crypto.h>
    1.30 +#include <openssl/err.h>
    1.31 +#include <openssl/rand.h>
    1.32 +
    1.33 +void printBN(BIGNUM* bn)
    1.34 +    {
    1.35 +    char* text = BN_bn2hex(bn);
    1.36 +    printf("%s", text);
    1.37 +    OPENSSL_free(text);
    1.38 +    }
    1.39 +
    1.40 +void printBin(char* data, int len)
    1.41 +    {
    1.42 +    BIGNUM* bn = BN_new();
    1.43 +    bn = BN_bin2bn(data, len, bn);
    1.44 +    printBN(bn);
    1.45 +    BN_free(bn);
    1.46 +    }
    1.47 +
    1.48 +static int vectorNumber = 1;
    1.49 +
    1.50 +/**
    1.51 + * Print the first few lines of the action block.
    1.52 + */
    1.53 +
    1.54 +void printActionHeader(char* name, char* type)
    1.55 +    {
    1.56 +    printf("<action>\n");
    1.57 +    printf("\t<actionname>%s %i (%s)</actionname>\n", name, vectorNumber++, type);
    1.58 +    printf("\t<actiontype>%s</actiontype>\n", type);
    1.59 +    printf("\t<actionbody>\n");
    1.60 +    } 
    1.61 +
    1.62 +/**
    1.63 + * Print the last few lines of the action block.
    1.64 + */
    1.65 +
    1.66 +void printActionFooter(BOOL passes)
    1.67 +    {
    1.68 +    printf("\t</actionbody>\n");
    1.69 +    printf("\t<actionresult>\n");
    1.70 +    printf("\t\t<result>%s</result>\n", passes ? "ETrue" : "EFalse");
    1.71 +    printf("\t</actionresult>\n");
    1.72 +    printf("</action>\n");
    1.73 +    }
    1.74 +
    1.75 +/**
    1.76 + * Print an element containg hex data.
    1.77 + */
    1.78 +
    1.79 +void printHexElement(char* name, unsigned char* data, int len)
    1.80 +    {
    1.81 +    printf("\t\t<%s>", name);
    1.82 +    printBin(data, len);
    1.83 +    printf("</%s>\n", name);
    1.84 +    }
    1.85 +
    1.86 +/**
    1.87 + * Print an element containg hex data.
    1.88 + */
    1.89 +
    1.90 +void printBNElement(char* name, BIGNUM* num)
    1.91 +    {
    1.92 +    printf("\t\t<%s>", name);
    1.93 +    printBN(num);
    1.94 +    printf("</%s>\n", name);
    1.95 +    }
    1.96 +
    1.97 +/**
    1.98 + * Scramble some data - used for generating tests that we expect to fail.
    1.99 + */
   1.100 +
   1.101 +void scramble(unsigned char* data, int len)
   1.102 +    {
   1.103 +    int i;
   1.104 +    for (i = 0 ; i < len ; ++ i)
   1.105 +        data[i] ^= i;
   1.106 +    }
   1.107 +
   1.108 +/**
   1.109 + * Print an openssl error and exit.
   1.110 + */
   1.111 +
   1.112 +void processError()
   1.113 +    {
   1.114 +    unsigned long err = ERR_get_error();
   1.115 +    ERR_load_crypto_strings();
   1.116 +    printf("Openssl error: %s\n", ERR_error_string(err, NULL));
   1.117 +    exit(1);
   1.118 +    }
   1.119 +
   1.120 +////////////////////////////////////////////////////////////////////////////////
   1.121 +// Random stuff
   1.122 +////////////////////////////////////////////////////////////////////////////////
   1.123 +
   1.124 +int random_value = 1;
   1.125 +
   1.126 +void random_seed(const void* buf, int num)
   1.127 +    {
   1.128 +    }
   1.129 +
   1.130 +int random_bytes(unsigned char *buf, int num)
   1.131 +    {
   1.132 +    int i;
   1.133 +    for (i=0; i<num; i++)
   1.134 +			buf[i]=random_value++;
   1.135 +
   1.136 +    return(1);
   1.137 +    }
   1.138 +
   1.139 +void random_cleanup(void)
   1.140 +    {
   1.141 +    }
   1.142 +
   1.143 +void random_add(const void *buf, int num, double entropy)
   1.144 +    {
   1.145 +    }
   1.146 +
   1.147 +int random_pseudorand(unsigned char *buf, int num)
   1.148 +    {
   1.149 +    return 1;
   1.150 +    }
   1.151 +
   1.152 +int random_status(void)
   1.153 +    {
   1.154 +    return 1;
   1.155 +    }
   1.156 +
   1.157 +RAND_METHOD ourRNG = {random_seed, random_bytes, random_cleanup, random_add, random_pseudorand, random_status};
   1.158 +
   1.159 +void testOurRandom()
   1.160 +    {
   1.161 +    char data[16];
   1.162 +    int i;
   1.163 +
   1.164 +    RAND_bytes(data, 16);
   1.165 +
   1.166 +    for (i = 0 ; i < 16 ; ++i)
   1.167 +        {
   1.168 +        if (data[i] != i + 1)
   1.169 +            {
   1.170 +            printf("Random number generator not crippled\n");
   1.171 +            exit(1);
   1.172 +            }
   1.173 +        }
   1.174 +    }
   1.175 +
   1.176 +void setOurRandom()
   1.177 +    {
   1.178 +    random_value = 1;
   1.179 +    RAND_set_rand_method(&ourRNG);
   1.180 +    }
   1.181 +
   1.182 +/** Print C source for assinging binary data to a variable. */
   1.183 +void printCBin(char* varname, unsigned char* data, int len)
   1.184 +    {
   1.185 +    int i, j;
   1.186 +
   1.187 +    printf("\tunsigned char %s[] =", varname);
   1.188 +    for (i = 0 ; i < len ; i += 16)
   1.189 +        {
   1.190 +        printf("\n\t\t\"");
   1.191 +        for (j = i ; j < len && j < (i + 16) ; ++j)
   1.192 +            {
   1.193 +            printf("\\x%02x", data[j]);
   1.194 +            }
   1.195 +        printf("\"");
   1.196 +        }
   1.197 +
   1.198 +    printf(";\n\n");
   1.199 +
   1.200 +    printf("\tint %s_len = %i;\n\n", varname, len);
   1.201 +    }
   1.202 +
   1.203 +/** Print C source for assigning the binary form of a BIGNUM to a variable. */
   1.204 +void printCBN(char* varname, BIGNUM* bignum)
   1.205 +    {
   1.206 +    int len = BN_num_bytes(bignum);
   1.207 +    unsigned char buffer[len];
   1.208 +
   1.209 +    BN_bn2bin(bignum, buffer);
   1.210 +    printCBin(varname, buffer, len);
   1.211 +    }