diff --git a/testsuite/testutils.c b/testsuite/testutils.c index d7886e1be2ec0e88ecc75f2fad02ffe202e18740..657b7f2c57e828be04c10d70a2059bb9678ea18e 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 91ec49621cf97d9aeb339928181fbfde4060e3c6..265cc498df87ec3899a141544be70acf13bfadc5 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,