Botan 3.6.1
Crypto and TLS for C&
serpent_sbox.h
Go to the documentation of this file.
1/*
2* (C) 1999-2007,2013 Jack Lloyd
3*
4* The sbox expressions used here were discovered by Dag Arne Osvik and
5* are described in his paper "Speeding Up Serpent".
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#ifndef BOTAN_SERPENT_SBOX_FN_H_
11#define BOTAN_SERPENT_SBOX_FN_H_
12
13#include <botan/types.h>
14#include <botan/internal/serpent_fn.h>
15
16namespace Botan::Serpent_F {
17
18template <typename T>
19BOTAN_FORCE_INLINE void SBoxE0(T& a, T& b, T& c, T& d) {
20 d ^= a;
21 T t0 = b;
22 b &= d;
23 t0 ^= c;
24 b ^= a;
25 a |= d;
26 a ^= t0;
27 t0 ^= d;
28 d ^= c;
29 c |= b;
30 c ^= t0;
31 t0 = ~t0;
32 t0 |= b;
33 b ^= d;
34 b ^= t0;
35 d |= a;
36 b ^= d;
37 t0 ^= d;
38 d = a;
39 a = b;
40 b = t0;
41}
42
43template <typename T>
44BOTAN_FORCE_INLINE void SBoxE1(T& a, T& b, T& c, T& d) {
45 a = ~a;
46 c = ~c;
47 T t0 = a;
48 a &= b;
49 c ^= a;
50 a |= d;
51 d ^= c;
52 b ^= a;
53 a ^= t0;
54 t0 |= b;
55 b ^= d;
56 c |= a;
57 c &= t0;
58 a ^= b;
59 b &= c;
60 b ^= a;
61 a &= c;
62 t0 ^= a;
63 a = c;
64 c = d;
65 d = b;
66 b = t0;
67}
68
69template <typename T>
70BOTAN_FORCE_INLINE void SBoxE2(T& a, T& b, T& c, T& d) {
71 T t0 = a;
72 a &= c;
73 a ^= d;
74 c ^= b;
75 c ^= a;
76 d |= t0;
77 d ^= b;
78 t0 ^= c;
79 b = d;
80 d |= t0;
81 d ^= a;
82 a &= b;
83 t0 ^= a;
84 b ^= d;
85 b ^= t0;
86 a = c;
87 c = b;
88 b = d;
89 d = ~t0;
90}
91
92template <typename T>
93BOTAN_FORCE_INLINE void SBoxE3(T& a, T& b, T& c, T& d) {
94 T t0 = a;
95 a |= d;
96 d ^= b;
97 b &= t0;
98 t0 ^= c;
99 c ^= d;
100 d &= a;
101 t0 |= b;
102 d ^= t0;
103 a ^= b;
104 t0 &= a;
105 b ^= d;
106 t0 ^= c;
107 b |= a;
108 b ^= c;
109 a ^= d;
110 c = b;
111 b |= d;
112 a ^= b;
113 b = c;
114 c = d;
115 d = t0;
116}
117
118template <typename T>
119BOTAN_FORCE_INLINE void SBoxE4(T& a, T& b, T& c, T& d) {
120 b ^= d;
121 d = ~d;
122 c ^= d;
123 d ^= a;
124 T t0 = b;
125 b &= d;
126 b ^= c;
127 t0 ^= d;
128 a ^= t0;
129 c &= t0;
130 c ^= a;
131 a &= b;
132 d ^= a;
133 t0 |= b;
134 t0 ^= a;
135 a |= d;
136 a ^= c;
137 c &= d;
138 a = ~a;
139 t0 ^= c;
140 c = a;
141 a = b;
142 b = t0;
143}
144
145template <typename T>
146BOTAN_FORCE_INLINE void SBoxE5(T& a, T& b, T& c, T& d) {
147 a ^= b;
148 b ^= d;
149 d = ~d;
150 T t0 = b;
151 b &= a;
152 c ^= d;
153 b ^= c;
154 c |= t0;
155 t0 ^= d;
156 d &= b;
157 d ^= a;
158 t0 ^= b;
159 t0 ^= c;
160 c ^= a;
161 a &= d;
162 c = ~c;
163 a ^= t0;
164 t0 |= d;
165 t0 ^= c;
166 c = a;
167 a = b;
168 b = d;
169 d = t0;
170}
171
172template <typename T>
173BOTAN_FORCE_INLINE void SBoxE6(T& a, T& b, T& c, T& d) {
174 c = ~c;
175 T t0 = d;
176 d &= a;
177 a ^= t0;
178 d ^= c;
179 c |= t0;
180 b ^= d;
181 c ^= a;
182 a |= b;
183 c ^= b;
184 t0 ^= a;
185 a |= d;
186 a ^= c;
187 t0 ^= d;
188 t0 ^= a;
189 d = ~d;
190 c &= t0;
191 d ^= c;
192 c = t0;
193}
194
195template <typename T>
196BOTAN_FORCE_INLINE void SBoxE7(T& a, T& b, T& c, T& d) {
197 T t0 = b;
198 b |= c;
199 b ^= d;
200 t0 ^= c;
201 c ^= b;
202 d |= t0;
203 d &= a;
204 t0 ^= c;
205 d ^= b;
206 b |= t0;
207 b ^= a;
208 a |= t0;
209 a ^= c;
210 b ^= t0;
211 c ^= b;
212 b &= a;
213 b ^= t0;
214 c = ~c;
215 c |= a;
216 t0 ^= c;
217 c = b;
218 b = d;
219 d = a;
220 a = t0;
221}
222
223template <typename T>
224BOTAN_FORCE_INLINE void SBoxD0(T& a, T& b, T& c, T& d) {
225 c = ~c;
226 T t0 = b;
227 b |= a;
228 t0 = ~t0;
229 b ^= c;
230 c |= t0;
231 b ^= d;
232 a ^= t0;
233 c ^= a;
234 a &= d;
235 t0 ^= a;
236 a |= b;
237 a ^= c;
238 d ^= t0;
239 c ^= b;
240 d ^= a;
241 d ^= b;
242 c &= d;
243 t0 ^= c;
244 c = b;
245 b = t0;
246}
247
248template <typename T>
249BOTAN_FORCE_INLINE void SBoxD1(T& a, T& b, T& c, T& d) {
250 T t0 = b;
251 b ^= d;
252 d &= b;
253 t0 ^= c;
254 d ^= a;
255 a |= b;
256 c ^= d;
257 a ^= t0;
258 a |= c;
259 b ^= d;
260 a ^= b;
261 b |= d;
262 b ^= a;
263 t0 = ~t0;
264 t0 ^= b;
265 b |= a;
266 b ^= a;
267 b |= t0;
268 d ^= b;
269 b = a;
270 a = t0;
271 t0 = c;
272 c = d;
273 d = t0;
274}
275
276template <typename T>
277BOTAN_FORCE_INLINE void SBoxD2(T& a, T& b, T& c, T& d) {
278 c ^= d;
279 d ^= a;
280 T t0 = d;
281 d &= c;
282 d ^= b;
283 b |= c;
284 b ^= t0;
285 t0 &= d;
286 c ^= d;
287 t0 &= a;
288 t0 ^= c;
289 c &= b;
290 c |= a;
291 d = ~d;
292 c ^= d;
293 a ^= d;
294 a &= b;
295 d ^= t0;
296 d ^= a;
297 a = b;
298 b = t0;
299}
300
301template <typename T>
302BOTAN_FORCE_INLINE void SBoxD3(T& a, T& b, T& c, T& d) {
303 T t0 = c;
304 c ^= b;
305 a ^= c;
306 t0 &= c;
307 t0 ^= a;
308 a &= b;
309 b ^= d;
310 d |= t0;
311 c ^= d;
312 a ^= d;
313 b ^= t0;
314 d &= c;
315 d ^= b;
316 b ^= a;
317 b |= c;
318 a ^= d;
319 b ^= t0;
320 a ^= b;
321 t0 = a;
322 a = c;
323 c = d;
324 d = t0;
325}
326
327template <typename T>
328BOTAN_FORCE_INLINE void SBoxD4(T& a, T& b, T& c, T& d) {
329 T t0 = c;
330 c &= d;
331 c ^= b;
332 b |= d;
333 b &= a;
334 t0 ^= c;
335 t0 ^= b;
336 b &= c;
337 a = ~a;
338 d ^= t0;
339 b ^= d;
340 d &= a;
341 d ^= c;
342 a ^= b;
343 c &= a;
344 d ^= a;
345 c ^= t0;
346 c |= d;
347 d ^= a;
348 c ^= b;
349 b = d;
350 d = t0;
351}
352
353template <typename T>
354BOTAN_FORCE_INLINE void SBoxD5(T& a, T& b, T& c, T& d) {
355 b = ~b;
356 T t0 = d;
357 c ^= b;
358 d |= a;
359 d ^= c;
360 c |= b;
361 c &= a;
362 t0 ^= d;
363 c ^= t0;
364 t0 |= a;
365 t0 ^= b;
366 b &= c;
367 b ^= d;
368 t0 ^= c;
369 d &= t0;
370 t0 ^= b;
371 d ^= t0;
372 t0 = ~t0;
373 d ^= a;
374 a = b;
375 b = t0;
376 t0 = d;
377 d = c;
378 c = t0;
379}
380
381template <typename T>
382BOTAN_FORCE_INLINE void SBoxD6(T& a, T& b, T& c, T& d) {
383 a ^= c;
384 T t0 = c;
385 c &= a;
386 t0 ^= d;
387 c = ~c;
388 d ^= b;
389 c ^= d;
390 t0 |= a;
391 a ^= c;
392 d ^= t0;
393 t0 ^= b;
394 b &= d;
395 b ^= a;
396 a ^= d;
397 a |= c;
398 d ^= b;
399 t0 ^= a;
400 a = b;
401 b = c;
402 c = t0;
403}
404
405template <typename T>
406BOTAN_FORCE_INLINE void SBoxD7(T& a, T& b, T& c, T& d) {
407 T t0 = c;
408 c ^= a;
409 a &= d;
410 t0 |= d;
411 c = ~c;
412 d ^= b;
413 b |= a;
414 a ^= c;
415 c &= t0;
416 d &= t0;
417 b ^= c;
418 c ^= a;
419 a |= c;
420 t0 ^= b;
421 a ^= d;
422 d ^= t0;
423 t0 |= a;
424 d ^= c;
425 t0 ^= c;
426 c = b;
427 b = a;
428 a = d;
429 d = t0;
430}
431
432} // namespace Botan::Serpent_F
433
434#endif
#define BOTAN_FORCE_INLINE
Definition compiler.h:165
FE_25519 T
Definition ge.cpp:34
BOTAN_FORCE_INLINE void SBoxD5(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxD4(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxE0(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxE1(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxE3(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxD0(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxE5(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxD6(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxD3(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxD2(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxE6(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxE2(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxE4(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxD1(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxE7(T &a, T &b, T &c, T &d)
BOTAN_FORCE_INLINE void SBoxD7(T &a, T &b, T &c, T &d)
const SIMD_8x32 & b