1 /* camellia-internal.h
2 
3    The camellia block cipher.
4 
5    Copyright (C) 2006,2007 NTT
6    (Nippon Telegraph and Telephone Corporation).
7 
8    Copyright (C) 2010 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 
46 #ifndef NETTLE_CAMELLIA_INTERNAL_H_INCLUDED
47 #define NETTLE_CAMELLIA_INTERNAL_H_INCLUDED
48 
49 #include "camellia.h"
50 
51 /*
52  *  macros
53  */
54 
55 /* Destructive rotation of 128 bit values. */
56 #define ROTL128(bits, xl, xr) do {		\
57     uint64_t __rol128_t = (xl);			     \
58     (xl) = ((xl) << (bits)) | ((xr) >> (64 - (bits)));	   \
59     (xr) = ((xr) << (bits)) | (__rol128_t >> (64 - (bits)));	\
60   } while (0)
61 
62 struct camellia_table
63 {
64   uint32_t sp1110[256];
65   uint32_t sp0222[256];
66   uint32_t sp3033[256];
67   uint32_t sp4404[256];
68 };
69 
70 /* key constants */
71 
72 #define SIGMA1 0xA09E667F3BCC908BULL
73 #define SIGMA2 0xB67AE8584CAA73B2ULL
74 #define SIGMA3 0xC6EF372FE94F82BEULL
75 #define SIGMA4 0x54FF53A5F1D36F1CULL
76 #define SIGMA5 0x10E527FADE682D1DULL
77 #define SIGMA6 0xB05688C2B3E6C1FDULL
78 
79 #define CAMELLIA_SP1110(INDEX) (_nettle_camellia_table.sp1110[(int)(INDEX)])
80 #define CAMELLIA_SP0222(INDEX) (_nettle_camellia_table.sp0222[(int)(INDEX)])
81 #define CAMELLIA_SP3033(INDEX) (_nettle_camellia_table.sp3033[(int)(INDEX)])
82 #define CAMELLIA_SP4404(INDEX) (_nettle_camellia_table.sp4404[(int)(INDEX)])
83 
84 #define CAMELLIA_F(x, k, y) do {		\
85     uint32_t __yl, __yr;			\
86     uint64_t __i = (x) ^ (k);			\
87     __yl					\
88       = CAMELLIA_SP1110( __i & 0xff)		\
89       ^ CAMELLIA_SP0222((__i >> 24) & 0xff)	\
90       ^ CAMELLIA_SP3033((__i >> 16) & 0xff)	\
91       ^ CAMELLIA_SP4404((__i >> 8) & 0xff);	\
92     __yr					\
93       = CAMELLIA_SP1110( __i >> 56)		\
94       ^ CAMELLIA_SP0222((__i >> 48) & 0xff)	\
95       ^ CAMELLIA_SP3033((__i >> 40) & 0xff)	\
96       ^ CAMELLIA_SP4404((__i >> 32) & 0xff);	\
97     __yl ^= __yr;				\
98     __yr = ROTL32(24, __yr);			\
99     __yr ^= __yl;				\
100     (y) = ((uint64_t) __yl << 32) | __yr;	\
101   } while (0)
102 
103 #if ! HAVE_NATIVE_64_BIT
104 #define CAMELLIA_F_HALF_INV(x) do {            \
105     uint32_t __t, __w;                         \
106     __t = (x) >> 32;                           \
107     __w = __t ^(x);                            \
108     __w = ROTL32(8, __w);                       \
109     (x) = ((uint64_t) __w << 32) | (__t ^ __w);        \
110   } while (0)
111 #endif
112 
113 void
114 _nettle_camellia_crypt(unsigned nkeys, const uint64_t *keys,
115 		       const struct camellia_table *T,
116 		       size_t length, uint8_t *dst,
117 		       const uint8_t *src);
118 
119 /* The initial NKEYS + 2 subkeys in SUBKEY are reduced to the final
120    NKEYS subkeys stored in DST. SUBKEY data is modified in the
121    process. */
122 void
123 _nettle_camellia_absorb(unsigned nkeys, uint64_t *dst, uint64_t *subkey);
124 
125 void
126 _nettle_camellia_invert_key(unsigned nkeys,
127 			    uint64_t *dst, const uint64_t *src);
128 
129 extern const struct camellia_table _nettle_camellia_table;
130 
131 #endif /* NETTLE_CAMELLIA_INTERNAL_H_INCLUDED */
132