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.
8296341Sdelphij *
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).
15296341Sdelphij *
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.
22296341Sdelphij *
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 :-).
37296341Sdelphij * 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)"
40296341Sdelphij *
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.
52296341Sdelphij *
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");
70296341Sdelphij    return (0);
7155714Skris}
7255714Skris#else
73296341Sdelphij# include <openssl/cast.h>
7455714Skris
75296341Sdelphij# define FULL_TEST
7655714Skris
77296341Sdelphijstatic unsigned char k[16] = {
78296341Sdelphij    0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78,
79296341Sdelphij    0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A
80296341Sdelphij};
8155714Skris
82296341Sdelphijstatic unsigned char in[8] =
83296341Sdelphij    { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
8455714Skris
85296341Sdelphijstatic int k_len[3] = { 16, 10, 5 };
86296341Sdelphij
87296341Sdelphijstatic unsigned char c[3][8] = {
88296341Sdelphij    {0x23, 0x8B, 0x4F, 0xE5, 0x84, 0x7E, 0x44, 0xB2},
89296341Sdelphij    {0xEB, 0x6A, 0x71, 0x1A, 0x2C, 0x02, 0x27, 0x1B},
90296341Sdelphij    {0x7A, 0xC8, 0x16, 0xD1, 0x6E, 0x9B, 0x30, 0x2E},
91296341Sdelphij};
92296341Sdelphij
9359191Skrisstatic unsigned char out[80];
9455714Skris
95296341Sdelphijstatic unsigned char in_a[16] = {
96296341Sdelphij    0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78,
97296341Sdelphij    0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A
98296341Sdelphij};
9955714Skris
100296341Sdelphijstatic unsigned char in_b[16] = {
101296341Sdelphij    0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78,
102296341Sdelphij    0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A
103296341Sdelphij};
10455714Skris
105296341Sdelphijstatic unsigned char c_a[16] = {
106296341Sdelphij    0xEE, 0xA9, 0xD0, 0xA2, 0x49, 0xFD, 0x3B, 0xA6,
107296341Sdelphij    0xB3, 0x43, 0x6F, 0xB8, 0x9D, 0x6D, 0xCA, 0x92
108296341Sdelphij};
10955714Skris
110296341Sdelphijstatic unsigned char c_b[16] = {
111296341Sdelphij    0xB2, 0xC9, 0x5E, 0xB0, 0x0C, 0x31, 0xAD, 0x71,
112296341Sdelphij    0x80, 0xAC, 0x05, 0xB8, 0xE8, 0x3D, 0x69, 0x6E
113296341Sdelphij};
11455714Skris
115296341Sdelphij# if 0
116296341Sdelphijchar *text = "Hello to all people out there";
11755714Skris
118296341Sdelphijstatic unsigned char cfb_key[16] = {
119296341Sdelphij    0xe1, 0xf0, 0xc3, 0xd2, 0xa5, 0xb4, 0x87, 0x96,
120296341Sdelphij    0x69, 0x78, 0x4b, 0x5a, 0x2d, 0x3c, 0x0f, 0x1e,
121296341Sdelphij};
122296341Sdelphijstatic unsigned char cfb_iv[80] =
123296341Sdelphij    { 0x34, 0x12, 0x78, 0x56, 0xab, 0x90, 0xef, 0xcd };
124296341Sdelphijstatic unsigned char cfb_buf1[40], cfb_buf2[40], cfb_tmp[8];
125296341Sdelphij#  define CFB_TEST_SIZE 24
126296341Sdelphijstatic unsigned char plain[CFB_TEST_SIZE] = {
127296341Sdelphij    0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73,
128296341Sdelphij    0x20, 0x74, 0x68, 0x65, 0x20, 0x74,
129296341Sdelphij    0x69, 0x6d, 0x65, 0x20, 0x66, 0x6f,
130296341Sdelphij    0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20
131296341Sdelphij};
132296341Sdelphij
133296341Sdelphijstatic unsigned char cfb_cipher64[CFB_TEST_SIZE] = {
134296341Sdelphij    0x59, 0xD8, 0xE2, 0x65, 0x00, 0x58, 0x6C, 0x3F,
135296341Sdelphij    0x2C, 0x17, 0x25, 0xD0, 0x1A, 0x38, 0xB7, 0x2A,
136296341Sdelphij    0x39, 0x61, 0x37, 0xDC, 0x79, 0xFB, 0x9F, 0x45
137296341Sdelphij/*- 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
138296341Sdelphij    0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
139296341Sdelphij    0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
140296341Sdelphij};
141296341Sdelphij# endif
142296341Sdelphij
14355714Skrisint main(int argc, char *argv[])
144296341Sdelphij{
145296341Sdelphij# ifdef FULL_TEST
14655714Skris    long l;
14755714Skris    CAST_KEY key_b;
148296341Sdelphij# endif
149296341Sdelphij    int i, z, err = 0;
15055714Skris    CAST_KEY key;
15155714Skris
152296341Sdelphij    for (z = 0; z < 3; z++) {
153296341Sdelphij        CAST_set_key(&key, k_len[z], k);
15455714Skris
155296341Sdelphij        CAST_ecb_encrypt(in, out, &key, CAST_ENCRYPT);
156296341Sdelphij        if (memcmp(out, &(c[z][0]), 8) != 0) {
157296341Sdelphij            printf("ecb cast error encrypting for keysize %d\n",
158296341Sdelphij                   k_len[z] * 8);
159296341Sdelphij            printf("got     :");
160296341Sdelphij            for (i = 0; i < 8; i++)
161296341Sdelphij                printf("%02X ", out[i]);
162296341Sdelphij            printf("\n");
163296341Sdelphij            printf("expected:");
164296341Sdelphij            for (i = 0; i < 8; i++)
165296341Sdelphij                printf("%02X ", c[z][i]);
166296341Sdelphij            err = 20;
167296341Sdelphij            printf("\n");
168296341Sdelphij        }
16955714Skris
170296341Sdelphij        CAST_ecb_encrypt(out, out, &key, CAST_DECRYPT);
171296341Sdelphij        if (memcmp(out, in, 8) != 0) {
172296341Sdelphij            printf("ecb cast error decrypting for keysize %d\n",
173296341Sdelphij                   k_len[z] * 8);
174296341Sdelphij            printf("got     :");
175296341Sdelphij            for (i = 0; i < 8; i++)
176296341Sdelphij                printf("%02X ", out[i]);
177296341Sdelphij            printf("\n");
178296341Sdelphij            printf("expected:");
179296341Sdelphij            for (i = 0; i < 8; i++)
180296341Sdelphij                printf("%02X ", in[i]);
181296341Sdelphij            printf("\n");
182296341Sdelphij            err = 3;
183296341Sdelphij        }
184296341Sdelphij    }
18555714Skris    if (err == 0)
186296341Sdelphij        printf("ecb cast5 ok\n");
18755714Skris
188296341Sdelphij# ifdef FULL_TEST
189296341Sdelphij    {
190296341Sdelphij        unsigned char out_a[16], out_b[16];
191296341Sdelphij        static char *hex = "0123456789ABCDEF";
19255714Skris
193296341Sdelphij        printf("This test will take some time....");
194296341Sdelphij        fflush(stdout);
195296341Sdelphij        memcpy(out_a, in_a, sizeof(in_a));
196296341Sdelphij        memcpy(out_b, in_b, sizeof(in_b));
197296341Sdelphij        i = 1;
19855714Skris
199296341Sdelphij        for (l = 0; l < 1000000L; l++) {
200296341Sdelphij            CAST_set_key(&key_b, 16, out_b);
201296341Sdelphij            CAST_ecb_encrypt(&(out_a[0]), &(out_a[0]), &key_b, CAST_ENCRYPT);
202296341Sdelphij            CAST_ecb_encrypt(&(out_a[8]), &(out_a[8]), &key_b, CAST_ENCRYPT);
203296341Sdelphij            CAST_set_key(&key, 16, out_a);
204296341Sdelphij            CAST_ecb_encrypt(&(out_b[0]), &(out_b[0]), &key, CAST_ENCRYPT);
205296341Sdelphij            CAST_ecb_encrypt(&(out_b[8]), &(out_b[8]), &key, CAST_ENCRYPT);
206296341Sdelphij            if ((l & 0xffff) == 0xffff) {
207296341Sdelphij                printf("%c", hex[i & 0x0f]);
208296341Sdelphij                fflush(stdout);
209296341Sdelphij                i++;
210296341Sdelphij            }
211296341Sdelphij        }
21255714Skris
213296341Sdelphij        if ((memcmp(out_a, c_a, sizeof(c_a)) != 0) ||
214296341Sdelphij            (memcmp(out_b, c_b, sizeof(c_b)) != 0)) {
215296341Sdelphij            printf("\n");
216296341Sdelphij            printf("Error\n");
21755714Skris
218296341Sdelphij            printf("A out =");
219296341Sdelphij            for (i = 0; i < 16; i++)
220296341Sdelphij                printf("%02X ", out_a[i]);
221296341Sdelphij            printf("\nactual=");
222296341Sdelphij            for (i = 0; i < 16; i++)
223296341Sdelphij                printf("%02X ", c_a[i]);
224296341Sdelphij            printf("\n");
22555714Skris
226296341Sdelphij            printf("B out =");
227296341Sdelphij            for (i = 0; i < 16; i++)
228296341Sdelphij                printf("%02X ", out_b[i]);
229296341Sdelphij            printf("\nactual=");
230296341Sdelphij            for (i = 0; i < 16; i++)
231296341Sdelphij                printf("%02X ", c_b[i]);
232296341Sdelphij            printf("\n");
233296341Sdelphij        } else
234296341Sdelphij            printf(" ok\n");
235296341Sdelphij    }
236296341Sdelphij# endif
237296341Sdelphij
238109998Smarkm    EXIT(err);
239296341Sdelphij    return (err);
240296341Sdelphij}
24155714Skris#endif
242