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.
8280304Sjkim *
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).
15280304Sjkim *
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.
22280304Sjkim *
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 :-).
37280304Sjkim * 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)"
40280304Sjkim *
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.
52280304Sjkim *
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");
69280304Sjkim    return (0);
7061828Smarkm}
7161828Smarkm#else
72280304Sjkim# include <openssl/idea.h>
7361828Smarkm
74280304Sjkimunsigned char k[16] = {
75280304Sjkim    0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04,
76280304Sjkim    0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08
77280304Sjkim};
7861828Smarkm
79280304Sjkimunsigned char in[8] = { 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03 };
80280304Sjkimunsigned char c[8] = { 0x11, 0xFB, 0xED, 0x2B, 0x01, 0x98, 0x6D, 0xE5 };
81280304Sjkim
8261828Smarkmunsigned char out[80];
8361828Smarkm
84280304Sjkimchar *text = "Hello to all people out there";
8561828Smarkm
86280304Sjkimstatic unsigned char cfb_key[16] = {
87280304Sjkim    0xe1, 0xf0, 0xc3, 0xd2, 0xa5, 0xb4, 0x87, 0x96,
88280304Sjkim    0x69, 0x78, 0x4b, 0x5a, 0x2d, 0x3c, 0x0f, 0x1e,
89280304Sjkim};
90280304Sjkimstatic unsigned char cfb_iv[80] =
91280304Sjkim    { 0x34, 0x12, 0x78, 0x56, 0xab, 0x90, 0xef, 0xcd };
92280304Sjkimstatic unsigned char cfb_buf1[40], cfb_buf2[40], cfb_tmp[8];
93280304Sjkim# define CFB_TEST_SIZE 24
94280304Sjkimstatic unsigned char plain[CFB_TEST_SIZE] = {
95280304Sjkim    0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73,
96280304Sjkim    0x20, 0x74, 0x68, 0x65, 0x20, 0x74,
97280304Sjkim    0x69, 0x6d, 0x65, 0x20, 0x66, 0x6f,
98280304Sjkim    0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20
99280304Sjkim};
10061828Smarkm
101280304Sjkimstatic unsigned char cfb_cipher64[CFB_TEST_SIZE] = {
102280304Sjkim    0x59, 0xD8, 0xE2, 0x65, 0x00, 0x58, 0x6C, 0x3F,
103280304Sjkim    0x2C, 0x17, 0x25, 0xD0, 0x1A, 0x38, 0xB7, 0x2A,
104280304Sjkim    0x39, 0x61, 0x37, 0xDC, 0x79, 0xFB, 0x9F, 0x45
105280304Sjkim/*- 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
106280304Sjkim    0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
107280304Sjkim    0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
108280304Sjkim};
10961828Smarkm
11061828Smarkmstatic int cfb64_test(unsigned char *cfb_cipher);
11161828Smarkmstatic char *pt(unsigned char *p);
11261828Smarkmint main(int argc, char *argv[])
113280304Sjkim{
114280304Sjkim    int i, err = 0;
115280304Sjkim    IDEA_KEY_SCHEDULE key, dkey;
116280304Sjkim    unsigned char iv[8];
11761828Smarkm
118280304Sjkim    idea_set_encrypt_key(k, &key);
119280304Sjkim    idea_ecb_encrypt(in, out, &key);
120280304Sjkim    if (memcmp(out, c, 8) != 0) {
121280304Sjkim        printf("ecb idea error encrypting\n");
122280304Sjkim        printf("got     :");
123280304Sjkim        for (i = 0; i < 8; i++)
124280304Sjkim            printf("%02X ", out[i]);
125280304Sjkim        printf("\n");
126280304Sjkim        printf("expected:");
127280304Sjkim        for (i = 0; i < 8; i++)
128280304Sjkim            printf("%02X ", c[i]);
129280304Sjkim        err = 20;
130280304Sjkim        printf("\n");
131280304Sjkim    }
13261828Smarkm
133280304Sjkim    idea_set_decrypt_key(&key, &dkey);
134280304Sjkim    idea_ecb_encrypt(c, out, &dkey);
135280304Sjkim    if (memcmp(out, in, 8) != 0) {
136280304Sjkim        printf("ecb idea error decrypting\n");
137280304Sjkim        printf("got     :");
138280304Sjkim        for (i = 0; i < 8; i++)
139280304Sjkim            printf("%02X ", out[i]);
140280304Sjkim        printf("\n");
141280304Sjkim        printf("expected:");
142280304Sjkim        for (i = 0; i < 8; i++)
143280304Sjkim            printf("%02X ", in[i]);
144280304Sjkim        printf("\n");
145280304Sjkim        err = 3;
146280304Sjkim    }
14761828Smarkm
148280304Sjkim    if (err == 0)
149280304Sjkim        printf("ecb idea ok\n");
15061828Smarkm
151280304Sjkim    memcpy(iv, k, 8);
152280304Sjkim    idea_cbc_encrypt((unsigned char *)text, out, strlen(text) + 1, &key, iv,
153280304Sjkim                     1);
154280304Sjkim    memcpy(iv, k, 8);
155280304Sjkim    idea_cbc_encrypt(out, out, 8, &dkey, iv, 0);
156280304Sjkim    idea_cbc_encrypt(&(out[8]), &(out[8]), strlen(text) + 1 - 8, &dkey, iv,
157280304Sjkim                     0);
158280304Sjkim    if (memcmp(text, out, strlen(text) + 1) != 0) {
159280304Sjkim        printf("cbc idea bad\n");
160280304Sjkim        err = 4;
161280304Sjkim    } else
162280304Sjkim        printf("cbc idea ok\n");
16361828Smarkm
164280304Sjkim    printf("cfb64 idea ");
165280304Sjkim    if (cfb64_test(cfb_cipher64)) {
166280304Sjkim        printf("bad\n");
167280304Sjkim        err = 5;
168280304Sjkim    } else
169280304Sjkim        printf("ok\n");
17061828Smarkm
171280304Sjkim# ifdef OPENSSL_SYS_NETWARE
172280304Sjkim    if (err)
173280304Sjkim        printf("ERROR: %d\n", err);
174280304Sjkim# endif
175280304Sjkim    EXIT(err);
176280304Sjkim    return (err);
177280304Sjkim}
17861828Smarkm
17961828Smarkmstatic int cfb64_test(unsigned char *cfb_cipher)
180280304Sjkim{
181280304Sjkim    IDEA_KEY_SCHEDULE eks, dks;
182280304Sjkim    int err = 0, i, n;
18361828Smarkm
184280304Sjkim    idea_set_encrypt_key(cfb_key, &eks);
185280304Sjkim    idea_set_decrypt_key(&eks, &dks);
186280304Sjkim    memcpy(cfb_tmp, cfb_iv, 8);
187280304Sjkim    n = 0;
188280304Sjkim    idea_cfb64_encrypt(plain, cfb_buf1, (long)12, &eks,
189280304Sjkim                       cfb_tmp, &n, IDEA_ENCRYPT);
190280304Sjkim    idea_cfb64_encrypt(&(plain[12]), &(cfb_buf1[12]),
191280304Sjkim                       (long)CFB_TEST_SIZE - 12, &eks,
192280304Sjkim                       cfb_tmp, &n, IDEA_ENCRYPT);
193280304Sjkim    if (memcmp(cfb_cipher, cfb_buf1, CFB_TEST_SIZE) != 0) {
194280304Sjkim        err = 1;
195280304Sjkim        printf("idea_cfb64_encrypt encrypt error\n");
196280304Sjkim        for (i = 0; i < CFB_TEST_SIZE; i += 8)
197280304Sjkim            printf("%s\n", pt(&(cfb_buf1[i])));
198280304Sjkim    }
199280304Sjkim    memcpy(cfb_tmp, cfb_iv, 8);
200280304Sjkim    n = 0;
201280304Sjkim    idea_cfb64_encrypt(cfb_buf1, cfb_buf2, (long)13, &eks,
202280304Sjkim                       cfb_tmp, &n, IDEA_DECRYPT);
203280304Sjkim    idea_cfb64_encrypt(&(cfb_buf1[13]), &(cfb_buf2[13]),
204280304Sjkim                       (long)CFB_TEST_SIZE - 13, &eks,
205280304Sjkim                       cfb_tmp, &n, IDEA_DECRYPT);
206280304Sjkim    if (memcmp(plain, cfb_buf2, CFB_TEST_SIZE) != 0) {
207280304Sjkim        err = 1;
208280304Sjkim        printf("idea_cfb_encrypt decrypt error\n");
209280304Sjkim        for (i = 0; i < 24; i += 8)
210280304Sjkim            printf("%s\n", pt(&(cfb_buf2[i])));
211280304Sjkim    }
212280304Sjkim    return (err);
213280304Sjkim}
21461828Smarkm
21561828Smarkmstatic char *pt(unsigned char *p)
216280304Sjkim{
217280304Sjkim    static char bufs[10][20];
218280304Sjkim    static int bnum = 0;
219280304Sjkim    char *ret;
220280304Sjkim    int i;
221280304Sjkim    static char *f = "0123456789ABCDEF";
22261828Smarkm
223280304Sjkim    ret = &(bufs[bnum++][0]);
224280304Sjkim    bnum %= 10;
225280304Sjkim    for (i = 0; i < 8; i++) {
226280304Sjkim        ret[i * 2] = f[(p[i] >> 4) & 0xf];
227280304Sjkim        ret[i * 2 + 1] = f[p[i] & 0xf];
228280304Sjkim    }
229280304Sjkim    ret[16] = '\0';
230280304Sjkim    return (ret);
231280304Sjkim}
23261828Smarkm#endif
233