]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Move MAC testing code to generic place from cmac-test
authorDmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Tue, 2 Jul 2019 12:38:55 +0000 (15:38 +0300)
committerNiels Möller <nisse@lysator.liu.se>
Tue, 2 Jul 2019 20:30:35 +0000 (22:30 +0200)
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
testsuite/cmac-test.c
testsuite/testutils.c
testsuite/testutils.h

index 31662d1b6c1b0ebcaa15082b1c132abf969faa20..b1d4aa30dfbed06d61a380b4883fef11f7762da8 100644 (file)
@@ -2,83 +2,35 @@
 #include "nettle-internal.h"
 #include "cmac.h"
 
+const struct nettle_mac nettle_cmac_aes128 =
+{
+  "CMAC-AES128",
+  sizeof(struct cmac_aes128_ctx),
+  CMAC128_DIGEST_SIZE,
+  AES128_KEY_SIZE,
+
+  (nettle_set_key_func*) cmac_aes128_set_key,
+  (nettle_hash_update_func*) cmac_aes128_update,
+  (nettle_hash_digest_func*) cmac_aes128_digest
+};
+
+const struct nettle_mac nettle_cmac_aes256 =
+{
+  "CMAC-AES256",
+  sizeof(struct cmac_aes256_ctx),
+  CMAC128_DIGEST_SIZE,
+  AES256_KEY_SIZE,
+
+  (nettle_set_key_func*) cmac_aes256_set_key,
+  (nettle_hash_update_func*) cmac_aes256_update,
+  (nettle_hash_digest_func*) cmac_aes256_digest
+};
+
 #define test_cmac_aes128(key, msg, ref)                                        \
-  test_cmac_hash ((nettle_set_key_func*) cmac_aes128_set_key,          \
-                 (nettle_hash_update_func*) cmac_aes128_update,        \
-                 (nettle_hash_digest_func*) cmac_aes128_digest,        \
-                 sizeof(struct cmac_aes128_ctx),                       \
-                 key, msg, ref)
+  test_mac(&nettle_cmac_aes128, key, msg, ref)
 
 #define test_cmac_aes256(key, msg, ref)                                        \
-  test_cmac_hash ((nettle_set_key_func*) cmac_aes256_set_key,          \
-                 (nettle_hash_update_func*) cmac_aes256_update,        \
-                 (nettle_hash_digest_func*) cmac_aes256_digest,        \
-                 sizeof(struct cmac_aes256_ctx),                       \
-                 key, msg, ref)
-
-static void
-test_cmac_hash (nettle_set_key_func *set_key,
-               nettle_hash_update_func *update,
-               nettle_hash_digest_func *digest, size_t ctx_size,
-               const struct tstring *key, const struct tstring *msg,
-               const struct tstring *ref)
-{
-  void *ctx;
-  uint8_t hash[16];
-  unsigned i;
-
-  ctx = xalloc(ctx_size);
-
-  ASSERT (ref->length == sizeof(hash));
-  ASSERT (key->length == 16 || key->length == 32);
-  set_key (ctx, key->data);
-  update (ctx, msg->length, msg->data);
-  digest (ctx, sizeof(hash), hash);
-  if (!MEMEQ (ref->length, ref->data, hash))
-    {
-      fprintf (stderr, "cmac_hash failed, msg: ");
-      print_hex (msg->length, msg->data);
-      fprintf(stderr, "Output:");
-      print_hex (16, hash);
-      fprintf(stderr, "Expected:");
-      tstring_print_hex(ref);
-      fprintf(stderr, "\n");
-      FAIL();
-    }
-
-  /* attempt to re-use the structure */
-  update (ctx, msg->length, msg->data);
-  digest (ctx, sizeof(hash), hash);
-  if (!MEMEQ (ref->length, ref->data, hash))
-    {
-      fprintf (stderr, "cmac_hash failed on re-use, msg: ");
-      print_hex (msg->length, msg->data);
-      fprintf(stderr, "Output:");
-      print_hex (16, hash);
-      fprintf(stderr, "Expected:");
-      tstring_print_hex(ref);
-      fprintf(stderr, "\n");
-      FAIL();
-    }
-
-  /* attempt byte-by-byte hashing */
-  set_key (ctx, key->data);
-  for (i=0;i<msg->length;i++)
-    update (ctx, 1, msg->data+i);
-  digest (ctx, sizeof(hash), hash);
-  if (!MEMEQ (ref->length, ref->data, hash))
-    {
-      fprintf (stderr, "cmac_hash failed on byte-by-byte, msg: ");
-      print_hex (msg->length, msg->data);
-      fprintf(stderr, "Output:");
-      print_hex (16, hash);
-      fprintf(stderr, "Expected:");
-      tstring_print_hex(ref);
-      fprintf(stderr, "\n");
-      FAIL();
-    }
-  free (ctx);
-}
+  test_mac(&nettle_cmac_aes256, key, msg, ref)
 
 void
 test_main(void)
index 337e4c4c7cd171eda3f717b4951366947c361393..2a19c0ac8205a59ef22958b072b8637901af2cc2 100644 (file)
@@ -924,6 +924,70 @@ test_hash_large(const struct nettle_hash *hash,
   free(data);
 }
 
+void
+test_mac(const struct nettle_mac *mac,
+        const struct tstring *key,
+        const struct tstring *msg,
+        const struct tstring *digest)
+{
+  void *ctx = xalloc(mac->context_size);
+  uint8_t *hash = xalloc(mac->digest_size);
+  unsigned i;
+
+
+  ASSERT (digest->length == mac->digest_size);
+  ASSERT (key->length == mac->key_size);
+  mac->set_key (ctx, key->data);
+  mac->update (ctx, msg->length, msg->data);
+  mac->digest (ctx, digest->length, hash);
+
+  if (!MEMEQ (digest->length, digest->data, hash))
+    {
+      fprintf (stderr, "test_mac failed, msg: ");
+      print_hex (msg->length, msg->data);
+      fprintf(stderr, "Output:");
+      print_hex (mac->digest_size, hash);
+      fprintf(stderr, "Expected:");
+      tstring_print_hex(digest);
+      fprintf(stderr, "\n");
+      FAIL();
+    }
+
+  /* attempt to re-use the structure */
+  mac->update (ctx, msg->length, msg->data);
+  mac->digest (ctx, digest->length, hash);
+  if (!MEMEQ (digest->length, digest->data, hash))
+    {
+      fprintf (stderr, "test_mac: failed on re-use, msg: ");
+      print_hex (msg->length, msg->data);
+      fprintf(stderr, "Output:");
+      print_hex (mac->digest_size, hash);
+      fprintf(stderr, "Expected:");
+      tstring_print_hex(digest);
+      fprintf(stderr, "\n");
+      FAIL();
+    }
+
+  /* attempt byte-by-byte hashing */
+  mac->set_key (ctx, key->data);
+  for (i=0;i<msg->length;i++)
+    mac->update (ctx, 1, msg->data+i);
+  mac->digest (ctx, digest->length, hash);
+  if (!MEMEQ (digest->length, digest->data, hash))
+    {
+      fprintf (stderr, "cmac_hash failed on byte-by-byte, msg: ");
+      print_hex (msg->length, msg->data);
+      fprintf(stderr, "Output:");
+      print_hex (16, hash);
+      fprintf(stderr, "Expected:");
+      tstring_print_hex(digest);
+      fprintf(stderr, "\n");
+      FAIL();
+    }
+  free (ctx);
+  free (hash);
+}
+
 void
 test_armor(const struct nettle_armor *armor,
            size_t data_length,
index ded57db6ab4f24edf6c3ac29a979677dcb9edb79..f4ea38da9deb17b795b9825bd14cd03d41823b81 100644 (file)
@@ -170,6 +170,12 @@ test_hash_large(const struct nettle_hash *hash,
                uint8_t c,
                const struct tstring *digest);
 
+void
+test_mac(const struct nettle_mac *mac,
+        const struct tstring *key,
+        const struct tstring *msg,
+        const struct tstring *digest);
+
 void
 test_armor(const struct nettle_armor *armor,
            size_t data_length,