xref: /onnv-gate/usr/src/common/openssl/crypto/idea/ideatest.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/idea/ideatest.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <string.h>
610Sstevel@tonic-gate #include <stdlib.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include "../e_os.h"
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #ifdef OPENSSL_NO_IDEA
main(int argc,char * argv[])660Sstevel@tonic-gate int main(int argc, char *argv[])
670Sstevel@tonic-gate {
680Sstevel@tonic-gate     printf("No IDEA support\n");
690Sstevel@tonic-gate     return(0);
700Sstevel@tonic-gate }
710Sstevel@tonic-gate #else
720Sstevel@tonic-gate #include <openssl/idea.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate unsigned char k[16]={
750Sstevel@tonic-gate 	0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04,
760Sstevel@tonic-gate 	0x00,0x05,0x00,0x06,0x00,0x07,0x00,0x08};
770Sstevel@tonic-gate 
780Sstevel@tonic-gate unsigned char in[8]={0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x03};
790Sstevel@tonic-gate unsigned char  c[8]={0x11,0xFB,0xED,0x2B,0x01,0x98,0x6D,0xE5};
800Sstevel@tonic-gate unsigned char out[80];
810Sstevel@tonic-gate 
820Sstevel@tonic-gate char *text="Hello to all people out there";
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static unsigned char cfb_key[16]={
850Sstevel@tonic-gate 	0xe1,0xf0,0xc3,0xd2,0xa5,0xb4,0x87,0x96,
860Sstevel@tonic-gate 	0x69,0x78,0x4b,0x5a,0x2d,0x3c,0x0f,0x1e,
870Sstevel@tonic-gate 	};
880Sstevel@tonic-gate static unsigned char cfb_iv[80]={0x34,0x12,0x78,0x56,0xab,0x90,0xef,0xcd};
890Sstevel@tonic-gate static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
900Sstevel@tonic-gate #define CFB_TEST_SIZE 24
910Sstevel@tonic-gate static unsigned char plain[CFB_TEST_SIZE]=
920Sstevel@tonic-gate         {
930Sstevel@tonic-gate         0x4e,0x6f,0x77,0x20,0x69,0x73,
940Sstevel@tonic-gate         0x20,0x74,0x68,0x65,0x20,0x74,
950Sstevel@tonic-gate         0x69,0x6d,0x65,0x20,0x66,0x6f,
960Sstevel@tonic-gate         0x72,0x20,0x61,0x6c,0x6c,0x20
970Sstevel@tonic-gate         };
980Sstevel@tonic-gate static unsigned char cfb_cipher64[CFB_TEST_SIZE]={
990Sstevel@tonic-gate 	0x59,0xD8,0xE2,0x65,0x00,0x58,0x6C,0x3F,
1000Sstevel@tonic-gate 	0x2C,0x17,0x25,0xD0,0x1A,0x38,0xB7,0x2A,
1010Sstevel@tonic-gate 	0x39,0x61,0x37,0xDC,0x79,0xFB,0x9F,0x45
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*	0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
1040Sstevel@tonic-gate 	0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
1050Sstevel@tonic-gate 	0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
1060Sstevel@tonic-gate 	};
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static int cfb64_test(unsigned char *cfb_cipher);
1090Sstevel@tonic-gate static char *pt(unsigned char *p);
main(int argc,char * argv[])1100Sstevel@tonic-gate int main(int argc, char *argv[])
1110Sstevel@tonic-gate 	{
1120Sstevel@tonic-gate 	int i,err=0;
1130Sstevel@tonic-gate 	IDEA_KEY_SCHEDULE key,dkey;
1140Sstevel@tonic-gate 	unsigned char iv[8];
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	idea_set_encrypt_key(k,&key);
1170Sstevel@tonic-gate 	idea_ecb_encrypt(in,out,&key);
1180Sstevel@tonic-gate 	if (memcmp(out,c,8) != 0)
1190Sstevel@tonic-gate 		{
1200Sstevel@tonic-gate 		printf("ecb idea error encrypting\n");
1210Sstevel@tonic-gate 		printf("got     :");
1220Sstevel@tonic-gate 		for (i=0; i<8; i++)
1230Sstevel@tonic-gate 			printf("%02X ",out[i]);
1240Sstevel@tonic-gate 		printf("\n");
1250Sstevel@tonic-gate 		printf("expected:");
1260Sstevel@tonic-gate 		for (i=0; i<8; i++)
1270Sstevel@tonic-gate 			printf("%02X ",c[i]);
1280Sstevel@tonic-gate 		err=20;
1290Sstevel@tonic-gate 		printf("\n");
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	idea_set_decrypt_key(&key,&dkey);
1330Sstevel@tonic-gate 	idea_ecb_encrypt(c,out,&dkey);
1340Sstevel@tonic-gate 	if (memcmp(out,in,8) != 0)
1350Sstevel@tonic-gate 		{
1360Sstevel@tonic-gate 		printf("ecb idea error decrypting\n");
1370Sstevel@tonic-gate 		printf("got     :");
1380Sstevel@tonic-gate 		for (i=0; i<8; i++)
1390Sstevel@tonic-gate 			printf("%02X ",out[i]);
1400Sstevel@tonic-gate 		printf("\n");
1410Sstevel@tonic-gate 		printf("expected:");
1420Sstevel@tonic-gate 		for (i=0; i<8; i++)
1430Sstevel@tonic-gate 			printf("%02X ",in[i]);
1440Sstevel@tonic-gate 		printf("\n");
1450Sstevel@tonic-gate 		err=3;
1460Sstevel@tonic-gate 		}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (err == 0) printf("ecb idea ok\n");
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	memcpy(iv,k,8);
1510Sstevel@tonic-gate 	idea_cbc_encrypt((unsigned char *)text,out,strlen(text)+1,&key,iv,1);
1520Sstevel@tonic-gate 	memcpy(iv,k,8);
1530Sstevel@tonic-gate 	idea_cbc_encrypt(out,out,8,&dkey,iv,0);
1540Sstevel@tonic-gate 	idea_cbc_encrypt(&(out[8]),&(out[8]),strlen(text)+1-8,&dkey,iv,0);
1550Sstevel@tonic-gate 	if (memcmp(text,out,strlen(text)+1) != 0)
1560Sstevel@tonic-gate 		{
1570Sstevel@tonic-gate 		printf("cbc idea bad\n");
1580Sstevel@tonic-gate 		err=4;
1590Sstevel@tonic-gate 		}
1600Sstevel@tonic-gate 	else
1610Sstevel@tonic-gate 		printf("cbc idea ok\n");
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	printf("cfb64 idea ");
1640Sstevel@tonic-gate 	if (cfb64_test(cfb_cipher64))
1650Sstevel@tonic-gate 		{
1660Sstevel@tonic-gate 		printf("bad\n");
1670Sstevel@tonic-gate 		err=5;
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 	else
1700Sstevel@tonic-gate 		printf("ok\n");
1710Sstevel@tonic-gate 
172*2139Sjp161948 #ifdef OPENSSL_SYS_NETWARE
173*2139Sjp161948     if (err) printf("ERROR: %d\n", err);
174*2139Sjp161948 #endif
1750Sstevel@tonic-gate 	EXIT(err);
1760Sstevel@tonic-gate 	return(err);
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
cfb64_test(unsigned char * cfb_cipher)1790Sstevel@tonic-gate static int cfb64_test(unsigned char *cfb_cipher)
1800Sstevel@tonic-gate         {
1810Sstevel@tonic-gate         IDEA_KEY_SCHEDULE eks,dks;
1820Sstevel@tonic-gate         int err=0,i,n;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate         idea_set_encrypt_key(cfb_key,&eks);
1850Sstevel@tonic-gate         idea_set_decrypt_key(&eks,&dks);
1860Sstevel@tonic-gate         memcpy(cfb_tmp,cfb_iv,8);
1870Sstevel@tonic-gate         n=0;
1880Sstevel@tonic-gate         idea_cfb64_encrypt(plain,cfb_buf1,(long)12,&eks,
1890Sstevel@tonic-gate                 cfb_tmp,&n,IDEA_ENCRYPT);
1900Sstevel@tonic-gate         idea_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),
1910Sstevel@tonic-gate                 (long)CFB_TEST_SIZE-12,&eks,
1920Sstevel@tonic-gate                 cfb_tmp,&n,IDEA_ENCRYPT);
1930Sstevel@tonic-gate         if (memcmp(cfb_cipher,cfb_buf1,CFB_TEST_SIZE) != 0)
1940Sstevel@tonic-gate                 {
1950Sstevel@tonic-gate                 err=1;
1960Sstevel@tonic-gate                 printf("idea_cfb64_encrypt encrypt error\n");
1970Sstevel@tonic-gate                 for (i=0; i<CFB_TEST_SIZE; i+=8)
1980Sstevel@tonic-gate                         printf("%s\n",pt(&(cfb_buf1[i])));
1990Sstevel@tonic-gate                 }
2000Sstevel@tonic-gate         memcpy(cfb_tmp,cfb_iv,8);
2010Sstevel@tonic-gate         n=0;
2020Sstevel@tonic-gate         idea_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)17,&eks,
2030Sstevel@tonic-gate                 cfb_tmp,&n,IDEA_DECRYPT);
2040Sstevel@tonic-gate         idea_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
2050Sstevel@tonic-gate                 (long)CFB_TEST_SIZE-17,&dks,
2060Sstevel@tonic-gate                 cfb_tmp,&n,IDEA_DECRYPT);
2070Sstevel@tonic-gate         if (memcmp(plain,cfb_buf2,CFB_TEST_SIZE) != 0)
2080Sstevel@tonic-gate                 {
2090Sstevel@tonic-gate                 err=1;
2100Sstevel@tonic-gate                 printf("idea_cfb_encrypt decrypt error\n");
2110Sstevel@tonic-gate                 for (i=0; i<24; i+=8)
2120Sstevel@tonic-gate                         printf("%s\n",pt(&(cfb_buf2[i])));
2130Sstevel@tonic-gate                 }
2140Sstevel@tonic-gate         return(err);
2150Sstevel@tonic-gate         }
2160Sstevel@tonic-gate 
pt(unsigned char * p)2170Sstevel@tonic-gate static char *pt(unsigned char *p)
2180Sstevel@tonic-gate 	{
2190Sstevel@tonic-gate 	static char bufs[10][20];
2200Sstevel@tonic-gate 	static int bnum=0;
2210Sstevel@tonic-gate 	char *ret;
2220Sstevel@tonic-gate 	int i;
2230Sstevel@tonic-gate 	static char *f="0123456789ABCDEF";
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	ret= &(bufs[bnum++][0]);
2260Sstevel@tonic-gate 	bnum%=10;
2270Sstevel@tonic-gate 	for (i=0; i<8; i++)
2280Sstevel@tonic-gate 		{
2290Sstevel@tonic-gate 		ret[i*2]=f[(p[i]>>4)&0xf];
2300Sstevel@tonic-gate 		ret[i*2+1]=f[p[i]&0xf];
2310Sstevel@tonic-gate 		}
2320Sstevel@tonic-gate 	ret[16]='\0';
2330Sstevel@tonic-gate 	return(ret);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate #endif
236