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>
55 static void hexdump(FILE *f,const char *title,const unsigned char *s,int l)
59 fprintf(f,"%s",title);
63 fprintf(f,"\n%04x",n);
64 fprintf(f," %02x",s[n]);
69 static int convert(unsigned char *s)
73 for(d=s ; *s ; s+=2,++d)
79 fprintf(stderr,"Odd number of hex digits!");
82 sscanf((char *)s,"%2x",&n);
88 static unsigned char *ustrsep(char **p,const char *sep)
89 { return (unsigned char *)strsep((char **)p,sep); }
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)
97 unsigned char out[4096];
100 printf("Testing cipher %s\n",EVP_CIPHER_name(c));
101 hexdump(stdout,"Key",key,kn);
103 hexdump(stdout,"IV",iv,in);
104 hexdump(stdout,"Plaintext",plaintext,pn);
105 hexdump(stdout,"Ciphertext",ciphertext,cn);
110 fprintf(stderr,"Key length doesn't match, got %d expected %d\n",kn,
115 if(!EVP_EncryptInit(&ctx,c,key,iv))
117 fprintf(stderr,"EncryptInit failed\n");
120 EVP_CIPHER_CTX_set_padding(&ctx,0);
122 if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn))
124 fprintf(stderr,"Encrypt failed\n");
127 if(!EVP_EncryptFinal(&ctx,out+outl,&outl2))
129 fprintf(stderr,"EncryptFinal failed\n");
135 fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n",
140 if(memcmp(out,ciphertext,cn))
142 fprintf(stderr,"Ciphertext mismatch\n");
143 hexdump(stderr,"Got",out,cn);
144 hexdump(stderr,"Expected",ciphertext,cn);
148 if(!EVP_DecryptInit(&ctx,c,key,iv))
150 fprintf(stderr,"DecryptInit failed\n");
153 EVP_CIPHER_CTX_set_padding(&ctx,0);
155 if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,pn))
157 fprintf(stderr,"Decrypt failed\n");
160 if(!EVP_DecryptFinal(&ctx,out+outl,&outl2))
162 fprintf(stderr,"DecryptFinal failed\n");
168 fprintf(stderr,"Plaintext length mismatch got %d expected %d\n",
173 if(memcmp(out,plaintext,cn))
175 fprintf(stderr,"Plaintext mismatch\n");
176 hexdump(stderr,"Got",out,cn);
177 hexdump(stderr,"Expected",plaintext,cn);
184 int main(int argc,char **argv)
186 const char *szTestFile;
191 fprintf(stderr,"%s <test file>\n",argv[0]);
197 f=fopen(szTestFile,"r");
204 OpenSSL_add_all_ciphers();
205 ENGINE_load_builtin_engines();
212 unsigned char *iv,*key,*plaintext,*ciphertext;
217 if(!fgets((char *)line,sizeof line,f))
219 if(line[0] == '#' || line[0] == '\n')
222 cipher=strsep(&p,":");
225 plaintext=ustrsep(&p,":");
226 ciphertext=ustrsep(&p,"\n");
228 c=EVP_get_cipherbyname(cipher);
231 fprintf(stderr,"Can't find cipher %s!\n",cipher);
237 pn=convert(plaintext);
238 cn=convert(ciphertext);
240 test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn);
242 for(e=ENGINE_get_first() ; e ; e=ENGINE_get_next(e))
244 c=ENGINE_get_cipher_by_name(e,cipher);
247 printf("Testing engine %s\n",ENGINE_get_name(e));
249 test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn);