gcm-test.c 12.4 KB
Newer Older
1
#include "testutils.h"
2
#include "nettle-internal.h"
Niels Möller's avatar
Niels Möller committed
3 4 5 6 7
#include "gcm.h"

static void
test_gcm_hash (const struct tstring *msg, const struct tstring *ref)
{
Niels Möller's avatar
Niels Möller committed
8
  struct gcm_aes128_ctx ctx;
Niels Möller's avatar
Niels Möller committed
9 10 11 12
  const uint8_t z16[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  uint8_t digest[16];

  ASSERT (ref->length == sizeof(digest));
Niels Möller's avatar
Niels Möller committed
13 14 15 16
  gcm_aes128_set_key (&ctx, 16, z16);
  gcm_aes128_set_iv (&ctx, 16, z16);
  gcm_aes128_update (&ctx, msg->length, msg->data);
  gcm_aes128_digest (&ctx, sizeof(digest), digest);
Niels Möller's avatar
Niels Möller committed
17 18 19 20 21 22 23 24 25 26
  if (!MEMEQ (ref->length, ref->data, digest))
    {
      fprintf (stderr, "gcm_hash failed, msg: %s\nOutput: ", msg->data);
      print_hex (16, digest);
      fprintf(stderr, "Expected:");
      tstring_print_hex(ref);
      fprintf(stderr, "\n");
      FAIL();
    }
}
27

Niels Möller's avatar
Niels Möller committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
static void
gcm_aes128_set_key_wrapper (void *ctx, size_t length, const uint8_t *key)
{
  ASSERT (length == AES128_KEY_SIZE);
  gcm_aes_set_key (ctx, length, key);
}
static const struct nettle_aead
nettle_gcm_unified_aes128 = {
  "gcm-aes128",
  sizeof (struct gcm_aes_ctx),
  GCM_BLOCK_SIZE,
  AES128_KEY_SIZE,
  gcm_aes128_set_key_wrapper,
  (nettle_set_key_func *) gcm_aes_set_iv,
  (nettle_hash_update_func *) gcm_aes_update,
  (nettle_crypt_func *) gcm_aes_encrypt,
  (nettle_crypt_func *) gcm_aes_decrypt,
  (nettle_hash_digest_func *) gcm_aes_digest
};
    

49
void
50 51 52 53
test_main(void)
{
  /* 
   * GCM-AES Test Vectors from
Niels Möller's avatar
Niels Möller committed
54
   * http://www.cryptobarn.com/papers/gcm-spec.pdf
55 56 57
   */

  /* Test case 1 */
58
  test_aead(&nettle_gcm_aes128,
59 60 61 62 63 64
	    SHEX("00000000000000000000000000000000"),	/* key */
	    SHEX(""),					/* auth data */ 
	    SHEX(""),					/* plaintext */
	    SHEX(""),					/* ciphertext*/
	    SHEX("000000000000000000000000"),		/* IV */
	    SHEX("58e2fccefa7e3061367f1d57a4e7455a"));	/* tag */
65 66

  /* Test case 2 */
67
  test_aead(&nettle_gcm_aes128,
68 69 70 71 72 73
	    SHEX("00000000000000000000000000000000"),
	    SHEX(""),
	    SHEX("00000000000000000000000000000000"),
	    SHEX("0388dace60b6a392f328c2b971b2fe78"),
	    SHEX("000000000000000000000000"),
	    SHEX("ab6e47d42cec13bdf53a67b21257bddf"));
74 75

  /* Test case 3 */
76
  test_aead(&nettle_gcm_aes128,
77 78 79 80 81 82 83 84 85 86 87 88
	    SHEX("feffe9928665731c6d6a8f9467308308"),
	    SHEX(""),
	    SHEX("d9313225f88406e5a55909c5aff5269a"
		 "86a7a9531534f7da2e4c303d8a318a72"
		 "1c3c0c95956809532fcf0e2449a6b525"
		 "b16aedf5aa0de657ba637b391aafd255"),
	    SHEX("42831ec2217774244b7221b784d0d49c"
		 "e3aa212f2c02a4e035c17e2329aca12e"
		 "21d514b25466931c7d8f6a5aac84aa05"
		 "1ba30b396a0aac973d58e091473f5985"),
	    SHEX("cafebabefacedbaddecaf888"),
	    SHEX("4d5c2af327cd64a62cf35abd2ba6fab4"));
89 90

  /* Test case 4 */
91
  test_aead(&nettle_gcm_aes128,
92 93 94 95 96 97 98 99 100 101 102 103 104
	    SHEX("feffe9928665731c6d6a8f9467308308"),
	    SHEX("feedfacedeadbeeffeedfacedeadbeef"
		 "abaddad2"),
	    SHEX("d9313225f88406e5a55909c5aff5269a"
		 "86a7a9531534f7da2e4c303d8a318a72"
		 "1c3c0c95956809532fcf0e2449a6b525"
		 "b16aedf5aa0de657ba637b39"),
	    SHEX("42831ec2217774244b7221b784d0d49c"
		 "e3aa212f2c02a4e035c17e2329aca12e"
		 "21d514b25466931c7d8f6a5aac84aa05"
		 "1ba30b396a0aac973d58e091"),
	    SHEX("cafebabefacedbaddecaf888"),
	    SHEX("5bc94fbc3221a5db94fae95ae7121a47"));
105 106

  /* Test case 5 */
107
  test_aead(&nettle_gcm_aes128,
108 109 110 111 112 113 114 115 116 117 118 119 120
	    SHEX("feffe9928665731c6d6a8f9467308308"),
	    SHEX("feedfacedeadbeeffeedfacedeadbeef"
		 "abaddad2"),
	    SHEX("d9313225f88406e5a55909c5aff5269a"
		 "86a7a9531534f7da2e4c303d8a318a72"
		 "1c3c0c95956809532fcf0e2449a6b525"
		 "b16aedf5aa0de657ba637b39"),
	    SHEX("61353b4c2806934a777ff51fa22a4755"
		 "699b2a714fcdc6f83766e5f97b6c7423"
		 "73806900e49f24b22b097544d4896b42"
		 "4989b5e1ebac0f07c23f4598"),
	    SHEX("cafebabefacedbad"),
	    SHEX("3612d2e79e3b0785561be14aaca2fccb"));
121 122

  /* Test case 6 */
123
  test_aead(&nettle_gcm_aes128,
124 125
	    SHEX("feffe9928665731c6d6a8f9467308308"),
	    SHEX("feedfacedeadbeeffeedfacedeadbeef"
Niels Möller's avatar
Niels Möller committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
		 "abaddad2"),
	    SHEX("d9313225f88406e5a55909c5aff5269a"
		 "86a7a9531534f7da2e4c303d8a318a72"
		 "1c3c0c95956809532fcf0e2449a6b525"
		 "b16aedf5aa0de657ba637b39"),
	    SHEX("8ce24998625615b603a033aca13fb894"
		 "be9112a5c3a211a8ba262a3cca7e2ca7"
		 "01e4a9a4fba43c90ccdcb281d48c7c6f"
		 "d62875d2aca417034c34aee5"),
	    SHEX("9313225df88406e555909c5aff5269aa"
		 "6a7a9538534f7da1e4c303d2a318a728"
		 "c3c0c95156809539fcf0e2429a6b5254"
		 "16aedbf5a0de6a57a637b39b"),
	    SHEX("619cc5aefffe0bfa462af43c1699d050"));

  /* Same test, but with old gcm_aes interface */
  test_aead(&nettle_gcm_unified_aes128,
	    SHEX("feffe9928665731c6d6a8f9467308308"),
	    SHEX("feedfacedeadbeeffeedfacedeadbeef"
145 146 147 148 149 150 151 152 153 154 155 156 157 158
		 "abaddad2"),
	    SHEX("d9313225f88406e5a55909c5aff5269a"
		 "86a7a9531534f7da2e4c303d8a318a72"
		 "1c3c0c95956809532fcf0e2449a6b525"
		 "b16aedf5aa0de657ba637b39"),
	    SHEX("8ce24998625615b603a033aca13fb894"
		 "be9112a5c3a211a8ba262a3cca7e2ca7"
		 "01e4a9a4fba43c90ccdcb281d48c7c6f"
		 "d62875d2aca417034c34aee5"),
	    SHEX("9313225df88406e555909c5aff5269aa"
		 "6a7a9538534f7da1e4c303d2a318a728"
		 "c3c0c95156809539fcf0e2429a6b5254"
		 "16aedbf5a0de6a57a637b39b"),
	    SHEX("619cc5aefffe0bfa462af43c1699d050"));
Niels Möller's avatar
Niels Möller committed
159

Niels Möller's avatar
Niels Möller committed
160
  /* Test case 7 */
Niels Möller's avatar
Niels Möller committed
161
  test_aead(&nettle_gcm_aes192,
162 163 164 165 166 167 168
	    SHEX("00000000000000000000000000000000"
		 "0000000000000000"),
	    SHEX(""),
	    SHEX(""),
	    SHEX(""),
	    SHEX("000000000000000000000000"),
	    SHEX("cd33b28ac773f74ba00ed1f312572435"));
Niels Möller's avatar
Niels Möller committed
169 170

  /* Test case 8 */
Niels Möller's avatar
Niels Möller committed
171
  test_aead(&nettle_gcm_aes192,
172 173 174 175 176 177 178
	    SHEX("00000000000000000000000000000000"
		 "0000000000000000"),
	    SHEX(""),
	    SHEX("00000000000000000000000000000000"),
	    SHEX("98e7247c07f0fe411c267e4384b0f600"),
	    SHEX("000000000000000000000000"),
	    SHEX("2ff58d80033927ab8ef4d4587514f0fb"));
179

Niels Möller's avatar
Niels Möller committed
180
  /* Test case 9 */
Niels Möller's avatar
Niels Möller committed
181
  test_aead(&nettle_gcm_aes192,
182 183 184 185 186 187 188 189 190 191 192 193 194
	    SHEX("feffe9928665731c6d6a8f9467308308"
		 "feffe9928665731c"),
	    SHEX(""),
	    SHEX("d9313225f88406e5a55909c5aff5269a"
		 "86a7a9531534f7da2e4c303d8a318a72"
		 "1c3c0c95956809532fcf0e2449a6b525"
		 "b16aedf5aa0de657ba637b391aafd255"),
	    SHEX("3980ca0b3c00e841eb06fac4872a2757"
		 "859e1ceaa6efd984628593b40ca1e19c"
		 "7d773d00c144c525ac619d18c84a3f47"
		 "18e2448b2fe324d9ccda2710acade256"),
	    SHEX("cafebabefacedbaddecaf888"),
	    SHEX("9924a7c8587336bfb118024db8674a14"));
Niels Möller's avatar
Niels Möller committed
195 196

  /* Test case 10 */
Niels Möller's avatar
Niels Möller committed
197
  test_aead(&nettle_gcm_aes192,
198 199 200 201 202 203 204 205 206 207 208 209 210 211
	    SHEX("feffe9928665731c6d6a8f9467308308"
		 "feffe9928665731c"),
	    SHEX("feedfacedeadbeeffeedfacedeadbeef"
		 "abaddad2"),
	    SHEX("d9313225f88406e5a55909c5aff5269a"
		 "86a7a9531534f7da2e4c303d8a318a72"
		 "1c3c0c95956809532fcf0e2449a6b525"
		 "b16aedf5aa0de657ba637b39"),
	    SHEX("3980ca0b3c00e841eb06fac4872a2757"
		 "859e1ceaa6efd984628593b40ca1e19c"
		 "7d773d00c144c525ac619d18c84a3f47"
		 "18e2448b2fe324d9ccda2710"),
	    SHEX("cafebabefacedbaddecaf888"),
	    SHEX("2519498e80f1478f37ba55bd6d27618c"));
Niels Möller's avatar
Niels Möller committed
212 213

  /* Test case 11 */
Niels Möller's avatar
Niels Möller committed
214
  test_aead(&nettle_gcm_aes192,
215 216 217 218 219 220 221 222 223 224 225 226 227 228
	    SHEX("feffe9928665731c6d6a8f9467308308"
		 "feffe9928665731c"),
	    SHEX("feedfacedeadbeeffeedfacedeadbeef"
		 "abaddad2"),
	    SHEX("d9313225f88406e5a55909c5aff5269a"
		 "86a7a9531534f7da2e4c303d8a318a72"
		 "1c3c0c95956809532fcf0e2449a6b525"
		 "b16aedf5aa0de657ba637b39"),
	    SHEX("0f10f599ae14a154ed24b36e25324db8"
		 "c566632ef2bbb34f8347280fc4507057"
		 "fddc29df9a471f75c66541d4d4dad1c9"
		 "e93a19a58e8b473fa0f062f7"),
	    SHEX("cafebabefacedbad"),
	    SHEX("65dcc57fcf623a24094fcca40d3533f8"));
229

Niels Möller's avatar
Niels Möller committed
230
  /* Test case 12 */
Niels Möller's avatar
Niels Möller committed
231
  test_aead(&nettle_gcm_aes192,
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	    SHEX("feffe9928665731c6d6a8f9467308308"
		 "feffe9928665731c"),
	    SHEX("feedfacedeadbeeffeedfacedeadbeef"
		 "abaddad2"),
	    SHEX("d9313225f88406e5a55909c5aff5269a"
		 "86a7a9531534f7da2e4c303d8a318a72"
		 "1c3c0c95956809532fcf0e2449a6b525"
		 "b16aedf5aa0de657ba637b39"),
	    SHEX("d27e88681ce3243c4830165a8fdcf9ff"
		 "1de9a1d8e6b447ef6ef7b79828666e45"
		 "81e79012af34ddd9e2f037589b292db3"
		 "e67c036745fa22e7e9b7373b"),
	    SHEX("9313225df88406e555909c5aff5269aa"
		 "6a7a9538534f7da1e4c303d2a318a728"
		 "c3c0c95156809539fcf0e2429a6b5254"
		 "16aedbf5a0de6a57a637b39b"),
	    SHEX("dcf566ff291c25bbb8568fc3d376a6d9"));
Niels Möller's avatar
Niels Möller committed
249 250

  /* Test case 13 */
Niels Möller's avatar
Niels Möller committed
251
  test_aead(&nettle_gcm_aes256,
252 253 254 255 256 257 258
	    SHEX("00000000000000000000000000000000"
		 "00000000000000000000000000000000"),
	    SHEX(""),
	    SHEX(""),
	    SHEX(""),
	    SHEX("000000000000000000000000"),
	    SHEX("530f8afbc74536b9a963b4f1c4cb738b"));
Niels Möller's avatar
Niels Möller committed
259 260

  /* Test case 14 */
Niels Möller's avatar
Niels Möller committed
261
  test_aead(&nettle_gcm_aes256,
262 263 264 265 266 267 268
	    SHEX("00000000000000000000000000000000"
		 "00000000000000000000000000000000"),
	    SHEX(""),
	    SHEX("00000000000000000000000000000000"),
	    SHEX("cea7403d4d606b6e074ec5d3baf39d18"),
	    SHEX("000000000000000000000000"),
	    SHEX("d0d1c8a799996bf0265b98b5d48ab919"));
Niels Möller's avatar
Niels Möller committed
269 270

  /* Test case 15 */
Niels Möller's avatar
Niels Möller committed
271
  test_aead(&nettle_gcm_aes256,
272 273 274 275 276 277 278 279 280 281 282 283 284
	    SHEX("feffe9928665731c6d6a8f9467308308"
		 "feffe9928665731c6d6a8f9467308308"),
	    SHEX(""),
	    SHEX("d9313225f88406e5a55909c5aff5269a"
		 "86a7a9531534f7da2e4c303d8a318a72"
		 "1c3c0c95956809532fcf0e2449a6b525"
		 "b16aedf5aa0de657ba637b391aafd255"),
	    SHEX("522dc1f099567d07f47f37a32a84427d"
		 "643a8cdcbfe5c0c97598a2bd2555d1aa"
		 "8cb08e48590dbb3da7b08b1056828838"
		 "c5f61e6393ba7a0abcc9f662898015ad"),
	    SHEX("cafebabefacedbaddecaf888"),
	    SHEX("b094dac5d93471bdec1a502270e3cc6c"));
Niels Möller's avatar
Niels Möller committed
285 286

  /* Test case 16 */
Niels Möller's avatar
Niels Möller committed
287
  test_aead(&nettle_gcm_aes256,
288 289 290 291 292 293 294 295 296 297 298 299 300 301
	    SHEX("feffe9928665731c6d6a8f9467308308"
		 "feffe9928665731c6d6a8f9467308308"),
	    SHEX("feedfacedeadbeeffeedfacedeadbeef"
		 "abaddad2"),
	    SHEX("d9313225f88406e5a55909c5aff5269a"
		 "86a7a9531534f7da2e4c303d8a318a72"
		 "1c3c0c95956809532fcf0e2449a6b525"
		 "b16aedf5aa0de657ba637b39"),
	    SHEX("522dc1f099567d07f47f37a32a84427d"
		 "643a8cdcbfe5c0c97598a2bd2555d1aa"
		 "8cb08e48590dbb3da7b08b1056828838"
		 "c5f61e6393ba7a0abcc9f662"),
	    SHEX("cafebabefacedbaddecaf888"),
	    SHEX("76fc6ece0f4e1768cddf8853bb2d551b"));
Niels Möller's avatar
Niels Möller committed
302 303

  /* Test case 17 */
Niels Möller's avatar
Niels Möller committed
304
  test_aead(&nettle_gcm_aes256,
305 306 307 308 309 310 311 312 313 314 315 316 317 318
	    SHEX("feffe9928665731c6d6a8f9467308308"
		 "feffe9928665731c6d6a8f9467308308"),
	    SHEX("feedfacedeadbeeffeedfacedeadbeef"
		 "abaddad2"),
	    SHEX("d9313225f88406e5a55909c5aff5269a"
		 "86a7a9531534f7da2e4c303d8a318a72"
		 "1c3c0c95956809532fcf0e2449a6b525"
		 "b16aedf5aa0de657ba637b39"),
	    SHEX("c3762df1ca787d32ae47c13bf19844cb"
		 "af1ae14d0b976afac52ff7d79bba9de0"
		 "feb582d33934a4f0954cc2363bc73f78"
		 "62ac430e64abe499f47c9b1f"),
	    SHEX("cafebabefacedbad"),
	    SHEX("3a337dbf46a792c45e454913fe2ea8f2"));
Niels Möller's avatar
Niels Möller committed
319 320

  /* Test case 18 */
Niels Möller's avatar
Niels Möller committed
321
  test_aead(&nettle_gcm_aes256,
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
	    SHEX("feffe9928665731c6d6a8f9467308308"
		 "feffe9928665731c6d6a8f9467308308"),
	    SHEX("feedfacedeadbeeffeedfacedeadbeef"
		 "abaddad2"),
	    SHEX("d9313225f88406e5a55909c5aff5269a"
		 "86a7a9531534f7da2e4c303d8a318a72"
		 "1c3c0c95956809532fcf0e2449a6b525"
		 "b16aedf5aa0de657ba637b39"),
	    SHEX("5a8def2f0c9e53f1f75d7853659e2a20"
		 "eeb2b22aafde6419a058ab4f6f746bf4"
		 "0fc0c3b780f244452da3ebf1c5d82cde"
		 "a2418997200ef82e44ae7e3f"),
	    SHEX("9313225df88406e555909c5aff5269aa"
		 "6a7a9538534f7da1e4c303d2a318a728"
		 "c3c0c95156809539fcf0e2429a6b5254"
		 "16aedbf5a0de6a57a637b39b"),
	    SHEX("a44a8266ee1c8eb0c8b5d4cf5ae9f19a"));
Niels Möller's avatar
Niels Möller committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377

  /* Test gcm_hash, with varying message size, keys and iv all zero.
     Not compared to any other implementation. */
  test_gcm_hash (SDATA("a"),
		 SHEX("1521c9a442bbf63b 2293a21d4874a5fd"));
  test_gcm_hash (SDATA("ab"),
		 SHEX("afb4592d2c7c1687 37f27271ee30412a"));
  test_gcm_hash (SDATA("abc"), 
		 SHEX("9543ca3e1662ba03 9a921ec2a20769be"));
  test_gcm_hash (SDATA("abcd"),
		 SHEX("8f041cc12bcb7e1b 0257a6da22ee1185"));
  test_gcm_hash (SDATA("abcde"),
		 SHEX("0b2376e5fed58ffb 717b520c27cd5c35"));
  test_gcm_hash (SDATA("abcdef"), 
		 SHEX("9679497a1eafa161 4942963380c1a76f"));
  test_gcm_hash (SDATA("abcdefg"),
		 SHEX("83862e40339536bc 723d9817f7df8282"));
  test_gcm_hash (SDATA("abcdefgh"), 
		 SHEX("b73bcc4d6815c4dc d7424a04e61b87c5"));
  test_gcm_hash (SDATA("abcdefghi"), 
		 SHEX("8e7846a383f0b3b2 07b01160a5ef993d"));
  test_gcm_hash (SDATA("abcdefghij"),
		 SHEX("37651643b6f8ecac 4ea1b320e6ea308c"));
  test_gcm_hash (SDATA("abcdefghijk"), 
		 SHEX("c1ce10106ee23286 f00513f55e2226b0"));
  test_gcm_hash (SDATA("abcdefghijkl"),
		 SHEX("c6a3e32a90196cdf b2c7a415d637e6ca"));
  test_gcm_hash (SDATA("abcdefghijklm"), 
		 SHEX("6cca29389d4444fa 3d20e65497088fd8"));
  test_gcm_hash (SDATA("abcdefghijklmn"),
		 SHEX("19476a997ec0a824 2022db0f0e8455ce"));
  test_gcm_hash (SDATA("abcdefghijklmno"), 
		 SHEX("f66931cee7eadcbb d42753c3ac3c4c16"));
  test_gcm_hash (SDATA("abcdefghijklmnop"),
		 SHEX("a79699ce8bed61f9 b8b1b4c5abb1712e"));
  test_gcm_hash (SDATA("abcdefghijklmnopq"), 
		 SHEX("65f8245330febf15 6fd95e324304c258"));
  test_gcm_hash (SDATA("abcdefghijklmnopqr"),
		 SHEX("d07259e85d4fc998 5a662eed41c8ed1d"));
378 379
}