]>
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 | 55 | #include <openssl/evp.h> |
0b13e9f0 | 56 | #ifndef OPENSSL_NO_ENGINE |
0e360199 | 57 | #include <openssl/engine.h> |
0b13e9f0 RL |
58 | #endif |
59 | #include <openssl/err.h> | |
9dd5ae65 | 60 | #include <openssl/conf.h> |
0e360199 BL |
61 | |
62 | static void hexdump(FILE *f,const char *title,const unsigned char *s,int l) | |
63 | { | |
64 | int n=0; | |
65 | ||
66 | fprintf(f,"%s",title); | |
67 | for( ; n < l ; ++n) | |
68 | { | |
69 | if((n%16) == 0) | |
70 | fprintf(f,"\n%04x",n); | |
71 | fprintf(f," %02x",s[n]); | |
72 | } | |
73 | fprintf(f,"\n"); | |
74 | } | |
75 | ||
76 | static int convert(unsigned char *s) | |
77 | { | |
78 | unsigned char *d; | |
79 | ||
80 | for(d=s ; *s ; s+=2,++d) | |
81 | { | |
236be532 | 82 | unsigned int n; |
0e360199 BL |
83 | |
84 | if(!s[1]) | |
85 | { | |
86 | fprintf(stderr,"Odd number of hex digits!"); | |
55f78baf | 87 | EXIT(4); |
0e360199 BL |
88 | } |
89 | sscanf((char *)s,"%2x",&n); | |
90 | *d=(unsigned char)n; | |
91 | } | |
92 | return s-d; | |
93 | } | |
94 | ||
5b46eee0 UM |
95 | static char *sstrsep(char **string, const char *delim) |
96 | { | |
97 | char isdelim[256]; | |
98 | char *token = *string; | |
99 | ||
100 | if (**string == 0) | |
101 | return NULL; | |
102 | ||
103 | memset(isdelim, 0, 256); | |
104 | isdelim[0] = 1; | |
105 | ||
106 | while (*delim) | |
107 | { | |
108 | isdelim[(unsigned char)(*delim)] = 1; | |
109 | delim++; | |
110 | } | |
111 | ||
112 | while (!isdelim[(unsigned char)(**string)]) | |
113 | { | |
114 | (*string)++; | |
115 | } | |
116 | ||
117 | if (**string) | |
118 | { | |
119 | **string = 0; | |
120 | (*string)++; | |
121 | } | |
122 | ||
123 | return token; | |
124 | } | |
125 | ||
8716dbea | 126 | static unsigned char *ustrsep(char **p,const char *sep) |
6631a7e7 | 127 | { return (unsigned char *)sstrsep(p,sep); } |
0e360199 | 128 | |
848f735a RL |
129 | static int test1_exit(int ec) |
130 | { | |
131 | EXIT(ec); | |
3dda0dd2 | 132 | return(0); /* To keep some compilers quiet */ |
848f735a RL |
133 | } |
134 | ||
0e360199 BL |
135 | static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn, |
136 | const unsigned char *iv,int in, | |
137 | const unsigned char *plaintext,int pn, | |
e6bd5e8a RL |
138 | const unsigned char *ciphertext,int cn, |
139 | int encdec) | |
0e360199 BL |
140 | { |
141 | EVP_CIPHER_CTX ctx; | |
142 | unsigned char out[4096]; | |
143 | int outl,outl2; | |
144 | ||
e6bd5e8a RL |
145 | printf("Testing cipher %s%s\n",EVP_CIPHER_name(c), |
146 | (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)"))); | |
0e360199 BL |
147 | hexdump(stdout,"Key",key,kn); |
148 | if(in) | |
149 | hexdump(stdout,"IV",iv,in); | |
150 | hexdump(stdout,"Plaintext",plaintext,pn); | |
151 | hexdump(stdout,"Ciphertext",ciphertext,cn); | |
152 | ||
0e360199 BL |
153 | if(kn != c->key_len) |
154 | { | |
155 | fprintf(stderr,"Key length doesn't match, got %d expected %d\n",kn, | |
156 | c->key_len); | |
848f735a | 157 | test1_exit(5); |
0e360199 | 158 | } |
997a54c9 | 159 | EVP_CIPHER_CTX_init(&ctx); |
e6bd5e8a RL |
160 | if (encdec != 0) |
161 | { | |
162 | if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv)) | |
163 | { | |
164 | fprintf(stderr,"EncryptInit failed\n"); | |
848f735a | 165 | test1_exit(10); |
e6bd5e8a RL |
166 | } |
167 | EVP_CIPHER_CTX_set_padding(&ctx,0); | |
0e360199 | 168 | |
e6bd5e8a RL |
169 | if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn)) |
170 | { | |
171 | fprintf(stderr,"Encrypt failed\n"); | |
848f735a | 172 | test1_exit(6); |
e6bd5e8a RL |
173 | } |
174 | if(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2)) | |
175 | { | |
176 | fprintf(stderr,"EncryptFinal failed\n"); | |
848f735a | 177 | test1_exit(7); |
e6bd5e8a | 178 | } |
0e360199 | 179 | |
e6bd5e8a RL |
180 | if(outl+outl2 != cn) |
181 | { | |
182 | fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n", | |
183 | outl+outl2,cn); | |
848f735a | 184 | test1_exit(8); |
e6bd5e8a | 185 | } |
0e360199 | 186 | |
e6bd5e8a RL |
187 | if(memcmp(out,ciphertext,cn)) |
188 | { | |
189 | fprintf(stderr,"Ciphertext mismatch\n"); | |
190 | hexdump(stderr,"Got",out,cn); | |
191 | hexdump(stderr,"Expected",ciphertext,cn); | |
848f735a | 192 | test1_exit(9); |
e6bd5e8a | 193 | } |
0e360199 BL |
194 | } |
195 | ||
e6bd5e8a RL |
196 | if (encdec <= 0) |
197 | { | |
198 | if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv)) | |
199 | { | |
200 | fprintf(stderr,"DecryptInit failed\n"); | |
848f735a | 201 | test1_exit(11); |
e6bd5e8a RL |
202 | } |
203 | EVP_CIPHER_CTX_set_padding(&ctx,0); | |
0e360199 | 204 | |
e6bd5e8a RL |
205 | if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn)) |
206 | { | |
207 | fprintf(stderr,"Decrypt failed\n"); | |
848f735a | 208 | test1_exit(6); |
e6bd5e8a RL |
209 | } |
210 | if(!EVP_DecryptFinal_ex(&ctx,out+outl,&outl2)) | |
211 | { | |
212 | fprintf(stderr,"DecryptFinal failed\n"); | |
848f735a | 213 | test1_exit(7); |
e6bd5e8a | 214 | } |
0e360199 | 215 | |
e6bd5e8a RL |
216 | if(outl+outl2 != cn) |
217 | { | |
218 | fprintf(stderr,"Plaintext length mismatch got %d expected %d\n", | |
219 | outl+outl2,cn); | |
848f735a | 220 | test1_exit(8); |
e6bd5e8a | 221 | } |
0e360199 | 222 | |
e6bd5e8a RL |
223 | if(memcmp(out,plaintext,cn)) |
224 | { | |
225 | fprintf(stderr,"Plaintext mismatch\n"); | |
226 | hexdump(stderr,"Got",out,cn); | |
227 | hexdump(stderr,"Expected",plaintext,cn); | |
848f735a | 228 | test1_exit(9); |
e6bd5e8a | 229 | } |
0e360199 BL |
230 | } |
231 | ||
544a2aea DSH |
232 | EVP_CIPHER_CTX_cleanup(&ctx); |
233 | ||
0e360199 BL |
234 | printf("\n"); |
235 | } | |
236 | ||
4897dc40 BL |
237 | static int test_cipher(const char *cipher,const unsigned char *key,int kn, |
238 | const unsigned char *iv,int in, | |
239 | const unsigned char *plaintext,int pn, | |
e6bd5e8a RL |
240 | const unsigned char *ciphertext,int cn, |
241 | int encdec) | |
4897dc40 BL |
242 | { |
243 | const EVP_CIPHER *c; | |
4897dc40 BL |
244 | |
245 | c=EVP_get_cipherbyname(cipher); | |
246 | if(!c) | |
247 | return 0; | |
248 | ||
e6bd5e8a | 249 | test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec); |
4897dc40 | 250 | |
4897dc40 BL |
251 | return 1; |
252 | } | |
253 | ||
254 | static int test_digest(const char *digest, | |
255 | const unsigned char *plaintext,int pn, | |
b83eddc5 | 256 | const unsigned char *ciphertext, unsigned int cn) |
4897dc40 BL |
257 | { |
258 | const EVP_MD *d; | |
259 | EVP_MD_CTX ctx; | |
260 | unsigned char md[EVP_MAX_MD_SIZE]; | |
261 | unsigned int mdn; | |
262 | ||
263 | d=EVP_get_digestbyname(digest); | |
264 | if(!d) | |
265 | return 0; | |
266 | ||
267 | printf("Testing digest %s\n",EVP_MD_name(d)); | |
268 | hexdump(stdout,"Plaintext",plaintext,pn); | |
269 | hexdump(stdout,"Digest",ciphertext,cn); | |
270 | ||
271 | EVP_MD_CTX_init(&ctx); | |
20d2186c | 272 | if(!EVP_DigestInit_ex(&ctx,d, NULL)) |
4897dc40 BL |
273 | { |
274 | fprintf(stderr,"DigestInit failed\n"); | |
55f78baf | 275 | EXIT(100); |
4897dc40 BL |
276 | } |
277 | if(!EVP_DigestUpdate(&ctx,plaintext,pn)) | |
278 | { | |
279 | fprintf(stderr,"DigestUpdate failed\n"); | |
55f78baf | 280 | EXIT(101); |
4897dc40 | 281 | } |
20d2186c | 282 | if(!EVP_DigestFinal_ex(&ctx,md,&mdn)) |
4897dc40 | 283 | { |
997a54c9 | 284 | fprintf(stderr,"DigestFinal failed\n"); |
55f78baf | 285 | EXIT(101); |
4897dc40 | 286 | } |
997a54c9 | 287 | EVP_MD_CTX_cleanup(&ctx); |
4897dc40 BL |
288 | |
289 | if(mdn != cn) | |
290 | { | |
291 | fprintf(stderr,"Digest length mismatch, got %d expected %d\n",mdn,cn); | |
55f78baf | 292 | EXIT(102); |
4897dc40 BL |
293 | } |
294 | ||
295 | if(memcmp(md,ciphertext,cn)) | |
296 | { | |
297 | fprintf(stderr,"Digest mismatch\n"); | |
298 | hexdump(stderr,"Got",md,cn); | |
299 | hexdump(stderr,"Expected",ciphertext,cn); | |
55f78baf | 300 | EXIT(103); |
4897dc40 BL |
301 | } |
302 | ||
303 | printf("\n"); | |
304 | ||
544a2aea DSH |
305 | EVP_MD_CTX_cleanup(&ctx); |
306 | ||
4897dc40 BL |
307 | return 1; |
308 | } | |
309 | ||
0e360199 BL |
310 | int main(int argc,char **argv) |
311 | { | |
312 | const char *szTestFile; | |
313 | FILE *f; | |
314 | ||
315 | if(argc != 2) | |
316 | { | |
317 | fprintf(stderr,"%s <test file>\n",argv[0]); | |
55f78baf | 318 | EXIT(1); |
0e360199 | 319 | } |
997a54c9 GT |
320 | CRYPTO_malloc_debug_init(); |
321 | CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); | |
322 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); | |
0e360199 BL |
323 | |
324 | szTestFile=argv[1]; | |
325 | ||
326 | f=fopen(szTestFile,"r"); | |
327 | if(!f) | |
328 | { | |
329 | perror(szTestFile); | |
55f78baf | 330 | EXIT(2); |
0e360199 BL |
331 | } |
332 | ||
cb78486d | 333 | /* Load up the software EVP_CIPHER and EVP_MD definitions */ |
0e360199 | 334 | OpenSSL_add_all_ciphers(); |
4897dc40 | 335 | OpenSSL_add_all_digests(); |
0b13e9f0 | 336 | #ifndef OPENSSL_NO_ENGINE |
cb78486d | 337 | /* Load all compiled-in ENGINEs */ |
0e360199 | 338 | ENGINE_load_builtin_engines(); |
0b13e9f0 | 339 | #endif |
6cc37003 | 340 | #if 0 |
9dd5ae65 | 341 | OPENSSL_config(); |
6cc37003 | 342 | #endif |
0b13e9f0 | 343 | #ifndef OPENSSL_NO_ENGINE |
11a57c7b GT |
344 | /* Register all available ENGINE implementations of ciphers and digests. |
345 | * This could perhaps be changed to "ENGINE_register_all_complete()"? */ | |
346 | ENGINE_register_all_ciphers(); | |
347 | ENGINE_register_all_digests(); | |
348 | /* If we add command-line options, this statement should be switchable. | |
349 | * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if | |
350 | * they weren't already initialised. */ | |
351 | /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */ | |
0b13e9f0 | 352 | #endif |
0e360199 BL |
353 | |
354 | for( ; ; ) | |
355 | { | |
a8a00498 | 356 | char line[4096]; |
0e360199 BL |
357 | char *p; |
358 | char *cipher; | |
359 | unsigned char *iv,*key,*plaintext,*ciphertext; | |
e6bd5e8a | 360 | int encdec; |
0e360199 | 361 | int kn,in,pn,cn; |
0e360199 | 362 | |
a8a00498 | 363 | if(!fgets((char *)line,sizeof line,f)) |
0e360199 | 364 | break; |
a8a00498 | 365 | if(line[0] == '#' || line[0] == '\n') |
0e360199 | 366 | continue; |
a8a00498 | 367 | p=line; |
5b46eee0 | 368 | cipher=sstrsep(&p,":"); |
0e360199 BL |
369 | key=ustrsep(&p,":"); |
370 | iv=ustrsep(&p,":"); | |
371 | plaintext=ustrsep(&p,":"); | |
e6bd5e8a RL |
372 | ciphertext=ustrsep(&p,":"); |
373 | if (p[-1] == '\n') { | |
374 | p[-1] = '\0'; | |
375 | encdec = -1; | |
376 | } else { | |
6631a7e7 | 377 | encdec = atoi(sstrsep(&p,"\n")); |
e6bd5e8a RL |
378 | } |
379 | ||
0e360199 | 380 | |
0e360199 BL |
381 | kn=convert(key); |
382 | in=convert(iv); | |
383 | pn=convert(plaintext); | |
384 | cn=convert(ciphertext); | |
385 | ||
e6bd5e8a | 386 | if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec) |
4897dc40 | 387 | && !test_digest(cipher,plaintext,pn,ciphertext,cn)) |
0e360199 | 388 | { |
4897dc40 | 389 | fprintf(stderr,"Can't find %s\n",cipher); |
55f78baf | 390 | EXIT(3); |
0e360199 BL |
391 | } |
392 | } | |
393 | ||
0b13e9f0 | 394 | #ifndef OPENSSL_NO_ENGINE |
997a54c9 | 395 | ENGINE_cleanup(); |
0b13e9f0 | 396 | #endif |
997a54c9 GT |
397 | EVP_cleanup(); |
398 | CRYPTO_cleanup_all_ex_data(); | |
399 | ERR_remove_state(0); | |
400 | ERR_free_strings(); | |
401 | CRYPTO_mem_leaks_fp(stderr); | |
0e360199 BL |
402 | |
403 | return 0; | |
404 | } |