serpent-test.c 4.25 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  /* From libgcrypt */
  test_cipher(&nettle_serpent128,
	      HL("0000000000000000 0000000000000000"),
	      HL("D29D576FCEA3A3A7 ED9099F29273D78E"),
	      H("B2288B968AE8B086 48D1CE9606FD992D"));
  test_cipher(&nettle_serpent192,
	      HL("0000000000000000 0000000000000000 0000000000000000"),
	      HL("D29D576FCEABA3A7 ED9899F2927BD78E"),
	      H("130E353E1037C224 05E8FAEFB2C3C3E9"));
  test_cipher(&nettle_serpent256,
	      HL("0000000000000000 0000000000000000"
		 "0000000000000000 0000000000000000"),
	      HL("D095576FCEA3E3A7 ED98D9F29073D78E"),
	      H("B90EE5862DE69168 F2BDD5125B45472B"));
  test_cipher(&nettle_serpent256,
	      HL("0000000000000000 0000000000000000"
		 "0000000000000000 0000000000000000"),
	      HL("0000000001000000 0200000003000000"),
	      H("2061A42782BD52EC 691EC383B03BA77C"));

48 49 50
  /* The first test for each key size from the ecb_vk.txt and ecb_vt.txt
   * files in the serpent package. */

51 52
  /* NOTE: These vectors uses strange byte-reversed order of inputs
     and outputs. */
53 54 55 56
  /* 128 bit key */

  /* vk, 1 */
  test_cipher(&nettle_serpent128,
57 58 59
	      RHL("8000000000000000 0000000000000000"),
	      RHL("0000000000000000 0000000000000000"),
	      RH("49AFBFAD9D5A3405 2CD8FFA5986BD2DD"));
60 61 62

  /* vt, 1 */
  test_cipher(&nettle_serpent128,
63 64 65
	      RHL("0000000000000000 0000000000000000"),
	      RHL("8000000000000000 0000000000000000"),
	      RH("10B5FFB720B8CB90 02A1142B0BA2E94A"));
66 67 68 69 70

  /* 192 bit key */

  /* vk, 1 */
  test_cipher(&nettle_serpent192,
71
	      RHL("8000000000000000 0000000000000000"
72
		 "0000000000000000"),
73 74
	      RHL("0000000000000000 0000000000000000"),
	      RH("E78E5402C7195568 AC3678F7A3F60C66"));
75 76 77

  /* vt, 1 */
  test_cipher(&nettle_serpent192,
78
	      RHL("0000000000000000 0000000000000000"
79
		 "0000000000000000"),
80 81
	      RHL("8000000000000000 0000000000000000"),
	      RH("B10B271BA25257E1 294F2B51F076D0D9"));
82 83 84 85 86

  /* 256 bit key */

  /* vk, 1 */
  test_cipher(&nettle_serpent256,
87
	      RHL("8000000000000000 0000000000000000"
88
		 "0000000000000000 0000000000000000"),
89 90
	      RHL("0000000000000000 0000000000000000"),
	      RH("ABED96E766BF28CB C0EBD21A82EF0819"));
91 92 93

  /* vt, 1 */
  test_cipher(&nettle_serpent256,
94
	      RHL("0000000000000000 0000000000000000"
95
		 "0000000000000000 0000000000000000"),
96 97 98
	      RHL("8000000000000000 0000000000000000"),
	      RH("DA5A7992B1B4AE6F 8C004BC8A7DE5520"));

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  /* 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 */
137 138 139 140 141
  test_cipher(&nettle_serpent256,
	      HL("2BD6459F82C5B300952C49104881FF48"
		 "2BD6459F82C5B300952C49104881FF48"),
	      HL("EA024714AD5C4D84EA024714AD5C4D84"),
	      H("3E507730776B93FDEA661235E1DD99F0"));
142 143 144

  SUCCESS();
}