]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
Reset GCM mode when setting IV.
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Mon, 7 Feb 2011 12:39:53 +0000 (13:39 +0100)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Mon, 7 Feb 2011 12:39:53 +0000 (13:39 +0100)
lib/nettle/cipher.c

index a2b5407ca8215462fd16b3ec10f9756bf221118f..93c4e5213e41df03e4ea2e3c40971b1415c91c44 100644 (file)
@@ -164,8 +164,6 @@ static void _gcm_encrypt(void *_ctx, nettle_crypt_func f,
 {
 struct gcm_full_ctx *ctx = _ctx;
 
-  gcm_set_iv(&ctx->gcm, GCM_DEFAULT_NONCE_SIZE, iv);
-
   return gcm_encrypt(&ctx->gcm, ctx->cipher, ctx->f, length, dst, src);
 }
 
@@ -176,8 +174,6 @@ static void _gcm_decrypt(void *_ctx, nettle_crypt_func f,
 {
 struct gcm_full_ctx *ctx = _ctx;
 
-  gcm_set_iv(&ctx->gcm, GCM_DEFAULT_NONCE_SIZE, iv);
-
   return gcm_decrypt(&ctx->gcm, ctx->cipher, ctx->f, length, dst, src);
 }
 
@@ -356,14 +352,31 @@ wrap_nettle_cipher_setkey (void *_ctx, const void *key, size_t keysize)
 static int
 wrap_nettle_cipher_setiv (void *_ctx, const void *iv, size_t ivsize)
 {
-  struct nettle_cipher_ctx *ctx = _ctx;
+struct nettle_cipher_ctx *ctx = _ctx;
 
-  if (ivsize > ctx->block_size)
+  switch (ctx->algo)
     {
-      gnutls_assert ();
-      return GNUTLS_E_INVALID_REQUEST;
+#ifdef NETTLE_GCM
+    case GNUTLS_CIPHER_AES_128_GCM:
+      if (ivsize != GCM_DEFAULT_NONCE_SIZE)
+        {
+          gnutls_assert ();
+          return GNUTLS_E_INVALID_REQUEST;
+        }
+
+      ctx->mode_ctx.gcm.gcm.data_size = ctx->mode_ctx.gcm.gcm.auth_size = 0;
+      gcm_set_iv(&ctx->mode_ctx.gcm.gcm, GCM_DEFAULT_NONCE_SIZE, iv);
+      break;
+#endif
+    default:
+      if (ivsize > ctx->block_size)
+        {
+          gnutls_assert ();
+          return GNUTLS_E_INVALID_REQUEST;
+        }
+
+      memcpy (ctx->iv, iv, ivsize);
     }
-  memcpy (ctx->iv, iv, ivsize);
 
   return 0;
 }