]>
Commit | Line | Data |
---|---|---|
0e360199 BL |
1 | /* Written by Ben Laurie, 2001 */ |
2 | /* | |
3 | * Copyright (c) 2001 The OpenSSL Project. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in | |
14 | * the documentation and/or other materials provided with the | |
15 | * distribution. | |
16 | * | |
17 | * 3. All advertising materials mentioning features or use of this | |
18 | * software must display the following acknowledgment: | |
19 | * "This product includes software developed by the OpenSSL Project | |
20 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
21 | * | |
22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
23 | * endorse or promote products derived from this software without | |
24 | * prior written permission. For written permission, please contact | |
25 | * openssl-core@openssl.org. | |
26 | * | |
27 | * 5. Products derived from this software may not be called "OpenSSL" | |
28 | * nor may "OpenSSL" appear in their names without prior written | |
29 | * permission of the OpenSSL Project. | |
30 | * | |
31 | * 6. Redistributions of any form whatsoever must retain the following | |
32 | * acknowledgment: | |
33 | * "This product includes software developed by the OpenSSL Project | |
34 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
35 | * | |
36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
48 | */ | |
49 | ||
50 | #include <stdio.h> | |
51 | #include <string.h> | |
55f78baf RL |
52 | |
53 | #include "../e_os.h" | |
54 | ||
0e360199 BL |
55 | #include <openssl/evp.h> |
56 | #include <openssl/engine.h> | |
9dd5ae65 | 57 | #include <openssl/conf.h> |
0e360199 BL |
58 | |
59 | static void hexdump(FILE *f,const char *title,const unsigned char *s,int l) | |
60 | { | |
61 | int n=0; | |
62 | ||
63 | fprintf(f,"%s",title); | |
64 | for( ; n < l ; ++n) | |
65 | { | |
66 | if((n%16) == 0) | |
67 | fprintf(f,"\n%04x",n); | |
68 | fprintf(f," %02x",s[n]); | |
69 | } | |
70 | fprintf(f,"\n"); | |
71 | } | |
72 | ||
73 | static int convert(unsigned char *s) | |
74 | { | |
75 | unsigned char *d; | |
76 | ||
77 | for(d=s ; *s ; s+=2,++d) | |
78 | { | |
236be532 | 79 | unsigned int n; |
0e360199 BL |
80 | |
81 | if(!s[1]) | |
82 | { | |
83 | fprintf(stderr,"Odd number of hex digits!"); | |
55f78baf | 84 | EXIT(4); |
0e360199 BL |
85 | } |
86 | sscanf((char *)s,"%2x",&n); | |
87 | *d=(unsigned char)n; | |
88 | } | |
89 | return s-d; | |
90 | } | |
91 | ||
5b46eee0 UM |
92 | static char *sstrsep(char **string, const char *delim) |
93 | { | |
94 | char isdelim[256]; | |
95 | char *token = *string; | |
96 | ||
97 | if (**string == 0) | |
98 | return NULL; | |
99 | ||
100 | memset(isdelim, 0, 256); | |
101 | isdelim[0] = 1; | |
102 | ||
103 | while (*delim) | |
104 | { | |
105 | isdelim[(unsigned char)(*delim)] = 1; | |
106 | delim++; | |
107 | } | |
108 | ||
109 | while (!isdelim[(unsigned char)(**string)]) | |
110 | { | |
111 | (*string)++; | |
112 | } | |
113 | ||
114 | if (**string) | |
115 | { | |
116 | **string = 0; | |
117 | (*string)++; | |
118 | } | |
119 | ||
120 | return token; | |
121 | } | |
122 | ||
8716dbea | 123 | static unsigned char *ustrsep(char **p,const char *sep) |
6631a7e7 | 124 | { return (unsigned char *)sstrsep(p,sep); } |
0e360199 BL |
125 | |
126 | static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn, | |
127 | const unsigned char *iv,int in, | |
128 | const unsigned char *plaintext,int pn, | |
e6bd5e8a RL |
129 | const unsigned char *ciphertext,int cn, |
130 | int encdec) | |
0e360199 BL |
131 | { |
132 | EVP_CIPHER_CTX ctx; | |
133 | unsigned char out[4096]; | |
134 | int outl,outl2; | |
135 | ||
e6bd5e8a RL |
136 | printf("Testing cipher %s%s\n",EVP_CIPHER_name(c), |
137 | (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)"))); | |
0e360199 BL |
138 | hexdump(stdout,"Key",key,kn); |
139 | if(in) | |
140 | hexdump(stdout,"IV",iv,in); | |
141 | hexdump(stdout,"Plaintext",plaintext,pn); | |
142 | hexdump(stdout,"Ciphertext",ciphertext,cn); | |
143 | ||
0e360199 BL |
144 | if(kn != c->key_len) |
145 | { | |
146 | fprintf(stderr,"Key length doesn't match, got %d expected %d\n",kn, | |
147 | c->key_len); | |
55f78baf | 148 | EXIT(5); |
0e360199 | 149 | } |
997a54c9 | 150 | EVP_CIPHER_CTX_init(&ctx); |
e6bd5e8a RL |
151 | if (encdec != 0) |
152 | { | |
153 | if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv)) | |
154 | { | |
155 | fprintf(stderr,"EncryptInit failed\n"); | |
55f78baf | 156 | EXIT(10); |
e6bd5e8a RL |
157 | } |
158 | EVP_CIPHER_CTX_set_padding(&ctx,0); | |
0e360199 | 159 | |
e6bd5e8a RL |
160 | if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn)) |
161 | { | |
162 | fprintf(stderr,"Encrypt failed\n"); | |
55f78baf | 163 | EXIT(6); |
e6bd5e8a RL |
164 | } |
165 | if(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2)) | |
166 | { | |
167 | fprintf(stderr,"EncryptFinal failed\n"); | |
55f78baf | 168 | EXIT(7); |
e6bd5e8a | 169 | } |
0e360199 | 170 | |
e6bd5e8a RL |
171 | if(outl+outl2 != cn) |
172 | { | |
173 | fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n", | |
174 | outl+outl2,cn); | |
55f78baf | 175 | EXIT(8); |
e6bd5e8a | 176 | } |
0e360199 | 177 | |
e6bd5e8a RL |
178 | if(memcmp(out,ciphertext,cn)) |
179 | { | |
180 | fprintf(stderr,"Ciphertext mismatch\n"); | |
181 | hexdump(stderr,"Got",out,cn); | |
182 | hexdump(stderr,"Expected",ciphertext,cn); | |
55f78baf | 183 | EXIT(9); |
e6bd5e8a | 184 | } |
0e360199 BL |
185 | } |
186 | ||
e6bd5e8a RL |
187 | if (encdec <= 0) |
188 | { | |
189 | if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv)) | |
190 | { | |
191 | fprintf(stderr,"DecryptInit failed\n"); | |
55f78baf | 192 | EXIT(11); |
e6bd5e8a RL |
193 | } |
194 | EVP_CIPHER_CTX_set_padding(&ctx,0); | |
0e360199 | 195 | |
e6bd5e8a RL |
196 | if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn)) |
197 | { | |
198 | fprintf(stderr,"Decrypt failed\n"); | |
55f78baf | 199 | EXIT(6); |
e6bd5e8a RL |
200 | } |
201 | if(!EVP_DecryptFinal_ex(&ctx,out+outl,&outl2)) | |
202 | { | |
203 | fprintf(stderr,"DecryptFinal failed\n"); | |
55f78baf | 204 | EXIT(7); |
e6bd5e8a | 205 | } |
0e360199 | 206 | |
e6bd5e8a RL |
207 | if(outl+outl2 != cn) |
208 | { | |
209 | fprintf(stderr,"Plaintext length mismatch got %d expected %d\n", | |
210 | outl+outl2,cn); | |
55f78baf | 211 | EXIT(8); |
e6bd5e8a | 212 | } |
0e360199 | 213 | |
e6bd5e8a RL |
214 | if(memcmp(out,plaintext,cn)) |
215 | { | |
216 | fprintf(stderr,"Plaintext mismatch\n"); | |
217 | hexdump(stderr,"Got",out,cn); | |
218 | hexdump(stderr,"Expected",plaintext,cn); | |
55f78baf | 219 | EXIT(9); |
e6bd5e8a | 220 | } |
0e360199 BL |
221 | } |
222 | ||
544a2aea DSH |
223 | EVP_CIPHER_CTX_cleanup(&ctx); |
224 | ||
0e360199 BL |
225 | printf("\n"); |
226 | } | |
227 | ||
4897dc40 BL |
228 | static int test_cipher(const char *cipher,const unsigned char *key,int kn, |
229 | const unsigned char *iv,int in, | |
230 | const unsigned char *plaintext,int pn, | |
e6bd5e8a RL |
231 | const unsigned char *ciphertext,int cn, |
232 | int encdec) | |
4897dc40 BL |
233 | { |
234 | const EVP_CIPHER *c; | |
4897dc40 BL |
235 | |
236 | c=EVP_get_cipherbyname(cipher); | |
237 | if(!c) | |
238 | return 0; | |
239 | ||
e6bd5e8a | 240 | test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec); |
4897dc40 | 241 | |
4897dc40 BL |
242 | return 1; |
243 | } | |
244 | ||
245 | static int test_digest(const char *digest, | |
246 | const unsigned char *plaintext,int pn, | |
b83eddc5 | 247 | const unsigned char *ciphertext, unsigned int cn) |
4897dc40 BL |
248 | { |
249 | const EVP_MD *d; | |
250 | EVP_MD_CTX ctx; | |
251 | unsigned char md[EVP_MAX_MD_SIZE]; | |
252 | unsigned int mdn; | |
253 | ||
254 | d=EVP_get_digestbyname(digest); | |
255 | if(!d) | |
256 | return 0; | |
257 | ||
258 | printf("Testing digest %s\n",EVP_MD_name(d)); | |
259 | hexdump(stdout,"Plaintext",plaintext,pn); | |
260 | hexdump(stdout,"Digest",ciphertext,cn); | |
261 | ||
262 | EVP_MD_CTX_init(&ctx); | |
20d2186c | 263 | if(!EVP_DigestInit_ex(&ctx,d, NULL)) |
4897dc40 BL |
264 | { |
265 | fprintf(stderr,"DigestInit failed\n"); | |
55f78baf | 266 | EXIT(100); |
4897dc40 BL |
267 | } |
268 | if(!EVP_DigestUpdate(&ctx,plaintext,pn)) | |
269 | { | |
270 | fprintf(stderr,"DigestUpdate failed\n"); | |
55f78baf | 271 | EXIT(101); |
4897dc40 | 272 | } |
20d2186c | 273 | if(!EVP_DigestFinal_ex(&ctx,md,&mdn)) |
4897dc40 | 274 | { |
997a54c9 | 275 | fprintf(stderr,"DigestFinal failed\n"); |
55f78baf | 276 | EXIT(101); |
4897dc40 | 277 | } |
997a54c9 | 278 | EVP_MD_CTX_cleanup(&ctx); |
4897dc40 BL |
279 | |
280 | if(mdn != cn) | |
281 | { | |
282 | fprintf(stderr,"Digest length mismatch, got %d expected %d\n",mdn,cn); | |
55f78baf | 283 | EXIT(102); |
4897dc40 BL |
284 | } |
285 | ||
286 | if(memcmp(md,ciphertext,cn)) | |
287 | { | |
288 | fprintf(stderr,"Digest mismatch\n"); | |
289 | hexdump(stderr,"Got",md,cn); | |
290 | hexdump(stderr,"Expected",ciphertext,cn); | |
55f78baf | 291 | EXIT(103); |
4897dc40 BL |
292 | } |
293 | ||
294 | printf("\n"); | |
295 | ||
544a2aea DSH |
296 | EVP_MD_CTX_cleanup(&ctx); |
297 | ||
4897dc40 BL |
298 | return 1; |
299 | } | |
300 | ||
0e360199 BL |
301 | int main(int argc,char **argv) |
302 | { | |
303 | const char *szTestFile; | |
304 | FILE *f; | |
305 | ||
306 | if(argc != 2) | |
307 | { | |
308 | fprintf(stderr,"%s <test file>\n",argv[0]); | |
55f78baf | 309 | EXIT(1); |
0e360199 | 310 | } |
997a54c9 GT |
311 | CRYPTO_malloc_debug_init(); |
312 | CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); | |
313 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); | |
0e360199 BL |
314 | |
315 | szTestFile=argv[1]; | |
316 | ||
317 | f=fopen(szTestFile,"r"); | |
318 | if(!f) | |
319 | { | |
320 | perror(szTestFile); | |
55f78baf | 321 | EXIT(2); |
0e360199 BL |
322 | } |
323 | ||
cb78486d | 324 | /* Load up the software EVP_CIPHER and EVP_MD definitions */ |
0e360199 | 325 | OpenSSL_add_all_ciphers(); |
4897dc40 | 326 | OpenSSL_add_all_digests(); |
cb78486d | 327 | /* Load all compiled-in ENGINEs */ |
0e360199 | 328 | ENGINE_load_builtin_engines(); |
6cc37003 | 329 | #if 0 |
9dd5ae65 | 330 | OPENSSL_config(); |
6cc37003 | 331 | #endif |
11a57c7b GT |
332 | /* Register all available ENGINE implementations of ciphers and digests. |
333 | * This could perhaps be changed to "ENGINE_register_all_complete()"? */ | |
334 | ENGINE_register_all_ciphers(); | |
335 | ENGINE_register_all_digests(); | |
336 | /* If we add command-line options, this statement should be switchable. | |
337 | * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if | |
338 | * they weren't already initialised. */ | |
339 | /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */ | |
0e360199 BL |
340 | |
341 | for( ; ; ) | |
342 | { | |
a8a00498 | 343 | char line[4096]; |
0e360199 BL |
344 | char *p; |
345 | char *cipher; | |
346 | unsigned char *iv,*key,*plaintext,*ciphertext; | |
e6bd5e8a | 347 | int encdec; |
0e360199 | 348 | int kn,in,pn,cn; |
0e360199 | 349 | |
a8a00498 | 350 | if(!fgets((char *)line,sizeof line,f)) |
0e360199 | 351 | break; |
a8a00498 | 352 | if(line[0] == '#' || line[0] == '\n') |
0e360199 | 353 | continue; |
a8a00498 | 354 | p=line; |
5b46eee0 | 355 | cipher=sstrsep(&p,":"); |
0e360199 BL |
356 | key=ustrsep(&p,":"); |
357 | iv=ustrsep(&p,":"); | |
358 | plaintext=ustrsep(&p,":"); | |
e6bd5e8a RL |
359 | ciphertext=ustrsep(&p,":"); |
360 | if (p[-1] == '\n') { | |
361 | p[-1] = '\0'; | |
362 | encdec = -1; | |
363 | } else { | |
6631a7e7 | 364 | encdec = atoi(sstrsep(&p,"\n")); |
e6bd5e8a RL |
365 | } |
366 | ||
0e360199 | 367 | |
0e360199 BL |
368 | kn=convert(key); |
369 | in=convert(iv); | |
370 | pn=convert(plaintext); | |
371 | cn=convert(ciphertext); | |
372 | ||
e6bd5e8a | 373 | if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec) |
4897dc40 | 374 | && !test_digest(cipher,plaintext,pn,ciphertext,cn)) |
0e360199 | 375 | { |
4897dc40 | 376 | fprintf(stderr,"Can't find %s\n",cipher); |
55f78baf | 377 | EXIT(3); |
0e360199 BL |
378 | } |
379 | } | |
380 | ||
997a54c9 GT |
381 | ENGINE_cleanup(); |
382 | EVP_cleanup(); | |
383 | CRYPTO_cleanup_all_ex_data(); | |
384 | ERR_remove_state(0); | |
385 | ERR_free_strings(); | |
386 | CRYPTO_mem_leaks_fp(stderr); | |
0e360199 BL |
387 | |
388 | return 0; | |
389 | } |