serpent-test.c 3.44 KB
Newer Older
1
2
3
#include "testutils.h"
#include "serpent.h"

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
static uint8_t *
decode_hex_reverse (const char *hex)
{
  unsigned length = decode_hex_length (hex);
  uint8_t *p = xalloc(length);
  unsigned i;

  decode_hex(p, hex);

  for (i = 0; i < (length+1)/2; i++)
    {
      uint8_t t = p[i];
      p[i] = p[length - 1 - i];
      p[length - 1 - i] = t;
    }
  return p;
}

#define RH(x) decode_hex_reverse(x)
#define RHL(x) decode_hex_length(x), decode_hex_reverse(x)

25
int
Niels Möller's avatar
Niels Möller committed
26
test_main(void)
27
28
29
30
{
  /* The first test for each key size from the ecb_vk.txt and ecb_vt.txt
   * files in the serpent package. */

31
32
  /* NOTE: These vectors uses strange byte-reversed order of inputs
     and outputs. */
33
34
35
36
  /* 128 bit key */

  /* vk, 1 */
  test_cipher(&nettle_serpent128,
37
38
39
	      RHL("8000000000000000 0000000000000000"),
	      RHL("0000000000000000 0000000000000000"),
	      RH("49AFBFAD9D5A3405 2CD8FFA5986BD2DD"));
40
41
42

  /* vt, 1 */
  test_cipher(&nettle_serpent128,
43
44
45
	      RHL("0000000000000000 0000000000000000"),
	      RHL("8000000000000000 0000000000000000"),
	      RH("10B5FFB720B8CB90 02A1142B0BA2E94A"));
46
47
48
49
50

  /* 192 bit key */

  /* vk, 1 */
  test_cipher(&nettle_serpent192,
51
	      RHL("8000000000000000 0000000000000000"
52
		 "0000000000000000"),
53
54
	      RHL("0000000000000000 0000000000000000"),
	      RH("E78E5402C7195568 AC3678F7A3F60C66"));
55
56
57

  /* vt, 1 */
  test_cipher(&nettle_serpent192,
58
	      RHL("0000000000000000 0000000000000000"
59
		 "0000000000000000"),
60
61
	      RHL("8000000000000000 0000000000000000"),
	      RH("B10B271BA25257E1 294F2B51F076D0D9"));
62
63
64
65
66

  /* 256 bit key */

  /* vk, 1 */
  test_cipher(&nettle_serpent256,
67
	      RHL("8000000000000000 0000000000000000"
68
		 "0000000000000000 0000000000000000"),
69
70
	      RHL("0000000000000000 0000000000000000"),
	      RH("ABED96E766BF28CB C0EBD21A82EF0819"));
71
72
73

  /* vt, 1 */
  test_cipher(&nettle_serpent256,
74
	      RHL("0000000000000000 0000000000000000"
75
		 "0000000000000000 0000000000000000"),
76
77
78
	      RHL("8000000000000000 0000000000000000"),
	      RH("DA5A7992B1B4AE6F 8C004BC8A7DE5520"));

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  /* Test vectors from
     http://www.cs.technion.ac.il/~biham/Reports/Serpent/ */

  /* serpent128 */
  /* Set 4, vector#  0 */
  test_cipher(&nettle_serpent128,
	      HL("000102030405060708090A0B0C0D0E0F"),
	      HL("00112233445566778899AABBCCDDEEFF"),
	      H("563E2CF8740A27C164804560391E9B27"));

  /* Set 4, vector#  1 */
  test_cipher(&nettle_serpent128,
	      HL("2BD6459F82C5B300952C49104881FF48"),
	      HL("EA024714AD5C4D84EA024714AD5C4D84"),
	      H("92D7F8EF2C36C53409F275902F06539F"));

  /* serpent192 */
  /* Set 4, vector#  0 */
  test_cipher(&nettle_serpent192,
	      HL("000102030405060708090A0B0C0D0E0F1011121314151617"),
	      HL("00112233445566778899AABBCCDDEEFF"),
	      H("6AB816C82DE53B93005008AFA2246A02"));

  /* Set 4, vector#  1 */
  test_cipher(&nettle_serpent192,
	      HL("2BD6459F82C5B300952C49104881FF482BD6459F82C5B300"),
	      HL("EA024714AD5C4D84EA024714AD5C4D84"),
	      H("827B18C2678A239DFC5512842000E204"));

  /* serpent256 */
  /* Set 4, vector#  0 */
  test_cipher(&nettle_serpent256,
	      HL("000102030405060708090A0B0C0D0E0F"
		 "101112131415161718191A1B1C1D1E1F"),
	      HL("00112233445566778899AABBCCDDEEFF"),
	      H("2868B7A2D28ECD5E4FDEFAC3C4330074"));

  /* Set 4, vector#  1 */
117
118
119
120
121
  test_cipher(&nettle_serpent256,
	      HL("2BD6459F82C5B300952C49104881FF48"
		 "2BD6459F82C5B300952C49104881FF48"),
	      HL("EA024714AD5C4D84EA024714AD5C4D84"),
	      H("3E507730776B93FDEA661235E1DD99F0"));
122
123
124

  SUCCESS();
}