git: 433fe061fc59 - releng/14.0 - ossl: Fix some bugs in the fallback AES-GCM implementation

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 05 Dec 2023 18:27:34 UTC
The branch releng/14.0 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=433fe061fc5954fe00b33ed9d850a655920f0a2d

commit 433fe061fc5954fe00b33ed9d850a655920f0a2d
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2023-11-29 20:08:12 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-12-04 14:02:28 +0000

    ossl: Fix some bugs in the fallback AES-GCM implementation
    
    gcm_*_aesni() are used when the AVX512 implementation is not available.
    Fix two bugs which manifest when handling operations spanning multiple
    segments:
    - Avoid underflow when the length of the input is smaller than the
      residual.
    - In gcm_decrypt_aesni(), ensure that we begin the operation at the
      right offset into the input and output buffers.
    
    Reviewed by:    jhb
    Fixes:          9b1d87286c78 ("ossl: Add a fallback AES-GCM implementation using AES-NI")
    MFC after:      3 days
    Differential Revision:  https://reviews.freebsd.org/D42838
    Approved by:    so
    Security:       FreeBSD-EN-23:17.ossl
    
    (cherry picked from commit 47d767dab54895f3ba8abac6ab2295797394659e)
    (cherry picked from commit 118b866d9c39da1f5a17680e2034b3eff7337988)
---
 sys/crypto/openssl/amd64/ossl_aes_gcm.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/sys/crypto/openssl/amd64/ossl_aes_gcm.c b/sys/crypto/openssl/amd64/ossl_aes_gcm.c
index 0d205ec3ff90..d08b2ac8a759 100644
--- a/sys/crypto/openssl/amd64/ossl_aes_gcm.c
+++ b/sys/crypto/openssl/amd64/ossl_aes_gcm.c
@@ -459,7 +459,7 @@ gcm_encrypt_aesni(struct ossl_gcm_context *ctx, const unsigned char *in,
 	size_t bulk = 0, res;
 	int error;
 
-	res = (AES_BLOCK_LEN - ctx->gcm.mres) % AES_BLOCK_LEN;
+	res = MIN(len, (AES_BLOCK_LEN - ctx->gcm.mres) % AES_BLOCK_LEN);
 	if ((error = gcm_encrypt(ctx, in, out, res)) != 0)
 		return error;
 
@@ -621,12 +621,12 @@ gcm_decrypt_aesni(struct ossl_gcm_context *ctx, const unsigned char *in,
 	size_t bulk = 0, res;
 	int error;
 
-	res = (AES_BLOCK_LEN - ctx->gcm.mres) % AES_BLOCK_LEN;
+	res = MIN(len, (AES_BLOCK_LEN - ctx->gcm.mres) % AES_BLOCK_LEN);
 	if ((error = gcm_decrypt(ctx, in, out, res)) != 0)
 		return error;
 
-	bulk = aesni_gcm_decrypt(in, out, len, &ctx->aes_ks, ctx->gcm.Yi.c,
-	    ctx->gcm.Xi.u);
+	bulk = aesni_gcm_decrypt(in + res, out + res, len - res, &ctx->aes_ks,
+	    ctx->gcm.Yi.c, ctx->gcm.Xi.u);
 	ctx->gcm.len.u[1] += bulk;
 	bulk += res;