blob: 69a9840b72ce7648c885fa5025a7adce5812ad0d [file] [log] [blame]
Paul Bakker38119b12009-01-10 23:31:23 +00001/*
2 * Camellia implementation
3 *
4 * Copyright (C) 2009 Paul Bakker
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20/*
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000021 * The Camellia block cipher was designed by NTT and Mitsubishi Electric
22 * Corporation.
Paul Bakker38119b12009-01-10 23:31:23 +000023 *
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +000024 * http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/01espec.pdf
Paul Bakker38119b12009-01-10 23:31:23 +000025 */
26
27#include "polarssl/config.h"
28
29#if defined(POLARSSL_CAMELLIA_C)
30
31#include "polarssl/camellia.h"
32
33#include <string.h>
34
35#include <stdio.h> /* TEMP */
36int verbose = 0;
37
38
39/*
40 * 32-bit integer manipulation macros (big endian)
41 */
42#ifndef GET_ULONG_BE
43#define GET_ULONG_BE(n,b,i) \
44{ \
45 (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
46 | ( (unsigned long) (b)[(i) + 1] << 16 ) \
47 | ( (unsigned long) (b)[(i) + 2] << 8 ) \
48 | ( (unsigned long) (b)[(i) + 3] ); \
49}
50#endif
51
52#ifndef PUT_ULONG_BE
53#define PUT_ULONG_BE(n,b,i) \
54{ \
55 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
56 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
57 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
58 (b)[(i) + 3] = (unsigned char) ( (n) ); \
59}
60#endif
61
62static const unsigned char SIGMA_CHARS[6][8] =
63{
64 { 0xa0, 0x9e, 0x66, 0x7f, 0x3b, 0xcc, 0x90, 0x8b },
65 { 0xb6, 0x7a, 0xe8, 0x58, 0x4c, 0xaa, 0x73, 0xb2 },
66 { 0xc6, 0xef, 0x37, 0x2f, 0xe9, 0x4f, 0x82, 0xbe },
67 { 0x54, 0xff, 0x53, 0xa5, 0xf1, 0xd3, 0x6f, 0x1c },
68 { 0x10, 0xe5, 0x27, 0xfa, 0xde, 0x68, 0x2d, 0x1d },
69 { 0xb0, 0x56, 0x88, 0xc2, 0xb3, 0xe6, 0xc1, 0xfd }
70};
71
72static const unsigned char FSb[256] =
73{
74 112,130, 44,236,179, 39,192,229,228,133, 87, 53,234, 12,174, 65,
75 35,239,107,147, 69, 25,165, 33,237, 14, 79, 78, 29,101,146,189,
76 134,184,175,143,124,235, 31,206, 62, 48,220, 95, 94,197, 11, 26,
77 166,225, 57,202,213, 71, 93, 61,217, 1, 90,214, 81, 86,108, 77,
78 139, 13,154,102,251,204,176, 45,116, 18, 43, 32,240,177,132,153,
79 223, 76,203,194, 52,126,118, 5,109,183,169, 49,209, 23, 4,215,
80 20, 88, 58, 97,222, 27, 17, 28, 50, 15,156, 22, 83, 24,242, 34,
81 254, 68,207,178,195,181,122,145, 36, 8,232,168, 96,252,105, 80,
82 170,208,160,125,161,137, 98,151, 84, 91, 30,149,224,255,100,210,
83 16,196, 0, 72,163,247,117,219,138, 3,230,218, 9, 63,221,148,
84 135, 92,131, 2,205, 74,144, 51,115,103,246,243,157,127,191,226,
85 82,155,216, 38,200, 55,198, 59,129,150,111, 75, 19,190, 99, 46,
86 233,121,167,140,159,110,188,142, 41,245,249,182, 47,253,180, 89,
87 120,152, 6,106,231, 70,113,186,212, 37,171, 66,136,162,141,250,
88 114, 7,185, 85,248,238,172, 10, 54, 73, 42,104, 60, 56,241,164,
89 64, 40,211,123,187,201, 67,193, 21,227,173,244,119,199,128,158
90};
91
92#define SBOX1(n) FSb[(n)]
93#define SBOX2(n) (unsigned char)((FSb[(n)] >> 7 ^ FSb[(n)] << 1) & 0xff)
94#define SBOX3(n) (unsigned char)((FSb[(n)] >> 1 ^ FSb[(n)] << 7) & 0xff)
95#define SBOX4(n) FSb[((n) << 1 ^ (n) >> 7) &0xff]
96
97static const unsigned char shifts[2][4][4] =
98{
99 {
100 { 1, 1, 1, 1 }, /* KL */
101 { 0, 0, 0, 0 }, /* KR */
102 { 1, 1, 1, 1 }, /* KA */
103 { 0, 0, 0, 0 } /* KB */
104 },
105 {
106 { 1, 0, 1, 1 }, /* KL */
107 { 1, 1, 0, 1 }, /* KR */
108 { 1, 1, 1, 0 }, /* KA */
109 { 1, 1, 0, 1 } /* KB */
110 }
111};
112
113static const char indexes[2][4][20] =
114{
115 {
116 { 0, 1, 2, 3, 8, 9, 10, 11, 38, 39,
117 36, 37, 23, 20, 21, 22, 27, -1, -1, 26 }, /* KL -> RK */
118 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
119 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, /* KR -> RK */
120 { 4, 5, 6, 7, 12, 13, 14, 15, 16, 17,
121 18, 19, -1, 24, 25, -1, 31, 28, 29, 30 }, /* KA -> RK */
122 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
123 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } /* KB -> RK */
124 },
125 {
126 { 0, 1, 2, 3, 61, 62, 63, 60, -1, -1,
127 -1, -1, 27, 24, 25, 26, 35, 32, 33, 34 }, /* KL -> RK */
128 { -1, -1, -1, -1, 8, 9, 10, 11, 16, 17,
129 18, 19, -1, -1, -1, -1, 39, 36, 37, 38 }, /* KR -> RK */
130 { -1, -1, -1, -1, 12, 13, 14, 15, 58, 59,
131 56, 57, 31, 28, 29, 30, -1, -1, -1, -1 }, /* KA -> RK */
132 { 4, 5, 6, 7, 65, 66, 67, 64, 20, 21,
133 22, 23, -1, -1, -1, -1, 43, 40, 41, 42 } /* KB -> RK */
134 }
135};
136
137static const char transposes[2][20] =
138{
139 {
140 21, 22, 23, 20,
141 -1, -1, -1, -1,
142 18, 19, 16, 17,
143 11, 8, 9, 10,
144 15, 12, 13, 14
145 },
146 {
147 25, 26, 27, 24,
148 29, 30, 31, 28,
149 18, 19, 16, 17,
150 -1, -1, -1, -1,
151 -1, -1, -1, -1
152 }
153};
154
155/* Shift macro for smaller than 32 bits (!) */
156#define ROTL(DEST, SRC, SHIFT) \
157{ \
158 (DEST)[0] = (SRC)[0] << (SHIFT) ^ (SRC)[1] >> (32 - (SHIFT)); \
159 (DEST)[1] = (SRC)[1] << (SHIFT) ^ (SRC)[2] >> (32 - (SHIFT)); \
160 (DEST)[2] = (SRC)[2] << (SHIFT) ^ (SRC)[3] >> (32 - (SHIFT)); \
161 (DEST)[3] = (SRC)[3] << (SHIFT) ^ (SRC)[0] >> (32 - (SHIFT)); \
162}
163
164#define FL(XL, XR, KL, KR) \
165{ \
166 (XR) = ((((XL) & (KL)) << 1) | (((XL) & (KL)) >> 31)) ^ (XR); \
167 (XL) = ((XR) | (KR)) ^ (XL); \
168}
169
170#define FLInv(YL, YR, KL, KR) \
171{ \
172 (YL) = ((YR) | (KR)) ^ (YL); \
173 (YR) = ((((YL) & (KL)) << 1) | (((YL) & (KL)) >> 31)) ^ (YR); \
174}
175
176#define SHIFT_AND_PLACE(INDEX, OFFSET) \
177{ \
178 TK[0] = KC[(OFFSET) * 4 + 0]; \
179 TK[1] = KC[(OFFSET) * 4 + 1]; \
180 TK[2] = KC[(OFFSET) * 4 + 2]; \
181 TK[3] = KC[(OFFSET) * 4 + 3]; \
182 \
183 for ( i = 1; i <= 4; i++ ) \
184 if (shifts[(INDEX)][(OFFSET)][i -1]) \
185 ROTL(TK + i * 4, TK, (15 * i) % 32); \
186 \
187 for ( i = 0; i < 20; i++ ) \
188 if (indexes[(INDEX)][(OFFSET)][i] != -1) { \
189 RK[indexes[(INDEX)][(OFFSET)][i]] = TK[ i ]; \
190 } \
191}
192
193void camellia_feistel(unsigned long x[2], unsigned long k[2], unsigned long z[2])
194{
195 unsigned char t[8];
196 if (verbose >= 2)
197 printf("FEISTEL: X: %08x%08x K: %08x%08x ", x[0], x[1], k[0], k[1]);
198
199 t[0] = SBOX1(((x[0] ^ k[0]) >> 24) & 0xFF);
200 t[1] = SBOX2(((x[0] ^ k[0]) >> 16) & 0xFF);
201 t[2] = SBOX3(((x[0] ^ k[0]) >> 8) & 0xFF);
202 t[3] = SBOX4(((x[0] ^ k[0]) ) & 0xFF);
203 t[4] = SBOX2(((x[1] ^ k[1]) >> 24) & 0xFF);
204 t[5] = SBOX3(((x[1] ^ k[1]) >> 16) & 0xFF);
205 t[6] = SBOX4(((x[1] ^ k[1]) >> 8) & 0xFF);
206 t[7] = SBOX1(((x[1] ^ k[1]) ) & 0xFF);
207
208 z[0] ^= ((t[0] ^ t[2] ^ t[3] ^ t[5] ^ t[6] ^ t[7]) << 24) |
209 ((t[0] ^ t[1] ^ t[3] ^ t[4] ^ t[6] ^ t[7]) << 16) |
210 ((t[0] ^ t[1] ^ t[2] ^ t[4] ^ t[5] ^ t[7]) << 8) |
211 ((t[1] ^ t[2] ^ t[3] ^ t[4] ^ t[5] ^ t[6]) );
212 z[1] ^= ((t[0] ^ t[1] ^ t[5] ^ t[6] ^ t[7]) << 24) |
213 ((t[1] ^ t[2] ^ t[4] ^ t[6] ^ t[7]) << 16) |
214 ((t[2] ^ t[3] ^ t[4] ^ t[5] ^ t[7]) << 8) |
215 ((t[0] ^ t[3] ^ t[4] ^ t[5] ^ t[6]) );
216
217 if (verbose >= 2)
218 printf("Z: %08x%08x\n", z[0], z[1]);
219}
220
221/*
222 * Camellia key schedule (encryption)
223 */
224void camellia_setkey_enc( camellia_context *ctx, unsigned char *key, int keysize )
225{
226 int i, idx;
227 unsigned long *RK;
228 unsigned char t[64];
229
230 RK = ctx->rk;
231
232 memset(t, 0, 64);
233 memset(RK, 0, sizeof(ctx->rk));
234
235 switch( keysize )
236 {
237 case 128: ctx->nr = 3; idx = 0; break;
238 case 192:
239 case 256: ctx->nr = 4; idx = 1; break;
240 default : return;
241 }
242
243 for( i = 0; i < keysize / 8; ++i)
244 t[i] = key[i];
245
246 if (keysize == 192) {
247 for (i = 0; i < 8; i++)
248 t[24 + i] = ~t[16 + i];
249 }
250
251 if (verbose >= 2)
252 printf("\nKey schedule (enc)\n");
253
254 /*
255 * Prepare SIGMA values
256 */
257 unsigned long SIGMA[6][2];
258 for (i = 0; i < 6; i++) {
259 GET_ULONG_BE(SIGMA[i][0], SIGMA_CHARS[i], 0);
260 GET_ULONG_BE(SIGMA[i][1], SIGMA_CHARS[i], 4);
261 }
262
263 /*
264 * Key storage in KC
265 * Order: KL, KR, KA, KB
266 */
267 unsigned long KC[16];
268 memset(KC, 0, sizeof(KC));
269
270 /* Store KL, KR */
271 for (i = 0; i < 8; i++)
272 GET_ULONG_BE(KC[i], t, i * 4);
273
274 /* Generate KA */
275 for( i = 0; i < 4; ++i)
276 KC[8 + i] = KC[i] ^ KC[4 + i];
277
278 camellia_feistel(KC + 8, SIGMA[0], KC + 10);
279 camellia_feistel(KC + 10, SIGMA[1], KC + 8);
280
281 for( i = 0; i < 4; ++i)
282 KC[8 + i] ^= KC[i];
283
284 camellia_feistel(KC + 8, SIGMA[2], KC + 10);
285 camellia_feistel(KC + 10, SIGMA[3], KC + 8);
286
287 if (keysize > 128) {
288 /* Generate KB */
289 for( i = 0; i < 4; ++i)
290 KC[12 + i] = KC[4 + i] ^ KC[8 + i];
291
292 camellia_feistel(KC + 12, SIGMA[4], KC + 14);
293 camellia_feistel(KC + 14, SIGMA[5], KC + 12);
294 }
295
296 /*
297 * Generating subkeys
298 */
299 unsigned long TK[20];
300
301 /* Manipulating KL */
302 SHIFT_AND_PLACE(idx, 0);
303
304 /* Manipulating KR */
305 if (keysize > 128) {
306 SHIFT_AND_PLACE(idx, 1);
307 }
308
309 /* Manipulating KA */
310 SHIFT_AND_PLACE(idx, 2);
311
312 /* Manipulating KB */
313 if (keysize > 128) {
314 SHIFT_AND_PLACE(idx, 3);
315 }
316
317 /* Do transpositions */
318 for ( i = 0; i < 20; i++ ) {
319 if (transposes[idx][i] != -1) {
320 RK[32 + 12 * idx + i] = RK[transposes[idx][i]];
321 }
322 }
323
324 if (verbose >= 3)
325 for (i = 0; i < 26 + 8 * idx; ++i)
326 printf("RK[%d]: %08x%08x\n", i * 2, ctx->rk[i * 2 + 0], ctx->rk[i * 2 + 1]);
327}
328
329/*
330 * Camellia key schedule (decryption)
331 */
332void camellia_setkey_dec( camellia_context *ctx, unsigned char *key, int keysize )
333{
334 int i, idx;
335 camellia_context cty;
336 unsigned long *RK;
337 unsigned long *SK;
338
339 switch( keysize )
340 {
341 case 128: ctx->nr = 3; idx = 0; break;
342 case 192:
343 case 256: ctx->nr = 4; idx = 1; break;
344 default : return;
345 }
346
347 RK = ctx->rk;
348
349 camellia_setkey_enc(&cty, key, keysize);
350
351 SK = cty.rk + 24 * 2 + 8 * idx * 2;
352
353 *RK++ = *SK++;
354 *RK++ = *SK++;
355 *RK++ = *SK++;
356 *RK++ = *SK++;
357
358 for (i = 22 + 8 * idx, SK -= 6; i > 0; i--, SK -= 4)
359 {
360 *RK++ = *SK++;
361 *RK++ = *SK++;
362 }
363
364 SK -= 2;
365
366 *RK++ = *SK++;
367 *RK++ = *SK++;
368 *RK++ = *SK++;
369 *RK++ = *SK++;
370
371 memset( &cty, 0, sizeof( camellia_context ) );
372
373 if (verbose >= 3)
374 for (i = 0; i < 26 + 8 * idx; ++i)
375 printf("RK[%d]: %08x%08x\n", i * 2, ctx->rk[i * 2 + 0], ctx->rk[i * 2 + 1]);
376
377}
378
379/*
380 * Camellia-ECB block encryption/decryption
381 */
382void camellia_crypt_ecb( camellia_context *ctx,
383 int mode,
384 unsigned char input[16],
385 unsigned char output[16] )
386{
387 int i, NR;
388 unsigned long *RK, X[4], Y[4], T;
389
390 NR = ctx->nr;
391 RK = ctx->rk;
392
393 if (verbose >= 2)
394 printf("\nCrypt\n");
395
396 GET_ULONG_BE( X[0], input, 0 );
397 GET_ULONG_BE( X[1], input, 4 );
398 GET_ULONG_BE( X[2], input, 8 );
399 GET_ULONG_BE( X[3], input, 12 );
400
401 X[0] ^= *RK++;
402 X[1] ^= *RK++;
403 X[2] ^= *RK++;
404 X[3] ^= *RK++;
405
406 while (NR) {
407 --NR;
408 camellia_feistel(X, RK, X + 2);
409 RK += 2;
410 camellia_feistel(X + 2, RK, X);
411 RK += 2;
412 camellia_feistel(X, RK, X + 2);
413 RK += 2;
414 camellia_feistel(X + 2, RK, X);
415 RK += 2;
416 camellia_feistel(X, RK, X + 2);
417 RK += 2;
418 camellia_feistel(X + 2, RK, X);
419 RK += 2;
420
421 if (NR) {
422 FL(X[0], X[1], RK[0], RK[1]);
423 RK += 2;
424 FLInv(X[2], X[3], RK[0], RK[1]);
425 RK += 2;
426 }
427 }
428
429 X[2] ^= *RK++;
430 X[3] ^= *RK++;
431 X[0] ^= *RK++;
432 X[1] ^= *RK++;
433
434 PUT_ULONG_BE( X[2], output, 0 );
435 PUT_ULONG_BE( X[3], output, 4 );
436 PUT_ULONG_BE( X[0], output, 8 );
437 PUT_ULONG_BE( X[1], output, 12 );
438}
439
440/*
441 * Camellia-CBC buffer encryption/decryption
442 */
443void camellia_crypt_cbc( camellia_context *ctx,
444 int mode,
445 int length,
446 unsigned char iv[16],
447 unsigned char *input,
448 unsigned char *output )
449{
450 int i;
451 unsigned char temp[16];
452
453 if( mode == CAMELLIA_DECRYPT )
454 {
455 while( length > 0 )
456 {
457 memcpy( temp, input, 16 );
458 camellia_crypt_ecb( ctx, mode, input, output );
459
460 for( i = 0; i < 16; i++ )
461 output[i] = (unsigned char)( output[i] ^ iv[i] );
462
463 memcpy( iv, temp, 16 );
464
465 input += 16;
466 output += 16;
467 length -= 16;
468 }
469 }
470 else
471 {
472 while( length > 0 )
473 {
474 for( i = 0; i < 16; i++ )
475 output[i] = (unsigned char)( input[i] ^ iv[i] );
476
477 camellia_crypt_ecb( ctx, mode, output, output );
478 memcpy( iv, output, 16 );
479
480 input += 16;
481 output += 16;
482 length -= 16;
483 }
484 }
485}
486
487/*
488 * Camellia-CFB128 buffer encryption/decryption
489 */
490void camellia_crypt_cfb128( camellia_context *ctx,
491 int mode,
492 int length,
493 int *iv_off,
494 unsigned char iv[16],
495 unsigned char *input,
496 unsigned char *output )
497{
498 int c, n = *iv_off;
499
500 if( mode == CAMELLIA_DECRYPT )
501 {
502 while( length-- )
503 {
504 if( n == 0 )
505 camellia_crypt_ecb( ctx, CAMELLIA_ENCRYPT, iv, iv );
506
507 c = *input++;
508 *output++ = (unsigned char)( c ^ iv[n] );
509 iv[n] = (unsigned char) c;
510
511 n = (n + 1) & 0x0F;
512 }
513 }
514 else
515 {
516 while( length-- )
517 {
518 if( n == 0 )
519 camellia_crypt_ecb( ctx, CAMELLIA_ENCRYPT, iv, iv );
520
521 iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
522
523 n = (n + 1) & 0x0F;
524 }
525 }
526
527 *iv_off = n;
528}
529
530#if defined(POLARSSL_SELF_TEST)
531
532#include <stdio.h>
533
534/*
535 * Camellia test vectors from:
536 *
537 * http://info.isl.ntt.co.jp/crypt/eng/camellia/technology.html:
538 * http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/cryptrec/intermediate.txt
539 * http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/cryptrec/t_camellia.txt
540 * (For each bitlength: Key 0, Nr 39)
541 */
542#define CAMELLIA_TESTS_ECB 2
543
544static const unsigned char camellia_test_ecb_key[3][CAMELLIA_TESTS_ECB][32] =
545{
546 {
547 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
548 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 },
549 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
550 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
551 },
552 {
553 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
554 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
555 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
556 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
557 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
558 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
559 },
560 {
561 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
562 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
563 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
564 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
565 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
566 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
567 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
568 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
569 },
570};
571
572static const unsigned char camellia_test_ecb_plain[CAMELLIA_TESTS_ECB][16] =
573{
574 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
575 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 },
576 { 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
577 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
578};
579
580static const unsigned char camellia_test_ecb_cipher[3][CAMELLIA_TESTS_ECB][16] =
581{
582 {
583 { 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
584 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43 },
585 { 0x38, 0x3C, 0x6C, 0x2A, 0xAB, 0xEF, 0x7F, 0xDE,
586 0x25, 0xCD, 0x47, 0x0B, 0xF7, 0x74, 0xA3, 0x31 }
587 },
588 {
589 { 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
590 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9 },
591 { 0xD1, 0x76, 0x3F, 0xC0, 0x19, 0xD7, 0x7C, 0xC9,
592 0x30, 0xBF, 0xF2, 0xA5, 0x6F, 0x7C, 0x93, 0x64 }
593 },
594 {
595 { 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
596 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09 },
597 { 0x05, 0x03, 0xFB, 0x10, 0xAB, 0x24, 0x1E, 0x7C,
598 0xF4, 0x5D, 0x8C, 0xDE, 0xEE, 0x47, 0x43, 0x35 }
599 }
600};
601
602#define CAMELLIA_TESTS_CBC 3
603
604static const unsigned char camellia_test_cbc_key[3][32] =
605{
606 { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
607 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
608 ,
609 { 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
610 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
611 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B }
612 ,
613 { 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
614 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
615 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
616 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4 }
617};
618
619static const unsigned char camellia_test_cbc_iv[16] =
620
621 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
622 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }
623;
624
625static const unsigned char camellia_test_cbc_plain[CAMELLIA_TESTS_CBC][16] =
626{
627 { 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
628 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A },
629 { 0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C,
630 0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51 },
631 { 0x30, 0xC8, 0x1C, 0x46, 0xA3, 0x5C, 0xE4, 0x11,
632 0xE5, 0xFB, 0xC1, 0x19, 0x1A, 0x0A, 0x52, 0xEF }
633
634};
635
636static const unsigned char camellia_test_cbc_cipher[3][CAMELLIA_TESTS_CBC][16] =
637{
638 {
639 { 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
640 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB },
641 { 0xA2, 0xF2, 0xCF, 0x67, 0x16, 0x29, 0xEF, 0x78,
642 0x40, 0xC5, 0xA5, 0xDF, 0xB5, 0x07, 0x48, 0x87 },
643 { 0x0F, 0x06, 0x16, 0x50, 0x08, 0xCF, 0x8B, 0x8B,
644 0x5A, 0x63, 0x58, 0x63, 0x62, 0x54, 0x3E, 0x54 }
645 },
646 {
647 { 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
648 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93 },
649 { 0x5D, 0x5A, 0x86, 0x9B, 0xD1, 0x4C, 0xE5, 0x42,
650 0x64, 0xF8, 0x92, 0xA6, 0xDD, 0x2E, 0xC3, 0xD5 },
651 { 0x37, 0xD3, 0x59, 0xC3, 0x34, 0x98, 0x36, 0xD8,
652 0x84, 0xE3, 0x10, 0xAD, 0xDF, 0x68, 0xC4, 0x49 }
653 },
654 {
655 { 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
656 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA },
657 { 0x36, 0xCB, 0xEB, 0x73, 0xBD, 0x50, 0x4B, 0x40,
658 0x70, 0xB1, 0xB7, 0xDE, 0x2B, 0x21, 0xEB, 0x50 },
659 { 0xE3, 0x1A, 0x60, 0x55, 0x29, 0x7D, 0x96, 0xCA,
660 0x33, 0x30, 0xCD, 0xF1, 0xB1, 0x86, 0x0A, 0x83 }
661 }
662};
663
664
665/*
666 * Checkup routine
667 */
668int camellia_self_test( int verbose )
669{
670 int i, j, u, v, offset;
671 unsigned char key[32];
672 unsigned char buf[64];
673 unsigned char prv[16];
674 unsigned char src[16];
675 unsigned char dst[16];
676 unsigned char iv[16];
677 camellia_context ctx;
678
679 memset( key, 0, 32 );
680
681 for (j = 0; j < 6; j++) {
682 u = j >> 1;
683 v = j & 1;
684
685 if( verbose != 0 )
686 printf( " CAMELLIA-ECB-%3d (%s): ", 128 + u * 64,
687 (v == CAMELLIA_DECRYPT) ? "dec" : "enc");
688
689 for (i = 0; i < CAMELLIA_TESTS_ECB; i++ ) {
690 memcpy( key, camellia_test_ecb_key[u][i], 16 + 8 * u);
691
692 if (v == CAMELLIA_DECRYPT) {
693 camellia_setkey_dec(&ctx, key, 128 + u * 64);
694 memcpy(src, camellia_test_ecb_cipher[u][i], 16);
695 memcpy(dst, camellia_test_ecb_plain[i], 16);
696 } else { /* CAMELLIA_ENCRYPT */
697 camellia_setkey_enc(&ctx, key, 128 + u * 64);
698 memcpy(src, camellia_test_ecb_plain[i], 16);
699 memcpy(dst, camellia_test_ecb_cipher[u][i], 16);
700 }
701
702 camellia_crypt_ecb(&ctx, v, src, buf);
703
704 if( memcmp( buf, dst, 16 ) != 0 )
705 {
706 if( verbose != 0 )
707 printf( "failed\n" );
708
709 return( 1 );
710 }
711 }
712
713 if( verbose != 0 )
714 printf( "passed\n" );
715 }
716
717 if( verbose != 0 )
718 printf( "\n" );
719
720 /*
721 * CBC mode
722 */
723 for( j = 0; j < 6; j++ )
724 {
725 u = j >> 1;
726 v = j & 1;
727
728 if( verbose != 0 )
729 printf( " CAMELLIA-CBC-%3d (%s): ", 128 + u * 64,
730 ( v == CAMELLIA_DECRYPT ) ? "dec" : "enc" );
731
732 memcpy( src, camellia_test_cbc_iv, 16);
733 memcpy( dst, camellia_test_cbc_iv, 16);
734 memcpy( key, camellia_test_cbc_key[u], 16 + 8 * u);
735
736 if (v == CAMELLIA_DECRYPT) {
737 camellia_setkey_dec(&ctx, key, 128 + u * 64);
738 } else {
739 camellia_setkey_enc(&ctx, key, 128 + u * 64);
740 }
741
742 for (i = 0; i < CAMELLIA_TESTS_CBC; i++ ) {
743
744 if (v == CAMELLIA_DECRYPT) {
745 memcpy( iv , src, 16 );
746 memcpy(src, camellia_test_cbc_cipher[u][i], 16);
747 memcpy(dst, camellia_test_cbc_plain[i], 16);
748 } else { /* CAMELLIA_ENCRYPT */
749 memcpy( iv , dst, 16 );
750 memcpy(src, camellia_test_cbc_plain[i], 16);
751 memcpy(dst, camellia_test_cbc_cipher[u][i], 16);
752 }
753
754 camellia_crypt_cbc(&ctx, v, 16, iv, src, buf);
755
756 if( memcmp( buf, dst, 16 ) != 0 )
757 {
758 if( verbose != 0 )
759 printf( "failed\n" );
760
761 return( 1 );
762 }
763 }
764
765 if( verbose != 0 )
766 printf( "passed\n" );
767 }
768
769 if( verbose != 0 )
770 printf( "\n" );
771
772 return ( 0 );
773
774 /*
775 * CFB128 mode
776 */
777 /*
778 for( i = 0; i < 6; i++ )
779 {
780 u = i >> 1;
781 v = i & 1;
782
783 if( verbose != 0 )
784 printf( " AES-CFB128-%3d (%s): ", 128 + u * 64,
785 ( v == AES_DECRYPT ) ? "dec" : "enc" );
786
787 memcpy( iv, aes_test_cfb128_iv, 16 );
788 memcpy( key, aes_test_cfb128_key[u], 16 + u * 8 );
789
790 offset = 0;
791 aes_setkey_enc( &ctx, key, 128 + u * 64 );
792
793 if( v == AES_DECRYPT )
794 {
795 memcpy( buf, aes_test_cfb128_ct[u], 64 );
796 aes_crypt_cfb128( &ctx, v, 64, &offset, iv, buf, buf );
797
798 if( memcmp( buf, aes_test_cfb128_pt, 64 ) != 0 )
799 {
800 if( verbose != 0 )
801 printf( "failed\n" );
802
803 return( 1 );
804 }
805 }
806 else
807 {
808 memcpy( buf, aes_test_cfb128_pt, 64 );
809 aes_crypt_cfb128( &ctx, v, 64, &offset, iv, buf, buf );
810
811 if( memcmp( buf, aes_test_cfb128_ct[u], 64 ) != 0 )
812 {
813 if( verbose != 0 )
814 printf( "failed\n" );
815
816 return( 1 );
817 }
818 }
819
820 if( verbose != 0 )
821 printf( "passed\n" );
822 }
823
824
825 if( verbose != 0 )
826 printf( "\n" );
827
828 return( 0 ); */
829}
830
831#endif
832
833#endif