cd0318
From 2f61f102169e4d6652c9b82246353cd276366809 Mon Sep 17 00:00:00 2001
cd0318
From: Daiki Ueno <ueno@gnu.org>
cd0318
Date: Mon, 27 Jun 2022 11:14:50 +0900
cd0318
Subject: [PATCH] cipher: limit plaintext length supplied to AES-GCM
cd0318
cd0318
According to SP800-38D 5.2.1.1, input data length of AES-GCM
cd0318
encryption function must be less than or equal to 2^39-256 bits.
cd0318
cd0318
Signed-off-by: Daiki Ueno <ueno@gnu.org>
cd0318
---
cd0318
 NEWS                                         |  3 +
cd0318
 lib/accelerated/aarch64/aes-aarch64.h        | 15 ++++
cd0318
 lib/accelerated/aarch64/aes-gcm-aarch64.c    |  9 +++
cd0318
 lib/accelerated/x86/aes-gcm-padlock.c        | 29 ++++---
cd0318
 lib/accelerated/x86/aes-gcm-x86-aesni.c      | 30 +++++---
cd0318
 lib/accelerated/x86/aes-gcm-x86-pclmul-avx.c |  9 +++
cd0318
 lib/accelerated/x86/aes-gcm-x86-pclmul.c     |  9 +++
cd0318
 lib/accelerated/x86/aes-gcm-x86-ssse3.c      | 30 +++++---
cd0318
 lib/accelerated/x86/aes-x86.h                | 15 ++++
cd0318
 lib/nettle/cipher.c                          | 41 ++++++++++
cd0318
 tests/slow/cipher-api-test.c                 | 79 ++++++++++++++++++++
cd0318
 11 files changed, 240 insertions(+), 29 deletions(-)
cd0318
cd0318
diff --git a/lib/accelerated/aarch64/aes-aarch64.h b/lib/accelerated/aarch64/aes-aarch64.h
cd0318
index 692d8620d7..0e64f4ed8d 100644
cd0318
--- a/lib/accelerated/aarch64/aes-aarch64.h
cd0318
+++ b/lib/accelerated/aarch64/aes-aarch64.h
cd0318
@@ -20,6 +20,21 @@ typedef struct {
cd0318
 	if (s != 16 && s != 24 && s != 32) \
cd0318
 		return GNUTLS_E_INVALID_REQUEST
cd0318
 
cd0318
+#include <intprops.h>
cd0318
+#define AES_GCM_ENCRYPT_MAX_BYTES ((1ULL << 36) - 32)
cd0318
+static inline int
cd0318
+record_aes_gcm_encrypt_size(size_t *counter, size_t size) {
cd0318
+	size_t sum;
cd0318
+
cd0318
+	if (!INT_ADD_OK(*counter, size, &sum) ||
cd0318
+	    sum > AES_GCM_ENCRYPT_MAX_BYTES) {
cd0318
+		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
cd0318
+	}
cd0318
+	*counter = sum;
cd0318
+
cd0318
+	return 0;
cd0318
+}
cd0318
+
cd0318
 int aes_v8_set_encrypt_key(const unsigned char *userKey, int bits, AES_KEY *key);  
cd0318
 int aes_v8_set_decrypt_key(const unsigned char *userKey, int bits, AES_KEY *key);
cd0318
 void aes_v8_cbc_encrypt(const unsigned char *in, unsigned char *out,
cd0318
diff --git a/lib/accelerated/aarch64/aes-gcm-aarch64.c b/lib/accelerated/aarch64/aes-gcm-aarch64.c
cd0318
index 901bd9f60f..be1e69c784 100644
cd0318
--- a/lib/accelerated/aarch64/aes-gcm-aarch64.c
cd0318
+++ b/lib/accelerated/aarch64/aes-gcm-aarch64.c
cd0318
@@ -62,6 +62,7 @@ struct aes_gcm_ctx {
cd0318
 	struct gcm128_context gcm;
cd0318
 	unsigned finished;
cd0318
 	unsigned auth_finished;
cd0318
+	size_t rekey_counter;
cd0318
 };
cd0318
 
cd0318
 void gcm_init_v8(u128 Htable[16], const uint64_t Xi[2]);
cd0318
@@ -116,6 +117,7 @@ aes_gcm_cipher_setkey(void *_ctx, const void *userkey, size_t keysize)
cd0318
 	ctx->gcm.H.u[1] = bswap_64(ctx->gcm.H.u[1]);
cd0318
 
cd0318
 	gcm_init_v8(ctx->gcm.Htable, ctx->gcm.H.u);
cd0318
+	ctx->rekey_counter = 0;
cd0318
 
cd0318
 	return 0;
cd0318
 }
cd0318
@@ -141,6 +143,7 @@ static int aes_gcm_setiv(void *_ctx, const void *iv, size_t iv_size)
cd0318
 	ctx->gcm.Yi.c[GCM_BLOCK_SIZE - 1] = 2;
cd0318
 	ctx->finished = 0;
cd0318
 	ctx->auth_finished = 0;
cd0318
+	ctx->rekey_counter = 0;
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -229,6 +232,7 @@ aes_gcm_encrypt(void *_ctx, const void *src, size_t src_size,
cd0318
 	int exp_blocks = blocks * GCM_BLOCK_SIZE;
cd0318
 	int rest = src_size - (exp_blocks);
cd0318
 	uint32_t counter;
cd0318
+	int ret;
cd0318
 
cd0318
 	if (unlikely(ctx->finished))
cd0318
 		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
cd0318
@@ -236,6 +240,11 @@ aes_gcm_encrypt(void *_ctx, const void *src, size_t src_size,
cd0318
 	if (unlikely(length < src_size))
cd0318
 		return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
cd0318
 
cd0318
+	ret = record_aes_gcm_encrypt_size(&ctx->rekey_counter, src_size);
cd0318
+	if (ret < 0) {
cd0318
+		return gnutls_assert_val(ret);
cd0318
+	}
cd0318
+
cd0318
 	if (blocks > 0) {
cd0318
 		ctr32_encrypt_blocks(src, dst,
cd0318
 				     blocks,
cd0318
diff --git a/lib/accelerated/x86/aes-gcm-padlock.c b/lib/accelerated/x86/aes-gcm-padlock.c
cd0318
index a9c7441d65..739883ab1b 100644
cd0318
--- a/lib/accelerated/x86/aes-gcm-padlock.c
cd0318
+++ b/lib/accelerated/x86/aes-gcm-padlock.c
cd0318
@@ -43,7 +43,10 @@
cd0318
  * Actually padlock doesn't include GCM mode. We just use
cd0318
  * the ECB part of padlock and nettle for everything else.
cd0318
  */
cd0318
-struct gcm_padlock_aes_ctx GCM_CTX(struct padlock_ctx);
cd0318
+struct gcm_padlock_aes_ctx {
cd0318
+	struct GCM_CTX(struct padlock_ctx) inner;
cd0318
+	size_t rekey_counter;
cd0318
+};
cd0318
 
cd0318
 static void padlock_aes_encrypt(const void *_ctx,
cd0318
 				size_t length, uint8_t * dst,
cd0318
@@ -78,7 +81,7 @@ static void padlock_aes256_set_encrypt_key(struct padlock_ctx *_ctx,
cd0318
 
cd0318
 static void aes_gcm_deinit(void *_ctx)
cd0318
 {
cd0318
-	struct padlock_ctx *ctx = _ctx;
cd0318
+	struct gcm_padlock_aes_ctx *ctx = _ctx;
cd0318
 
cd0318
 	zeroize_temp_key(ctx, sizeof(*ctx));
cd0318
 	gnutls_free(ctx);
cd0318
@@ -108,14 +111,15 @@ aes_gcm_cipher_setkey(void *_ctx, const void *key, size_t keysize)
cd0318
 	struct gcm_padlock_aes_ctx *ctx = _ctx;
cd0318
 
cd0318
 	if (keysize == 16) {
cd0318
-		GCM_SET_KEY(ctx, padlock_aes128_set_encrypt_key, padlock_aes_encrypt,
cd0318
+		GCM_SET_KEY(&ctx->inner, padlock_aes128_set_encrypt_key, padlock_aes_encrypt,
cd0318
 			    key);
cd0318
 	} else if (keysize == 32) {
cd0318
-		GCM_SET_KEY(ctx, padlock_aes256_set_encrypt_key, padlock_aes_encrypt,
cd0318
+		GCM_SET_KEY(&ctx->inner, padlock_aes256_set_encrypt_key, padlock_aes_encrypt,
cd0318
 			    key);
cd0318
 	} else
cd0318
 		return GNUTLS_E_INVALID_REQUEST;
cd0318
 
cd0318
+	ctx->rekey_counter = 0;
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -126,8 +130,9 @@ static int aes_gcm_setiv(void *_ctx, const void *iv, size_t iv_size)
cd0318
 	if (iv_size != GCM_BLOCK_SIZE - 4)
cd0318
 		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
cd0318
 
cd0318
-	GCM_SET_IV(ctx, iv_size, iv);
cd0318
+	GCM_SET_IV(&ctx->inner, iv_size, iv);
cd0318
 
cd0318
+	ctx->rekey_counter = 0;
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -136,11 +141,17 @@ aes_gcm_encrypt(void *_ctx, const void *src, size_t src_size,
cd0318
 		void *dst, size_t length)
cd0318
 {
cd0318
 	struct gcm_padlock_aes_ctx *ctx = _ctx;
cd0318
+	int ret;
cd0318
 
cd0318
 	if (unlikely(length < src_size))
cd0318
 		return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
cd0318
 
cd0318
-	GCM_ENCRYPT(ctx, padlock_aes_encrypt, src_size, dst, src);
cd0318
+	ret = record_aes_gcm_encrypt_size(&ctx->rekey_counter, src_size);
cd0318
+	if (ret < 0) {
cd0318
+		return gnutls_assert_val(ret);
cd0318
+	}
cd0318
+
cd0318
+	GCM_ENCRYPT(&ctx->inner, padlock_aes_encrypt, src_size, dst, src);
cd0318
 
cd0318
 	return 0;
cd0318
 }
cd0318
@@ -154,7 +165,7 @@ aes_gcm_decrypt(void *_ctx, const void *src, size_t src_size,
cd0318
 	if (unlikely(dst_size < src_size))
cd0318
 		return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
cd0318
 
cd0318
-	GCM_DECRYPT(ctx, padlock_aes_encrypt, src_size, dst, src);
cd0318
+	GCM_DECRYPT(&ctx->inner, padlock_aes_encrypt, src_size, dst, src);
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -162,7 +173,7 @@ static int aes_gcm_auth(void *_ctx, const void *src, size_t src_size)
cd0318
 {
cd0318
 	struct gcm_padlock_aes_ctx *ctx = _ctx;
cd0318
 
cd0318
-	GCM_UPDATE(ctx, src_size, src);
cd0318
+	GCM_UPDATE(&ctx->inner, src_size, src);
cd0318
 
cd0318
 	return 0;
cd0318
 }
cd0318
@@ -171,7 +182,7 @@ static void aes_gcm_tag(void *_ctx, void *tag, size_t tagsize)
cd0318
 {
cd0318
 	struct gcm_padlock_aes_ctx *ctx = _ctx;
cd0318
 
cd0318
-	GCM_DIGEST(ctx, padlock_aes_encrypt, tagsize, tag);
cd0318
+	GCM_DIGEST(&ctx->inner, padlock_aes_encrypt, tagsize, tag);
cd0318
 }
cd0318
 
cd0318
 #include "aes-gcm-aead.h"
cd0318
diff --git a/lib/accelerated/x86/aes-gcm-x86-aesni.c b/lib/accelerated/x86/aes-gcm-x86-aesni.c
cd0318
index b0edaebfba..3be63ddd97 100644
cd0318
--- a/lib/accelerated/x86/aes-gcm-x86-aesni.c
cd0318
+++ b/lib/accelerated/x86/aes-gcm-x86-aesni.c
cd0318
@@ -36,12 +36,14 @@
cd0318
 #include <x86-common.h>
cd0318
 #include <byteswap.h>
cd0318
 #include <nettle/gcm.h>
cd0318
-#include <aes-x86.h>
cd0318
 
cd0318
 /* GCM mode 
cd0318
  * It is used when the CPU doesn't include the PCLMUL instructions.
cd0318
  */
cd0318
-struct gcm_x86_aes_ctx GCM_CTX(AES_KEY);
cd0318
+struct gcm_x86_aes_ctx {
cd0318
+	struct GCM_CTX(AES_KEY) inner;
cd0318
+	size_t rekey_counter;
cd0318
+};
cd0318
 
cd0318
 static void x86_aes_encrypt(const void *_ctx,
cd0318
 				size_t length, uint8_t * dst,
cd0318
@@ -101,17 +103,18 @@ aes_gcm_cipher_setkey(void *_ctx, const void *key, size_t length)
cd0318
 	struct gcm_x86_aes_ctx *ctx = _ctx;
cd0318
 
cd0318
 	if (length == 16) {
cd0318
-		GCM_SET_KEY(ctx, x86_aes128_set_encrypt_key, x86_aes_encrypt,
cd0318
+		GCM_SET_KEY(&ctx->inner, x86_aes128_set_encrypt_key, x86_aes_encrypt,
cd0318
 			    key);
cd0318
 	} else if (length == 24) {
cd0318
-		GCM_SET_KEY(ctx, x86_aes192_set_encrypt_key, x86_aes_encrypt,
cd0318
+		GCM_SET_KEY(&ctx->inner, x86_aes192_set_encrypt_key, x86_aes_encrypt,
cd0318
 			    key);
cd0318
 	} else if (length == 32) {
cd0318
-		GCM_SET_KEY(ctx, x86_aes256_set_encrypt_key, x86_aes_encrypt,
cd0318
+		GCM_SET_KEY(&ctx->inner, x86_aes256_set_encrypt_key, x86_aes_encrypt,
cd0318
 			    key);
cd0318
 	} else
cd0318
 		return GNUTLS_E_INVALID_REQUEST;
cd0318
 
cd0318
+	ctx->rekey_counter = 0;
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -122,8 +125,9 @@ static int aes_gcm_setiv(void *_ctx, const void *iv, size_t iv_size)
cd0318
 	if (iv_size != GCM_BLOCK_SIZE - 4)
cd0318
 		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
cd0318
 
cd0318
-	GCM_SET_IV(ctx, iv_size, iv);
cd0318
+	GCM_SET_IV(&ctx->inner, iv_size, iv);
cd0318
 
cd0318
+	ctx->rekey_counter = 0;
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -132,11 +136,17 @@ aes_gcm_encrypt(void *_ctx, const void *src, size_t src_size,
cd0318
 		void *dst, size_t length)
cd0318
 {
cd0318
 	struct gcm_x86_aes_ctx *ctx = _ctx;
cd0318
+	int ret;
cd0318
 
cd0318
 	if (unlikely(length < src_size))
cd0318
 		return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
cd0318
 
cd0318
-	GCM_ENCRYPT(ctx, x86_aes_encrypt, src_size, dst, src);
cd0318
+	ret = record_aes_gcm_encrypt_size(&ctx->rekey_counter, src_size);
cd0318
+	if (ret < 0) {
cd0318
+		return gnutls_assert_val(ret);
cd0318
+	}
cd0318
+
cd0318
+	GCM_ENCRYPT(&ctx->inner, x86_aes_encrypt, src_size, dst, src);
cd0318
 
cd0318
 	return 0;
cd0318
 }
cd0318
@@ -150,7 +160,7 @@ aes_gcm_decrypt(void *_ctx, const void *src, size_t src_size,
cd0318
 	if (unlikely(dst_size < src_size))
cd0318
 		return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
cd0318
 
cd0318
-	GCM_DECRYPT(ctx, x86_aes_encrypt, src_size, dst, src);
cd0318
+	GCM_DECRYPT(&ctx->inner, x86_aes_encrypt, src_size, dst, src);
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -158,7 +168,7 @@ static int aes_gcm_auth(void *_ctx, const void *src, size_t src_size)
cd0318
 {
cd0318
 	struct gcm_x86_aes_ctx *ctx = _ctx;
cd0318
 
cd0318
-	GCM_UPDATE(ctx, src_size, src);
cd0318
+	GCM_UPDATE(&ctx->inner, src_size, src);
cd0318
 
cd0318
 	return 0;
cd0318
 }
cd0318
@@ -167,7 +177,7 @@ static void aes_gcm_tag(void *_ctx, void *tag, size_t tagsize)
cd0318
 {
cd0318
 	struct gcm_x86_aes_ctx *ctx = _ctx;
cd0318
 
cd0318
-	GCM_DIGEST(ctx, x86_aes_encrypt, tagsize, tag);
cd0318
+	GCM_DIGEST(&ctx->inner, x86_aes_encrypt, tagsize, tag);
cd0318
 }
cd0318
 
cd0318
 static void aes_gcm_deinit(void *_ctx)
cd0318
diff --git a/lib/accelerated/x86/aes-gcm-x86-pclmul-avx.c b/lib/accelerated/x86/aes-gcm-x86-pclmul-avx.c
cd0318
index 21aef94440..fbefe432f4 100644
cd0318
--- a/lib/accelerated/x86/aes-gcm-x86-pclmul-avx.c
cd0318
+++ b/lib/accelerated/x86/aes-gcm-x86-pclmul-avx.c
cd0318
@@ -61,6 +61,7 @@ struct aes_gcm_ctx {
cd0318
 	struct gcm128_context gcm;
cd0318
 	unsigned finished;
cd0318
 	unsigned auth_finished;
cd0318
+	size_t rekey_counter;
cd0318
 };
cd0318
 
cd0318
 void gcm_init_avx(u128 Htable[16], const uint64_t Xi[2]);
cd0318
@@ -116,6 +117,7 @@ aes_gcm_cipher_setkey(void *_ctx, const void *userkey, size_t keysize)
cd0318
 
cd0318
 	gcm_init_avx(ctx->gcm.Htable, ctx->gcm.H.u);
cd0318
 
cd0318
+	ctx->rekey_counter = 0;
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -140,6 +142,7 @@ static int aes_gcm_setiv(void *_ctx, const void *iv, size_t iv_size)
cd0318
 	ctx->gcm.Yi.c[GCM_BLOCK_SIZE - 1] = 2;
cd0318
 	ctx->finished = 0;
cd0318
 	ctx->auth_finished = 0;
cd0318
+	ctx->rekey_counter = 0;
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -184,6 +187,7 @@ aes_gcm_encrypt(void *_ctx, const void *src, size_t src_size,
cd0318
 	int exp_blocks = blocks * GCM_BLOCK_SIZE;
cd0318
 	int rest = src_size - (exp_blocks);
cd0318
 	uint32_t counter;
cd0318
+	int ret;
cd0318
 
cd0318
 	if (unlikely(ctx->finished))
cd0318
 		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
cd0318
@@ -191,6 +195,11 @@ aes_gcm_encrypt(void *_ctx, const void *src, size_t src_size,
cd0318
 	if (unlikely(length < src_size))
cd0318
 		return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
cd0318
 
cd0318
+	ret = record_aes_gcm_encrypt_size(&ctx->rekey_counter, src_size);
cd0318
+	if (ret < 0) {
cd0318
+		return gnutls_assert_val(ret);
cd0318
+	}
cd0318
+
cd0318
 	if (blocks > 0) {
cd0318
 		aesni_ctr32_encrypt_blocks(src, dst,
cd0318
 					   blocks,
cd0318
diff --git a/lib/accelerated/x86/aes-gcm-x86-pclmul.c b/lib/accelerated/x86/aes-gcm-x86-pclmul.c
cd0318
index e6b4990cbf..5385acbb6b 100644
cd0318
--- a/lib/accelerated/x86/aes-gcm-x86-pclmul.c
cd0318
+++ b/lib/accelerated/x86/aes-gcm-x86-pclmul.c
cd0318
@@ -60,6 +60,7 @@ struct aes_gcm_ctx {
cd0318
 	struct gcm128_context gcm;
cd0318
 	unsigned finished;
cd0318
 	unsigned auth_finished;
cd0318
+	size_t rekey_counter;
cd0318
 };
cd0318
 
cd0318
 void gcm_init_clmul(u128 Htable[16], const uint64_t Xi[2]);
cd0318
@@ -116,6 +117,7 @@ aes_gcm_cipher_setkey(void *_ctx, const void *userkey, size_t keysize)
cd0318
 
cd0318
 	gcm_init_clmul(ctx->gcm.Htable, ctx->gcm.H.u);
cd0318
 
cd0318
+	ctx->rekey_counter = 0;
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -140,6 +142,7 @@ static int aes_gcm_setiv(void *_ctx, const void *iv, size_t iv_size)
cd0318
 	ctx->gcm.Yi.c[GCM_BLOCK_SIZE - 1] = 2;
cd0318
 	ctx->finished = 0;
cd0318
 	ctx->auth_finished = 0;
cd0318
+	ctx->rekey_counter = 0;
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -184,6 +187,7 @@ aes_gcm_encrypt(void *_ctx, const void *src, size_t src_size,
cd0318
 	int exp_blocks = blocks * GCM_BLOCK_SIZE;
cd0318
 	int rest = src_size - (exp_blocks);
cd0318
 	uint32_t counter;
cd0318
+	int ret;
cd0318
 
cd0318
 	if (unlikely(ctx->finished))
cd0318
 		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
cd0318
@@ -191,6 +195,11 @@ aes_gcm_encrypt(void *_ctx, const void *src, size_t src_size,
cd0318
 	if (unlikely(length < src_size))
cd0318
 		return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
cd0318
 
cd0318
+	ret = record_aes_gcm_encrypt_size(&ctx->rekey_counter, src_size);
cd0318
+	if (ret < 0) {
cd0318
+		return gnutls_assert_val(ret);
cd0318
+	}
cd0318
+
cd0318
 	if (blocks > 0) {
cd0318
 		aesni_ctr32_encrypt_blocks(src, dst,
cd0318
 					   blocks,
cd0318
diff --git a/lib/accelerated/x86/aes-gcm-x86-ssse3.c b/lib/accelerated/x86/aes-gcm-x86-ssse3.c
cd0318
index 7a2ac50869..f074cb1096 100644
cd0318
--- a/lib/accelerated/x86/aes-gcm-x86-ssse3.c
cd0318
+++ b/lib/accelerated/x86/aes-gcm-x86-ssse3.c
cd0318
@@ -36,13 +36,15 @@
cd0318
 #include <x86-common.h>
cd0318
 #include <byteswap.h>
cd0318
 #include <nettle/gcm.h>
cd0318
-#include <aes-x86.h>
cd0318
 #include <assert.h>
cd0318
 
cd0318
 /* GCM mode 
cd0318
  * It is used when the CPU doesn't include the PCLMUL instructions.
cd0318
  */
cd0318
-struct gcm_x86_aes_ctx GCM_CTX(AES_KEY);
cd0318
+struct gcm_x86_aes_ctx {
cd0318
+	struct GCM_CTX(AES_KEY) inner;
cd0318
+	size_t rekey_counter;
cd0318
+};
cd0318
 
cd0318
 static void x86_aes_encrypt(const void *_ctx,
cd0318
 				size_t length, uint8_t * dst,
cd0318
@@ -110,17 +112,18 @@ aes_gcm_cipher_setkey(void *_ctx, const void *key, size_t keysize)
cd0318
 	struct gcm_x86_aes_ctx *ctx = _ctx;
cd0318
 
cd0318
 	if (keysize == 16) {
cd0318
-		GCM_SET_KEY(ctx, x86_aes_128_set_encrypt_key, x86_aes_encrypt,
cd0318
+		GCM_SET_KEY(&ctx->inner, x86_aes_128_set_encrypt_key, x86_aes_encrypt,
cd0318
 			    key);
cd0318
 	} else if (keysize == 24) {
cd0318
-		GCM_SET_KEY(ctx, x86_aes_192_set_encrypt_key, x86_aes_encrypt,
cd0318
+		GCM_SET_KEY(&ctx->inner, x86_aes_192_set_encrypt_key, x86_aes_encrypt,
cd0318
 			    key);
cd0318
 	} else if (keysize == 32) {
cd0318
-		GCM_SET_KEY(ctx, x86_aes_256_set_encrypt_key, x86_aes_encrypt,
cd0318
+		GCM_SET_KEY(&ctx->inner, x86_aes_256_set_encrypt_key, x86_aes_encrypt,
cd0318
 			    key);
cd0318
 	} else
cd0318
 		return GNUTLS_E_INVALID_REQUEST;
cd0318
 
cd0318
+	ctx->rekey_counter = 0;
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -131,8 +134,9 @@ static int aes_gcm_setiv(void *_ctx, const void *iv, size_t iv_size)
cd0318
 	if (iv_size != GCM_BLOCK_SIZE - 4)
cd0318
 		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
cd0318
 
cd0318
-	GCM_SET_IV(ctx, iv_size, iv);
cd0318
+	GCM_SET_IV(&ctx->inner, iv_size, iv);
cd0318
 
cd0318
+	ctx->rekey_counter = 0;
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -141,11 +145,17 @@ aes_gcm_encrypt(void *_ctx, const void *src, size_t src_size,
cd0318
 		void *dst, size_t length)
cd0318
 {
cd0318
 	struct gcm_x86_aes_ctx *ctx = _ctx;
cd0318
+	int ret;
cd0318
 
cd0318
 	if (unlikely(length < src_size))
cd0318
 		return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
cd0318
 
cd0318
-	GCM_ENCRYPT(ctx, x86_aes_encrypt, src_size, dst, src);
cd0318
+	ret = record_aes_gcm_encrypt_size(&ctx->rekey_counter, src_size);
cd0318
+	if (ret < 0) {
cd0318
+		return gnutls_assert_val(ret);
cd0318
+	}
cd0318
+
cd0318
+	GCM_ENCRYPT(&ctx->inner, x86_aes_encrypt, src_size, dst, src);
cd0318
 
cd0318
 	return 0;
cd0318
 }
cd0318
@@ -159,7 +169,7 @@ aes_gcm_decrypt(void *_ctx, const void *src, size_t src_size,
cd0318
 	if (unlikely(dst_size < src_size))
cd0318
 		return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
cd0318
 
cd0318
-	GCM_DECRYPT(ctx, x86_aes_encrypt, src_size, dst, src);
cd0318
+	GCM_DECRYPT(&ctx->inner, x86_aes_encrypt, src_size, dst, src);
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -167,7 +177,7 @@ static int aes_gcm_auth(void *_ctx, const void *src, size_t src_size)
cd0318
 {
cd0318
 	struct gcm_x86_aes_ctx *ctx = _ctx;
cd0318
 
cd0318
-	GCM_UPDATE(ctx, src_size, src);
cd0318
+	GCM_UPDATE(&ctx->inner, src_size, src);
cd0318
 
cd0318
 	return 0;
cd0318
 }
cd0318
@@ -176,7 +186,7 @@ static void aes_gcm_tag(void *_ctx, void *tag, size_t tagsize)
cd0318
 {
cd0318
 	struct gcm_x86_aes_ctx *ctx = _ctx;
cd0318
 
cd0318
-	GCM_DIGEST(ctx, x86_aes_encrypt, tagsize, tag);
cd0318
+	GCM_DIGEST(&ctx->inner, x86_aes_encrypt, tagsize, tag);
cd0318
 }
cd0318
 
cd0318
 static void aes_gcm_deinit(void *_ctx)
cd0318
diff --git a/lib/accelerated/x86/aes-x86.h b/lib/accelerated/x86/aes-x86.h
cd0318
index 023b5f7be6..349d3d5d9c 100644
cd0318
--- a/lib/accelerated/x86/aes-x86.h
cd0318
+++ b/lib/accelerated/x86/aes-x86.h
cd0318
@@ -22,6 +22,21 @@ typedef struct {
cd0318
 	if (s != 16 && s != 24 && s != 32) \
cd0318
 		return GNUTLS_E_INVALID_REQUEST
cd0318
 
cd0318
+#include <intprops.h>
cd0318
+#define AES_GCM_ENCRYPT_MAX_BYTES ((1ULL << 36) - 32)
cd0318
+static inline int
cd0318
+record_aes_gcm_encrypt_size(size_t *counter, size_t size) {
cd0318
+	size_t sum;
cd0318
+
cd0318
+	if (!INT_ADD_OK(*counter, size, &sum) ||
cd0318
+	    sum > AES_GCM_ENCRYPT_MAX_BYTES) {
cd0318
+		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
cd0318
+	}
cd0318
+	*counter = sum;
cd0318
+
cd0318
+	return 0;
cd0318
+}
cd0318
+
cd0318
 void aesni_ecb_encrypt(const unsigned char *in, unsigned char *out,
cd0318
 		       size_t len, const AES_KEY * key, int enc);
cd0318
 
cd0318
diff --git a/lib/nettle/cipher.c b/lib/nettle/cipher.c
cd0318
index ab4c46d2d0..b41862d1ea 100644
cd0318
--- a/lib/nettle/cipher.c
cd0318
+++ b/lib/nettle/cipher.c
cd0318
@@ -63,6 +63,7 @@
cd0318
 #include <nettle/xts.h>
cd0318
 #include <nettle/siv-cmac.h>
cd0318
 #include <fips.h>
cd0318
+#include <intprops.h>
cd0318
 
cd0318
 struct nettle_cipher_ctx;
cd0318
 
cd0318
@@ -120,8 +121,23 @@ struct nettle_cipher_ctx {
cd0318
 	unsigned iv_size;
cd0318
 
cd0318
 	bool enc;
cd0318
+	size_t rekey_counter;
cd0318
 };
cd0318
 
cd0318
+#define AES_GCM_ENCRYPT_MAX_BYTES ((1ULL << 36) - 32)
cd0318
+static inline int
cd0318
+record_aes_gcm_encrypt_size(size_t *counter, size_t size) {
cd0318
+	size_t sum;
cd0318
+
cd0318
+	if (!INT_ADD_OK(*counter, size, &sum) ||
cd0318
+	    sum > AES_GCM_ENCRYPT_MAX_BYTES) {
cd0318
+		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
cd0318
+	}
cd0318
+	*counter = sum;
cd0318
+
cd0318
+	return 0;
cd0318
+}
cd0318
+
cd0318
 static void
cd0318
 _stream_encrypt(struct nettle_cipher_ctx *ctx, size_t length, uint8_t * dst,
cd0318
 		const uint8_t * src)
cd0318
@@ -1133,6 +1149,16 @@ wrap_nettle_cipher_setkey(void *_ctx, const void *key, size_t keysize)
cd0318
 	else
cd0318
 		ctx->cipher->set_decrypt_key(ctx->ctx_ptr, key);
cd0318
 
cd0318
+	switch (ctx->cipher->algo) {
cd0318
+	case GNUTLS_CIPHER_AES_128_GCM:
cd0318
+	case GNUTLS_CIPHER_AES_192_GCM:
cd0318
+	case GNUTLS_CIPHER_AES_256_GCM:
cd0318
+		ctx->rekey_counter = 0;
cd0318
+		break;
cd0318
+	default:
cd0318
+		break;
cd0318
+	}
cd0318
+
cd0318
 	return 0;
cd0318
 }
cd0318
 
cd0318
@@ -1147,6 +1173,7 @@ wrap_nettle_cipher_setiv(void *_ctx, const void *iv, size_t iv_size)
cd0318
 	case GNUTLS_CIPHER_AES_192_GCM:
cd0318
 	case GNUTLS_CIPHER_AES_256_GCM:
cd0318
 		FIPS_RULE(iv_size < GCM_IV_SIZE, GNUTLS_E_INVALID_REQUEST, "access to short GCM nonce size\n");
cd0318
+		ctx->rekey_counter = 0;
cd0318
 		break;
cd0318
 	case GNUTLS_CIPHER_SALSA20_256:
cd0318
 	case GNUTLS_CIPHER_ESTREAM_SALSA20_256:
cd0318
@@ -1207,10 +1234,24 @@ wrap_nettle_cipher_encrypt(void *_ctx, const void *plain, size_t plain_size,
cd0318
 			   void *encr, size_t encr_size)
cd0318
 {
cd0318
 	struct nettle_cipher_ctx *ctx = _ctx;
cd0318
+	int ret;
cd0318
 
cd0318
 	if (unlikely(ctx->cipher->encrypt == NULL))
cd0318
 		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
cd0318
 
cd0318
+	switch (ctx->cipher->algo) {
cd0318
+	case GNUTLS_CIPHER_AES_128_GCM:
cd0318
+	case GNUTLS_CIPHER_AES_192_GCM:
cd0318
+	case GNUTLS_CIPHER_AES_256_GCM:
cd0318
+		ret = record_aes_gcm_encrypt_size(&ctx->rekey_counter, plain_size);
cd0318
+		if (ret < 0) {
cd0318
+			return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
cd0318
+		}
cd0318
+		break;
cd0318
+	default:
cd0318
+		break;
cd0318
+	}
cd0318
+
cd0318
 	ctx->cipher->encrypt(ctx, plain_size, encr, plain);
cd0318
 
cd0318
 	return 0;
cd0318
diff --git a/tests/slow/cipher-api-test.c b/tests/slow/cipher-api-test.c
cd0318
index fc880bcc9f..1d267ce312 100644
cd0318
--- a/tests/slow/cipher-api-test.c
cd0318
+++ b/tests/slow/cipher-api-test.c
cd0318
@@ -21,6 +21,7 @@
cd0318
  */
cd0318
 
cd0318
 #include <config.h>
cd0318
+#include <limits.h>
cd0318
 #include <stdint.h>
cd0318
 #include <stdio.h>
cd0318
 #include <string.h>
cd0318
@@ -48,6 +49,11 @@ int main(int argc, char **argv)
cd0318
 #include <assert.h>
cd0318
 #include <utils.h>
cd0318
 
cd0318
+#define AES_GCM_ENCRYPT_PLAINTEXT_MAX ((1ULL << 36) - 32)
cd0318
+#if SIZE_MAX >= AES_GCM_ENCRYPT_PLAINTEXT_MAX
cd0318
+#define TEST_AES_GCM_ENCRYPT_PLAINTEXT_SIZE 1
cd0318
+#endif
cd0318
+
cd0318
 static void tls_log_func(int level, const char *str)
cd0318
 {
cd0318
 	fprintf(stderr, "<%d>| %s", level, str);
cd0318
@@ -401,6 +407,74 @@ static void test_aead_invalid_short_decrypt(int algo)
cd0318
 	return;
cd0318
 }
cd0318
 
cd0318
+#ifdef TEST_AES_GCM_ENCRYPT_PLAINTEXT_SIZE
cd0318
+/* Test whether an invalid call to gnutls_cipher_encrypt() with too
cd0318
+ * long message is caught */
cd0318
+static void test_aead_invalid_too_long_encrypt(int algo)
cd0318
+{
cd0318
+	int ret;
cd0318
+	gnutls_cipher_hd_t ch;
cd0318
+	uint8_t key16[64];
cd0318
+	uint8_t iv16[32];
cd0318
+	uint8_t data[128];
cd0318
+	gnutls_datum_t key, iv;
cd0318
+
cd0318
+	if (algo != GNUTLS_CIPHER_AES_128_GCM &&
cd0318
+	    algo != GNUTLS_CIPHER_AES_192_GCM &&
cd0318
+	    algo != GNUTLS_CIPHER_AES_256_GCM) {
cd0318
+		return;
cd0318
+	}
cd0318
+
cd0318
+	key.data = key16;
cd0318
+	key.size = gnutls_cipher_get_key_size(algo);
cd0318
+	assert(key.size <= sizeof(key16));
cd0318
+
cd0318
+	iv.data = iv16;
cd0318
+	iv.size = gnutls_cipher_get_iv_size(algo);
cd0318
+	assert(iv.size <= sizeof(iv16));
cd0318
+
cd0318
+	memset(iv.data, 0xff, iv.size);
cd0318
+	memset(key.data, 0xfe, key.size);
cd0318
+	memset(data, 0xfa, sizeof(data));
cd0318
+
cd0318
+	gnutls_global_set_log_function(tls_log_func);
cd0318
+	if (debug)
cd0318
+		gnutls_global_set_log_level(4711);
cd0318
+
cd0318
+	ret = global_init();
cd0318
+	if (ret < 0) {
cd0318
+		fail("Cannot initialize library\n"); /*errcode 1 */
cd0318
+	}
cd0318
+
cd0318
+	ret = gnutls_cipher_init(&ch, algo, &key, &iv;;
cd0318
+	if (ret < 0)
cd0318
+		fail("gnutls_cipher_init failed\n"); /*errcode 1 */
cd0318
+
cd0318
+	/* Test exceeding AES-GCM plaintext limit */
cd0318
+	ret = gnutls_cipher_encrypt(ch, data, sizeof(data));
cd0318
+	if (ret < 0)
cd0318
+		fail("could not encrypt data\n");
cd0318
+
cd0318
+	/* A few blocks larger than AES_GCM_ENCRYPT_PLAINTEXT_MAX combined with
cd0318
+	 * the previous call.  Use NULL for PLAINTEXT so the access to the first
cd0318
+	 * block always results in page fault (in case the limit is not
cd0318
+	 * enforced).
cd0318
+	 */
cd0318
+	ret = gnutls_cipher_encrypt(ch, NULL, AES_GCM_ENCRYPT_PLAINTEXT_MAX);
cd0318
+	if (ret >= 0)
cd0318
+		fail("succeeded in encrypting too long data\n");
cd0318
+	if (ret != GNUTLS_E_INVALID_REQUEST)
cd0318
+		fail("wrong kind of error on encrypting too long data,"
cd0318
+		     "%s instead of GNUTLS_E_INVALID_REQUEST\n",
cd0318
+		     gnutls_strerror_name(ret));
cd0318
+
cd0318
+	gnutls_cipher_deinit(ch);
cd0318
+
cd0318
+	gnutls_global_deinit();
cd0318
+	return;
cd0318
+}
cd0318
+#endif
cd0318
+
cd0318
 static void check_status(int status)
cd0318
 {
cd0318
 	if (WEXITSTATUS(status) != 0 ||
cd0318
@@ -464,6 +538,11 @@ void start(const char *name, int algo, unsigned aead)
cd0318
 
cd0318
 		success("trying %s: test_aead_invalid_short_decrypt\n", name);
cd0318
 		fork_subtest(test_aead_invalid_short_decrypt, algo);
cd0318
+
cd0318
+#if TEST_AES_GCM_ENCRYPT_PLAINTEXT_SIZE
cd0318
+		success("trying %s: test_aead_invalid_too_long_encrypt\n", name);
cd0318
+		fork_subtest(test_aead_invalid_too_long_encrypt, algo);
cd0318
+#endif
cd0318
 	}
cd0318
 }
cd0318
 
cd0318
-- 
cd0318
2.37.1
cd0318