rc4test.c revision 296465
1148330Snetchild/* crypto/rc4/rc4test.c */ 2148330Snetchild/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3148330Snetchild * All rights reserved. 4148330Snetchild * 5148330Snetchild * This package is an SSL implementation written 6148330Snetchild * by Eric Young (eay@cryptsoft.com). 7148330Snetchild * The implementation was written so as to conform with Netscapes SSL. 8148330Snetchild * 9148330Snetchild * This library is free for commercial and non-commercial use as long as 10148330Snetchild * the following conditions are aheared to. The following conditions 11148330Snetchild * apply to all code found in this distribution, be it the RC4, RSA, 12148330Snetchild * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13148330Snetchild * included with this distribution is covered by the same copyright terms 14148543Snetchild * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15148543Snetchild * 16148330Snetchild * Copyright remains Eric Young's, and as such any Copyright notices in 17171137Sbz * the code are not to be removed. 18171137Sbz * If this package is used in a product, Eric Young should be given attribution 19171137Sbz * as the author of the parts of the library used. 20171137Sbz * This can be in the form of a textual message at program startup or 21171137Sbz * in documentation (online or textual) provided with the package. 22171137Sbz * 23171137Sbz * Redistribution and use in source and binary forms, with or without 24171137Sbz * modification, are permitted provided that the following conditions 25171137Sbz * are met: 26171137Sbz * 1. Redistributions of source code must retain the copyright 27171137Sbz * notice, this list of conditions and the following disclaimer. 28171137Sbz * 2. Redistributions in binary form must reproduce the above copyright 29171137Sbz * notice, this list of conditions and the following disclaimer in the 30171137Sbz * documentation and/or other materials provided with the distribution. 31171137Sbz * 3. All advertising materials mentioning features or use of this software 32171137Sbz * must display the following acknowledgement: 33171137Sbz * "This product includes cryptographic software written by 34171137Sbz * Eric Young (eay@cryptsoft.com)" 35171137Sbz * The word 'cryptographic' can be left out if the rouines from the library 36171131Sthompsa * being used are not cryptographic related :-). 37171131Sthompsa * 4. If you include any Windows specific code (or a derivative thereof) from 38171023Srafan * the apps directory (application code) you must include an acknowledgement: 39171023Srafan * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40171023Srafan * 41171023Srafan * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42171023Srafan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43171023Srafan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44170926Srafan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45170926Srafan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46170926Srafan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47170926Srafan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48170926Srafan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49170926Srafan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50170926Srafan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51170926Srafan * SUCH DAMAGE. 52170926Srafan * 53170926Srafan * The licence and distribution terms for any publically available version or 54170926Srafan * derivative of this code cannot be changed. i.e. this code cannot simply be 55170926Srafan * copied and put under another distribution licence 56170926Srafan * [including the GNU Public Licence.] 57170926Srafan */ 58170926Srafan 59170926Srafan#include <stdio.h> 60170926Srafan#include <stdlib.h> 61170926Srafan#include <string.h> 62170926Srafan 63170926Srafan#include "../e_os.h" 64170926Srafan 65170926Srafan#ifdef OPENSSL_NO_RC4 66170926Srafanint main(int argc, char *argv[]) 67170926Srafan{ 68170926Srafan printf("No RC4 support\n"); 69170926Srafan return (0); 70170926Srafan} 71170926Srafan#else 72170926Srafan# include <openssl/rc4.h> 73170926Srafan# include <openssl/sha.h> 74170926Srafan 75170926Srafanstatic unsigned char keys[7][30] = { 76170926Srafan {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, 77170926Srafan {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, 78170926Srafan {8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 79170926Srafan {4, 0xef, 0x01, 0x23, 0x45}, 80170926Srafan {8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, 81170926Srafan {4, 0xef, 0x01, 0x23, 0x45}, 82170926Srafan}; 83170926Srafan 84170926Srafanstatic unsigned char data_len[7] = { 8, 8, 8, 20, 28, 10 }; 85170312Sdelphij 86170312Sdelphijstatic unsigned char data[7][30] = { 87170926Srafan {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff}, 88170926Srafan {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}, 89169815Sdelphij {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}, 90169815Sdelphij {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 91169815Sdelphij 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 92169815Sdelphij 0x00, 0x00, 0x00, 0x00, 0xff}, 93169815Sdelphij {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 94169815Sdelphij 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 95169815Sdelphij 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 96169815Sdelphij 0x12, 0x34, 0x56, 0x78, 0xff}, 97169815Sdelphij {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}, 98169815Sdelphij {0}, 99169815Sdelphij}; 100169815Sdelphij 101170204Srustatic unsigned char output[7][30] = { 102169815Sdelphij {0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96, 0x00}, 103169815Sdelphij {0x74, 0x94, 0xc2, 0xe7, 0x10, 0x4b, 0x08, 0x79, 0x00}, 104169815Sdelphij {0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a, 0x00}, 105169815Sdelphij {0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf, 106170204Sru 0xbd, 0x61, 0x5a, 0x11, 0x62, 0xe1, 0xc7, 0xba, 107169815Sdelphij 0x36, 0xb6, 0x78, 0x58, 0x00}, 108169815Sdelphij {0x66, 0xa0, 0x94, 0x9f, 0x8a, 0xf7, 0xd6, 0x89, 109169815Sdelphij 0x1f, 0x7f, 0x83, 0x2b, 0xa8, 0x33, 0xc0, 0x0c, 110169815Sdelphij 0x89, 0x2e, 0xbe, 0x30, 0x14, 0x3c, 0xe2, 0x87, 111169815Sdelphij 0x40, 0x01, 0x1e, 0xcf, 0x00}, 112169815Sdelphij {0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf, 0xbd, 0x61, 0x00}, 113169815Sdelphij {0}, 114169815Sdelphij}; 115169815Sdelphij 116169815Sdelphijint main(int argc, char *argv[]) 117169815Sdelphij{ 118169815Sdelphij int err = 0; 119169815Sdelphij unsigned int i, j; 120169815Sdelphij unsigned char *p; 121169815Sdelphij RC4_KEY key; 122169815Sdelphij unsigned char obuf[512]; 123169815Sdelphij 124169815Sdelphij for (i = 0; i < 6; i++) { 125169815Sdelphij RC4_set_key(&key, keys[i][0], &(keys[i][1])); 126169815Sdelphij memset(obuf, 0x00, sizeof(obuf)); 127169815Sdelphij RC4(&key, data_len[i], &(data[i][0]), obuf); 128169815Sdelphij if (memcmp(obuf, output[i], data_len[i] + 1) != 0) { 129169815Sdelphij printf("error calculating RC4\n"); 130169815Sdelphij printf("output:"); 131169815Sdelphij for (j = 0; j < data_len[i] + 1U; j++) 132169815Sdelphij printf(" %02x", obuf[j]); 133169815Sdelphij printf("\n"); 134169815Sdelphij printf("expect:"); 135169815Sdelphij p = &(output[i][0]); 136169815Sdelphij for (j = 0; j < data_len[i] + 1U; j++) 137169815Sdelphij printf(" %02x", *(p++)); 138169815Sdelphij printf("\n"); 139169815Sdelphij err++; 140169815Sdelphij } else 141169815Sdelphij printf("test %d ok\n", i); 142169815Sdelphij } 143170204Sru printf("test end processing "); 144169815Sdelphij for (i = 0; i < data_len[3]; i++) { 145169815Sdelphij RC4_set_key(&key, keys[3][0], &(keys[3][1])); 146169815Sdelphij memset(obuf, 0x00, sizeof(obuf)); 147170917Srafan RC4(&key, i, &(data[3][0]), obuf); 148169815Sdelphij if ((memcmp(obuf, output[3], i) != 0) || (obuf[i] != 0)) { 149169815Sdelphij printf("error in RC4 length processing\n"); 150169815Sdelphij printf("output:"); 151169815Sdelphij for (j = 0; j < i + 1; j++) 152169815Sdelphij printf(" %02x", obuf[j]); 153169815Sdelphij printf("\n"); 154169815Sdelphij printf("expect:"); 155169815Sdelphij p = &(output[3][0]); 156169815Sdelphij for (j = 0; j < i; j++) 157169815Sdelphij printf(" %02x", *(p++)); 158169815Sdelphij printf(" 00\n"); 159169815Sdelphij err++; 160169815Sdelphij } else { 161169815Sdelphij printf("."); 162169815Sdelphij fflush(stdout); 163169815Sdelphij } 164169815Sdelphij } 165169815Sdelphij printf("done\n"); 166169815Sdelphij printf("test multi-call "); 167169815Sdelphij for (i = 0; i < data_len[3]; i++) { 168169815Sdelphij RC4_set_key(&key, keys[3][0], &(keys[3][1])); 169169815Sdelphij memset(obuf, 0x00, sizeof(obuf)); 170169815Sdelphij RC4(&key, i, &(data[3][0]), obuf); 171169815Sdelphij RC4(&key, data_len[3] - i, &(data[3][i]), &(obuf[i])); 172169815Sdelphij if (memcmp(obuf, output[3], data_len[3] + 1) != 0) { 173169815Sdelphij printf("error in RC4 multi-call processing\n"); 174169815Sdelphij printf("output:"); 175169815Sdelphij for (j = 0; j < data_len[3] + 1U; j++) 176169815Sdelphij printf(" %02x", obuf[j]); 177169815Sdelphij printf("\n"); 178169815Sdelphij printf("expect:"); 179169815Sdelphij p = &(output[3][0]); 180169815Sdelphij for (j = 0; j < data_len[3] + 1U; j++) 181169815Sdelphij printf(" %02x", *(p++)); 182169815Sdelphij err++; 183169815Sdelphij } else { 184169815Sdelphij printf("."); 185169815Sdelphij fflush(stdout); 186169815Sdelphij } 187169815Sdelphij } 188169815Sdelphij printf("done\n"); 189169815Sdelphij printf("bulk test "); 190169815Sdelphij { 191169815Sdelphij unsigned char buf[513]; 192169815Sdelphij SHA_CTX c; 193169815Sdelphij unsigned char md[SHA_DIGEST_LENGTH]; 194169815Sdelphij static unsigned char expected[] = { 195169815Sdelphij 0xa4, 0x7b, 0xcc, 0x00, 0x3d, 0xd0, 0xbd, 0xe1, 0xac, 0x5f, 196169815Sdelphij 0x12, 0x1e, 0x45, 0xbc, 0xfb, 0x1a, 0xa1, 0xf2, 0x7f, 0xc5 197170204Sru }; 198169815Sdelphij 199169815Sdelphij RC4_set_key(&key, keys[0][0], &(keys[3][1])); 200169815Sdelphij memset(buf, '\0', sizeof(buf)); 201169815Sdelphij SHA1_Init(&c); 202169815Sdelphij for (i = 0; i < 2571; i++) { 203170190Sru RC4(&key, sizeof(buf), buf, buf); 204169815Sdelphij SHA1_Update(&c, buf, sizeof(buf)); 205169815Sdelphij } 206169815Sdelphij SHA1_Final(md, &c); 207169815Sdelphij 208169815Sdelphij if (memcmp(md, expected, sizeof(md))) { 209169815Sdelphij printf("error in RC4 bulk test\n"); 210169815Sdelphij printf("output:"); 211169815Sdelphij for (j = 0; j < sizeof(md); j++) 212169815Sdelphij printf(" %02x", md[j]); 213169815Sdelphij printf("\n"); 214169815Sdelphij printf("expect:"); 215169815Sdelphij for (j = 0; j < sizeof(md); j++) 216170190Sru printf(" %02x", expected[j]); 217170190Sru printf("\n"); 218170190Sru err++; 219170190Sru } else 220170190Sru printf("ok\n"); 221170190Sru } 222170190Sru# ifdef OPENSSL_SYS_NETWARE 223169445Sroberto if (err) 224169445Sroberto printf("ERROR: %d\n", err); 225169445Sroberto# endif 226169026Semax EXIT(err); 227169026Semax return (0); 228168916Sbrueffer} 229168916Sbrueffer#endif 230168796Sthompsa