xref: /onnv-gate/usr/src/common/openssl/crypto/evp/evp_test.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* Written by Ben Laurie, 2001 */
20Sstevel@tonic-gate /*
30Sstevel@tonic-gate  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
60Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
70Sstevel@tonic-gate  * are met:
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
100Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
140Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
150Sstevel@tonic-gate  *    distribution.
160Sstevel@tonic-gate  *
170Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
180Sstevel@tonic-gate  *    software must display the following acknowledgment:
190Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
200Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
210Sstevel@tonic-gate  *
220Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
230Sstevel@tonic-gate  *    endorse or promote products derived from this software without
240Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
250Sstevel@tonic-gate  *    openssl-core@openssl.org.
260Sstevel@tonic-gate  *
270Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
280Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
290Sstevel@tonic-gate  *    permission of the OpenSSL Project.
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
320Sstevel@tonic-gate  *    acknowledgment:
330Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
340Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
370Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
380Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
390Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
400Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
410Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
420Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
430Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
440Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
450Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
460Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
470Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <stdio.h>
510Sstevel@tonic-gate #include <string.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include "../e_os.h"
540Sstevel@tonic-gate 
55*2139Sjp161948 #include <openssl/opensslconf.h>
560Sstevel@tonic-gate #include <openssl/evp.h>
570Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
580Sstevel@tonic-gate #include <openssl/engine.h>
590Sstevel@tonic-gate #endif
600Sstevel@tonic-gate #include <openssl/err.h>
610Sstevel@tonic-gate #include <openssl/conf.h>
620Sstevel@tonic-gate 
hexdump(FILE * f,const char * title,const unsigned char * s,int l)630Sstevel@tonic-gate static void hexdump(FILE *f,const char *title,const unsigned char *s,int l)
640Sstevel@tonic-gate     {
650Sstevel@tonic-gate     int n=0;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate     fprintf(f,"%s",title);
680Sstevel@tonic-gate     for( ; n < l ; ++n)
690Sstevel@tonic-gate 	{
700Sstevel@tonic-gate 	if((n%16) == 0)
710Sstevel@tonic-gate 	    fprintf(f,"\n%04x",n);
720Sstevel@tonic-gate 	fprintf(f," %02x",s[n]);
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate     fprintf(f,"\n");
750Sstevel@tonic-gate     }
760Sstevel@tonic-gate 
convert(unsigned char * s)770Sstevel@tonic-gate static int convert(unsigned char *s)
780Sstevel@tonic-gate     {
790Sstevel@tonic-gate     unsigned char *d;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate     for(d=s ; *s ; s+=2,++d)
820Sstevel@tonic-gate 	{
830Sstevel@tonic-gate 	unsigned int n;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if(!s[1])
860Sstevel@tonic-gate 	    {
870Sstevel@tonic-gate 	    fprintf(stderr,"Odd number of hex digits!");
880Sstevel@tonic-gate 	    EXIT(4);
890Sstevel@tonic-gate 	    }
900Sstevel@tonic-gate 	sscanf((char *)s,"%2x",&n);
910Sstevel@tonic-gate 	*d=(unsigned char)n;
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate     return s-d;
940Sstevel@tonic-gate     }
950Sstevel@tonic-gate 
sstrsep(char ** string,const char * delim)960Sstevel@tonic-gate static char *sstrsep(char **string, const char *delim)
970Sstevel@tonic-gate     {
980Sstevel@tonic-gate     char isdelim[256];
990Sstevel@tonic-gate     char *token = *string;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate     if (**string == 0)
1020Sstevel@tonic-gate         return NULL;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate     memset(isdelim, 0, 256);
1050Sstevel@tonic-gate     isdelim[0] = 1;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate     while (*delim)
1080Sstevel@tonic-gate         {
1090Sstevel@tonic-gate         isdelim[(unsigned char)(*delim)] = 1;
1100Sstevel@tonic-gate         delim++;
1110Sstevel@tonic-gate         }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate     while (!isdelim[(unsigned char)(**string)])
1140Sstevel@tonic-gate         {
1150Sstevel@tonic-gate         (*string)++;
1160Sstevel@tonic-gate         }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate     if (**string)
1190Sstevel@tonic-gate         {
1200Sstevel@tonic-gate         **string = 0;
1210Sstevel@tonic-gate         (*string)++;
1220Sstevel@tonic-gate         }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate     return token;
1250Sstevel@tonic-gate     }
1260Sstevel@tonic-gate 
ustrsep(char ** p,const char * sep)1270Sstevel@tonic-gate static unsigned char *ustrsep(char **p,const char *sep)
1280Sstevel@tonic-gate     { return (unsigned char *)sstrsep(p,sep); }
1290Sstevel@tonic-gate 
test1_exit(int ec)1300Sstevel@tonic-gate static int test1_exit(int ec)
1310Sstevel@tonic-gate 	{
1320Sstevel@tonic-gate 	EXIT(ec);
1330Sstevel@tonic-gate 	return(0);		/* To keep some compilers quiet */
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
test1(const EVP_CIPHER * c,const unsigned char * key,int kn,const unsigned char * iv,int in,const unsigned char * plaintext,int pn,const unsigned char * ciphertext,int cn,int encdec)1360Sstevel@tonic-gate static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
1370Sstevel@tonic-gate 		  const unsigned char *iv,int in,
1380Sstevel@tonic-gate 		  const unsigned char *plaintext,int pn,
1390Sstevel@tonic-gate 		  const unsigned char *ciphertext,int cn,
1400Sstevel@tonic-gate 		  int encdec)
1410Sstevel@tonic-gate     {
1420Sstevel@tonic-gate     EVP_CIPHER_CTX ctx;
1430Sstevel@tonic-gate     unsigned char out[4096];
1440Sstevel@tonic-gate     int outl,outl2;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate     printf("Testing cipher %s%s\n",EVP_CIPHER_name(c),
1470Sstevel@tonic-gate 	   (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
1480Sstevel@tonic-gate     hexdump(stdout,"Key",key,kn);
1490Sstevel@tonic-gate     if(in)
1500Sstevel@tonic-gate 	hexdump(stdout,"IV",iv,in);
1510Sstevel@tonic-gate     hexdump(stdout,"Plaintext",plaintext,pn);
1520Sstevel@tonic-gate     hexdump(stdout,"Ciphertext",ciphertext,cn);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate     if(kn != c->key_len)
1550Sstevel@tonic-gate 	{
1560Sstevel@tonic-gate 	fprintf(stderr,"Key length doesn't match, got %d expected %d\n",kn,
1570Sstevel@tonic-gate 		c->key_len);
1580Sstevel@tonic-gate 	test1_exit(5);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate     EVP_CIPHER_CTX_init(&ctx);
1610Sstevel@tonic-gate     if (encdec != 0)
1620Sstevel@tonic-gate         {
1630Sstevel@tonic-gate 	if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv))
1640Sstevel@tonic-gate 	    {
1650Sstevel@tonic-gate 	    fprintf(stderr,"EncryptInit failed\n");
166*2139Sjp161948 	    ERR_print_errors_fp(stderr);
1670Sstevel@tonic-gate 	    test1_exit(10);
1680Sstevel@tonic-gate 	    }
1690Sstevel@tonic-gate 	EVP_CIPHER_CTX_set_padding(&ctx,0);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn))
1720Sstevel@tonic-gate 	    {
1730Sstevel@tonic-gate 	    fprintf(stderr,"Encrypt failed\n");
174*2139Sjp161948 	    ERR_print_errors_fp(stderr);
1750Sstevel@tonic-gate 	    test1_exit(6);
1760Sstevel@tonic-gate 	    }
1770Sstevel@tonic-gate 	if(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2))
1780Sstevel@tonic-gate 	    {
1790Sstevel@tonic-gate 	    fprintf(stderr,"EncryptFinal failed\n");
180*2139Sjp161948 	    ERR_print_errors_fp(stderr);
1810Sstevel@tonic-gate 	    test1_exit(7);
1820Sstevel@tonic-gate 	    }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	if(outl+outl2 != cn)
1850Sstevel@tonic-gate 	    {
1860Sstevel@tonic-gate 	    fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n",
1870Sstevel@tonic-gate 		    outl+outl2,cn);
1880Sstevel@tonic-gate 	    test1_exit(8);
1890Sstevel@tonic-gate 	    }
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if(memcmp(out,ciphertext,cn))
1920Sstevel@tonic-gate 	    {
1930Sstevel@tonic-gate 	    fprintf(stderr,"Ciphertext mismatch\n");
1940Sstevel@tonic-gate 	    hexdump(stderr,"Got",out,cn);
1950Sstevel@tonic-gate 	    hexdump(stderr,"Expected",ciphertext,cn);
1960Sstevel@tonic-gate 	    test1_exit(9);
1970Sstevel@tonic-gate 	    }
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate     if (encdec <= 0)
2010Sstevel@tonic-gate         {
2020Sstevel@tonic-gate 	if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv))
2030Sstevel@tonic-gate 	    {
2040Sstevel@tonic-gate 	    fprintf(stderr,"DecryptInit failed\n");
205*2139Sjp161948 	    ERR_print_errors_fp(stderr);
2060Sstevel@tonic-gate 	    test1_exit(11);
2070Sstevel@tonic-gate 	    }
2080Sstevel@tonic-gate 	EVP_CIPHER_CTX_set_padding(&ctx,0);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn))
2110Sstevel@tonic-gate 	    {
2120Sstevel@tonic-gate 	    fprintf(stderr,"Decrypt failed\n");
213*2139Sjp161948 	    ERR_print_errors_fp(stderr);
2140Sstevel@tonic-gate 	    test1_exit(6);
2150Sstevel@tonic-gate 	    }
2160Sstevel@tonic-gate 	if(!EVP_DecryptFinal_ex(&ctx,out+outl,&outl2))
2170Sstevel@tonic-gate 	    {
2180Sstevel@tonic-gate 	    fprintf(stderr,"DecryptFinal failed\n");
219*2139Sjp161948 	    ERR_print_errors_fp(stderr);
2200Sstevel@tonic-gate 	    test1_exit(7);
2210Sstevel@tonic-gate 	    }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if(outl+outl2 != cn)
2240Sstevel@tonic-gate 	    {
2250Sstevel@tonic-gate 	    fprintf(stderr,"Plaintext length mismatch got %d expected %d\n",
2260Sstevel@tonic-gate 		    outl+outl2,cn);
2270Sstevel@tonic-gate 	    test1_exit(8);
2280Sstevel@tonic-gate 	    }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	if(memcmp(out,plaintext,cn))
2310Sstevel@tonic-gate 	    {
2320Sstevel@tonic-gate 	    fprintf(stderr,"Plaintext mismatch\n");
2330Sstevel@tonic-gate 	    hexdump(stderr,"Got",out,cn);
2340Sstevel@tonic-gate 	    hexdump(stderr,"Expected",plaintext,cn);
2350Sstevel@tonic-gate 	    test1_exit(9);
2360Sstevel@tonic-gate 	    }
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate     EVP_CIPHER_CTX_cleanup(&ctx);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate     printf("\n");
2420Sstevel@tonic-gate     }
2430Sstevel@tonic-gate 
test_cipher(const char * cipher,const unsigned char * key,int kn,const unsigned char * iv,int in,const unsigned char * plaintext,int pn,const unsigned char * ciphertext,int cn,int encdec)2440Sstevel@tonic-gate static int test_cipher(const char *cipher,const unsigned char *key,int kn,
2450Sstevel@tonic-gate 		       const unsigned char *iv,int in,
2460Sstevel@tonic-gate 		       const unsigned char *plaintext,int pn,
2470Sstevel@tonic-gate 		       const unsigned char *ciphertext,int cn,
2480Sstevel@tonic-gate 		       int encdec)
2490Sstevel@tonic-gate     {
2500Sstevel@tonic-gate     const EVP_CIPHER *c;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate     c=EVP_get_cipherbyname(cipher);
2530Sstevel@tonic-gate     if(!c)
2540Sstevel@tonic-gate 	return 0;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate     test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate     return 1;
2590Sstevel@tonic-gate     }
2600Sstevel@tonic-gate 
test_digest(const char * digest,const unsigned char * plaintext,int pn,const unsigned char * ciphertext,unsigned int cn)2610Sstevel@tonic-gate static int test_digest(const char *digest,
2620Sstevel@tonic-gate 		       const unsigned char *plaintext,int pn,
2630Sstevel@tonic-gate 		       const unsigned char *ciphertext, unsigned int cn)
2640Sstevel@tonic-gate     {
2650Sstevel@tonic-gate     const EVP_MD *d;
2660Sstevel@tonic-gate     EVP_MD_CTX ctx;
2670Sstevel@tonic-gate     unsigned char md[EVP_MAX_MD_SIZE];
2680Sstevel@tonic-gate     unsigned int mdn;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate     d=EVP_get_digestbyname(digest);
2710Sstevel@tonic-gate     if(!d)
2720Sstevel@tonic-gate 	return 0;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate     printf("Testing digest %s\n",EVP_MD_name(d));
2750Sstevel@tonic-gate     hexdump(stdout,"Plaintext",plaintext,pn);
2760Sstevel@tonic-gate     hexdump(stdout,"Digest",ciphertext,cn);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate     EVP_MD_CTX_init(&ctx);
2790Sstevel@tonic-gate     if(!EVP_DigestInit_ex(&ctx,d, NULL))
2800Sstevel@tonic-gate 	{
2810Sstevel@tonic-gate 	fprintf(stderr,"DigestInit failed\n");
282*2139Sjp161948 	ERR_print_errors_fp(stderr);
2830Sstevel@tonic-gate 	EXIT(100);
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate     if(!EVP_DigestUpdate(&ctx,plaintext,pn))
2860Sstevel@tonic-gate 	{
2870Sstevel@tonic-gate 	fprintf(stderr,"DigestUpdate failed\n");
288*2139Sjp161948 	ERR_print_errors_fp(stderr);
2890Sstevel@tonic-gate 	EXIT(101);
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate     if(!EVP_DigestFinal_ex(&ctx,md,&mdn))
2920Sstevel@tonic-gate 	{
2930Sstevel@tonic-gate 	fprintf(stderr,"DigestFinal failed\n");
294*2139Sjp161948 	ERR_print_errors_fp(stderr);
2950Sstevel@tonic-gate 	EXIT(101);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate     EVP_MD_CTX_cleanup(&ctx);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate     if(mdn != cn)
3000Sstevel@tonic-gate 	{
3010Sstevel@tonic-gate 	fprintf(stderr,"Digest length mismatch, got %d expected %d\n",mdn,cn);
3020Sstevel@tonic-gate 	EXIT(102);
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate     if(memcmp(md,ciphertext,cn))
3060Sstevel@tonic-gate 	{
3070Sstevel@tonic-gate 	fprintf(stderr,"Digest mismatch\n");
3080Sstevel@tonic-gate 	hexdump(stderr,"Got",md,cn);
3090Sstevel@tonic-gate 	hexdump(stderr,"Expected",ciphertext,cn);
3100Sstevel@tonic-gate 	EXIT(103);
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate     printf("\n");
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate     EVP_MD_CTX_cleanup(&ctx);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate     return 1;
3180Sstevel@tonic-gate     }
3190Sstevel@tonic-gate 
main(int argc,char ** argv)3200Sstevel@tonic-gate int main(int argc,char **argv)
3210Sstevel@tonic-gate     {
3220Sstevel@tonic-gate     const char *szTestFile;
3230Sstevel@tonic-gate     FILE *f;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate     if(argc != 2)
3260Sstevel@tonic-gate 	{
3270Sstevel@tonic-gate 	fprintf(stderr,"%s <test file>\n",argv[0]);
3280Sstevel@tonic-gate 	EXIT(1);
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate     CRYPTO_malloc_debug_init();
3310Sstevel@tonic-gate     CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
3320Sstevel@tonic-gate     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate     szTestFile=argv[1];
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate     f=fopen(szTestFile,"r");
3370Sstevel@tonic-gate     if(!f)
3380Sstevel@tonic-gate 	{
3390Sstevel@tonic-gate 	perror(szTestFile);
3400Sstevel@tonic-gate 	EXIT(2);
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate     /* Load up the software EVP_CIPHER and EVP_MD definitions */
3440Sstevel@tonic-gate     OpenSSL_add_all_ciphers();
3450Sstevel@tonic-gate     OpenSSL_add_all_digests();
3460Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
3470Sstevel@tonic-gate     /* Load all compiled-in ENGINEs */
3480Sstevel@tonic-gate     ENGINE_load_builtin_engines();
3490Sstevel@tonic-gate #endif
3500Sstevel@tonic-gate #if 0
3510Sstevel@tonic-gate     OPENSSL_config();
3520Sstevel@tonic-gate #endif
3530Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
3540Sstevel@tonic-gate     /* Register all available ENGINE implementations of ciphers and digests.
3550Sstevel@tonic-gate      * This could perhaps be changed to "ENGINE_register_all_complete()"? */
3560Sstevel@tonic-gate     ENGINE_register_all_ciphers();
3570Sstevel@tonic-gate     ENGINE_register_all_digests();
3580Sstevel@tonic-gate     /* If we add command-line options, this statement should be switchable.
3590Sstevel@tonic-gate      * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if
3600Sstevel@tonic-gate      * they weren't already initialised. */
3610Sstevel@tonic-gate     /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */
3620Sstevel@tonic-gate #endif
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate     for( ; ; )
3650Sstevel@tonic-gate 	{
3660Sstevel@tonic-gate 	char line[4096];
3670Sstevel@tonic-gate 	char *p;
3680Sstevel@tonic-gate 	char *cipher;
3690Sstevel@tonic-gate 	unsigned char *iv,*key,*plaintext,*ciphertext;
3700Sstevel@tonic-gate 	int encdec;
3710Sstevel@tonic-gate 	int kn,in,pn,cn;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	if(!fgets((char *)line,sizeof line,f))
3740Sstevel@tonic-gate 	    break;
3750Sstevel@tonic-gate 	if(line[0] == '#' || line[0] == '\n')
3760Sstevel@tonic-gate 	    continue;
3770Sstevel@tonic-gate 	p=line;
3780Sstevel@tonic-gate 	cipher=sstrsep(&p,":");
3790Sstevel@tonic-gate 	key=ustrsep(&p,":");
3800Sstevel@tonic-gate 	iv=ustrsep(&p,":");
3810Sstevel@tonic-gate 	plaintext=ustrsep(&p,":");
3820Sstevel@tonic-gate 	ciphertext=ustrsep(&p,":");
3830Sstevel@tonic-gate 	if (p[-1] == '\n') {
3840Sstevel@tonic-gate 	    p[-1] = '\0';
3850Sstevel@tonic-gate 	    encdec = -1;
3860Sstevel@tonic-gate 	} else {
3870Sstevel@tonic-gate 	    encdec = atoi(sstrsep(&p,"\n"));
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	kn=convert(key);
3920Sstevel@tonic-gate 	in=convert(iv);
3930Sstevel@tonic-gate 	pn=convert(plaintext);
3940Sstevel@tonic-gate 	cn=convert(ciphertext);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec)
3970Sstevel@tonic-gate 	   && !test_digest(cipher,plaintext,pn,ciphertext,cn))
3980Sstevel@tonic-gate 	    {
399*2139Sjp161948 #ifdef OPENSSL_NO_AES
400*2139Sjp161948 	    if (strstr(cipher, "AES") == cipher)
401*2139Sjp161948 		{
402*2139Sjp161948 		fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
403*2139Sjp161948 		continue;
404*2139Sjp161948 		}
405*2139Sjp161948 #endif
406*2139Sjp161948 #ifdef OPENSSL_NO_DES
407*2139Sjp161948 	    if (strstr(cipher, "DES") == cipher)
408*2139Sjp161948 		{
409*2139Sjp161948 		fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
410*2139Sjp161948 		continue;
411*2139Sjp161948 		}
412*2139Sjp161948 #endif
413*2139Sjp161948 #ifdef OPENSSL_NO_RC4
414*2139Sjp161948 	    if (strstr(cipher, "RC4") == cipher)
415*2139Sjp161948 		{
416*2139Sjp161948 		fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
417*2139Sjp161948 		continue;
418*2139Sjp161948 		}
419*2139Sjp161948 #endif
4200Sstevel@tonic-gate 	    fprintf(stderr,"Can't find %s\n",cipher);
4210Sstevel@tonic-gate 	    EXIT(3);
4220Sstevel@tonic-gate 	    }
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
4260Sstevel@tonic-gate     ENGINE_cleanup();
4270Sstevel@tonic-gate #endif
4280Sstevel@tonic-gate     EVP_cleanup();
4290Sstevel@tonic-gate     CRYPTO_cleanup_all_ex_data();
4300Sstevel@tonic-gate     ERR_remove_state(0);
4310Sstevel@tonic-gate     ERR_free_strings();
4320Sstevel@tonic-gate     CRYPTO_mem_leaks_fp(stderr);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate     return 0;
4350Sstevel@tonic-gate     }
436