[PATCH v2 08/17] crypto/cipher: Add setaad/gettag for AEAD modes

Jamin Lin posted 17 patches 1 month, 4 weeks ago
Maintainers: "Daniel P. Berrangé" <berrange@redhat.com>, "Cédric Le Goater" <clg@kaod.org>, Peter Maydell <peter.maydell@linaro.org>, Steven Lee <steven_lee@aspeedtech.com>, Troy Lee <leetroy@gmail.com>, Jamin Lin <jamin_lin@aspeedtech.com>, Kane Chen <kane_chen@aspeedtech.com>, Andrew Jeffery <andrew@codeconstruct.com.au>, Joel Stanley <joel@jms.id.au>, Eric Blake <eblake@redhat.com>, Markus Armbruster <armbru@redhat.com>, Fabiano Rosas <farosas@suse.de>, Laurent Vivier <lvivier@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>
There is a newer version of this series
[PATCH v2 08/17] crypto/cipher: Add setaad/gettag for AEAD modes
Posted by Jamin Lin 1 month, 4 weeks ago
AEAD modes such as GCM authenticate optional associated data (AAD) and
produce an authentication tag, which the block-cipher encrypt/decrypt
interface cannot express. Add qcrypto_cipher_setaad() and
qcrypto_cipher_gettag() plus the matching backend driver hooks. The
generic front-end reports an error when the selected mode's driver does
not implement them, so calling them on a non-AEAD mode fails cleanly.

Signed-off-by: Jamin Lin <jamin_lin@aspeedtech.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
---
 crypto/cipherpriv.h     |  8 ++++++++
 include/crypto/cipher.h | 36 ++++++++++++++++++++++++++++++++++++
 crypto/cipher.c         | 31 +++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+)

diff --git a/crypto/cipherpriv.h b/crypto/cipherpriv.h
index 64737ce961..c4f995f369 100644
--- a/crypto/cipherpriv.h
+++ b/crypto/cipherpriv.h
@@ -34,6 +34,14 @@ struct QCryptoCipherDriver {
                         const uint8_t *iv, size_t niv,
                         Error **errp);
 
+    int (*cipher_setaad)(QCryptoCipher *cipher,
+                         const uint8_t *aad, size_t len,
+                         Error **errp);
+
+    int (*cipher_gettag)(QCryptoCipher *cipher,
+                         uint8_t *tag, size_t len,
+                         Error **errp);
+
     void (*cipher_free)(QCryptoCipher *cipher);
 };
 
diff --git a/include/crypto/cipher.h b/include/crypto/cipher.h
index 92939310ef..2e361411b9 100644
--- a/include/crypto/cipher.h
+++ b/include/crypto/cipher.h
@@ -235,4 +235,40 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher,
                          const uint8_t *iv, size_t niv,
                          Error **errp);
 
+/**
+ * qcrypto_cipher_setaad:
+ * @cipher: the cipher object
+ * @aad: the associated data to authenticate
+ * @len: the length of @aad
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * For AEAD modes such as GCM, feed the associated data (AAD) that is
+ * authenticated but not encrypted. It must be called after
+ * qcrypto_cipher_setiv() and before the first encrypt/decrypt call. It is
+ * an error to call this on a mode that is not an AEAD mode.
+ *
+ * Returns: 0 on success, -1 on error
+ */
+int qcrypto_cipher_setaad(QCryptoCipher *cipher,
+                          const uint8_t *aad, size_t len,
+                          Error **errp);
+
+/**
+ * qcrypto_cipher_gettag:
+ * @cipher: the cipher object
+ * @tag: buffer to receive the authentication tag
+ * @len: the length of @tag
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * For AEAD modes such as GCM, read back the authentication tag computed
+ * over the associated data and the message. It must be called after the
+ * encrypt/decrypt operation. It is an error to call this on a mode that is
+ * not an AEAD mode.
+ *
+ * Returns: 0 on success, -1 on error
+ */
+int qcrypto_cipher_gettag(QCryptoCipher *cipher,
+                          uint8_t *tag, size_t len,
+                          Error **errp);
+
 #endif /* QCRYPTO_CIPHER_H */
diff --git a/crypto/cipher.c b/crypto/cipher.c
index 52d071a90c..67014aad6f 100644
--- a/crypto/cipher.c
+++ b/crypto/cipher.c
@@ -205,6 +205,37 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher,
 }
 
 
+int qcrypto_cipher_setaad(QCryptoCipher *cipher,
+                          const uint8_t *aad, size_t len,
+                          Error **errp)
+{
+    const QCryptoCipherDriver *drv = cipher->driver;
+
+    if (!drv->cipher_setaad) {
+        error_setg(errp, "The cipher mode does not support associated data");
+        return -1;
+    }
+
+    return drv->cipher_setaad(cipher, aad, len, errp);
+}
+
+
+int qcrypto_cipher_gettag(QCryptoCipher *cipher,
+                          uint8_t *tag, size_t len,
+                          Error **errp)
+{
+    const QCryptoCipherDriver *drv = cipher->driver;
+
+    if (!drv->cipher_gettag) {
+        error_setg(errp,
+                   "The cipher mode does not produce an authentication tag");
+        return -1;
+    }
+
+    return drv->cipher_gettag(cipher, tag, len, errp);
+}
+
+
 void qcrypto_cipher_free(QCryptoCipher *cipher)
 {
     if (cipher) {
-- 
2.43.0
RE: [PATCH v2 08/17] crypto/cipher: Add setaad/gettag for AEAD modes
Posted by Kane Chen 1 month ago
> -----Original Message-----
> From: Jamin Lin <jamin_lin@aspeedtech.com>
> Sent: Wednesday, July 15, 2026 11:33 AM
> To: Daniel P. Berrangé <berrange@redhat.com>; Cédric Le Goater
> <clg@kaod.org>; Peter Maydell <peter.maydell@linaro.org>; Steven Lee
> <steven_lee@aspeedtech.com>; Troy Lee <leetroy@gmail.com>; Kane Chen
> <kane_chen@aspeedtech.com>; Andrew Jeffery
> <andrew@codeconstruct.com.au>; Joel Stanley <joel@jms.id.au>; Eric Blake
> <eblake@redhat.com>; Markus Armbruster <armbru@redhat.com>; Fabiano
> Rosas <farosas@suse.de>; Laurent Vivier <lvivier@redhat.com>; Paolo Bonzini
> <pbonzini@redhat.com>; open list:All patches CC here
> <qemu-devel@nongnu.org>; open list:ASPEED BMCs
> <qemu-arm@nongnu.org>
> Cc: Jamin Lin <jamin_lin@aspeedtech.com>; Troy Lee
> <troy_lee@aspeedtech.com>
> Subject: [PATCH v2 08/17] crypto/cipher: Add setaad/gettag for AEAD modes
> 
> AEAD modes such as GCM authenticate optional associated data (AAD) and
> produce an authentication tag, which the block-cipher encrypt/decrypt
> interface cannot express. Add qcrypto_cipher_setaad() and
> qcrypto_cipher_gettag() plus the matching backend driver hooks. The generic
> front-end reports an error when the selected mode's driver does not
> implement them, so calling them on a non-AEAD mode fails cleanly.
> 
> Signed-off-by: Jamin Lin <jamin_lin@aspeedtech.com>
> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  crypto/cipherpriv.h     |  8 ++++++++
>  include/crypto/cipher.h | 36 ++++++++++++++++++++++++++++++++++++
>  crypto/cipher.c         | 31 +++++++++++++++++++++++++++++++
>  3 files changed, 75 insertions(+)
> 
> diff --git a/crypto/cipherpriv.h b/crypto/cipherpriv.h index
> 64737ce961..c4f995f369 100644
> --- a/crypto/cipherpriv.h
> +++ b/crypto/cipherpriv.h
> @@ -34,6 +34,14 @@ struct QCryptoCipherDriver {
>                          const uint8_t *iv, size_t niv,
>                          Error **errp);
> 
> +    int (*cipher_setaad)(QCryptoCipher *cipher,
> +                         const uint8_t *aad, size_t len,
> +                         Error **errp);
> +
> +    int (*cipher_gettag)(QCryptoCipher *cipher,
> +                         uint8_t *tag, size_t len,
> +                         Error **errp);
> +
>      void (*cipher_free)(QCryptoCipher *cipher);  };
> 
> diff --git a/include/crypto/cipher.h b/include/crypto/cipher.h index
> 92939310ef..2e361411b9 100644
> --- a/include/crypto/cipher.h
> +++ b/include/crypto/cipher.h
> @@ -235,4 +235,40 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher,
>                           const uint8_t *iv, size_t niv,
>                           Error **errp);
> 
> +/**
> + * qcrypto_cipher_setaad:
> + * @cipher: the cipher object
> + * @aad: the associated data to authenticate
> + * @len: the length of @aad
> + * @errp: pointer to a NULL-initialized error object
> + *
> + * For AEAD modes such as GCM, feed the associated data (AAD) that is
> + * authenticated but not encrypted. It must be called after
> + * qcrypto_cipher_setiv() and before the first encrypt/decrypt call. It
> +is
> + * an error to call this on a mode that is not an AEAD mode.
> + *
> + * Returns: 0 on success, -1 on error
> + */
> +int qcrypto_cipher_setaad(QCryptoCipher *cipher,
> +                          const uint8_t *aad, size_t len,
> +                          Error **errp);
> +
> +/**
> + * qcrypto_cipher_gettag:
> + * @cipher: the cipher object
> + * @tag: buffer to receive the authentication tag
> + * @len: the length of @tag
> + * @errp: pointer to a NULL-initialized error object
> + *
> + * For AEAD modes such as GCM, read back the authentication tag
> +computed
> + * over the associated data and the message. It must be called after
> +the
> + * encrypt/decrypt operation. It is an error to call this on a mode
> +that is
> + * not an AEAD mode.
> + *
> + * Returns: 0 on success, -1 on error
> + */
> +int qcrypto_cipher_gettag(QCryptoCipher *cipher,
> +                          uint8_t *tag, size_t len,
> +                          Error **errp);
> +
>  #endif /* QCRYPTO_CIPHER_H */
> diff --git a/crypto/cipher.c b/crypto/cipher.c index 52d071a90c..67014aad6f
> 100644
> --- a/crypto/cipher.c
> +++ b/crypto/cipher.c
> @@ -205,6 +205,37 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher,  }
> 
> 
> +int qcrypto_cipher_setaad(QCryptoCipher *cipher,
> +                          const uint8_t *aad, size_t len,
> +                          Error **errp) {
> +    const QCryptoCipherDriver *drv = cipher->driver;
> +
> +    if (!drv->cipher_setaad) {
> +        error_setg(errp, "The cipher mode does not support associated
> data");
> +        return -1;
> +    }
> +
> +    return drv->cipher_setaad(cipher, aad, len, errp); }
> +
> +
> +int qcrypto_cipher_gettag(QCryptoCipher *cipher,
> +                          uint8_t *tag, size_t len,
> +                          Error **errp) {
> +    const QCryptoCipherDriver *drv = cipher->driver;
> +
> +    if (!drv->cipher_gettag) {
> +        error_setg(errp,
> +                   "The cipher mode does not produce an authentication
> tag");
> +        return -1;
> +    }
> +
> +    return drv->cipher_gettag(cipher, tag, len, errp); }
> +
> +
>  void qcrypto_cipher_free(QCryptoCipher *cipher)  {
>      if (cipher) {
> --
> 2.43.0

Reviewed-by: Kane Chen <kane_chen@aspeedtech.com>