xref: /minix3/crypto/external/bsd/openssl/dist/crypto/idea/ideatest.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* crypto/idea/ideatest.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc #include <stdio.h>
60ebfedea0SLionel Sambuc #include <string.h>
61ebfedea0SLionel Sambuc #include <stdlib.h>
62ebfedea0SLionel Sambuc 
63ebfedea0SLionel Sambuc #include "../e_os.h"
64ebfedea0SLionel Sambuc 
65ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_IDEA
main(int argc,char * argv[])66ebfedea0SLionel Sambuc int main(int argc, char *argv[])
67ebfedea0SLionel Sambuc {
68ebfedea0SLionel Sambuc     printf("No IDEA support\n");
69ebfedea0SLionel Sambuc     return (0);
70ebfedea0SLionel Sambuc }
71ebfedea0SLionel Sambuc #else
72ebfedea0SLionel Sambuc # include <openssl/idea.h>
73ebfedea0SLionel Sambuc 
74ebfedea0SLionel Sambuc unsigned char k[16] = {
75ebfedea0SLionel Sambuc     0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04,
76*0a6a1f1dSLionel Sambuc     0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08
77*0a6a1f1dSLionel Sambuc };
78ebfedea0SLionel Sambuc 
79ebfedea0SLionel Sambuc unsigned char in[8] = { 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03 };
80ebfedea0SLionel Sambuc unsigned char c[8] = { 0x11, 0xFB, 0xED, 0x2B, 0x01, 0x98, 0x6D, 0xE5 };
81*0a6a1f1dSLionel Sambuc 
82ebfedea0SLionel Sambuc unsigned char out[80];
83ebfedea0SLionel Sambuc 
84ebfedea0SLionel Sambuc const char *text = "Hello to all people out there";
85ebfedea0SLionel Sambuc 
86ebfedea0SLionel Sambuc static unsigned char cfb_key[16] = {
87ebfedea0SLionel Sambuc     0xe1, 0xf0, 0xc3, 0xd2, 0xa5, 0xb4, 0x87, 0x96,
88ebfedea0SLionel Sambuc     0x69, 0x78, 0x4b, 0x5a, 0x2d, 0x3c, 0x0f, 0x1e,
89ebfedea0SLionel Sambuc };
90*0a6a1f1dSLionel Sambuc static unsigned char cfb_iv[80] =
91*0a6a1f1dSLionel Sambuc     { 0x34, 0x12, 0x78, 0x56, 0xab, 0x90, 0xef, 0xcd };
92ebfedea0SLionel Sambuc static unsigned char cfb_buf1[40], cfb_buf2[40], cfb_tmp[8];
93ebfedea0SLionel Sambuc # define CFB_TEST_SIZE 24
94*0a6a1f1dSLionel Sambuc static unsigned char plain[CFB_TEST_SIZE] = {
95ebfedea0SLionel Sambuc     0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73,
96ebfedea0SLionel Sambuc     0x20, 0x74, 0x68, 0x65, 0x20, 0x74,
97ebfedea0SLionel Sambuc     0x69, 0x6d, 0x65, 0x20, 0x66, 0x6f,
98ebfedea0SLionel Sambuc     0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20
99ebfedea0SLionel Sambuc };
100*0a6a1f1dSLionel Sambuc 
101ebfedea0SLionel Sambuc static unsigned char cfb_cipher64[CFB_TEST_SIZE] = {
102ebfedea0SLionel Sambuc     0x59, 0xD8, 0xE2, 0x65, 0x00, 0x58, 0x6C, 0x3F,
103ebfedea0SLionel Sambuc     0x2C, 0x17, 0x25, 0xD0, 0x1A, 0x38, 0xB7, 0x2A,
104ebfedea0SLionel Sambuc     0x39, 0x61, 0x37, 0xDC, 0x79, 0xFB, 0x9F, 0x45
105*0a6a1f1dSLionel Sambuc /*- 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
106ebfedea0SLionel Sambuc     0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
107ebfedea0SLionel Sambuc     0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
108ebfedea0SLionel Sambuc };
109ebfedea0SLionel Sambuc 
110ebfedea0SLionel Sambuc static int cfb64_test(unsigned char *cfb_cipher);
111ebfedea0SLionel Sambuc static char *pt(unsigned char *p);
main(int argc,char * argv[])112ebfedea0SLionel Sambuc int main(int argc, char *argv[])
113ebfedea0SLionel Sambuc {
114ebfedea0SLionel Sambuc     int i, err = 0;
115ebfedea0SLionel Sambuc     IDEA_KEY_SCHEDULE key, dkey;
116ebfedea0SLionel Sambuc     unsigned char iv[8];
117ebfedea0SLionel Sambuc 
118ebfedea0SLionel Sambuc     idea_set_encrypt_key(k, &key);
119ebfedea0SLionel Sambuc     idea_ecb_encrypt(in, out, &key);
120*0a6a1f1dSLionel Sambuc     if (memcmp(out, c, 8) != 0) {
121ebfedea0SLionel Sambuc         printf("ecb idea error encrypting\n");
122ebfedea0SLionel Sambuc         printf("got     :");
123ebfedea0SLionel Sambuc         for (i = 0; i < 8; i++)
124ebfedea0SLionel Sambuc             printf("%02X ", out[i]);
125ebfedea0SLionel Sambuc         printf("\n");
126ebfedea0SLionel Sambuc         printf("expected:");
127ebfedea0SLionel Sambuc         for (i = 0; i < 8; i++)
128ebfedea0SLionel Sambuc             printf("%02X ", c[i]);
129ebfedea0SLionel Sambuc         err = 20;
130ebfedea0SLionel Sambuc         printf("\n");
131ebfedea0SLionel Sambuc     }
132ebfedea0SLionel Sambuc 
133ebfedea0SLionel Sambuc     idea_set_decrypt_key(&key, &dkey);
134ebfedea0SLionel Sambuc     idea_ecb_encrypt(c, out, &dkey);
135*0a6a1f1dSLionel Sambuc     if (memcmp(out, in, 8) != 0) {
136ebfedea0SLionel Sambuc         printf("ecb idea error decrypting\n");
137ebfedea0SLionel Sambuc         printf("got     :");
138ebfedea0SLionel Sambuc         for (i = 0; i < 8; i++)
139ebfedea0SLionel Sambuc             printf("%02X ", out[i]);
140ebfedea0SLionel Sambuc         printf("\n");
141ebfedea0SLionel Sambuc         printf("expected:");
142ebfedea0SLionel Sambuc         for (i = 0; i < 8; i++)
143ebfedea0SLionel Sambuc             printf("%02X ", in[i]);
144ebfedea0SLionel Sambuc         printf("\n");
145ebfedea0SLionel Sambuc         err = 3;
146ebfedea0SLionel Sambuc     }
147ebfedea0SLionel Sambuc 
148*0a6a1f1dSLionel Sambuc     if (err == 0)
149*0a6a1f1dSLionel Sambuc         printf("ecb idea ok\n");
150ebfedea0SLionel Sambuc 
151ebfedea0SLionel Sambuc     memcpy(iv, k, 8);
152*0a6a1f1dSLionel Sambuc     idea_cbc_encrypt((const unsigned char *)text, out, strlen(text) + 1, &key, iv,
153*0a6a1f1dSLionel Sambuc                      1);
154ebfedea0SLionel Sambuc     memcpy(iv, k, 8);
155ebfedea0SLionel Sambuc     idea_cbc_encrypt(out, out, 8, &dkey, iv, 0);
156*0a6a1f1dSLionel Sambuc     idea_cbc_encrypt(&(out[8]), &(out[8]), strlen(text) + 1 - 8, &dkey, iv,
157*0a6a1f1dSLionel Sambuc                      0);
158*0a6a1f1dSLionel Sambuc     if (memcmp(text, out, strlen(text) + 1) != 0) {
159ebfedea0SLionel Sambuc         printf("cbc idea bad\n");
160ebfedea0SLionel Sambuc         err = 4;
161*0a6a1f1dSLionel Sambuc     } else
162ebfedea0SLionel Sambuc         printf("cbc idea ok\n");
163ebfedea0SLionel Sambuc 
164ebfedea0SLionel Sambuc     printf("cfb64 idea ");
165*0a6a1f1dSLionel Sambuc     if (cfb64_test(cfb_cipher64)) {
166ebfedea0SLionel Sambuc         printf("bad\n");
167ebfedea0SLionel Sambuc         err = 5;
168*0a6a1f1dSLionel Sambuc     } else
169ebfedea0SLionel Sambuc         printf("ok\n");
170ebfedea0SLionel Sambuc 
171ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_NETWARE
172*0a6a1f1dSLionel Sambuc     if (err)
173*0a6a1f1dSLionel Sambuc         printf("ERROR: %d\n", err);
174ebfedea0SLionel Sambuc # endif
175ebfedea0SLionel Sambuc     EXIT(err);
176ebfedea0SLionel Sambuc     return (err);
177ebfedea0SLionel Sambuc }
178ebfedea0SLionel Sambuc 
cfb64_test(unsigned char * cfb_cipher)179ebfedea0SLionel Sambuc static int cfb64_test(unsigned char *cfb_cipher)
180ebfedea0SLionel Sambuc {
181ebfedea0SLionel Sambuc     IDEA_KEY_SCHEDULE eks, dks;
182ebfedea0SLionel Sambuc     int err = 0, i, n;
183ebfedea0SLionel Sambuc 
184ebfedea0SLionel Sambuc     idea_set_encrypt_key(cfb_key, &eks);
185ebfedea0SLionel Sambuc     idea_set_decrypt_key(&eks, &dks);
186ebfedea0SLionel Sambuc     memcpy(cfb_tmp, cfb_iv, 8);
187ebfedea0SLionel Sambuc     n = 0;
188ebfedea0SLionel Sambuc     idea_cfb64_encrypt(plain, cfb_buf1, (long)12, &eks,
189ebfedea0SLionel Sambuc                        cfb_tmp, &n, IDEA_ENCRYPT);
190ebfedea0SLionel Sambuc     idea_cfb64_encrypt(&(plain[12]), &(cfb_buf1[12]),
191ebfedea0SLionel Sambuc                        (long)CFB_TEST_SIZE - 12, &eks,
192ebfedea0SLionel Sambuc                        cfb_tmp, &n, IDEA_ENCRYPT);
193*0a6a1f1dSLionel Sambuc     if (memcmp(cfb_cipher, cfb_buf1, CFB_TEST_SIZE) != 0) {
194ebfedea0SLionel Sambuc         err = 1;
195ebfedea0SLionel Sambuc         printf("idea_cfb64_encrypt encrypt error\n");
196ebfedea0SLionel Sambuc         for (i = 0; i < CFB_TEST_SIZE; i += 8)
197ebfedea0SLionel Sambuc             printf("%s\n", pt(&(cfb_buf1[i])));
198ebfedea0SLionel Sambuc     }
199ebfedea0SLionel Sambuc     memcpy(cfb_tmp, cfb_iv, 8);
200ebfedea0SLionel Sambuc     n = 0;
201*0a6a1f1dSLionel Sambuc     idea_cfb64_encrypt(cfb_buf1, cfb_buf2, (long)13, &eks,
202ebfedea0SLionel Sambuc                        cfb_tmp, &n, IDEA_DECRYPT);
203*0a6a1f1dSLionel Sambuc     idea_cfb64_encrypt(&(cfb_buf1[13]), &(cfb_buf2[13]),
204*0a6a1f1dSLionel Sambuc                        (long)CFB_TEST_SIZE - 13, &eks,
205ebfedea0SLionel Sambuc                        cfb_tmp, &n, IDEA_DECRYPT);
206*0a6a1f1dSLionel Sambuc     if (memcmp(plain, cfb_buf2, CFB_TEST_SIZE) != 0) {
207ebfedea0SLionel Sambuc         err = 1;
208ebfedea0SLionel Sambuc         printf("idea_cfb_encrypt decrypt error\n");
209ebfedea0SLionel Sambuc         for (i = 0; i < 24; i += 8)
210ebfedea0SLionel Sambuc             printf("%s\n", pt(&(cfb_buf2[i])));
211ebfedea0SLionel Sambuc     }
212ebfedea0SLionel Sambuc     return (err);
213ebfedea0SLionel Sambuc }
214ebfedea0SLionel Sambuc 
pt(unsigned char * p)215ebfedea0SLionel Sambuc static char *pt(unsigned char *p)
216ebfedea0SLionel Sambuc {
217ebfedea0SLionel Sambuc     static char bufs[10][20];
218ebfedea0SLionel Sambuc     static int bnum = 0;
219ebfedea0SLionel Sambuc     char *ret;
220ebfedea0SLionel Sambuc     int i;
221ebfedea0SLionel Sambuc     static const char *f = "0123456789ABCDEF";
222ebfedea0SLionel Sambuc 
223ebfedea0SLionel Sambuc     ret = &(bufs[bnum++][0]);
224ebfedea0SLionel Sambuc     bnum %= 10;
225*0a6a1f1dSLionel Sambuc     for (i = 0; i < 8; i++) {
226ebfedea0SLionel Sambuc         ret[i * 2] = f[(p[i] >> 4) & 0xf];
227ebfedea0SLionel Sambuc         ret[i * 2 + 1] = f[p[i] & 0xf];
228ebfedea0SLionel Sambuc     }
229ebfedea0SLionel Sambuc     ret[16] = '\0';
230ebfedea0SLionel Sambuc     return (ret);
231ebfedea0SLionel Sambuc }
232ebfedea0SLionel Sambuc #endif
233