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.
52 #include <openssl/evp.h>
53 #include <openssl/engine.h>
54 #include <openssl/conf.h>
56 static void hexdump(FILE *f,const char *title,const unsigned char *s,int l)
60 fprintf(f,"%s",title);
64 fprintf(f,"\n%04x",n);
65 fprintf(f," %02x",s[n]);
70 static int convert(unsigned char *s)
74 for(d=s ; *s ; s+=2,++d)
80 fprintf(stderr,"Odd number of hex digits!");
83 sscanf((char *)s,"%2x",&n);
89 static char *sstrsep(char **string, const char *delim)
92 char *token = *string;
97 memset(isdelim, 0, 256);
102 isdelim[(unsigned char)(*delim)] = 1;
106 while (!isdelim[(unsigned char)(**string)])
120 static unsigned char *ustrsep(char **p,const char *sep)
121 { return (unsigned char *)sstrsep((char **)p,sep); }
123 static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
124 const unsigned char *iv,int in,
125 const unsigned char *plaintext,int pn,
126 const unsigned char *ciphertext,int cn)
129 unsigned char out[4096];
132 printf("Testing cipher %s\n",EVP_CIPHER_name(c));
133 hexdump(stdout,"Key",key,kn);
135 hexdump(stdout,"IV",iv,in);
136 hexdump(stdout,"Plaintext",plaintext,pn);
137 hexdump(stdout,"Ciphertext",ciphertext,cn);
141 fprintf(stderr,"Key length doesn't match, got %d expected %d\n",kn,
145 EVP_CIPHER_CTX_init(&ctx);
146 if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv))
148 fprintf(stderr,"EncryptInit failed\n");
151 EVP_CIPHER_CTX_set_padding(&ctx,0);
153 if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn))
155 fprintf(stderr,"Encrypt failed\n");
158 if(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2))
160 fprintf(stderr,"EncryptFinal failed\n");
166 fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n",
171 if(memcmp(out,ciphertext,cn))
173 fprintf(stderr,"Ciphertext mismatch\n");
174 hexdump(stderr,"Got",out,cn);
175 hexdump(stderr,"Expected",ciphertext,cn);
179 if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv))
181 fprintf(stderr,"DecryptInit failed\n");
184 EVP_CIPHER_CTX_set_padding(&ctx,0);
186 if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,pn))
188 fprintf(stderr,"Decrypt failed\n");
191 if(!EVP_DecryptFinal_ex(&ctx,out+outl,&outl2))
193 fprintf(stderr,"DecryptFinal failed\n");
199 fprintf(stderr,"Plaintext length mismatch got %d expected %d\n",
204 if(memcmp(out,plaintext,cn))
206 fprintf(stderr,"Plaintext mismatch\n");
207 hexdump(stderr,"Got",out,cn);
208 hexdump(stderr,"Expected",plaintext,cn);
212 EVP_CIPHER_CTX_cleanup(&ctx);
217 static int test_cipher(const char *cipher,const unsigned char *key,int kn,
218 const unsigned char *iv,int in,
219 const unsigned char *plaintext,int pn,
220 const unsigned char *ciphertext,int cn)
224 c=EVP_get_cipherbyname(cipher);
228 test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn);
233 static int test_digest(const char *digest,
234 const unsigned char *plaintext,int pn,
235 const unsigned char *ciphertext, unsigned int cn)
239 unsigned char md[EVP_MAX_MD_SIZE];
242 d=EVP_get_digestbyname(digest);
246 printf("Testing digest %s\n",EVP_MD_name(d));
247 hexdump(stdout,"Plaintext",plaintext,pn);
248 hexdump(stdout,"Digest",ciphertext,cn);
250 EVP_MD_CTX_init(&ctx);
251 if(!EVP_DigestInit_ex(&ctx,d, NULL))
253 fprintf(stderr,"DigestInit failed\n");
256 if(!EVP_DigestUpdate(&ctx,plaintext,pn))
258 fprintf(stderr,"DigestUpdate failed\n");
261 if(!EVP_DigestFinal_ex(&ctx,md,&mdn))
263 fprintf(stderr,"DigestFinal failed\n");
266 EVP_MD_CTX_cleanup(&ctx);
270 fprintf(stderr,"Digest length mismatch, got %d expected %d\n",mdn,cn);
274 if(memcmp(md,ciphertext,cn))
276 fprintf(stderr,"Digest mismatch\n");
277 hexdump(stderr,"Got",md,cn);
278 hexdump(stderr,"Expected",ciphertext,cn);
284 EVP_MD_CTX_cleanup(&ctx);
289 int main(int argc,char **argv)
291 const char *szTestFile;
296 fprintf(stderr,"%s <test file>\n",argv[0]);
299 CRYPTO_malloc_debug_init();
300 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
301 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
305 f=fopen(szTestFile,"r");
312 /* Load up the software EVP_CIPHER and EVP_MD definitions */
313 OpenSSL_add_all_ciphers();
314 OpenSSL_add_all_digests();
315 /* Load all compiled-in ENGINEs */
316 ENGINE_load_builtin_engines();
320 /* Register all available ENGINE implementations of ciphers and digests.
321 * This could perhaps be changed to "ENGINE_register_all_complete()"? */
322 ENGINE_register_all_ciphers();
323 ENGINE_register_all_digests();
324 /* If we add command-line options, this statement should be switchable.
325 * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if
326 * they weren't already initialised. */
327 /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */
334 unsigned char *iv,*key,*plaintext,*ciphertext;
337 if(!fgets((char *)line,sizeof line,f))
339 if(line[0] == '#' || line[0] == '\n')
342 cipher=sstrsep(&p,":");
345 plaintext=ustrsep(&p,":");
346 ciphertext=ustrsep(&p,"\n");
350 pn=convert(plaintext);
351 cn=convert(ciphertext);
353 if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn)
354 && !test_digest(cipher,plaintext,pn,ciphertext,cn))
356 fprintf(stderr,"Can't find %s\n",cipher);
363 CRYPTO_cleanup_all_ex_data();
366 CRYPTO_mem_leaks_fp(stderr);