Add AES tests.
[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 **p,const char *sep)
89     { return (unsigned char *)strsep((char **)p,sep); }
90
91 static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
92                   const unsigned char *iv,int in,
93                   const unsigned char *plaintext,int pn,
94                   const unsigned char *ciphertext,int cn)
95     {
96     EVP_CIPHER_CTX ctx;
97     unsigned char out[4096];
98     int outl,outl2;
99
100     printf("Testing cipher %s\n",EVP_CIPHER_name(c));
101     hexdump(stdout,"Key",key,kn);
102     if(in)
103         hexdump(stdout,"IV",iv,in);
104     hexdump(stdout,"Plaintext",plaintext,pn);
105     hexdump(stdout,"Ciphertext",ciphertext,cn);
106     
107     
108     if(kn != c->key_len)
109         {
110         fprintf(stderr,"Key length doesn't match, got %d expected %d\n",kn,
111                 c->key_len);
112         exit(5);
113         }
114
115     if(!EVP_EncryptInit(&ctx,c,key,iv))
116         {
117         fprintf(stderr,"EncryptInit failed\n");
118         exit(10);
119         }
120     EVP_CIPHER_CTX_set_padding(&ctx,0);
121
122     if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn))
123         {
124         fprintf(stderr,"Encrypt failed\n");
125         exit(6);
126         }
127     if(!EVP_EncryptFinal(&ctx,out+outl,&outl2))
128         {
129         fprintf(stderr,"EncryptFinal failed\n");
130         exit(7);
131         }
132
133     if(outl+outl2 != cn)
134         {
135         fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n",
136                 outl+outl2,cn);
137         exit(8);
138         }
139
140     if(memcmp(out,ciphertext,cn))
141         {
142         fprintf(stderr,"Ciphertext mismatch\n");
143         hexdump(stderr,"Got",out,cn);
144         hexdump(stderr,"Expected",ciphertext,cn);
145         exit(9);
146         }
147
148     if(!EVP_DecryptInit(&ctx,c,key,iv))
149         {
150         fprintf(stderr,"DecryptInit failed\n");
151         exit(10);
152         }
153     EVP_CIPHER_CTX_set_padding(&ctx,0);
154
155     if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,pn))
156         {
157         fprintf(stderr,"Decrypt failed\n");
158         exit(6);
159         }
160     if(!EVP_DecryptFinal(&ctx,out+outl,&outl2))
161         {
162         fprintf(stderr,"DecryptFinal failed\n");
163         exit(7);
164         }
165
166     if(outl+outl2 != cn)
167         {
168         fprintf(stderr,"Plaintext length mismatch got %d expected %d\n",
169                 outl+outl2,cn);
170         exit(8);
171         }
172
173     if(memcmp(out,plaintext,cn))
174         {
175         fprintf(stderr,"Plaintext mismatch\n");
176         hexdump(stderr,"Got",out,cn);
177         hexdump(stderr,"Expected",plaintext,cn);
178         exit(9);
179         }
180
181     printf("\n");
182     }
183
184 int main(int argc,char **argv)
185     {
186     const char *szTestFile;
187     FILE *f;
188
189     if(argc != 2)
190         {
191         fprintf(stderr,"%s <test file>\n",argv[0]);
192         exit(1);
193         }
194
195     szTestFile=argv[1];
196
197     f=fopen(szTestFile,"r");
198     if(!f)
199         {
200         perror(szTestFile);
201         exit(2);
202         }
203
204     OpenSSL_add_all_ciphers();
205     ENGINE_load_builtin_engines();
206
207     for( ; ; )
208         {
209         char line[4096];
210         char *p;
211         char *cipher;
212         unsigned char *iv,*key,*plaintext,*ciphertext;
213         const EVP_CIPHER *c;
214         int kn,in,pn,cn;
215         ENGINE *e;
216
217         if(!fgets((char *)line,sizeof line,f))
218             break;
219         if(line[0] == '#' || line[0] == '\n')
220             continue;
221         p=line;
222         cipher=strsep(&p,":");  
223         key=ustrsep(&p,":");
224         iv=ustrsep(&p,":");
225         plaintext=ustrsep(&p,":");
226         ciphertext=ustrsep(&p,"\n");
227
228         c=EVP_get_cipherbyname(cipher);
229         if(!c)
230             {
231             fprintf(stderr,"Can't find cipher %s!\n",cipher);
232             exit(3);
233             }
234
235         kn=convert(key);
236         in=convert(iv);
237         pn=convert(plaintext);
238         cn=convert(ciphertext);
239
240         test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn);
241
242         for(e=ENGINE_get_first() ; e ; e=ENGINE_get_next(e))
243             {
244             c=ENGINE_get_cipher_by_name(e,cipher);
245             if(!c)
246                 continue;
247             printf("Testing engine %s\n",ENGINE_get_name(e));
248
249             test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn);
250             }
251         }
252
253
254     return 0;
255     }