1 /* camellia-set-encrypt-key.c
3 * Key setup for the camellia block cipher.
6 * Copyright (C) 2006,2007
7 * NTT (Nippon Telegraph and Telephone Corporation).
9 * Copyright (C) 2010 Niels Möller
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * Algorithm Specification
28 * http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications.html
31 /* Based on camellia.c ver 1.2.0, see
32 http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/camellia-LGPL-1.2.0.tar.gz.
40 #include "camellia-internal.h"
46 #define SIGMA1 0xA09E667F3BCC908BULL
47 #define SIGMA2 0xB67AE8584CAA73B2ULL
48 #define SIGMA3 0xC6EF372FE94F82BEULL
49 #define SIGMA4 0x54FF53A5F1D36F1CULL
50 #define SIGMA5 0x10E527FADE682D1DULL
51 #define SIGMA6 0xB05688C2B3E6C1FDULL
53 #define CAMELLIA_SP1110(INDEX) (_nettle_camellia_table.sp1110[(int)(INDEX)])
54 #define CAMELLIA_SP0222(INDEX) (_nettle_camellia_table.sp0222[(int)(INDEX)])
55 #define CAMELLIA_SP3033(INDEX) (_nettle_camellia_table.sp3033[(int)(INDEX)])
56 #define CAMELLIA_SP4404(INDEX) (_nettle_camellia_table.sp4404[(int)(INDEX)])
58 #define CAMELLIA_F(x, k, y) do { \
59 uint32_t __yl, __yr; \
60 uint64_t __i = (x) ^ (k); \
62 = CAMELLIA_SP1110( __i & 0xff) \
63 ^ CAMELLIA_SP0222((__i >> 24) & 0xff) \
64 ^ CAMELLIA_SP3033((__i >> 16) & 0xff) \
65 ^ CAMELLIA_SP4404((__i >> 8) & 0xff); \
67 = CAMELLIA_SP1110( __i >> 56) \
68 ^ CAMELLIA_SP0222((__i >> 48) & 0xff) \
69 ^ CAMELLIA_SP3033((__i >> 40) & 0xff) \
70 ^ CAMELLIA_SP4404((__i >> 32) & 0xff); \
72 __yr = ROL32(24, __yr); \
74 (y) = ((uint64_t) __yl << 32) | __yr; \
77 #define CAMELLIA_F_HALF_INV(x) do { \
81 __w = ROL32(8, __w); \
82 (x) = ((uint64_t) __w << 32) | (__t ^ __w); \
86 camellia_set_encrypt_key(struct camellia_ctx *ctx,
87 unsigned length, const uint8_t *key)
97 k0 = READ_UINT64(key);
98 k1 = READ_UINT64(key + 8);
104 * generate KL dependent subkeys
106 subkey[0] = k0; subkey[1] = k1;
108 subkey[4] = k0; subkey[5] = k1;
110 subkey[10] = k0; subkey[11] = k1;
114 subkey[16] = k0; subkey[17] = k1;
116 subkey[18] = k0; subkey[19] = k1;
118 subkey[22] = k0; subkey[23] = k1;
120 /* generate KA. D1 is k0, d2 is k1. */
121 /* FIXME: Make notation match the spec better. */
122 /* For the 128-bit case, KR = 0, the construction of KA reduces to:
129 D2 = D2 ^ F(D1, Sigma3);
130 D1 = D1 ^ F(D2, Sigma4);
131 KA = (D1 << 64) | D2;
133 k0 = subkey[0]; w = subkey[1];
134 CAMELLIA_F(k0, SIGMA1, k1);
136 CAMELLIA_F(w, SIGMA2, k0);
137 CAMELLIA_F(k0, SIGMA3, w);
139 CAMELLIA_F(k1, SIGMA4, w);
142 /* generate KA dependent subkeys */
143 subkey[2] = k0; subkey[3] = k1;
145 subkey[6] = k0; subkey[7] = k1;
147 subkey[8] = k0; subkey[9] = k1;
151 subkey[14] = k0; subkey[15] = k1;
153 subkey[20] = k0; subkey[21] = k1;
155 subkey[24] = k0; subkey[25] = k1;
162 k2 = READ_UINT64(key + 16);
168 assert (length == 32);
169 k3 = READ_UINT64(key + 24);
171 /* generate KL dependent subkeys */
172 subkey[0] = k0; subkey[1] = k1;
174 subkey[12] = k0; subkey[13] = k1;
176 subkey[16] = k0; subkey[17] = k1;
178 subkey[22] = k0; subkey[23] = k1;
180 subkey[30] = k0; subkey[31] = k1;
182 /* generate KR dependent subkeys */
184 subkey[4] = k2; subkey[5] = k3;
186 subkey[8] = k2; subkey[9] = k3;
188 subkey[18] = k2; subkey[19] = k3;
190 subkey[26] = k2; subkey[27] = k3;
194 /* The construction of KA is done as
197 D2 = (KL ^ KR) & MASK64
200 D1 = F(D2, SIGMA2) ^ (KR >> 64)
201 D2 = F(D1, SIGMA3) ^ W ^ (KR & MASK64)
202 D1 = D1 ^ F(W, SIGMA2)
203 D2 = D2 ^ F(D1, SIGMA3)
204 D1 = D1 ^ F(D2, SIGMA4)
210 CAMELLIA_F(k0, SIGMA1, w);
213 CAMELLIA_F(k1, SIGMA2, k0);
216 CAMELLIA_F(k0, SIGMA3, k1);
219 CAMELLIA_F(k1, SIGMA4, w);
224 CAMELLIA_F(k2, SIGMA5, w);
226 CAMELLIA_F(k3, SIGMA6, w);
229 /* generate KA dependent subkeys */
231 subkey[6] = k0; subkey[7] = k1;
233 subkey[14] = k0; subkey[15] = k1;
235 subkey[24] = k0; subkey[25] = k1;
237 subkey[28] = k0; subkey[29] = k1;
239 /* generate KB dependent subkeys */
240 subkey[2] = k2; subkey[3] = k3;
242 subkey[10] = k2; subkey[11] = k3;
244 subkey[20] = k2; subkey[21] = k3;
246 subkey[32] = k2; subkey[33] = k3;
249 /* At this point, the subkey array contains the subkeys as described
250 in the spec, 26 for short keys and 34 for large keys. */
252 /* absorb kw2 to other subkeys */
258 for (i = 8; i < ctx->nkeys; i += 8)
260 /* FIXME: gcc for x86_32 is smart enough to fetch the 32 low bits
261 and xor the result into the 32 high bits, but it still generates
262 worse code than for explicit 32-bit operations. */
263 kw2 ^= (kw2 & ~subkey[i+1]) << 32;
264 dw = (kw2 & subkey[i+1]) >> 32; kw2 ^= ROL32(1, dw);
272 /* absorb kw4 to other subkeys */
273 kw4 = subkey[ctx->nkeys + 1];
275 for (i = ctx->nkeys - 8; i > 0; i -= 8)
280 kw4 ^= (kw4 & ~subkey[i]) << 32;
281 dw = (kw4 & subkey[i]) >> 32; kw4 ^= ROL32(1, dw);
289 /* key XOR is end of F-function */
290 ctx->keys[0] = subkey[0] ^ subkey[2];
291 ctx->keys[1] = subkey[3];
293 ctx->keys[2] = subkey[2] ^ subkey[4];
294 ctx->keys[3] = subkey[3] ^ subkey[5];
295 ctx->keys[4] = subkey[4] ^ subkey[6];
296 ctx->keys[5] = subkey[5] ^ subkey[7];
298 for (i = 8; i < ctx->nkeys; i += 8)
300 tl = (subkey[i+2] >> 32) ^ (subkey[i+2] & ~subkey[i]);
301 dw = tl & (subkey[i] >> 32);
302 tr = subkey[i+2] ^ ROL32(1, dw);
303 ctx->keys[i-2] = subkey[i-2] ^ ( ((uint64_t) tl << 32) | tr);
305 ctx->keys[i-1] = subkey[i];
306 ctx->keys[i] = subkey[i+1];
308 tl = (subkey[i-1] >> 32) ^ (subkey[i-1] & ~subkey[i+1]);
309 dw = tl & (subkey[i+1] >> 32);
310 tr = subkey[i-1] ^ ROL32(1, dw);
311 ctx->keys[i+1] = subkey[i+3] ^ ( ((uint64_t) tl << 32) | tr);
313 ctx->keys[i+2] = subkey[i+2] ^ subkey[i+4];
314 ctx->keys[i+3] = subkey[i+3] ^ subkey[i+5];
315 ctx->keys[i+4] = subkey[i+4] ^ subkey[i+6];
316 ctx->keys[i+5] = subkey[i+5] ^ subkey[i+7];
318 ctx->keys[i-2] = subkey[i-2];
319 ctx->keys[i-1] = subkey[i] ^ subkey[i-1];
321 for (i = 0; i < ctx->nkeys; i += 8)
323 /* apply the inverse of the last half of F-function */
324 CAMELLIA_F_HALF_INV(ctx->keys[i+1]);
325 CAMELLIA_F_HALF_INV(ctx->keys[i+2]);
326 CAMELLIA_F_HALF_INV(ctx->keys[i+3]);
327 CAMELLIA_F_HALF_INV(ctx->keys[i+4]);
328 CAMELLIA_F_HALF_INV(ctx->keys[i+5]);
329 CAMELLIA_F_HALF_INV(ctx->keys[i+6]);