summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Eremin-Solenikov <dbaryshkov@gmail.com>2019-07-02 15:38:55 +0300
committerNiels Möller <nisse@lysator.liu.se>2019-07-02 22:30:35 +0200
commiteead7cfa32c341b5193a697273de9fc7336e694a (patch)
treedba7aa502d96f213c1f1a045ba3c7f5540f99745
parentba24450dce47682583a3a207e72d4493619ceaf3 (diff)
downloadnettle-eead7cfa32c341b5193a697273de9fc7336e694a.tar.gz
Move MAC testing code to generic place from cmac-test
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
-rw-r--r--testsuite/cmac-test.c100
-rw-r--r--testsuite/testutils.c64
-rw-r--r--testsuite/testutils.h6
3 files changed, 96 insertions, 74 deletions
diff --git a/testsuite/cmac-test.c b/testsuite/cmac-test.c
index 31662d1b..b1d4aa30 100644
--- a/testsuite/cmac-test.c
+++ b/testsuite/cmac-test.c
@@ -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)
diff --git a/testsuite/testutils.c b/testsuite/testutils.c
index 337e4c4c..2a19c0ac 100644
--- a/testsuite/testutils.c
+++ b/testsuite/testutils.c
@@ -925,6 +925,70 @@ test_hash_large(const struct nettle_hash *hash,
}
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,
const uint8_t *data,
diff --git a/testsuite/testutils.h b/testsuite/testutils.h
index ded57db6..f4ea38da 100644
--- a/testsuite/testutils.h
+++ b/testsuite/testutils.h
@@ -171,6 +171,12 @@ test_hash_large(const struct nettle_hash *hash,
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,
const uint8_t *data,