1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/*
* Noekeon
* (C) 1999-2008 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/noekeon.h>
#include <botan/loadstor.h>
#include <botan/cpuid.h>
namespace Botan {
namespace {
/*
* Noekeon's Theta Operation
*/
inline void theta(u32bit& A0, u32bit& A1,
u32bit& A2, u32bit& A3,
const u32bit EK[4])
{
u32bit T = A0 ^ A2;
T ^= rotate_left(T, 8) ^ rotate_right(T, 8);
A1 ^= T;
A3 ^= T;
A0 ^= EK[0];
A1 ^= EK[1];
A2 ^= EK[2];
A3 ^= EK[3];
T = A1 ^ A3;
T ^= rotate_left(T, 8) ^ rotate_right(T, 8);
A0 ^= T;
A2 ^= T;
}
/*
* Theta With Null Key
*/
inline void theta(u32bit& A0, u32bit& A1,
u32bit& A2, u32bit& A3)
{
u32bit T = A0 ^ A2;
T ^= rotate_left(T, 8) ^ rotate_right(T, 8);
A1 ^= T;
A3 ^= T;
T = A1 ^ A3;
T ^= rotate_left(T, 8) ^ rotate_right(T, 8);
A0 ^= T;
A2 ^= T;
}
/*
* Noekeon's Gamma S-Box Layer
*/
inline void gamma(u32bit& A0, u32bit& A1, u32bit& A2, u32bit& A3)
{
A1 ^= ~A3 & ~A2;
A0 ^= A2 & A1;
u32bit T = A3;
A3 = A0;
A0 = T;
A2 ^= A0 ^ A1 ^ A3;
A1 ^= ~A3 & ~A2;
A0 ^= A2 & A1;
}
}
/*
* Noekeon Round Constants
*/
const byte Noekeon::RC[] = {
0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A,
0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A,
0xD4 };
/*
* Noekeon Encryption
*/
void Noekeon::encrypt_n(const byte in[], byte out[], size_t blocks) const
{
#if defined(BOTAN_HAS_NOEKEON_SIMD)
if(CPUID::has_simd_32())
{
while(blocks >= 4)
{
simd_encrypt_4(in, out);
in += 4 * BLOCK_SIZE;
out += 4 * BLOCK_SIZE;
blocks -= 4;
}
}
#endif
for(size_t i = 0; i != blocks; ++i)
{
u32bit A0 = load_be<u32bit>(in, 0);
u32bit A1 = load_be<u32bit>(in, 1);
u32bit A2 = load_be<u32bit>(in, 2);
u32bit A3 = load_be<u32bit>(in, 3);
for(size_t j = 0; j != 16; ++j)
{
A0 ^= RC[j];
theta(A0, A1, A2, A3, m_EK.data());
A1 = rotate_left(A1, 1);
A2 = rotate_left(A2, 5);
A3 = rotate_left(A3, 2);
gamma(A0, A1, A2, A3);
A1 = rotate_right(A1, 1);
A2 = rotate_right(A2, 5);
A3 = rotate_right(A3, 2);
}
A0 ^= RC[16];
theta(A0, A1, A2, A3, m_EK.data());
store_be(out, A0, A1, A2, A3);
in += BLOCK_SIZE;
out += BLOCK_SIZE;
}
}
/*
* Noekeon Encryption
*/
void Noekeon::decrypt_n(const byte in[], byte out[], size_t blocks) const
{
#if defined(BOTAN_HAS_NOEKEON_SIMD)
if(CPUID::has_simd_32())
{
/*
const size_t blocks4 = blocks / 4;
const size_t blocks_left = blocks % 4;
in += blocks4 * BLOCK_SIZE;
out += blocks4 * BLOCK_SIZE;
blocks = blocks % 4;
BOTAN_PARALLEL_FOR(size_t i = 0; i < blocks4; ++i)
{
simd_encrypt_4(in + i*4*BLOCK_SIZE, out + i*4*BLOCK_SIZE);
}
*/
while(blocks >= 4)
{
simd_decrypt_4(in, out);
in += 4 * BLOCK_SIZE;
out += 4 * BLOCK_SIZE;
blocks -= 4;
}
}
#endif
for(size_t i = 0; i != blocks; ++i)
{
u32bit A0 = load_be<u32bit>(in, 0);
u32bit A1 = load_be<u32bit>(in, 1);
u32bit A2 = load_be<u32bit>(in, 2);
u32bit A3 = load_be<u32bit>(in, 3);
for(size_t j = 16; j != 0; --j)
{
theta(A0, A1, A2, A3, m_DK.data());
A0 ^= RC[j];
A1 = rotate_left(A1, 1);
A2 = rotate_left(A2, 5);
A3 = rotate_left(A3, 2);
gamma(A0, A1, A2, A3);
A1 = rotate_right(A1, 1);
A2 = rotate_right(A2, 5);
A3 = rotate_right(A3, 2);
}
theta(A0, A1, A2, A3, m_DK.data());
A0 ^= RC[0];
store_be(out, A0, A1, A2, A3);
in += BLOCK_SIZE;
out += BLOCK_SIZE;
}
}
/*
* Noekeon Key Schedule
*/
void Noekeon::key_schedule(const byte key[], size_t)
{
u32bit A0 = load_be<u32bit>(key, 0);
u32bit A1 = load_be<u32bit>(key, 1);
u32bit A2 = load_be<u32bit>(key, 2);
u32bit A3 = load_be<u32bit>(key, 3);
for(size_t i = 0; i != 16; ++i)
{
A0 ^= RC[i];
theta(A0, A1, A2, A3);
A1 = rotate_left(A1, 1);
A2 = rotate_left(A2, 5);
A3 = rotate_left(A3, 2);
gamma(A0, A1, A2, A3);
A1 = rotate_right(A1, 1);
A2 = rotate_right(A2, 5);
A3 = rotate_right(A3, 2);
}
A0 ^= RC[16];
m_DK.resize(4);
m_DK[0] = A0;
m_DK[1] = A1;
m_DK[2] = A2;
m_DK[3] = A3;
theta(A0, A1, A2, A3);
m_EK.resize(4);
m_EK[0] = A0;
m_EK[1] = A1;
m_EK[2] = A2;
m_EK[3] = A3;
}
/*
* Clear memory of sensitive data
*/
void Noekeon::clear()
{
zap(m_EK);
zap(m_DK);
}
}
|