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/evp.h>
56 #include <openssl/engine.h>
57 #include <openssl/conf.h>
59 static void hexdump(FILE *f,const char *title,const unsigned char *s,int l)
63 fprintf(f,"%s",title);
67 fprintf(f,"\n%04x",n);
68 fprintf(f," %02x",s[n]);
73 static int convert(unsigned char *s)
77 for(d=s ; *s ; s+=2,++d)
83 fprintf(stderr,"Odd number of hex digits!");
86 sscanf((char *)s,"%2x",&n);
92 static char *sstrsep(char **string, const char *delim)
95 char *token = *string;
100 memset(isdelim, 0, 256);
105 isdelim[(unsigned char)(*delim)] = 1;
109 while (!isdelim[(unsigned char)(**string)])
123 static unsigned char *ustrsep(char **p,const char *sep)
124 { return (unsigned char *)sstrsep(p,sep); }
126 static int test1_exit(int ec)
131 static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
132 const unsigned char *iv,int in,
133 const unsigned char *plaintext,int pn,
134 const unsigned char *ciphertext,int cn,
138 unsigned char out[4096];
141 printf("Testing cipher %s%s\n",EVP_CIPHER_name(c),
142 (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
143 hexdump(stdout,"Key",key,kn);
145 hexdump(stdout,"IV",iv,in);
146 hexdump(stdout,"Plaintext",plaintext,pn);
147 hexdump(stdout,"Ciphertext",ciphertext,cn);
151 fprintf(stderr,"Key length doesn't match, got %d expected %d\n",kn,
155 EVP_CIPHER_CTX_init(&ctx);
158 if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv))
160 fprintf(stderr,"EncryptInit failed\n");
163 EVP_CIPHER_CTX_set_padding(&ctx,0);
165 if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn))
167 fprintf(stderr,"Encrypt failed\n");
170 if(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2))
172 fprintf(stderr,"EncryptFinal failed\n");
178 fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n",
183 if(memcmp(out,ciphertext,cn))
185 fprintf(stderr,"Ciphertext mismatch\n");
186 hexdump(stderr,"Got",out,cn);
187 hexdump(stderr,"Expected",ciphertext,cn);
194 if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv))
196 fprintf(stderr,"DecryptInit failed\n");
199 EVP_CIPHER_CTX_set_padding(&ctx,0);
201 if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn))
203 fprintf(stderr,"Decrypt failed\n");
206 if(!EVP_DecryptFinal_ex(&ctx,out+outl,&outl2))
208 fprintf(stderr,"DecryptFinal failed\n");
214 fprintf(stderr,"Plaintext length mismatch got %d expected %d\n",
219 if(memcmp(out,plaintext,cn))
221 fprintf(stderr,"Plaintext mismatch\n");
222 hexdump(stderr,"Got",out,cn);
223 hexdump(stderr,"Expected",plaintext,cn);
228 EVP_CIPHER_CTX_cleanup(&ctx);
233 static int test_cipher(const char *cipher,const unsigned char *key,int kn,
234 const unsigned char *iv,int in,
235 const unsigned char *plaintext,int pn,
236 const unsigned char *ciphertext,int cn,
241 c=EVP_get_cipherbyname(cipher);
245 test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec);
250 static int test_digest(const char *digest,
251 const unsigned char *plaintext,int pn,
252 const unsigned char *ciphertext, unsigned int cn)
256 unsigned char md[EVP_MAX_MD_SIZE];
259 d=EVP_get_digestbyname(digest);
263 printf("Testing digest %s\n",EVP_MD_name(d));
264 hexdump(stdout,"Plaintext",plaintext,pn);
265 hexdump(stdout,"Digest",ciphertext,cn);
267 EVP_MD_CTX_init(&ctx);
268 if(!EVP_DigestInit_ex(&ctx,d, NULL))
270 fprintf(stderr,"DigestInit failed\n");
273 if(!EVP_DigestUpdate(&ctx,plaintext,pn))
275 fprintf(stderr,"DigestUpdate failed\n");
278 if(!EVP_DigestFinal_ex(&ctx,md,&mdn))
280 fprintf(stderr,"DigestFinal failed\n");
283 EVP_MD_CTX_cleanup(&ctx);
287 fprintf(stderr,"Digest length mismatch, got %d expected %d\n",mdn,cn);
291 if(memcmp(md,ciphertext,cn))
293 fprintf(stderr,"Digest mismatch\n");
294 hexdump(stderr,"Got",md,cn);
295 hexdump(stderr,"Expected",ciphertext,cn);
301 EVP_MD_CTX_cleanup(&ctx);
306 int main(int argc,char **argv)
308 const char *szTestFile;
313 fprintf(stderr,"%s <test file>\n",argv[0]);
316 CRYPTO_malloc_debug_init();
317 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
318 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
322 f=fopen(szTestFile,"r");
329 /* Load up the software EVP_CIPHER and EVP_MD definitions */
330 OpenSSL_add_all_ciphers();
331 OpenSSL_add_all_digests();
332 /* Load all compiled-in ENGINEs */
333 ENGINE_load_builtin_engines();
337 /* Register all available ENGINE implementations of ciphers and digests.
338 * This could perhaps be changed to "ENGINE_register_all_complete()"? */
339 ENGINE_register_all_ciphers();
340 ENGINE_register_all_digests();
341 /* If we add command-line options, this statement should be switchable.
342 * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if
343 * they weren't already initialised. */
344 /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */
351 unsigned char *iv,*key,*plaintext,*ciphertext;
355 if(!fgets((char *)line,sizeof line,f))
357 if(line[0] == '#' || line[0] == '\n')
360 cipher=sstrsep(&p,":");
363 plaintext=ustrsep(&p,":");
364 ciphertext=ustrsep(&p,":");
369 encdec = atoi(sstrsep(&p,"\n"));
375 pn=convert(plaintext);
376 cn=convert(ciphertext);
378 if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec)
379 && !test_digest(cipher,plaintext,pn,ciphertext,cn))
381 fprintf(stderr,"Can't find %s\n",cipher);
388 CRYPTO_cleanup_all_ex_data();
391 CRYPTO_mem_leaks_fp(stderr);