155714Skris/* crypto/rc2/rc2test.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 855714Skris * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1555714Skris * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 2255714Skris * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4055714Skris * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 5255714Skris * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris/* This has been a quickly hacked 'ideatest.c'. When I add tests for other 6055714Skris * RC2 modes, more of the code will be uncommented. */ 6155714Skris 6255714Skris#include <stdio.h> 6355714Skris#include <string.h> 6455714Skris#include <stdlib.h> 6555714Skris 66109998Smarkm#include "../e_os.h" 67109998Smarkm 68109998Smarkm#ifdef OPENSSL_NO_RC2 6955714Skrisint main(int argc, char *argv[]) 7055714Skris{ 7155714Skris printf("No RC2 support\n"); 7255714Skris return(0); 7355714Skris} 7455714Skris#else 7555714Skris#include <openssl/rc2.h> 7655714Skris 7759191Skrisstatic unsigned char RC2key[4][16]={ 7855714Skris {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 7955714Skris 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 8055714Skris {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 8155714Skris 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}, 8255714Skris {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 8355714Skris 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 8455714Skris {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 8555714Skris 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F}, 8655714Skris }; 8755714Skris 8859191Skrisstatic unsigned char RC2plain[4][8]={ 8955714Skris {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 9055714Skris {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 9155714Skris {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}, 9255714Skris {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 9355714Skris }; 9455714Skris 9559191Skrisstatic unsigned char RC2cipher[4][8]={ 9655714Skris {0x1C,0x19,0x8A,0x83,0x8D,0xF0,0x28,0xB7}, 9755714Skris {0x21,0x82,0x9C,0x78,0xA9,0xF9,0xC0,0x74}, 9855714Skris {0x13,0xDB,0x35,0x17,0xD3,0x21,0x86,0x9E}, 9955714Skris {0x50,0xDC,0x01,0x62,0xBD,0x75,0x7F,0x31}, 10055714Skris }; 10155714Skris/************/ 10255714Skris#ifdef undef 10355714Skrisunsigned char k[16]={ 10455714Skris 0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04, 10555714Skris 0x00,0x05,0x00,0x06,0x00,0x07,0x00,0x08}; 10655714Skris 10755714Skrisunsigned char in[8]={0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x03}; 10855714Skrisunsigned char c[8]={0x11,0xFB,0xED,0x2B,0x01,0x98,0x6D,0xE5}; 10955714Skrisunsigned char out[80]; 11055714Skris 11155714Skrischar *text="Hello to all people out there"; 11255714Skris 11355714Skrisstatic unsigned char cfb_key[16]={ 11455714Skris 0xe1,0xf0,0xc3,0xd2,0xa5,0xb4,0x87,0x96, 11555714Skris 0x69,0x78,0x4b,0x5a,0x2d,0x3c,0x0f,0x1e, 11655714Skris }; 11755714Skrisstatic unsigned char cfb_iv[80]={0x34,0x12,0x78,0x56,0xab,0x90,0xef,0xcd}; 11855714Skrisstatic unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8]; 11955714Skris#define CFB_TEST_SIZE 24 12055714Skrisstatic unsigned char plain[CFB_TEST_SIZE]= 12155714Skris { 12255714Skris 0x4e,0x6f,0x77,0x20,0x69,0x73, 12355714Skris 0x20,0x74,0x68,0x65,0x20,0x74, 12455714Skris 0x69,0x6d,0x65,0x20,0x66,0x6f, 12555714Skris 0x72,0x20,0x61,0x6c,0x6c,0x20 12655714Skris }; 12755714Skrisstatic unsigned char cfb_cipher64[CFB_TEST_SIZE]={ 12855714Skris 0x59,0xD8,0xE2,0x65,0x00,0x58,0x6C,0x3F, 12955714Skris 0x2C,0x17,0x25,0xD0,0x1A,0x38,0xB7,0x2A, 13055714Skris 0x39,0x61,0x37,0xDC,0x79,0xFB,0x9F,0x45 13155714Skris 13255714Skris/* 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38, 13355714Skris 0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9, 13455714Skris 0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/ 13555714Skris }; 13655714Skris 13755714Skris 13855714Skris/*static int cfb64_test(unsigned char *cfb_cipher);*/ 13955714Skrisstatic char *pt(unsigned char *p); 14055714Skris#endif 14155714Skris 14255714Skrisint main(int argc, char *argv[]) 14355714Skris { 14455714Skris int i,n,err=0; 14555714Skris RC2_KEY key; 14655714Skris unsigned char buf[8],buf2[8]; 14755714Skris 14855714Skris for (n=0; n<4; n++) 14955714Skris { 15055714Skris RC2_set_key(&key,16,&(RC2key[n][0]),0 /* or 1024 */); 15155714Skris 15255714Skris RC2_ecb_encrypt(&(RC2plain[n][0]),buf,&key,RC2_ENCRYPT); 15355714Skris if (memcmp(&(RC2cipher[n][0]),buf,8) != 0) 15455714Skris { 15555714Skris printf("ecb rc2 error encrypting\n"); 15655714Skris printf("got :"); 15755714Skris for (i=0; i<8; i++) 15855714Skris printf("%02X ",buf[i]); 15955714Skris printf("\n"); 16055714Skris printf("expected:"); 16155714Skris for (i=0; i<8; i++) 16255714Skris printf("%02X ",RC2cipher[n][i]); 16355714Skris err=20; 16455714Skris printf("\n"); 16555714Skris } 16655714Skris 16755714Skris RC2_ecb_encrypt(buf,buf2,&key,RC2_DECRYPT); 16855714Skris if (memcmp(&(RC2plain[n][0]),buf2,8) != 0) 16955714Skris { 17055714Skris printf("ecb RC2 error decrypting\n"); 17155714Skris printf("got :"); 17255714Skris for (i=0; i<8; i++) 17355714Skris printf("%02X ",buf[i]); 17455714Skris printf("\n"); 17555714Skris printf("expected:"); 17655714Skris for (i=0; i<8; i++) 17755714Skris printf("%02X ",RC2plain[n][i]); 17855714Skris printf("\n"); 17955714Skris err=3; 18055714Skris } 18155714Skris } 18255714Skris 18355714Skris if (err == 0) printf("ecb RC2 ok\n"); 18455714Skris#ifdef undef 18555714Skris memcpy(iv,k,8); 18655714Skris idea_cbc_encrypt((unsigned char *)text,out,strlen(text)+1,&key,iv,1); 18755714Skris memcpy(iv,k,8); 18855714Skris idea_cbc_encrypt(out,out,8,&dkey,iv,0); 18955714Skris idea_cbc_encrypt(&(out[8]),&(out[8]),strlen(text)+1-8,&dkey,iv,0); 19055714Skris if (memcmp(text,out,strlen(text)+1) != 0) 19155714Skris { 19255714Skris printf("cbc idea bad\n"); 19355714Skris err=4; 19455714Skris } 19555714Skris else 19655714Skris printf("cbc idea ok\n"); 19755714Skris 19855714Skris printf("cfb64 idea "); 19955714Skris if (cfb64_test(cfb_cipher64)) 20055714Skris { 20155714Skris printf("bad\n"); 20255714Skris err=5; 20355714Skris } 20455714Skris else 20555714Skris printf("ok\n"); 20655714Skris#endif 20755714Skris 208160814Ssimon#ifdef OPENSSL_SYS_NETWARE 209160814Ssimon if (err) printf("ERROR: %d\n", err); 210160814Ssimon#endif 211109998Smarkm EXIT(err); 21255714Skris return(err); 21355714Skris } 21455714Skris 21555714Skris#ifdef undef 21655714Skrisstatic int cfb64_test(unsigned char *cfb_cipher) 21755714Skris { 21855714Skris IDEA_KEY_SCHEDULE eks,dks; 21955714Skris int err=0,i,n; 22055714Skris 22155714Skris idea_set_encrypt_key(cfb_key,&eks); 22255714Skris idea_set_decrypt_key(&eks,&dks); 22355714Skris memcpy(cfb_tmp,cfb_iv,8); 22455714Skris n=0; 22555714Skris idea_cfb64_encrypt(plain,cfb_buf1,(long)12,&eks, 22655714Skris cfb_tmp,&n,IDEA_ENCRYPT); 22755714Skris idea_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]), 22855714Skris (long)CFB_TEST_SIZE-12,&eks, 22955714Skris cfb_tmp,&n,IDEA_ENCRYPT); 23055714Skris if (memcmp(cfb_cipher,cfb_buf1,CFB_TEST_SIZE) != 0) 23155714Skris { 23255714Skris err=1; 23355714Skris printf("idea_cfb64_encrypt encrypt error\n"); 23455714Skris for (i=0; i<CFB_TEST_SIZE; i+=8) 23555714Skris printf("%s\n",pt(&(cfb_buf1[i]))); 23655714Skris } 23755714Skris memcpy(cfb_tmp,cfb_iv,8); 23855714Skris n=0; 23955714Skris idea_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)17,&eks, 24055714Skris cfb_tmp,&n,IDEA_DECRYPT); 24155714Skris idea_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]), 24255714Skris (long)CFB_TEST_SIZE-17,&dks, 24355714Skris cfb_tmp,&n,IDEA_DECRYPT); 24455714Skris if (memcmp(plain,cfb_buf2,CFB_TEST_SIZE) != 0) 24555714Skris { 24655714Skris err=1; 24755714Skris printf("idea_cfb_encrypt decrypt error\n"); 24855714Skris for (i=0; i<24; i+=8) 24955714Skris printf("%s\n",pt(&(cfb_buf2[i]))); 25055714Skris } 25155714Skris return(err); 25255714Skris } 25355714Skris 25455714Skrisstatic char *pt(unsigned char *p) 25555714Skris { 25655714Skris static char bufs[10][20]; 25755714Skris static int bnum=0; 25855714Skris char *ret; 25955714Skris int i; 26055714Skris static char *f="0123456789ABCDEF"; 26155714Skris 26255714Skris ret= &(bufs[bnum++][0]); 26355714Skris bnum%=10; 26455714Skris for (i=0; i<8; i++) 26555714Skris { 26655714Skris ret[i*2]=f[(p[i]>>4)&0xf]; 26755714Skris ret[i*2+1]=f[p[i]&0xf]; 26855714Skris } 26955714Skris ret[16]='\0'; 27055714Skris return(ret); 27155714Skris } 27255714Skris 27355714Skris#endif 27455714Skris#endif 275