xref: /onnv-gate/usr/src/common/openssl/crypto/rc4/rc4test.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/rc4/rc4test.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <stdlib.h>
610Sstevel@tonic-gate #include <string.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include "../e_os.h"
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #ifdef OPENSSL_NO_RC4
main(int argc,char * argv[])660Sstevel@tonic-gate int main(int argc, char *argv[])
670Sstevel@tonic-gate {
680Sstevel@tonic-gate     printf("No RC4 support\n");
690Sstevel@tonic-gate     return(0);
700Sstevel@tonic-gate }
710Sstevel@tonic-gate #else
720Sstevel@tonic-gate #include <openssl/rc4.h>
73*2139Sjp161948 #include <openssl/sha.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate static unsigned char keys[7][30]={
760Sstevel@tonic-gate 	{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
770Sstevel@tonic-gate 	{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
780Sstevel@tonic-gate 	{8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
790Sstevel@tonic-gate 	{4,0xef,0x01,0x23,0x45},
800Sstevel@tonic-gate 	{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
810Sstevel@tonic-gate 	{4,0xef,0x01,0x23,0x45},
820Sstevel@tonic-gate 	};
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static unsigned char data_len[7]={8,8,8,20,28,10};
850Sstevel@tonic-gate static unsigned char data[7][30]={
860Sstevel@tonic-gate 	{0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xff},
870Sstevel@tonic-gate 	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
880Sstevel@tonic-gate 	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
890Sstevel@tonic-gate 	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
900Sstevel@tonic-gate 	   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
910Sstevel@tonic-gate 	   0x00,0x00,0x00,0x00,0xff},
920Sstevel@tonic-gate 	{0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
930Sstevel@tonic-gate 	   0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
940Sstevel@tonic-gate 	   0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
950Sstevel@tonic-gate 	   0x12,0x34,0x56,0x78,0xff},
960Sstevel@tonic-gate 	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
970Sstevel@tonic-gate 	{0},
980Sstevel@tonic-gate 	};
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate static unsigned char output[7][30]={
1010Sstevel@tonic-gate 	{0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96,0x00},
1020Sstevel@tonic-gate 	{0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79,0x00},
1030Sstevel@tonic-gate 	{0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a,0x00},
1040Sstevel@tonic-gate 	{0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,
1050Sstevel@tonic-gate 	 0xbd,0x61,0x5a,0x11,0x62,0xe1,0xc7,0xba,
1060Sstevel@tonic-gate 	 0x36,0xb6,0x78,0x58,0x00},
1070Sstevel@tonic-gate 	{0x66,0xa0,0x94,0x9f,0x8a,0xf7,0xd6,0x89,
1080Sstevel@tonic-gate 	 0x1f,0x7f,0x83,0x2b,0xa8,0x33,0xc0,0x0c,
1090Sstevel@tonic-gate 	 0x89,0x2e,0xbe,0x30,0x14,0x3c,0xe2,0x87,
1100Sstevel@tonic-gate 	 0x40,0x01,0x1e,0xcf,0x00},
1110Sstevel@tonic-gate 	{0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61,0x00},
1120Sstevel@tonic-gate 	{0},
1130Sstevel@tonic-gate 	};
1140Sstevel@tonic-gate 
main(int argc,char * argv[])1150Sstevel@tonic-gate int main(int argc, char *argv[])
1160Sstevel@tonic-gate 	{
117*2139Sjp161948 	int err=0;
118*2139Sjp161948 	unsigned int i, j;
1190Sstevel@tonic-gate 	unsigned char *p;
1200Sstevel@tonic-gate 	RC4_KEY key;
121*2139Sjp161948 	unsigned char obuf[512];
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	for (i=0; i<6; i++)
1240Sstevel@tonic-gate 		{
1250Sstevel@tonic-gate 		RC4_set_key(&key,keys[i][0],&(keys[i][1]));
1260Sstevel@tonic-gate 		memset(obuf,0x00,sizeof(obuf));
1270Sstevel@tonic-gate 		RC4(&key,data_len[i],&(data[i][0]),obuf);
1280Sstevel@tonic-gate 		if (memcmp(obuf,output[i],data_len[i]+1) != 0)
1290Sstevel@tonic-gate 			{
1300Sstevel@tonic-gate 			printf("error calculating RC4\n");
1310Sstevel@tonic-gate 			printf("output:");
132*2139Sjp161948 			for (j=0; j<data_len[i]+1U; j++)
1330Sstevel@tonic-gate 				printf(" %02x",obuf[j]);
1340Sstevel@tonic-gate 			printf("\n");
1350Sstevel@tonic-gate 			printf("expect:");
1360Sstevel@tonic-gate 			p= &(output[i][0]);
137*2139Sjp161948 			for (j=0; j<data_len[i]+1U; j++)
1380Sstevel@tonic-gate 				printf(" %02x",*(p++));
1390Sstevel@tonic-gate 			printf("\n");
1400Sstevel@tonic-gate 			err++;
1410Sstevel@tonic-gate 			}
1420Sstevel@tonic-gate 		else
1430Sstevel@tonic-gate 			printf("test %d ok\n",i);
1440Sstevel@tonic-gate 		}
1450Sstevel@tonic-gate 	printf("test end processing ");
1460Sstevel@tonic-gate 	for (i=0; i<data_len[3]; i++)
1470Sstevel@tonic-gate 		{
1480Sstevel@tonic-gate 		RC4_set_key(&key,keys[3][0],&(keys[3][1]));
1490Sstevel@tonic-gate 		memset(obuf,0x00,sizeof(obuf));
1500Sstevel@tonic-gate 		RC4(&key,i,&(data[3][0]),obuf);
1510Sstevel@tonic-gate 		if ((memcmp(obuf,output[3],i) != 0) || (obuf[i] != 0))
1520Sstevel@tonic-gate 			{
1530Sstevel@tonic-gate 			printf("error in RC4 length processing\n");
1540Sstevel@tonic-gate 			printf("output:");
1550Sstevel@tonic-gate 			for (j=0; j<i+1; j++)
1560Sstevel@tonic-gate 				printf(" %02x",obuf[j]);
1570Sstevel@tonic-gate 			printf("\n");
1580Sstevel@tonic-gate 			printf("expect:");
1590Sstevel@tonic-gate 			p= &(output[3][0]);
1600Sstevel@tonic-gate 			for (j=0; j<i; j++)
1610Sstevel@tonic-gate 				printf(" %02x",*(p++));
1620Sstevel@tonic-gate 			printf(" 00\n");
1630Sstevel@tonic-gate 			err++;
1640Sstevel@tonic-gate 			}
1650Sstevel@tonic-gate 		else
1660Sstevel@tonic-gate 			{
1670Sstevel@tonic-gate 			printf(".");
1680Sstevel@tonic-gate 			fflush(stdout);
1690Sstevel@tonic-gate 			}
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 	printf("done\n");
1720Sstevel@tonic-gate 	printf("test multi-call ");
1730Sstevel@tonic-gate 	for (i=0; i<data_len[3]; i++)
1740Sstevel@tonic-gate 		{
1750Sstevel@tonic-gate 		RC4_set_key(&key,keys[3][0],&(keys[3][1]));
1760Sstevel@tonic-gate 		memset(obuf,0x00,sizeof(obuf));
1770Sstevel@tonic-gate 		RC4(&key,i,&(data[3][0]),obuf);
1780Sstevel@tonic-gate 		RC4(&key,data_len[3]-i,&(data[3][i]),&(obuf[i]));
1790Sstevel@tonic-gate 		if (memcmp(obuf,output[3],data_len[3]+1) != 0)
1800Sstevel@tonic-gate 			{
1810Sstevel@tonic-gate 			printf("error in RC4 multi-call processing\n");
1820Sstevel@tonic-gate 			printf("output:");
183*2139Sjp161948 			for (j=0; j<data_len[3]+1U; j++)
1840Sstevel@tonic-gate 				printf(" %02x",obuf[j]);
1850Sstevel@tonic-gate 			printf("\n");
1860Sstevel@tonic-gate 			printf("expect:");
1870Sstevel@tonic-gate 			p= &(output[3][0]);
188*2139Sjp161948 			for (j=0; j<data_len[3]+1U; j++)
1890Sstevel@tonic-gate 				printf(" %02x",*(p++));
1900Sstevel@tonic-gate 			err++;
1910Sstevel@tonic-gate 			}
1920Sstevel@tonic-gate 		else
1930Sstevel@tonic-gate 			{
1940Sstevel@tonic-gate 			printf(".");
1950Sstevel@tonic-gate 			fflush(stdout);
1960Sstevel@tonic-gate 			}
1970Sstevel@tonic-gate 		}
1980Sstevel@tonic-gate 	printf("done\n");
199*2139Sjp161948 	printf("bulk test ");
200*2139Sjp161948 	{   unsigned char buf[513];
201*2139Sjp161948 	    SHA_CTX c;
202*2139Sjp161948 	    unsigned char md[SHA_DIGEST_LENGTH];
203*2139Sjp161948 	    static unsigned char expected[]={
204*2139Sjp161948 		0xa4,0x7b,0xcc,0x00,0x3d,0xd0,0xbd,0xe1,0xac,0x5f,
205*2139Sjp161948 		0x12,0x1e,0x45,0xbc,0xfb,0x1a,0xa1,0xf2,0x7f,0xc5 };
206*2139Sjp161948 
207*2139Sjp161948 		RC4_set_key(&key,keys[0][0],&(keys[3][1]));
208*2139Sjp161948 		memset(buf,'\0',sizeof(buf));
209*2139Sjp161948 		SHA1_Init(&c);
210*2139Sjp161948 		for (i=0;i<2571;i++) {
211*2139Sjp161948 			RC4(&key,sizeof(buf),buf,buf);
212*2139Sjp161948 			SHA1_Update(&c,buf,sizeof(buf));
213*2139Sjp161948 		}
214*2139Sjp161948 		SHA1_Final(md,&c);
215*2139Sjp161948 
216*2139Sjp161948 		if (memcmp(md,expected,sizeof(md))) {
217*2139Sjp161948 			printf("error in RC4 bulk test\n");
218*2139Sjp161948 			printf("output:");
219*2139Sjp161948 			for (j=0; j<sizeof(md); j++)
220*2139Sjp161948 				printf(" %02x",md[j]);
221*2139Sjp161948 			printf("\n");
222*2139Sjp161948 			printf("expect:");
223*2139Sjp161948 			for (j=0; j<sizeof(md); j++)
224*2139Sjp161948 				printf(" %02x",expected[j]);
225*2139Sjp161948 			printf("\n");
226*2139Sjp161948 			err++;
227*2139Sjp161948 		}
228*2139Sjp161948 		else	printf("ok\n");
229*2139Sjp161948 	}
230*2139Sjp161948 #ifdef OPENSSL_SYS_NETWARE
231*2139Sjp161948     if (err) printf("ERROR: %d\n", err);
232*2139Sjp161948 #endif
2330Sstevel@tonic-gate 	EXIT(err);
2340Sstevel@tonic-gate 	return(0);
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate #endif
237