diff options
Diffstat (limited to 'src/lib/libcrypto/evp/evp_test.c')
-rw-r--r-- | src/lib/libcrypto/evp/evp_test.c | 145 |
1 files changed, 83 insertions, 62 deletions
diff --git a/src/lib/libcrypto/evp/evp_test.c b/src/lib/libcrypto/evp/evp_test.c index 3607fe7776..1bfffb34cf 100644 --- a/src/lib/libcrypto/evp/evp_test.c +++ b/src/lib/libcrypto/evp/evp_test.c | |||
@@ -123,13 +123,15 @@ static unsigned char *ustrsep(char **p,const char *sep) | |||
123 | static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn, | 123 | static 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,79 +145,88 @@ 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 | ||
220 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
221 | |||
212 | printf("\n"); | 222 | printf("\n"); |
213 | } | 223 | } |
214 | 224 | ||
215 | static int test_cipher(const char *cipher,const unsigned char *key,int kn, | 225 | static int test_cipher(const char *cipher,const unsigned char *key,int kn, |
216 | const unsigned char *iv,int in, | 226 | const unsigned char *iv,int in, |
217 | const unsigned char *plaintext,int pn, | 227 | const unsigned char *plaintext,int pn, |
218 | const unsigned char *ciphertext,int cn) | 228 | const unsigned char *ciphertext,int cn, |
229 | int encdec) | ||
219 | { | 230 | { |
220 | const EVP_CIPHER *c; | 231 | const EVP_CIPHER *c; |
221 | 232 | ||
@@ -223,7 +234,7 @@ static int test_cipher(const char *cipher,const unsigned char *key,int kn, | |||
223 | if(!c) | 234 | if(!c) |
224 | return 0; | 235 | return 0; |
225 | 236 | ||
226 | test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn); | 237 | test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec); |
227 | 238 | ||
228 | return 1; | 239 | return 1; |
229 | } | 240 | } |
@@ -279,6 +290,8 @@ static int test_digest(const char *digest, | |||
279 | 290 | ||
280 | printf("\n"); | 291 | printf("\n"); |
281 | 292 | ||
293 | EVP_MD_CTX_cleanup(&ctx); | ||
294 | |||
282 | return 1; | 295 | return 1; |
283 | } | 296 | } |
284 | 297 | ||
@@ -328,6 +341,7 @@ int main(int argc,char **argv) | |||
328 | char *p; | 341 | char *p; |
329 | char *cipher; | 342 | char *cipher; |
330 | unsigned char *iv,*key,*plaintext,*ciphertext; | 343 | unsigned char *iv,*key,*plaintext,*ciphertext; |
344 | int encdec; | ||
331 | int kn,in,pn,cn; | 345 | int kn,in,pn,cn; |
332 | 346 | ||
333 | if(!fgets((char *)line,sizeof line,f)) | 347 | if(!fgets((char *)line,sizeof line,f)) |
@@ -339,14 +353,21 @@ int main(int argc,char **argv) | |||
339 | key=ustrsep(&p,":"); | 353 | key=ustrsep(&p,":"); |
340 | iv=ustrsep(&p,":"); | 354 | iv=ustrsep(&p,":"); |
341 | plaintext=ustrsep(&p,":"); | 355 | plaintext=ustrsep(&p,":"); |
342 | 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(strsep(&p,"\n")); | ||
362 | } | ||
363 | |||
343 | 364 | ||
344 | kn=convert(key); | 365 | kn=convert(key); |
345 | in=convert(iv); | 366 | in=convert(iv); |
346 | pn=convert(plaintext); | 367 | pn=convert(plaintext); |
347 | cn=convert(ciphertext); | 368 | cn=convert(ciphertext); |
348 | 369 | ||
349 | 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) |
350 | && !test_digest(cipher,plaintext,pn,ciphertext,cn)) | 371 | && !test_digest(cipher,plaintext,pn,ciphertext,cn)) |
351 | { | 372 | { |
352 | fprintf(stderr,"Can't find %s\n",cipher); | 373 | fprintf(stderr,"Can't find %s\n",cipher); |