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