1 /* camellia256-set-encrypt-key.c
3 Key setup for the camellia block cipher.
5 Copyright (C) 2006,2007 NTT
6 (Nippon Telegraph and Telephone Corporation).
8 Copyright (C) 2010, 2013 Niels Möller
10 This file is part of GNU Nettle.
12 GNU Nettle is free software: you can redistribute it and/or
13 modify it under the terms of either:
15 * the GNU Lesser General Public License as published by the Free
16 Software Foundation; either version 3 of the License, or (at your
17 option) any later version.
21 * the GNU General Public License as published by the Free
22 Software Foundation; either version 2 of the License, or (at your
23 option) any later version.
25 or both in parallel, as here.
27 GNU Nettle is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 General Public License for more details.
32 You should have received copies of the GNU General Public License and
33 the GNU Lesser General Public License along with this program. If
34 not, see http://www.gnu.org/licenses/.
38 * Algorithm Specification
39 * http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications.html
42 /* Based on camellia.c ver 1.2.0, see
43 http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/camellia-LGPL-1.2.0.tar.gz.
52 #include "camellia-internal.h"
57 _camellia256_set_encrypt_key (struct camellia256_ctx *ctx,
58 uint64_t k0, uint64_t k1,
59 uint64_t k2, uint64_t k3)
61 uint64_t subkey[_CAMELLIA256_NKEYS + 2];
64 /* generate KL dependent subkeys */
65 subkey[0] = k0; subkey[1] = k1;
67 subkey[12] = k0; subkey[13] = k1;
69 subkey[16] = k0; subkey[17] = k1;
71 subkey[22] = k0; subkey[23] = k1;
73 subkey[30] = k0; subkey[31] = k1;
75 /* generate KR dependent subkeys */
77 subkey[4] = k2; subkey[5] = k3;
79 subkey[8] = k2; subkey[9] = k3;
81 subkey[18] = k2; subkey[19] = k3;
83 subkey[26] = k2; subkey[27] = k3;
87 /* The construction of KA is done as
90 D2 = (KL ^ KR) & MASK64
93 D1 = F(D2, SIGMA2) ^ (KR >> 64)
94 D2 = F(D1, SIGMA3) ^ W ^ (KR & MASK64)
95 D1 = D1 ^ F(W, SIGMA2)
96 D2 = D2 ^ F(D1, SIGMA3)
97 D1 = D1 ^ F(D2, SIGMA4)
103 CAMELLIA_F(k0, SIGMA1, w);
106 CAMELLIA_F(k1, SIGMA2, k0);
109 CAMELLIA_F(k0, SIGMA3, k1);
112 CAMELLIA_F(k1, SIGMA4, w);
117 CAMELLIA_F(k2, SIGMA5, w);
119 CAMELLIA_F(k3, SIGMA6, w);
122 /* generate KA dependent subkeys */
124 subkey[6] = k0; subkey[7] = k1;
126 subkey[14] = k0; subkey[15] = k1;
128 subkey[24] = k0; subkey[25] = k1;
130 subkey[28] = k0; subkey[29] = k1;
132 /* generate KB dependent subkeys */
133 subkey[2] = k2; subkey[3] = k3;
135 subkey[10] = k2; subkey[11] = k3;
137 subkey[20] = k2; subkey[21] = k3;
139 subkey[32] = k2; subkey[33] = k3;
141 /* Common final processing */
142 _camellia_absorb (_CAMELLIA256_NKEYS, ctx->keys, subkey);
146 camellia256_set_encrypt_key(struct camellia256_ctx *ctx,
149 uint64_t k0, k1, k2, k3;
150 k0 = READ_UINT64(key);
151 k1 = READ_UINT64(key + 8);
152 k2 = READ_UINT64(key + 16);
153 k3 = READ_UINT64(key + 24);
155 _camellia256_set_encrypt_key (ctx, k0, k1, k2, k3);
159 camellia192_set_encrypt_key(struct camellia256_ctx *ctx,
163 k0 = READ_UINT64(key);
164 k1 = READ_UINT64(key + 8);
165 k2 = READ_UINT64(key + 16);
167 _camellia256_set_encrypt_key (ctx, k0, k1, k2, ~k2);