#include "cbc.h"
#include "ctr.h"
+#include "gcm.h"
#include "knuth-lfib.h"
#include <ctype.h>
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,
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,