rc4test.c revision 55714
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
6355714Skris#ifdef NO_RC4
6455714Skrisint main(int argc, char *argv[])
6555714Skris{
6655714Skris    printf("No RC4 support\n");
6755714Skris    return(0);
6855714Skris}
6955714Skris#else
7055714Skris#include <openssl/rc4.h>
7155714Skris
7255714Skrisunsigned char keys[7][30]={
7355714Skris	{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
7455714Skris	{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
7555714Skris	{8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
7655714Skris	{4,0xef,0x01,0x23,0x45},
7755714Skris	{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
7855714Skris	{4,0xef,0x01,0x23,0x45},
7955714Skris	};
8055714Skris
8155714Skrisunsigned char data_len[7]={8,8,8,20,28,10};
8255714Skrisunsigned char data[7][30]={
8355714Skris	{0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xff},
8455714Skris	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
8555714Skris	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
8655714Skris	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
8755714Skris	   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
8855714Skris	   0x00,0x00,0x00,0x00,0xff},
8955714Skris	{0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
9055714Skris	   0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
9155714Skris	   0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
9255714Skris	   0x12,0x34,0x56,0x78,0xff},
9355714Skris	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
9455714Skris	{0},
9555714Skris	};
9655714Skris
9755714Skrisunsigned char output[7][30]={
9855714Skris	{0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96,0x00},
9955714Skris	{0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79,0x00},
10055714Skris	{0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a,0x00},
10155714Skris	{0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,
10255714Skris	 0xbd,0x61,0x5a,0x11,0x62,0xe1,0xc7,0xba,
10355714Skris	 0x36,0xb6,0x78,0x58,0x00},
10455714Skris	{0x66,0xa0,0x94,0x9f,0x8a,0xf7,0xd6,0x89,
10555714Skris	 0x1f,0x7f,0x83,0x2b,0xa8,0x33,0xc0,0x0c,
10655714Skris	 0x89,0x2e,0xbe,0x30,0x14,0x3c,0xe2,0x87,
10755714Skris	 0x40,0x01,0x1e,0xcf,0x00},
10855714Skris	{0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61,0x00},
10955714Skris	{0},
11055714Skris	};
11155714Skris
11255714Skrisint main(int argc, char *argv[])
11355714Skris	{
11455714Skris	int i,err=0;
11555714Skris	int j;
11655714Skris	unsigned char *p;
11755714Skris	RC4_KEY key;
11855714Skris	unsigned char buf[512],obuf[512];
11955714Skris
12055714Skris	for (i=0; i<512; i++) buf[i]=0x01;
12155714Skris
12255714Skris	for (i=0; i<6; i++)
12355714Skris		{
12455714Skris		RC4_set_key(&key,keys[i][0],&(keys[i][1]));
12555714Skris		memset(obuf,0x00,sizeof(obuf));
12655714Skris		RC4(&key,data_len[i],&(data[i][0]),obuf);
12755714Skris		if (memcmp(obuf,output[i],data_len[i]+1) != 0)
12855714Skris			{
12955714Skris			printf("error calculating RC4\n");
13055714Skris			printf("output:");
13155714Skris			for (j=0; j<data_len[i]+1; j++)
13255714Skris				printf(" %02x",obuf[j]);
13355714Skris			printf("\n");
13455714Skris			printf("expect:");
13555714Skris			p= &(output[i][0]);
13655714Skris			for (j=0; j<data_len[i]+1; j++)
13755714Skris				printf(" %02x",*(p++));
13855714Skris			printf("\n");
13955714Skris			err++;
14055714Skris			}
14155714Skris		else
14255714Skris			printf("test %d ok\n",i);
14355714Skris		}
14455714Skris	printf("test end processing ");
14555714Skris	for (i=0; i<data_len[3]; i++)
14655714Skris		{
14755714Skris		RC4_set_key(&key,keys[3][0],&(keys[3][1]));
14855714Skris		memset(obuf,0x00,sizeof(obuf));
14955714Skris		RC4(&key,i,&(data[3][0]),obuf);
15055714Skris		if ((memcmp(obuf,output[3],i) != 0) || (obuf[i] != 0))
15155714Skris			{
15255714Skris			printf("error in RC4 length processing\n");
15355714Skris			printf("output:");
15455714Skris			for (j=0; j<i+1; j++)
15555714Skris				printf(" %02x",obuf[j]);
15655714Skris			printf("\n");
15755714Skris			printf("expect:");
15855714Skris			p= &(output[3][0]);
15955714Skris			for (j=0; j<i; j++)
16055714Skris				printf(" %02x",*(p++));
16155714Skris			printf(" 00\n");
16255714Skris			err++;
16355714Skris			}
16455714Skris		else
16555714Skris			{
16655714Skris			printf(".");
16755714Skris			fflush(stdout);
16855714Skris			}
16955714Skris		}
17055714Skris	printf("done\n");
17155714Skris	printf("test multi-call ");
17255714Skris	for (i=0; i<data_len[3]; i++)
17355714Skris		{
17455714Skris		RC4_set_key(&key,keys[3][0],&(keys[3][1]));
17555714Skris		memset(obuf,0x00,sizeof(obuf));
17655714Skris		RC4(&key,i,&(data[3][0]),obuf);
17755714Skris		RC4(&key,data_len[3]-i,&(data[3][i]),&(obuf[i]));
17855714Skris		if (memcmp(obuf,output[3],data_len[3]+1) != 0)
17955714Skris			{
18055714Skris			printf("error in RC4 multi-call processing\n");
18155714Skris			printf("output:");
18255714Skris			for (j=0; j<data_len[3]+1; j++)
18355714Skris				printf(" %02x",obuf[j]);
18455714Skris			printf("\n");
18555714Skris			printf("expect:");
18655714Skris			p= &(output[3][0]);
18755714Skris			for (j=0; j<data_len[3]+1; j++)
18855714Skris				printf(" %02x",*(p++));
18955714Skris			err++;
19055714Skris			}
19155714Skris		else
19255714Skris			{
19355714Skris			printf(".");
19455714Skris			fflush(stdout);
19555714Skris			}
19655714Skris		}
19755714Skris	printf("done\n");
19855714Skris	exit(err);
19955714Skris	return(0);
20055714Skris	}
20155714Skris#endif
202