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 static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
137 const unsigned char *iv,int in,
138 const unsigned char *plaintext,int pn,
139 const unsigned char *ciphertext,int cn,
140 const unsigned char *aad,int an,
141 const unsigned char *tag,int tn,
145 unsigned char out[4096];
148 printf("Testing cipher %s%s\n",EVP_CIPHER_name(c),
149 (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
150 hexdump(stdout,"Key",key,kn);
152 hexdump(stdout,"IV",iv,in);
153 hexdump(stdout,"Plaintext",plaintext,pn);
154 hexdump(stdout,"Ciphertext",ciphertext,cn);
156 hexdump(stdout,"AAD",aad,an);
158 hexdump(stdout,"Tag",tag,tn);
159 mode = EVP_CIPHER_mode(c);
160 if(kn != EVP_CIPHER_key_length(c))
162 fprintf(stderr,"Key length doesn't match, got %d expected %lu\n",kn,
163 (unsigned long)EVP_CIPHER_key_length(c));
166 EVP_CIPHER_CTX_init(&ctx);
169 if (mode == EVP_CIPH_GCM_MODE)
171 if(!EVP_EncryptInit_ex(&ctx,c,NULL,NULL,NULL))
173 fprintf(stderr,"EncryptInit failed\n");
174 ERR_print_errors_fp(stderr);
177 if(!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, in, NULL))
179 fprintf(stderr,"IV length set failed\n");
180 ERR_print_errors_fp(stderr);
183 if(!EVP_EncryptInit_ex(&ctx,NULL,NULL,key,iv))
185 fprintf(stderr,"Key/IV set failed\n");
186 ERR_print_errors_fp(stderr);
189 if (an && !EVP_EncryptUpdate(&ctx,NULL,&outl,aad,an))
191 fprintf(stderr,"AAD set failed\n");
192 ERR_print_errors_fp(stderr);
196 else if (mode == EVP_CIPH_CCM_MODE)
198 if(!EVP_EncryptInit_ex(&ctx,c,NULL,NULL,NULL))
200 fprintf(stderr,"EncryptInit failed\n");
201 ERR_print_errors_fp(stderr);
204 if(!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_IVLEN, in, NULL))
206 fprintf(stderr,"IV length set failed\n");
207 ERR_print_errors_fp(stderr);
210 if(!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_TAG, tn, NULL))
212 fprintf(stderr,"Tag length set failed\n");
213 ERR_print_errors_fp(stderr);
216 if(!EVP_EncryptInit_ex(&ctx,NULL,NULL,key,iv))
218 fprintf(stderr,"Key/IV set failed\n");
219 ERR_print_errors_fp(stderr);
222 if (!EVP_EncryptUpdate(&ctx,NULL,&outl,NULL,pn))
224 fprintf(stderr,"Plaintext length set failed\n");
225 ERR_print_errors_fp(stderr);
228 if (an && !EVP_EncryptUpdate(&ctx,NULL,&outl,aad,an))
230 fprintf(stderr,"AAD set failed\n");
231 ERR_print_errors_fp(stderr);
235 else if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv))
237 fprintf(stderr,"EncryptInit failed\n");
238 ERR_print_errors_fp(stderr);
241 EVP_CIPHER_CTX_set_padding(&ctx,0);
243 if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn))
245 fprintf(stderr,"Encrypt failed\n");
246 ERR_print_errors_fp(stderr);
249 if(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2))
251 fprintf(stderr,"EncryptFinal failed\n");
252 ERR_print_errors_fp(stderr);
258 fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n",
263 if(memcmp(out,ciphertext,cn))
265 fprintf(stderr,"Ciphertext mismatch\n");
266 hexdump(stderr,"Got",out,cn);
267 hexdump(stderr,"Expected",ciphertext,cn);
270 if (mode == EVP_CIPH_GCM_MODE || mode == EVP_CIPH_CCM_MODE)
272 unsigned char rtag[16];
273 /* Note: EVP_CTRL_CCM_GET_TAG has same value as
274 * EVP_CTRL_GCM_GET_TAG
276 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_GET_TAG, tn, rtag))
278 fprintf(stderr,"Get tag failed\n");
279 ERR_print_errors_fp(stderr);
282 if (memcmp(rtag, tag, tn))
284 fprintf(stderr,"Tag mismatch\n");
285 hexdump(stderr,"Got",rtag,tn);
286 hexdump(stderr,"Expected",tag,tn);
294 if (mode == EVP_CIPH_GCM_MODE)
296 if(!EVP_DecryptInit_ex(&ctx,c,NULL,NULL,NULL))
298 fprintf(stderr,"EncryptInit failed\n");
299 ERR_print_errors_fp(stderr);
302 if(!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, in, NULL))
304 fprintf(stderr,"IV length set failed\n");
305 ERR_print_errors_fp(stderr);
308 if(!EVP_DecryptInit_ex(&ctx,NULL,NULL,key,iv))
310 fprintf(stderr,"Key/IV set failed\n");
311 ERR_print_errors_fp(stderr);
314 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_TAG, tn, (void *)tag))
316 fprintf(stderr,"Set tag failed\n");
317 ERR_print_errors_fp(stderr);
320 if (an && !EVP_DecryptUpdate(&ctx,NULL,&outl,aad,an))
322 fprintf(stderr,"AAD set failed\n");
323 ERR_print_errors_fp(stderr);
327 else if (mode == EVP_CIPH_CCM_MODE)
329 if(!EVP_DecryptInit_ex(&ctx,c,NULL,NULL,NULL))
331 fprintf(stderr,"DecryptInit failed\n");
332 ERR_print_errors_fp(stderr);
335 if(!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_IVLEN, in, NULL))
337 fprintf(stderr,"IV length set failed\n");
338 ERR_print_errors_fp(stderr);
341 if(!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_TAG, tn, (void *)tag))
343 fprintf(stderr,"Tag length set failed\n");
344 ERR_print_errors_fp(stderr);
347 if(!EVP_DecryptInit_ex(&ctx,NULL,NULL,key,iv))
349 fprintf(stderr,"Key/Nonce set failed\n");
350 ERR_print_errors_fp(stderr);
353 if (!EVP_DecryptUpdate(&ctx,NULL,&outl,NULL,pn))
355 fprintf(stderr,"Plaintext length set failed\n");
356 ERR_print_errors_fp(stderr);
359 if (an && !EVP_EncryptUpdate(&ctx,NULL,&outl,aad,an))
361 fprintf(stderr,"AAD set failed\n");
362 ERR_print_errors_fp(stderr);
366 else if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv))
368 fprintf(stderr,"DecryptInit failed\n");
369 ERR_print_errors_fp(stderr);
372 EVP_CIPHER_CTX_set_padding(&ctx,0);
374 if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn))
376 fprintf(stderr,"Decrypt failed\n");
377 ERR_print_errors_fp(stderr);
380 if(mode != EVP_CIPH_CCM_MODE && !EVP_DecryptFinal_ex(&ctx,out+outl,&outl2))
382 fprintf(stderr,"DecryptFinal failed\n");
383 ERR_print_errors_fp(stderr);
389 fprintf(stderr,"Plaintext length mismatch got %d expected %d\n",
394 if(memcmp(out,plaintext,pn))
396 fprintf(stderr,"Plaintext mismatch\n");
397 hexdump(stderr,"Got",out,pn);
398 hexdump(stderr,"Expected",plaintext,pn);
403 EVP_CIPHER_CTX_cleanup(&ctx);
408 static int test_cipher(const char *cipher,const unsigned char *key,int kn,
409 const unsigned char *iv,int in,
410 const unsigned char *plaintext,int pn,
411 const unsigned char *ciphertext,int cn,
412 const unsigned char *aad,int an,
413 const unsigned char *tag,int tn,
418 c=EVP_get_cipherbyname(cipher);
422 test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,aad,an,tag,tn,encdec);
427 static int test_digest(const char *digest,
428 const unsigned char *plaintext,int pn,
429 const unsigned char *ciphertext, unsigned int cn)
433 unsigned char md[EVP_MAX_MD_SIZE];
436 d=EVP_get_digestbyname(digest);
440 printf("Testing digest %s\n",EVP_MD_name(d));
441 hexdump(stdout,"Plaintext",plaintext,pn);
442 hexdump(stdout,"Digest",ciphertext,cn);
444 EVP_MD_CTX_init(&ctx);
445 if(!EVP_DigestInit_ex(&ctx,d, NULL))
447 fprintf(stderr,"DigestInit failed\n");
448 ERR_print_errors_fp(stderr);
451 if(!EVP_DigestUpdate(&ctx,plaintext,pn))
453 fprintf(stderr,"DigestUpdate failed\n");
454 ERR_print_errors_fp(stderr);
457 if(!EVP_DigestFinal_ex(&ctx,md,&mdn))
459 fprintf(stderr,"DigestFinal failed\n");
460 ERR_print_errors_fp(stderr);
463 EVP_MD_CTX_cleanup(&ctx);
467 fprintf(stderr,"Digest length mismatch, got %d expected %d\n",mdn,cn);
471 if(memcmp(md,ciphertext,cn))
473 fprintf(stderr,"Digest mismatch\n");
474 hexdump(stderr,"Got",md,cn);
475 hexdump(stderr,"Expected",ciphertext,cn);
481 EVP_MD_CTX_cleanup(&ctx);
486 int main(int argc,char **argv)
488 const char *szTestFile;
493 fprintf(stderr,"%s <test file>\n",argv[0]);
496 CRYPTO_malloc_debug_init();
497 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
498 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
502 f=fopen(szTestFile,"r");
509 /* Load up the software EVP_CIPHER and EVP_MD definitions */
510 OpenSSL_add_all_ciphers();
511 OpenSSL_add_all_digests();
512 #ifndef OPENSSL_NO_ENGINE
513 /* Load all compiled-in ENGINEs */
514 ENGINE_load_builtin_engines();
519 #ifndef OPENSSL_NO_ENGINE
520 /* Register all available ENGINE implementations of ciphers and digests.
521 * This could perhaps be changed to "ENGINE_register_all_complete()"? */
522 ENGINE_register_all_ciphers();
523 ENGINE_register_all_digests();
524 /* If we add command-line options, this statement should be switchable.
525 * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if
526 * they weren't already initialised. */
527 /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */
535 unsigned char *iv,*key,*plaintext,*ciphertext,*aad,*tag;
541 if(!fgets((char *)line,sizeof line,f))
543 if(line[0] == '#' || line[0] == '\n')
546 cipher=sstrsep(&p,":");
549 plaintext=ustrsep(&p,":");
550 ciphertext=ustrsep(&p,":");
564 if (p [-1] == '\n') {
568 encdec = atoi(sstrsep(&p,"\n"));
573 pn=convert(plaintext);
574 cn=convert(ciphertext);
580 if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn,aad,an,tag,tn,encdec)
581 && !test_digest(cipher,plaintext,pn,ciphertext,cn))
583 #ifdef OPENSSL_NO_AES
584 if (strstr(cipher, "AES") == cipher)
586 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
590 #ifdef OPENSSL_NO_DES
591 if (strstr(cipher, "DES") == cipher)
593 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
597 #ifdef OPENSSL_NO_RC4
598 if (strstr(cipher, "RC4") == cipher)
600 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
604 #ifdef OPENSSL_NO_CAMELLIA
605 if (strstr(cipher, "CAMELLIA") == cipher)
607 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
611 #ifdef OPENSSL_NO_SEED
612 if (strstr(cipher, "SEED") == cipher)
614 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
618 fprintf(stderr,"Can't find %s\n",cipher);
624 #ifndef OPENSSL_NO_ENGINE
628 CRYPTO_cleanup_all_ex_data();
629 ERR_remove_thread_state(NULL);
631 CRYPTO_mem_leaks_fp(stderr);