os/ossrv/ssl/tsrc/crypto_test/src/rc2test.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/rc2test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,312 @@
     1.4 +/* crypto/rc2/rc2test.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 +/* This has been a quickly hacked 'ideatest.c'.  When I add tests for other
    1.65 + * RC2 modes, more of the code will be uncommented. */
    1.66 +
    1.67 +#include <stdio.h>
    1.68 +#include <string.h>
    1.69 +#include <stdlib.h>
    1.70 +#include <errno.h>
    1.71 +#ifndef SYMBIAN
    1.72 +#include "../e_os.h"
    1.73 +#else
    1.74 +#include "e_os.h"
    1.75 +#endif
    1.76 +#include <openssl/rc2.h>
    1.77 +#ifdef SYMBIAN
    1.78 +#ifdef stdin
    1.79 +#undef stdin
    1.80 +#endif
    1.81 +#ifdef stdout
    1.82 +#undef stdout
    1.83 +#endif
    1.84 +#ifdef stderr
    1.85 +#undef stderr
    1.86 +#endif
    1.87 +
    1.88 +#define stdin fp_stdin
    1.89 +#define stdout fp_stdout
    1.90 +#define stderr fp_stderr
    1.91 +
    1.92 +extern FILE *fp_stdout;
    1.93 +extern FILE *fp_stderr;
    1.94 +#endif
    1.95 +#ifdef OPENSSL_NO_RC2
    1.96 +int main(int argc, char *argv[])
    1.97 +{
    1.98 +    fprintf(stdout,"No RC2 support\n");
    1.99 +    return(0);
   1.100 +}
   1.101 +#else
   1.102 +
   1.103 +static unsigned char RC2key[4][16]={
   1.104 +	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
   1.105 +	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
   1.106 +	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
   1.107 +	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
   1.108 +	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
   1.109 +	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
   1.110 +	{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
   1.111 +	 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F},
   1.112 +	};
   1.113 +
   1.114 +static unsigned char RC2plain[4][8]={
   1.115 +	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
   1.116 +	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
   1.117 +	{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
   1.118 +	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
   1.119 +	};
   1.120 +
   1.121 +static unsigned char RC2cipher[4][8]={
   1.122 +	{0x1C,0x19,0x8A,0x83,0x8D,0xF0,0x28,0xB7},
   1.123 +	{0x21,0x82,0x9C,0x78,0xA9,0xF9,0xC0,0x74},
   1.124 +	{0x13,0xDB,0x35,0x17,0xD3,0x21,0x86,0x9E},
   1.125 +	{0x50,0xDC,0x01,0x62,0xBD,0x75,0x7F,0x31},
   1.126 +	};
   1.127 +/************/
   1.128 +#ifdef undef
   1.129 +unsigned char k[16]={
   1.130 +	0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04,
   1.131 +	0x00,0x05,0x00,0x06,0x00,0x07,0x00,0x08};
   1.132 +
   1.133 +unsigned char in[8]={0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x03};
   1.134 +unsigned char  c[8]={0x11,0xFB,0xED,0x2B,0x01,0x98,0x6D,0xE5};
   1.135 +unsigned char out[80];
   1.136 +
   1.137 +char *text="Hello to all people out there";
   1.138 +
   1.139 +static unsigned char cfb_key[16]={
   1.140 +	0xe1,0xf0,0xc3,0xd2,0xa5,0xb4,0x87,0x96,
   1.141 +	0x69,0x78,0x4b,0x5a,0x2d,0x3c,0x0f,0x1e,
   1.142 +	};
   1.143 +static unsigned char cfb_iv[80]={0x34,0x12,0x78,0x56,0xab,0x90,0xef,0xcd};
   1.144 +static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
   1.145 +#define CFB_TEST_SIZE 24
   1.146 +static unsigned char plain[CFB_TEST_SIZE]=
   1.147 +        {
   1.148 +        0x4e,0x6f,0x77,0x20,0x69,0x73,
   1.149 +        0x20,0x74,0x68,0x65,0x20,0x74,
   1.150 +        0x69,0x6d,0x65,0x20,0x66,0x6f,
   1.151 +        0x72,0x20,0x61,0x6c,0x6c,0x20
   1.152 +        };
   1.153 +static unsigned char cfb_cipher64[CFB_TEST_SIZE]={
   1.154 +	0x59,0xD8,0xE2,0x65,0x00,0x58,0x6C,0x3F,
   1.155 +	0x2C,0x17,0x25,0xD0,0x1A,0x38,0xB7,0x2A,
   1.156 +	0x39,0x61,0x37,0xDC,0x79,0xFB,0x9F,0x45
   1.157 +
   1.158 +/*	0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
   1.159 +	0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
   1.160 +	0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
   1.161 +	}; 
   1.162 +
   1.163 +
   1.164 +/*static int cfb64_test(unsigned char *cfb_cipher);*/
   1.165 +static char *pt(unsigned char *p);
   1.166 +#endif
   1.167 +#ifndef SYMBIAN
   1.168 +int main(int argc, char *argv[])
   1.169 +#else
   1.170 +int rc2_main(int argc, char *argv[])
   1.171 +#endif
   1.172 +	{
   1.173 +	int i,n,err=0;
   1.174 +	RC2_KEY key; 
   1.175 +	unsigned char buf[8],buf2[8];
   1.176 +    
   1.177 +	for (n=0; n<4; n++)
   1.178 +		{
   1.179 +		RC2_set_key(&key,16,&(RC2key[n][0]),0 /* or 1024 */);
   1.180 +	   if(errno==ENOMEM)
   1.181 +    {
   1.182 +	    return 1;
   1.183 +	  }  
   1.184 +		RC2_ecb_encrypt(&(RC2plain[n][0]),buf,&key,RC2_ENCRYPT);
   1.185 +	   if(errno==ENOMEM)
   1.186 +    {
   1.187 +	    return 1;
   1.188 +	  }  
   1.189 +		if (memcmp(&(RC2cipher[n][0]),buf,8) != 0)
   1.190 +			{
   1.191 +			fprintf(stdout,"ecb rc2 error encrypting\n");
   1.192 +			fprintf(stdout,"got     :");
   1.193 +			for (i=0; i<8; i++)
   1.194 +				fprintf(stdout,"%02X ",buf[i]);
   1.195 +			fprintf(stdout,"\n");
   1.196 +			fprintf(stdout,"expected:");
   1.197 +			for (i=0; i<8; i++)
   1.198 +				fprintf(stdout,"%02X ",RC2cipher[n][i]);
   1.199 +			err=20;
   1.200 +			fprintf(stdout,"\n");
   1.201 +			}
   1.202 +
   1.203 +		RC2_ecb_encrypt(buf,buf2,&key,RC2_DECRYPT);
   1.204 +	   if(errno==ENOMEM)
   1.205 +    {
   1.206 +	    return 1;
   1.207 +	  }  
   1.208 +		if (memcmp(&(RC2plain[n][0]),buf2,8) != 0)
   1.209 +			{
   1.210 +			fprintf(stdout,"ecb RC2 error decrypting\n");
   1.211 +			fprintf(stdout,"got     :");
   1.212 +			for (i=0; i<8; i++)
   1.213 +				fprintf(stdout,"%02X ",buf[i]);
   1.214 +			printf("\n");
   1.215 +			fprintf(stdout,"expected:");
   1.216 +			for (i=0; i<8; i++)
   1.217 +				fprintf(stdout,"%02X ",RC2plain[n][i]);
   1.218 +			fprintf(stdout,"\n");
   1.219 +			err=3;
   1.220 +			}
   1.221 +		}
   1.222 +
   1.223 +	if (err == 0) fprintf(stdout,"ecb RC2 ok\n");
   1.224 +#ifdef undef
   1.225 +	memcpy(iv,k,8);
   1.226 +	idea_cbc_encrypt((unsigned char *)text,out,strlen(text)+1,&key,iv,1);
   1.227 +	memcpy(iv,k,8);
   1.228 +	idea_cbc_encrypt(out,out,8,&dkey,iv,0);
   1.229 +	idea_cbc_encrypt(&(out[8]),&(out[8]),strlen(text)+1-8,&dkey,iv,0);
   1.230 +	if (memcmp(text,out,strlen(text)+1) != 0)
   1.231 +		{
   1.232 +		fprintf(stdout,"cbc idea bad\n");
   1.233 +		err=4;
   1.234 +		}
   1.235 +	else
   1.236 +		fprintf(stdout,"cbc idea ok\n");
   1.237 +
   1.238 +	fprintf(stdout,"cfb64 idea ");
   1.239 +	if (cfb64_test(cfb_cipher64))
   1.240 +		{
   1.241 +		fprintf(stdout,"bad\n");
   1.242 +		err=5;
   1.243 +		}
   1.244 +	else
   1.245 +		fprintf(stdout,"ok\n");
   1.246 +#endif
   1.247 +
   1.248 +#ifdef OPENSSL_SYS_NETWARE
   1.249 +    if (err) fprintf(stdout,"ERROR: %d\n", err);
   1.250 +#endif
   1.251 +    fprintf(stdout,"Test case Passed\n");    
   1.252 +	//EXIT(err);
   1.253 +	return(err);
   1.254 +	}
   1.255 +
   1.256 +#ifdef undef
   1.257 +static int cfb64_test(unsigned char *cfb_cipher)
   1.258 +        {
   1.259 +        IDEA_KEY_SCHEDULE eks,dks;
   1.260 +        int err=0,i,n;
   1.261 +
   1.262 +        idea_set_encrypt_key(cfb_key,&eks);
   1.263 +        idea_set_decrypt_key(&eks,&dks);
   1.264 +        memcpy(cfb_tmp,cfb_iv,8);
   1.265 +        n=0;
   1.266 +        idea_cfb64_encrypt(plain,cfb_buf1,(long)12,&eks,
   1.267 +                cfb_tmp,&n,IDEA_ENCRYPT);
   1.268 +        idea_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),
   1.269 +                (long)CFB_TEST_SIZE-12,&eks,
   1.270 +                cfb_tmp,&n,IDEA_ENCRYPT);
   1.271 +        if (memcmp(cfb_cipher,cfb_buf1,CFB_TEST_SIZE) != 0)
   1.272 +                {
   1.273 +                err=1;
   1.274 +                fprintf(stdout,"idea_cfb64_encrypt encrypt error\n");
   1.275 +                for (i=0; i<CFB_TEST_SIZE; i+=8)
   1.276 +                        printf("%s\n",pt(&(cfb_buf1[i])));
   1.277 +                }
   1.278 +        memcpy(cfb_tmp,cfb_iv,8);
   1.279 +        n=0;
   1.280 +        idea_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)17,&eks,
   1.281 +                cfb_tmp,&n,IDEA_DECRYPT);
   1.282 +        idea_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
   1.283 +                (long)CFB_TEST_SIZE-17,&dks,
   1.284 +                cfb_tmp,&n,IDEA_DECRYPT);
   1.285 +        if (memcmp(plain,cfb_buf2,CFB_TEST_SIZE) != 0)
   1.286 +                {
   1.287 +                err=1;
   1.288 +                fprintf(stdout,"idea_cfb_encrypt decrypt error\n");
   1.289 +                for (i=0; i<24; i+=8)
   1.290 +                        printf("%s\n",pt(&(cfb_buf2[i])));
   1.291 +                }
   1.292 +        return(err);
   1.293 +        }
   1.294 +
   1.295 +static char *pt(unsigned char *p)
   1.296 +	{
   1.297 +	static char bufs[10][20];
   1.298 +	static int bnum=0;
   1.299 +	char *ret;
   1.300 +	int i;
   1.301 +	static char *f="0123456789ABCDEF";
   1.302 +
   1.303 +	ret= &(bufs[bnum++][0]);
   1.304 +	bnum%=10;
   1.305 +	for (i=0; i<8; i++)
   1.306 +		{
   1.307 +		ret[i*2]=f[(p[i]>>4)&0xf];
   1.308 +		ret[i*2+1]=f[p[i]&0xf];
   1.309 +		}
   1.310 +	ret[16]='\0';
   1.311 +	return(ret);
   1.312 +	}
   1.313 +	
   1.314 +#endif
   1.315 +#endif