hc
2024-01-31 f9004dbfff8a3fbbd7e2a88c8a4327c7f2f8e5b2
kernel/include/crypto/aead.h
....@@ -1,13 +1,8 @@
1
+/* SPDX-License-Identifier: GPL-2.0-or-later */
12 /*
23 * AEAD: Authenticated Encryption with Associated Data
34 *
45 * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
5
- *
6
- * This program is free software; you can redistribute it and/or modify it
7
- * under the terms of the GNU General Public License as published by the Free
8
- * Software Foundation; either version 2 of the License, or (at your option)
9
- * any later version.
10
- *
116 */
127
138 #ifndef _CRYPTO_AEAD_H
....@@ -48,27 +43,33 @@
4843 *
4944 * Memory Structure:
5045 *
51
- * To support the needs of the most prominent user of AEAD ciphers, namely
52
- * IPSEC, the AEAD ciphers have a special memory layout the caller must adhere
53
- * to.
46
+ * The source scatterlist must contain the concatenation of
47
+ * associated data || plaintext or ciphertext.
5448 *
55
- * The scatter list pointing to the input data must contain:
49
+ * The destination scatterlist has the same layout, except that the plaintext
50
+ * (resp. ciphertext) will grow (resp. shrink) by the authentication tag size
51
+ * during encryption (resp. decryption).
5652 *
57
- * * for RFC4106 ciphers, the concatenation of
58
- * associated authentication data || IV || plaintext or ciphertext. Note, the
59
- * same IV (buffer) is also set with the aead_request_set_crypt call. Note,
60
- * the API call of aead_request_set_ad must provide the length of the AAD and
61
- * the IV. The API call of aead_request_set_crypt only points to the size of
62
- * the input plaintext or ciphertext.
53
+ * In-place encryption/decryption is enabled by using the same scatterlist
54
+ * pointer for both the source and destination.
6355 *
64
- * * for "normal" AEAD ciphers, the concatenation of
65
- * associated authentication data || plaintext or ciphertext.
56
+ * Even in the out-of-place case, space must be reserved in the destination for
57
+ * the associated data, even though it won't be written to. This makes the
58
+ * in-place and out-of-place cases more consistent. It is permissible for the
59
+ * "destination" associated data to alias the "source" associated data.
6660 *
67
- * It is important to note that if multiple scatter gather list entries form
68
- * the input data mentioned above, the first entry must not point to a NULL
69
- * buffer. If there is any potential where the AAD buffer can be NULL, the
70
- * calling code must contain a precaution to ensure that this does not result
71
- * in the first scatter gather list entry pointing to a NULL buffer.
61
+ * As with the other scatterlist crypto APIs, zero-length scatterlist elements
62
+ * are not allowed in the used part of the scatterlist. Thus, if there is no
63
+ * associated data, the first element must point to the plaintext/ciphertext.
64
+ *
65
+ * To meet the needs of IPsec, a special quirk applies to rfc4106, rfc4309,
66
+ * rfc4543, and rfc7539esp ciphers. For these ciphers, the final 'ivsize' bytes
67
+ * of the associated data buffer must contain a second copy of the IV. This is
68
+ * in addition to the copy passed to aead_request_set_crypt(). These two IV
69
+ * copies must not differ; different implementations of the same algorithm may
70
+ * behave differently in that case. Note that the algorithm might not actually
71
+ * treat the IV as associated data; nevertheless the length passed to
72
+ * aead_request_set_ad() must include it.
7273 */
7374
7475 struct crypto_aead;
....@@ -115,7 +116,6 @@
115116 * @setkey: see struct skcipher_alg
116117 * @encrypt: see struct skcipher_alg
117118 * @decrypt: see struct skcipher_alg
118
- * @geniv: see struct skcipher_alg
119119 * @ivsize: see struct skcipher_alg
120120 * @chunksize: see struct skcipher_alg
121121 * @init: Initialize the cryptographic transformation object. This function
....@@ -141,8 +141,6 @@
141141 int (*decrypt)(struct aead_request *req);
142142 int (*init)(struct crypto_aead *tfm);
143143 void (*exit)(struct crypto_aead *tfm);
144
-
145
- const char *geniv;
146144
147145 unsigned int ivsize;
148146 unsigned int maxauthsize;
....@@ -237,6 +235,16 @@
237235 return tfm->authsize;
238236 }
239237
238
+static inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg)
239
+{
240
+ return alg->maxauthsize;
241
+}
242
+
243
+static inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead)
244
+{
245
+ return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead));
246
+}
247
+
240248 /**
241249 * crypto_aead_blocksize() - obtain block size of cipher
242250 * @tfm: cipher handle
....@@ -327,19 +335,11 @@
327335 *
328336 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
329337 */
330
-static inline int crypto_aead_encrypt(struct aead_request *req)
331
-{
332
- struct crypto_aead *aead = crypto_aead_reqtfm(req);
333
-
334
- if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
335
- return -ENOKEY;
336
-
337
- return crypto_aead_alg(aead)->encrypt(req);
338
-}
338
+int crypto_aead_encrypt(struct aead_request *req);
339339
340340 /**
341341 * crypto_aead_decrypt() - decrypt ciphertext
342
- * @req: reference to the ablkcipher_request handle that holds all information
342
+ * @req: reference to the aead_request handle that holds all information
343343 * needed to perform the cipher operation
344344 *
345345 * Decrypt ciphertext data using the aead_request handle. That data structure
....@@ -359,18 +359,7 @@
359359 * integrity of the ciphertext or the associated data was violated);
360360 * < 0 if an error occurred.
361361 */
362
-static inline int crypto_aead_decrypt(struct aead_request *req)
363
-{
364
- struct crypto_aead *aead = crypto_aead_reqtfm(req);
365
-
366
- if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
367
- return -ENOKEY;
368
-
369
- if (req->cryptlen < crypto_aead_authsize(aead))
370
- return -EINVAL;
371
-
372
- return crypto_aead_alg(aead)->decrypt(req);
373
-}
362
+int crypto_aead_decrypt(struct aead_request *req);
374363
375364 /**
376365 * DOC: Asynchronous AEAD Request Handle
....@@ -438,7 +427,7 @@
438427 */
439428 static inline void aead_request_free(struct aead_request *req)
440429 {
441
- kzfree(req);
430
+ kfree_sensitive(req);
442431 }
443432
444433 /**