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