sl@0: /* Written by Ben Laurie, 2001 */ sl@0: /* sl@0: * Copyright (c) 2001 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: #include sl@0: #include sl@0: sl@0: #ifndef SYMBIAN sl@0: #include "../e_os.h" sl@0: #else sl@0: #include "e_os.h" sl@0: #endif sl@0: sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: #include sl@0: #endif sl@0: #include sl@0: #include sl@0: #ifdef SYMBIAN sl@0: #ifdef stdin sl@0: #undef stdin sl@0: #endif sl@0: #ifdef stdout sl@0: #undef stdout sl@0: #endif sl@0: #ifdef stderr sl@0: #undef stderr sl@0: #endif sl@0: sl@0: #define stdin fp_stdin sl@0: #define stdout fp_stdout sl@0: #define stderr fp_stderr sl@0: sl@0: extern FILE *fp_stdout; sl@0: extern FILE *fp_stderr; sl@0: #endif sl@0: sl@0: static void hexdump(FILE *f,const char *title,const unsigned char *s,int l) sl@0: { sl@0: int n=0; sl@0: sl@0: fprintf(f,"%s",title); sl@0: for( ; n < l ; ++n) sl@0: { sl@0: if((n%16) == 0) sl@0: fprintf(f,"\n%04x",n); sl@0: fprintf(f," %02x",s[n]); sl@0: } sl@0: fprintf(f,"\n"); sl@0: } sl@0: sl@0: static int convert(unsigned char *s) sl@0: { sl@0: unsigned char *d; sl@0: sl@0: for(d=s ; (*s)&&(*s!='\r') ; s+=2,++d) sl@0: { sl@0: unsigned int n; sl@0: sl@0: if(!s[1]) sl@0: { sl@0: fprintf(stderr,"Odd number of hex digits!"); sl@0: return 4; sl@0: //EXIT(4); sl@0: } sl@0: sscanf((char *)s,"%2x",&n); sl@0: *d=(unsigned char)n; sl@0: } sl@0: return s-d; sl@0: } sl@0: sl@0: static char *sstrsep(char **string, const char *delim) sl@0: { sl@0: char isdelim[256]; sl@0: char *token = *string; sl@0: sl@0: if (**string == 0) sl@0: return NULL; sl@0: sl@0: memset(isdelim, 0, 256); sl@0: isdelim[0] = 1; sl@0: sl@0: while (*delim) sl@0: { sl@0: isdelim[(unsigned char)(*delim)] = 1; sl@0: delim++; sl@0: } sl@0: sl@0: while (!isdelim[(unsigned char)(**string)]) sl@0: { sl@0: (*string)++; sl@0: } sl@0: sl@0: if (**string) sl@0: { sl@0: **string = 0; sl@0: (*string)++; sl@0: } sl@0: sl@0: return token; sl@0: } sl@0: sl@0: static unsigned char *ustrsep(char **p,const char *sep) sl@0: { return (unsigned char *)sstrsep(p,sep); } sl@0: sl@0: static int test1_exit(int ec) sl@0: { sl@0: sl@0: return(0); /* To keep some compilers quiet */ sl@0: } sl@0: sl@0: static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn, sl@0: const unsigned char *iv,int in, sl@0: const unsigned char *plaintext,int pn, sl@0: const unsigned char *ciphertext,int cn, sl@0: int encdec) sl@0: { sl@0: EVP_CIPHER_CTX ctx; sl@0: #ifndef SYMBIAN sl@0: unsigned char out[4096]; sl@0: #else sl@0: unsigned char out[400]; sl@0: #endif sl@0: int outl,outl2; sl@0: sl@0: fprintf(stdout,"Testing cipher %s%s\n",EVP_CIPHER_name(c), sl@0: (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)"))); sl@0: hexdump(stdout,"Key",key,kn); sl@0: if(in) sl@0: hexdump(stdout,"IV",iv,in); sl@0: hexdump(stdout,"Plaintext",plaintext,pn); sl@0: hexdump(stdout,"Ciphertext",ciphertext,cn); sl@0: sl@0: if(kn != c->key_len) sl@0: { sl@0: fprintf(stderr,"Key length doesn't match, got %d expected %d\n",kn, sl@0: c->key_len); sl@0: test1_exit(5); sl@0: } sl@0: EVP_CIPHER_CTX_init(&ctx); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: if (encdec != 0) sl@0: { sl@0: if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: fprintf(stderr,"EncryptInit failed\n"); sl@0: ERR_print_errors_fp(stderr); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: sl@0: test1_exit(10); sl@0: } sl@0: EVP_CIPHER_CTX_set_padding(&ctx,0); sl@0: sl@0: if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: fprintf(stderr,"Encrypt failed\n"); sl@0: ERR_print_errors_fp(stderr); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: test1_exit(6); sl@0: } sl@0: if(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: fprintf(stderr,"EncryptFinal failed\n"); sl@0: ERR_print_errors_fp(stderr); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: test1_exit(7); sl@0: } sl@0: sl@0: if(outl+outl2 != cn) sl@0: { sl@0: fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n", sl@0: outl+outl2,cn); sl@0: test1_exit(8); sl@0: } sl@0: sl@0: if(memcmp(out,ciphertext,cn)) sl@0: { sl@0: fprintf(stderr,"Ciphertext mismatch\n"); sl@0: hexdump(stderr,"Got",out,cn); sl@0: hexdump(stderr,"Expected",ciphertext,cn); sl@0: test1_exit(9); sl@0: } sl@0: } sl@0: sl@0: if (encdec <= 0) sl@0: { sl@0: if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: fprintf(stderr,"DecryptInit failed\n"); sl@0: ERR_print_errors_fp(stderr); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: test1_exit(11); sl@0: } sl@0: EVP_CIPHER_CTX_set_padding(&ctx,0); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: sl@0: sl@0: if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: fprintf(stderr,"Decrypt failed\n"); sl@0: ERR_print_errors_fp(stderr); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: test1_exit(6); sl@0: } sl@0: if(!EVP_DecryptFinal_ex(&ctx,out+outl,&outl2)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: fprintf(stderr,"DecryptFinal failed\n"); sl@0: ERR_print_errors_fp(stderr); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: test1_exit(7); sl@0: } sl@0: sl@0: if(outl+outl2 != cn) sl@0: { sl@0: fprintf(stderr,"Plaintext length mismatch got %d expected %d\n", sl@0: outl+outl2,cn); sl@0: test1_exit(8); sl@0: } sl@0: sl@0: if(memcmp(out,plaintext,cn)) sl@0: { sl@0: fprintf(stderr,"Plaintext mismatch\n"); sl@0: hexdump(stderr,"Got",out,cn); sl@0: hexdump(stderr,"Expected",plaintext,cn); sl@0: test1_exit(9); sl@0: } sl@0: } sl@0: sl@0: EVP_CIPHER_CTX_cleanup(&ctx); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return ; sl@0: } sl@0: sl@0: fprintf(stdout,"Test Case passed!\n"); sl@0: sl@0: } sl@0: sl@0: static int test_cipher(const char *cipher,const unsigned char *key,int kn, sl@0: const unsigned char *iv,int in, sl@0: const unsigned char *plaintext,int pn, sl@0: const unsigned char *ciphertext,int cn, sl@0: int encdec) sl@0: { sl@0: const EVP_CIPHER *c; sl@0: sl@0: c=EVP_get_cipherbyname(cipher); sl@0: if(c==NULL&&errno==ENOMEM) sl@0: { sl@0: return 0; sl@0: } sl@0: if(!c) sl@0: return 0; sl@0: sl@0: test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: static int test_digest(const char *digest, sl@0: const unsigned char *plaintext,int pn, sl@0: const unsigned char *ciphertext, unsigned int cn) sl@0: { sl@0: const EVP_MD *d; sl@0: EVP_MD_CTX ctx; sl@0: unsigned char md[EVP_MAX_MD_SIZE]; sl@0: unsigned int mdn; sl@0: sl@0: d=EVP_get_digestbyname(digest); sl@0: if(d==0&&errno==ENOMEM) sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: if(!d) sl@0: return 0; sl@0: sl@0: fprintf(stdout,"Testing digest %s\n",EVP_MD_name(d)); sl@0: hexdump(stdout,"Plaintext",plaintext,pn); sl@0: hexdump(stdout,"Digest",ciphertext,cn); sl@0: sl@0: EVP_MD_CTX_init(&ctx); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: if(!EVP_DigestInit_ex(&ctx,d, NULL)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: fprintf(stderr,"DigestInit failed\n"); sl@0: ERR_print_errors_fp(stderr); sl@0: return 100; sl@0: //EXIT(100); sl@0: } sl@0: if(!EVP_DigestUpdate(&ctx,plaintext,pn)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: fprintf(stderr,"DigestUpdate failed\n"); sl@0: ERR_print_errors_fp(stderr); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: return 101; sl@0: //EXIT(101); sl@0: } sl@0: if(!EVP_DigestFinal_ex(&ctx,md,&mdn)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 0; sl@0: } sl@0: fprintf(stderr,"DigestFinal failed\n"); sl@0: ERR_print_errors_fp(stderr); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: return 101; sl@0: //EXIT(101); sl@0: } sl@0: EVP_MD_CTX_cleanup(&ctx); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: if(mdn != cn) sl@0: { sl@0: fprintf(stderr,"Digest length mismatch, got %d expected %d\n",mdn,cn); sl@0: return 102; sl@0: //EXIT(102); sl@0: } sl@0: sl@0: if(memcmp(md,ciphertext,cn)) sl@0: { sl@0: fprintf(stderr,"Digest mismatch\n"); sl@0: hexdump(stderr,"Got",md,cn); sl@0: hexdump(stderr,"Expected",ciphertext,cn); sl@0: return 103; sl@0: //EXIT(103); sl@0: } sl@0: sl@0: fprintf(stdout,"done\n"); sl@0: sl@0: EVP_MD_CTX_cleanup(&ctx); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: return 1; sl@0: } sl@0: #ifndef SYMBIAN sl@0: int main(int argc,char **argv) sl@0: #else sl@0: int evp_main(int argc,char **argv) sl@0: #endif sl@0: { sl@0: const char *szTestFile; sl@0: FILE *f; sl@0: if(argc != 2) sl@0: { sl@0: fprintf(stderr,"%s \n",argv[0]); sl@0: return 1; sl@0: //EXIT(1); sl@0: } sl@0: sl@0: /*CRYPTO_malloc_debug_init(); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: }*/ sl@0: sl@0: szTestFile=argv[1]; sl@0: sl@0: f=fopen(szTestFile,"r"); sl@0: if(!f) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: fprintf(stderr,"Couldn't open file"); sl@0: //perror(szTestFile); sl@0: return 1; sl@0: //EXIT(2); sl@0: } sl@0: fprintf(stderr,"Opened file sucessfully\n"); sl@0: /* Load up the software EVP_CIPHER and EVP_MD definitions */ sl@0: OpenSSL_add_all_ciphers(); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: fprintf(stderr,"OpenSSL_add_all_ciphers(): done\n"); sl@0: OpenSSL_add_all_digests(); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: fprintf(stderr,"OpenSSL_add_all_digests() : done\n"); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: /* Load all compiled-in ENGINEs */ sl@0: ENGINE_load_builtin_engines(); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: #endif sl@0: #if 0 sl@0: OPENSSL_config(); sl@0: #endif sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: /* Register all available ENGINE implementations of ciphers and digests. sl@0: * This could perhaps be changed to "ENGINE_register_all_complete()"? */ sl@0: ENGINE_register_all_ciphers(); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: ENGINE_register_all_digests(); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: /* If we add command-line options, this statement should be switchable. sl@0: * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if sl@0: * they weren't already initialised. */ sl@0: /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */ sl@0: #endif sl@0: sl@0: for( ; ; ) sl@0: { sl@0: #ifndef SYMBIAN sl@0: char line[4096]; sl@0: #else sl@0: char line[400]; sl@0: #endif sl@0: char *p; sl@0: char *cipher; sl@0: unsigned char *iv,*key,*plaintext,*ciphertext; sl@0: int encdec; sl@0: int kn,in,pn,cn; sl@0: sl@0: if(!fgets((char *)line,sizeof line,f)) sl@0: break; sl@0: if(line[0] == '#' || line[0] == '\n'||line[0] == '\r') sl@0: continue; sl@0: sl@0: p=line; sl@0: cipher=sstrsep(&p,":"); sl@0: key=ustrsep(&p,":"); sl@0: iv=ustrsep(&p,":"); sl@0: plaintext=ustrsep(&p,":"); sl@0: ciphertext=ustrsep(&p,":"); sl@0: if (p[-1] == '\n') { sl@0: p[-1] = '\0'; sl@0: encdec = -1; sl@0: } else { sl@0: encdec = atoi(sstrsep(&p,"\n")); sl@0: } sl@0: sl@0: kn=convert(key); sl@0: sl@0: in=convert(iv); sl@0: pn=convert(plaintext); sl@0: cn=convert(ciphertext); sl@0: if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec) sl@0: && !test_digest(cipher,plaintext,pn,ciphertext,cn)) sl@0: { sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: #ifdef OPENSSL_NO_AES sl@0: if (strstr(cipher, "AES") == cipher) sl@0: { sl@0: fprintf(stdout, "Cipher disabled, skipping %s\n", cipher); sl@0: continue; sl@0: } sl@0: #endif sl@0: #ifdef OPENSSL_NO_DES sl@0: if (strstr(cipher, "DES") == cipher) sl@0: { sl@0: fprintf(stdout, "Cipher disabled, skipping %s\n", cipher); sl@0: continue; sl@0: } sl@0: #endif sl@0: #ifdef OPENSSL_NO_RC4 sl@0: if (strstr(cipher, "RC4") == cipher) sl@0: { sl@0: fprintf(stdout, "Cipher disabled, skipping %s\n", cipher); sl@0: continue; sl@0: } sl@0: #endif sl@0: fprintf(stderr,"Can't find %s\n",cipher); sl@0: return 3; sl@0: //EXIT(3); sl@0: } sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: ENGINE_cleanup(); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: #endif sl@0: EVP_cleanup(); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: CRYPTO_cleanup_all_ex_data(); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: ERR_remove_state(0); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: ERR_free_strings(); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: CRYPTO_mem_leaks_fp(stderr); sl@0: if(errno==ENOMEM) sl@0: { sl@0: return 1; sl@0: } sl@0: fprintf(stderr,"*************END OF THE TEST CASE\n************"); sl@0: sl@0: return 0; sl@0: }