From d61ae2d8880ec73149347aea73d49d39ff5f5ec2 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos <nmav@gnutls.org> Date: Sat, 5 Feb 2011 23:47:11 +0100 Subject: [PATCH] (test_cipher_gcm): New function, contributed by Nikos Mavrogiannopoulos. Rev: nettle/testsuite/testutils.c:1.12 Rev: nettle/testsuite/testutils.h:1.6 --- testsuite/testutils.c | 66 +++++++++++++++++++++++++++++++++++++++++++ testsuite/testutils.h | 13 +++++++++ 2 files changed, 79 insertions(+) diff --git a/testsuite/testutils.c b/testsuite/testutils.c index a0b3924b..3228cb73 100644 --- a/testsuite/testutils.c +++ b/testsuite/testutils.c @@ -4,6 +4,7 @@ #include "cbc.h" #include "ctr.h" +#include "gcm.h" #include "knuth-lfib.h" #include <ctype.h> @@ -295,6 +296,71 @@ test_cipher_ctr(const struct nettle_cipher *cipher, free(ctr); } +void +test_cipher_gcm(const struct nettle_cipher *cipher, + 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) +{ + struct gcm_ctx gctx; + void *cipher_ctx = xalloc(cipher->context_size); + uint8_t *data = xalloc(length); + uint8_t buffer[GCM_BLOCK_SIZE]; + + /* encryption */ + memset(buffer, 0, sizeof(buffer)); + cipher->set_encrypt_key(cipher_ctx, key_length, key); + + gcm_set_key(&gctx, cipher_ctx, cipher->encrypt); + gcm_set_iv(&gctx, iv_length, iv); + + if (auth_length) + gcm_auth(&gctx, auth_length, authtext); + + if (length) + gcm_encrypt(&gctx, cipher_ctx, cipher->encrypt, + length, data, cleartext); + + gcm_digest(&gctx, cipher_ctx, cipher->encrypt, + GCM_BLOCK_SIZE, buffer); + + if (!MEMEQ(length, data, ciphertext)) + FAIL(); + + if (!MEMEQ(GCM_BLOCK_SIZE, buffer, digest)) + FAIL(); + + /* decryption */ + memset(buffer, 0, sizeof(buffer)); + gcm_set_iv(&gctx, iv_length, iv); + + if (auth_length) + gcm_auth(&gctx, auth_length, authtext); + + if (length) + gcm_decrypt(&gctx, cipher_ctx, cipher->encrypt, + length, data, data); + + gcm_digest(&gctx, cipher_ctx, cipher->encrypt, + GCM_BLOCK_SIZE, buffer); + + if (!MEMEQ(length, data, cleartext)) + FAIL(); + + if (!MEMEQ(GCM_BLOCK_SIZE, buffer, digest)) + FAIL(); + + free(cipher_ctx); + free(data); +} + void test_cipher_stream(const struct nettle_cipher *cipher, unsigned key_length, diff --git a/testsuite/testutils.h b/testsuite/testutils.h index 91ec4962..bbfa468d 100644 --- a/testsuite/testutils.h +++ b/testsuite/testutils.h @@ -105,6 +105,19 @@ test_cipher_ctr(const struct nettle_cipher *cipher, const uint8_t *ciphertext, const uint8_t *iv); +void +test_cipher_gcm(const struct nettle_cipher *cipher, + 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_cipher_stream(const struct nettle_cipher *cipher, unsigned key_length, -- GitLab