serpent.c 18.7 KB
Newer Older
Simon Josefsson's avatar
Simon Josefsson committed
1
/* serpent.c
2
 *
3
 * The serpent block cipher.
4
5
6
7
8
 *
 * For more details on this algorithm, see the Serpent website at
 * http://www.cl.cam.ac.uk/~rja14/serpent.html
 */

9
10
/* nettle, low-level cryptographics library
 *
11
 * Copyright (C) 2011  Niels Möller
Simon Josefsson's avatar
Simon Josefsson committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * Copyright (C) 2010, 2011  Simon Josefsson
 * Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
 *  
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * The nettle library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
29
30
 */

Simon Josefsson's avatar
Simon Josefsson committed
31
32
33
34
35
36
37
38
39
/* This file is derived from cipher/serpent.c in Libgcrypt v1.4.6.
   The adaption to Nettle was made by Simon Josefsson on 2010-12-07
   with final touches on 2011-05-30.  Changes include replacing
   libgcrypt with nettle in the license template, renaming
   serpent_context to serpent_ctx, renaming u32 to uint32_t, removing
   libgcrypt stubs and selftests, modifying entry function prototypes,
   using FOR_BLOCKS to iterate through data in encrypt/decrypt, using
   LE_READ_UINT32 and LE_WRITE_UINT32 to access data in
   encrypt/decrypt, and running indent on the code. */
40

41
#if HAVE_CONFIG_H
Simon Josefsson's avatar
Simon Josefsson committed
42
#include "config.h"
43
44
45
#endif

#include <assert.h>
46

Rafael Sevilla's avatar
Rafael Sevilla committed
47
48
#include "serpent.h"

49
50
#include "macros.h"

Simon Josefsson's avatar
Simon Josefsson committed
51
52
53
/* Magic number, used during generating of the subkeys.  */
#define PHI 0x9E3779B9

54
55
/* FIXME: Unify ROL macros used here, in camellia.c and cast128.c. */
#define ROL32(x,n) ((((uint32_t)(x))<<(n))|	\
Simon Josefsson's avatar
Simon Josefsson committed
56
                  (((uint32_t)(x))>>(32-(n))))
57
#define ROR32(x,n) ((((uint32_t)(x))<<(32-(n)))|	\
Simon Josefsson's avatar
Simon Josefsson committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
                  (((uint32_t)(x))>>(n)))

/* These are the S-Boxes of Serpent.  They are copied from Serpents
   reference implementation (the optimized one, contained in
   `floppy2') and are therefore:

     Copyright (C) 1998 Ross Anderson, Eli Biham, Lars Knudsen.

  To quote the Serpent homepage
  (http://www.cl.cam.ac.uk/~rja14/serpent.html):

  "Serpent is now completely in the public domain, and we impose no
   restrictions on its use.  This was announced on the 21st August at
   the First AES Candidate Conference. The optimised implementations
   in the submission package are now under the GNU PUBLIC LICENSE
   (GPL), although some comments in the code still say otherwise. You
   are welcome to use Serpent for any application."  */

76
77
78
/* FIXME: Except when used within the key schedule, the inputs are not
   used after the substitution, and hence we could allow them to be
   destroyed. Can this freedom be used to optimize the sboxes? */
Simon Josefsson's avatar
Simon Josefsson committed
79
#define SBOX0(a, b, c, d, w, x, y, z) \
80
  do { \
Simon Josefsson's avatar
Simon Josefsson committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    uint32_t t02, t03, t05, t06, t07, t08, t09; \
    uint32_t t11, t12, t13, t14, t15, t17, t01; \
    t01 = b   ^ c  ; \
    t02 = a   | d  ; \
    t03 = a   ^ b  ; \
    z   = t02 ^ t01; \
    t05 = c   | z  ; \
    t06 = a   ^ d  ; \
    t07 = b   | c  ; \
    t08 = d   & t05; \
    t09 = t03 & t07; \
    y   = t09 ^ t08; \
    t11 = t09 & y  ; \
    t12 = c   ^ d  ; \
    t13 = t07 ^ t11; \
    t14 = b   & t06; \
    t15 = t06 ^ t13; \
    w   =     ~ t15; \
    t17 = w   ^ t14; \
    x   = t12 ^ t17; \
101
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
102
103

#define SBOX0_INVERSE(a, b, c, d, w, x, y, z) \
104
  do { \
Simon Josefsson's avatar
Simon Josefsson committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    uint32_t t02, t03, t04, t05, t06, t08, t09, t10; \
    uint32_t t12, t13, t14, t15, t17, t18, t01; \
    t01 = c   ^ d  ; \
    t02 = a   | b  ; \
    t03 = b   | c  ; \
    t04 = c   & t01; \
    t05 = t02 ^ t01; \
    t06 = a   | t04; \
    y   =     ~ t05; \
    t08 = b   ^ d  ; \
    t09 = t03 & t08; \
    t10 = d   | y  ; \
    x   = t09 ^ t06; \
    t12 = a   | t05; \
    t13 = x   ^ t12; \
    t14 = t03 ^ t10; \
    t15 = a   ^ c  ; \
    z   = t14 ^ t13; \
    t17 = t05 & t13; \
    t18 = t14 | t17; \
    w   = t15 ^ t18; \
126
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
127
128

#define SBOX1(a, b, c, d, w, x, y, z) \
129
  do { \
Simon Josefsson's avatar
Simon Josefsson committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    uint32_t t02, t03, t04, t05, t06, t07, t08; \
    uint32_t t10, t11, t12, t13, t16, t17, t01; \
    t01 = a   | d  ; \
    t02 = c   ^ d  ; \
    t03 =     ~ b  ; \
    t04 = a   ^ c  ; \
    t05 = a   | t03; \
    t06 = d   & t04; \
    t07 = t01 & t02; \
    t08 = b   | t06; \
    y   = t02 ^ t05; \
    t10 = t07 ^ t08; \
    t11 = t01 ^ t10; \
    t12 = y   ^ t11; \
    t13 = b   & d  ; \
    z   =     ~ t10; \
    x   = t13 ^ t12; \
    t16 = t10 | x  ; \
    t17 = t05 & t16; \
    w   = c   ^ t17; \
150
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
151
152

#define SBOX1_INVERSE(a, b, c, d, w, x, y, z) \
153
  do { \
Simon Josefsson's avatar
Simon Josefsson committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    uint32_t t02, t03, t04, t05, t06, t07, t08; \
    uint32_t t09, t10, t11, t14, t15, t17, t01; \
    t01 = a   ^ b  ; \
    t02 = b   | d  ; \
    t03 = a   & c  ; \
    t04 = c   ^ t02; \
    t05 = a   | t04; \
    t06 = t01 & t05; \
    t07 = d   | t03; \
    t08 = b   ^ t06; \
    t09 = t07 ^ t06; \
    t10 = t04 | t03; \
    t11 = d   & t08; \
    y   =     ~ t09; \
    x   = t10 ^ t11; \
    t14 = a   | y  ; \
    t15 = t06 ^ x  ; \
    z   = t01 ^ t04; \
    t17 = c   ^ t15; \
    w   = t14 ^ t17; \
174
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
175
176

#define SBOX2(a, b, c, d, w, x, y, z) \
177
  do {					   \
Simon Josefsson's avatar
Simon Josefsson committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    uint32_t t02, t03, t05, t06, t07, t08; \
    uint32_t t09, t10, t12, t13, t14, t01; \
    t01 = a   | c  ; \
    t02 = a   ^ b  ; \
    t03 = d   ^ t01; \
    w   = t02 ^ t03; \
    t05 = c   ^ w  ; \
    t06 = b   ^ t05; \
    t07 = b   | t05; \
    t08 = t01 & t06; \
    t09 = t03 ^ t07; \
    t10 = t02 | t09; \
    x   = t10 ^ t08; \
    t12 = a   | d  ; \
    t13 = t09 ^ x  ; \
    t14 = b   ^ t13; \
    z   =     ~ t09; \
    y   = t12 ^ t14; \
196
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
197
198

#define SBOX2_INVERSE(a, b, c, d, w, x, y, z) \
199
  do {						\
Simon Josefsson's avatar
Simon Josefsson committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
    uint32_t t02, t03, t04, t06, t07, t08, t09; \
    uint32_t t10, t11, t12, t15, t16, t17, t01; \
    t01 = a   ^ d  ; \
    t02 = c   ^ d  ; \
    t03 = a   & c  ; \
    t04 = b   | t02; \
    w   = t01 ^ t04; \
    t06 = a   | c  ; \
    t07 = d   | w  ; \
    t08 =     ~ d  ; \
    t09 = b   & t06; \
    t10 = t08 | t03; \
    t11 = b   & t07; \
    t12 = t06 & t02; \
    z   = t09 ^ t10; \
    x   = t12 ^ t11; \
    t15 = c   & z  ; \
    t16 = w   ^ x  ; \
    t17 = t10 ^ t15; \
    y   = t16 ^ t17; \
220
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
221
222

#define SBOX3(a, b, c, d, w, x, y, z) \
223
  do {						\
Simon Josefsson's avatar
Simon Josefsson committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    uint32_t t02, t03, t04, t05, t06, t07, t08; \
    uint32_t t09, t10, t11, t13, t14, t15, t01; \
    t01 = a   ^ c  ; \
    t02 = a   | d  ; \
    t03 = a   & d  ; \
    t04 = t01 & t02; \
    t05 = b   | t03; \
    t06 = a   & b  ; \
    t07 = d   ^ t04; \
    t08 = c   | t06; \
    t09 = b   ^ t07; \
    t10 = d   & t05; \
    t11 = t02 ^ t10; \
    z   = t08 ^ t09; \
    t13 = d   | z  ; \
    t14 = a   | t07; \
    t15 = b   & t13; \
    y   = t08 ^ t11; \
    w   = t14 ^ t15; \
    x   = t05 ^ t04; \
244
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
245
246

#define SBOX3_INVERSE(a, b, c, d, w, x, y, z) \
247
  do { \
Simon Josefsson's avatar
Simon Josefsson committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
    uint32_t t02, t03, t04, t05, t06, t07, t09; \
    uint32_t t11, t12, t13, t14, t16, t01; \
    t01 = c   | d  ; \
    t02 = a   | d  ; \
    t03 = c   ^ t02; \
    t04 = b   ^ t02; \
    t05 = a   ^ d  ; \
    t06 = t04 & t03; \
    t07 = b   & t01; \
    y   = t05 ^ t06; \
    t09 = a   ^ t03; \
    w   = t07 ^ t03; \
    t11 = w   | t05; \
    t12 = t09 & t11; \
    t13 = a   & y  ; \
    t14 = t01 ^ t05; \
    x   = b   ^ t12; \
    t16 = b   | t13; \
    z   = t14 ^ t16; \
267
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
268
269

#define SBOX4(a, b, c, d, w, x, y, z) \
270
  do { \
Simon Josefsson's avatar
Simon Josefsson committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    uint32_t t02, t03, t04, t05, t06, t08, t09; \
    uint32_t t10, t11, t12, t13, t14, t15, t16, t01; \
    t01 = a   | b  ; \
    t02 = b   | c  ; \
    t03 = a   ^ t02; \
    t04 = b   ^ d  ; \
    t05 = d   | t03; \
    t06 = d   & t01; \
    z   = t03 ^ t06; \
    t08 = z   & t04; \
    t09 = t04 & t05; \
    t10 = c   ^ t06; \
    t11 = b   & c  ; \
    t12 = t04 ^ t08; \
    t13 = t11 | t03; \
    t14 = t10 ^ t09; \
    t15 = a   & t05; \
    t16 = t11 | t12; \
    y   = t13 ^ t08; \
    x   = t15 ^ t16; \
    w   =     ~ t14; \
292
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
293
294

#define SBOX4_INVERSE(a, b, c, d, w, x, y, z) \
295
  do { \
Simon Josefsson's avatar
Simon Josefsson committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
    uint32_t t02, t03, t04, t05, t06, t07, t09; \
    uint32_t t10, t11, t12, t13, t15, t01; \
    t01 = b   | d  ; \
    t02 = c   | d  ; \
    t03 = a   & t01; \
    t04 = b   ^ t02; \
    t05 = c   ^ d  ; \
    t06 =     ~ t03; \
    t07 = a   & t04; \
    x   = t05 ^ t07; \
    t09 = x   | t06; \
    t10 = a   ^ t07; \
    t11 = t01 ^ t09; \
    t12 = d   ^ t04; \
    t13 = c   | t10; \
    z   = t03 ^ t12; \
    t15 = a   ^ t04; \
    y   = t11 ^ t13; \
    w   = t15 ^ t09; \
315
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
316
317

#define SBOX5(a, b, c, d, w, x, y, z) \
318
  do { \
Simon Josefsson's avatar
Simon Josefsson committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
    uint32_t t02, t03, t04, t05, t07, t08, t09; \
    uint32_t t10, t11, t12, t13, t14, t01; \
    t01 = b   ^ d  ; \
    t02 = b   | d  ; \
    t03 = a   & t01; \
    t04 = c   ^ t02; \
    t05 = t03 ^ t04; \
    w   =     ~ t05; \
    t07 = a   ^ t01; \
    t08 = d   | w  ; \
    t09 = b   | t05; \
    t10 = d   ^ t08; \
    t11 = b   | t07; \
    t12 = t03 | w  ; \
    t13 = t07 | t10; \
    t14 = t01 ^ t11; \
    y   = t09 ^ t13; \
    x   = t07 ^ t08; \
    z   = t12 ^ t14; \
338
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
339
340

#define SBOX5_INVERSE(a, b, c, d, w, x, y, z) \
341
  do { \
Simon Josefsson's avatar
Simon Josefsson committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
    uint32_t t02, t03, t04, t05, t07, t08, t09; \
    uint32_t t10, t12, t13, t15, t16, t01; \
    t01 = a   & d  ; \
    t02 = c   ^ t01; \
    t03 = a   ^ d  ; \
    t04 = b   & t02; \
    t05 = a   & c  ; \
    w   = t03 ^ t04; \
    t07 = a   & w  ; \
    t08 = t01 ^ w  ; \
    t09 = b   | t05; \
    t10 =     ~ b  ; \
    x   = t08 ^ t09; \
    t12 = t10 | t07; \
    t13 = w   | x  ; \
    z   = t02 ^ t12; \
    t15 = t02 ^ t13; \
    t16 = b   ^ d  ; \
    y   = t16 ^ t15; \
361
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
362
363

#define SBOX6(a, b, c, d, w, x, y, z) \
364
  do { \
Simon Josefsson's avatar
Simon Josefsson committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    uint32_t t02, t03, t04, t05, t07, t08, t09, t10; \
    uint32_t t11, t12, t13, t15, t17, t18, t01; \
    t01 = a   & d  ; \
    t02 = b   ^ c  ; \
    t03 = a   ^ d  ; \
    t04 = t01 ^ t02; \
    t05 = b   | c  ; \
    x   =     ~ t04; \
    t07 = t03 & t05; \
    t08 = b   & x  ; \
    t09 = a   | c  ; \
    t10 = t07 ^ t08; \
    t11 = b   | d  ; \
    t12 = c   ^ t11; \
    t13 = t09 ^ t10; \
    y   =     ~ t13; \
    t15 = x   & t03; \
    z   = t12 ^ t07; \
    t17 = a   ^ b  ; \
    t18 = y   ^ t15; \
    w   = t17 ^ t18; \
386
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
387
388

#define SBOX6_INVERSE(a, b, c, d, w, x, y, z) \
389
  do { \
Simon Josefsson's avatar
Simon Josefsson committed
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
    uint32_t t02, t03, t04, t05, t06, t07, t08, t09; \
    uint32_t t12, t13, t14, t15, t16, t17, t01; \
    t01 = a   ^ c  ; \
    t02 =     ~ c  ; \
    t03 = b   & t01; \
    t04 = b   | t02; \
    t05 = d   | t03; \
    t06 = b   ^ d  ; \
    t07 = a   & t04; \
    t08 = a   | t02; \
    t09 = t07 ^ t05; \
    x   = t06 ^ t08; \
    w   =     ~ t09; \
    t12 = b   & w  ; \
    t13 = t01 & t05; \
    t14 = t01 ^ t12; \
    t15 = t07 ^ t13; \
    t16 = d   | t02; \
    t17 = a   ^ x  ; \
    z   = t17 ^ t15; \
    y   = t16 ^ t14; \
411
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
412
413

#define SBOX7(a, b, c, d, w, x, y, z) \
414
  do { \
Simon Josefsson's avatar
Simon Josefsson committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
    uint32_t t02, t03, t04, t05, t06, t08, t09, t10; \
    uint32_t t11, t13, t14, t15, t16, t17, t01; \
    t01 = a   & c  ; \
    t02 =     ~ d  ; \
    t03 = a   & t02; \
    t04 = b   | t01; \
    t05 = a   & b  ; \
    t06 = c   ^ t04; \
    z   = t03 ^ t06; \
    t08 = c   | z  ; \
    t09 = d   | t05; \
    t10 = a   ^ t08; \
    t11 = t04 & z  ; \
    x   = t09 ^ t10; \
    t13 = b   ^ x  ; \
    t14 = t01 ^ x  ; \
    t15 = c   ^ t05; \
    t16 = t11 | t13; \
    t17 = t02 | t14; \
    w   = t15 ^ t17; \
    y   = a   ^ t16; \
436
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
437
438

#define SBOX7_INVERSE(a, b, c, d, w, x, y, z) \
439
  do { \
Simon Josefsson's avatar
Simon Josefsson committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
    uint32_t t02, t03, t04, t06, t07, t08, t09; \
    uint32_t t10, t11, t13, t14, t15, t16, t01; \
    t01 = a   & b  ; \
    t02 = a   | b  ; \
    t03 = c   | t01; \
    t04 = d   & t02; \
    z   = t03 ^ t04; \
    t06 = b   ^ t04; \
    t07 = d   ^ z  ; \
    t08 =     ~ t07; \
    t09 = t06 | t08; \
    t10 = b   ^ d  ; \
    t11 = a   | d  ; \
    x   = a   ^ t09; \
    t13 = c   ^ t06; \
    t14 = c   & t11; \
    t15 = d   | x  ; \
    t16 = t01 | t10; \
    w   = t13 ^ t15; \
    y   = t14 ^ t16; \
460
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
461

462
463
/* In-place linear transformation.  */
#define LINEAR_TRANSFORMATION(x0,x1,x2,x3)		 \
464
  do {                                                   \
465
466
467
468
469
470
471
472
473
474
    x0 = ROL32 (x0, 13);                    \
    x2 = ROL32 (x2, 3);                     \
    x1 = x1 ^ x0 ^ x2;        \
    x3 = x3 ^ x2 ^ (x0 << 3); \
    x1 = ROL32 (x1, 1);                     \
    x3 = ROL32 (x3, 7);                     \
    x0 = x0 ^ x1 ^ x3;        \
    x2 = x2 ^ x3 ^ (x1 << 7); \
    x0 = ROL32 (x0, 5);                     \
    x2 = ROL32 (x2, 22);                    \
475
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
476

477
478
/* In-place inverse linear transformation.  */
#define LINEAR_TRANSFORMATION_INVERSE(x0,x1,x2,x3)	 \
479
  do {                                                   \
480
481
482
483
484
485
486
487
488
489
    x2 = ROR32 (x2, 22);                    \
    x0 = ROR32 (x0 , 5);                    \
    x2 = x2 ^ x3 ^ (x1 << 7); \
    x0 = x0 ^ x1 ^ x3;        \
    x3 = ROR32 (x3, 7);                     \
    x1 = ROR32 (x1, 1);                     \
    x3 = x3 ^ x2 ^ (x0 << 3); \
    x1 = x1 ^ x0 ^ x2;        \
    x2 = ROR32 (x2, 3);                     \
    x0 = ROR32 (x0, 13);                    \
490
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
491

492
493
494
495
496
497
#define KEYXOR(x0,x1,x2,x3, subkey)		       \
  do {						       \
    (x0) ^= (subkey)[0];			       \
    (x1) ^= (subkey)[1];			       \
    (x2) ^= (subkey)[2];			       \
    (x3) ^= (subkey)[3];			       \
498
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
499

500
501
502
503
504
505
506
/* Round inputs are x0,x1,x2,x3 (destroyed), and round outputs are
   y0,y1,y2,y3. */
#define ROUND(which, subkey, x0,x1,x2,x3, y0,y1,y2,y3) \
  do {						       \
    KEYXOR(x0,x1,x2,x3, subkey);		       \
    SBOX##which(x0,x1,x2,x3, y0,y1,y2,y3);	       \
    LINEAR_TRANSFORMATION(y0,y1,y2,y3);		       \
507
  } while (0)
Simon Josefsson's avatar
Simon Josefsson committed
508

509
510
511
512
513
514
515
/* Round inputs are x0,x1,x2,x3 (destroyed), and round outputs are
   y0,y1,y2,y3. */
#define ROUND_INVERSE(which, subkey, x0,x1,x2,x3, y0,y1,y2,y3) \
  do {							       \
    LINEAR_TRANSFORMATION_INVERSE (x0,x1,x2,x3);	       \
    SBOX##which##_INVERSE(x0,x1,x2,x3, y0,y1,y2,y3);	       \
    KEYXOR(y0,y1,y2,y3, subkey);			       \
516
517
  } while (0)

518
/* Key schedule */
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* Note: Increments k */
#define KS_RECURRENCE(w, i, k)						\
  do {									\
    uint32_t _wn = (w)[(i)] ^ (w)[((i)+3)&7] ^ w[((i)+5)&7]		\
      ^ w[((i)+7)&7] ^ PHI ^ (k)++;					\
    ((w)[(i)] = ROL32(_wn, 11));					\
  } while (0)

/* Note: Increments k four times and keys once */
#define KS(keys, s, w, i, k)					\
  do {								\
    KS_RECURRENCE(w, (i), (k));					\
    KS_RECURRENCE(w, (i)+1, (k));				\
    KS_RECURRENCE(w, (i)+2, (k));				\
    KS_RECURRENCE(w, (i)+3, (k));				\
    SBOX##s(w[(i)],w[(i)+1],w[(i)+2],w[(i)+3],			\
	    (*keys)[0],(*keys)[1],(*keys)[2],(*keys)[3]);	\
    (keys)++;							\
  } while (0)

/* Pad user key and convert to an array of 8 uint32_t. */
Simon Josefsson's avatar
Simon Josefsson committed
540
static void
541
542
serpent_key_pad (const uint8_t *key, unsigned int key_length,
		 uint32_t *w)
Simon Josefsson's avatar
Simon Josefsson committed
543
544
545
{
  unsigned int i;

Niels Möller's avatar
Niels Möller committed
546
547
548
  assert (key_length <= SERPENT_MAX_KEY_SIZE);
  
  for (i = 0; key_length >= 4; key_length -=4, key += 4)
549
    w[i++] = LE_READ_UINT32(key);
550

551
  if (i < 8)
552
    {
Niels Möller's avatar
Niels Möller committed
553
554
555
556
557
558
559
      /* Key must be padded according to the Serpent specification.
         "aabbcc" -> "aabbcc0100...00" -> 0x01ccbbaa. */
      uint32_t pad = 0x01;
      
      while (key_length > 0)
	pad = pad << 8 | key[--key_length];

560
      w[i++] = pad;
Simon Josefsson's avatar
Simon Josefsson committed
561

Niels Möller's avatar
Niels Möller committed
562
      while (i < 8)
563
	w[i++] = 0;
564
    }
Simon Josefsson's avatar
Simon Josefsson committed
565
566
567
}

/* Initialize CONTEXT with the key KEY of KEY_LENGTH bits.  */
Rafael Sevilla's avatar
Rafael Sevilla committed
568
void
Simon Josefsson's avatar
Simon Josefsson committed
569
570
serpent_set_key (struct serpent_ctx *ctx,
		 unsigned length, const uint8_t * key)
Rafael Sevilla's avatar
Rafael Sevilla committed
571
{
572
573
574
575
576
  uint32_t w[8];
  uint32_t (*keys)[4];
  unsigned k;
  
  serpent_key_pad (key, length, w);
577

578
579
580
581
582
583
584
585
  /* Derive the 33 subkeys from KEY and store them in SUBKEYS. We do
     the recurrence in the key schedule using W as a circular buffer
     of just 8 uint32_t. */

  /* FIXME: Would be better to invoke SBOX with scalar variables as
     arguments, no arrays. To do that, unpack w into separate
     variables, use temporary variables as the SBOX destination. */

586
587
588
  keys = ctx->keys;
  k = 0;
  for (;;)
589
590
    {
      KS(keys, 3, w, 0, k);
591
592
      if (k == 132)
	break;
593
594
595
596
597
598
599
600
601
      KS(keys, 2, w, 4, k);
      KS(keys, 1, w, 0, k);
      KS(keys, 0, w, 4, k);
      KS(keys, 7, w, 0, k);
      KS(keys, 6, w, 4, k);
      KS(keys, 5, w, 0, k);
      KS(keys, 4, w, 4, k);
    }
  assert (keys == ctx->keys + 33);
Rafael Sevilla's avatar
Rafael Sevilla committed
602
603
604
}

void
Simon Josefsson's avatar
Simon Josefsson committed
605
606
serpent_encrypt (const struct serpent_ctx *ctx,
		 unsigned length, uint8_t * dst, const uint8_t * src)
Rafael Sevilla's avatar
Rafael Sevilla committed
607
{
Simon Josefsson's avatar
Simon Josefsson committed
608
  FOR_BLOCKS (length, dst, src, SERPENT_BLOCK_SIZE)
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
    {
      uint32_t x0,x1,x2,x3, y0,y1,y2,y3;
      unsigned k;

      x0 = LE_READ_UINT32 (src);
      x1 = LE_READ_UINT32 (src + 4);
      x2 = LE_READ_UINT32 (src + 8);
      x3 = LE_READ_UINT32 (src + 12);

      for (k = 0; ; k += 8)
	{
	  ROUND (0, ctx->keys[k+0], x0,x1,x2,x3, y0,y1,y2,y3);
	  ROUND (1, ctx->keys[k+1], y0,y1,y2,y3, x0,x1,x2,x3);
	  ROUND (2, ctx->keys[k+2], x0,x1,x2,x3, y0,y1,y2,y3);
	  ROUND (3, ctx->keys[k+3], y0,y1,y2,y3, x0,x1,x2,x3);
	  ROUND (4, ctx->keys[k+4], x0,x1,x2,x3, y0,y1,y2,y3);
	  ROUND (5, ctx->keys[k+5], y0,y1,y2,y3, x0,x1,x2,x3);
	  ROUND (6, ctx->keys[k+6], x0,x1,x2,x3, y0,y1,y2,y3);
	  if (k == 24)
	    break;
	  ROUND (7, ctx->keys[k+7], y0,y1,y2,y3, x0,x1,x2,x3);
	}

      /* Special final round, using two subkeys. */
      KEYXOR (y0,y1,y2,y3, ctx->keys[31]);
      SBOX7 (y0,y1,y2,y3, x0,x1,x2,x3);
      KEYXOR (x0,x1,x2,x3, ctx->keys[32]);
    
      LE_WRITE_UINT32 (dst, x0);
      LE_WRITE_UINT32 (dst + 4, x1);
      LE_WRITE_UINT32 (dst + 8, x2);
      LE_WRITE_UINT32 (dst + 12, x3);
    }
Simon Josefsson's avatar
Simon Josefsson committed
642
643
644
645
646
647
648
}

void
serpent_decrypt (const struct serpent_ctx *ctx,
		 unsigned length, uint8_t * dst, const uint8_t * src)
{
  FOR_BLOCKS (length, dst, src, SERPENT_BLOCK_SIZE)
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
    {
      uint32_t x0,x1,x2,x3, y0,y1,y2,y3;
      unsigned k;

      x0 = LE_READ_UINT32 (src);
      x1 = LE_READ_UINT32 (src + 4);
      x2 = LE_READ_UINT32 (src + 8);
      x3 = LE_READ_UINT32 (src + 12);

      /* Inverse of special round */
      KEYXOR (x0,x1,x2,x3, ctx->keys[32]);
      SBOX7_INVERSE (x0,x1,x2,x3, y0,y1,y2,y3);
      KEYXOR (y0,y1,y2,y3, ctx->keys[31]);

      k = 24;
      goto start;
      while (k > 0)
	{
	  k -= 8;
	  ROUND_INVERSE (7, ctx->keys[k+7], x0,x1,x2,x3, y0,y1,y2,y3);
	start:
	  ROUND_INVERSE (6, ctx->keys[k+6], y0,y1,y2,y3, x0,x1,x2,x3);
	  ROUND_INVERSE (5, ctx->keys[k+5], x0,x1,x2,x3, y0,y1,y2,y3);
	  ROUND_INVERSE (4, ctx->keys[k+4], y0,y1,y2,y3, x0,x1,x2,x3);
	  ROUND_INVERSE (3, ctx->keys[k+3], x0,x1,x2,x3, y0,y1,y2,y3);
	  ROUND_INVERSE (2, ctx->keys[k+2], y0,y1,y2,y3, x0,x1,x2,x3);
	  ROUND_INVERSE (1, ctx->keys[k+1], x0,x1,x2,x3, y0,y1,y2,y3);
	  ROUND_INVERSE (0, ctx->keys[k], y0,y1,y2,y3, x0,x1,x2,x3);
	}
      
      LE_WRITE_UINT32 (dst, x0);
      LE_WRITE_UINT32 (dst + 4, x1);
      LE_WRITE_UINT32 (dst + 8, x2);
      LE_WRITE_UINT32 (dst + 12, x3);
    }
Rafael Sevilla's avatar
Rafael Sevilla committed
684
}