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. 8280304Sjkim * 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). 15280304Sjkim * 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. 22280304Sjkim * 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 :-). 37280304Sjkim * 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)" 40280304Sjkim * 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. 52280304Sjkim * 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"); 69280304Sjkim return (0); 7055714Skris} 7155714Skris#else 72280304Sjkim# include <openssl/rc4.h> 73280304Sjkim# include <openssl/sha.h> 7455714Skris 75280304Sjkimstatic unsigned char keys[7][30] = { 76280304Sjkim {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, 77280304Sjkim {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, 78280304Sjkim {8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 79280304Sjkim {4, 0xef, 0x01, 0x23, 0x45}, 80280304Sjkim {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, 81280304Sjkim {4, 0xef, 0x01, 0x23, 0x45}, 82280304Sjkim}; 8355714Skris 84280304Sjkimstatic unsigned char data_len[7] = { 8, 8, 8, 20, 28, 10 }; 8555714Skris 86280304Sjkimstatic unsigned char data[7][30] = { 87280304Sjkim {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff}, 88280304Sjkim {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}, 89280304Sjkim {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}, 90280304Sjkim {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 91280304Sjkim 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 92280304Sjkim 0x00, 0x00, 0x00, 0x00, 0xff}, 93280304Sjkim {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 94280304Sjkim 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 95280304Sjkim 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 96280304Sjkim 0x12, 0x34, 0x56, 0x78, 0xff}, 97280304Sjkim {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}, 98280304Sjkim {0}, 99280304Sjkim}; 10055714Skris 101280304Sjkimstatic unsigned char output[7][30] = { 102280304Sjkim {0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96, 0x00}, 103280304Sjkim {0x74, 0x94, 0xc2, 0xe7, 0x10, 0x4b, 0x08, 0x79, 0x00}, 104280304Sjkim {0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a, 0x00}, 105280304Sjkim {0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf, 106280304Sjkim 0xbd, 0x61, 0x5a, 0x11, 0x62, 0xe1, 0xc7, 0xba, 107280304Sjkim 0x36, 0xb6, 0x78, 0x58, 0x00}, 108280304Sjkim {0x66, 0xa0, 0x94, 0x9f, 0x8a, 0xf7, 0xd6, 0x89, 109280304Sjkim 0x1f, 0x7f, 0x83, 0x2b, 0xa8, 0x33, 0xc0, 0x0c, 110280304Sjkim 0x89, 0x2e, 0xbe, 0x30, 0x14, 0x3c, 0xe2, 0x87, 111280304Sjkim 0x40, 0x01, 0x1e, 0xcf, 0x00}, 112280304Sjkim {0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf, 0xbd, 0x61, 0x00}, 113280304Sjkim {0}, 114280304Sjkim}; 115280304Sjkim 11655714Skrisint main(int argc, char *argv[]) 117280304Sjkim{ 118280304Sjkim int i, err = 0; 119280304Sjkim int j; 120280304Sjkim unsigned char *p; 121280304Sjkim RC4_KEY key; 122280304Sjkim unsigned char obuf[512]; 12355714Skris 124280304Sjkim# if !defined(OPENSSL_PIC) 125280304Sjkim void OPENSSL_cpuid_setup(void); 126238405Sjkim 127280304Sjkim OPENSSL_cpuid_setup(); 128280304Sjkim# endif 129238405Sjkim 130280304Sjkim for (i = 0; i < 6; i++) { 131280304Sjkim RC4_set_key(&key, keys[i][0], &(keys[i][1])); 132280304Sjkim memset(obuf, 0x00, sizeof(obuf)); 133280304Sjkim RC4(&key, data_len[i], &(data[i][0]), obuf); 134280304Sjkim if (memcmp(obuf, output[i], data_len[i] + 1) != 0) { 135280304Sjkim printf("error calculating RC4\n"); 136280304Sjkim printf("output:"); 137280304Sjkim for (j = 0; j < data_len[i] + 1; j++) 138280304Sjkim printf(" %02x", obuf[j]); 139280304Sjkim printf("\n"); 140280304Sjkim printf("expect:"); 141280304Sjkim p = &(output[i][0]); 142280304Sjkim for (j = 0; j < data_len[i] + 1; j++) 143280304Sjkim printf(" %02x", *(p++)); 144280304Sjkim printf("\n"); 145280304Sjkim err++; 146280304Sjkim } else 147280304Sjkim printf("test %d ok\n", i); 148280304Sjkim } 149280304Sjkim printf("test end processing "); 150280304Sjkim for (i = 0; i < data_len[3]; i++) { 151280304Sjkim RC4_set_key(&key, keys[3][0], &(keys[3][1])); 152280304Sjkim memset(obuf, 0x00, sizeof(obuf)); 153280304Sjkim RC4(&key, i, &(data[3][0]), obuf); 154280304Sjkim if ((memcmp(obuf, output[3], i) != 0) || (obuf[i] != 0)) { 155280304Sjkim printf("error in RC4 length processing\n"); 156280304Sjkim printf("output:"); 157280304Sjkim for (j = 0; j < i + 1; j++) 158280304Sjkim printf(" %02x", obuf[j]); 159280304Sjkim printf("\n"); 160280304Sjkim printf("expect:"); 161280304Sjkim p = &(output[3][0]); 162280304Sjkim for (j = 0; j < i; j++) 163280304Sjkim printf(" %02x", *(p++)); 164280304Sjkim printf(" 00\n"); 165280304Sjkim err++; 166280304Sjkim } else { 167280304Sjkim printf("."); 168280304Sjkim fflush(stdout); 169280304Sjkim } 170280304Sjkim } 171280304Sjkim printf("done\n"); 172280304Sjkim printf("test multi-call "); 173280304Sjkim for (i = 0; i < data_len[3]; i++) { 174280304Sjkim RC4_set_key(&key, keys[3][0], &(keys[3][1])); 175280304Sjkim memset(obuf, 0x00, sizeof(obuf)); 176280304Sjkim RC4(&key, i, &(data[3][0]), obuf); 177280304Sjkim RC4(&key, data_len[3] - i, &(data[3][i]), &(obuf[i])); 178280304Sjkim if (memcmp(obuf, output[3], data_len[3] + 1) != 0) { 179280304Sjkim printf("error in RC4 multi-call processing\n"); 180280304Sjkim printf("output:"); 181280304Sjkim for (j = 0; j < data_len[3] + 1; j++) 182280304Sjkim printf(" %02x", obuf[j]); 183280304Sjkim printf("\n"); 184280304Sjkim printf("expect:"); 185280304Sjkim p = &(output[3][0]); 186280304Sjkim for (j = 0; j < data_len[3] + 1; j++) 187280304Sjkim printf(" %02x", *(p++)); 188280304Sjkim err++; 189280304Sjkim } else { 190280304Sjkim printf("."); 191280304Sjkim fflush(stdout); 192280304Sjkim } 193280304Sjkim } 194280304Sjkim printf("done\n"); 195280304Sjkim printf("bulk test "); 196280304Sjkim { 197280304Sjkim unsigned char buf[513]; 198280304Sjkim SHA_CTX c; 199280304Sjkim unsigned char md[SHA_DIGEST_LENGTH]; 200280304Sjkim static unsigned char expected[] = { 201280304Sjkim 0xa4, 0x7b, 0xcc, 0x00, 0x3d, 0xd0, 0xbd, 0xe1, 0xac, 0x5f, 202280304Sjkim 0x12, 0x1e, 0x45, 0xbc, 0xfb, 0x1a, 0xa1, 0xf2, 0x7f, 0xc5 203280304Sjkim }; 204160814Ssimon 205280304Sjkim RC4_set_key(&key, keys[0][0], &(keys[3][1])); 206280304Sjkim memset(buf, '\0', sizeof(buf)); 207280304Sjkim SHA1_Init(&c); 208280304Sjkim for (i = 0; i < 2571; i++) { 209280304Sjkim RC4(&key, sizeof(buf), buf, buf); 210280304Sjkim SHA1_Update(&c, buf, sizeof(buf)); 211280304Sjkim } 212280304Sjkim SHA1_Final(md, &c); 213160814Ssimon 214280304Sjkim if (memcmp(md, expected, sizeof(md))) { 215280304Sjkim printf("error in RC4 bulk test\n"); 216280304Sjkim printf("output:"); 217280304Sjkim for (j = 0; j < (int)sizeof(md); j++) 218280304Sjkim printf(" %02x", md[j]); 219280304Sjkim printf("\n"); 220280304Sjkim printf("expect:"); 221280304Sjkim for (j = 0; j < (int)sizeof(md); j++) 222280304Sjkim printf(" %02x", expected[j]); 223280304Sjkim printf("\n"); 224280304Sjkim err++; 225280304Sjkim } else 226280304Sjkim printf("ok\n"); 227280304Sjkim } 228280304Sjkim# ifdef OPENSSL_SYS_NETWARE 229280304Sjkim if (err) 230280304Sjkim printf("ERROR: %d\n", err); 231280304Sjkim# endif 232280304Sjkim EXIT(err); 233280304Sjkim return (0); 234280304Sjkim} 235160814Ssimon#endif 236