summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/evp/evp_test.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/evp/evp_test.c143
1 files changed, 80 insertions, 63 deletions
diff --git a/src/lib/libcrypto/evp/evp_test.c b/src/lib/libcrypto/evp/evp_test.c
index decd0713d6..90294ef686 100644
--- a/src/lib/libcrypto/evp/evp_test.c
+++ b/src/lib/libcrypto/evp/evp_test.c
@@ -118,18 +118,20 @@ static char *sstrsep(char **string, const char *delim)
118 } 118 }
119 119
120static unsigned char *ustrsep(char **p,const char *sep) 120static unsigned char *ustrsep(char **p,const char *sep)
121 { return (unsigned char *)sstrsep((char **)p,sep); } 121 { return (unsigned char *)sstrsep(p,sep); }
122 122
123static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn, 123static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
124 const unsigned char *iv,int in, 124 const unsigned char *iv,int in,
125 const unsigned char *plaintext,int pn, 125 const unsigned char *plaintext,int pn,
126 const unsigned char *ciphertext,int cn) 126 const unsigned char *ciphertext,int cn,
127 int encdec)
127 { 128 {
128 EVP_CIPHER_CTX ctx; 129 EVP_CIPHER_CTX ctx;
129 unsigned char out[4096]; 130 unsigned char out[4096];
130 int outl,outl2; 131 int outl,outl2;
131 132
132 printf("Testing cipher %s\n",EVP_CIPHER_name(c)); 133 printf("Testing cipher %s%s\n",EVP_CIPHER_name(c),
134 (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
133 hexdump(stdout,"Key",key,kn); 135 hexdump(stdout,"Key",key,kn);
134 if(in) 136 if(in)
135 hexdump(stdout,"IV",iv,in); 137 hexdump(stdout,"IV",iv,in);
@@ -143,70 +145,76 @@ static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
143 exit(5); 145 exit(5);
144 } 146 }
145 EVP_CIPHER_CTX_init(&ctx); 147 EVP_CIPHER_CTX_init(&ctx);
146 if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv)) 148 if (encdec != 0)
147 { 149 {
148 fprintf(stderr,"EncryptInit failed\n"); 150 if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv))
149 exit(10); 151 {
150 } 152 fprintf(stderr,"EncryptInit failed\n");
151 EVP_CIPHER_CTX_set_padding(&ctx,0); 153 exit(10);
154 }
155 EVP_CIPHER_CTX_set_padding(&ctx,0);
152 156
153 if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn)) 157 if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn))
154 { 158 {
155 fprintf(stderr,"Encrypt failed\n"); 159 fprintf(stderr,"Encrypt failed\n");
156 exit(6); 160 exit(6);
157 } 161 }
158 if(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2)) 162 if(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2))
159 { 163 {
160 fprintf(stderr,"EncryptFinal failed\n"); 164 fprintf(stderr,"EncryptFinal failed\n");
161 exit(7); 165 exit(7);
162 } 166 }
163 167
164 if(outl+outl2 != cn) 168 if(outl+outl2 != cn)
165 { 169 {
166 fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n", 170 fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n",
167 outl+outl2,cn); 171 outl+outl2,cn);
168 exit(8); 172 exit(8);
169 } 173 }
170 174
171 if(memcmp(out,ciphertext,cn)) 175 if(memcmp(out,ciphertext,cn))
172 { 176 {
173 fprintf(stderr,"Ciphertext mismatch\n"); 177 fprintf(stderr,"Ciphertext mismatch\n");
174 hexdump(stderr,"Got",out,cn); 178 hexdump(stderr,"Got",out,cn);
175 hexdump(stderr,"Expected",ciphertext,cn); 179 hexdump(stderr,"Expected",ciphertext,cn);
176 exit(9); 180 exit(9);
181 }
177 } 182 }
178 183
179 if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv)) 184 if (encdec <= 0)
180 { 185 {
181 fprintf(stderr,"DecryptInit failed\n"); 186 if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv))
182 exit(11); 187 {
183 } 188 fprintf(stderr,"DecryptInit failed\n");
184 EVP_CIPHER_CTX_set_padding(&ctx,0); 189 exit(11);
190 }
191 EVP_CIPHER_CTX_set_padding(&ctx,0);
185 192
186 if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,pn)) 193 if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn))
187 { 194 {
188 fprintf(stderr,"Decrypt failed\n"); 195 fprintf(stderr,"Decrypt failed\n");
189 exit(6); 196 exit(6);
190 } 197 }
191 if(!EVP_DecryptFinal_ex(&ctx,out+outl,&outl2)) 198 if(!EVP_DecryptFinal_ex(&ctx,out+outl,&outl2))
192 { 199 {
193 fprintf(stderr,"DecryptFinal failed\n"); 200 fprintf(stderr,"DecryptFinal failed\n");
194 exit(7); 201 exit(7);
195 } 202 }
196 203
197 if(outl+outl2 != cn) 204 if(outl+outl2 != cn)
198 { 205 {
199 fprintf(stderr,"Plaintext length mismatch got %d expected %d\n", 206 fprintf(stderr,"Plaintext length mismatch got %d expected %d\n",
200 outl+outl2,cn); 207 outl+outl2,cn);
201 exit(8); 208 exit(8);
202 } 209 }
203 210
204 if(memcmp(out,plaintext,cn)) 211 if(memcmp(out,plaintext,cn))
205 { 212 {
206 fprintf(stderr,"Plaintext mismatch\n"); 213 fprintf(stderr,"Plaintext mismatch\n");
207 hexdump(stderr,"Got",out,cn); 214 hexdump(stderr,"Got",out,cn);
208 hexdump(stderr,"Expected",plaintext,cn); 215 hexdump(stderr,"Expected",plaintext,cn);
209 exit(9); 216 exit(9);
217 }
210 } 218 }
211 219
212 EVP_CIPHER_CTX_cleanup(&ctx); 220 EVP_CIPHER_CTX_cleanup(&ctx);
@@ -217,7 +225,8 @@ static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
217static int test_cipher(const char *cipher,const unsigned char *key,int kn, 225static int test_cipher(const char *cipher,const unsigned char *key,int kn,
218 const unsigned char *iv,int in, 226 const unsigned char *iv,int in,
219 const unsigned char *plaintext,int pn, 227 const unsigned char *plaintext,int pn,
220 const unsigned char *ciphertext,int cn) 228 const unsigned char *ciphertext,int cn,
229 int encdec)
221 { 230 {
222 const EVP_CIPHER *c; 231 const EVP_CIPHER *c;
223 232
@@ -225,7 +234,7 @@ static int test_cipher(const char *cipher,const unsigned char *key,int kn,
225 if(!c) 234 if(!c)
226 return 0; 235 return 0;
227 236
228 test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn); 237 test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec);
229 238
230 return 1; 239 return 1;
231 } 240 }
@@ -332,6 +341,7 @@ int main(int argc,char **argv)
332 char *p; 341 char *p;
333 char *cipher; 342 char *cipher;
334 unsigned char *iv,*key,*plaintext,*ciphertext; 343 unsigned char *iv,*key,*plaintext,*ciphertext;
344 int encdec;
335 int kn,in,pn,cn; 345 int kn,in,pn,cn;
336 346
337 if(!fgets((char *)line,sizeof line,f)) 347 if(!fgets((char *)line,sizeof line,f))
@@ -343,14 +353,21 @@ int main(int argc,char **argv)
343 key=ustrsep(&p,":"); 353 key=ustrsep(&p,":");
344 iv=ustrsep(&p,":"); 354 iv=ustrsep(&p,":");
345 plaintext=ustrsep(&p,":"); 355 plaintext=ustrsep(&p,":");
346 ciphertext=ustrsep(&p,"\n"); 356 ciphertext=ustrsep(&p,":");
357 if (p[-1] == '\n') {
358 p[-1] = '\0';
359 encdec = -1;
360 } else {
361 encdec = atoi(sstrsep(&p,"\n"));
362 }
363
347 364
348 kn=convert(key); 365 kn=convert(key);
349 in=convert(iv); 366 in=convert(iv);
350 pn=convert(plaintext); 367 pn=convert(plaintext);
351 cn=convert(ciphertext); 368 cn=convert(ciphertext);
352 369
353 if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn) 370 if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec)
354 && !test_digest(cipher,plaintext,pn,ciphertext,cn)) 371 && !test_digest(cipher,plaintext,pn,ciphertext,cn))
355 { 372 {
356 fprintf(stderr,"Can't find %s\n",cipher); 373 fprintf(stderr,"Can't find %s\n",cipher);