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.
8296465Sdelphij *
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).
15296465Sdelphij *
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.
22296465Sdelphij *
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 :-).
37296465Sdelphij * 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)"
40296465Sdelphij *
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.
52296465Sdelphij *
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");
69296465Sdelphij    return (0);
7055714Skris}
7155714Skris#else
72296465Sdelphij# include <openssl/rc4.h>
73296465Sdelphij# include <openssl/sha.h>
7455714Skris
75296465Sdelphijstatic unsigned char keys[7][30] = {
76296465Sdelphij    {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
77296465Sdelphij    {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
78296465Sdelphij    {8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
79296465Sdelphij    {4, 0xef, 0x01, 0x23, 0x45},
80296465Sdelphij    {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
81296465Sdelphij    {4, 0xef, 0x01, 0x23, 0x45},
82296465Sdelphij};
8355714Skris
84296465Sdelphijstatic unsigned char data_len[7] = { 8, 8, 8, 20, 28, 10 };
8555714Skris
86296465Sdelphijstatic unsigned char data[7][30] = {
87296465Sdelphij    {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff},
88296465Sdelphij    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff},
89296465Sdelphij    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff},
90296465Sdelphij    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91296465Sdelphij     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92296465Sdelphij     0x00, 0x00, 0x00, 0x00, 0xff},
93296465Sdelphij    {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
94296465Sdelphij     0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
95296465Sdelphij     0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
96296465Sdelphij     0x12, 0x34, 0x56, 0x78, 0xff},
97296465Sdelphij    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff},
98296465Sdelphij    {0},
99296465Sdelphij};
10055714Skris
101296465Sdelphijstatic unsigned char output[7][30] = {
102296465Sdelphij    {0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96, 0x00},
103296465Sdelphij    {0x74, 0x94, 0xc2, 0xe7, 0x10, 0x4b, 0x08, 0x79, 0x00},
104296465Sdelphij    {0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a, 0x00},
105296465Sdelphij    {0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf,
106296465Sdelphij     0xbd, 0x61, 0x5a, 0x11, 0x62, 0xe1, 0xc7, 0xba,
107296465Sdelphij     0x36, 0xb6, 0x78, 0x58, 0x00},
108296465Sdelphij    {0x66, 0xa0, 0x94, 0x9f, 0x8a, 0xf7, 0xd6, 0x89,
109296465Sdelphij     0x1f, 0x7f, 0x83, 0x2b, 0xa8, 0x33, 0xc0, 0x0c,
110296465Sdelphij     0x89, 0x2e, 0xbe, 0x30, 0x14, 0x3c, 0xe2, 0x87,
111296465Sdelphij     0x40, 0x01, 0x1e, 0xcf, 0x00},
112296465Sdelphij    {0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf, 0xbd, 0x61, 0x00},
113296465Sdelphij    {0},
114296465Sdelphij};
115296465Sdelphij
11655714Skrisint main(int argc, char *argv[])
117296465Sdelphij{
118296465Sdelphij    int err = 0;
119296465Sdelphij    unsigned int i, j;
120296465Sdelphij    unsigned char *p;
121296465Sdelphij    RC4_KEY key;
122296465Sdelphij    unsigned char obuf[512];
12355714Skris
124296465Sdelphij    for (i = 0; i < 6; i++) {
125296465Sdelphij        RC4_set_key(&key, keys[i][0], &(keys[i][1]));
126296465Sdelphij        memset(obuf, 0x00, sizeof(obuf));
127296465Sdelphij        RC4(&key, data_len[i], &(data[i][0]), obuf);
128296465Sdelphij        if (memcmp(obuf, output[i], data_len[i] + 1) != 0) {
129296465Sdelphij            printf("error calculating RC4\n");
130296465Sdelphij            printf("output:");
131296465Sdelphij            for (j = 0; j < data_len[i] + 1U; j++)
132296465Sdelphij                printf(" %02x", obuf[j]);
133296465Sdelphij            printf("\n");
134296465Sdelphij            printf("expect:");
135296465Sdelphij            p = &(output[i][0]);
136296465Sdelphij            for (j = 0; j < data_len[i] + 1U; j++)
137296465Sdelphij                printf(" %02x", *(p++));
138296465Sdelphij            printf("\n");
139296465Sdelphij            err++;
140296465Sdelphij        } else
141296465Sdelphij            printf("test %d ok\n", i);
142296465Sdelphij    }
143296465Sdelphij    printf("test end processing ");
144296465Sdelphij    for (i = 0; i < data_len[3]; i++) {
145296465Sdelphij        RC4_set_key(&key, keys[3][0], &(keys[3][1]));
146296465Sdelphij        memset(obuf, 0x00, sizeof(obuf));
147296465Sdelphij        RC4(&key, i, &(data[3][0]), obuf);
148296465Sdelphij        if ((memcmp(obuf, output[3], i) != 0) || (obuf[i] != 0)) {
149296465Sdelphij            printf("error in RC4 length processing\n");
150296465Sdelphij            printf("output:");
151296465Sdelphij            for (j = 0; j < i + 1; j++)
152296465Sdelphij                printf(" %02x", obuf[j]);
153296465Sdelphij            printf("\n");
154296465Sdelphij            printf("expect:");
155296465Sdelphij            p = &(output[3][0]);
156296465Sdelphij            for (j = 0; j < i; j++)
157296465Sdelphij                printf(" %02x", *(p++));
158296465Sdelphij            printf(" 00\n");
159296465Sdelphij            err++;
160296465Sdelphij        } else {
161296465Sdelphij            printf(".");
162296465Sdelphij            fflush(stdout);
163296465Sdelphij        }
164296465Sdelphij    }
165296465Sdelphij    printf("done\n");
166296465Sdelphij    printf("test multi-call ");
167296465Sdelphij    for (i = 0; i < data_len[3]; i++) {
168296465Sdelphij        RC4_set_key(&key, keys[3][0], &(keys[3][1]));
169296465Sdelphij        memset(obuf, 0x00, sizeof(obuf));
170296465Sdelphij        RC4(&key, i, &(data[3][0]), obuf);
171296465Sdelphij        RC4(&key, data_len[3] - i, &(data[3][i]), &(obuf[i]));
172296465Sdelphij        if (memcmp(obuf, output[3], data_len[3] + 1) != 0) {
173296465Sdelphij            printf("error in RC4 multi-call processing\n");
174296465Sdelphij            printf("output:");
175296465Sdelphij            for (j = 0; j < data_len[3] + 1U; j++)
176296465Sdelphij                printf(" %02x", obuf[j]);
177296465Sdelphij            printf("\n");
178296465Sdelphij            printf("expect:");
179296465Sdelphij            p = &(output[3][0]);
180296465Sdelphij            for (j = 0; j < data_len[3] + 1U; j++)
181296465Sdelphij                printf(" %02x", *(p++));
182296465Sdelphij            err++;
183296465Sdelphij        } else {
184296465Sdelphij            printf(".");
185296465Sdelphij            fflush(stdout);
186296465Sdelphij        }
187296465Sdelphij    }
188296465Sdelphij    printf("done\n");
189296465Sdelphij    printf("bulk test ");
190296465Sdelphij    {
191296465Sdelphij        unsigned char buf[513];
192296465Sdelphij        SHA_CTX c;
193296465Sdelphij        unsigned char md[SHA_DIGEST_LENGTH];
194296465Sdelphij        static unsigned char expected[] = {
195296465Sdelphij            0xa4, 0x7b, 0xcc, 0x00, 0x3d, 0xd0, 0xbd, 0xe1, 0xac, 0x5f,
196296465Sdelphij            0x12, 0x1e, 0x45, 0xbc, 0xfb, 0x1a, 0xa1, 0xf2, 0x7f, 0xc5
197296465Sdelphij        };
198160814Ssimon
199296465Sdelphij        RC4_set_key(&key, keys[0][0], &(keys[3][1]));
200296465Sdelphij        memset(buf, '\0', sizeof(buf));
201296465Sdelphij        SHA1_Init(&c);
202296465Sdelphij        for (i = 0; i < 2571; i++) {
203296465Sdelphij            RC4(&key, sizeof(buf), buf, buf);
204296465Sdelphij            SHA1_Update(&c, buf, sizeof(buf));
205296465Sdelphij        }
206296465Sdelphij        SHA1_Final(md, &c);
207160814Ssimon
208296465Sdelphij        if (memcmp(md, expected, sizeof(md))) {
209296465Sdelphij            printf("error in RC4 bulk test\n");
210296465Sdelphij            printf("output:");
211296465Sdelphij            for (j = 0; j < sizeof(md); j++)
212296465Sdelphij                printf(" %02x", md[j]);
213296465Sdelphij            printf("\n");
214296465Sdelphij            printf("expect:");
215296465Sdelphij            for (j = 0; j < sizeof(md); j++)
216296465Sdelphij                printf(" %02x", expected[j]);
217296465Sdelphij            printf("\n");
218296465Sdelphij            err++;
219296465Sdelphij        } else
220296465Sdelphij            printf("ok\n");
221296465Sdelphij    }
222296465Sdelphij# ifdef OPENSSL_SYS_NETWARE
223296465Sdelphij    if (err)
224296465Sdelphij        printf("ERROR: %d\n", err);
225296465Sdelphij# endif
226296465Sdelphij    EXIT(err);
227296465Sdelphij    return (0);
228296465Sdelphij}
229160814Ssimon#endif
230