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