1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/tsrc/crypto_test/src/evp_test.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,656 @@
1.4 +/* Written by Ben Laurie, 2001 */
1.5 +/*
1.6 + * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
1.7 + *
1.8 + * Redistribution and use in source and binary forms, with or without
1.9 + * modification, are permitted provided that the following conditions
1.10 + * are met:
1.11 + *
1.12 + * 1. Redistributions of source code must retain the above copyright
1.13 + * notice, this list of conditions and the following disclaimer.
1.14 + *
1.15 + * 2. Redistributions in binary form must reproduce the above copyright
1.16 + * notice, this list of conditions and the following disclaimer in
1.17 + * the documentation and/or other materials provided with the
1.18 + * distribution.
1.19 + *
1.20 + * 3. All advertising materials mentioning features or use of this
1.21 + * software must display the following acknowledgment:
1.22 + * "This product includes software developed by the OpenSSL Project
1.23 + * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
1.24 + *
1.25 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1.26 + * endorse or promote products derived from this software without
1.27 + * prior written permission. For written permission, please contact
1.28 + * openssl-core@openssl.org.
1.29 + *
1.30 + * 5. Products derived from this software may not be called "OpenSSL"
1.31 + * nor may "OpenSSL" appear in their names without prior written
1.32 + * permission of the OpenSSL Project.
1.33 + *
1.34 + * 6. Redistributions of any form whatsoever must retain the following
1.35 + * acknowledgment:
1.36 + * "This product includes software developed by the OpenSSL Project
1.37 + * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
1.38 + *
1.39 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1.40 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.41 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.42 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1.43 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1.44 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1.45 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1.46 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.47 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.48 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.49 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1.50 + * OF THE POSSIBILITY OF SUCH DAMAGE.
1.51 + */
1.52 +/*
1.53 + © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
1.54 + */
1.55 +#include <stdio.h>
1.56 +#include <string.h>
1.57 +
1.58 +#ifndef SYMBIAN
1.59 +#include "../e_os.h"
1.60 +#else
1.61 +#include "e_os.h"
1.62 +#endif
1.63 +
1.64 +#include <openssl/opensslconf.h>
1.65 +#include <openssl/evp.h>
1.66 +#ifndef OPENSSL_NO_ENGINE
1.67 +#include <openssl/engine.h>
1.68 +#endif
1.69 +#include <openssl/err.h>
1.70 +#include <openssl/conf.h>
1.71 +#ifdef SYMBIAN
1.72 +#ifdef stdin
1.73 +#undef stdin
1.74 +#endif
1.75 +#ifdef stdout
1.76 +#undef stdout
1.77 +#endif
1.78 +#ifdef stderr
1.79 +#undef stderr
1.80 +#endif
1.81 +
1.82 +#define stdin fp_stdin
1.83 +#define stdout fp_stdout
1.84 +#define stderr fp_stderr
1.85 +
1.86 +extern FILE *fp_stdout;
1.87 +extern FILE *fp_stderr;
1.88 +#endif
1.89 +
1.90 +static void hexdump(FILE *f,const char *title,const unsigned char *s,int l)
1.91 + {
1.92 + int n=0;
1.93 +
1.94 + fprintf(f,"%s",title);
1.95 + for( ; n < l ; ++n)
1.96 + {
1.97 + if((n%16) == 0)
1.98 + fprintf(f,"\n%04x",n);
1.99 + fprintf(f," %02x",s[n]);
1.100 + }
1.101 + fprintf(f,"\n");
1.102 + }
1.103 +
1.104 +static int convert(unsigned char *s)
1.105 + {
1.106 + unsigned char *d;
1.107 +
1.108 + for(d=s ; (*s)&&(*s!='\r') ; s+=2,++d)
1.109 + {
1.110 + unsigned int n;
1.111 +
1.112 + if(!s[1])
1.113 + {
1.114 + fprintf(stderr,"Odd number of hex digits!");
1.115 + return 4;
1.116 + //EXIT(4);
1.117 + }
1.118 + sscanf((char *)s,"%2x",&n);
1.119 + *d=(unsigned char)n;
1.120 + }
1.121 + return s-d;
1.122 + }
1.123 +
1.124 +static char *sstrsep(char **string, const char *delim)
1.125 + {
1.126 + char isdelim[256];
1.127 + char *token = *string;
1.128 +
1.129 + if (**string == 0)
1.130 + return NULL;
1.131 +
1.132 + memset(isdelim, 0, 256);
1.133 + isdelim[0] = 1;
1.134 +
1.135 + while (*delim)
1.136 + {
1.137 + isdelim[(unsigned char)(*delim)] = 1;
1.138 + delim++;
1.139 + }
1.140 +
1.141 + while (!isdelim[(unsigned char)(**string)])
1.142 + {
1.143 + (*string)++;
1.144 + }
1.145 +
1.146 + if (**string)
1.147 + {
1.148 + **string = 0;
1.149 + (*string)++;
1.150 + }
1.151 +
1.152 + return token;
1.153 + }
1.154 +
1.155 +static unsigned char *ustrsep(char **p,const char *sep)
1.156 + { return (unsigned char *)sstrsep(p,sep); }
1.157 +
1.158 +static int test1_exit(int ec)
1.159 + {
1.160 +
1.161 + return(0); /* To keep some compilers quiet */
1.162 + }
1.163 +
1.164 +static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
1.165 + const unsigned char *iv,int in,
1.166 + const unsigned char *plaintext,int pn,
1.167 + const unsigned char *ciphertext,int cn,
1.168 + int encdec)
1.169 + {
1.170 + EVP_CIPHER_CTX ctx;
1.171 +#ifndef SYMBIAN
1.172 + unsigned char out[4096];
1.173 +#else
1.174 + unsigned char out[400];
1.175 +#endif
1.176 + int outl,outl2;
1.177 +
1.178 + fprintf(stdout,"Testing cipher %s%s\n",EVP_CIPHER_name(c),
1.179 + (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
1.180 + hexdump(stdout,"Key",key,kn);
1.181 + if(in)
1.182 + hexdump(stdout,"IV",iv,in);
1.183 + hexdump(stdout,"Plaintext",plaintext,pn);
1.184 + hexdump(stdout,"Ciphertext",ciphertext,cn);
1.185 +
1.186 + if(kn != c->key_len)
1.187 + {
1.188 + fprintf(stderr,"Key length doesn't match, got %d expected %d\n",kn,
1.189 + c->key_len);
1.190 + test1_exit(5);
1.191 + }
1.192 + EVP_CIPHER_CTX_init(&ctx);
1.193 + if(errno==ENOMEM)
1.194 + {
1.195 + return ;
1.196 + }
1.197 + if (encdec != 0)
1.198 + {
1.199 + if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv))
1.200 + {
1.201 + if(errno==ENOMEM)
1.202 + {
1.203 + return ;
1.204 + }
1.205 + fprintf(stderr,"EncryptInit failed\n");
1.206 + ERR_print_errors_fp(stderr);
1.207 + if(errno==ENOMEM)
1.208 + {
1.209 + return ;
1.210 + }
1.211 +
1.212 + test1_exit(10);
1.213 + }
1.214 + EVP_CIPHER_CTX_set_padding(&ctx,0);
1.215 +
1.216 + if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn))
1.217 + {
1.218 + if(errno==ENOMEM)
1.219 + {
1.220 + return ;
1.221 + }
1.222 + fprintf(stderr,"Encrypt failed\n");
1.223 + ERR_print_errors_fp(stderr);
1.224 + if(errno==ENOMEM)
1.225 + {
1.226 + return ;
1.227 + }
1.228 + test1_exit(6);
1.229 + }
1.230 + if(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2))
1.231 + {
1.232 + if(errno==ENOMEM)
1.233 + {
1.234 + return ;
1.235 + }
1.236 + fprintf(stderr,"EncryptFinal failed\n");
1.237 + ERR_print_errors_fp(stderr);
1.238 + if(errno==ENOMEM)
1.239 + {
1.240 + return ;
1.241 + }
1.242 + test1_exit(7);
1.243 + }
1.244 +
1.245 + if(outl+outl2 != cn)
1.246 + {
1.247 + fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n",
1.248 + outl+outl2,cn);
1.249 + test1_exit(8);
1.250 + }
1.251 +
1.252 + if(memcmp(out,ciphertext,cn))
1.253 + {
1.254 + fprintf(stderr,"Ciphertext mismatch\n");
1.255 + hexdump(stderr,"Got",out,cn);
1.256 + hexdump(stderr,"Expected",ciphertext,cn);
1.257 + test1_exit(9);
1.258 + }
1.259 + }
1.260 +
1.261 + if (encdec <= 0)
1.262 + {
1.263 + if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv))
1.264 + {
1.265 + if(errno==ENOMEM)
1.266 + {
1.267 + return ;
1.268 + }
1.269 + fprintf(stderr,"DecryptInit failed\n");
1.270 + ERR_print_errors_fp(stderr);
1.271 + if(errno==ENOMEM)
1.272 + {
1.273 + return ;
1.274 + }
1.275 + test1_exit(11);
1.276 + }
1.277 + EVP_CIPHER_CTX_set_padding(&ctx,0);
1.278 + if(errno==ENOMEM)
1.279 + {
1.280 + return ;
1.281 + }
1.282 +
1.283 +
1.284 + if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn))
1.285 + {
1.286 + if(errno==ENOMEM)
1.287 + {
1.288 + return ;
1.289 + }
1.290 + fprintf(stderr,"Decrypt failed\n");
1.291 + ERR_print_errors_fp(stderr);
1.292 + if(errno==ENOMEM)
1.293 + {
1.294 + return ;
1.295 + }
1.296 + test1_exit(6);
1.297 + }
1.298 + if(!EVP_DecryptFinal_ex(&ctx,out+outl,&outl2))
1.299 + {
1.300 + if(errno==ENOMEM)
1.301 + {
1.302 + return ;
1.303 + }
1.304 + fprintf(stderr,"DecryptFinal failed\n");
1.305 + ERR_print_errors_fp(stderr);
1.306 + if(errno==ENOMEM)
1.307 + {
1.308 + return ;
1.309 + }
1.310 + test1_exit(7);
1.311 + }
1.312 +
1.313 + if(outl+outl2 != cn)
1.314 + {
1.315 + fprintf(stderr,"Plaintext length mismatch got %d expected %d\n",
1.316 + outl+outl2,cn);
1.317 + test1_exit(8);
1.318 + }
1.319 +
1.320 + if(memcmp(out,plaintext,cn))
1.321 + {
1.322 + fprintf(stderr,"Plaintext mismatch\n");
1.323 + hexdump(stderr,"Got",out,cn);
1.324 + hexdump(stderr,"Expected",plaintext,cn);
1.325 + test1_exit(9);
1.326 + }
1.327 + }
1.328 +
1.329 + EVP_CIPHER_CTX_cleanup(&ctx);
1.330 + if(errno==ENOMEM)
1.331 + {
1.332 + return ;
1.333 + }
1.334 +
1.335 + fprintf(stdout,"Test Case passed!\n");
1.336 +
1.337 + }
1.338 +
1.339 +static int test_cipher(const char *cipher,const unsigned char *key,int kn,
1.340 + const unsigned char *iv,int in,
1.341 + const unsigned char *plaintext,int pn,
1.342 + const unsigned char *ciphertext,int cn,
1.343 + int encdec)
1.344 + {
1.345 + const EVP_CIPHER *c;
1.346 +
1.347 + c=EVP_get_cipherbyname(cipher);
1.348 + if(c==NULL&&errno==ENOMEM)
1.349 + {
1.350 + return 0;
1.351 + }
1.352 + if(!c)
1.353 + return 0;
1.354 +
1.355 + test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec);
1.356 +
1.357 + return 1;
1.358 + }
1.359 +
1.360 +static int test_digest(const char *digest,
1.361 + const unsigned char *plaintext,int pn,
1.362 + const unsigned char *ciphertext, unsigned int cn)
1.363 + {
1.364 + const EVP_MD *d;
1.365 + EVP_MD_CTX ctx;
1.366 + unsigned char md[EVP_MAX_MD_SIZE];
1.367 + unsigned int mdn;
1.368 +
1.369 + d=EVP_get_digestbyname(digest);
1.370 + if(d==0&&errno==ENOMEM)
1.371 + {
1.372 + return 0;
1.373 + }
1.374 +
1.375 + if(!d)
1.376 + return 0;
1.377 +
1.378 + fprintf(stdout,"Testing digest %s\n",EVP_MD_name(d));
1.379 + hexdump(stdout,"Plaintext",plaintext,pn);
1.380 + hexdump(stdout,"Digest",ciphertext,cn);
1.381 +
1.382 + EVP_MD_CTX_init(&ctx);
1.383 + if(errno==ENOMEM)
1.384 + {
1.385 + return 0;
1.386 + }
1.387 +
1.388 + if(!EVP_DigestInit_ex(&ctx,d, NULL))
1.389 + {
1.390 + if(errno==ENOMEM)
1.391 + {
1.392 + return 0;
1.393 + }
1.394 +
1.395 + fprintf(stderr,"DigestInit failed\n");
1.396 + ERR_print_errors_fp(stderr);
1.397 + return 100;
1.398 + //EXIT(100);
1.399 + }
1.400 + if(!EVP_DigestUpdate(&ctx,plaintext,pn))
1.401 + {
1.402 + if(errno==ENOMEM)
1.403 + {
1.404 + return 0;
1.405 + }
1.406 +
1.407 + fprintf(stderr,"DigestUpdate failed\n");
1.408 + ERR_print_errors_fp(stderr);
1.409 + if(errno==ENOMEM)
1.410 + {
1.411 + return 0;
1.412 + }
1.413 +
1.414 + return 101;
1.415 + //EXIT(101);
1.416 + }
1.417 + if(!EVP_DigestFinal_ex(&ctx,md,&mdn))
1.418 + {
1.419 + if(errno==ENOMEM)
1.420 + {
1.421 + return 0;
1.422 + }
1.423 + fprintf(stderr,"DigestFinal failed\n");
1.424 + ERR_print_errors_fp(stderr);
1.425 + if(errno==ENOMEM)
1.426 + {
1.427 + return 0;
1.428 + }
1.429 +
1.430 + return 101;
1.431 + //EXIT(101);
1.432 + }
1.433 + EVP_MD_CTX_cleanup(&ctx);
1.434 + if(errno==ENOMEM)
1.435 + {
1.436 + return 0;
1.437 + }
1.438 +
1.439 + if(mdn != cn)
1.440 + {
1.441 + fprintf(stderr,"Digest length mismatch, got %d expected %d\n",mdn,cn);
1.442 + return 102;
1.443 + //EXIT(102);
1.444 + }
1.445 +
1.446 + if(memcmp(md,ciphertext,cn))
1.447 + {
1.448 + fprintf(stderr,"Digest mismatch\n");
1.449 + hexdump(stderr,"Got",md,cn);
1.450 + hexdump(stderr,"Expected",ciphertext,cn);
1.451 + return 103;
1.452 + //EXIT(103);
1.453 + }
1.454 +
1.455 + fprintf(stdout,"done\n");
1.456 +
1.457 + EVP_MD_CTX_cleanup(&ctx);
1.458 + if(errno==ENOMEM)
1.459 + {
1.460 + return 0;
1.461 + }
1.462 +
1.463 + return 1;
1.464 + }
1.465 +#ifndef SYMBIAN
1.466 +int main(int argc,char **argv)
1.467 +#else
1.468 +int evp_main(int argc,char **argv)
1.469 +#endif
1.470 + {
1.471 + const char *szTestFile;
1.472 + FILE *f;
1.473 + if(argc != 2)
1.474 + {
1.475 + fprintf(stderr,"%s <test file>\n",argv[0]);
1.476 + return 1;
1.477 + //EXIT(1);
1.478 + }
1.479 +
1.480 + /*CRYPTO_malloc_debug_init();
1.481 + if(errno==ENOMEM)
1.482 + {
1.483 + return 1;
1.484 + }
1.485 + CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
1.486 + if(errno==ENOMEM)
1.487 + {
1.488 + return 1;
1.489 + }
1.490 + CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
1.491 + if(errno==ENOMEM)
1.492 + {
1.493 + return 1;
1.494 + }*/
1.495 +
1.496 + szTestFile=argv[1];
1.497 +
1.498 + f=fopen(szTestFile,"r");
1.499 + if(!f)
1.500 + {
1.501 + if(errno==ENOMEM)
1.502 + {
1.503 + return 1;
1.504 + }
1.505 + fprintf(stderr,"Couldn't open file");
1.506 + //perror(szTestFile);
1.507 + return 1;
1.508 + //EXIT(2);
1.509 + }
1.510 + fprintf(stderr,"Opened file sucessfully\n");
1.511 + /* Load up the software EVP_CIPHER and EVP_MD definitions */
1.512 + OpenSSL_add_all_ciphers();
1.513 + if(errno==ENOMEM)
1.514 + {
1.515 + return 1;
1.516 + }
1.517 + fprintf(stderr,"OpenSSL_add_all_ciphers(): done\n");
1.518 + OpenSSL_add_all_digests();
1.519 + if(errno==ENOMEM)
1.520 + {
1.521 + return 1;
1.522 + }
1.523 + fprintf(stderr,"OpenSSL_add_all_digests() : done\n");
1.524 +#ifndef OPENSSL_NO_ENGINE
1.525 + /* Load all compiled-in ENGINEs */
1.526 + ENGINE_load_builtin_engines();
1.527 + if(errno==ENOMEM)
1.528 + {
1.529 + return 1;
1.530 + }
1.531 +#endif
1.532 +#if 0
1.533 + OPENSSL_config();
1.534 +#endif
1.535 +#ifndef OPENSSL_NO_ENGINE
1.536 + /* Register all available ENGINE implementations of ciphers and digests.
1.537 + * This could perhaps be changed to "ENGINE_register_all_complete()"? */
1.538 + ENGINE_register_all_ciphers();
1.539 + if(errno==ENOMEM)
1.540 + {
1.541 + return 1;
1.542 + }
1.543 + ENGINE_register_all_digests();
1.544 + if(errno==ENOMEM)
1.545 + {
1.546 + return 1;
1.547 + }
1.548 + /* If we add command-line options, this statement should be switchable.
1.549 + * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if
1.550 + * they weren't already initialised. */
1.551 + /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */
1.552 +#endif
1.553 +
1.554 + for( ; ; )
1.555 + {
1.556 +#ifndef SYMBIAN
1.557 + char line[4096];
1.558 +#else
1.559 + char line[400];
1.560 +#endif
1.561 + char *p;
1.562 + char *cipher;
1.563 + unsigned char *iv,*key,*plaintext,*ciphertext;
1.564 + int encdec;
1.565 + int kn,in,pn,cn;
1.566 +
1.567 + if(!fgets((char *)line,sizeof line,f))
1.568 + break;
1.569 + if(line[0] == '#' || line[0] == '\n'||line[0] == '\r')
1.570 + continue;
1.571 +
1.572 + p=line;
1.573 + cipher=sstrsep(&p,":");
1.574 + key=ustrsep(&p,":");
1.575 + iv=ustrsep(&p,":");
1.576 + plaintext=ustrsep(&p,":");
1.577 + ciphertext=ustrsep(&p,":");
1.578 + if (p[-1] == '\n') {
1.579 + p[-1] = '\0';
1.580 + encdec = -1;
1.581 + } else {
1.582 + encdec = atoi(sstrsep(&p,"\n"));
1.583 + }
1.584 +
1.585 + kn=convert(key);
1.586 +
1.587 + in=convert(iv);
1.588 + pn=convert(plaintext);
1.589 + cn=convert(ciphertext);
1.590 + if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec)
1.591 + && !test_digest(cipher,plaintext,pn,ciphertext,cn))
1.592 + {
1.593 + if(errno==ENOMEM)
1.594 + {
1.595 + return 1;
1.596 + }
1.597 +#ifdef OPENSSL_NO_AES
1.598 + if (strstr(cipher, "AES") == cipher)
1.599 + {
1.600 + fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
1.601 + continue;
1.602 + }
1.603 +#endif
1.604 +#ifdef OPENSSL_NO_DES
1.605 + if (strstr(cipher, "DES") == cipher)
1.606 + {
1.607 + fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
1.608 + continue;
1.609 + }
1.610 +#endif
1.611 +#ifdef OPENSSL_NO_RC4
1.612 + if (strstr(cipher, "RC4") == cipher)
1.613 + {
1.614 + fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
1.615 + continue;
1.616 + }
1.617 +#endif
1.618 + fprintf(stderr,"Can't find %s\n",cipher);
1.619 + return 3;
1.620 + //EXIT(3);
1.621 + }
1.622 + }
1.623 +
1.624 +#ifndef OPENSSL_NO_ENGINE
1.625 + ENGINE_cleanup();
1.626 + if(errno==ENOMEM)
1.627 + {
1.628 + return 1;
1.629 + }
1.630 +#endif
1.631 + EVP_cleanup();
1.632 + if(errno==ENOMEM)
1.633 + {
1.634 + return 1;
1.635 + }
1.636 + CRYPTO_cleanup_all_ex_data();
1.637 + if(errno==ENOMEM)
1.638 + {
1.639 + return 1;
1.640 + }
1.641 + ERR_remove_state(0);
1.642 + if(errno==ENOMEM)
1.643 + {
1.644 + return 1;
1.645 + }
1.646 + ERR_free_strings();
1.647 + if(errno==ENOMEM)
1.648 + {
1.649 + return 1;
1.650 + }
1.651 + CRYPTO_mem_leaks_fp(stderr);
1.652 + if(errno==ENOMEM)
1.653 + {
1.654 + return 1;
1.655 + }
1.656 + fprintf(stderr,"*************END OF THE TEST CASE\n************");
1.657 +
1.658 + return 0;
1.659 + }