serpent-encrypt.c 14.5 KB
Newer Older
1
/* serpent-encrypt.c
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

   The serpent block cipher.

   For more details on this algorithm, see the Serpent website at
   http://www.cl.cam.ac.uk/~rja14/serpent.html

   Copyright (C) 2011  Niels Möller
   Copyright (C) 2010, 2011  Simon Josefsson
   Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle 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
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/
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

/* 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. */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
#include <limits.h>

#include "serpent.h"

#include "macros.h"
#include "serpent-internal.h"

/* 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."  */

/* S0:  3  8 15  1 10  6  5 11 14 13  4  2  7  0  9 12 */
78
/* Could easily let y0, y1 overlap with x0, x1, and possibly also x2 and y2 */
79
#define SBOX0(x0, x1, x2, x3, y0, y1, y2, y3)	\
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  do {							\
    y3  = x1 ^ x2;					\
    y0  = x0 | x3;					\
    y1  = x0 ^ x1;					\
    y3 ^= y0;						\
    y2  = x2 | y3;					\
    x0 ^= x3;						\
    y2 &= x3;						\
    x3 ^= x2;						\
    x2 |= x1;						\
    y0  = y1 & x2;					\
    y2 ^= y0;						\
    y0 &= y2;						\
    y0 ^= x2;						\
    x1 &= x0;						\
    y0 ^= x0;						\
    y0  = ~ y0;						\
    y1  = y0 ^ x1;					\
    y1 ^= x3;						\
99 100
  } while (0)

101
/* FIXME: Arrange for some overlap between inputs and outputs? */
102
/* S1: 15 12  2  7  9  0  5 10  1 11 14  8  6 13  3  4 */
103 104
/* Original single-assignment form:
   
Niels Möller's avatar
Niels Möller committed
105 106 107 108 109 110 111 112 113 114 115 116 117
     t01 = x0  | x3;
     t02 = x2  ^ x3;
     t03 =     ~ x1;
     t04 = x0  ^ x2;
     t05 = x0  | t03;
     t06 = x3  & t04;
     t07 = t01 & t02;
     t08 = x1  | t06;
     y2  = t02 ^ t05;
     t10 = t07 ^ t08;
     t11 = t01 ^ t10;
     t12 = y2  ^ t11;
     t13 = x1  & x3;
118
     y3  =     ~ t10;
Niels Möller's avatar
Niels Möller committed
119 120 121 122
     y1  = t13 ^ t12;
     t16 = t10 | y1;
     t17 = t05 & t16;
     y0  = x2  ^ t17;
123 124
*/
#define SBOX1(x0, x1, x2, x3, y0, y1, y2, y3)		\
125 126 127 128
  do {							\
    y1  = x0 | x3;					\
    y2  = x2 ^ x3;					\
    y0  = ~ x1;						\
Niels Möller's avatar
Niels Möller committed
129
    y3  = x0 ^ x2;					\
130 131 132 133 134 135 136 137
    y0 |= x0;						\
    y3 &= x3;						\
    x0 = y1 & y2;					\
    y3 |= x1;						\
    y2 ^= y0;						\
    y3 ^= x0;						\
    x0  = y1 ^ y3;					\
    x0 ^= y2;						\
Niels Möller's avatar
Niels Möller committed
138
    y1  = x1 & x3;					\
139
    y1 ^= x0;						\
140
    x3  = y1 | y3;					\
141 142 143
    y3  = ~ y3;						\
    y0 &= x3;						\
    y0 ^= x2;						\
144 145
  } while (0)

146
/* FIXME: Arrange for some overlap between inputs and outputs? */
147
/* S2:  8  6  7  9  3 12 10 15 13  1 14  4  0 11  5  2 */
148
#define SBOX2(x0, x1, x2, x3, y0, y1, y2, y3)	\
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  do {							\
    y2  = x0 | x2;					\
    y1  = x0 ^ x1;					\
    y3  = x3 ^ y2;					\
    y0  = y1 ^ y3;					\
    x3 |= x0;						\
    x2 ^= y0;						\
    x0  = x1 ^ x2;					\
    x2 |= x1;						\
    x0 &= y2;						\
    y3 ^= x2;						\
    y1 |= y3;						\
    y1 ^= x0;						\
    y2  = y3 ^ y1;					\
    y2 ^= x1;						\
    y3  = ~ y3;						\
    y2 ^= x3;						\
166 167 168
  } while (0)

/* S3:  0 15 11  8 12  9  6  3 13  1  2  4 10  7  5 14 */
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/* Original single-assignment form:
   
     t01 = x0  ^ x2;
     t02 = x0  | x3;
     t03 = x0  & x3;
     t04 = t01 & t02;
     t05 = x1  | t03;
     t06 = x0  & x1;
     t07 = x3  ^ t04;
     t08 = x2  | t06;
     t09 = x1  ^ t07;
     t10 = x3  & t05;
     t11 = t02 ^ t10;
     y3  = t08 ^ t09;
     t13 = x3  | y3;
     t14 = x0  | t07;
     t15 = x1  & t13;
     y2  = t08 ^ t11;
     y0  = t14 ^ t15;
     y1  = t05 ^ t04;
*/
190
#define SBOX3(x0, x1, x2, x3, y0, y1, y2, y3)	\
191
  do {							\
Niels Möller's avatar
Niels Möller committed
192
    y1  = x0 ^ x2;					\
193 194
    y0  = x0 | x3;					\
    y3  = x0 & x3;					\
Niels Möller's avatar
Niels Möller committed
195
    y1 &= y0;						\
196
    y3 |= x1;						\
Niels Möller's avatar
Niels Möller committed
197
    y2  = x0 & x1;					\
198
    y2 |= x2;						\
Niels Möller's avatar
Niels Möller committed
199
    x2  = x3 ^ y1;					\
200 201 202 203 204 205 206 207 208 209
    y1 ^= y3;						\
    x0 |= x2;						\
    x2 ^= x1;						\
    y3 &= x3;						\
    y0 ^= y3;						\
    y3  = y2 ^ x2;					\
    y2 ^= y0;						\
    x3 |= y3;						\
    x1 &= x3;						\
    y0  = x0 ^ x1;					\
210 211
  } while (0)

212

213
/* S4:  1 15  8  3 12  0 11  6  2  5  4 10  9 14  7 13 */
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
/* Original single-assignment form:
    t01 = x0  | x1;
    t02 = x1  | x2;
    t03 = x0  ^ t02;
    t04 = x1  ^ x3;
    t05 = x3  | t03;
    t06 = x3  & t01;
    y3  = t03 ^ t06;
    t08 = y3  & t04;
    t09 = t04 & t05;
    t10 = x2  ^ t06;
    t11 = x1  & x2;
    t12 = t04 ^ t08;
    t13 = t11 | t03;
    t14 = t10 ^ t09;
    t15 = x0  & t05;
    t16 = t11 | t12;
    y2  = t13 ^ t08;
    y1  = t15 ^ t16;
    y0  =     ~ t14;
*/
235
#define SBOX4(x0, x1, x2, x3, y0, y1, y2, y3)	\
236 237 238
  do {							\
    y3  = x0 | x1;					\
    y2  = x1 | x2;					\
Niels Möller's avatar
Niels Möller committed
239
    y2 ^= x0;						\
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    y3 &= x3;						\
    y0  = x1 ^ x3;					\
    x3 |= y2;						\
    x0 &= x3;						\
    x1 &= x2;						\
    x2 ^= y3;						\
    y3 ^= y2;						\
    y2 |= x1;						\
    y1  = y3 & y0;					\
    y2 ^= y1;						\
    y1 ^= y0;						\
    y1 |= x1;						\
    y1 ^= x0;						\
    y0 &= x3;						\
    y0 ^= x2;						\
    y0  = ~y0;						\
256 257 258
  } while (0)

/* S5: 15  5  2 11  4 10  9 12  0  3 14  8 13  6  7  1 */
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
/* Original single-assignment form:
    t01 = x1  ^ x3;
    t02 = x1  | x3;
    t03 = x0  & t01;
    t04 = x2  ^ t02;
    t05 = t03 ^ t04;
    y0  =     ~ t05;
    t07 = x0  ^ t01;
    t08 = x3  | y0;
    t09 = x1  | t05;
    t10 = x3  ^ t08;
    t11 = x1  | t07;
    t12 = t03 | y0;
    t13 = t07 | t10;
    t14 = t01 ^ t11;
    y2  = t09 ^ t13;
    y1  = t07 ^ t08;
    y3  = t12 ^ t14;
*/
278
#define SBOX5(x0, x1, x2, x3, y0, y1, y2, y3)	\
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  do {							\
    y0  = x1 | x3;					\
    y0 ^= x2;						\
    x2  = x1 ^ x3;					\
    y2  = x0 ^ x2;					\
    x0 &= x2;						\
    y0 ^= x0;						\
    y3  = x1 | y2;					\
    x1 |= y0;						\
    y0  = ~y0;						\
    x0 |= y0;						\
    y3 ^= x2;						\
    y3 ^= x0;						\
    y1  = x3 | y0;					\
    x3 ^= y1;						\
    y1 ^= y2;						\
    y2 |= x3;						\
    y2 ^= x1;						\
297 298 299
  } while (0)

/* S6:  7  2 12  5  8  4  6 11 14  9  1 15 13  3 10  0 */
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
/* Original single-assignment form:
    t01 = x0  & x3;
    t02 = x1  ^ x2;
    t03 = x0  ^ x3;
    t04 = t01 ^ t02;
    t05 = x1  | x2;
    y1  =     ~ t04;
    t07 = t03 & t05;
    t08 = x1  & y1;
    t09 = x0  | x2;
    t10 = t07 ^ t08;
    t11 = x1  | x3;
    t12 = x2  ^ t11;
    t13 = t09 ^ t10;
    y2  =     ~ t13;
    t15 = y1  & t03;
    y3  = t12 ^ t07;
    t17 = x0  ^ x1;
    t18 = y2  ^ t15;
    y0  = t17 ^ t18;
*/
#define SBOX6(x0, x1, x2, x3, y0, y1, y2, y3)	\
  do {							\
    y0  = x0 ^ x3;					\
    y1  = x0 & x3;					\
    y2  = x0 | x2;					\
    x3 |= x1;						\
    x3 ^= x2;						\
    x0 ^= x1;						\
    y3  = x1 | x2;					\
    x2 ^= x1;						\
    y3 &= y0;						\
    y1 ^= x2;						\
    y1  = ~y1;						\
    y0 &= y1;						\
    x1 &= y1;						\
    x1 ^= y3;						\
    y3 ^= x3;						\
    y2 ^= x1;						\
    y2  = ~y2;						\
    y0 ^= y2;						\
    y0 ^= x0;						\
342 343 344
  } while (0)

/* S7:  1 13 15  0 14  8  2 11  7  4 12 10  9  3  5  6 */
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
/* Original single-assignment form:
    t01 = x0  & x2;
    t02 =     ~ x3;
    t03 = x0  & t02;
    t04 = x1  | t01;
    t05 = x0  & x1;
    t06 = x2  ^ t04;
    y3  = t03 ^ t06;
    t08 = x2  | y3;
    t09 = x3  | t05;
    t10 = x0  ^ t08;
    t11 = t04 & y3;
    y1  = t09 ^ t10;
    t13 = x1  ^ y1;
    t14 = t01 ^ y1;
    t15 = x2  ^ t05;
    t16 = t11 | t13;
    t17 = t02 | t14;
    y0  = t15 ^ t17;
    y2  = x0  ^ t16;
*/
/* It appears impossible to do this with only 8 registers. We
   recompute t02, and t04 (if we have spare registers, hopefully the
Niels Möller's avatar
Niels Möller committed
368
   compiler can recognize them as common subexpressions). */
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
#define SBOX7(x0, x1, x2, x3, y0, y1, y2, y3)	\
  do {							\
    y0  = x0 & x2;					\
    y3  = x1 | y0; /* t04 */				\
    y3 ^= x2;						\
    y1  = ~x3;     /* t02 */				\
    y1 &= x0;						\
    y3 ^= y1;						\
    y1  = x2 | y3;					\
    y1 ^= x0;						\
    y2  = x0 & x1;					\
    x2 ^= y2;						\
    y2 |= x3;						\
    y1 ^= y2;						\
    y2  = x1 | y0; /* t04 */				\
    y2 &= y3;						\
    x1 ^= y1;						\
    y2 |= x1;						\
    y2 ^= x0;						\
    y0 ^= y1;						\
    x3  = ~x3;     /* t02 */				\
    y0 |= x3;						\
    y0 ^= x2;						\
392 393 394 395 396
  } while (0)

/* In-place linear transformation.  */
#define LINEAR_TRANSFORMATION(x0,x1,x2,x3)		 \
  do {                                                   \
397 398
    x0 = ROTL32 (13, x0);                    \
    x2 = ROTL32 (3, x2);                     \
399 400
    x1 = x1 ^ x0 ^ x2;        \
    x3 = x3 ^ x2 ^ (x0 << 3); \
401 402
    x1 = ROTL32 (1, x1);                     \
    x3 = ROTL32 (7, x3);                     \
403 404
    x0 = x0 ^ x1 ^ x3;        \
    x2 = x2 ^ x3 ^ (x1 << 7); \
405 406
    x0 = ROTL32 (5, x0);                     \
    x2 = ROTL32 (22, x2);                    \
407 408 409 410 411 412 413
  } while (0)

/* 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);		       \
414
    SBOX##which(x0,x1,x2,x3, y0,y1,y2,y3);	       \
415 416 417 418 419 420 421
    LINEAR_TRANSFORMATION(y0,y1,y2,y3);		       \
  } while (0)

#if HAVE_NATIVE_64_BIT

#define LINEAR_TRANSFORMATION64(x0,x1,x2,x3)		 \
  do {                                                   \
422 423
    x0 = DROTL32 (13, x0);                    \
    x2 = DROTL32 (3, x2);                     \
424
    x1 = x1 ^ x0 ^ x2;        \
425 426 427
    x3 = x3 ^ x2 ^ DRSHIFT32(3, x0);	    \
    x1 = DROTL32 (1, x1);                     \
    x3 = DROTL32 (7, x3);                     \
428
    x0 = x0 ^ x1 ^ x3;        \
429 430 431
    x2 = x2 ^ x3 ^ DRSHIFT32(7, x1);	    \
    x0 = DROTL32 (5, x0);                     \
    x2 = DROTL32 (22, x2);                    \
432 433 434 435 436
  } while (0)

#define ROUND64(which, subkey, x0,x1,x2,x3, y0,y1,y2,y3) \
  do {						       \
    KEYXOR64(x0,x1,x2,x3, subkey);		       \
437
    SBOX##which(x0,x1,x2,x3, y0,y1,y2,y3);	       \
438 439 440 441 442 443 444
    LINEAR_TRANSFORMATION64(y0,y1,y2,y3);		       \
  } while (0)

#endif /* HAVE_NATIVE_64_BIT */

void
serpent_encrypt (const struct serpent_ctx *ctx,
445
		 size_t length, uint8_t * dst, const uint8_t * src)
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
{
  assert( !(length % SERPENT_BLOCK_SIZE));
  
#if HAVE_NATIVE_64_BIT
  if (length & SERPENT_BLOCK_SIZE)
#else
  while (length >= SERPENT_BLOCK_SIZE)
#endif
    {
      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]);
479
      SBOX7 (y0,y1,y2,y3, x0,x1,x2,x3);
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
      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);

      src += SERPENT_BLOCK_SIZE;
      dst += SERPENT_BLOCK_SIZE;
      length -= SERPENT_BLOCK_SIZE;
    }
#if HAVE_NATIVE_64_BIT
  FOR_BLOCKS(length, dst, src, 2*SERPENT_BLOCK_SIZE)
    {
      uint64_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);

      x0 <<= 32; x0 |= LE_READ_UINT32 (src + 16);
      x1 <<= 32; x1 |= LE_READ_UINT32 (src + 20);
      x2 <<= 32; x2 |= LE_READ_UINT32 (src + 24);
      x3 <<= 32; x3 |= LE_READ_UINT32 (src + 28);

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

      /* Special final round, using two subkeys. */
      KEYXOR64 (y0,y1,y2,y3, ctx->keys[31]);
523
      SBOX7 (y0,y1,y2,y3, x0,x1,x2,x3);
524 525 526 527 528 529 530 531 532 533 534 535 536
      KEYXOR64 (x0,x1,x2,x3, ctx->keys[32]);
    
      LE_WRITE_UINT32 (dst + 16, x0);
      LE_WRITE_UINT32 (dst + 20, x1);
      LE_WRITE_UINT32 (dst + 24, x2);
      LE_WRITE_UINT32 (dst + 28, x3);
      x0 >>= 32; LE_WRITE_UINT32 (dst, x0);
      x1 >>= 32; LE_WRITE_UINT32 (dst + 4, x1);
      x2 >>= 32; LE_WRITE_UINT32 (dst + 8, x2);
      x3 >>= 32; LE_WRITE_UINT32 (dst + 12, x3);
    }
#endif /* HAVE_NATIVE_64_BIT */
}