Update contrib.
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
26 #include <openssl/crypto.h>
27 #include <openssl/err.h>
28 #include <openssl/rand.h>
30 void printBN(BIGNUM* bn)
32 char* text = BN_bn2hex(bn);
37 void printBin(char* data, int len)
39 BIGNUM* bn = BN_new();
40 bn = BN_bin2bn(data, len, bn);
45 static int vectorNumber = 1;
48 * Print the first few lines of the action block.
51 void printActionHeader(char* name, char* type)
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");
60 * Print the last few lines of the action block.
63 void printActionFooter(BOOL passes)
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");
73 * Print an element containg hex data.
76 void printHexElement(char* name, unsigned char* data, int len)
78 printf("\t\t<%s>", name);
80 printf("</%s>\n", name);
84 * Print an element containg hex data.
87 void printBNElement(char* name, BIGNUM* num)
89 printf("\t\t<%s>", name);
91 printf("</%s>\n", name);
95 * Scramble some data - used for generating tests that we expect to fail.
98 void scramble(unsigned char* data, int len)
101 for (i = 0 ; i < len ; ++ i)
106 * Print an openssl error and exit.
111 unsigned long err = ERR_get_error();
112 ERR_load_crypto_strings();
113 printf("Openssl error: %s\n", ERR_error_string(err, NULL));
117 ////////////////////////////////////////////////////////////////////////////////
119 ////////////////////////////////////////////////////////////////////////////////
121 int random_value = 1;
123 void random_seed(const void* buf, int num)
127 int random_bytes(unsigned char *buf, int num)
130 for (i=0; i<num; i++)
131 buf[i]=random_value++;
136 void random_cleanup(void)
140 void random_add(const void *buf, int num, double entropy)
144 int random_pseudorand(unsigned char *buf, int num)
149 int random_status(void)
154 RAND_METHOD ourRNG = {random_seed, random_bytes, random_cleanup, random_add, random_pseudorand, random_status};
161 RAND_bytes(data, 16);
163 for (i = 0 ; i < 16 ; ++i)
165 if (data[i] != i + 1)
167 printf("Random number generator not crippled\n");
176 RAND_set_rand_method(&ourRNG);
179 /** Print C source for assinging binary data to a variable. */
180 void printCBin(char* varname, unsigned char* data, int len)
184 printf("\tunsigned char %s[] =", varname);
185 for (i = 0 ; i < len ; i += 16)
188 for (j = i ; j < len && j < (i + 16) ; ++j)
190 printf("\\x%02x", data[j]);
197 printf("\tint %s_len = %i;\n\n", varname, len);
200 /** Print C source for assigning the binary form of a BIGNUM to a variable. */
201 void printCBN(char* varname, BIGNUM* bignum)
203 int len = BN_num_bytes(bignum);
204 unsigned char buffer[len];
206 BN_bn2bin(bignum, buffer);
207 printCBin(varname, buffer, len);