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> Reviewed-by: Kane Chen <kane_chen@aspeedtech.com> Link: https://lore.kernel.org/qemu-devel/20260811060115.1849266-9-jamin_lin@aspeedtech.com Signed-off-by: Cédric Le Goater <clg@redhat.com>
Jamin Lin committed
Aug 11, 2026 at 06:01 UTC
a4383c086228f8626f2318b7f4b984002ce820df
3 files changed
+75
crypto/cipher.c
+31
@@ -205,6 +205,37 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher,
205
}
206
207
208
+int qcrypto_cipher_setaad(QCryptoCipher *cipher,
209
+ const uint8_t *aad, size_t len,
210
+ Error **errp)
211
+{
212
+ const QCryptoCipherDriver *drv = cipher->driver;
213
+
214
+ if (!drv->cipher_setaad) {
215
+ error_setg(errp, "The cipher mode does not support associated data");
216
+ return -1;
217
+ }
218
+
219
+ return drv->cipher_setaad(cipher, aad, len, errp);
220
+}
221
+
222
+
223
+int qcrypto_cipher_gettag(QCryptoCipher *cipher,
224
+ uint8_t *tag, size_t len,
225
+ Error **errp)
226
+{
227
+ const QCryptoCipherDriver *drv = cipher->driver;
228
+
229
+ if (!drv->cipher_gettag) {
230
+ error_setg(errp,
231
+ "The cipher mode does not produce an authentication tag");
232
+ return -1;
233
+ }
234
+
235
+ return drv->cipher_gettag(cipher, tag, len, errp);
236
+}
237
+
238
+
239
void qcrypto_cipher_free(QCryptoCipher *cipher)
240
{
241
if (cipher) {
crypto/cipherpriv.h
+8
@@ -34,6 +34,14 @@ struct QCryptoCipherDriver {
34
const uint8_t *iv, size_t niv,
35
Error **errp);
36
37
+ int (*cipher_setaad)(QCryptoCipher *cipher,
38
+ const uint8_t *aad, size_t len,
39
+ Error **errp);
40
+
41
+ int (*cipher_gettag)(QCryptoCipher *cipher,
42
+ uint8_t *tag, size_t len,
43
+ Error **errp);
44
+
45
void (*cipher_free)(QCryptoCipher *cipher);
46
};
47
include/crypto/cipher.h
+36
@@ -235,4 +235,40 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher,
235
const uint8_t *iv, size_t niv,
236
Error **errp);
237
238
+/**
239
+ * qcrypto_cipher_setaad:
240
+ * @cipher: the cipher object
241
+ * @aad: the associated data to authenticate
242
+ * @len: the length of @aad
243
+ * @errp: pointer to a NULL-initialized error object
244
+ *
245
+ * For AEAD modes such as GCM, feed the associated data (AAD) that is
246
+ * authenticated but not encrypted. It must be called after
247
+ * qcrypto_cipher_setiv() and before the first encrypt/decrypt call. It is
248
+ * an error to call this on a mode that is not an AEAD mode.
249
+ *
250
+ * Returns: 0 on success, -1 on error
251
+ */
252
+int qcrypto_cipher_setaad(QCryptoCipher *cipher,
253
+ const uint8_t *aad, size_t len,
254
+ Error **errp);
255
+
256
+/**
257
+ * qcrypto_cipher_gettag:
258
+ * @cipher: the cipher object
259
+ * @tag: buffer to receive the authentication tag
260
+ * @len: the length of @tag
261
+ * @errp: pointer to a NULL-initialized error object
262
+ *
263
+ * For AEAD modes such as GCM, read back the authentication tag computed
264
+ * over the associated data and the message. It must be called after the
265
+ * encrypt/decrypt operation. It is an error to call this on a mode that is
266
+ * not an AEAD mode.
267
+ *
268
+ * Returns: 0 on success, -1 on error
269
+ */
270
+int qcrypto_cipher_gettag(QCryptoCipher *cipher,
271
+ uint8_t *tag, size_t len,
272
+ Error **errp);
273
+
274
#endif /* QCRYPTO_CIPHER_H */