1 /* Written by Ben Laurie, 2001 */
3 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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.
55 #include <openssl/opensslconf.h>
56 #include <openssl/evp.h>
57 #ifndef OPENSSL_NO_ENGINE
58 #include <openssl/engine.h>
60 #include <openssl/err.h>
61 #include <openssl/conf.h>
63 static void hexdump(FILE *f,const char *title,const unsigned char *s,int l)
67 fprintf(f,"%s",title);
71 fprintf(f,"\n%04x",n);
72 fprintf(f," %02x",s[n]);
77 static int convert(unsigned char *s)
81 for(d=s ; *s ; s+=2,++d)
87 fprintf(stderr,"Odd number of hex digits!");
90 sscanf((char *)s,"%2x",&n);
96 static char *sstrsep(char **string, const char *delim)
99 char *token = *string;
104 memset(isdelim, 0, 256);
109 isdelim[(unsigned char)(*delim)] = 1;
113 while (!isdelim[(unsigned char)(**string)])
127 static unsigned char *ustrsep(char **p,const char *sep)
128 { return (unsigned char *)sstrsep(p,sep); }
130 static int test1_exit(int ec)
133 return(0); /* To keep some compilers quiet */
136 /* Test copying of contexts */
137 static void test_ctx_replace(EVP_CIPHER_CTX **pctx)
139 /* Make copy of context and replace original */
140 EVP_CIPHER_CTX *ctx_copy;
141 ctx_copy = EVP_CIPHER_CTX_new();
142 EVP_CIPHER_CTX_copy(ctx_copy, *pctx);
143 EVP_CIPHER_CTX_free(*pctx);
147 static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
148 const unsigned char *iv,int in,
149 const unsigned char *plaintext,int pn,
150 const unsigned char *ciphertext,int cn,
151 const unsigned char *aad,int an,
152 const unsigned char *tag,int tn,
155 EVP_CIPHER_CTX *ctx = NULL;
156 unsigned char out[4096];
159 printf("Testing cipher %s%s\n",EVP_CIPHER_name(c),
160 (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
161 hexdump(stdout,"Key",key,kn);
163 hexdump(stdout,"IV",iv,in);
164 hexdump(stdout,"Plaintext",plaintext,pn);
165 hexdump(stdout,"Ciphertext",ciphertext,cn);
167 hexdump(stdout,"AAD",aad,an);
169 hexdump(stdout,"Tag",tag,tn);
170 mode = EVP_CIPHER_mode(c);
171 if(kn != EVP_CIPHER_key_length(c))
173 fprintf(stderr,"Key length doesn't match, got %d expected %lu\n",kn,
174 (unsigned long)EVP_CIPHER_key_length(c));
177 ctx = EVP_CIPHER_CTX_new();
178 EVP_CIPHER_CTX_set_flags(ctx,EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
181 if ((mode == EVP_CIPH_GCM_MODE) || (mode == EVP_CIPH_OCB_MODE))
183 if(!EVP_EncryptInit_ex(ctx,c,NULL,NULL,NULL))
185 fprintf(stderr,"EncryptInit failed\n");
186 ERR_print_errors_fp(stderr);
189 if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_IVLEN, in, NULL))
191 fprintf(stderr,"IV length set failed\n");
192 ERR_print_errors_fp(stderr);
195 if((mode == EVP_CIPH_OCB_MODE) &&
196 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_OCB_SET_TAGLEN, tn, NULL))
198 fprintf(stderr,"Tag length set failed\n");
199 ERR_print_errors_fp(stderr);
202 if(!EVP_EncryptInit_ex(ctx,NULL,NULL,key,iv))
204 fprintf(stderr,"Key/IV set failed\n");
205 ERR_print_errors_fp(stderr);
208 if (an && !EVP_EncryptUpdate(ctx,NULL,&outl,aad,an))
210 fprintf(stderr,"AAD set failed\n");
211 ERR_print_errors_fp(stderr);
215 else if (mode == EVP_CIPH_CCM_MODE)
217 if(!EVP_EncryptInit_ex(ctx,c,NULL,NULL,NULL))
219 fprintf(stderr,"EncryptInit failed\n");
220 ERR_print_errors_fp(stderr);
223 if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, in, NULL))
225 fprintf(stderr,"IV length set failed\n");
226 ERR_print_errors_fp(stderr);
229 if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tn, NULL))
231 fprintf(stderr,"Tag length set failed\n");
232 ERR_print_errors_fp(stderr);
235 if(!EVP_EncryptInit_ex(ctx,NULL,NULL,key,iv))
237 fprintf(stderr,"Key/IV set failed\n");
238 ERR_print_errors_fp(stderr);
241 if (!EVP_EncryptUpdate(ctx,NULL,&outl,NULL,pn))
243 fprintf(stderr,"Plaintext length set failed\n");
244 ERR_print_errors_fp(stderr);
247 if (an && !EVP_EncryptUpdate(ctx,NULL,&outl,aad,an))
249 fprintf(stderr,"AAD set failed\n");
250 ERR_print_errors_fp(stderr);
254 else if (mode == EVP_CIPH_WRAP_MODE)
256 if(!EVP_EncryptInit_ex(ctx,c,NULL,key,in ? iv : NULL))
258 fprintf(stderr,"EncryptInit failed\n");
259 ERR_print_errors_fp(stderr);
263 else if(!EVP_EncryptInit_ex(ctx,c,NULL,key,iv))
265 fprintf(stderr,"EncryptInit failed\n");
266 ERR_print_errors_fp(stderr);
269 EVP_CIPHER_CTX_set_padding(ctx,0);
271 test_ctx_replace(&ctx);
273 if(!EVP_EncryptUpdate(ctx,out,&outl,plaintext,pn))
275 fprintf(stderr,"Encrypt failed\n");
276 ERR_print_errors_fp(stderr);
279 if(!EVP_EncryptFinal_ex(ctx,out+outl,&outl2))
281 fprintf(stderr,"EncryptFinal failed\n");
282 ERR_print_errors_fp(stderr);
288 fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n",
293 if(memcmp(out,ciphertext,cn))
295 fprintf(stderr,"Ciphertext mismatch\n");
296 hexdump(stderr,"Got",out,cn);
297 hexdump(stderr,"Expected",ciphertext,cn);
300 if ((mode == EVP_CIPH_GCM_MODE) || (mode == EVP_CIPH_OCB_MODE)
301 || (mode == EVP_CIPH_CCM_MODE))
303 unsigned char rtag[16];
305 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GET_TAG, tn, rtag))
307 fprintf(stderr,"Get tag failed\n");
308 ERR_print_errors_fp(stderr);
311 if (memcmp(rtag, tag, tn))
313 fprintf(stderr,"Tag mismatch\n");
314 hexdump(stderr,"Got",rtag,tn);
315 hexdump(stderr,"Expected",tag,tn);
323 if ((mode == EVP_CIPH_GCM_MODE) || (mode == EVP_CIPH_OCB_MODE))
325 if(!EVP_DecryptInit_ex(ctx,c,NULL,NULL,NULL))
327 fprintf(stderr,"DecryptInit failed\n");
328 ERR_print_errors_fp(stderr);
331 if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_IVLEN, in, NULL))
333 fprintf(stderr,"IV length set failed\n");
334 ERR_print_errors_fp(stderr);
337 if((mode == EVP_CIPH_OCB_MODE) &&
338 !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_OCB_SET_TAGLEN, tn, NULL))
340 fprintf(stderr,"Tag length set failed\n");
341 ERR_print_errors_fp(stderr);
344 if(!EVP_DecryptInit_ex(ctx,NULL,NULL,key,iv))
346 fprintf(stderr,"Key/IV set failed\n");
347 ERR_print_errors_fp(stderr);
350 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_TAG, tn, (void *)tag))
352 fprintf(stderr,"Set tag failed\n");
353 ERR_print_errors_fp(stderr);
356 if (an && !EVP_DecryptUpdate(ctx,NULL,&outl,aad,an))
358 fprintf(stderr,"AAD set failed\n");
359 ERR_print_errors_fp(stderr);
363 else if (mode == EVP_CIPH_CCM_MODE)
365 if(!EVP_DecryptInit_ex(ctx,c,NULL,NULL,NULL))
367 fprintf(stderr,"DecryptInit failed\n");
368 ERR_print_errors_fp(stderr);
371 if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, in, NULL))
373 fprintf(stderr,"IV length set failed\n");
374 ERR_print_errors_fp(stderr);
377 if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tn, (void *)tag))
379 fprintf(stderr,"Tag length set failed\n");
380 ERR_print_errors_fp(stderr);
383 if(!EVP_DecryptInit_ex(ctx,NULL,NULL,key,iv))
385 fprintf(stderr,"Key/Nonce set failed\n");
386 ERR_print_errors_fp(stderr);
389 if (!EVP_DecryptUpdate(ctx,NULL,&outl,NULL,pn))
391 fprintf(stderr,"Plaintext length set failed\n");
392 ERR_print_errors_fp(stderr);
395 if (an && !EVP_EncryptUpdate(ctx,NULL,&outl,aad,an))
397 fprintf(stderr,"AAD set failed\n");
398 ERR_print_errors_fp(stderr);
402 else if (mode == EVP_CIPH_WRAP_MODE)
404 if(!EVP_DecryptInit_ex(ctx,c,NULL,key,in ? iv : NULL))
406 fprintf(stderr,"EncryptInit failed\n");
407 ERR_print_errors_fp(stderr);
411 else if(!EVP_DecryptInit_ex(ctx,c,NULL,key,iv))
413 fprintf(stderr,"DecryptInit failed\n");
414 ERR_print_errors_fp(stderr);
417 EVP_CIPHER_CTX_set_padding(ctx,0);
419 test_ctx_replace(&ctx);
421 if(!EVP_DecryptUpdate(ctx,out,&outl,ciphertext,cn))
423 fprintf(stderr,"Decrypt failed\n");
424 ERR_print_errors_fp(stderr);
427 if(mode != EVP_CIPH_CCM_MODE && !EVP_DecryptFinal_ex(ctx,out+outl,&outl2))
429 fprintf(stderr,"DecryptFinal failed\n");
430 ERR_print_errors_fp(stderr);
436 fprintf(stderr,"Plaintext length mismatch got %d expected %d\n",
441 if(memcmp(out,plaintext,pn))
443 fprintf(stderr,"Plaintext mismatch\n");
444 hexdump(stderr,"Got",out,pn);
445 hexdump(stderr,"Expected",plaintext,pn);
450 EVP_CIPHER_CTX_free(ctx);
455 static int test_cipher(const char *cipher,const unsigned char *key,int kn,
456 const unsigned char *iv,int in,
457 const unsigned char *plaintext,int pn,
458 const unsigned char *ciphertext,int cn,
459 const unsigned char *aad,int an,
460 const unsigned char *tag,int tn,
465 c=EVP_get_cipherbyname(cipher);
469 test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,aad,an,tag,tn,encdec);
474 static int test_digest(const char *digest,
475 const unsigned char *plaintext,int pn,
476 const unsigned char *ciphertext, unsigned int cn)
480 unsigned char md[EVP_MAX_MD_SIZE];
483 d=EVP_get_digestbyname(digest);
487 printf("Testing digest %s\n",EVP_MD_name(d));
488 hexdump(stdout,"Plaintext",plaintext,pn);
489 hexdump(stdout,"Digest",ciphertext,cn);
491 EVP_MD_CTX_init(&ctx);
492 if(!EVP_DigestInit_ex(&ctx,d, NULL))
494 fprintf(stderr,"DigestInit failed\n");
495 ERR_print_errors_fp(stderr);
498 if(!EVP_DigestUpdate(&ctx,plaintext,pn))
500 fprintf(stderr,"DigestUpdate failed\n");
501 ERR_print_errors_fp(stderr);
504 if(!EVP_DigestFinal_ex(&ctx,md,&mdn))
506 fprintf(stderr,"DigestFinal failed\n");
507 ERR_print_errors_fp(stderr);
510 EVP_MD_CTX_cleanup(&ctx);
514 fprintf(stderr,"Digest length mismatch, got %d expected %d\n",mdn,cn);
518 if(memcmp(md,ciphertext,cn))
520 fprintf(stderr,"Digest mismatch\n");
521 hexdump(stderr,"Got",md,cn);
522 hexdump(stderr,"Expected",ciphertext,cn);
528 EVP_MD_CTX_cleanup(&ctx);
533 int main(int argc,char **argv)
535 const char *szTestFile;
540 fprintf(stderr,"%s <test file>\n",argv[0]);
543 CRYPTO_malloc_debug_init();
544 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
545 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
549 f=fopen(szTestFile,"r");
555 ERR_load_crypto_strings();
556 /* Load up the software EVP_CIPHER and EVP_MD definitions */
557 OpenSSL_add_all_ciphers();
558 OpenSSL_add_all_digests();
559 #ifndef OPENSSL_NO_ENGINE
560 /* Load all compiled-in ENGINEs */
561 ENGINE_load_builtin_engines();
566 #ifndef OPENSSL_NO_ENGINE
567 /* Register all available ENGINE implementations of ciphers and digests.
568 * This could perhaps be changed to "ENGINE_register_all_complete()"? */
569 ENGINE_register_all_ciphers();
570 ENGINE_register_all_digests();
571 /* If we add command-line options, this statement should be switchable.
572 * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if
573 * they weren't already initialised. */
574 /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */
582 unsigned char *iv,*key,*plaintext,*ciphertext,*aad,*tag;
588 if(!fgets((char *)line,sizeof line,f))
590 if(line[0] == '#' || line[0] == '\n')
593 cipher=sstrsep(&p,":");
596 plaintext=ustrsep(&p,":");
597 ciphertext=ustrsep(&p,":");
611 if (p [-1] == '\n') {
615 encdec = atoi(sstrsep(&p,"\n"));
620 pn=convert(plaintext);
621 cn=convert(ciphertext);
627 if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn,aad,an,tag,tn,encdec)
628 && !test_digest(cipher,plaintext,pn,ciphertext,cn))
630 #ifdef OPENSSL_NO_AES
631 if (strstr(cipher, "AES") == cipher)
633 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
637 #ifdef OPENSSL_NO_DES
638 if (strstr(cipher, "DES") == cipher)
640 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
644 #ifdef OPENSSL_NO_RC4
645 if (strstr(cipher, "RC4") == cipher)
647 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
651 #ifdef OPENSSL_NO_CAMELLIA
652 if (strstr(cipher, "CAMELLIA") == cipher)
654 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
658 #ifdef OPENSSL_NO_SEED
659 if (strstr(cipher, "SEED") == cipher)
661 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
665 fprintf(stderr,"Can't find %s\n",cipher);
671 #ifndef OPENSSL_NO_ENGINE
675 CRYPTO_cleanup_all_ex_data();
676 ERR_remove_thread_state(NULL);
678 CRYPTO_mem_leaks_fp(stderr);