os/ossrv/ssl/tsrc/crypto_test/src/rc4test.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/tsrc/crypto_test/src/rc4test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,327 @@
     1.4 +/* crypto/rc4/rc4test.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +/*
    1.62 + © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    1.63 + */
    1.64 +#include <stdio.h>
    1.65 +#include <stdlib.h>
    1.66 +#include <string.h>
    1.67 +#include <errno.h>
    1.68 +#ifndef SYMBIAN
    1.69 +#include "../e_os.h"
    1.70 +#else
    1.71 +#include "e_os.h"
    1.72 +#endif
    1.73 +#include <openssl/rc2.h>
    1.74 +#ifdef SYMBIAN
    1.75 +#ifdef stdin
    1.76 +#undef stdin
    1.77 +#endif
    1.78 +#ifdef stdout
    1.79 +#undef stdout
    1.80 +#endif
    1.81 +#ifdef stderr
    1.82 +#undef stderr
    1.83 +#endif
    1.84 +
    1.85 +#define stdin fp_stdin
    1.86 +#define stdout fp_stdout
    1.87 +#define stderr fp_stderr
    1.88 +
    1.89 +extern FILE *fp_stdout;
    1.90 +extern FILE *fp_stderr;
    1.91 +#endif
    1.92 +
    1.93 +#ifdef OPENSSL_NO_RC4
    1.94 +int main(int argc, char *argv[])
    1.95 +{
    1.96 +    fprintf(stdout,"No RC4 support\n");
    1.97 +    return(0);
    1.98 +}
    1.99 +#else
   1.100 +#include <openssl/rc4.h>
   1.101 +#include <openssl/sha.h>
   1.102 +
   1.103 +static unsigned char keys[7][30]={
   1.104 +	{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
   1.105 +	{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
   1.106 +	{8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
   1.107 +	{4,0xef,0x01,0x23,0x45},
   1.108 +	{8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
   1.109 +	{4,0xef,0x01,0x23,0x45},
   1.110 +	};
   1.111 +
   1.112 +static unsigned char data_len[7]={8,8,8,20,28,10};
   1.113 +static unsigned char data[7][30]={
   1.114 +	{0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xff},
   1.115 +	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
   1.116 +	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
   1.117 +	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
   1.118 +	   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
   1.119 +	   0x00,0x00,0x00,0x00,0xff},
   1.120 +	{0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
   1.121 +	   0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
   1.122 +	   0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
   1.123 +	   0x12,0x34,0x56,0x78,0xff},
   1.124 +	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
   1.125 +	{0},
   1.126 +	};
   1.127 +
   1.128 +static unsigned char output[7][30]={
   1.129 +	{0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96,0x00},
   1.130 +	{0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79,0x00},
   1.131 +	{0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a,0x00},
   1.132 +	{0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,
   1.133 +	 0xbd,0x61,0x5a,0x11,0x62,0xe1,0xc7,0xba,
   1.134 +	 0x36,0xb6,0x78,0x58,0x00},
   1.135 +	{0x66,0xa0,0x94,0x9f,0x8a,0xf7,0xd6,0x89,
   1.136 +	 0x1f,0x7f,0x83,0x2b,0xa8,0x33,0xc0,0x0c,
   1.137 +	 0x89,0x2e,0xbe,0x30,0x14,0x3c,0xe2,0x87,
   1.138 +	 0x40,0x01,0x1e,0xcf,0x00},
   1.139 +	{0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61,0x00},
   1.140 +	{0},
   1.141 +	};
   1.142 +#ifndef SYMBIAN
   1.143 +int main(int argc, char *argv[])
   1.144 +#else
   1.145 +int rc4_main(int argc, char *argv[])
   1.146 +#endif
   1.147 +	{
   1.148 +	int err=0;
   1.149 +	unsigned int i, j;
   1.150 +	unsigned char *p;
   1.151 +	RC4_KEY key;
   1.152 +	unsigned char obuf[512];
   1.153 +    
   1.154 +	for (i=0; i<6; i++)
   1.155 +		{
   1.156 +		RC4_set_key(&key,keys[i][0],&(keys[i][1]));
   1.157 +    if(errno==ENOMEM)
   1.158 +    {
   1.159 +	    return 1;
   1.160 +	  }  
   1.161 +		memset(obuf,0x00,sizeof(obuf));
   1.162 +   if(errno==ENOMEM)
   1.163 +    {
   1.164 +	    return 1;
   1.165 +	  }  
   1.166 +
   1.167 +		RC4(&key,data_len[i],&(data[i][0]),obuf);
   1.168 +   if(errno==ENOMEM)
   1.169 +    {
   1.170 +	    return 1;
   1.171 +	  }  
   1.172 +		
   1.173 +		if (memcmp(obuf,output[i],data_len[i]+1) != 0)
   1.174 +			{
   1.175 +			fprintf(stdout,"error calculating RC4\n");
   1.176 +			fprintf(stdout,"output:");
   1.177 +			for (j=0; j<data_len[i]+1U; j++)
   1.178 +				fprintf(stdout," %02x",obuf[j]);
   1.179 +			fprintf(stdout,"\n");
   1.180 +			fprintf(stdout,"expect:");
   1.181 +			p= &(output[i][0]);
   1.182 +			for (j=0; j<data_len[i]+1U; j++)
   1.183 +				fprintf(stdout," %02x",*(p++));
   1.184 +			fprintf(stdout,"\n");
   1.185 +			err++;
   1.186 +			}
   1.187 +		else
   1.188 +			fprintf(stdout,"test %d ok\n",i);
   1.189 +		}
   1.190 +	fprintf(stdout,"test end processing ");
   1.191 +	for (i=0; i<data_len[3]; i++)
   1.192 +		{
   1.193 +		RC4_set_key(&key,keys[3][0],&(keys[3][1]));
   1.194 +		if(errno==ENOMEM)
   1.195 +    {
   1.196 +	    return 1;
   1.197 +	  }  
   1.198 +		memset(obuf,0x00,sizeof(obuf));
   1.199 +		RC4(&key,i,&(data[3][0]),obuf);
   1.200 +	   if(errno==ENOMEM)
   1.201 +    {
   1.202 +	    return 1;
   1.203 +	  }  
   1.204 +		if ((memcmp(obuf,output[3],i) != 0) || (obuf[i] != 0))
   1.205 +			{
   1.206 +			fprintf(stdout,"error in RC4 length processing\n");
   1.207 +			fprintf(stdout,"output:");
   1.208 +			for (j=0; j<i+1; j++)
   1.209 +				printf(" %02x",obuf[j]);
   1.210 +			fprintf(stdout,"\n");
   1.211 +			fprintf(stdout,"expect:");
   1.212 +			p= &(output[3][0]);
   1.213 +			for (j=0; j<i; j++)
   1.214 +				fprintf(stdout," %02x",*(p++));
   1.215 +			fprintf(stdout," 00\n");
   1.216 +			err++;
   1.217 +			}
   1.218 +		else
   1.219 +			{
   1.220 +			fprintf(stdout,".");
   1.221 +			fflush(stdout);
   1.222 +			}
   1.223 +		}
   1.224 +	fprintf(stdout,"done\n");
   1.225 +	fprintf(stdout,"test multi-call ");
   1.226 +	for (i=0; i<data_len[3]; i++)
   1.227 +		{
   1.228 +		RC4_set_key(&key,keys[3][0],&(keys[3][1]));
   1.229 +	   if(errno==ENOMEM)
   1.230 +    {
   1.231 +	    return 1;
   1.232 +	  }  
   1.233 +		
   1.234 +		memset(obuf,0x00,sizeof(obuf));
   1.235 +		RC4(&key,i,&(data[3][0]),obuf);
   1.236 +	   if(errno==ENOMEM)
   1.237 +    {
   1.238 +	    return 1;
   1.239 +	  }  
   1.240 +		
   1.241 +		RC4(&key,data_len[3]-i,&(data[3][i]),&(obuf[i]));
   1.242 +	   if(errno==ENOMEM)
   1.243 +    {
   1.244 +	    return 1;
   1.245 +	  }  
   1.246 +		
   1.247 +		if (memcmp(obuf,output[3],data_len[3]+1) != 0)
   1.248 +			{
   1.249 +			fprintf(stdout,"error in RC4 multi-call processing\n");
   1.250 +			fprintf(stdout,"output:");
   1.251 +			for (j=0; j<data_len[3]+1U; j++)
   1.252 +				fprintf(stdout," %02x",obuf[j]);
   1.253 +			fprintf(stdout,"\n");
   1.254 +			fprintf(stdout,"expect:");
   1.255 +			p= &(output[3][0]);
   1.256 +			for (j=0; j<data_len[3]+1U; j++)
   1.257 +				fprintf(stdout," %02x",*(p++));
   1.258 +			err++;
   1.259 +			}
   1.260 +		else
   1.261 +			{
   1.262 +			fprintf(stdout,".");
   1.263 +			fflush(stdout);
   1.264 +			}
   1.265 +		}
   1.266 +	fprintf(stdout,"done\n");
   1.267 +	fprintf(stdout,"bulk test ");
   1.268 +	{   unsigned char buf[513];
   1.269 +	    SHA_CTX c;
   1.270 +	    unsigned char md[SHA_DIGEST_LENGTH];
   1.271 +	    static unsigned char expected[]={
   1.272 +		0xa4,0x7b,0xcc,0x00,0x3d,0xd0,0xbd,0xe1,0xac,0x5f,
   1.273 +		0x12,0x1e,0x45,0xbc,0xfb,0x1a,0xa1,0xf2,0x7f,0xc5 };
   1.274 +
   1.275 +		RC4_set_key(&key,keys[0][0],&(keys[3][1]));
   1.276 +	   if(errno==ENOMEM)
   1.277 +    {
   1.278 +	    return 1;
   1.279 +	  }  
   1.280 +		
   1.281 +		memset(buf,'\0',sizeof(buf));
   1.282 +		SHA1_Init(&c);
   1.283 +			   if(errno==ENOMEM)
   1.284 +    {
   1.285 +	    return 1;
   1.286 +	  }  
   1.287 +
   1.288 +		for (i=0;i<2571;i++) {
   1.289 +			RC4(&key,sizeof(buf),buf,buf);
   1.290 +		   if(errno==ENOMEM)
   1.291 +     {
   1.292 +	    return 1;
   1.293 +	   }  
   1.294 +
   1.295 +			SHA1_Update(&c,buf,sizeof(buf));
   1.296 +		  if(errno==ENOMEM)
   1.297 +     {
   1.298 +	    return 1;
   1.299 +	   }  
   1.300 +
   1.301 +		}
   1.302 +		SHA1_Final(md,&c);
   1.303 +		if(errno==ENOMEM)
   1.304 +    {
   1.305 +	    return 1;
   1.306 +	  }  
   1.307 +
   1.308 +
   1.309 +		if (memcmp(md,expected,sizeof(md))) {
   1.310 +			fprintf(stdout,"error in RC4 bulk test\n");
   1.311 +			fprintf(stdout,"output:");
   1.312 +			for (j=0; j<sizeof(md); j++)
   1.313 +				fprintf(stdout," %02x",md[j]);
   1.314 +			fprintf(stdout,"\n");
   1.315 +			fprintf(stdout,"expect:");
   1.316 +			for (j=0; j<sizeof(md); j++)
   1.317 +				fprintf(stdout," %02x",expected[j]);
   1.318 +			fprintf(stdout,"\n");
   1.319 +			err++;
   1.320 +		}
   1.321 +		else	fprintf(stdout,"ok\n");
   1.322 +	}
   1.323 +#ifdef OPENSSL_SYS_NETWARE
   1.324 +    if (err) fprintf(stdout,"ERROR: %d\n", err);
   1.325 +#endif
   1.326 +  fprintf(stdout,"Test case Passed\n");    
   1.327 +	//EXIT(err);
   1.328 +	return(0);
   1.329 +	}
   1.330 +#endif