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