155714Skris/* crypto/cast/casttest.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. 8280297Sjkim * 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). 15280297Sjkim * 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. 22280297Sjkim * 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 :-). 37280297Sjkim * 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)" 40280297Sjkim * 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. 52280297Sjkim * 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#include <stdio.h> 6055714Skris#include <string.h> 6155714Skris#include <stdlib.h> 62160814Ssimon#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_CAST is defined */ 6355714Skris 64109998Smarkm#include "../e_os.h" 65109998Smarkm 66109998Smarkm#ifdef OPENSSL_NO_CAST 6755714Skrisint main(int argc, char *argv[]) 6855714Skris{ 6955714Skris printf("No CAST support\n"); 70280297Sjkim return (0); 7155714Skris} 7255714Skris#else 73280297Sjkim# include <openssl/cast.h> 7455714Skris 75280297Sjkim# define FULL_TEST 7655714Skris 77280297Sjkimstatic unsigned char k[16] = { 78280297Sjkim 0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 79280297Sjkim 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A 80280297Sjkim}; 8155714Skris 82280297Sjkimstatic unsigned char in[8] = 83280297Sjkim { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }; 8455714Skris 85280297Sjkimstatic int k_len[3] = { 16, 10, 5 }; 86280297Sjkim 87280297Sjkimstatic unsigned char c[3][8] = { 88280297Sjkim {0x23, 0x8B, 0x4F, 0xE5, 0x84, 0x7E, 0x44, 0xB2}, 89280297Sjkim {0xEB, 0x6A, 0x71, 0x1A, 0x2C, 0x02, 0x27, 0x1B}, 90280297Sjkim {0x7A, 0xC8, 0x16, 0xD1, 0x6E, 0x9B, 0x30, 0x2E}, 91280297Sjkim}; 92280297Sjkim 9359191Skrisstatic unsigned char out[80]; 9455714Skris 95280297Sjkimstatic unsigned char in_a[16] = { 96280297Sjkim 0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 97280297Sjkim 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A 98280297Sjkim}; 9955714Skris 100280297Sjkimstatic unsigned char in_b[16] = { 101280297Sjkim 0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 102280297Sjkim 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A 103280297Sjkim}; 10455714Skris 105280297Sjkimstatic unsigned char c_a[16] = { 106280297Sjkim 0xEE, 0xA9, 0xD0, 0xA2, 0x49, 0xFD, 0x3B, 0xA6, 107280297Sjkim 0xB3, 0x43, 0x6F, 0xB8, 0x9D, 0x6D, 0xCA, 0x92 108280297Sjkim}; 10955714Skris 110280297Sjkimstatic unsigned char c_b[16] = { 111280297Sjkim 0xB2, 0xC9, 0x5E, 0xB0, 0x0C, 0x31, 0xAD, 0x71, 112280297Sjkim 0x80, 0xAC, 0x05, 0xB8, 0xE8, 0x3D, 0x69, 0x6E 113280297Sjkim}; 11455714Skris 115280297Sjkim# if 0 116280297Sjkimchar *text = "Hello to all people out there"; 11755714Skris 118280297Sjkimstatic unsigned char cfb_key[16] = { 119280297Sjkim 0xe1, 0xf0, 0xc3, 0xd2, 0xa5, 0xb4, 0x87, 0x96, 120280297Sjkim 0x69, 0x78, 0x4b, 0x5a, 0x2d, 0x3c, 0x0f, 0x1e, 121280297Sjkim}; 122280297Sjkimstatic unsigned char cfb_iv[80] = 123280297Sjkim { 0x34, 0x12, 0x78, 0x56, 0xab, 0x90, 0xef, 0xcd }; 124280297Sjkimstatic unsigned char cfb_buf1[40], cfb_buf2[40], cfb_tmp[8]; 125280297Sjkim# define CFB_TEST_SIZE 24 126280297Sjkimstatic unsigned char plain[CFB_TEST_SIZE] = { 127280297Sjkim 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 128280297Sjkim 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 129280297Sjkim 0x69, 0x6d, 0x65, 0x20, 0x66, 0x6f, 130280297Sjkim 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20 131280297Sjkim}; 132280297Sjkim 133280297Sjkimstatic unsigned char cfb_cipher64[CFB_TEST_SIZE] = { 134280297Sjkim 0x59, 0xD8, 0xE2, 0x65, 0x00, 0x58, 0x6C, 0x3F, 135280297Sjkim 0x2C, 0x17, 0x25, 0xD0, 0x1A, 0x38, 0xB7, 0x2A, 136280297Sjkim 0x39, 0x61, 0x37, 0xDC, 0x79, 0xFB, 0x9F, 0x45 137280297Sjkim/*- 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38, 138280297Sjkim 0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9, 139280297Sjkim 0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/ 140280297Sjkim}; 141280297Sjkim# endif 142280297Sjkim 14355714Skrisint main(int argc, char *argv[]) 144280297Sjkim{ 145280297Sjkim# ifdef FULL_TEST 14655714Skris long l; 14755714Skris CAST_KEY key_b; 148280297Sjkim# endif 149280297Sjkim int i, z, err = 0; 15055714Skris CAST_KEY key; 15155714Skris 152280297Sjkim for (z = 0; z < 3; z++) { 153280297Sjkim CAST_set_key(&key, k_len[z], k); 15455714Skris 155280297Sjkim CAST_ecb_encrypt(in, out, &key, CAST_ENCRYPT); 156280297Sjkim if (memcmp(out, &(c[z][0]), 8) != 0) { 157280297Sjkim printf("ecb cast error encrypting for keysize %d\n", 158280297Sjkim k_len[z] * 8); 159280297Sjkim printf("got :"); 160280297Sjkim for (i = 0; i < 8; i++) 161280297Sjkim printf("%02X ", out[i]); 162280297Sjkim printf("\n"); 163280297Sjkim printf("expected:"); 164280297Sjkim for (i = 0; i < 8; i++) 165280297Sjkim printf("%02X ", c[z][i]); 166280297Sjkim err = 20; 167280297Sjkim printf("\n"); 168280297Sjkim } 16955714Skris 170280297Sjkim CAST_ecb_encrypt(out, out, &key, CAST_DECRYPT); 171280297Sjkim if (memcmp(out, in, 8) != 0) { 172280297Sjkim printf("ecb cast error decrypting for keysize %d\n", 173280297Sjkim k_len[z] * 8); 174280297Sjkim printf("got :"); 175280297Sjkim for (i = 0; i < 8; i++) 176280297Sjkim printf("%02X ", out[i]); 177280297Sjkim printf("\n"); 178280297Sjkim printf("expected:"); 179280297Sjkim for (i = 0; i < 8; i++) 180280297Sjkim printf("%02X ", in[i]); 181280297Sjkim printf("\n"); 182280297Sjkim err = 3; 183280297Sjkim } 184280297Sjkim } 18555714Skris if (err == 0) 186280297Sjkim printf("ecb cast5 ok\n"); 18755714Skris 188280297Sjkim# ifdef FULL_TEST 189280297Sjkim { 190280297Sjkim unsigned char out_a[16], out_b[16]; 191280297Sjkim static char *hex = "0123456789ABCDEF"; 19255714Skris 193280297Sjkim printf("This test will take some time...."); 194280297Sjkim fflush(stdout); 195280297Sjkim memcpy(out_a, in_a, sizeof(in_a)); 196280297Sjkim memcpy(out_b, in_b, sizeof(in_b)); 197280297Sjkim i = 1; 19855714Skris 199280297Sjkim for (l = 0; l < 1000000L; l++) { 200280297Sjkim CAST_set_key(&key_b, 16, out_b); 201280297Sjkim CAST_ecb_encrypt(&(out_a[0]), &(out_a[0]), &key_b, CAST_ENCRYPT); 202280297Sjkim CAST_ecb_encrypt(&(out_a[8]), &(out_a[8]), &key_b, CAST_ENCRYPT); 203280297Sjkim CAST_set_key(&key, 16, out_a); 204280297Sjkim CAST_ecb_encrypt(&(out_b[0]), &(out_b[0]), &key, CAST_ENCRYPT); 205280297Sjkim CAST_ecb_encrypt(&(out_b[8]), &(out_b[8]), &key, CAST_ENCRYPT); 206280297Sjkim if ((l & 0xffff) == 0xffff) { 207280297Sjkim printf("%c", hex[i & 0x0f]); 208280297Sjkim fflush(stdout); 209280297Sjkim i++; 210280297Sjkim } 211280297Sjkim } 21255714Skris 213280297Sjkim if ((memcmp(out_a, c_a, sizeof(c_a)) != 0) || 214280297Sjkim (memcmp(out_b, c_b, sizeof(c_b)) != 0)) { 215280297Sjkim printf("\n"); 216280297Sjkim printf("Error\n"); 21755714Skris 218280297Sjkim printf("A out ="); 219280297Sjkim for (i = 0; i < 16; i++) 220280297Sjkim printf("%02X ", out_a[i]); 221280297Sjkim printf("\nactual="); 222280297Sjkim for (i = 0; i < 16; i++) 223280297Sjkim printf("%02X ", c_a[i]); 224280297Sjkim printf("\n"); 22555714Skris 226280297Sjkim printf("B out ="); 227280297Sjkim for (i = 0; i < 16; i++) 228280297Sjkim printf("%02X ", out_b[i]); 229280297Sjkim printf("\nactual="); 230280297Sjkim for (i = 0; i < 16; i++) 231280297Sjkim printf("%02X ", c_b[i]); 232280297Sjkim printf("\n"); 233280297Sjkim } else 234280297Sjkim printf(" ok\n"); 235280297Sjkim } 236280297Sjkim# endif 237280297Sjkim 238109998Smarkm EXIT(err); 239280297Sjkim return (err); 240280297Sjkim} 24155714Skris#endif 242