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.
8280297Sjkim *
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).
15280297Sjkim *
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.
22280297Sjkim *
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 :-).
37280297Sjkim * 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)"
40280297Sjkim *
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.
52280297Sjkim *
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");
69280297Sjkim    return (0);
7055714Skris}
7155714Skris#else
72280297Sjkim# include <openssl/rc4.h>
73280297Sjkim# include <openssl/sha.h>
7455714Skris
75280297Sjkimstatic unsigned char keys[7][30] = {
76280297Sjkim    {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
77280297Sjkim    {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
78280297Sjkim    {8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
79280297Sjkim    {4, 0xef, 0x01, 0x23, 0x45},
80280297Sjkim    {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
81280297Sjkim    {4, 0xef, 0x01, 0x23, 0x45},
82280297Sjkim};
8355714Skris
84280297Sjkimstatic unsigned char data_len[7] = { 8, 8, 8, 20, 28, 10 };
8555714Skris
86280297Sjkimstatic unsigned char data[7][30] = {
87280297Sjkim    {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff},
88280297Sjkim    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff},
89280297Sjkim    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff},
90280297Sjkim    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91280297Sjkim     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92280297Sjkim     0x00, 0x00, 0x00, 0x00, 0xff},
93280297Sjkim    {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
94280297Sjkim     0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
95280297Sjkim     0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
96280297Sjkim     0x12, 0x34, 0x56, 0x78, 0xff},
97280297Sjkim    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff},
98280297Sjkim    {0},
99280297Sjkim};
10055714Skris
101280297Sjkimstatic unsigned char output[7][30] = {
102280297Sjkim    {0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96, 0x00},
103280297Sjkim    {0x74, 0x94, 0xc2, 0xe7, 0x10, 0x4b, 0x08, 0x79, 0x00},
104280297Sjkim    {0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a, 0x00},
105280297Sjkim    {0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf,
106280297Sjkim     0xbd, 0x61, 0x5a, 0x11, 0x62, 0xe1, 0xc7, 0xba,
107280297Sjkim     0x36, 0xb6, 0x78, 0x58, 0x00},
108280297Sjkim    {0x66, 0xa0, 0x94, 0x9f, 0x8a, 0xf7, 0xd6, 0x89,
109280297Sjkim     0x1f, 0x7f, 0x83, 0x2b, 0xa8, 0x33, 0xc0, 0x0c,
110280297Sjkim     0x89, 0x2e, 0xbe, 0x30, 0x14, 0x3c, 0xe2, 0x87,
111280297Sjkim     0x40, 0x01, 0x1e, 0xcf, 0x00},
112280297Sjkim    {0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf, 0xbd, 0x61, 0x00},
113280297Sjkim    {0},
114280297Sjkim};
115280297Sjkim
11655714Skrisint main(int argc, char *argv[])
117280297Sjkim{
118280297Sjkim    int i, err = 0;
119280297Sjkim    int j;
120280297Sjkim    unsigned char *p;
121280297Sjkim    RC4_KEY key;
122280297Sjkim    unsigned char obuf[512];
12355714Skris
124280297Sjkim# if !defined(OPENSSL_PIC)
125280297Sjkim    void OPENSSL_cpuid_setup(void);
126238405Sjkim
127280297Sjkim    OPENSSL_cpuid_setup();
128280297Sjkim# endif
129238405Sjkim
130280297Sjkim    for (i = 0; i < 6; i++) {
131280297Sjkim        RC4_set_key(&key, keys[i][0], &(keys[i][1]));
132280297Sjkim        memset(obuf, 0x00, sizeof(obuf));
133280297Sjkim        RC4(&key, data_len[i], &(data[i][0]), obuf);
134280297Sjkim        if (memcmp(obuf, output[i], data_len[i] + 1) != 0) {
135280297Sjkim            printf("error calculating RC4\n");
136280297Sjkim            printf("output:");
137280297Sjkim            for (j = 0; j < data_len[i] + 1; j++)
138280297Sjkim                printf(" %02x", obuf[j]);
139280297Sjkim            printf("\n");
140280297Sjkim            printf("expect:");
141280297Sjkim            p = &(output[i][0]);
142280297Sjkim            for (j = 0; j < data_len[i] + 1; j++)
143280297Sjkim                printf(" %02x", *(p++));
144280297Sjkim            printf("\n");
145280297Sjkim            err++;
146280297Sjkim        } else
147280297Sjkim            printf("test %d ok\n", i);
148280297Sjkim    }
149280297Sjkim    printf("test end processing ");
150280297Sjkim    for (i = 0; i < data_len[3]; i++) {
151280297Sjkim        RC4_set_key(&key, keys[3][0], &(keys[3][1]));
152280297Sjkim        memset(obuf, 0x00, sizeof(obuf));
153280297Sjkim        RC4(&key, i, &(data[3][0]), obuf);
154280297Sjkim        if ((memcmp(obuf, output[3], i) != 0) || (obuf[i] != 0)) {
155280297Sjkim            printf("error in RC4 length processing\n");
156280297Sjkim            printf("output:");
157280297Sjkim            for (j = 0; j < i + 1; j++)
158280297Sjkim                printf(" %02x", obuf[j]);
159280297Sjkim            printf("\n");
160280297Sjkim            printf("expect:");
161280297Sjkim            p = &(output[3][0]);
162280297Sjkim            for (j = 0; j < i; j++)
163280297Sjkim                printf(" %02x", *(p++));
164280297Sjkim            printf(" 00\n");
165280297Sjkim            err++;
166280297Sjkim        } else {
167280297Sjkim            printf(".");
168280297Sjkim            fflush(stdout);
169280297Sjkim        }
170280297Sjkim    }
171280297Sjkim    printf("done\n");
172280297Sjkim    printf("test multi-call ");
173280297Sjkim    for (i = 0; i < data_len[3]; i++) {
174280297Sjkim        RC4_set_key(&key, keys[3][0], &(keys[3][1]));
175280297Sjkim        memset(obuf, 0x00, sizeof(obuf));
176280297Sjkim        RC4(&key, i, &(data[3][0]), obuf);
177280297Sjkim        RC4(&key, data_len[3] - i, &(data[3][i]), &(obuf[i]));
178280297Sjkim        if (memcmp(obuf, output[3], data_len[3] + 1) != 0) {
179280297Sjkim            printf("error in RC4 multi-call processing\n");
180280297Sjkim            printf("output:");
181280297Sjkim            for (j = 0; j < data_len[3] + 1; j++)
182280297Sjkim                printf(" %02x", obuf[j]);
183280297Sjkim            printf("\n");
184280297Sjkim            printf("expect:");
185280297Sjkim            p = &(output[3][0]);
186280297Sjkim            for (j = 0; j < data_len[3] + 1; j++)
187280297Sjkim                printf(" %02x", *(p++));
188280297Sjkim            err++;
189280297Sjkim        } else {
190280297Sjkim            printf(".");
191280297Sjkim            fflush(stdout);
192280297Sjkim        }
193280297Sjkim    }
194280297Sjkim    printf("done\n");
195280297Sjkim    printf("bulk test ");
196280297Sjkim    {
197280297Sjkim        unsigned char buf[513];
198280297Sjkim        SHA_CTX c;
199280297Sjkim        unsigned char md[SHA_DIGEST_LENGTH];
200280297Sjkim        static unsigned char expected[] = {
201280297Sjkim            0xa4, 0x7b, 0xcc, 0x00, 0x3d, 0xd0, 0xbd, 0xe1, 0xac, 0x5f,
202280297Sjkim            0x12, 0x1e, 0x45, 0xbc, 0xfb, 0x1a, 0xa1, 0xf2, 0x7f, 0xc5
203280297Sjkim        };
204160814Ssimon
205280297Sjkim        RC4_set_key(&key, keys[0][0], &(keys[3][1]));
206280297Sjkim        memset(buf, '\0', sizeof(buf));
207280297Sjkim        SHA1_Init(&c);
208280297Sjkim        for (i = 0; i < 2571; i++) {
209280297Sjkim            RC4(&key, sizeof(buf), buf, buf);
210280297Sjkim            SHA1_Update(&c, buf, sizeof(buf));
211280297Sjkim        }
212280297Sjkim        SHA1_Final(md, &c);
213160814Ssimon
214280297Sjkim        if (memcmp(md, expected, sizeof(md))) {
215280297Sjkim            printf("error in RC4 bulk test\n");
216280297Sjkim            printf("output:");
217280297Sjkim            for (j = 0; j < (int)sizeof(md); j++)
218280297Sjkim                printf(" %02x", md[j]);
219280297Sjkim            printf("\n");
220280297Sjkim            printf("expect:");
221280297Sjkim            for (j = 0; j < (int)sizeof(md); j++)
222280297Sjkim                printf(" %02x", expected[j]);
223280297Sjkim            printf("\n");
224280297Sjkim            err++;
225280297Sjkim        } else
226280297Sjkim            printf("ok\n");
227280297Sjkim    }
228280297Sjkim# ifdef OPENSSL_SYS_NETWARE
229280297Sjkim    if (err)
230280297Sjkim        printf("ERROR: %d\n", err);
231280297Sjkim# endif
232280297Sjkim    EXIT(err);
233280297Sjkim    return (0);
234280297Sjkim}
235160814Ssimon#endif
236