From 1abba6a83c2fda927a7913180e405e5b8599d47e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se> Date: Mon, 14 Feb 2011 14:30:20 +0100 Subject: [PATCH] (test_aead): New function, replacing test_gcm_aes and before that test_cipher_gcm. Rev: nettle/testsuite/testutils.c:1.14 Rev: nettle/testsuite/testutils.h:1.8 --- testsuite/testutils.c | 62 ++++++++++++++++++++++++++++++++++++++++++- testsuite/testutils.h | 16 +++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/testsuite/testutils.c b/testsuite/testutils.c index d7886e1b..657b7f2c 100644 --- a/testsuite/testutils.c +++ b/testsuite/testutils.c @@ -4,8 +4,8 @@ #include "cbc.h" #include "ctr.h" -#include "gcm.h" #include "knuth-lfib.h" +#include "nettle-internal.h" #include <ctype.h> #include <stdio.h> @@ -362,6 +362,66 @@ test_cipher_stream(const struct nettle_cipher *cipher, free(data); } +void +test_aead(const struct nettle_aead *aead, + unsigned key_length, + const uint8_t *key, + unsigned auth_length, + const uint8_t *authtext, + unsigned length, + const uint8_t *cleartext, + const uint8_t *ciphertext, + unsigned iv_length, + const uint8_t *iv, + const uint8_t *digest) +{ + void *ctx = xalloc(aead->context_size); + uint8_t *data = xalloc(length); + uint8_t *buffer = xalloc(aead->block_size); + + /* encryption */ + memset(buffer, 0, aead->block_size); + aead->set_key(ctx, key_length, key); + + aead->set_iv(ctx, iv_length, iv); + + if (auth_length) + aead->update(ctx, auth_length, authtext); + + if (length) + aead->encrypt(ctx, length, data, cleartext); + + aead->digest(ctx, aead->block_size, buffer); + + if (!MEMEQ(length, data, ciphertext)) + FAIL(); + + if (!MEMEQ(aead->block_size, buffer, digest)) + FAIL(); + + /* decryption */ + memset(buffer, 0, aead->block_size); + aead->set_iv(ctx, iv_length, iv); + + if (auth_length) + aead->update(ctx, auth_length, authtext); + + if (length) + aead->decrypt(ctx, length, data, data); + + aead->digest(ctx, aead->block_size, buffer); + + if (!MEMEQ(length, data, cleartext)) + FAIL(); + + if (!MEMEQ(aead->block_size, buffer, digest)) + FAIL(); + + free(ctx); + free(data); + free(buffer); +} + void test_hash(const struct nettle_hash *hash, unsigned length, diff --git a/testsuite/testutils.h b/testsuite/testutils.h index 91ec4962..265cc498 100644 --- a/testsuite/testutils.h +++ b/testsuite/testutils.h @@ -22,6 +22,9 @@ #include "nettle-meta.h" +/* Forward declare */ +struct nettle_aead; + #ifdef __cplusplus extern "C" { #endif @@ -113,6 +116,19 @@ test_cipher_stream(const struct nettle_cipher *cipher, const uint8_t *cleartext, const uint8_t *ciphertext); +void +test_aead(const struct nettle_aead *aead, + unsigned key_length, + const uint8_t *key, + unsigned auth_length, + const uint8_t *authtext, + unsigned length, + const uint8_t *cleartext, + const uint8_t *ciphertext, + unsigned iv_length, + const uint8_t *iv, + const uint8_t *digest); + void test_hash(const struct nettle_hash *hash, unsigned length, -- GitLab