aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2007-11-06 09:01:20 -0500
committerHerbert Xu <herbert@gondor.apana.org.au>2008-01-10 16:16:06 -0500
commit1721a81256ed7f7bfdd1a721f3a6c9c85efeac53 (patch)
tree077c9b6469a106e9077fdf0d0d5a48377c19b859
parente2b21b5002a2bf21ca73c7448309a7288a984ddf (diff)
[CRYPTO] camellia: Code cleanup
Move code blocks around so that related pieces are closer together: e.g. CAMELLIA_ROUNDSM macro does not need to be separated from the rest of the code by huge array of constants. Remove unused macros (COPY4WORD, SWAP4WORD, XOR4WORD[2]) Drop SUBL(), SUBR() macros which only obscure things. Same for CAMELLIA_SP1110() macro and KEY_TABLE_TYPE typedef. Remove useless comments: /* encryption */ -- well it's obvious enough already! void camellia_encrypt128(...) Combine swap with copying at the beginning/end of encrypt/decrypt. Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com> Acked-by: Noriaki TAKAMIYA <takamiya@po.ntts.co.jp> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--crypto/camellia.c966
1 files changed, 426 insertions, 540 deletions
diff --git a/crypto/camellia.c b/crypto/camellia.c
index 6877ecfd90bb..aaae60e8bf25 100644
--- a/crypto/camellia.c
+++ b/crypto/camellia.c
@@ -36,176 +36,6 @@
36#include <linux/kernel.h> 36#include <linux/kernel.h>
37#include <linux/module.h> 37#include <linux/module.h>
38 38
39
40#define CAMELLIA_MIN_KEY_SIZE 16
41#define CAMELLIA_MAX_KEY_SIZE 32
42#define CAMELLIA_BLOCK_SIZE 16
43#define CAMELLIA_TABLE_BYTE_LEN 272
44#define CAMELLIA_TABLE_WORD_LEN (CAMELLIA_TABLE_BYTE_LEN / 4)
45
46typedef u32 KEY_TABLE_TYPE[CAMELLIA_TABLE_WORD_LEN];
47
48
49/* key constants */
50
51#define CAMELLIA_SIGMA1L (0xA09E667FL)
52#define CAMELLIA_SIGMA1R (0x3BCC908BL)
53#define CAMELLIA_SIGMA2L (0xB67AE858L)
54#define CAMELLIA_SIGMA2R (0x4CAA73B2L)
55#define CAMELLIA_SIGMA3L (0xC6EF372FL)
56#define CAMELLIA_SIGMA3R (0xE94F82BEL)
57#define CAMELLIA_SIGMA4L (0x54FF53A5L)
58#define CAMELLIA_SIGMA4R (0xF1D36F1CL)
59#define CAMELLIA_SIGMA5L (0x10E527FAL)
60#define CAMELLIA_SIGMA5R (0xDE682D1DL)
61#define CAMELLIA_SIGMA6L (0xB05688C2L)
62#define CAMELLIA_SIGMA6R (0xB3E6C1FDL)
63
64struct camellia_ctx {
65 int key_length;
66 KEY_TABLE_TYPE key_table;
67};
68
69
70/*
71 * macros
72 */
73
74
75# define GETU32(pt) (((u32)(pt)[0] << 24) \
76 ^ ((u32)(pt)[1] << 16) \
77 ^ ((u32)(pt)[2] << 8) \
78 ^ ((u32)(pt)[3]))
79
80#define COPY4WORD(dst, src) \
81 do { \
82 (dst)[0]=(src)[0]; \
83 (dst)[1]=(src)[1]; \
84 (dst)[2]=(src)[2]; \
85 (dst)[3]=(src)[3]; \
86 }while(0)
87
88#define SWAP4WORD(word) \
89 do { \
90 CAMELLIA_SWAP4((word)[0]); \
91 CAMELLIA_SWAP4((word)[1]); \
92 CAMELLIA_SWAP4((word)[2]); \
93 CAMELLIA_SWAP4((word)[3]); \
94 }while(0)
95
96#define XOR4WORD(a, b)/* a = a ^ b */ \
97 do { \
98 (a)[0]^=(b)[0]; \
99 (a)[1]^=(b)[1]; \
100 (a)[2]^=(b)[2]; \
101 (a)[3]^=(b)[3]; \
102 }while(0)
103
104#define XOR4WORD2(a, b, c)/* a = b ^ c */ \
105 do { \
106 (a)[0]=(b)[0]^(c)[0]; \
107 (a)[1]=(b)[1]^(c)[1]; \
108 (a)[2]=(b)[2]^(c)[2]; \
109 (a)[3]=(b)[3]^(c)[3]; \
110 }while(0)
111
112#define CAMELLIA_SUBKEY_L(INDEX) (subkey[(INDEX)*2])
113#define CAMELLIA_SUBKEY_R(INDEX) (subkey[(INDEX)*2 + 1])
114
115/* rotation right shift 1byte */
116#define CAMELLIA_RR8(x) (((x) >> 8) + ((x) << 24))
117/* rotation left shift 1bit */
118#define CAMELLIA_RL1(x) (((x) << 1) + ((x) >> 31))
119/* rotation left shift 1byte */
120#define CAMELLIA_RL8(x) (((x) << 8) + ((x) >> 24))
121
122#define CAMELLIA_ROLDQ(ll, lr, rl, rr, w0, w1, bits) \
123 do { \
124 w0 = ll; \
125 ll = (ll << bits) + (lr >> (32 - bits)); \
126 lr = (lr << bits) + (rl >> (32 - bits)); \
127 rl = (rl << bits) + (rr >> (32 - bits)); \
128 rr = (rr << bits) + (w0 >> (32 - bits)); \
129 } while(0)
130
131#define CAMELLIA_ROLDQo32(ll, lr, rl, rr, w0, w1, bits) \
132 do { \
133 w0 = ll; \
134 w1 = lr; \
135 ll = (lr << (bits - 32)) + (rl >> (64 - bits)); \
136 lr = (rl << (bits - 32)) + (rr >> (64 - bits)); \
137 rl = (rr << (bits - 32)) + (w0 >> (64 - bits)); \
138 rr = (w0 << (bits - 32)) + (w1 >> (64 - bits)); \
139 } while(0)
140
141#define CAMELLIA_SP1110(INDEX) (camellia_sp1110[(INDEX)])
142#define CAMELLIA_SP0222(INDEX) (camellia_sp0222[(INDEX)])
143#define CAMELLIA_SP3033(INDEX) (camellia_sp3033[(INDEX)])
144#define CAMELLIA_SP4404(INDEX) (camellia_sp4404[(INDEX)])
145
146#define CAMELLIA_F(xl, xr, kl, kr, yl, yr, il, ir, t0, t1) \
147 do { \
148 il = xl ^ kl; \
149 ir = xr ^ kr; \
150 t0 = il >> 16; \
151 t1 = ir >> 16; \
152 yl = CAMELLIA_SP1110(ir & 0xff) \
153 ^ CAMELLIA_SP0222((t1 >> 8) & 0xff) \
154 ^ CAMELLIA_SP3033(t1 & 0xff) \
155 ^ CAMELLIA_SP4404((ir >> 8) & 0xff); \
156 yr = CAMELLIA_SP1110((t0 >> 8) & 0xff) \
157 ^ CAMELLIA_SP0222(t0 & 0xff) \
158 ^ CAMELLIA_SP3033((il >> 8) & 0xff) \
159 ^ CAMELLIA_SP4404(il & 0xff); \
160 yl ^= yr; \
161 yr = CAMELLIA_RR8(yr); \
162 yr ^= yl; \
163 } while(0)
164
165
166/*
167 * for speed up
168 *
169 */
170#define CAMELLIA_FLS(ll, lr, rl, rr, kll, klr, krl, krr, t0, t1, t2, t3) \
171 do { \
172 t0 = kll; \
173 t2 = krr; \
174 t0 &= ll; \
175 t2 |= rr; \
176 rl ^= t2; \
177 lr ^= CAMELLIA_RL1(t0); \
178 t3 = krl; \
179 t1 = klr; \
180 t3 &= rl; \
181 t1 |= lr; \
182 ll ^= t1; \
183 rr ^= CAMELLIA_RL1(t3); \
184 } while(0)
185
186#define CAMELLIA_ROUNDSM(xl, xr, kl, kr, yl, yr, il, ir, t0, t1) \
187 do { \
188 ir = CAMELLIA_SP1110(xr & 0xff); \
189 il = CAMELLIA_SP1110((xl>>24) & 0xff); \
190 ir ^= CAMELLIA_SP0222((xr>>24) & 0xff); \
191 il ^= CAMELLIA_SP0222((xl>>16) & 0xff); \
192 ir ^= CAMELLIA_SP3033((xr>>16) & 0xff); \
193 il ^= CAMELLIA_SP3033((xl>>8) & 0xff); \
194 ir ^= CAMELLIA_SP4404((xr>>8) & 0xff); \
195 il ^= CAMELLIA_SP4404(xl & 0xff); \
196 il ^= kl; \
197 ir ^= il ^ kr; \
198 yl ^= ir; \
199 yr ^= CAMELLIA_RR8(il) ^ ir; \
200 } while(0)
201
202/**
203 * Stuff related to the Camellia key schedule
204 */
205#define SUBL(x) subL[(x)]
206#define SUBR(x) subR[(x)]
207
208
209static const u32 camellia_sp1110[256] = { 39static const u32 camellia_sp1110[256] = {
210 0x70707000,0x82828200,0x2c2c2c00,0xececec00, 40 0x70707000,0x82828200,0x2c2c2c00,0xececec00,
211 0xb3b3b300,0x27272700,0xc0c0c000,0xe5e5e500, 41 0xb3b3b300,0x27272700,0xc0c0c000,0xe5e5e500,
@@ -475,6 +305,122 @@ static const u32 camellia_sp4404[256] = {
475}; 305};
476 306
477 307
308#define CAMELLIA_MIN_KEY_SIZE 16
309#define CAMELLIA_MAX_KEY_SIZE 32
310#define CAMELLIA_BLOCK_SIZE 16
311#define CAMELLIA_TABLE_BYTE_LEN 272
312
313
314/* key constants */
315
316#define CAMELLIA_SIGMA1L (0xA09E667FL)
317#define CAMELLIA_SIGMA1R (0x3BCC908BL)
318#define CAMELLIA_SIGMA2L (0xB67AE858L)
319#define CAMELLIA_SIGMA2R (0x4CAA73B2L)
320#define CAMELLIA_SIGMA3L (0xC6EF372FL)
321#define CAMELLIA_SIGMA3R (0xE94F82BEL)
322#define CAMELLIA_SIGMA4L (0x54FF53A5L)
323#define CAMELLIA_SIGMA4R (0xF1D36F1CL)
324#define CAMELLIA_SIGMA5L (0x10E527FAL)
325#define CAMELLIA_SIGMA5R (0xDE682D1DL)
326#define CAMELLIA_SIGMA6L (0xB05688C2L)
327#define CAMELLIA_SIGMA6R (0xB3E6C1FDL)
328
329/*
330 * macros
331 */
332
333# define GETU32(pt) (((u32)(pt)[0] << 24) \
334 ^ ((u32)(pt)[1] << 16) \
335 ^ ((u32)(pt)[2] << 8) \
336 ^ ((u32)(pt)[3]))
337
338/* rotation right shift 1byte */
339#define CAMELLIA_RR8(x) (((x) >> 8) + ((x) << 24))
340/* rotation left shift 1bit */
341#define CAMELLIA_RL1(x) (((x) << 1) + ((x) >> 31))
342/* rotation left shift 1byte */
343#define CAMELLIA_RL8(x) (((x) << 8) + ((x) >> 24))
344
345#define CAMELLIA_ROLDQ(ll, lr, rl, rr, w0, w1, bits) \
346 do { \
347 w0 = ll; \
348 ll = (ll << bits) + (lr >> (32 - bits)); \
349 lr = (lr << bits) + (rl >> (32 - bits)); \
350 rl = (rl << bits) + (rr >> (32 - bits)); \
351 rr = (rr << bits) + (w0 >> (32 - bits)); \
352 } while(0)
353
354#define CAMELLIA_ROLDQo32(ll, lr, rl, rr, w0, w1, bits) \
355 do { \
356 w0 = ll; \
357 w1 = lr; \
358 ll = (lr << (bits - 32)) + (rl >> (64 - bits)); \
359 lr = (rl << (bits - 32)) + (rr >> (64 - bits)); \
360 rl = (rr << (bits - 32)) + (w0 >> (64 - bits)); \
361 rr = (w0 << (bits - 32)) + (w1 >> (64 - bits)); \
362 } while(0)
363
364
365#define CAMELLIA_F(xl, xr, kl, kr, yl, yr, il, ir, t0, t1) \
366 do { \
367 il = xl ^ kl; \
368 ir = xr ^ kr; \
369 t0 = il >> 16; \
370 t1 = ir >> 16; \
371 yl = camellia_sp1110[ir & 0xff] \
372 ^ camellia_sp0222[(t1 >> 8) & 0xff] \
373 ^ camellia_sp3033[t1 & 0xff] \
374 ^ camellia_sp4404[(ir >> 8) & 0xff]; \
375 yr = camellia_sp1110[(t0 >> 8) & 0xff] \
376 ^ camellia_sp0222[t0 & 0xff] \
377 ^ camellia_sp3033[(il >> 8) & 0xff] \
378 ^ camellia_sp4404[il & 0xff]; \
379 yl ^= yr; \
380 yr = CAMELLIA_RR8(yr); \
381 yr ^= yl; \
382 } while(0)
383
384
385/*
386 * for speed up
387 *
388 */
389#define CAMELLIA_FLS(ll, lr, rl, rr, kll, klr, krl, krr, t0, t1, t2, t3) \
390 do { \
391 t0 = kll; \
392 t2 = krr; \
393 t0 &= ll; \
394 t2 |= rr; \
395 rl ^= t2; \
396 lr ^= CAMELLIA_RL1(t0); \
397 t3 = krl; \
398 t1 = klr; \
399 t3 &= rl; \
400 t1 |= lr; \
401 ll ^= t1; \
402 rr ^= CAMELLIA_RL1(t3); \
403 } while(0)
404
405#define CAMELLIA_ROUNDSM(xl, xr, kl, kr, yl, yr, il, ir, t0, t1) \
406 do { \
407 ir = camellia_sp1110[xr & 0xff]; \
408 il = camellia_sp1110[(xl>>24) & 0xff]; \
409 ir ^= camellia_sp0222[(xr>>24) & 0xff]; \
410 il ^= camellia_sp0222[(xl>>16) & 0xff]; \
411 ir ^= camellia_sp3033[(xr>>16) & 0xff]; \
412 il ^= camellia_sp3033[(xl>>8) & 0xff]; \
413 ir ^= camellia_sp4404[(xr>>8) & 0xff]; \
414 il ^= camellia_sp4404[xl & 0xff]; \
415 il ^= kl; \
416 ir ^= il ^ kr; \
417 yl ^= ir; \
418 yr ^= CAMELLIA_RR8(il) ^ ir; \
419 } while(0)
420
421
422#define CAMELLIA_SUBKEY_L(INDEX) (subkey[(INDEX)*2])
423#define CAMELLIA_SUBKEY_R(INDEX) (subkey[(INDEX)*2 + 1])
478 424
479static void camellia_setup128(const unsigned char *key, u32 *subkey) 425static void camellia_setup128(const unsigned char *key, u32 *subkey)
480{ 426{
@@ -495,47 +441,47 @@ static void camellia_setup128(const unsigned char *key, u32 *subkey)
495 * generate KL dependent subkeys 441 * generate KL dependent subkeys
496 */ 442 */
497 /* kw1 */ 443 /* kw1 */
498 SUBL(0) = kll; SUBR(0) = klr; 444 subL[0] = kll; subR[0] = klr;
499 /* kw2 */ 445 /* kw2 */
500 SUBL(1) = krl; SUBR(1) = krr; 446 subL[1] = krl; subR[1] = krr;
501 /* rotation left shift 15bit */ 447 /* rotation left shift 15bit */
502 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); 448 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
503 /* k3 */ 449 /* k3 */
504 SUBL(4) = kll; SUBR(4) = klr; 450 subL[4] = kll; subR[4] = klr;
505 /* k4 */ 451 /* k4 */
506 SUBL(5) = krl; SUBR(5) = krr; 452 subL[5] = krl; subR[5] = krr;
507 /* rotation left shift 15+30bit */ 453 /* rotation left shift 15+30bit */
508 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 30); 454 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 30);
509 /* k7 */ 455 /* k7 */
510 SUBL(10) = kll; SUBR(10) = klr; 456 subL[10] = kll; subR[10] = klr;
511 /* k8 */ 457 /* k8 */
512 SUBL(11) = krl; SUBR(11) = krr; 458 subL[11] = krl; subR[11] = krr;
513 /* rotation left shift 15+30+15bit */ 459 /* rotation left shift 15+30+15bit */
514 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); 460 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
515 /* k10 */ 461 /* k10 */
516 SUBL(13) = krl; SUBR(13) = krr; 462 subL[13] = krl; subR[13] = krr;
517 /* rotation left shift 15+30+15+17 bit */ 463 /* rotation left shift 15+30+15+17 bit */
518 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17); 464 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
519 /* kl3 */ 465 /* kl3 */
520 SUBL(16) = kll; SUBR(16) = klr; 466 subL[16] = kll; subR[16] = klr;
521 /* kl4 */ 467 /* kl4 */
522 SUBL(17) = krl; SUBR(17) = krr; 468 subL[17] = krl; subR[17] = krr;
523 /* rotation left shift 15+30+15+17+17 bit */ 469 /* rotation left shift 15+30+15+17+17 bit */
524 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17); 470 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
525 /* k13 */ 471 /* k13 */
526 SUBL(18) = kll; SUBR(18) = klr; 472 subL[18] = kll; subR[18] = klr;
527 /* k14 */ 473 /* k14 */
528 SUBL(19) = krl; SUBR(19) = krr; 474 subL[19] = krl; subR[19] = krr;
529 /* rotation left shift 15+30+15+17+17+17 bit */ 475 /* rotation left shift 15+30+15+17+17+17 bit */
530 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17); 476 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
531 /* k17 */ 477 /* k17 */
532 SUBL(22) = kll; SUBR(22) = klr; 478 subL[22] = kll; subR[22] = klr;
533 /* k18 */ 479 /* k18 */
534 SUBL(23) = krl; SUBR(23) = krr; 480 subL[23] = krl; subR[23] = krr;
535 481
536 /* generate KA */ 482 /* generate KA */
537 kll = SUBL(0); klr = SUBR(0); 483 kll = subL[0]; klr = subR[0];
538 krl = SUBL(1); krr = SUBR(1); 484 krl = subL[1]; krr = subR[1];
539 CAMELLIA_F(kll, klr, 485 CAMELLIA_F(kll, klr,
540 CAMELLIA_SIGMA1L, CAMELLIA_SIGMA1R, 486 CAMELLIA_SIGMA1L, CAMELLIA_SIGMA1R,
541 w0, w1, il, ir, t0, t1); 487 w0, w1, il, ir, t0, t1);
@@ -555,152 +501,150 @@ static void camellia_setup128(const unsigned char *key, u32 *subkey)
555 501
556 /* generate KA dependent subkeys */ 502 /* generate KA dependent subkeys */
557 /* k1, k2 */ 503 /* k1, k2 */
558 SUBL(2) = kll; SUBR(2) = klr; 504 subL[2] = kll; subR[2] = klr;
559 SUBL(3) = krl; SUBR(3) = krr; 505 subL[3] = krl; subR[3] = krr;
560 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); 506 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
561 /* k5,k6 */ 507 /* k5,k6 */
562 SUBL(6) = kll; SUBR(6) = klr; 508 subL[6] = kll; subR[6] = klr;
563 SUBL(7) = krl; SUBR(7) = krr; 509 subL[7] = krl; subR[7] = krr;
564 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); 510 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
565 /* kl1, kl2 */ 511 /* kl1, kl2 */
566 SUBL(8) = kll; SUBR(8) = klr; 512 subL[8] = kll; subR[8] = klr;
567 SUBL(9) = krl; SUBR(9) = krr; 513 subL[9] = krl; subR[9] = krr;
568 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); 514 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
569 /* k9 */ 515 /* k9 */
570 SUBL(12) = kll; SUBR(12) = klr; 516 subL[12] = kll; subR[12] = klr;
571 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); 517 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
572 /* k11, k12 */ 518 /* k11, k12 */
573 SUBL(14) = kll; SUBR(14) = klr; 519 subL[14] = kll; subR[14] = klr;
574 SUBL(15) = krl; SUBR(15) = krr; 520 subL[15] = krl; subR[15] = krr;
575 CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 34); 521 CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 34);
576 /* k15, k16 */ 522 /* k15, k16 */
577 SUBL(20) = kll; SUBR(20) = klr; 523 subL[20] = kll; subR[20] = klr;
578 SUBL(21) = krl; SUBR(21) = krr; 524 subL[21] = krl; subR[21] = krr;
579 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17); 525 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
580 /* kw3, kw4 */ 526 /* kw3, kw4 */
581 SUBL(24) = kll; SUBR(24) = klr; 527 subL[24] = kll; subR[24] = klr;
582 SUBL(25) = krl; SUBR(25) = krr; 528 subL[25] = krl; subR[25] = krr;
583
584 529
585 /* absorb kw2 to other subkeys */ 530 /* absorb kw2 to other subkeys */
586 /* round 2 */ 531 /* round 2 */
587 SUBL(3) ^= SUBL(1); SUBR(3) ^= SUBR(1); 532 subL[3] ^= subL[1]; subR[3] ^= subR[1];
588 /* round 4 */ 533 /* round 4 */
589 SUBL(5) ^= SUBL(1); SUBR(5) ^= SUBR(1); 534 subL[5] ^= subL[1]; subR[5] ^= subR[1];
590 /* round 6 */ 535 /* round 6 */
591 SUBL(7) ^= SUBL(1); SUBR(7) ^= SUBR(1); 536 subL[7] ^= subL[1]; subR[7] ^= subR[1];
592 SUBL(1) ^= SUBR(1) & ~SUBR(9); 537 subL[1] ^= subR[1] & ~subR[9];
593 dw = SUBL(1) & SUBL(9), 538 dw = subL[1] & subL[9],
594 SUBR(1) ^= CAMELLIA_RL1(dw); /* modified for FLinv(kl2) */ 539 subR[1] ^= CAMELLIA_RL1(dw); /* modified for FLinv(kl2) */
595 /* round 8 */ 540 /* round 8 */
596 SUBL(11) ^= SUBL(1); SUBR(11) ^= SUBR(1); 541 subL[11] ^= subL[1]; subR[11] ^= subR[1];
597 /* round 10 */ 542 /* round 10 */
598 SUBL(13) ^= SUBL(1); SUBR(13) ^= SUBR(1); 543 subL[13] ^= subL[1]; subR[13] ^= subR[1];
599 /* round 12 */ 544 /* round 12 */
600 SUBL(15) ^= SUBL(1); SUBR(15) ^= SUBR(1); 545 subL[15] ^= subL[1]; subR[15] ^= subR[1];
601 SUBL(1) ^= SUBR(1) & ~SUBR(17); 546 subL[1] ^= subR[1] & ~subR[17];
602 dw = SUBL(1) & SUBL(17), 547 dw = subL[1] & subL[17],
603 SUBR(1) ^= CAMELLIA_RL1(dw); /* modified for FLinv(kl4) */ 548 subR[1] ^= CAMELLIA_RL1(dw); /* modified for FLinv(kl4) */
604 /* round 14 */ 549 /* round 14 */
605 SUBL(19) ^= SUBL(1); SUBR(19) ^= SUBR(1); 550 subL[19] ^= subL[1]; subR[19] ^= subR[1];
606 /* round 16 */ 551 /* round 16 */
607 SUBL(21) ^= SUBL(1); SUBR(21) ^= SUBR(1); 552 subL[21] ^= subL[1]; subR[21] ^= subR[1];
608 /* round 18 */ 553 /* round 18 */
609 SUBL(23) ^= SUBL(1); SUBR(23) ^= SUBR(1); 554 subL[23] ^= subL[1]; subR[23] ^= subR[1];
610 /* kw3 */ 555 /* kw3 */
611 SUBL(24) ^= SUBL(1); SUBR(24) ^= SUBR(1); 556 subL[24] ^= subL[1]; subR[24] ^= subR[1];
612 557
613 /* absorb kw4 to other subkeys */ 558 /* absorb kw4 to other subkeys */
614 kw4l = SUBL(25); kw4r = SUBR(25); 559 kw4l = subL[25]; kw4r = subR[25];
615 /* round 17 */ 560 /* round 17 */
616 SUBL(22) ^= kw4l; SUBR(22) ^= kw4r; 561 subL[22] ^= kw4l; subR[22] ^= kw4r;
617 /* round 15 */ 562 /* round 15 */
618 SUBL(20) ^= kw4l; SUBR(20) ^= kw4r; 563 subL[20] ^= kw4l; subR[20] ^= kw4r;
619 /* round 13 */ 564 /* round 13 */
620 SUBL(18) ^= kw4l; SUBR(18) ^= kw4r; 565 subL[18] ^= kw4l; subR[18] ^= kw4r;
621 kw4l ^= kw4r & ~SUBR(16); 566 kw4l ^= kw4r & ~subR[16];
622 dw = kw4l & SUBL(16), 567 dw = kw4l & subL[16],
623 kw4r ^= CAMELLIA_RL1(dw); /* modified for FL(kl3) */ 568 kw4r ^= CAMELLIA_RL1(dw); /* modified for FL(kl3) */
624 /* round 11 */ 569 /* round 11 */
625 SUBL(14) ^= kw4l; SUBR(14) ^= kw4r; 570 subL[14] ^= kw4l; subR[14] ^= kw4r;
626 /* round 9 */ 571 /* round 9 */
627 SUBL(12) ^= kw4l; SUBR(12) ^= kw4r; 572 subL[12] ^= kw4l; subR[12] ^= kw4r;
628 /* round 7 */ 573 /* round 7 */
629 SUBL(10) ^= kw4l; SUBR(10) ^= kw4r; 574 subL[10] ^= kw4l; subR[10] ^= kw4r;
630 kw4l ^= kw4r & ~SUBR(8); 575 kw4l ^= kw4r & ~subR[8];
631 dw = kw4l & SUBL(8), 576 dw = kw4l & subL[8],
632 kw4r ^= CAMELLIA_RL1(dw); /* modified for FL(kl1) */ 577 kw4r ^= CAMELLIA_RL1(dw); /* modified for FL(kl1) */
633 /* round 5 */ 578 /* round 5 */
634 SUBL(6) ^= kw4l; SUBR(6) ^= kw4r; 579 subL[6] ^= kw4l; subR[6] ^= kw4r;
635 /* round 3 */ 580 /* round 3 */
636 SUBL(4) ^= kw4l; SUBR(4) ^= kw4r; 581 subL[4] ^= kw4l; subR[4] ^= kw4r;
637 /* round 1 */ 582 /* round 1 */
638 SUBL(2) ^= kw4l; SUBR(2) ^= kw4r; 583 subL[2] ^= kw4l; subR[2] ^= kw4r;
639 /* kw1 */ 584 /* kw1 */
640 SUBL(0) ^= kw4l; SUBR(0) ^= kw4r; 585 subL[0] ^= kw4l; subR[0] ^= kw4r;
641
642 586
643 /* key XOR is end of F-function */ 587 /* key XOR is end of F-function */
644 CAMELLIA_SUBKEY_L(0) = SUBL(0) ^ SUBL(2);/* kw1 */ 588 CAMELLIA_SUBKEY_L(0) = subL[0] ^ subL[2];/* kw1 */
645 CAMELLIA_SUBKEY_R(0) = SUBR(0) ^ SUBR(2); 589 CAMELLIA_SUBKEY_R(0) = subR[0] ^ subR[2];
646 CAMELLIA_SUBKEY_L(2) = SUBL(3); /* round 1 */ 590 CAMELLIA_SUBKEY_L(2) = subL[3]; /* round 1 */
647 CAMELLIA_SUBKEY_R(2) = SUBR(3); 591 CAMELLIA_SUBKEY_R(2) = subR[3];
648 CAMELLIA_SUBKEY_L(3) = SUBL(2) ^ SUBL(4); /* round 2 */ 592 CAMELLIA_SUBKEY_L(3) = subL[2] ^ subL[4]; /* round 2 */
649 CAMELLIA_SUBKEY_R(3) = SUBR(2) ^ SUBR(4); 593 CAMELLIA_SUBKEY_R(3) = subR[2] ^ subR[4];
650 CAMELLIA_SUBKEY_L(4) = SUBL(3) ^ SUBL(5); /* round 3 */ 594 CAMELLIA_SUBKEY_L(4) = subL[3] ^ subL[5]; /* round 3 */
651 CAMELLIA_SUBKEY_R(4) = SUBR(3) ^ SUBR(5); 595 CAMELLIA_SUBKEY_R(4) = subR[3] ^ subR[5];
652 CAMELLIA_SUBKEY_L(5) = SUBL(4) ^ SUBL(6); /* round 4 */ 596 CAMELLIA_SUBKEY_L(5) = subL[4] ^ subL[6]; /* round 4 */
653 CAMELLIA_SUBKEY_R(5) = SUBR(4) ^ SUBR(6); 597 CAMELLIA_SUBKEY_R(5) = subR[4] ^ subR[6];
654 CAMELLIA_SUBKEY_L(6) = SUBL(5) ^ SUBL(7); /* round 5 */ 598 CAMELLIA_SUBKEY_L(6) = subL[5] ^ subL[7]; /* round 5 */
655 CAMELLIA_SUBKEY_R(6) = SUBR(5) ^ SUBR(7); 599 CAMELLIA_SUBKEY_R(6) = subR[5] ^ subR[7];
656 tl = SUBL(10) ^ (SUBR(10) & ~SUBR(8)); 600 tl = subL[10] ^ (subR[10] & ~subR[8]);
657 dw = tl & SUBL(8), /* FL(kl1) */ 601 dw = tl & subL[8], /* FL(kl1) */
658 tr = SUBR(10) ^ CAMELLIA_RL1(dw); 602 tr = subR[10] ^ CAMELLIA_RL1(dw);
659 CAMELLIA_SUBKEY_L(7) = SUBL(6) ^ tl; /* round 6 */ 603 CAMELLIA_SUBKEY_L(7) = subL[6] ^ tl; /* round 6 */
660 CAMELLIA_SUBKEY_R(7) = SUBR(6) ^ tr; 604 CAMELLIA_SUBKEY_R(7) = subR[6] ^ tr;
661 CAMELLIA_SUBKEY_L(8) = SUBL(8); /* FL(kl1) */ 605 CAMELLIA_SUBKEY_L(8) = subL[8]; /* FL(kl1) */
662 CAMELLIA_SUBKEY_R(8) = SUBR(8); 606 CAMELLIA_SUBKEY_R(8) = subR[8];
663 CAMELLIA_SUBKEY_L(9) = SUBL(9); /* FLinv(kl2) */ 607 CAMELLIA_SUBKEY_L(9) = subL[9]; /* FLinv(kl2) */
664 CAMELLIA_SUBKEY_R(9) = SUBR(9); 608 CAMELLIA_SUBKEY_R(9) = subR[9];
665 tl = SUBL(7) ^ (SUBR(7) & ~SUBR(9)); 609 tl = subL[7] ^ (subR[7] & ~subR[9]);
666 dw = tl & SUBL(9), /* FLinv(kl2) */ 610 dw = tl & subL[9], /* FLinv(kl2) */
667 tr = SUBR(7) ^ CAMELLIA_RL1(dw); 611 tr = subR[7] ^ CAMELLIA_RL1(dw);
668 CAMELLIA_SUBKEY_L(10) = tl ^ SUBL(11); /* round 7 */ 612 CAMELLIA_SUBKEY_L(10) = tl ^ subL[11]; /* round 7 */
669 CAMELLIA_SUBKEY_R(10) = tr ^ SUBR(11); 613 CAMELLIA_SUBKEY_R(10) = tr ^ subR[11];
670 CAMELLIA_SUBKEY_L(11) = SUBL(10) ^ SUBL(12); /* round 8 */ 614 CAMELLIA_SUBKEY_L(11) = subL[10] ^ subL[12]; /* round 8 */
671 CAMELLIA_SUBKEY_R(11) = SUBR(10) ^ SUBR(12); 615 CAMELLIA_SUBKEY_R(11) = subR[10] ^ subR[12];
672 CAMELLIA_SUBKEY_L(12) = SUBL(11) ^ SUBL(13); /* round 9 */ 616 CAMELLIA_SUBKEY_L(12) = subL[11] ^ subL[13]; /* round 9 */
673 CAMELLIA_SUBKEY_R(12) = SUBR(11) ^ SUBR(13); 617 CAMELLIA_SUBKEY_R(12) = subR[11] ^ subR[13];
674 CAMELLIA_SUBKEY_L(13) = SUBL(12) ^ SUBL(14); /* round 10 */ 618 CAMELLIA_SUBKEY_L(13) = subL[12] ^ subL[14]; /* round 10 */
675 CAMELLIA_SUBKEY_R(13) = SUBR(12) ^ SUBR(14); 619 CAMELLIA_SUBKEY_R(13) = subR[12] ^ subR[14];
676 CAMELLIA_SUBKEY_L(14) = SUBL(13) ^ SUBL(15); /* round 11 */ 620 CAMELLIA_SUBKEY_L(14) = subL[13] ^ subL[15]; /* round 11 */
677 CAMELLIA_SUBKEY_R(14) = SUBR(13) ^ SUBR(15); 621 CAMELLIA_SUBKEY_R(14) = subR[13] ^ subR[15];
678 tl = SUBL(18) ^ (SUBR(18) & ~SUBR(16)); 622 tl = subL[18] ^ (subR[18] & ~subR[16]);
679 dw = tl & SUBL(16), /* FL(kl3) */ 623 dw = tl & subL[16], /* FL(kl3) */
680 tr = SUBR(18) ^ CAMELLIA_RL1(dw); 624 tr = subR[18] ^ CAMELLIA_RL1(dw);
681 CAMELLIA_SUBKEY_L(15) = SUBL(14) ^ tl; /* round 12 */ 625 CAMELLIA_SUBKEY_L(15) = subL[14] ^ tl; /* round 12 */
682 CAMELLIA_SUBKEY_R(15) = SUBR(14) ^ tr; 626 CAMELLIA_SUBKEY_R(15) = subR[14] ^ tr;
683 CAMELLIA_SUBKEY_L(16) = SUBL(16); /* FL(kl3) */ 627 CAMELLIA_SUBKEY_L(16) = subL[16]; /* FL(kl3) */
684 CAMELLIA_SUBKEY_R(16) = SUBR(16); 628 CAMELLIA_SUBKEY_R(16) = subR[16];
685 CAMELLIA_SUBKEY_L(17) = SUBL(17); /* FLinv(kl4) */ 629 CAMELLIA_SUBKEY_L(17) = subL[17]; /* FLinv(kl4) */
686 CAMELLIA_SUBKEY_R(17) = SUBR(17); 630 CAMELLIA_SUBKEY_R(17) = subR[17];
687 tl = SUBL(15) ^ (SUBR(15) & ~SUBR(17)); 631 tl = subL[15] ^ (subR[15] & ~subR[17]);
688 dw = tl & SUBL(17), /* FLinv(kl4) */ 632 dw = tl & subL[17], /* FLinv(kl4) */
689 tr = SUBR(15) ^ CAMELLIA_RL1(dw); 633 tr = subR[15] ^ CAMELLIA_RL1(dw);
690 CAMELLIA_SUBKEY_L(18) = tl ^ SUBL(19); /* round 13 */ 634 CAMELLIA_SUBKEY_L(18) = tl ^ subL[19]; /* round 13 */
691 CAMELLIA_SUBKEY_R(18) = tr ^ SUBR(19); 635 CAMELLIA_SUBKEY_R(18) = tr ^ subR[19];
692 CAMELLIA_SUBKEY_L(19) = SUBL(18) ^ SUBL(20); /* round 14 */ 636 CAMELLIA_SUBKEY_L(19) = subL[18] ^ subL[20]; /* round 14 */
693 CAMELLIA_SUBKEY_R(19) = SUBR(18) ^ SUBR(20); 637 CAMELLIA_SUBKEY_R(19) = subR[18] ^ subR[20];
694 CAMELLIA_SUBKEY_L(20) = SUBL(19) ^ SUBL(21); /* round 15 */ 638 CAMELLIA_SUBKEY_L(20) = subL[19] ^ subL[21]; /* round 15 */
695 CAMELLIA_SUBKEY_R(20) = SUBR(19) ^ SUBR(21); 639 CAMELLIA_SUBKEY_R(20) = subR[19] ^ subR[21];
696 CAMELLIA_SUBKEY_L(21) = SUBL(20) ^ SUBL(22); /* round 16 */ 640 CAMELLIA_SUBKEY_L(21) = subL[20] ^ subL[22]; /* round 16 */
697 CAMELLIA_SUBKEY_R(21) = SUBR(20) ^ SUBR(22); 641 CAMELLIA_SUBKEY_R(21) = subR[20] ^ subR[22];
698 CAMELLIA_SUBKEY_L(22) = SUBL(21) ^ SUBL(23); /* round 17 */ 642 CAMELLIA_SUBKEY_L(22) = subL[21] ^ subL[23]; /* round 17 */
699 CAMELLIA_SUBKEY_R(22) = SUBR(21) ^ SUBR(23); 643 CAMELLIA_SUBKEY_R(22) = subR[21] ^ subR[23];
700 CAMELLIA_SUBKEY_L(23) = SUBL(22); /* round 18 */ 644 CAMELLIA_SUBKEY_L(23) = subL[22]; /* round 18 */
701 CAMELLIA_SUBKEY_R(23) = SUBR(22); 645 CAMELLIA_SUBKEY_R(23) = subR[22];
702 CAMELLIA_SUBKEY_L(24) = SUBL(24) ^ SUBL(23); /* kw3 */ 646 CAMELLIA_SUBKEY_L(24) = subL[24] ^ subL[23]; /* kw3 */
703 CAMELLIA_SUBKEY_R(24) = SUBR(24) ^ SUBR(23); 647 CAMELLIA_SUBKEY_R(24) = subR[24] ^ subR[23];
704 648
705 /* apply the inverse of the last half of P-function */ 649 /* apply the inverse of the last half of P-function */
706 dw = CAMELLIA_SUBKEY_L(2) ^ CAMELLIA_SUBKEY_R(2), 650 dw = CAMELLIA_SUBKEY_L(2) ^ CAMELLIA_SUBKEY_R(2),
@@ -775,11 +719,8 @@ static void camellia_setup128(const unsigned char *key, u32 *subkey)
775 dw = CAMELLIA_RL8(dw);/* round 18 */ 719 dw = CAMELLIA_RL8(dw);/* round 18 */
776 CAMELLIA_SUBKEY_R(23) = CAMELLIA_SUBKEY_L(23) ^ dw, 720 CAMELLIA_SUBKEY_R(23) = CAMELLIA_SUBKEY_L(23) ^ dw,
777 CAMELLIA_SUBKEY_L(23) = dw; 721 CAMELLIA_SUBKEY_L(23) = dw;
778
779 return;
780} 722}
781 723
782
783static void camellia_setup256(const unsigned char *key, u32 *subkey) 724static void camellia_setup256(const unsigned char *key, u32 *subkey)
784{ 725{
785 u32 kll,klr,krl,krr; /* left half of key */ 726 u32 kll,klr,krl,krr; /* left half of key */
@@ -805,56 +746,56 @@ static void camellia_setup256(const unsigned char *key, u32 *subkey)
805 746
806 /* generate KL dependent subkeys */ 747 /* generate KL dependent subkeys */
807 /* kw1 */ 748 /* kw1 */
808 SUBL(0) = kll; SUBR(0) = klr; 749 subL[0] = kll; subR[0] = klr;
809 /* kw2 */ 750 /* kw2 */
810 SUBL(1) = krl; SUBR(1) = krr; 751 subL[1] = krl; subR[1] = krr;
811 CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 45); 752 CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 45);
812 /* k9 */ 753 /* k9 */
813 SUBL(12) = kll; SUBR(12) = klr; 754 subL[12] = kll; subR[12] = klr;
814 /* k10 */ 755 /* k10 */
815 SUBL(13) = krl; SUBR(13) = krr; 756 subL[13] = krl; subR[13] = krr;
816 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); 757 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
817 /* kl3 */ 758 /* kl3 */
818 SUBL(16) = kll; SUBR(16) = klr; 759 subL[16] = kll; subR[16] = klr;
819 /* kl4 */ 760 /* kl4 */
820 SUBL(17) = krl; SUBR(17) = krr; 761 subL[17] = krl; subR[17] = krr;
821 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17); 762 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
822 /* k17 */ 763 /* k17 */
823 SUBL(22) = kll; SUBR(22) = klr; 764 subL[22] = kll; subR[22] = klr;
824 /* k18 */ 765 /* k18 */
825 SUBL(23) = krl; SUBR(23) = krr; 766 subL[23] = krl; subR[23] = krr;
826 CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 34); 767 CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 34);
827 /* k23 */ 768 /* k23 */
828 SUBL(30) = kll; SUBR(30) = klr; 769 subL[30] = kll; subR[30] = klr;
829 /* k24 */ 770 /* k24 */
830 SUBL(31) = krl; SUBR(31) = krr; 771 subL[31] = krl; subR[31] = krr;
831 772
832 /* generate KR dependent subkeys */ 773 /* generate KR dependent subkeys */
833 CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 15); 774 CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 15);
834 /* k3 */ 775 /* k3 */
835 SUBL(4) = krll; SUBR(4) = krlr; 776 subL[4] = krll; subR[4] = krlr;
836 /* k4 */ 777 /* k4 */
837 SUBL(5) = krrl; SUBR(5) = krrr; 778 subL[5] = krrl; subR[5] = krrr;
838 CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 15); 779 CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 15);
839 /* kl1 */ 780 /* kl1 */
840 SUBL(8) = krll; SUBR(8) = krlr; 781 subL[8] = krll; subR[8] = krlr;
841 /* kl2 */ 782 /* kl2 */
842 SUBL(9) = krrl; SUBR(9) = krrr; 783 subL[9] = krrl; subR[9] = krrr;
843 CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30); 784 CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30);
844 /* k13 */ 785 /* k13 */
845 SUBL(18) = krll; SUBR(18) = krlr; 786 subL[18] = krll; subR[18] = krlr;
846 /* k14 */ 787 /* k14 */
847 SUBL(19) = krrl; SUBR(19) = krrr; 788 subL[19] = krrl; subR[19] = krrr;
848 CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 34); 789 CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 34);
849 /* k19 */ 790 /* k19 */
850 SUBL(26) = krll; SUBR(26) = krlr; 791 subL[26] = krll; subR[26] = krlr;
851 /* k20 */ 792 /* k20 */
852 SUBL(27) = krrl; SUBR(27) = krrr; 793 subL[27] = krrl; subR[27] = krrr;
853 CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 34); 794 CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 34);
854 795
855 /* generate KA */ 796 /* generate KA */
856 kll = SUBL(0) ^ krll; klr = SUBR(0) ^ krlr; 797 kll = subL[0] ^ krll; klr = subR[0] ^ krlr;
857 krl = SUBL(1) ^ krrl; krr = SUBR(1) ^ krrr; 798 krl = subL[1] ^ krrl; krr = subR[1] ^ krrr;
858 CAMELLIA_F(kll, klr, 799 CAMELLIA_F(kll, klr,
859 CAMELLIA_SIGMA1L, CAMELLIA_SIGMA1R, 800 CAMELLIA_SIGMA1L, CAMELLIA_SIGMA1R,
860 w0, w1, il, ir, t0, t1); 801 w0, w1, il, ir, t0, t1);
@@ -887,208 +828,207 @@ static void camellia_setup256(const unsigned char *key, u32 *subkey)
887 /* generate KA dependent subkeys */ 828 /* generate KA dependent subkeys */
888 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15); 829 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
889 /* k5 */ 830 /* k5 */
890 SUBL(6) = kll; SUBR(6) = klr; 831 subL[6] = kll; subR[6] = klr;
891 /* k6 */ 832 /* k6 */
892 SUBL(7) = krl; SUBR(7) = krr; 833 subL[7] = krl; subR[7] = krr;
893 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 30); 834 CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 30);
894 /* k11 */ 835 /* k11 */
895 SUBL(14) = kll; SUBR(14) = klr; 836 subL[14] = kll; subR[14] = klr;
896 /* k12 */ 837 /* k12 */
897 SUBL(15) = krl; SUBR(15) = krr; 838 subL[15] = krl; subR[15] = krr;
898 /* rotation left shift 32bit */ 839 /* rotation left shift 32bit */
899 /* kl5 */ 840 /* kl5 */
900 SUBL(24) = klr; SUBR(24) = krl; 841 subL[24] = klr; subR[24] = krl;
901 /* kl6 */ 842 /* kl6 */
902 SUBL(25) = krr; SUBR(25) = kll; 843 subL[25] = krr; subR[25] = kll;
903 /* rotation left shift 49 from k11,k12 -> k21,k22 */ 844 /* rotation left shift 49 from k11,k12 -> k21,k22 */
904 CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 49); 845 CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 49);
905 /* k21 */ 846 /* k21 */
906 SUBL(28) = kll; SUBR(28) = klr; 847 subL[28] = kll; subR[28] = klr;
907 /* k22 */ 848 /* k22 */
908 SUBL(29) = krl; SUBR(29) = krr; 849 subL[29] = krl; subR[29] = krr;
909 850
910 /* generate KB dependent subkeys */ 851 /* generate KB dependent subkeys */
911 /* k1 */ 852 /* k1 */
912 SUBL(2) = krll; SUBR(2) = krlr; 853 subL[2] = krll; subR[2] = krlr;
913 /* k2 */ 854 /* k2 */
914 SUBL(3) = krrl; SUBR(3) = krrr; 855 subL[3] = krrl; subR[3] = krrr;
915 CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30); 856 CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30);
916 /* k7 */ 857 /* k7 */
917 SUBL(10) = krll; SUBR(10) = krlr; 858 subL[10] = krll; subR[10] = krlr;
918 /* k8 */ 859 /* k8 */
919 SUBL(11) = krrl; SUBR(11) = krrr; 860 subL[11] = krrl; subR[11] = krrr;
920 CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30); 861 CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30);
921 /* k15 */ 862 /* k15 */
922 SUBL(20) = krll; SUBR(20) = krlr; 863 subL[20] = krll; subR[20] = krlr;
923 /* k16 */ 864 /* k16 */
924 SUBL(21) = krrl; SUBR(21) = krrr; 865 subL[21] = krrl; subR[21] = krrr;
925 CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 51); 866 CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 51);
926 /* kw3 */ 867 /* kw3 */
927 SUBL(32) = krll; SUBR(32) = krlr; 868 subL[32] = krll; subR[32] = krlr;
928 /* kw4 */ 869 /* kw4 */
929 SUBL(33) = krrl; SUBR(33) = krrr; 870 subL[33] = krrl; subR[33] = krrr;
930 871
931 /* absorb kw2 to other subkeys */ 872 /* absorb kw2 to other subkeys */
932 /* round 2 */ 873 /* round 2 */
933 SUBL(3) ^= SUBL(1); SUBR(3) ^= SUBR(1); 874 subL[3] ^= subL[1]; subR[3] ^= subR[1];
934 /* round 4 */ 875 /* round 4 */
935 SUBL(5) ^= SUBL(1); SUBR(5) ^= SUBR(1); 876 subL[5] ^= subL[1]; subR[5] ^= subR[1];
936 /* round 6 */ 877 /* round 6 */
937 SUBL(7) ^= SUBL(1); SUBR(7) ^= SUBR(1); 878 subL[7] ^= subL[1]; subR[7] ^= subR[1];
938 SUBL(1) ^= SUBR(1) & ~SUBR(9); 879 subL[1] ^= subR[1] & ~subR[9];
939 dw = SUBL(1) & SUBL(9), 880 dw = subL[1] & subL[9],
940 SUBR(1) ^= CAMELLIA_RL1(dw); /* modified for FLinv(kl2) */ 881 subR[1] ^= CAMELLIA_RL1(dw); /* modified for FLinv(kl2) */
941 /* round 8 */ 882 /* round 8 */
942 SUBL(11) ^= SUBL(1); SUBR(11) ^= SUBR(1); 883 subL[11] ^= subL[1]; subR[11] ^= subR[1];
943 /* round 10 */ 884 /* round 10 */
944 SUBL(13) ^= SUBL(1); SUBR(13) ^= SUBR(1); 885 subL[13] ^= subL[1]; subR[13] ^= subR[1];
945 /* round 12 */ 886 /* round 12 */
946 SUBL(15) ^= SUBL(1); SUBR(15) ^= SUBR(1); 887 subL[15] ^= subL[1]; subR[15] ^= subR[1];
947 SUBL(1) ^= SUBR(1) & ~SUBR(17); 888 subL[1] ^= subR[1] & ~subR[17];
948 dw = SUBL(1) & SUBL(17), 889 dw = subL[1] & subL[17],
949 SUBR(1) ^= CAMELLIA_RL1(dw); /* modified for FLinv(kl4) */ 890 subR[1] ^= CAMELLIA_RL1(dw); /* modified for FLinv(kl4) */
950 /* round 14 */ 891 /* round 14 */
951 SUBL(19) ^= SUBL(1); SUBR(19) ^= SUBR(1); 892 subL[19] ^= subL[1]; subR[19] ^= subR[1];
952 /* round 16 */ 893 /* round 16 */
953 SUBL(21) ^= SUBL(1); SUBR(21) ^= SUBR(1); 894 subL[21] ^= subL[1]; subR[21] ^= subR[1];
954 /* round 18 */ 895 /* round 18 */
955 SUBL(23) ^= SUBL(1); SUBR(23) ^= SUBR(1); 896 subL[23] ^= subL[1]; subR[23] ^= subR[1];
956 SUBL(1) ^= SUBR(1) & ~SUBR(25); 897 subL[1] ^= subR[1] & ~subR[25];
957 dw = SUBL(1) & SUBL(25), 898 dw = subL[1] & subL[25],
958 SUBR(1) ^= CAMELLIA_RL1(dw); /* modified for FLinv(kl6) */ 899 subR[1] ^= CAMELLIA_RL1(dw); /* modified for FLinv(kl6) */
959 /* round 20 */ 900 /* round 20 */
960 SUBL(27) ^= SUBL(1); SUBR(27) ^= SUBR(1); 901 subL[27] ^= subL[1]; subR[27] ^= subR[1];
961 /* round 22 */ 902 /* round 22 */
962 SUBL(29) ^= SUBL(1); SUBR(29) ^= SUBR(1); 903 subL[29] ^= subL[1]; subR[29] ^= subR[1];
963 /* round 24 */ 904 /* round 24 */
964 SUBL(31) ^= SUBL(1); SUBR(31) ^= SUBR(1); 905 subL[31] ^= subL[1]; subR[31] ^= subR[1];
965 /* kw3 */ 906 /* kw3 */
966 SUBL(32) ^= SUBL(1); SUBR(32) ^= SUBR(1); 907 subL[32] ^= subL[1]; subR[32] ^= subR[1];
967
968 908
969 /* absorb kw4 to other subkeys */ 909 /* absorb kw4 to other subkeys */
970 kw4l = SUBL(33); kw4r = SUBR(33); 910 kw4l = subL[33]; kw4r = subR[33];
971 /* round 23 */ 911 /* round 23 */
972 SUBL(30) ^= kw4l; SUBR(30) ^= kw4r; 912 subL[30] ^= kw4l; subR[30] ^= kw4r;
973 /* round 21 */ 913 /* round 21 */
974 SUBL(28) ^= kw4l; SUBR(28) ^= kw4r; 914 subL[28] ^= kw4l; subR[28] ^= kw4r;
975 /* round 19 */ 915 /* round 19 */
976 SUBL(26) ^= kw4l; SUBR(26) ^= kw4r; 916 subL[26] ^= kw4l; subR[26] ^= kw4r;
977 kw4l ^= kw4r & ~SUBR(24); 917 kw4l ^= kw4r & ~subR[24];
978 dw = kw4l & SUBL(24), 918 dw = kw4l & subL[24],
979 kw4r ^= CAMELLIA_RL1(dw); /* modified for FL(kl5) */ 919 kw4r ^= CAMELLIA_RL1(dw); /* modified for FL(kl5) */
980 /* round 17 */ 920 /* round 17 */
981 SUBL(22) ^= kw4l; SUBR(22) ^= kw4r; 921 subL[22] ^= kw4l; subR[22] ^= kw4r;
982 /* round 15 */ 922 /* round 15 */
983 SUBL(20) ^= kw4l; SUBR(20) ^= kw4r; 923 subL[20] ^= kw4l; subR[20] ^= kw4r;
984 /* round 13 */ 924 /* round 13 */
985 SUBL(18) ^= kw4l; SUBR(18) ^= kw4r; 925 subL[18] ^= kw4l; subR[18] ^= kw4r;
986 kw4l ^= kw4r & ~SUBR(16); 926 kw4l ^= kw4r & ~subR[16];
987 dw = kw4l & SUBL(16), 927 dw = kw4l & subL[16],
988 kw4r ^= CAMELLIA_RL1(dw); /* modified for FL(kl3) */ 928 kw4r ^= CAMELLIA_RL1(dw); /* modified for FL(kl3) */
989 /* round 11 */ 929 /* round 11 */
990 SUBL(14) ^= kw4l; SUBR(14) ^= kw4r; 930 subL[14] ^= kw4l; subR[14] ^= kw4r;
991 /* round 9 */ 931 /* round 9 */
992 SUBL(12) ^= kw4l; SUBR(12) ^= kw4r; 932 subL[12] ^= kw4l; subR[12] ^= kw4r;
993 /* round 7 */ 933 /* round 7 */
994 SUBL(10) ^= kw4l; SUBR(10) ^= kw4r; 934 subL[10] ^= kw4l; subR[10] ^= kw4r;
995 kw4l ^= kw4r & ~SUBR(8); 935 kw4l ^= kw4r & ~subR[8];
996 dw = kw4l & SUBL(8), 936 dw = kw4l & subL[8],
997 kw4r ^= CAMELLIA_RL1(dw); /* modified for FL(kl1) */ 937 kw4r ^= CAMELLIA_RL1(dw); /* modified for FL(kl1) */
998 /* round 5 */ 938 /* round 5 */
999 SUBL(6) ^= kw4l; SUBR(6) ^= kw4r; 939 subL[6] ^= kw4l; subR[6] ^= kw4r;
1000 /* round 3 */ 940 /* round 3 */
1001 SUBL(4) ^= kw4l; SUBR(4) ^= kw4r; 941 subL[4] ^= kw4l; subR[4] ^= kw4r;
1002 /* round 1 */ 942 /* round 1 */
1003 SUBL(2) ^= kw4l; SUBR(2) ^= kw4r; 943 subL[2] ^= kw4l; subR[2] ^= kw4r;
1004 /* kw1 */ 944 /* kw1 */
1005 SUBL(0) ^= kw4l; SUBR(0) ^= kw4r; 945 subL[0] ^= kw4l; subR[0] ^= kw4r;
1006 946
1007 /* key XOR is end of F-function */ 947 /* key XOR is end of F-function */
1008 CAMELLIA_SUBKEY_L(0) = SUBL(0) ^ SUBL(2);/* kw1 */ 948 CAMELLIA_SUBKEY_L(0) = subL[0] ^ subL[2];/* kw1 */
1009 CAMELLIA_SUBKEY_R(0) = SUBR(0) ^ SUBR(2); 949 CAMELLIA_SUBKEY_R(0) = subR[0] ^ subR[2];
1010 CAMELLIA_SUBKEY_L(2) = SUBL(3); /* round 1 */ 950 CAMELLIA_SUBKEY_L(2) = subL[3]; /* round 1 */
1011 CAMELLIA_SUBKEY_R(2) = SUBR(3); 951 CAMELLIA_SUBKEY_R(2) = subR[3];
1012 CAMELLIA_SUBKEY_L(3) = SUBL(2) ^ SUBL(4); /* round 2 */ 952 CAMELLIA_SUBKEY_L(3) = subL[2] ^ subL[4]; /* round 2 */
1013 CAMELLIA_SUBKEY_R(3) = SUBR(2) ^ SUBR(4); 953 CAMELLIA_SUBKEY_R(3) = subR[2] ^ subR[4];
1014 CAMELLIA_SUBKEY_L(4) = SUBL(3) ^ SUBL(5); /* round 3 */ 954 CAMELLIA_SUBKEY_L(4) = subL[3] ^ subL[5]; /* round 3 */
1015 CAMELLIA_SUBKEY_R(4) = SUBR(3) ^ SUBR(5); 955 CAMELLIA_SUBKEY_R(4) = subR[3] ^ subR[5];
1016 CAMELLIA_SUBKEY_L(5) = SUBL(4) ^ SUBL(6); /* round 4 */ 956 CAMELLIA_SUBKEY_L(5) = subL[4] ^ subL[6]; /* round 4 */
1017 CAMELLIA_SUBKEY_R(5) = SUBR(4) ^ SUBR(6); 957 CAMELLIA_SUBKEY_R(5) = subR[4] ^ subR[6];
1018 CAMELLIA_SUBKEY_L(6) = SUBL(5) ^ SUBL(7); /* round 5 */ 958 CAMELLIA_SUBKEY_L(6) = subL[5] ^ subL[7]; /* round 5 */
1019 CAMELLIA_SUBKEY_R(6) = SUBR(5) ^ SUBR(7); 959 CAMELLIA_SUBKEY_R(6) = subR[5] ^ subR[7];
1020 tl = SUBL(10) ^ (SUBR(10) & ~SUBR(8)); 960 tl = subL[10] ^ (subR[10] & ~subR[8]);
1021 dw = tl & SUBL(8), /* FL(kl1) */ 961 dw = tl & subL[8], /* FL(kl1) */
1022 tr = SUBR(10) ^ CAMELLIA_RL1(dw); 962 tr = subR[10] ^ CAMELLIA_RL1(dw);
1023 CAMELLIA_SUBKEY_L(7) = SUBL(6) ^ tl; /* round 6 */ 963 CAMELLIA_SUBKEY_L(7) = subL[6] ^ tl; /* round 6 */
1024 CAMELLIA_SUBKEY_R(7) = SUBR(6) ^ tr; 964 CAMELLIA_SUBKEY_R(7) = subR[6] ^ tr;
1025 CAMELLIA_SUBKEY_L(8) = SUBL(8); /* FL(kl1) */ 965 CAMELLIA_SUBKEY_L(8) = subL[8]; /* FL(kl1) */
1026 CAMELLIA_SUBKEY_R(8) = SUBR(8); 966 CAMELLIA_SUBKEY_R(8) = subR[8];
1027 CAMELLIA_SUBKEY_L(9) = SUBL(9); /* FLinv(kl2) */ 967 CAMELLIA_SUBKEY_L(9) = subL[9]; /* FLinv(kl2) */
1028 CAMELLIA_SUBKEY_R(9) = SUBR(9); 968 CAMELLIA_SUBKEY_R(9) = subR[9];
1029 tl = SUBL(7) ^ (SUBR(7) & ~SUBR(9)); 969 tl = subL[7] ^ (subR[7] & ~subR[9]);
1030 dw = tl & SUBL(9), /* FLinv(kl2) */ 970 dw = tl & subL[9], /* FLinv(kl2) */
1031 tr = SUBR(7) ^ CAMELLIA_RL1(dw); 971 tr = subR[7] ^ CAMELLIA_RL1(dw);
1032 CAMELLIA_SUBKEY_L(10) = tl ^ SUBL(11); /* round 7 */ 972 CAMELLIA_SUBKEY_L(10) = tl ^ subL[11]; /* round 7 */
1033 CAMELLIA_SUBKEY_R(10) = tr ^ SUBR(11); 973 CAMELLIA_SUBKEY_R(10) = tr ^ subR[11];
1034 CAMELLIA_SUBKEY_L(11) = SUBL(10) ^ SUBL(12); /* round 8 */ 974 CAMELLIA_SUBKEY_L(11) = subL[10] ^ subL[12]; /* round 8 */
1035 CAMELLIA_SUBKEY_R(11) = SUBR(10) ^ SUBR(12); 975 CAMELLIA_SUBKEY_R(11) = subR[10] ^ subR[12];
1036 CAMELLIA_SUBKEY_L(12) = SUBL(11) ^ SUBL(13); /* round 9 */ 976 CAMELLIA_SUBKEY_L(12) = subL[11] ^ subL[13]; /* round 9 */
1037 CAMELLIA_SUBKEY_R(12) = SUBR(11) ^ SUBR(13); 977 CAMELLIA_SUBKEY_R(12) = subR[11] ^ subR[13];
1038 CAMELLIA_SUBKEY_L(13) = SUBL(12) ^ SUBL(14); /* round 10 */ 978 CAMELLIA_SUBKEY_L(13) = subL[12] ^ subL[14]; /* round 10 */
1039 CAMELLIA_SUBKEY_R(13) = SUBR(12) ^ SUBR(14); 979 CAMELLIA_SUBKEY_R(13) = subR[12] ^ subR[14];
1040 CAMELLIA_SUBKEY_L(14) = SUBL(13) ^ SUBL(15); /* round 11 */ 980 CAMELLIA_SUBKEY_L(14) = subL[13] ^ subL[15]; /* round 11 */
1041 CAMELLIA_SUBKEY_R(14) = SUBR(13) ^ SUBR(15); 981 CAMELLIA_SUBKEY_R(14) = subR[13] ^ subR[15];
1042 tl = SUBL(18) ^ (SUBR(18) & ~SUBR(16)); 982 tl = subL[18] ^ (subR[18] & ~subR[16]);
1043 dw = tl & SUBL(16), /* FL(kl3) */ 983 dw = tl & subL[16], /* FL(kl3) */
1044 tr = SUBR(18) ^ CAMELLIA_RL1(dw); 984 tr = subR[18] ^ CAMELLIA_RL1(dw);
1045 CAMELLIA_SUBKEY_L(15) = SUBL(14) ^ tl; /* round 12 */ 985 CAMELLIA_SUBKEY_L(15) = subL[14] ^ tl; /* round 12 */
1046 CAMELLIA_SUBKEY_R(15) = SUBR(14) ^ tr; 986 CAMELLIA_SUBKEY_R(15) = subR[14] ^ tr;
1047 CAMELLIA_SUBKEY_L(16) = SUBL(16); /* FL(kl3) */ 987 CAMELLIA_SUBKEY_L(16) = subL[16]; /* FL(kl3) */
1048 CAMELLIA_SUBKEY_R(16) = SUBR(16); 988 CAMELLIA_SUBKEY_R(16) = subR[16];
1049 CAMELLIA_SUBKEY_L(17) = SUBL(17); /* FLinv(kl4) */ 989 CAMELLIA_SUBKEY_L(17) = subL[17]; /* FLinv(kl4) */
1050 CAMELLIA_SUBKEY_R(17) = SUBR(17); 990 CAMELLIA_SUBKEY_R(17) = subR[17];
1051 tl = SUBL(15) ^ (SUBR(15) & ~SUBR(17)); 991 tl = subL[15] ^ (subR[15] & ~subR[17]);
1052 dw = tl & SUBL(17), /* FLinv(kl4) */ 992 dw = tl & subL[17], /* FLinv(kl4) */
1053 tr = SUBR(15) ^ CAMELLIA_RL1(dw); 993 tr = subR[15] ^ CAMELLIA_RL1(dw);
1054 CAMELLIA_SUBKEY_L(18) = tl ^ SUBL(19); /* round 13 */ 994 CAMELLIA_SUBKEY_L(18) = tl ^ subL[19]; /* round 13 */
1055 CAMELLIA_SUBKEY_R(18) = tr ^ SUBR(19); 995 CAMELLIA_SUBKEY_R(18) = tr ^ subR[19];
1056 CAMELLIA_SUBKEY_L(19) = SUBL(18) ^ SUBL(20); /* round 14 */ 996 CAMELLIA_SUBKEY_L(19) = subL[18] ^ subL[20]; /* round 14 */
1057 CAMELLIA_SUBKEY_R(19) = SUBR(18) ^ SUBR(20); 997 CAMELLIA_SUBKEY_R(19) = subR[18] ^ subR[20];
1058 CAMELLIA_SUBKEY_L(20) = SUBL(19) ^ SUBL(21); /* round 15 */ 998 CAMELLIA_SUBKEY_L(20) = subL[19] ^ subL[21]; /* round 15 */
1059 CAMELLIA_SUBKEY_R(20) = SUBR(19) ^ SUBR(21); 999 CAMELLIA_SUBKEY_R(20) = subR[19] ^ subR[21];
1060 CAMELLIA_SUBKEY_L(21) = SUBL(20) ^ SUBL(22); /* round 16 */ 1000 CAMELLIA_SUBKEY_L(21) = subL[20] ^ subL[22]; /* round 16 */
1061 CAMELLIA_SUBKEY_R(21) = SUBR(20) ^ SUBR(22); 1001 CAMELLIA_SUBKEY_R(21) = subR[20] ^ subR[22];
1062 CAMELLIA_SUBKEY_L(22) = SUBL(21) ^ SUBL(23); /* round 17 */ 1002 CAMELLIA_SUBKEY_L(22) = subL[21] ^ subL[23]; /* round 17 */
1063 CAMELLIA_SUBKEY_R(22) = SUBR(21) ^ SUBR(23); 1003 CAMELLIA_SUBKEY_R(22) = subR[21] ^ subR[23];
1064 tl = SUBL(26) ^ (SUBR(26) 1004 tl = subL[26] ^ (subR[26]
1065 & ~SUBR(24)); 1005 & ~subR[24]);
1066 dw = tl & SUBL(24), /* FL(kl5) */ 1006 dw = tl & subL[24], /* FL(kl5) */
1067 tr = SUBR(26) ^ CAMELLIA_RL1(dw); 1007 tr = subR[26] ^ CAMELLIA_RL1(dw);
1068 CAMELLIA_SUBKEY_L(23) = SUBL(22) ^ tl; /* round 18 */ 1008 CAMELLIA_SUBKEY_L(23) = subL[22] ^ tl; /* round 18 */
1069 CAMELLIA_SUBKEY_R(23) = SUBR(22) ^ tr; 1009 CAMELLIA_SUBKEY_R(23) = subR[22] ^ tr;
1070 CAMELLIA_SUBKEY_L(24) = SUBL(24); /* FL(kl5) */ 1010 CAMELLIA_SUBKEY_L(24) = subL[24]; /* FL(kl5) */
1071 CAMELLIA_SUBKEY_R(24) = SUBR(24); 1011 CAMELLIA_SUBKEY_R(24) = subR[24];
1072 CAMELLIA_SUBKEY_L(25) = SUBL(25); /* FLinv(kl6) */ 1012 CAMELLIA_SUBKEY_L(25) = subL[25]; /* FLinv(kl6) */
1073 CAMELLIA_SUBKEY_R(25) = SUBR(25); 1013 CAMELLIA_SUBKEY_R(25) = subR[25];
1074 tl = SUBL(23) ^ (SUBR(23) & 1014 tl = subL[23] ^ (subR[23] &
1075 ~SUBR(25)); 1015 ~subR[25]);
1076 dw = tl & SUBL(25), /* FLinv(kl6) */ 1016 dw = tl & subL[25], /* FLinv(kl6) */
1077 tr = SUBR(23) ^ CAMELLIA_RL1(dw); 1017 tr = subR[23] ^ CAMELLIA_RL1(dw);
1078 CAMELLIA_SUBKEY_L(26) = tl ^ SUBL(27); /* round 19 */ 1018 CAMELLIA_SUBKEY_L(26) = tl ^ subL[27]; /* round 19 */
1079 CAMELLIA_SUBKEY_R(26) = tr ^ SUBR(27); 1019 CAMELLIA_SUBKEY_R(26) = tr ^ subR[27];
1080 CAMELLIA_SUBKEY_L(27) = SUBL(26) ^ SUBL(28); /* round 20 */ 1020 CAMELLIA_SUBKEY_L(27) = subL[26] ^ subL[28]; /* round 20 */
1081 CAMELLIA_SUBKEY_R(27) = SUBR(26) ^ SUBR(28); 1021 CAMELLIA_SUBKEY_R(27) = subR[26] ^ subR[28];
1082 CAMELLIA_SUBKEY_L(28) = SUBL(27) ^ SUBL(29); /* round 21 */ 1022 CAMELLIA_SUBKEY_L(28) = subL[27] ^ subL[29]; /* round 21 */
1083 CAMELLIA_SUBKEY_R(28) = SUBR(27) ^ SUBR(29); 1023 CAMELLIA_SUBKEY_R(28) = subR[27] ^ subR[29];
1084 CAMELLIA_SUBKEY_L(29) = SUBL(28) ^ SUBL(30); /* round 22 */ 1024 CAMELLIA_SUBKEY_L(29) = subL[28] ^ subL[30]; /* round 22 */
1085 CAMELLIA_SUBKEY_R(29) = SUBR(28) ^ SUBR(30); 1025 CAMELLIA_SUBKEY_R(29) = subR[28] ^ subR[30];
1086 CAMELLIA_SUBKEY_L(30) = SUBL(29) ^ SUBL(31); /* round 23 */ 1026 CAMELLIA_SUBKEY_L(30) = subL[29] ^ subL[31]; /* round 23 */
1087 CAMELLIA_SUBKEY_R(30) = SUBR(29) ^ SUBR(31); 1027 CAMELLIA_SUBKEY_R(30) = subR[29] ^ subR[31];
1088 CAMELLIA_SUBKEY_L(31) = SUBL(30); /* round 24 */ 1028 CAMELLIA_SUBKEY_L(31) = subL[30]; /* round 24 */
1089 CAMELLIA_SUBKEY_R(31) = SUBR(30); 1029 CAMELLIA_SUBKEY_R(31) = subR[30];
1090 CAMELLIA_SUBKEY_L(32) = SUBL(32) ^ SUBL(31); /* kw3 */ 1030 CAMELLIA_SUBKEY_L(32) = subL[32] ^ subL[31]; /* kw3 */
1091 CAMELLIA_SUBKEY_R(32) = SUBR(32) ^ SUBR(31); 1031 CAMELLIA_SUBKEY_R(32) = subR[32] ^ subR[31];
1092 1032
1093 /* apply the inverse of the last half of P-function */ 1033 /* apply the inverse of the last half of P-function */
1094 dw = CAMELLIA_SUBKEY_L(2) ^ CAMELLIA_SUBKEY_R(2), 1034 dw = CAMELLIA_SUBKEY_L(2) ^ CAMELLIA_SUBKEY_R(2),
@@ -1187,8 +1127,6 @@ static void camellia_setup256(const unsigned char *key, u32 *subkey)
1187 dw = CAMELLIA_RL8(dw);/* round 24 */ 1127 dw = CAMELLIA_RL8(dw);/* round 24 */
1188 CAMELLIA_SUBKEY_R(31) = CAMELLIA_SUBKEY_L(31) ^ dw, 1128 CAMELLIA_SUBKEY_R(31) = CAMELLIA_SUBKEY_L(31) ^ dw,
1189 CAMELLIA_SUBKEY_L(31) = dw; 1129 CAMELLIA_SUBKEY_L(31) = dw;
1190
1191 return;
1192} 1130}
1193 1131
1194static void camellia_setup192(const unsigned char *key, u32 *subkey) 1132static void camellia_setup192(const unsigned char *key, u32 *subkey)
@@ -1197,20 +1135,16 @@ static void camellia_setup192(const unsigned char *key, u32 *subkey)
1197 u32 krll, krlr, krrl,krrr; 1135 u32 krll, krlr, krrl,krrr;
1198 1136
1199 memcpy(kk, key, 24); 1137 memcpy(kk, key, 24);
1200 memcpy((unsigned char *)&krll, key+16,4); 1138 memcpy((unsigned char *)&krll, key+16, 4);
1201 memcpy((unsigned char *)&krlr, key+20,4); 1139 memcpy((unsigned char *)&krlr, key+20, 4);
1202 krrl = ~krll; 1140 krrl = ~krll;
1203 krrr = ~krlr; 1141 krrr = ~krlr;
1204 memcpy(kk+24, (unsigned char *)&krrl, 4); 1142 memcpy(kk+24, (unsigned char *)&krrl, 4);
1205 memcpy(kk+28, (unsigned char *)&krrr, 4); 1143 memcpy(kk+28, (unsigned char *)&krrr, 4);
1206 camellia_setup256(kk, subkey); 1144 camellia_setup256(kk, subkey);
1207 return;
1208} 1145}
1209 1146
1210 1147
1211/**
1212 * Stuff related to camellia encryption/decryption
1213 */
1214static void camellia_encrypt128(const u32 *subkey, __be32 *io_text) 1148static void camellia_encrypt128(const u32 *subkey, __be32 *io_text)
1215{ 1149{
1216 u32 il,ir,t0,t1; /* temporary valiables */ 1150 u32 il,ir,t0,t1; /* temporary valiables */
@@ -1222,11 +1156,11 @@ static void camellia_encrypt128(const u32 *subkey, __be32 *io_text)
1222 io[2] = be32_to_cpu(io_text[2]); 1156 io[2] = be32_to_cpu(io_text[2]);
1223 io[3] = be32_to_cpu(io_text[3]); 1157 io[3] = be32_to_cpu(io_text[3]);
1224 1158
1225 /* pre whitening but absorb kw2*/ 1159 /* pre whitening but absorb kw2 */
1226 io[0] ^= CAMELLIA_SUBKEY_L(0); 1160 io[0] ^= CAMELLIA_SUBKEY_L(0);
1227 io[1] ^= CAMELLIA_SUBKEY_R(0); 1161 io[1] ^= CAMELLIA_SUBKEY_R(0);
1228 /* main iteration */
1229 1162
1163 /* main iteration */
1230 CAMELLIA_ROUNDSM(io[0],io[1], 1164 CAMELLIA_ROUNDSM(io[0],io[1],
1231 CAMELLIA_SUBKEY_L(2),CAMELLIA_SUBKEY_R(2), 1165 CAMELLIA_SUBKEY_L(2),CAMELLIA_SUBKEY_R(2),
1232 io[2],io[3],il,ir,t0,t1); 1166 io[2],io[3],il,ir,t0,t1);
@@ -1298,19 +1232,10 @@ static void camellia_encrypt128(const u32 *subkey, __be32 *io_text)
1298 io[2] ^= CAMELLIA_SUBKEY_L(24); 1232 io[2] ^= CAMELLIA_SUBKEY_L(24);
1299 io[3] ^= CAMELLIA_SUBKEY_R(24); 1233 io[3] ^= CAMELLIA_SUBKEY_R(24);
1300 1234
1301 t0 = io[0]; 1235 io_text[0] = cpu_to_be32(io[2]);
1302 t1 = io[1]; 1236 io_text[1] = cpu_to_be32(io[3]);
1303 io[0] = io[2]; 1237 io_text[2] = cpu_to_be32(io[0]);
1304 io[1] = io[3]; 1238 io_text[3] = cpu_to_be32(io[1]);
1305 io[2] = t0;
1306 io[3] = t1;
1307
1308 io_text[0] = cpu_to_be32(io[0]);
1309 io_text[1] = cpu_to_be32(io[1]);
1310 io_text[2] = cpu_to_be32(io[2]);
1311 io_text[3] = cpu_to_be32(io[3]);
1312
1313 return;
1314} 1239}
1315 1240
1316static void camellia_decrypt128(const u32 *subkey, __be32 *io_text) 1241static void camellia_decrypt128(const u32 *subkey, __be32 *io_text)
@@ -1324,7 +1249,7 @@ static void camellia_decrypt128(const u32 *subkey, __be32 *io_text)
1324 io[2] = be32_to_cpu(io_text[2]); 1249 io[2] = be32_to_cpu(io_text[2]);
1325 io[3] = be32_to_cpu(io_text[3]); 1250 io[3] = be32_to_cpu(io_text[3]);
1326 1251
1327 /* pre whitening but absorb kw2*/ 1252 /* pre whitening but absorb kw2 */
1328 io[0] ^= CAMELLIA_SUBKEY_L(24); 1253 io[0] ^= CAMELLIA_SUBKEY_L(24);
1329 io[1] ^= CAMELLIA_SUBKEY_R(24); 1254 io[1] ^= CAMELLIA_SUBKEY_R(24);
1330 1255
@@ -1400,25 +1325,12 @@ static void camellia_decrypt128(const u32 *subkey, __be32 *io_text)
1400 io[2] ^= CAMELLIA_SUBKEY_L(0); 1325 io[2] ^= CAMELLIA_SUBKEY_L(0);
1401 io[3] ^= CAMELLIA_SUBKEY_R(0); 1326 io[3] ^= CAMELLIA_SUBKEY_R(0);
1402 1327
1403 t0 = io[0]; 1328 io_text[0] = cpu_to_be32(io[2]);
1404 t1 = io[1]; 1329 io_text[1] = cpu_to_be32(io[3]);
1405 io[0] = io[2]; 1330 io_text[2] = cpu_to_be32(io[0]);
1406 io[1] = io[3]; 1331 io_text[3] = cpu_to_be32(io[1]);
1407 io[2] = t0;
1408 io[3] = t1;
1409
1410 io_text[0] = cpu_to_be32(io[0]);
1411 io_text[1] = cpu_to_be32(io[1]);
1412 io_text[2] = cpu_to_be32(io[2]);
1413 io_text[3] = cpu_to_be32(io[3]);
1414
1415 return;
1416} 1332}
1417 1333
1418
1419/**
1420 * stuff for 192 and 256bit encryption/decryption
1421 */
1422static void camellia_encrypt256(const u32 *subkey, __be32 *io_text) 1334static void camellia_encrypt256(const u32 *subkey, __be32 *io_text)
1423{ 1335{
1424 u32 il,ir,t0,t1; /* temporary valiables */ 1336 u32 il,ir,t0,t1; /* temporary valiables */
@@ -1430,7 +1342,7 @@ static void camellia_encrypt256(const u32 *subkey, __be32 *io_text)
1430 io[2] = be32_to_cpu(io_text[2]); 1342 io[2] = be32_to_cpu(io_text[2]);
1431 io[3] = be32_to_cpu(io_text[3]); 1343 io[3] = be32_to_cpu(io_text[3]);
1432 1344
1433 /* pre whitening but absorb kw2*/ 1345 /* pre whitening but absorb kw2 */
1434 io[0] ^= CAMELLIA_SUBKEY_L(0); 1346 io[0] ^= CAMELLIA_SUBKEY_L(0);
1435 io[1] ^= CAMELLIA_SUBKEY_R(0); 1347 io[1] ^= CAMELLIA_SUBKEY_R(0);
1436 1348
@@ -1530,22 +1442,12 @@ static void camellia_encrypt256(const u32 *subkey, __be32 *io_text)
1530 io[2] ^= CAMELLIA_SUBKEY_L(32); 1442 io[2] ^= CAMELLIA_SUBKEY_L(32);
1531 io[3] ^= CAMELLIA_SUBKEY_R(32); 1443 io[3] ^= CAMELLIA_SUBKEY_R(32);
1532 1444
1533 t0 = io[0]; 1445 io_text[0] = cpu_to_be32(io[2]);
1534 t1 = io[1]; 1446 io_text[1] = cpu_to_be32(io[3]);
1535 io[0] = io[2]; 1447 io_text[2] = cpu_to_be32(io[0]);
1536 io[1] = io[3]; 1448 io_text[3] = cpu_to_be32(io[1]);
1537 io[2] = t0;
1538 io[3] = t1;
1539
1540 io_text[0] = cpu_to_be32(io[0]);
1541 io_text[1] = cpu_to_be32(io[1]);
1542 io_text[2] = cpu_to_be32(io[2]);
1543 io_text[3] = cpu_to_be32(io[3]);
1544
1545 return;
1546} 1449}
1547 1450
1548
1549static void camellia_decrypt256(const u32 *subkey, __be32 *io_text) 1451static void camellia_decrypt256(const u32 *subkey, __be32 *io_text)
1550{ 1452{
1551 u32 il,ir,t0,t1; /* temporary valiables */ 1453 u32 il,ir,t0,t1; /* temporary valiables */
@@ -1557,7 +1459,7 @@ static void camellia_decrypt256(const u32 *subkey, __be32 *io_text)
1557 io[2] = be32_to_cpu(io_text[2]); 1459 io[2] = be32_to_cpu(io_text[2]);
1558 io[3] = be32_to_cpu(io_text[3]); 1460 io[3] = be32_to_cpu(io_text[3]);
1559 1461
1560 /* pre whitening but absorb kw2*/ 1462 /* pre whitening but absorb kw2 */
1561 io[0] ^= CAMELLIA_SUBKEY_L(32); 1463 io[0] ^= CAMELLIA_SUBKEY_L(32);
1562 io[1] ^= CAMELLIA_SUBKEY_R(32); 1464 io[1] ^= CAMELLIA_SUBKEY_R(32);
1563 1465
@@ -1657,22 +1559,18 @@ static void camellia_decrypt256(const u32 *subkey, __be32 *io_text)
1657 io[2] ^= CAMELLIA_SUBKEY_L(0); 1559 io[2] ^= CAMELLIA_SUBKEY_L(0);
1658 io[3] ^= CAMELLIA_SUBKEY_R(0); 1560 io[3] ^= CAMELLIA_SUBKEY_R(0);
1659 1561
1660 t0 = io[0]; 1562 io_text[0] = cpu_to_be32(io[2]);
1661 t1 = io[1]; 1563 io_text[1] = cpu_to_be32(io[3]);
1662 io[0] = io[2]; 1564 io_text[2] = cpu_to_be32(io[0]);
1663 io[1] = io[3]; 1565 io_text[3] = cpu_to_be32(io[1]);
1664 io[2] = t0;
1665 io[3] = t1;
1666
1667 io_text[0] = cpu_to_be32(io[0]);
1668 io_text[1] = cpu_to_be32(io[1]);
1669 io_text[2] = cpu_to_be32(io[2]);
1670 io_text[3] = cpu_to_be32(io[3]);
1671
1672 return;
1673} 1566}
1674 1567
1675 1568
1569struct camellia_ctx {
1570 int key_length;
1571 u32 key_table[CAMELLIA_TABLE_BYTE_LEN / 4];
1572};
1573
1676static int 1574static int
1677camellia_set_key(struct crypto_tfm *tfm, const u8 *in_key, 1575camellia_set_key(struct crypto_tfm *tfm, const u8 *in_key,
1678 unsigned int key_len) 1576 unsigned int key_len)
@@ -1688,7 +1586,7 @@ camellia_set_key(struct crypto_tfm *tfm, const u8 *in_key,
1688 1586
1689 cctx->key_length = key_len; 1587 cctx->key_length = key_len;
1690 1588
1691 switch(key_len) { 1589 switch (key_len) {
1692 case 16: 1590 case 16:
1693 camellia_setup128(key, cctx->key_table); 1591 camellia_setup128(key, cctx->key_table);
1694 break; 1592 break;
@@ -1698,14 +1596,11 @@ camellia_set_key(struct crypto_tfm *tfm, const u8 *in_key,
1698 case 32: 1596 case 32:
1699 camellia_setup256(key, cctx->key_table); 1597 camellia_setup256(key, cctx->key_table);
1700 break; 1598 break;
1701 default:
1702 break;
1703 } 1599 }
1704 1600
1705 return 0; 1601 return 0;
1706} 1602}
1707 1603
1708
1709static void camellia_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) 1604static void camellia_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1710{ 1605{
1711 const struct camellia_ctx *cctx = crypto_tfm_ctx(tfm); 1606 const struct camellia_ctx *cctx = crypto_tfm_ctx(tfm);
@@ -1725,14 +1620,11 @@ static void camellia_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1725 case 32: 1620 case 32:
1726 camellia_encrypt256(cctx->key_table, tmp); 1621 camellia_encrypt256(cctx->key_table, tmp);
1727 break; 1622 break;
1728 default:
1729 break;
1730 } 1623 }
1731 1624
1732 memcpy(dst, tmp, CAMELLIA_BLOCK_SIZE); 1625 memcpy(dst, tmp, CAMELLIA_BLOCK_SIZE);
1733} 1626}
1734 1627
1735
1736static void camellia_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) 1628static void camellia_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1737{ 1629{
1738 const struct camellia_ctx *cctx = crypto_tfm_ctx(tfm); 1630 const struct camellia_ctx *cctx = crypto_tfm_ctx(tfm);
@@ -1752,14 +1644,11 @@ static void camellia_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1752 case 32: 1644 case 32:
1753 camellia_decrypt256(cctx->key_table, tmp); 1645 camellia_decrypt256(cctx->key_table, tmp);
1754 break; 1646 break;
1755 default:
1756 break;
1757 } 1647 }
1758 1648
1759 memcpy(dst, tmp, CAMELLIA_BLOCK_SIZE); 1649 memcpy(dst, tmp, CAMELLIA_BLOCK_SIZE);
1760} 1650}
1761 1651
1762
1763static struct crypto_alg camellia_alg = { 1652static struct crypto_alg camellia_alg = {
1764 .cra_name = "camellia", 1653 .cra_name = "camellia",
1765 .cra_driver_name = "camellia-generic", 1654 .cra_driver_name = "camellia-generic",
@@ -1786,16 +1675,13 @@ static int __init camellia_init(void)
1786 return crypto_register_alg(&camellia_alg); 1675 return crypto_register_alg(&camellia_alg);
1787} 1676}
1788 1677
1789
1790static void __exit camellia_fini(void) 1678static void __exit camellia_fini(void)
1791{ 1679{
1792 crypto_unregister_alg(&camellia_alg); 1680 crypto_unregister_alg(&camellia_alg);
1793} 1681}
1794 1682
1795
1796module_init(camellia_init); 1683module_init(camellia_init);
1797module_exit(camellia_fini); 1684module_exit(camellia_fini);
1798 1685
1799
1800MODULE_DESCRIPTION("Camellia Cipher Algorithm"); 1686MODULE_DESCRIPTION("Camellia Cipher Algorithm");
1801MODULE_LICENSE("GPL"); 1687MODULE_LICENSE("GPL");