strsep implementation to allow the file to compile on non-BSD systems
[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 unsigned char *ustrsep(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         while (!isdelim[(unsigned char)(**string)])
108             {
109             (*string)++;
110             }
111
112         if (**string)
113             {
114             **string = 0;
115             (*string)++;
116             }
117
118         return token;
119         }
120     }
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
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     ENGINE *e;
221
222     c=EVP_get_cipherbyname(cipher);
223     if(!c)
224         return 0;
225
226     test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn);
227
228     for(e=ENGINE_get_first() ; e ; e=ENGINE_get_next(e))
229         {
230         c=ENGINE_get_cipher_by_name(e,cipher);
231         if(!c)
232             continue;
233         printf("Testing engine %s\n",ENGINE_get_name(e));
234
235         test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn);
236         }
237
238     return 1;
239     }
240
241 static int test_digest(const char *digest,
242                        const unsigned char *plaintext,int pn,
243                        const unsigned char *ciphertext, int cn)
244     {
245     const EVP_MD *d;
246     EVP_MD_CTX ctx;
247     unsigned char md[EVP_MAX_MD_SIZE];
248     unsigned int mdn;
249
250     d=EVP_get_digestbyname(digest);
251     if(!d)
252         return 0;
253
254     printf("Testing digest %s\n",EVP_MD_name(d));
255     hexdump(stdout,"Plaintext",plaintext,pn);
256     hexdump(stdout,"Digest",ciphertext,cn);
257
258     EVP_MD_CTX_init(&ctx);
259     if(!EVP_DigestInit(&ctx,d))
260         {
261         fprintf(stderr,"DigestInit failed\n");
262         exit(100);
263         }
264     if(!EVP_DigestUpdate(&ctx,plaintext,pn))
265         {
266         fprintf(stderr,"DigestUpdate failed\n");
267         exit(101);
268         }
269     if(!EVP_DigestFinal(&ctx,md,&mdn))
270         {
271         fprintf(stderr,"DigestUpdate failed\n");
272         exit(101);
273         }
274
275     if(mdn != cn)
276         {
277         fprintf(stderr,"Digest length mismatch, got %d expected %d\n",mdn,cn);
278         exit(102);
279         }
280
281     if(memcmp(md,ciphertext,cn))
282         {
283         fprintf(stderr,"Digest mismatch\n");
284         hexdump(stderr,"Got",md,cn);
285         hexdump(stderr,"Expected",ciphertext,cn);
286         exit(103);
287         }
288
289     printf("\n");
290
291     return 1;
292     }
293
294 int main(int argc,char **argv)
295     {
296     const char *szTestFile;
297     FILE *f;
298
299     if(argc != 2)
300         {
301         fprintf(stderr,"%s <test file>\n",argv[0]);
302         exit(1);
303         }
304
305     szTestFile=argv[1];
306
307     f=fopen(szTestFile,"r");
308     if(!f)
309         {
310         perror(szTestFile);
311         exit(2);
312         }
313
314     OpenSSL_add_all_ciphers();
315     OpenSSL_add_all_digests();
316     ENGINE_load_builtin_engines();
317
318     for( ; ; )
319         {
320         char line[4096];
321         char *p;
322         char *cipher;
323         unsigned char *iv,*key,*plaintext,*ciphertext;
324         int kn,in,pn,cn;
325
326         if(!fgets((char *)line,sizeof line,f))
327             break;
328         if(line[0] == '#' || line[0] == '\n')
329             continue;
330         p=line;
331         cipher=strsep(&p,":");  
332         key=ustrsep(&p,":");
333         iv=ustrsep(&p,":");
334         plaintext=ustrsep(&p,":");
335         ciphertext=ustrsep(&p,"\n");
336
337         kn=convert(key);
338         in=convert(iv);
339         pn=convert(plaintext);
340         cn=convert(ciphertext);
341
342         if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn)
343            && !test_digest(cipher,plaintext,pn,ciphertext,cn))
344             {
345             fprintf(stderr,"Can't find %s\n",cipher);
346             exit(3);
347             }
348         }
349
350
351     return 0;
352     }