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. 8296341Sdelphij * 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). 15296341Sdelphij * 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. 22296341Sdelphij * 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 :-). 37296341Sdelphij * 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)" 40296341Sdelphij * 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. 52296341Sdelphij * 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"); 69296341Sdelphij return (0); 7061828Smarkm} 7161828Smarkm#else 72296341Sdelphij# include <openssl/idea.h> 7361828Smarkm 74296341Sdelphijunsigned char k[16] = { 75296341Sdelphij 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 76296341Sdelphij 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 77296341Sdelphij}; 7861828Smarkm 79296341Sdelphijunsigned char in[8] = { 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03 }; 80296341Sdelphijunsigned char c[8] = { 0x11, 0xFB, 0xED, 0x2B, 0x01, 0x98, 0x6D, 0xE5 }; 81296341Sdelphij 8261828Smarkmunsigned char out[80]; 8361828Smarkm 84296341Sdelphijchar *text = "Hello to all people out there"; 8561828Smarkm 86296341Sdelphijstatic unsigned char cfb_key[16] = { 87296341Sdelphij 0xe1, 0xf0, 0xc3, 0xd2, 0xa5, 0xb4, 0x87, 0x96, 88296341Sdelphij 0x69, 0x78, 0x4b, 0x5a, 0x2d, 0x3c, 0x0f, 0x1e, 89296341Sdelphij}; 90296341Sdelphijstatic unsigned char cfb_iv[80] = 91296341Sdelphij { 0x34, 0x12, 0x78, 0x56, 0xab, 0x90, 0xef, 0xcd }; 92296341Sdelphijstatic unsigned char cfb_buf1[40], cfb_buf2[40], cfb_tmp[8]; 93296341Sdelphij# define CFB_TEST_SIZE 24 94296341Sdelphijstatic unsigned char plain[CFB_TEST_SIZE] = { 95296341Sdelphij 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 96296341Sdelphij 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 97296341Sdelphij 0x69, 0x6d, 0x65, 0x20, 0x66, 0x6f, 98296341Sdelphij 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20 99296341Sdelphij}; 10061828Smarkm 101296341Sdelphijstatic unsigned char cfb_cipher64[CFB_TEST_SIZE] = { 102296341Sdelphij 0x59, 0xD8, 0xE2, 0x65, 0x00, 0x58, 0x6C, 0x3F, 103296341Sdelphij 0x2C, 0x17, 0x25, 0xD0, 0x1A, 0x38, 0xB7, 0x2A, 104296341Sdelphij 0x39, 0x61, 0x37, 0xDC, 0x79, 0xFB, 0x9F, 0x45 105296341Sdelphij/*- 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38, 106296341Sdelphij 0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9, 107296341Sdelphij 0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/ 108296341Sdelphij}; 10961828Smarkm 11061828Smarkmstatic int cfb64_test(unsigned char *cfb_cipher); 11161828Smarkmstatic char *pt(unsigned char *p); 11261828Smarkmint main(int argc, char *argv[]) 113296341Sdelphij{ 114296341Sdelphij int i, err = 0; 115296341Sdelphij IDEA_KEY_SCHEDULE key, dkey; 116296341Sdelphij unsigned char iv[8]; 11761828Smarkm 118296341Sdelphij idea_set_encrypt_key(k, &key); 119296341Sdelphij idea_ecb_encrypt(in, out, &key); 120296341Sdelphij if (memcmp(out, c, 8) != 0) { 121296341Sdelphij printf("ecb idea error encrypting\n"); 122296341Sdelphij printf("got :"); 123296341Sdelphij for (i = 0; i < 8; i++) 124296341Sdelphij printf("%02X ", out[i]); 125296341Sdelphij printf("\n"); 126296341Sdelphij printf("expected:"); 127296341Sdelphij for (i = 0; i < 8; i++) 128296341Sdelphij printf("%02X ", c[i]); 129296341Sdelphij err = 20; 130296341Sdelphij printf("\n"); 131296341Sdelphij } 13261828Smarkm 133296341Sdelphij idea_set_decrypt_key(&key, &dkey); 134296341Sdelphij idea_ecb_encrypt(c, out, &dkey); 135296341Sdelphij if (memcmp(out, in, 8) != 0) { 136296341Sdelphij printf("ecb idea error decrypting\n"); 137296341Sdelphij printf("got :"); 138296341Sdelphij for (i = 0; i < 8; i++) 139296341Sdelphij printf("%02X ", out[i]); 140296341Sdelphij printf("\n"); 141296341Sdelphij printf("expected:"); 142296341Sdelphij for (i = 0; i < 8; i++) 143296341Sdelphij printf("%02X ", in[i]); 144296341Sdelphij printf("\n"); 145296341Sdelphij err = 3; 146296341Sdelphij } 14761828Smarkm 148296341Sdelphij if (err == 0) 149296341Sdelphij printf("ecb idea ok\n"); 15061828Smarkm 151296341Sdelphij memcpy(iv, k, 8); 152296341Sdelphij idea_cbc_encrypt((unsigned char *)text, out, strlen(text) + 1, &key, iv, 153296341Sdelphij 1); 154296341Sdelphij memcpy(iv, k, 8); 155296341Sdelphij idea_cbc_encrypt(out, out, 8, &dkey, iv, 0); 156296341Sdelphij idea_cbc_encrypt(&(out[8]), &(out[8]), strlen(text) + 1 - 8, &dkey, iv, 157296341Sdelphij 0); 158296341Sdelphij if (memcmp(text, out, strlen(text) + 1) != 0) { 159296341Sdelphij printf("cbc idea bad\n"); 160296341Sdelphij err = 4; 161296341Sdelphij } else 162296341Sdelphij printf("cbc idea ok\n"); 16361828Smarkm 164296341Sdelphij printf("cfb64 idea "); 165296341Sdelphij if (cfb64_test(cfb_cipher64)) { 166296341Sdelphij printf("bad\n"); 167296341Sdelphij err = 5; 168296341Sdelphij } else 169296341Sdelphij printf("ok\n"); 17061828Smarkm 171296341Sdelphij# ifdef OPENSSL_SYS_NETWARE 172296341Sdelphij if (err) 173296341Sdelphij printf("ERROR: %d\n", err); 174296341Sdelphij# endif 175296341Sdelphij EXIT(err); 176296341Sdelphij return (err); 177296341Sdelphij} 17861828Smarkm 17961828Smarkmstatic int cfb64_test(unsigned char *cfb_cipher) 180296341Sdelphij{ 181296341Sdelphij IDEA_KEY_SCHEDULE eks, dks; 182296341Sdelphij int err = 0, i, n; 18361828Smarkm 184296341Sdelphij idea_set_encrypt_key(cfb_key, &eks); 185296341Sdelphij idea_set_decrypt_key(&eks, &dks); 186296341Sdelphij memcpy(cfb_tmp, cfb_iv, 8); 187296341Sdelphij n = 0; 188296341Sdelphij idea_cfb64_encrypt(plain, cfb_buf1, (long)12, &eks, 189296341Sdelphij cfb_tmp, &n, IDEA_ENCRYPT); 190296341Sdelphij idea_cfb64_encrypt(&(plain[12]), &(cfb_buf1[12]), 191296341Sdelphij (long)CFB_TEST_SIZE - 12, &eks, 192296341Sdelphij cfb_tmp, &n, IDEA_ENCRYPT); 193296341Sdelphij if (memcmp(cfb_cipher, cfb_buf1, CFB_TEST_SIZE) != 0) { 194296341Sdelphij err = 1; 195296341Sdelphij printf("idea_cfb64_encrypt encrypt error\n"); 196296341Sdelphij for (i = 0; i < CFB_TEST_SIZE; i += 8) 197296341Sdelphij printf("%s\n", pt(&(cfb_buf1[i]))); 198296341Sdelphij } 199296341Sdelphij memcpy(cfb_tmp, cfb_iv, 8); 200296341Sdelphij n = 0; 201296341Sdelphij idea_cfb64_encrypt(cfb_buf1, cfb_buf2, (long)13, &eks, 202296341Sdelphij cfb_tmp, &n, IDEA_DECRYPT); 203296341Sdelphij idea_cfb64_encrypt(&(cfb_buf1[13]), &(cfb_buf2[13]), 204296341Sdelphij (long)CFB_TEST_SIZE - 13, &eks, 205296341Sdelphij cfb_tmp, &n, IDEA_DECRYPT); 206296341Sdelphij if (memcmp(plain, cfb_buf2, CFB_TEST_SIZE) != 0) { 207296341Sdelphij err = 1; 208296341Sdelphij printf("idea_cfb_encrypt decrypt error\n"); 209296341Sdelphij for (i = 0; i < 24; i += 8) 210296341Sdelphij printf("%s\n", pt(&(cfb_buf2[i]))); 211296341Sdelphij } 212296341Sdelphij return (err); 213296341Sdelphij} 21461828Smarkm 21561828Smarkmstatic char *pt(unsigned char *p) 216296341Sdelphij{ 217296341Sdelphij static char bufs[10][20]; 218296341Sdelphij static int bnum = 0; 219296341Sdelphij char *ret; 220296341Sdelphij int i; 221296341Sdelphij static char *f = "0123456789ABCDEF"; 22261828Smarkm 223296341Sdelphij ret = &(bufs[bnum++][0]); 224296341Sdelphij bnum %= 10; 225296341Sdelphij for (i = 0; i < 8; i++) { 226296341Sdelphij ret[i * 2] = f[(p[i] >> 4) & 0xf]; 227296341Sdelphij ret[i * 2 + 1] = f[p[i] & 0xf]; 228296341Sdelphij } 229296341Sdelphij ret[16] = '\0'; 230296341Sdelphij return (ret); 231296341Sdelphij} 23261828Smarkm#endif 233