xref: /openbsd-src/regress/lib/libcrypto/idea/ideatest.c (revision 657b055c4f2d15cc79398afc5793a57a63e700c8)
1*657b055cStb /*	$OpenBSD: ideatest.c,v 1.2 2018/07/17 17:06:49 tb Exp $	*/
23c6bd008Smiod /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33c6bd008Smiod  * All rights reserved.
43c6bd008Smiod  *
53c6bd008Smiod  * This package is an SSL implementation written
63c6bd008Smiod  * by Eric Young (eay@cryptsoft.com).
73c6bd008Smiod  * The implementation was written so as to conform with Netscapes SSL.
83c6bd008Smiod  *
93c6bd008Smiod  * This library is free for commercial and non-commercial use as long as
103c6bd008Smiod  * the following conditions are aheared to.  The following conditions
113c6bd008Smiod  * apply to all code found in this distribution, be it the RC4, RSA,
123c6bd008Smiod  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
133c6bd008Smiod  * included with this distribution is covered by the same copyright terms
143c6bd008Smiod  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
153c6bd008Smiod  *
163c6bd008Smiod  * Copyright remains Eric Young's, and as such any Copyright notices in
173c6bd008Smiod  * the code are not to be removed.
183c6bd008Smiod  * If this package is used in a product, Eric Young should be given attribution
193c6bd008Smiod  * as the author of the parts of the library used.
203c6bd008Smiod  * This can be in the form of a textual message at program startup or
213c6bd008Smiod  * in documentation (online or textual) provided with the package.
223c6bd008Smiod  *
233c6bd008Smiod  * Redistribution and use in source and binary forms, with or without
243c6bd008Smiod  * modification, are permitted provided that the following conditions
253c6bd008Smiod  * are met:
263c6bd008Smiod  * 1. Redistributions of source code must retain the copyright
273c6bd008Smiod  *    notice, this list of conditions and the following disclaimer.
283c6bd008Smiod  * 2. Redistributions in binary form must reproduce the above copyright
293c6bd008Smiod  *    notice, this list of conditions and the following disclaimer in the
303c6bd008Smiod  *    documentation and/or other materials provided with the distribution.
313c6bd008Smiod  * 3. All advertising materials mentioning features or use of this software
323c6bd008Smiod  *    must display the following acknowledgement:
333c6bd008Smiod  *    "This product includes cryptographic software written by
343c6bd008Smiod  *     Eric Young (eay@cryptsoft.com)"
353c6bd008Smiod  *    The word 'cryptographic' can be left out if the rouines from the library
363c6bd008Smiod  *    being used are not cryptographic related :-).
373c6bd008Smiod  * 4. If you include any Windows specific code (or a derivative thereof) from
383c6bd008Smiod  *    the apps directory (application code) you must include an acknowledgement:
393c6bd008Smiod  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
403c6bd008Smiod  *
413c6bd008Smiod  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
423c6bd008Smiod  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
433c6bd008Smiod  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
443c6bd008Smiod  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
453c6bd008Smiod  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
463c6bd008Smiod  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
473c6bd008Smiod  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
483c6bd008Smiod  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
493c6bd008Smiod  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
503c6bd008Smiod  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
513c6bd008Smiod  * SUCH DAMAGE.
523c6bd008Smiod  *
533c6bd008Smiod  * The licence and distribution terms for any publically available version or
543c6bd008Smiod  * derivative of this code cannot be changed.  i.e. this code cannot simply be
553c6bd008Smiod  * copied and put under another distribution licence
563c6bd008Smiod  * [including the GNU Public Licence.]
573c6bd008Smiod  */
583c6bd008Smiod 
593c6bd008Smiod #include <stdio.h>
603c6bd008Smiod #include <string.h>
613c6bd008Smiod #include <stdlib.h>
623c6bd008Smiod 
633c6bd008Smiod #include <openssl/idea.h>
643c6bd008Smiod 
653c6bd008Smiod unsigned char k[16]={
663c6bd008Smiod 	0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04,
673c6bd008Smiod 	0x00,0x05,0x00,0x06,0x00,0x07,0x00,0x08};
683c6bd008Smiod 
693c6bd008Smiod unsigned char in[8]={0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x03};
703c6bd008Smiod unsigned char  c[8]={0x11,0xFB,0xED,0x2B,0x01,0x98,0x6D,0xE5};
713c6bd008Smiod unsigned char out[80];
723c6bd008Smiod 
733c6bd008Smiod char *text="Hello to all people out there";
743c6bd008Smiod 
753c6bd008Smiod static unsigned char cfb_key[16]={
763c6bd008Smiod 	0xe1,0xf0,0xc3,0xd2,0xa5,0xb4,0x87,0x96,
773c6bd008Smiod 	0x69,0x78,0x4b,0x5a,0x2d,0x3c,0x0f,0x1e,
783c6bd008Smiod 	};
793c6bd008Smiod static unsigned char cfb_iv[80]={0x34,0x12,0x78,0x56,0xab,0x90,0xef,0xcd};
803c6bd008Smiod static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
813c6bd008Smiod #define CFB_TEST_SIZE 24
823c6bd008Smiod static unsigned char plain[CFB_TEST_SIZE]=
833c6bd008Smiod         {
843c6bd008Smiod         0x4e,0x6f,0x77,0x20,0x69,0x73,
853c6bd008Smiod         0x20,0x74,0x68,0x65,0x20,0x74,
863c6bd008Smiod         0x69,0x6d,0x65,0x20,0x66,0x6f,
873c6bd008Smiod         0x72,0x20,0x61,0x6c,0x6c,0x20
883c6bd008Smiod         };
893c6bd008Smiod static unsigned char cfb_cipher64[CFB_TEST_SIZE]={
903c6bd008Smiod 	0x59,0xD8,0xE2,0x65,0x00,0x58,0x6C,0x3F,
913c6bd008Smiod 	0x2C,0x17,0x25,0xD0,0x1A,0x38,0xB7,0x2A,
923c6bd008Smiod 	0x39,0x61,0x37,0xDC,0x79,0xFB,0x9F,0x45
933c6bd008Smiod 
943c6bd008Smiod /*	0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
953c6bd008Smiod 	0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
963c6bd008Smiod 	0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
973c6bd008Smiod 	};
983c6bd008Smiod 
993c6bd008Smiod static int cfb64_test(unsigned char *cfb_cipher);
1003c6bd008Smiod static char *pt(unsigned char *p);
main(int argc,char * argv[])1013c6bd008Smiod int main(int argc, char *argv[])
1023c6bd008Smiod 	{
1033c6bd008Smiod 	int i,err=0;
1043c6bd008Smiod 	IDEA_KEY_SCHEDULE key,dkey;
1053c6bd008Smiod 	unsigned char iv[8];
1063c6bd008Smiod 
1073c6bd008Smiod 	idea_set_encrypt_key(k,&key);
1083c6bd008Smiod 	idea_ecb_encrypt(in,out,&key);
1093c6bd008Smiod 	if (memcmp(out,c,8) != 0)
1103c6bd008Smiod 		{
1113c6bd008Smiod 		printf("ecb idea error encrypting\n");
1123c6bd008Smiod 		printf("got     :");
1133c6bd008Smiod 		for (i=0; i<8; i++)
1143c6bd008Smiod 			printf("%02X ",out[i]);
1153c6bd008Smiod 		printf("\n");
1163c6bd008Smiod 		printf("expected:");
1173c6bd008Smiod 		for (i=0; i<8; i++)
1183c6bd008Smiod 			printf("%02X ",c[i]);
1193c6bd008Smiod 		err=20;
1203c6bd008Smiod 		printf("\n");
1213c6bd008Smiod 		}
1223c6bd008Smiod 
1233c6bd008Smiod 	idea_set_decrypt_key(&key,&dkey);
1243c6bd008Smiod 	idea_ecb_encrypt(c,out,&dkey);
1253c6bd008Smiod 	if (memcmp(out,in,8) != 0)
1263c6bd008Smiod 		{
1273c6bd008Smiod 		printf("ecb idea error decrypting\n");
1283c6bd008Smiod 		printf("got     :");
1293c6bd008Smiod 		for (i=0; i<8; i++)
1303c6bd008Smiod 			printf("%02X ",out[i]);
1313c6bd008Smiod 		printf("\n");
1323c6bd008Smiod 		printf("expected:");
1333c6bd008Smiod 		for (i=0; i<8; i++)
1343c6bd008Smiod 			printf("%02X ",in[i]);
1353c6bd008Smiod 		printf("\n");
1363c6bd008Smiod 		err=3;
1373c6bd008Smiod 		}
1383c6bd008Smiod 
1393c6bd008Smiod 	if (err == 0) printf("ecb idea ok\n");
1403c6bd008Smiod 
1413c6bd008Smiod 	memcpy(iv,k,8);
1423c6bd008Smiod 	idea_cbc_encrypt((unsigned char *)text,out,strlen(text)+1,&key,iv,1);
1433c6bd008Smiod 	memcpy(iv,k,8);
1443c6bd008Smiod 	idea_cbc_encrypt(out,out,8,&dkey,iv,0);
1453c6bd008Smiod 	idea_cbc_encrypt(&(out[8]),&(out[8]),strlen(text)+1-8,&dkey,iv,0);
1463c6bd008Smiod 	if (memcmp(text,out,strlen(text)+1) != 0)
1473c6bd008Smiod 		{
1483c6bd008Smiod 		printf("cbc idea bad\n");
1493c6bd008Smiod 		err=4;
1503c6bd008Smiod 		}
1513c6bd008Smiod 	else
1523c6bd008Smiod 		printf("cbc idea ok\n");
1533c6bd008Smiod 
1543c6bd008Smiod 	printf("cfb64 idea ");
1553c6bd008Smiod 	if (cfb64_test(cfb_cipher64))
1563c6bd008Smiod 		{
1573c6bd008Smiod 		printf("bad\n");
1583c6bd008Smiod 		err=5;
1593c6bd008Smiod 		}
1603c6bd008Smiod 	else
1613c6bd008Smiod 		printf("ok\n");
1623c6bd008Smiod 
1633c6bd008Smiod 	exit(err);
1643c6bd008Smiod 	}
1653c6bd008Smiod 
cfb64_test(unsigned char * cfb_cipher)1663c6bd008Smiod static int cfb64_test(unsigned char *cfb_cipher)
1673c6bd008Smiod         {
1683c6bd008Smiod         IDEA_KEY_SCHEDULE eks,dks;
1693c6bd008Smiod         int err=0,i,n;
1703c6bd008Smiod 
1713c6bd008Smiod         idea_set_encrypt_key(cfb_key,&eks);
1723c6bd008Smiod         idea_set_decrypt_key(&eks,&dks);
1733c6bd008Smiod         memcpy(cfb_tmp,cfb_iv,8);
1743c6bd008Smiod         n=0;
1753c6bd008Smiod         idea_cfb64_encrypt(plain,cfb_buf1,(long)12,&eks,
1763c6bd008Smiod                 cfb_tmp,&n,IDEA_ENCRYPT);
1773c6bd008Smiod         idea_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),
1783c6bd008Smiod                 (long)CFB_TEST_SIZE-12,&eks,
1793c6bd008Smiod                 cfb_tmp,&n,IDEA_ENCRYPT);
1803c6bd008Smiod         if (memcmp(cfb_cipher,cfb_buf1,CFB_TEST_SIZE) != 0)
1813c6bd008Smiod                 {
1823c6bd008Smiod                 err=1;
1833c6bd008Smiod                 printf("idea_cfb64_encrypt encrypt error\n");
1843c6bd008Smiod                 for (i=0; i<CFB_TEST_SIZE; i+=8)
1853c6bd008Smiod                         printf("%s\n",pt(&(cfb_buf1[i])));
1863c6bd008Smiod                 }
1873c6bd008Smiod         memcpy(cfb_tmp,cfb_iv,8);
1883c6bd008Smiod         n=0;
1893c6bd008Smiod         idea_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)17,&eks,
1903c6bd008Smiod                 cfb_tmp,&n,IDEA_DECRYPT);
1913c6bd008Smiod         idea_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
1923c6bd008Smiod                 (long)CFB_TEST_SIZE-17,&dks,
1933c6bd008Smiod                 cfb_tmp,&n,IDEA_DECRYPT);
1943c6bd008Smiod         if (memcmp(plain,cfb_buf2,CFB_TEST_SIZE) != 0)
1953c6bd008Smiod                 {
1963c6bd008Smiod                 err=1;
1973c6bd008Smiod                 printf("idea_cfb_encrypt decrypt error\n");
1983c6bd008Smiod                 for (i=0; i<24; i+=8)
1993c6bd008Smiod                         printf("%s\n",pt(&(cfb_buf2[i])));
2003c6bd008Smiod                 }
2013c6bd008Smiod         return(err);
2023c6bd008Smiod         }
2033c6bd008Smiod 
pt(unsigned char * p)2043c6bd008Smiod static char *pt(unsigned char *p)
2053c6bd008Smiod 	{
2063c6bd008Smiod 	static char bufs[10][20];
2073c6bd008Smiod 	static int bnum=0;
2083c6bd008Smiod 	char *ret;
2093c6bd008Smiod 	int i;
2103c6bd008Smiod 	static char *f="0123456789ABCDEF";
2113c6bd008Smiod 
2123c6bd008Smiod 	ret= &(bufs[bnum++][0]);
2133c6bd008Smiod 	bnum%=10;
2143c6bd008Smiod 	for (i=0; i<8; i++)
2153c6bd008Smiod 		{
2163c6bd008Smiod 		ret[i*2]=f[(p[i]>>4)&0xf];
2173c6bd008Smiod 		ret[i*2+1]=f[p[i]&0xf];
2183c6bd008Smiod 		}
2193c6bd008Smiod 	ret[16]='\0';
2203c6bd008Smiod 	return(ret);
2213c6bd008Smiod 	}
222