155714Skris/* crypto/rc4/rc4test.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include <stdlib.h>
6155714Skris#include <string.h>
6255714Skris
63109998Smarkm#include "../e_os.h"
64109998Smarkm
65109998Smarkm#ifdef OPENSSL_NO_RC4
6655714Skrisint main(int argc, char *argv[])
6755714Skris{
6855714Skris    printf("No RC4 support\n");
6955714Skris    return(0);
7055714Skris}
7155714Skris#else
7255714Skris#include <openssl/rc4.h>
73160814Ssimon#include <openssl/sha.h>
7455714Skris
7559191Skrisstatic unsigned char keys[7][30]={
7655714Skris	{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
7755714Skris	{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
7855714Skris	{8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
7955714Skris	{4,0xef,0x01,0x23,0x45},
8055714Skris	{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
8155714Skris	{4,0xef,0x01,0x23,0x45},
8255714Skris	};
8355714Skris
8459191Skrisstatic unsigned char data_len[7]={8,8,8,20,28,10};
8559191Skrisstatic unsigned char data[7][30]={
8655714Skris	{0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xff},
8755714Skris	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
8855714Skris	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
8955714Skris	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
9055714Skris	   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
9155714Skris	   0x00,0x00,0x00,0x00,0xff},
9255714Skris	{0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
9355714Skris	   0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
9455714Skris	   0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
9555714Skris	   0x12,0x34,0x56,0x78,0xff},
9655714Skris	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
9755714Skris	{0},
9855714Skris	};
9955714Skris
10059191Skrisstatic unsigned char output[7][30]={
10155714Skris	{0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96,0x00},
10255714Skris	{0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79,0x00},
10355714Skris	{0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a,0x00},
10455714Skris	{0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,
10555714Skris	 0xbd,0x61,0x5a,0x11,0x62,0xe1,0xc7,0xba,
10655714Skris	 0x36,0xb6,0x78,0x58,0x00},
10755714Skris	{0x66,0xa0,0x94,0x9f,0x8a,0xf7,0xd6,0x89,
10855714Skris	 0x1f,0x7f,0x83,0x2b,0xa8,0x33,0xc0,0x0c,
10955714Skris	 0x89,0x2e,0xbe,0x30,0x14,0x3c,0xe2,0x87,
11055714Skris	 0x40,0x01,0x1e,0xcf,0x00},
11155714Skris	{0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61,0x00},
11255714Skris	{0},
11355714Skris	};
11455714Skris
11555714Skrisint main(int argc, char *argv[])
11655714Skris	{
117238405Sjkim	int i,err=0;
118238405Sjkim	int j;
11955714Skris	unsigned char *p;
12055714Skris	RC4_KEY key;
121160814Ssimon	unsigned char obuf[512];
12255714Skris
123238405Sjkim#if !defined(OPENSSL_PIC)
124238405Sjkim	void OPENSSL_cpuid_setup(void);
125238405Sjkim
126238405Sjkim	OPENSSL_cpuid_setup();
127238405Sjkim#endif
128238405Sjkim
12955714Skris	for (i=0; i<6; i++)
13055714Skris		{
13155714Skris		RC4_set_key(&key,keys[i][0],&(keys[i][1]));
13255714Skris		memset(obuf,0x00,sizeof(obuf));
13355714Skris		RC4(&key,data_len[i],&(data[i][0]),obuf);
13455714Skris		if (memcmp(obuf,output[i],data_len[i]+1) != 0)
13555714Skris			{
13655714Skris			printf("error calculating RC4\n");
13755714Skris			printf("output:");
138238405Sjkim			for (j=0; j<data_len[i]+1; j++)
13955714Skris				printf(" %02x",obuf[j]);
14055714Skris			printf("\n");
14155714Skris			printf("expect:");
14255714Skris			p= &(output[i][0]);
143238405Sjkim			for (j=0; j<data_len[i]+1; j++)
14455714Skris				printf(" %02x",*(p++));
14555714Skris			printf("\n");
14655714Skris			err++;
14755714Skris			}
14855714Skris		else
14955714Skris			printf("test %d ok\n",i);
15055714Skris		}
15155714Skris	printf("test end processing ");
15255714Skris	for (i=0; i<data_len[3]; i++)
15355714Skris		{
15455714Skris		RC4_set_key(&key,keys[3][0],&(keys[3][1]));
15555714Skris		memset(obuf,0x00,sizeof(obuf));
15655714Skris		RC4(&key,i,&(data[3][0]),obuf);
15755714Skris		if ((memcmp(obuf,output[3],i) != 0) || (obuf[i] != 0))
15855714Skris			{
15955714Skris			printf("error in RC4 length processing\n");
16055714Skris			printf("output:");
16155714Skris			for (j=0; j<i+1; j++)
16255714Skris				printf(" %02x",obuf[j]);
16355714Skris			printf("\n");
16455714Skris			printf("expect:");
16555714Skris			p= &(output[3][0]);
16655714Skris			for (j=0; j<i; j++)
16755714Skris				printf(" %02x",*(p++));
16855714Skris			printf(" 00\n");
16955714Skris			err++;
17055714Skris			}
17155714Skris		else
17255714Skris			{
17355714Skris			printf(".");
17455714Skris			fflush(stdout);
17555714Skris			}
17655714Skris		}
17755714Skris	printf("done\n");
17855714Skris	printf("test multi-call ");
17955714Skris	for (i=0; i<data_len[3]; i++)
18055714Skris		{
18155714Skris		RC4_set_key(&key,keys[3][0],&(keys[3][1]));
18255714Skris		memset(obuf,0x00,sizeof(obuf));
18355714Skris		RC4(&key,i,&(data[3][0]),obuf);
18455714Skris		RC4(&key,data_len[3]-i,&(data[3][i]),&(obuf[i]));
18555714Skris		if (memcmp(obuf,output[3],data_len[3]+1) != 0)
18655714Skris			{
18755714Skris			printf("error in RC4 multi-call processing\n");
18855714Skris			printf("output:");
189238405Sjkim			for (j=0; j<data_len[3]+1; j++)
19055714Skris				printf(" %02x",obuf[j]);
19155714Skris			printf("\n");
19255714Skris			printf("expect:");
19355714Skris			p= &(output[3][0]);
194238405Sjkim			for (j=0; j<data_len[3]+1; j++)
19555714Skris				printf(" %02x",*(p++));
19655714Skris			err++;
19755714Skris			}
19855714Skris		else
19955714Skris			{
20055714Skris			printf(".");
20155714Skris			fflush(stdout);
20255714Skris			}
20355714Skris		}
20455714Skris	printf("done\n");
205160814Ssimon	printf("bulk test ");
206160814Ssimon	{   unsigned char buf[513];
207160814Ssimon	    SHA_CTX c;
208160814Ssimon	    unsigned char md[SHA_DIGEST_LENGTH];
209160814Ssimon	    static unsigned char expected[]={
210160814Ssimon		0xa4,0x7b,0xcc,0x00,0x3d,0xd0,0xbd,0xe1,0xac,0x5f,
211160814Ssimon		0x12,0x1e,0x45,0xbc,0xfb,0x1a,0xa1,0xf2,0x7f,0xc5 };
212160814Ssimon
213160814Ssimon		RC4_set_key(&key,keys[0][0],&(keys[3][1]));
214160814Ssimon		memset(buf,'\0',sizeof(buf));
215160814Ssimon		SHA1_Init(&c);
216160814Ssimon		for (i=0;i<2571;i++) {
217160814Ssimon			RC4(&key,sizeof(buf),buf,buf);
218160814Ssimon			SHA1_Update(&c,buf,sizeof(buf));
219160814Ssimon		}
220160814Ssimon		SHA1_Final(md,&c);
221160814Ssimon
222160814Ssimon		if (memcmp(md,expected,sizeof(md))) {
223160814Ssimon			printf("error in RC4 bulk test\n");
224160814Ssimon			printf("output:");
225238405Sjkim			for (j=0; j<(int)sizeof(md); j++)
226160814Ssimon				printf(" %02x",md[j]);
227160814Ssimon			printf("\n");
228160814Ssimon			printf("expect:");
229238405Sjkim			for (j=0; j<(int)sizeof(md); j++)
230160814Ssimon				printf(" %02x",expected[j]);
231160814Ssimon			printf("\n");
232160814Ssimon			err++;
233160814Ssimon		}
234160814Ssimon		else	printf("ok\n");
235160814Ssimon	}
236160814Ssimon#ifdef OPENSSL_SYS_NETWARE
237160814Ssimon    if (err) printf("ERROR: %d\n", err);
238160814Ssimon#endif
239109998Smarkm	EXIT(err);
24055714Skris	return(0);
24155714Skris	}
24255714Skris#endif
243