/src/nettle-with-mini-gmp/camellia256-set-encrypt-key.c
Line  | Count  | Source  | 
1  |  | /* camellia256-set-encrypt-key.c  | 
2  |  |  | 
3  |  |    Key setup for the camellia block cipher.  | 
4  |  |  | 
5  |  |    Copyright (C) 2006,2007 NTT  | 
6  |  |    (Nippon Telegraph and Telephone Corporation).  | 
7  |  |  | 
8  |  |    Copyright (C) 2010, 2013 Niels Möller  | 
9  |  |  | 
10  |  |    This file is part of GNU Nettle.  | 
11  |  |  | 
12  |  |    GNU Nettle is free software: you can redistribute it and/or  | 
13  |  |    modify it under the terms of either:  | 
14  |  |  | 
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.  | 
18  |  |  | 
19  |  |    or  | 
20  |  |  | 
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.  | 
24  |  |  | 
25  |  |    or both in parallel, as here.  | 
26  |  |  | 
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.  | 
31  |  |  | 
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/.  | 
35  |  | */  | 
36  |  |  | 
37  |  | /*  | 
38  |  |  * Algorithm Specification   | 
39  |  |  *  http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications.html  | 
40  |  |  */  | 
41  |  |  | 
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.  | 
44  |  |  */  | 
45  |  | #if HAVE_CONFIG_H  | 
46  |  | # include "config.h"  | 
47  |  | #endif  | 
48  |  |  | 
49  |  | #include <assert.h>  | 
50  |  | #include <limits.h>  | 
51  |  |  | 
52  |  | #include "camellia-internal.h"  | 
53  |  |  | 
54  |  | #include "macros.h"  | 
55  |  |  | 
56  |  | static void  | 
57  |  | _camellia256_set_encrypt_key (struct camellia256_ctx *ctx,  | 
58  |  |             uint64_t k0, uint64_t k1,  | 
59  |  |             uint64_t k2, uint64_t k3)  | 
60  | 678  | { | 
61  | 678  |   uint64_t subkey[_CAMELLIA256_NKEYS + 2];  | 
62  | 678  |   uint64_t w;  | 
63  |  |     | 
64  |  |   /* generate KL dependent subkeys */  | 
65  | 678  |   subkey[0] = k0; subkey[1] = k1;  | 
66  | 678  |   ROTL128(45, k0, k1);  | 
67  | 678  |   subkey[12] = k0; subkey[13] = k1;  | 
68  | 678  |   ROTL128(15, k0, k1);  | 
69  | 678  |   subkey[16] = k0; subkey[17] = k1;  | 
70  | 678  |   ROTL128(17, k0, k1);  | 
71  | 678  |   subkey[22] = k0; subkey[23] = k1;  | 
72  | 678  |   ROTL128(34, k0, k1);  | 
73  | 678  |   subkey[30] = k0; subkey[31] = k1;  | 
74  |  |  | 
75  |  |   /* generate KR dependent subkeys */  | 
76  | 678  |   ROTL128(15, k2, k3);  | 
77  | 678  |   subkey[4] = k2; subkey[5] = k3;  | 
78  | 678  |   ROTL128(15, k2, k3);  | 
79  | 678  |   subkey[8] = k2; subkey[9] = k3;  | 
80  | 678  |   ROTL128(30, k2, k3);  | 
81  | 678  |   subkey[18] = k2; subkey[19] = k3;  | 
82  | 678  |   ROTL128(34, k2, k3);  | 
83  | 678  |   subkey[26] = k2; subkey[27] = k3;  | 
84  | 678  |   ROTL128(34, k2, k3);  | 
85  |  |  | 
86  |  |   /* generate KA */  | 
87  |  |   /* The construction of KA is done as  | 
88  |  |  | 
89  |  |      D1 = (KL ^ KR) >> 64  | 
90  |  |      D2 = (KL ^ KR) & MASK64  | 
91  |  |      W = F(D1, SIGMA1)  | 
92  |  |      D2 = D2 ^ W  | 
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)  | 
98  |  |   */  | 
99  |  |  | 
100  | 678  |   k0 = subkey[0] ^ k2;  | 
101  | 678  |   k1 = subkey[1] ^ k3;  | 
102  |  |  | 
103  | 678  |   CAMELLIA_F(k0, SIGMA1, w);  | 
104  | 678  |   k1 ^= w;  | 
105  |  |  | 
106  | 678  |   CAMELLIA_F(k1, SIGMA2, k0);  | 
107  | 678  |   k0 ^= k2;  | 
108  |  |  | 
109  | 678  |   CAMELLIA_F(k0, SIGMA3, k1);  | 
110  | 678  |   k1 ^= w ^ k3;  | 
111  |  |  | 
112  | 678  |   CAMELLIA_F(k1, SIGMA4, w);  | 
113  | 678  |   k0 ^= w;  | 
114  |  |  | 
115  |  |   /* generate KB */  | 
116  | 678  |   k2 ^= k0; k3 ^= k1;  | 
117  | 678  |   CAMELLIA_F(k2, SIGMA5, w);  | 
118  | 678  |   k3 ^= w;  | 
119  | 678  |   CAMELLIA_F(k3, SIGMA6, w);  | 
120  | 678  |   k2 ^= w;  | 
121  |  |  | 
122  |  |   /* generate KA dependent subkeys */  | 
123  | 678  |   ROTL128(15, k0, k1);  | 
124  | 678  |   subkey[6] = k0; subkey[7] = k1;  | 
125  | 678  |   ROTL128(30, k0, k1);  | 
126  | 678  |   subkey[14] = k0; subkey[15] = k1;  | 
127  | 678  |   ROTL128(32, k0, k1);  | 
128  | 678  |   subkey[24] = k0; subkey[25] = k1;  | 
129  | 678  |   ROTL128(17, k0, k1);  | 
130  | 678  |   subkey[28] = k0; subkey[29] = k1;  | 
131  |  |  | 
132  |  |   /* generate KB dependent subkeys */  | 
133  | 678  |   subkey[2] = k2; subkey[3] = k3;  | 
134  | 678  |   ROTL128(30, k2, k3);  | 
135  | 678  |   subkey[10] = k2; subkey[11] = k3;  | 
136  | 678  |   ROTL128(30, k2, k3);  | 
137  | 678  |   subkey[20] = k2; subkey[21] = k3;  | 
138  | 678  |   ROTL128(51, k2, k3);  | 
139  | 678  |   subkey[32] = k2; subkey[33] = k3;  | 
140  |  |  | 
141  |  |   /* Common final processing */  | 
142  | 678  |   _nettle_camellia_absorb (_CAMELLIA256_NKEYS, ctx->keys, subkey);  | 
143  | 678  | }  | 
144  |  |  | 
145  |  | void  | 
146  |  | camellia256_set_encrypt_key(struct camellia256_ctx *ctx,  | 
147  |  |           const uint8_t *key)  | 
148  | 577  | { | 
149  | 577  |   uint64_t k0, k1, k2, k3;  | 
150  | 577  |   k0 = READ_UINT64(key);  | 
151  | 577  |   k1 = READ_UINT64(key +  8);  | 
152  | 577  |   k2 = READ_UINT64(key + 16);  | 
153  | 577  |   k3 = READ_UINT64(key + 24);  | 
154  |  |  | 
155  | 577  |   _camellia256_set_encrypt_key (ctx, k0, k1, k2, k3);  | 
156  | 577  | }  | 
157  |  |  | 
158  |  | void  | 
159  |  | camellia192_set_encrypt_key(struct camellia256_ctx *ctx,  | 
160  |  |           const uint8_t *key)  | 
161  | 101  | { | 
162  | 101  |   uint64_t k0, k1, k2;  | 
163  | 101  |   k0 = READ_UINT64(key);  | 
164  | 101  |   k1 = READ_UINT64(key +  8);  | 
165  | 101  |   k2 = READ_UINT64(key + 16);  | 
166  |  |  | 
167  | 101  |   _camellia256_set_encrypt_key (ctx, k0, k1, k2, ~k2);  | 
168  | 101  | }  |