161828Smarkm/* crypto/idea/ideatest.c */
261828Smarkm/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
361828Smarkm * All rights reserved.
461828Smarkm *
561828Smarkm * This package is an SSL implementation written
661828Smarkm * by Eric Young (eay@cryptsoft.com).
761828Smarkm * The implementation was written so as to conform with Netscapes SSL.
861828Smarkm *
961828Smarkm * This library is free for commercial and non-commercial use as long as
1061828Smarkm * the following conditions are aheared to.  The following conditions
1161828Smarkm * apply to all code found in this distribution, be it the RC4, RSA,
1261828Smarkm * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1361828Smarkm * included with this distribution is covered by the same copyright terms
1461828Smarkm * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1561828Smarkm *
1661828Smarkm * Copyright remains Eric Young's, and as such any Copyright notices in
1761828Smarkm * the code are not to be removed.
1861828Smarkm * If this package is used in a product, Eric Young should be given attribution
1961828Smarkm * as the author of the parts of the library used.
2061828Smarkm * This can be in the form of a textual message at program startup or
2161828Smarkm * in documentation (online or textual) provided with the package.
2261828Smarkm *
2361828Smarkm * Redistribution and use in source and binary forms, with or without
2461828Smarkm * modification, are permitted provided that the following conditions
2561828Smarkm * are met:
2661828Smarkm * 1. Redistributions of source code must retain the copyright
2761828Smarkm *    notice, this list of conditions and the following disclaimer.
2861828Smarkm * 2. Redistributions in binary form must reproduce the above copyright
2961828Smarkm *    notice, this list of conditions and the following disclaimer in the
3061828Smarkm *    documentation and/or other materials provided with the distribution.
3161828Smarkm * 3. All advertising materials mentioning features or use of this software
3261828Smarkm *    must display the following acknowledgement:
3361828Smarkm *    "This product includes cryptographic software written by
3461828Smarkm *     Eric Young (eay@cryptsoft.com)"
3561828Smarkm *    The word 'cryptographic' can be left out if the rouines from the library
3661828Smarkm *    being used are not cryptographic related :-).
3761828Smarkm * 4. If you include any Windows specific code (or a derivative thereof) from
3861828Smarkm *    the apps directory (application code) you must include an acknowledgement:
3961828Smarkm *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4061828Smarkm *
4161828Smarkm * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4261828Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4361828Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4461828Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4561828Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4661828Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4761828Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4861828Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4961828Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5061828Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5161828Smarkm * SUCH DAMAGE.
5261828Smarkm *
5361828Smarkm * The licence and distribution terms for any publically available version or
5461828Smarkm * derivative of this code cannot be changed.  i.e. this code cannot simply be
5561828Smarkm * copied and put under another distribution licence
5661828Smarkm * [including the GNU Public Licence.]
5761828Smarkm */
5861828Smarkm
5961828Smarkm#include <stdio.h>
6061828Smarkm#include <string.h>
6161828Smarkm#include <stdlib.h>
6261828Smarkm
63110007Smarkm#include "../e_os.h"
64110007Smarkm
65110007Smarkm#ifdef OPENSSL_NO_IDEA
6661828Smarkmint main(int argc, char *argv[])
6761828Smarkm{
6861828Smarkm    printf("No IDEA support\n");
6961828Smarkm    return(0);
7061828Smarkm}
7161828Smarkm#else
7261828Smarkm#include <openssl/idea.h>
7361828Smarkm
7461828Smarkmunsigned char k[16]={
7561828Smarkm	0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04,
7661828Smarkm	0x00,0x05,0x00,0x06,0x00,0x07,0x00,0x08};
7761828Smarkm
7861828Smarkmunsigned char in[8]={0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x03};
7961828Smarkmunsigned char  c[8]={0x11,0xFB,0xED,0x2B,0x01,0x98,0x6D,0xE5};
8061828Smarkmunsigned char out[80];
8161828Smarkm
8261828Smarkmchar *text="Hello to all people out there";
8361828Smarkm
8461828Smarkmstatic unsigned char cfb_key[16]={
8561828Smarkm	0xe1,0xf0,0xc3,0xd2,0xa5,0xb4,0x87,0x96,
8661828Smarkm	0x69,0x78,0x4b,0x5a,0x2d,0x3c,0x0f,0x1e,
8761828Smarkm	};
8861828Smarkmstatic unsigned char cfb_iv[80]={0x34,0x12,0x78,0x56,0xab,0x90,0xef,0xcd};
8961828Smarkmstatic unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
9061828Smarkm#define CFB_TEST_SIZE 24
9161828Smarkmstatic unsigned char plain[CFB_TEST_SIZE]=
9261828Smarkm        {
9361828Smarkm        0x4e,0x6f,0x77,0x20,0x69,0x73,
9461828Smarkm        0x20,0x74,0x68,0x65,0x20,0x74,
9561828Smarkm        0x69,0x6d,0x65,0x20,0x66,0x6f,
9661828Smarkm        0x72,0x20,0x61,0x6c,0x6c,0x20
9761828Smarkm        };
9861828Smarkmstatic unsigned char cfb_cipher64[CFB_TEST_SIZE]={
9961828Smarkm	0x59,0xD8,0xE2,0x65,0x00,0x58,0x6C,0x3F,
10061828Smarkm	0x2C,0x17,0x25,0xD0,0x1A,0x38,0xB7,0x2A,
10161828Smarkm	0x39,0x61,0x37,0xDC,0x79,0xFB,0x9F,0x45
10261828Smarkm
10361828Smarkm/*	0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
10461828Smarkm	0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
10561828Smarkm	0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
10661828Smarkm	};
10761828Smarkm
10861828Smarkmstatic int cfb64_test(unsigned char *cfb_cipher);
10961828Smarkmstatic char *pt(unsigned char *p);
11061828Smarkmint main(int argc, char *argv[])
11161828Smarkm	{
11261828Smarkm	int i,err=0;
11361828Smarkm	IDEA_KEY_SCHEDULE key,dkey;
11461828Smarkm	unsigned char iv[8];
11561828Smarkm
11661828Smarkm	idea_set_encrypt_key(k,&key);
11761828Smarkm	idea_ecb_encrypt(in,out,&key);
11861828Smarkm	if (memcmp(out,c,8) != 0)
11961828Smarkm		{
12061828Smarkm		printf("ecb idea error encrypting\n");
12161828Smarkm		printf("got     :");
12261828Smarkm		for (i=0; i<8; i++)
12361828Smarkm			printf("%02X ",out[i]);
12461828Smarkm		printf("\n");
12561828Smarkm		printf("expected:");
12661828Smarkm		for (i=0; i<8; i++)
12761828Smarkm			printf("%02X ",c[i]);
12861828Smarkm		err=20;
12961828Smarkm		printf("\n");
13061828Smarkm		}
13161828Smarkm
13261828Smarkm	idea_set_decrypt_key(&key,&dkey);
13361828Smarkm	idea_ecb_encrypt(c,out,&dkey);
13461828Smarkm	if (memcmp(out,in,8) != 0)
13561828Smarkm		{
13661828Smarkm		printf("ecb idea error decrypting\n");
13761828Smarkm		printf("got     :");
13861828Smarkm		for (i=0; i<8; i++)
13961828Smarkm			printf("%02X ",out[i]);
14061828Smarkm		printf("\n");
14161828Smarkm		printf("expected:");
14261828Smarkm		for (i=0; i<8; i++)
14361828Smarkm			printf("%02X ",in[i]);
14461828Smarkm		printf("\n");
14561828Smarkm		err=3;
14661828Smarkm		}
14761828Smarkm
14861828Smarkm	if (err == 0) printf("ecb idea ok\n");
14961828Smarkm
15061828Smarkm	memcpy(iv,k,8);
15161828Smarkm	idea_cbc_encrypt((unsigned char *)text,out,strlen(text)+1,&key,iv,1);
15261828Smarkm	memcpy(iv,k,8);
15361828Smarkm	idea_cbc_encrypt(out,out,8,&dkey,iv,0);
15461828Smarkm	idea_cbc_encrypt(&(out[8]),&(out[8]),strlen(text)+1-8,&dkey,iv,0);
15561828Smarkm	if (memcmp(text,out,strlen(text)+1) != 0)
15661828Smarkm		{
15761828Smarkm		printf("cbc idea bad\n");
15861828Smarkm		err=4;
15961828Smarkm		}
16061828Smarkm	else
16161828Smarkm		printf("cbc idea ok\n");
16261828Smarkm
16361828Smarkm	printf("cfb64 idea ");
16461828Smarkm	if (cfb64_test(cfb_cipher64))
16561828Smarkm		{
16661828Smarkm		printf("bad\n");
16761828Smarkm		err=5;
16861828Smarkm		}
16961828Smarkm	else
17061828Smarkm		printf("ok\n");
17161828Smarkm
172160817Ssimon#ifdef OPENSSL_SYS_NETWARE
173160817Ssimon    if (err) printf("ERROR: %d\n", err);
174160817Ssimon#endif
175110007Smarkm	EXIT(err);
17661828Smarkm	return(err);
17761828Smarkm	}
17861828Smarkm
17961828Smarkmstatic int cfb64_test(unsigned char *cfb_cipher)
18061828Smarkm        {
18161828Smarkm        IDEA_KEY_SCHEDULE eks,dks;
18261828Smarkm        int err=0,i,n;
18361828Smarkm
18461828Smarkm        idea_set_encrypt_key(cfb_key,&eks);
18561828Smarkm        idea_set_decrypt_key(&eks,&dks);
18661828Smarkm        memcpy(cfb_tmp,cfb_iv,8);
18761828Smarkm        n=0;
18861828Smarkm        idea_cfb64_encrypt(plain,cfb_buf1,(long)12,&eks,
18961828Smarkm                cfb_tmp,&n,IDEA_ENCRYPT);
19061828Smarkm        idea_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),
19161828Smarkm                (long)CFB_TEST_SIZE-12,&eks,
19261828Smarkm                cfb_tmp,&n,IDEA_ENCRYPT);
19361828Smarkm        if (memcmp(cfb_cipher,cfb_buf1,CFB_TEST_SIZE) != 0)
19461828Smarkm                {
19561828Smarkm                err=1;
19661828Smarkm                printf("idea_cfb64_encrypt encrypt error\n");
19761828Smarkm                for (i=0; i<CFB_TEST_SIZE; i+=8)
19861828Smarkm                        printf("%s\n",pt(&(cfb_buf1[i])));
19961828Smarkm                }
20061828Smarkm        memcpy(cfb_tmp,cfb_iv,8);
20161828Smarkm        n=0;
202279264Sdelphij        idea_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)13,&eks,
20361828Smarkm                cfb_tmp,&n,IDEA_DECRYPT);
204279264Sdelphij        idea_cfb64_encrypt(&(cfb_buf1[13]),&(cfb_buf2[13]),
205279264Sdelphij                (long)CFB_TEST_SIZE-13,&eks,
20661828Smarkm                cfb_tmp,&n,IDEA_DECRYPT);
20761828Smarkm        if (memcmp(plain,cfb_buf2,CFB_TEST_SIZE) != 0)
20861828Smarkm                {
20961828Smarkm                err=1;
21061828Smarkm                printf("idea_cfb_encrypt decrypt error\n");
21161828Smarkm                for (i=0; i<24; i+=8)
21261828Smarkm                        printf("%s\n",pt(&(cfb_buf2[i])));
21361828Smarkm                }
21461828Smarkm        return(err);
21561828Smarkm        }
21661828Smarkm
21761828Smarkmstatic char *pt(unsigned char *p)
21861828Smarkm	{
21961828Smarkm	static char bufs[10][20];
22061828Smarkm	static int bnum=0;
22161828Smarkm	char *ret;
22261828Smarkm	int i;
22361828Smarkm	static char *f="0123456789ABCDEF";
22461828Smarkm
22561828Smarkm	ret= &(bufs[bnum++][0]);
22661828Smarkm	bnum%=10;
22761828Smarkm	for (i=0; i<8; i++)
22861828Smarkm		{
22961828Smarkm		ret[i*2]=f[(p[i]>>4)&0xf];
23061828Smarkm		ret[i*2+1]=f[p[i]&0xf];
23161828Smarkm		}
23261828Smarkm	ret[16]='\0';
23361828Smarkm	return(ret);
23461828Smarkm	}
23561828Smarkm#endif
236