3 * The serpent block cipher.
5 * For more details on this algorithm, see the Serpent website at
6 * http://www.cl.cam.ac.uk/~rja14/serpent.html
9 /* nettle, low-level cryptographics library
11 * Copyright (C) 2011 Niels Möller
12 * Copyright (C) 2010, 2011 Simon Josefsson
13 * Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
15 * The nettle library is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License as published by
17 * the Free Software Foundation; either version 2.1 of the License, or (at your
18 * option) any later version.
20 * The nettle library is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
23 * License for more details.
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with the nettle library; see the file COPYING.LIB. If not, write to
27 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
31 /* This file is derived from cipher/serpent.c in Libgcrypt v1.4.6.
32 The adaption to Nettle was made by Simon Josefsson on 2010-12-07
33 with final touches on 2011-05-30. Changes include replacing
34 libgcrypt with nettle in the license template, renaming
35 serpent_context to serpent_ctx, renaming u32 to uint32_t, removing
36 libgcrypt stubs and selftests, modifying entry function prototypes,
37 using FOR_BLOCKS to iterate through data in encrypt/decrypt, using
38 LE_READ_UINT32 and LE_WRITE_UINT32 to access data in
39 encrypt/decrypt, and running indent on the code. */
51 #include "serpent-internal.h"
53 /* Magic number, used during generating of the subkeys. */
54 #define PHI 0x9E3779B9
56 /* These are the S-Boxes of Serpent. They are copied from Serpents
57 reference implementation (the optimized one, contained in
58 `floppy2') and are therefore:
60 Copyright (C) 1998 Ross Anderson, Eli Biham, Lars Knudsen.
62 To quote the Serpent homepage
63 (http://www.cl.cam.ac.uk/~rja14/serpent.html):
65 "Serpent is now completely in the public domain, and we impose no
66 restrictions on its use. This was announced on the 21st August at
67 the First AES Candidate Conference. The optimised implementations
68 in the submission package are now under the GNU PUBLIC LICENSE
69 (GPL), although some comments in the code still say otherwise. You
70 are welcome to use Serpent for any application." */
72 /* FIXME: Except when used within the key schedule, the inputs are not
73 used after the substitution, and hence we could allow them to be
74 destroyed. Can this freedom be used to optimize the sboxes? */
75 #define SBOX0(type, a, b, c, d, w, x, y, z) \
77 type t02, t03, t05, t06, t07, t08, t09; \
78 type t11, t12, t13, t14, t15, t17, t01; \
99 #define SBOX1(type, a, b, c, d, w, x, y, z) \
101 type t02, t03, t04, t05, t06, t07, t08; \
102 type t10, t11, t12, t13, t16, t17, t01; \
123 #define SBOX2(type, a, b, c, d, w, x, y, z) \
125 type t02, t03, t05, t06, t07, t08; \
126 type t09, t10, t12, t13, t14, t01; \
145 #define SBOX3(type, a, b, c, d, w, x, y, z) \
147 type t02, t03, t04, t05, t06, t07, t08; \
148 type t09, t10, t11, t13, t14, t15, t01; \
169 #define SBOX4(type, a, b, c, d, w, x, y, z) \
171 type t02, t03, t04, t05, t06, t08, t09; \
172 type t10, t11, t12, t13, t14, t15, t16, t01; \
194 #define SBOX5(type, a, b, c, d, w, x, y, z) \
196 type t02, t03, t04, t05, t07, t08, t09; \
197 type t10, t11, t12, t13, t14, t01; \
217 #define SBOX6(type, a, b, c, d, w, x, y, z) \
219 type t02, t03, t04, t05, t07, t08, t09, t10; \
220 type t11, t12, t13, t15, t17, t18, t01; \
242 #define SBOX7(type, a, b, c, d, w, x, y, z) \
244 type t02, t03, t04, t05, t06, t08, t09, t10; \
245 type t11, t13, t14, t15, t16, t17, t01; \
268 /* Note: Increments k */
269 #define KS_RECURRENCE(w, i, k) \
271 uint32_t _wn = (w)[(i)] ^ (w)[((i)+3)&7] ^ w[((i)+5)&7] \
272 ^ w[((i)+7)&7] ^ PHI ^ (k)++; \
273 ((w)[(i)] = ROTL32(11, _wn)); \
276 /* Note: Increments k four times and keys once */
277 #define KS(keys, s, w, i, k) \
279 KS_RECURRENCE(w, (i), (k)); \
280 KS_RECURRENCE(w, (i)+1, (k)); \
281 KS_RECURRENCE(w, (i)+2, (k)); \
282 KS_RECURRENCE(w, (i)+3, (k)); \
283 SBOX##s(uint32_t, w[(i)],w[(i)+1],w[(i)+2],w[(i)+3], \
284 (*keys)[0],(*keys)[1],(*keys)[2],(*keys)[3]); \
288 /* Pad user key and convert to an array of 8 uint32_t. */
290 serpent_key_pad (const uint8_t *key
, unsigned int key_length
,
295 assert (key_length
<= SERPENT_MAX_KEY_SIZE
);
297 for (i
= 0; key_length
>= 4; key_length
-=4, key
+= 4)
298 w
[i
++] = LE_READ_UINT32(key
);
302 /* Key must be padded according to the Serpent specification.
303 "aabbcc" -> "aabbcc0100...00" -> 0x01ccbbaa. */
306 while (key_length
> 0)
307 pad
= pad
<< 8 | key
[--key_length
];
316 /* Initialize CONTEXT with the key KEY of KEY_LENGTH bits. */
318 serpent_set_key (struct serpent_ctx
*ctx
,
319 unsigned length
, const uint8_t * key
)
325 serpent_key_pad (key
, length
, w
);
327 /* Derive the 33 subkeys from KEY and store them in SUBKEYS. We do
328 the recurrence in the key schedule using W as a circular buffer
329 of just 8 uint32_t. */
331 /* FIXME: Would be better to invoke SBOX with scalar variables as
332 arguments, no arrays. To do that, unpack w into separate
333 variables, use temporary variables as the SBOX destination. */
339 KS(keys
, 3, w
, 0, k
);
342 KS(keys
, 2, w
, 4, k
);
343 KS(keys
, 1, w
, 0, k
);
344 KS(keys
, 0, w
, 4, k
);
345 KS(keys
, 7, w
, 0, k
);
346 KS(keys
, 6, w
, 4, k
);
347 KS(keys
, 5, w
, 0, k
);
348 KS(keys
, 4, w
, 4, k
);
350 assert (keys
== ctx
->keys
+ 33);