[PATCH 6/6] tests/basic: add MAC verify API test for bad tag length

Jussi Kivilinna jussi.kivilinna at iki.fi
Tue Sep 8 13:46:42 CEST 2026


* cipher/cipher-cmac.c (cmac_tag): Return GPG_ERR_INV_LENGTH for invalid
tag length.
* cipher/cipher-gcm.c (_gcry_cipher_gcm_tag): Likewise.
* tests/basic.c (check_one_mac): Check that gcry_mac_verify rejects
oversized tag length.
--

CMAC returned GPG_ERR_INV_ARG and GCM returned GPG_ERR_CHECKSUM for wrong
tag length, while HMAC and Poly1305 return GPG_ERR_INV_LENGTH.

Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
 cipher/cipher-cmac.c | 4 +++-
 cipher/cipher-gcm.c  | 5 +++--
 tests/basic.c        | 9 +++++++++
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/cipher/cipher-cmac.c b/cipher/cipher-cmac.c
index c8bedc08..aab4b042 100644
--- a/cipher/cipher-cmac.c
+++ b/cipher/cipher-cmac.c
@@ -216,8 +216,10 @@ cmac_tag (gcry_cipher_hd_t c, gcry_cmac_context_t *ctx,
 {
   gcry_err_code_t ret;
 
-  if (!tag || taglen == 0 || taglen > c->spec->blocksize)
+  if (!tag)
     return GPG_ERR_INV_ARG;
+  if (taglen == 0 || taglen > c->spec->blocksize)
+    return GPG_ERR_INV_LENGTH;
 
   if (!ctx->tag)
     {
diff --git a/cipher/cipher-gcm.c b/cipher/cipher-gcm.c
index e6a4df0f..ed04951b 100644
--- a/cipher/cipher-gcm.c
+++ b/cipher/cipher-gcm.c
@@ -1283,8 +1283,9 @@ _gcry_cipher_gcm_tag (gcry_cipher_hd_t c,
     {
       /* OUTBUFLEN gives the length of the user supplied tag in OUTBUF
        * and thus we need to compare its length first.  */
-      if (!is_tag_length_valid (outbuflen)
-          || !buf_eq_const (outbuf, c->u_mode.gcm.u_tag.tag, outbuflen))
+      if (!is_tag_length_valid (outbuflen))
+	return GPG_ERR_INV_LENGTH;
+      if (!buf_eq_const (outbuf, c->u_mode.gcm.u_tag.tag, outbuflen))
         return GPG_ERR_CHECKSUM;
     }
 
diff --git a/tests/basic.c b/tests/basic.c
index 405f9861..7b8c73f3 100644
--- a/tests/basic.c
+++ b/tests/basic.c
@@ -16405,6 +16405,7 @@ check_one_mac (int algo, const char *data, int datalen,
 	       const char *key, int keylen, const char *iv, int ivlen,
 	       const char *expect, int test_buffering)
 {
+  unsigned char big_bad_tag[64 * 2];
   gcry_mac_hd_t hd;
   unsigned char *p;
   unsigned int maclen;
@@ -16558,6 +16559,14 @@ check_one_mac (int algo, const char *data, int datalen,
   if (err)
     fail("algo %d, mac gcry_mac_verify failed: %s\n", algo, gpg_strerror (err));
 
+  clutter_vector_registers();
+  err = gcry_mac_verify (hd, big_bad_tag, sizeof(big_bad_tag));
+  if (gcry_err_code (err) != GPG_ERR_INV_LENGTH)
+    fail ("algo %d, gcry_mac_verify: oversized len %d not rejected: %s\n", algo,
+	  (int)sizeof(big_bad_tag), gpg_strerror (err));
+  if (err)
+    goto out;
+
   macoutlen = maclen;
   clutter_vector_registers();
   err = gcry_mac_read (hd, p, &macoutlen);
-- 
2.53.0



More information about the Gcrypt-devel mailing list