casttest.c revision 296465
1125665Sache/* crypto/cast/casttest.c */ 2136759Speter/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 321308Sache * All rights reserved. 421308Sache * 521308Sache * This package is an SSL implementation written 6136759Speter * by Eric Young (eay@cryptsoft.com). 721308Sache * The implementation was written so as to conform with Netscapes SSL. 821308Sache * 921308Sache * This library is free for commercial and non-commercial use as long as 1021308Sache * the following conditions are aheared to. The following conditions 1121308Sache * apply to all code found in this distribution, be it the RC4, RSA, 1221308Sache * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1358310Sache * included with this distribution is covered by the same copyright terms 1421308Sache * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1521308Sache * 1621308Sache * Copyright remains Eric Young's, and as such any Copyright notices in 1721308Sache * the code are not to be removed. 1821308Sache * If this package is used in a product, Eric Young should be given attribution 1921308Sache * as the author of the parts of the library used. 2021308Sache * This can be in the form of a textual message at program startup or 2121308Sache * in documentation (online or textual) provided with the package. 2221308Sache * 2321308Sache * Redistribution and use in source and binary forms, with or without 2458310Sache * modification, are permitted provided that the following conditions 2521308Sache * are met: 2621308Sache * 1. Redistributions of source code must retain the copyright 2721308Sache * notice, this list of conditions and the following disclaimer. 2821308Sache * 2. Redistributions in binary form must reproduce the above copyright 2921308Sache * notice, this list of conditions and the following disclaimer in the 3021308Sache * documentation and/or other materials provided with the distribution. 3121308Sache * 3. All advertising materials mentioning features or use of this software 3221308Sache * must display the following acknowledgement: 3321308Sache * "This product includes cryptographic software written by 3421308Sache * Eric Young (eay@cryptsoft.com)" 3521308Sache * The word 'cryptographic' can be left out if the rouines from the library 3621308Sache * being used are not cryptographic related :-). 3721308Sache * 4. If you include any Windows specific code (or a derivative thereof) from 3821308Sache * the apps directory (application code) you must include an acknowledgement: 3921308Sache * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4021308Sache * 4121308Sache * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4221308Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4321308Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4421308Sache * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4521308Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4621308Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4721308Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4821308Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4921308Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5021308Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5121308Sache * SUCH DAMAGE. 5221308Sache * 5321308Sache * The licence and distribution terms for any publically available version or 5421308Sache * derivative of this code cannot be changed. i.e. this code cannot simply be 5521308Sache * copied and put under another distribution licence 56119610Sache * [including the GNU Public Licence.] 57119610Sache */ 5821308Sache 5921308Sache#include <stdio.h> 6021308Sache#include <string.h> 6158310Sache#include <stdlib.h> 6258310Sache#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_CAST is defined */ 6358310Sache 6421308Sache#include "../e_os.h" 6521308Sache 6621308Sache#ifdef OPENSSL_NO_CAST 6721308Sacheint main(int argc, char *argv[]) 68136759Speter{ 69136759Speter printf("No CAST support\n"); 7021308Sache return (0); 7121308Sache} 7221308Sache#else 7321308Sache# include <openssl/cast.h> 74119610Sache 7521308Sache# define FULL_TEST 7621308Sache 7721308Sachestatic unsigned char k[16] = { 7821308Sache 0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 7921308Sache 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A 8021308Sache}; 8121308Sache 8221308Sachestatic unsigned char in[8] = 8321308Sache { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }; 8421308Sache 8521308Sachestatic int k_len[3] = { 16, 10, 5 }; 8621308Sache 8721308Sachestatic unsigned char c[3][8] = { 8821308Sache {0x23, 0x8B, 0x4F, 0xE5, 0x84, 0x7E, 0x44, 0xB2}, 8921308Sache {0xEB, 0x6A, 0x71, 0x1A, 0x2C, 0x02, 0x27, 0x1B}, 9021308Sache {0x7A, 0xC8, 0x16, 0xD1, 0x6E, 0x9B, 0x30, 0x2E}, 9121308Sache}; 9221308Sache 93119610Sachestatic unsigned char out[80]; 94119610Sache 95119610Sachestatic unsigned char in_a[16] = { 9621308Sache 0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 97119610Sache 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A 9821308Sache}; 9921308Sache 10021308Sachestatic unsigned char in_b[16] = { 10121308Sache 0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 10221308Sache 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A 10321308Sache}; 10421308Sache 10575406Sachestatic unsigned char c_a[16] = { 10621308Sache 0xEE, 0xA9, 0xD0, 0xA2, 0x49, 0xFD, 0x3B, 0xA6, 10721308Sache 0xB3, 0x43, 0x6F, 0xB8, 0x9D, 0x6D, 0xCA, 0x92 108119610Sache}; 10921308Sache 110119610Sachestatic unsigned char c_b[16] = { 111119610Sache 0xB2, 0xC9, 0x5E, 0xB0, 0x0C, 0x31, 0xAD, 0x71, 112119610Sache 0x80, 0xAC, 0x05, 0xB8, 0xE8, 0x3D, 0x69, 0x6E 11321308Sache}; 11421308Sache 11521308Sache# if 0 11621308Sachechar *text = "Hello to all people out there"; 11721308Sache 11821308Sachestatic unsigned char cfb_key[16] = { 11921308Sache 0xe1, 0xf0, 0xc3, 0xd2, 0xa5, 0xb4, 0x87, 0x96, 12021308Sache 0x69, 0x78, 0x4b, 0x5a, 0x2d, 0x3c, 0x0f, 0x1e, 12121308Sache}; 12221308Sachestatic unsigned char cfb_iv[80] = 12321308Sache { 0x34, 0x12, 0x78, 0x56, 0xab, 0x90, 0xef, 0xcd }; 12421308Sachestatic unsigned char cfb_buf1[40], cfb_buf2[40], cfb_tmp[8]; 12521308Sache# define CFB_TEST_SIZE 24 12621308Sachestatic unsigned char plain[CFB_TEST_SIZE] = { 12721308Sache 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 12821308Sache 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 12921308Sache 0x69, 0x6d, 0x65, 0x20, 0x66, 0x6f, 13021308Sache 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20 13121308Sache}; 13221308Sache 13321308Sachestatic unsigned char cfb_cipher64[CFB_TEST_SIZE] = { 13421308Sache 0x59, 0xD8, 0xE2, 0x65, 0x00, 0x58, 0x6C, 0x3F, 13521308Sache 0x2C, 0x17, 0x25, 0xD0, 0x1A, 0x38, 0xB7, 0x2A, 13621308Sache 0x39, 0x61, 0x37, 0xDC, 0x79, 0xFB, 0x9F, 0x45 13721308Sache/*- 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38, 13821308Sache 0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9, 13921308Sache 0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/ 14021308Sache}; 141136759Speter# endif 142136759Speter 143136759Speterint main(int argc, char *argv[]) 144136759Speter{ 145136759Speter# ifdef FULL_TEST 146136759Speter long l; 147136759Speter CAST_KEY key_b; 148136759Speter# endif 149136759Speter int i, z, err = 0; 150136759Speter CAST_KEY key; 15121308Sache 15221308Sache for (z = 0; z < 3; z++) { 15321308Sache CAST_set_key(&key, k_len[z], k); 15421308Sache 15521308Sache CAST_ecb_encrypt(in, out, &key, CAST_ENCRYPT); 15621308Sache if (memcmp(out, &(c[z][0]), 8) != 0) { 15721308Sache printf("ecb cast error encrypting for keysize %d\n", 15821308Sache k_len[z] * 8); 15921308Sache printf("got :"); 16021308Sache for (i = 0; i < 8; i++) 16121308Sache printf("%02X ", out[i]); 16221308Sache printf("\n"); 16321308Sache printf("expected:"); 16421308Sache for (i = 0; i < 8; i++) 16521308Sache printf("%02X ", c[z][i]); 16621308Sache err = 20; 16721308Sache printf("\n"); 16821308Sache } 16921308Sache 17021308Sache CAST_ecb_encrypt(out, out, &key, CAST_DECRYPT); 17121308Sache if (memcmp(out, in, 8) != 0) { 17221308Sache printf("ecb cast error decrypting for keysize %d\n", 17321308Sache k_len[z] * 8); 17421308Sache printf("got :"); 17521308Sache for (i = 0; i < 8; i++) 176119610Sache printf("%02X ", out[i]); 177119610Sache printf("\n"); 17821308Sache printf("expected:"); 17921308Sache for (i = 0; i < 8; i++) 18021308Sache printf("%02X ", in[i]); 18121308Sache printf("\n"); 18221308Sache err = 3; 18321308Sache } 184119610Sache } 18521308Sache if (err == 0) 18621308Sache printf("ecb cast5 ok\n"); 18721308Sache 18821308Sache# ifdef FULL_TEST 18921308Sache { 19021308Sache unsigned char out_a[16], out_b[16]; 19121308Sache static char *hex = "0123456789ABCDEF"; 19221308Sache 19321308Sache printf("This test will take some time...."); 19421308Sache fflush(stdout); 19521308Sache memcpy(out_a, in_a, sizeof(in_a)); 196119610Sache memcpy(out_b, in_b, sizeof(in_b)); 19721308Sache i = 1; 19821308Sache 199119610Sache for (l = 0; l < 1000000L; l++) { 20021308Sache CAST_set_key(&key_b, 16, out_b); 20121308Sache CAST_ecb_encrypt(&(out_a[0]), &(out_a[0]), &key_b, CAST_ENCRYPT); 20221308Sache CAST_ecb_encrypt(&(out_a[8]), &(out_a[8]), &key_b, CAST_ENCRYPT); 20321308Sache CAST_set_key(&key, 16, out_a); 20421308Sache CAST_ecb_encrypt(&(out_b[0]), &(out_b[0]), &key, CAST_ENCRYPT); 20521308Sache CAST_ecb_encrypt(&(out_b[8]), &(out_b[8]), &key, CAST_ENCRYPT); 20621308Sache if ((l & 0xffff) == 0xffff) { 20721308Sache printf("%c", hex[i & 0x0f]); 20821308Sache fflush(stdout); 20921308Sache i++; 21021308Sache } 21121308Sache } 21221308Sache 21321308Sache if ((memcmp(out_a, c_a, sizeof(c_a)) != 0) || 21421308Sache (memcmp(out_b, c_b, sizeof(c_b)) != 0)) { 21521308Sache printf("\n"); 21621308Sache printf("Error\n"); 21721308Sache 21821308Sache printf("A out ="); 21921308Sache for (i = 0; i < 16; i++) 22021308Sache printf("%02X ", out_a[i]); 22121308Sache printf("\nactual="); 22221308Sache for (i = 0; i < 16; i++) 22321308Sache printf("%02X ", c_a[i]); 22421308Sache printf("\n"); 22521308Sache 22621308Sache printf("B out ="); 22721308Sache for (i = 0; i < 16; i++) 22821308Sache printf("%02X ", out_b[i]); 22921308Sache printf("\nactual="); 23021308Sache for (i = 0; i < 16; i++) 23121308Sache printf("%02X ", c_b[i]); 23221308Sache printf("\n"); 23321308Sache } else 23421308Sache printf(" ok\n"); 23521308Sache } 23621308Sache# endif 23721308Sache 23821308Sache EXIT(err); 23921308Sache return (err); 24021308Sache} 24121308Sache#endif 24221308Sache