5
const struct _cipher_descriptor serpent_desc =
17
/* These defines are derived from Brian Gladman's work. Contact him at gladman@seven77.demon.co.uk
19
* Available on the web at http://fp.gladman.plus.com/cryptography_technology/aes/index.htm
21
#define sb0(a,b,c,d,e,f,g,h) \
40
#define ib0(a,b,c,d,e,f,g,h) \
59
#define sb1(a,b,c,d,e,f,g,h) \
77
#define ib1(a,b,c,d,e,f,g,h) \
98
#define sb2(a,b,c,d,e,f,g,h) \
118
#define ib2(a,b,c,d,e,f,g,h) \
138
#define sb3(a,b,c,d,e,f,g,h) \
157
/* 16 term solution that performs less well than 17 term one
158
in my environment (PPro/PII)
160
#define sb3(a,b,c,d,e,f,g,h) \
181
#define ib3(a,b,c,d,e,f,g,h) \
202
#define sb4(a,b,c,d,e,f,g,h) \
221
#define ib4(a,b,c,d,e,f,g,h) \
242
#define sb5(a,b,c,d,e,f,g,h) \
262
#define ib5(a,b,c,d,e,f,g,h) \
282
#define sb6(a,b,c,d,e,f,g,h) \
301
#define ib6(a,b,c,d,e,f,g,h) \
320
#define sb7(a,b,c,d,e,f,g,h) \
341
#define ib7(a,b,c,d,e,f,g,h) \
360
#define k_xor(r,a,b,c,d) \
361
a ^= skey->serpent.K[4 * (r) + 0]; \
362
b ^= skey->serpent.K[4 * (r) + 1]; \
363
c ^= skey->serpent.K[4 * (r) + 2]; \
364
d ^= skey->serpent.K[4 * (r) + 3]
366
#define k_set(r,a,b,c,d) \
367
a = lkey[4 * (r) + 8]; \
368
b = lkey[4 * (r) + 9]; \
369
c = lkey[4 * (r) + 10]; \
370
d = lkey[4 * (r) + 11]
372
#define k_get(r,a,b,c,d) \
373
skey->serpent.K[4 * (r) + 0] = a; \
374
skey->serpent.K[4 * (r) + 1] = b; \
375
skey->serpent.K[4 * (r) + 2] = c; \
376
skey->serpent.K[4 * (r) + 3] = d
378
/* the linear transformation and its inverse */
380
#define rot(a,b,c,d) \
392
#define irot(a,b,c,d) \
405
static int _serpent_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
407
int serpent_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
410
unsigned long lkey[140], t, a, b, c, d, e, f, g, h, x;
411
unsigned long t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16;
412
unsigned char buf[32];
414
_ARGCHK(key != NULL);
415
_ARGCHK(skey != NULL);
418
if (num_rounds != 0 && num_rounds != 32) {
419
return CRYPT_INVALID_ROUNDS;
423
if (keylen < 16 || keylen > 32) {
424
return CRYPT_INVALID_KEYSIZE;
427
/* copy key and expand to 32bytes as required */
428
for (x = 0; x < (unsigned long)keylen; x++) {
433
buf[x++] = (unsigned char)0x01;
435
buf[x++] = (unsigned char)0;
439
/* copy key into 32-bit words */
440
for (x = 0; x < 8; x++) {
441
LOAD32L(lkey[x], &buf[x*4]);
444
/* expand using the LFSR to 140 words */
445
for (x = 0; x < 132; x++) {
446
t = lkey[x] ^ lkey[x+3] ^ lkey[x+5] ^ lkey[x+7] ^ x ^ 0x9E3779B9UL;
447
lkey[x + 8] = ROL(t, 11);
450
/* perform the substituions */
451
for (x = 0; x < 32; ) {
452
k_set( x,a,b,c,d);sb3(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;
453
k_set( x,a,b,c,d);sb2(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;
454
k_set( x,a,b,c,d);sb1(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;
455
k_set( x,a,b,c,d);sb0(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;
456
k_set( x,a,b,c,d);sb7(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;
457
k_set( x,a,b,c,d);sb6(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;
458
k_set( x,a,b,c,d);sb5(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;
459
k_set( x,a,b,c,d);sb4(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;
461
k_set(32,a,b,c,d);sb3(a,b,c,d,e,f,g,h);k_get(32,e,f,g,h);
466
int serpent_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
469
x = _serpent_setup(key, keylen, num_rounds, skey);
470
burn_stack(sizeof(unsigned long)*166 + sizeof(unsigned char)*32);
476
static void _serpent_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
478
void serpent_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
481
unsigned long a,b,c,d,e,f,g,h;
482
unsigned long t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16;
486
_ARGCHK(skey != NULL);
488
LOAD32L(a, &pt[0]);LOAD32L(b, &pt[4]);LOAD32L(c, &pt[8]);LOAD32L(d, &pt[12]);
489
k_xor( 0,a,b,c,d); sb0(a,b,c,d,e,f,g,h); rot(e,f,g,h);
490
k_xor( 1,e,f,g,h); sb1(e,f,g,h,a,b,c,d); rot(a,b,c,d);
491
k_xor( 2,a,b,c,d); sb2(a,b,c,d,e,f,g,h); rot(e,f,g,h);
492
k_xor( 3,e,f,g,h); sb3(e,f,g,h,a,b,c,d); rot(a,b,c,d);
493
k_xor( 4,a,b,c,d); sb4(a,b,c,d,e,f,g,h); rot(e,f,g,h);
494
k_xor( 5,e,f,g,h); sb5(e,f,g,h,a,b,c,d); rot(a,b,c,d);
495
k_xor( 6,a,b,c,d); sb6(a,b,c,d,e,f,g,h); rot(e,f,g,h);
496
k_xor( 7,e,f,g,h); sb7(e,f,g,h,a,b,c,d); rot(a,b,c,d);
497
k_xor( 8,a,b,c,d); sb0(a,b,c,d,e,f,g,h); rot(e,f,g,h);
498
k_xor( 9,e,f,g,h); sb1(e,f,g,h,a,b,c,d); rot(a,b,c,d);
499
k_xor(10,a,b,c,d); sb2(a,b,c,d,e,f,g,h); rot(e,f,g,h);
500
k_xor(11,e,f,g,h); sb3(e,f,g,h,a,b,c,d); rot(a,b,c,d);
501
k_xor(12,a,b,c,d); sb4(a,b,c,d,e,f,g,h); rot(e,f,g,h);
502
k_xor(13,e,f,g,h); sb5(e,f,g,h,a,b,c,d); rot(a,b,c,d);
503
k_xor(14,a,b,c,d); sb6(a,b,c,d,e,f,g,h); rot(e,f,g,h);
504
k_xor(15,e,f,g,h); sb7(e,f,g,h,a,b,c,d); rot(a,b,c,d);
505
k_xor(16,a,b,c,d); sb0(a,b,c,d,e,f,g,h); rot(e,f,g,h);
506
k_xor(17,e,f,g,h); sb1(e,f,g,h,a,b,c,d); rot(a,b,c,d);
507
k_xor(18,a,b,c,d); sb2(a,b,c,d,e,f,g,h); rot(e,f,g,h);
508
k_xor(19,e,f,g,h); sb3(e,f,g,h,a,b,c,d); rot(a,b,c,d);
509
k_xor(20,a,b,c,d); sb4(a,b,c,d,e,f,g,h); rot(e,f,g,h);
510
k_xor(21,e,f,g,h); sb5(e,f,g,h,a,b,c,d); rot(a,b,c,d);
511
k_xor(22,a,b,c,d); sb6(a,b,c,d,e,f,g,h); rot(e,f,g,h);
512
k_xor(23,e,f,g,h); sb7(e,f,g,h,a,b,c,d); rot(a,b,c,d);
513
k_xor(24,a,b,c,d); sb0(a,b,c,d,e,f,g,h); rot(e,f,g,h);
514
k_xor(25,e,f,g,h); sb1(e,f,g,h,a,b,c,d); rot(a,b,c,d);
515
k_xor(26,a,b,c,d); sb2(a,b,c,d,e,f,g,h); rot(e,f,g,h);
516
k_xor(27,e,f,g,h); sb3(e,f,g,h,a,b,c,d); rot(a,b,c,d);
517
k_xor(28,a,b,c,d); sb4(a,b,c,d,e,f,g,h); rot(e,f,g,h);
518
k_xor(29,e,f,g,h); sb5(e,f,g,h,a,b,c,d); rot(a,b,c,d);
519
k_xor(30,a,b,c,d); sb6(a,b,c,d,e,f,g,h); rot(e,f,g,h);
520
k_xor(31,e,f,g,h); sb7(e,f,g,h,a,b,c,d); k_xor(32,a,b,c,d);
521
STORE32L(a, &ct[0]);STORE32L(b, &ct[4]);STORE32L(c, &ct[8]);STORE32L(d, &ct[12]);
525
void serpent_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
527
_serpent_ecb_encrypt(pt, ct, skey);
528
burn_stack(sizeof(unsigned long)*24);
533
static void _serpent_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
535
void serpent_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
538
unsigned long a,b,c,d,e,f,g,h;
539
unsigned long t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16;
543
_ARGCHK(skey != NULL);
545
LOAD32L(a, &ct[0]);LOAD32L(b, &ct[4]);LOAD32L(c, &ct[8]);LOAD32L(d, &ct[12]);
546
k_xor(32,a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor(31,e,f,g,h);
547
irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor(30,a,b,c,d);
548
irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor(29,e,f,g,h);
549
irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor(28,a,b,c,d);
550
irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor(27,e,f,g,h);
551
irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor(26,a,b,c,d);
552
irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor(25,e,f,g,h);
553
irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor(24,a,b,c,d);
554
irot(a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor(23,e,f,g,h);
555
irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor(22,a,b,c,d);
556
irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor(21,e,f,g,h);
557
irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor(20,a,b,c,d);
558
irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor(19,e,f,g,h);
559
irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor(18,a,b,c,d);
560
irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor(17,e,f,g,h);
561
irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor(16,a,b,c,d);
562
irot(a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor(15,e,f,g,h);
563
irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor(14,a,b,c,d);
564
irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor(13,e,f,g,h);
565
irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor(12,a,b,c,d);
566
irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor(11,e,f,g,h);
567
irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor(10,a,b,c,d);
568
irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor( 9,e,f,g,h);
569
irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor( 8,a,b,c,d);
570
irot(a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor( 7,e,f,g,h);
571
irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor( 6,a,b,c,d);
572
irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor( 5,e,f,g,h);
573
irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor( 4,a,b,c,d);
574
irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor( 3,e,f,g,h);
575
irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor( 2,a,b,c,d);
576
irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor( 1,e,f,g,h);
577
irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor( 0,a,b,c,d);
578
STORE32L(a, &pt[0]);STORE32L(b, &pt[4]);STORE32L(c, &pt[8]);STORE32L(d, &pt[12]);
582
void serpent_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
584
_serpent_ecb_decrypt(ct, pt, skey);
585
burn_stack(sizeof(unsigned long)*24);
589
int serpent_test(void)
591
static const struct {
593
unsigned char key[32], pt[16], ct[16];
597
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
598
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
599
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
600
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
601
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
602
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
603
{ 0xdd, 0xd2, 0x6b, 0x98, 0xa5, 0xff, 0xd8, 0x2c,
604
0x05, 0x34, 0x5a, 0x9d, 0xad, 0xbf, 0xaf, 0x49 }
608
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
609
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
610
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
611
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
612
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
613
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 },
614
{ 0x4a, 0xe9, 0xa2, 0x0b, 0x2b, 0x14, 0xa1, 0x02,
615
0x90, 0xcb, 0xb8, 0x20, 0xb7, 0xff, 0xb5, 0x10 }
619
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
620
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
621
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
622
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
623
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
624
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08 },
625
{ 0xe1, 0x1b, 0x01, 0x52, 0x4e, 0xa1, 0xf4, 0x65,
626
0xa2, 0xa2, 0x00, 0x43, 0xeb, 0x9f, 0x7e, 0x8a }
630
{ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
631
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
632
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
633
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
634
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
635
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
636
{ 0xe0, 0x88, 0x5d, 0x44, 0x60, 0x37, 0x34, 0x69,
637
0xd1, 0xfa, 0x6c, 0x36, 0xa6, 0xe1, 0xc5, 0x2f }
641
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
642
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
643
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
644
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
645
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
646
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
647
{ 0x17, 0xc6, 0x25, 0x8e, 0x60, 0x09, 0xe2, 0x82,
648
0x66, 0x18, 0x69, 0xd5, 0x25, 0xf7, 0xd2, 0x04 }
652
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
653
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
654
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
655
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
656
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00,
657
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
658
{ 0x9f, 0xe1, 0x43, 0x25, 0x0d, 0x00, 0xe2, 0x56,
659
0x96, 0xb0, 0x1e, 0x0a, 0x2e, 0xd0, 0x5d, 0xb3 }
663
unsigned char buf[2][16];
667
for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {
669
if ((err = serpent_setup(tests[x].key, tests[x].keylen, 0, &key))!= CRYPT_OK) {
673
/* encrypt and decrypt */
674
serpent_ecb_encrypt(tests[x].pt, buf[0], &key);
675
serpent_ecb_decrypt(buf[0], buf[1], &key);
678
if (memcmp(buf[0], tests[x].ct, 16) != 0 || memcmp(buf[1], tests[x].pt, 16) != 0) {
679
return CRYPT_FAIL_TESTVECTOR;
685
int serpent_keysize(int *desired_keysize)
687
_ARGCHK(desired_keysize != NULL);
689
if (*desired_keysize < 16)
690
return CRYPT_INVALID_KEYSIZE;
691
if (*desired_keysize > 32)
692
*desired_keysize = 32;