diff --git a/testsuite/testutils.c b/testsuite/testutils.c index a0b3924b8f124a966b8a86b7113c48fd83366463..3228cb73484dde5e055968b5b34e6b93997cba69 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 91ec49621cf97d9aeb339928181fbfde4060e3c6..bbfa468dda497f9d01ba895722c04375efca4838 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,