1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * AEAD: Authenticated Encryption with Associated Data
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #ifndef _CRYPTO_AEAD_H
9*4882a593Smuzhiyun #define _CRYPTO_AEAD_H
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/crypto.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun /**
16*4882a593Smuzhiyun * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD
19*4882a593Smuzhiyun * (listed as type "aead" in /proc/crypto)
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * The most prominent examples for this type of encryption is GCM and CCM.
22*4882a593Smuzhiyun * However, the kernel supports other types of AEAD ciphers which are defined
23*4882a593Smuzhiyun * with the following cipher string:
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * authenc(keyed message digest, block cipher)
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * For example: authenc(hmac(sha256), cbc(aes))
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * The example code provided for the symmetric key cipher operation
30*4882a593Smuzhiyun * applies here as well. Naturally all *skcipher* symbols must be exchanged
31*4882a593Smuzhiyun * the *aead* pendants discussed in the following. In addition, for the AEAD
32*4882a593Smuzhiyun * operation, the aead_request_set_ad function must be used to set the
33*4882a593Smuzhiyun * pointer to the associated data memory location before performing the
34*4882a593Smuzhiyun * encryption or decryption operation. In case of an encryption, the associated
35*4882a593Smuzhiyun * data memory is filled during the encryption operation. For decryption, the
36*4882a593Smuzhiyun * associated data memory must contain data that is used to verify the integrity
37*4882a593Smuzhiyun * of the decrypted data. Another deviation from the asynchronous block cipher
38*4882a593Smuzhiyun * operation is that the caller should explicitly check for -EBADMSG of the
39*4882a593Smuzhiyun * crypto_aead_decrypt. That error indicates an authentication error, i.e.
40*4882a593Smuzhiyun * a breach in the integrity of the message. In essence, that -EBADMSG error
41*4882a593Smuzhiyun * code is the key bonus an AEAD cipher has over "standard" block chaining
42*4882a593Smuzhiyun * modes.
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * Memory Structure:
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * The source scatterlist must contain the concatenation of
47*4882a593Smuzhiyun * associated data || plaintext or ciphertext.
48*4882a593Smuzhiyun *
49*4882a593Smuzhiyun * The destination scatterlist has the same layout, except that the plaintext
50*4882a593Smuzhiyun * (resp. ciphertext) will grow (resp. shrink) by the authentication tag size
51*4882a593Smuzhiyun * during encryption (resp. decryption).
52*4882a593Smuzhiyun *
53*4882a593Smuzhiyun * In-place encryption/decryption is enabled by using the same scatterlist
54*4882a593Smuzhiyun * pointer for both the source and destination.
55*4882a593Smuzhiyun *
56*4882a593Smuzhiyun * Even in the out-of-place case, space must be reserved in the destination for
57*4882a593Smuzhiyun * the associated data, even though it won't be written to. This makes the
58*4882a593Smuzhiyun * in-place and out-of-place cases more consistent. It is permissible for the
59*4882a593Smuzhiyun * "destination" associated data to alias the "source" associated data.
60*4882a593Smuzhiyun *
61*4882a593Smuzhiyun * As with the other scatterlist crypto APIs, zero-length scatterlist elements
62*4882a593Smuzhiyun * are not allowed in the used part of the scatterlist. Thus, if there is no
63*4882a593Smuzhiyun * associated data, the first element must point to the plaintext/ciphertext.
64*4882a593Smuzhiyun *
65*4882a593Smuzhiyun * To meet the needs of IPsec, a special quirk applies to rfc4106, rfc4309,
66*4882a593Smuzhiyun * rfc4543, and rfc7539esp ciphers. For these ciphers, the final 'ivsize' bytes
67*4882a593Smuzhiyun * of the associated data buffer must contain a second copy of the IV. This is
68*4882a593Smuzhiyun * in addition to the copy passed to aead_request_set_crypt(). These two IV
69*4882a593Smuzhiyun * copies must not differ; different implementations of the same algorithm may
70*4882a593Smuzhiyun * behave differently in that case. Note that the algorithm might not actually
71*4882a593Smuzhiyun * treat the IV as associated data; nevertheless the length passed to
72*4882a593Smuzhiyun * aead_request_set_ad() must include it.
73*4882a593Smuzhiyun */
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun struct crypto_aead;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /**
78*4882a593Smuzhiyun * struct aead_request - AEAD request
79*4882a593Smuzhiyun * @base: Common attributes for async crypto requests
80*4882a593Smuzhiyun * @assoclen: Length in bytes of associated data for authentication
81*4882a593Smuzhiyun * @cryptlen: Length of data to be encrypted or decrypted
82*4882a593Smuzhiyun * @iv: Initialisation vector
83*4882a593Smuzhiyun * @src: Source data
84*4882a593Smuzhiyun * @dst: Destination data
85*4882a593Smuzhiyun * @__ctx: Start of private context data
86*4882a593Smuzhiyun */
87*4882a593Smuzhiyun struct aead_request {
88*4882a593Smuzhiyun struct crypto_async_request base;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun unsigned int assoclen;
91*4882a593Smuzhiyun unsigned int cryptlen;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun u8 *iv;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun struct scatterlist *src;
96*4882a593Smuzhiyun struct scatterlist *dst;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun void *__ctx[] CRYPTO_MINALIGN_ATTR;
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /**
102*4882a593Smuzhiyun * struct aead_alg - AEAD cipher definition
103*4882a593Smuzhiyun * @maxauthsize: Set the maximum authentication tag size supported by the
104*4882a593Smuzhiyun * transformation. A transformation may support smaller tag sizes.
105*4882a593Smuzhiyun * As the authentication tag is a message digest to ensure the
106*4882a593Smuzhiyun * integrity of the encrypted data, a consumer typically wants the
107*4882a593Smuzhiyun * largest authentication tag possible as defined by this
108*4882a593Smuzhiyun * variable.
109*4882a593Smuzhiyun * @setauthsize: Set authentication size for the AEAD transformation. This
110*4882a593Smuzhiyun * function is used to specify the consumer requested size of the
111*4882a593Smuzhiyun * authentication tag to be either generated by the transformation
112*4882a593Smuzhiyun * during encryption or the size of the authentication tag to be
113*4882a593Smuzhiyun * supplied during the decryption operation. This function is also
114*4882a593Smuzhiyun * responsible for checking the authentication tag size for
115*4882a593Smuzhiyun * validity.
116*4882a593Smuzhiyun * @setkey: see struct skcipher_alg
117*4882a593Smuzhiyun * @encrypt: see struct skcipher_alg
118*4882a593Smuzhiyun * @decrypt: see struct skcipher_alg
119*4882a593Smuzhiyun * @ivsize: see struct skcipher_alg
120*4882a593Smuzhiyun * @chunksize: see struct skcipher_alg
121*4882a593Smuzhiyun * @init: Initialize the cryptographic transformation object. This function
122*4882a593Smuzhiyun * is used to initialize the cryptographic transformation object.
123*4882a593Smuzhiyun * This function is called only once at the instantiation time, right
124*4882a593Smuzhiyun * after the transformation context was allocated. In case the
125*4882a593Smuzhiyun * cryptographic hardware has some special requirements which need to
126*4882a593Smuzhiyun * be handled by software, this function shall check for the precise
127*4882a593Smuzhiyun * requirement of the transformation and put any software fallbacks
128*4882a593Smuzhiyun * in place.
129*4882a593Smuzhiyun * @exit: Deinitialize the cryptographic transformation object. This is a
130*4882a593Smuzhiyun * counterpart to @init, used to remove various changes set in
131*4882a593Smuzhiyun * @init.
132*4882a593Smuzhiyun * @base: Definition of a generic crypto cipher algorithm.
133*4882a593Smuzhiyun *
134*4882a593Smuzhiyun * All fields except @ivsize is mandatory and must be filled.
135*4882a593Smuzhiyun */
136*4882a593Smuzhiyun struct aead_alg {
137*4882a593Smuzhiyun int (*setkey)(struct crypto_aead *tfm, const u8 *key,
138*4882a593Smuzhiyun unsigned int keylen);
139*4882a593Smuzhiyun int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
140*4882a593Smuzhiyun int (*encrypt)(struct aead_request *req);
141*4882a593Smuzhiyun int (*decrypt)(struct aead_request *req);
142*4882a593Smuzhiyun int (*init)(struct crypto_aead *tfm);
143*4882a593Smuzhiyun void (*exit)(struct crypto_aead *tfm);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun unsigned int ivsize;
146*4882a593Smuzhiyun unsigned int maxauthsize;
147*4882a593Smuzhiyun unsigned int chunksize;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun struct crypto_alg base;
150*4882a593Smuzhiyun };
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun struct crypto_aead {
153*4882a593Smuzhiyun unsigned int authsize;
154*4882a593Smuzhiyun unsigned int reqsize;
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun struct crypto_tfm base;
157*4882a593Smuzhiyun };
158*4882a593Smuzhiyun
__crypto_aead_cast(struct crypto_tfm * tfm)159*4882a593Smuzhiyun static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun return container_of(tfm, struct crypto_aead, base);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /**
165*4882a593Smuzhiyun * crypto_alloc_aead() - allocate AEAD cipher handle
166*4882a593Smuzhiyun * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
167*4882a593Smuzhiyun * AEAD cipher
168*4882a593Smuzhiyun * @type: specifies the type of the cipher
169*4882a593Smuzhiyun * @mask: specifies the mask for the cipher
170*4882a593Smuzhiyun *
171*4882a593Smuzhiyun * Allocate a cipher handle for an AEAD. The returned struct
172*4882a593Smuzhiyun * crypto_aead is the cipher handle that is required for any subsequent
173*4882a593Smuzhiyun * API invocation for that AEAD.
174*4882a593Smuzhiyun *
175*4882a593Smuzhiyun * Return: allocated cipher handle in case of success; IS_ERR() is true in case
176*4882a593Smuzhiyun * of an error, PTR_ERR() returns the error code.
177*4882a593Smuzhiyun */
178*4882a593Smuzhiyun struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
179*4882a593Smuzhiyun
crypto_aead_tfm(struct crypto_aead * tfm)180*4882a593Smuzhiyun static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun return &tfm->base;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /**
186*4882a593Smuzhiyun * crypto_free_aead() - zeroize and free aead handle
187*4882a593Smuzhiyun * @tfm: cipher handle to be freed
188*4882a593Smuzhiyun *
189*4882a593Smuzhiyun * If @tfm is a NULL or error pointer, this function does nothing.
190*4882a593Smuzhiyun */
crypto_free_aead(struct crypto_aead * tfm)191*4882a593Smuzhiyun static inline void crypto_free_aead(struct crypto_aead *tfm)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun crypto_destroy_tfm(tfm, crypto_aead_tfm(tfm));
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
crypto_aead_alg(struct crypto_aead * tfm)196*4882a593Smuzhiyun static inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun return container_of(crypto_aead_tfm(tfm)->__crt_alg,
199*4882a593Smuzhiyun struct aead_alg, base);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
crypto_aead_alg_ivsize(struct aead_alg * alg)202*4882a593Smuzhiyun static inline unsigned int crypto_aead_alg_ivsize(struct aead_alg *alg)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun return alg->ivsize;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /**
208*4882a593Smuzhiyun * crypto_aead_ivsize() - obtain IV size
209*4882a593Smuzhiyun * @tfm: cipher handle
210*4882a593Smuzhiyun *
211*4882a593Smuzhiyun * The size of the IV for the aead referenced by the cipher handle is
212*4882a593Smuzhiyun * returned. This IV size may be zero if the cipher does not need an IV.
213*4882a593Smuzhiyun *
214*4882a593Smuzhiyun * Return: IV size in bytes
215*4882a593Smuzhiyun */
crypto_aead_ivsize(struct crypto_aead * tfm)216*4882a593Smuzhiyun static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun return crypto_aead_alg_ivsize(crypto_aead_alg(tfm));
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /**
222*4882a593Smuzhiyun * crypto_aead_authsize() - obtain maximum authentication data size
223*4882a593Smuzhiyun * @tfm: cipher handle
224*4882a593Smuzhiyun *
225*4882a593Smuzhiyun * The maximum size of the authentication data for the AEAD cipher referenced
226*4882a593Smuzhiyun * by the AEAD cipher handle is returned. The authentication data size may be
227*4882a593Smuzhiyun * zero if the cipher implements a hard-coded maximum.
228*4882a593Smuzhiyun *
229*4882a593Smuzhiyun * The authentication data may also be known as "tag value".
230*4882a593Smuzhiyun *
231*4882a593Smuzhiyun * Return: authentication data size / tag size in bytes
232*4882a593Smuzhiyun */
crypto_aead_authsize(struct crypto_aead * tfm)233*4882a593Smuzhiyun static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun return tfm->authsize;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
crypto_aead_alg_maxauthsize(struct aead_alg * alg)238*4882a593Smuzhiyun static inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun return alg->maxauthsize;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
crypto_aead_maxauthsize(struct crypto_aead * aead)243*4882a593Smuzhiyun static inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead));
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /**
249*4882a593Smuzhiyun * crypto_aead_blocksize() - obtain block size of cipher
250*4882a593Smuzhiyun * @tfm: cipher handle
251*4882a593Smuzhiyun *
252*4882a593Smuzhiyun * The block size for the AEAD referenced with the cipher handle is returned.
253*4882a593Smuzhiyun * The caller may use that information to allocate appropriate memory for the
254*4882a593Smuzhiyun * data returned by the encryption or decryption operation
255*4882a593Smuzhiyun *
256*4882a593Smuzhiyun * Return: block size of cipher
257*4882a593Smuzhiyun */
crypto_aead_blocksize(struct crypto_aead * tfm)258*4882a593Smuzhiyun static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
crypto_aead_alignmask(struct crypto_aead * tfm)263*4882a593Smuzhiyun static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
crypto_aead_get_flags(struct crypto_aead * tfm)268*4882a593Smuzhiyun static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
crypto_aead_set_flags(struct crypto_aead * tfm,u32 flags)273*4882a593Smuzhiyun static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
crypto_aead_clear_flags(struct crypto_aead * tfm,u32 flags)278*4882a593Smuzhiyun static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun /**
284*4882a593Smuzhiyun * crypto_aead_setkey() - set key for cipher
285*4882a593Smuzhiyun * @tfm: cipher handle
286*4882a593Smuzhiyun * @key: buffer holding the key
287*4882a593Smuzhiyun * @keylen: length of the key in bytes
288*4882a593Smuzhiyun *
289*4882a593Smuzhiyun * The caller provided key is set for the AEAD referenced by the cipher
290*4882a593Smuzhiyun * handle.
291*4882a593Smuzhiyun *
292*4882a593Smuzhiyun * Note, the key length determines the cipher type. Many block ciphers implement
293*4882a593Smuzhiyun * different cipher modes depending on the key size, such as AES-128 vs AES-192
294*4882a593Smuzhiyun * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
295*4882a593Smuzhiyun * is performed.
296*4882a593Smuzhiyun *
297*4882a593Smuzhiyun * Return: 0 if the setting of the key was successful; < 0 if an error occurred
298*4882a593Smuzhiyun */
299*4882a593Smuzhiyun int crypto_aead_setkey(struct crypto_aead *tfm,
300*4882a593Smuzhiyun const u8 *key, unsigned int keylen);
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun /**
303*4882a593Smuzhiyun * crypto_aead_setauthsize() - set authentication data size
304*4882a593Smuzhiyun * @tfm: cipher handle
305*4882a593Smuzhiyun * @authsize: size of the authentication data / tag in bytes
306*4882a593Smuzhiyun *
307*4882a593Smuzhiyun * Set the authentication data size / tag size. AEAD requires an authentication
308*4882a593Smuzhiyun * tag (or MAC) in addition to the associated data.
309*4882a593Smuzhiyun *
310*4882a593Smuzhiyun * Return: 0 if the setting of the key was successful; < 0 if an error occurred
311*4882a593Smuzhiyun */
312*4882a593Smuzhiyun int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
313*4882a593Smuzhiyun
crypto_aead_reqtfm(struct aead_request * req)314*4882a593Smuzhiyun static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun return __crypto_aead_cast(req->base.tfm);
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun /**
320*4882a593Smuzhiyun * crypto_aead_encrypt() - encrypt plaintext
321*4882a593Smuzhiyun * @req: reference to the aead_request handle that holds all information
322*4882a593Smuzhiyun * needed to perform the cipher operation
323*4882a593Smuzhiyun *
324*4882a593Smuzhiyun * Encrypt plaintext data using the aead_request handle. That data structure
325*4882a593Smuzhiyun * and how it is filled with data is discussed with the aead_request_*
326*4882a593Smuzhiyun * functions.
327*4882a593Smuzhiyun *
328*4882a593Smuzhiyun * IMPORTANT NOTE The encryption operation creates the authentication data /
329*4882a593Smuzhiyun * tag. That data is concatenated with the created ciphertext.
330*4882a593Smuzhiyun * The ciphertext memory size is therefore the given number of
331*4882a593Smuzhiyun * block cipher blocks + the size defined by the
332*4882a593Smuzhiyun * crypto_aead_setauthsize invocation. The caller must ensure
333*4882a593Smuzhiyun * that sufficient memory is available for the ciphertext and
334*4882a593Smuzhiyun * the authentication tag.
335*4882a593Smuzhiyun *
336*4882a593Smuzhiyun * Return: 0 if the cipher operation was successful; < 0 if an error occurred
337*4882a593Smuzhiyun */
338*4882a593Smuzhiyun int crypto_aead_encrypt(struct aead_request *req);
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun /**
341*4882a593Smuzhiyun * crypto_aead_decrypt() - decrypt ciphertext
342*4882a593Smuzhiyun * @req: reference to the aead_request handle that holds all information
343*4882a593Smuzhiyun * needed to perform the cipher operation
344*4882a593Smuzhiyun *
345*4882a593Smuzhiyun * Decrypt ciphertext data using the aead_request handle. That data structure
346*4882a593Smuzhiyun * and how it is filled with data is discussed with the aead_request_*
347*4882a593Smuzhiyun * functions.
348*4882a593Smuzhiyun *
349*4882a593Smuzhiyun * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
350*4882a593Smuzhiyun * authentication data / tag. That authentication data / tag
351*4882a593Smuzhiyun * must have the size defined by the crypto_aead_setauthsize
352*4882a593Smuzhiyun * invocation.
353*4882a593Smuzhiyun *
354*4882a593Smuzhiyun *
355*4882a593Smuzhiyun * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
356*4882a593Smuzhiyun * cipher operation performs the authentication of the data during the
357*4882a593Smuzhiyun * decryption operation. Therefore, the function returns this error if
358*4882a593Smuzhiyun * the authentication of the ciphertext was unsuccessful (i.e. the
359*4882a593Smuzhiyun * integrity of the ciphertext or the associated data was violated);
360*4882a593Smuzhiyun * < 0 if an error occurred.
361*4882a593Smuzhiyun */
362*4882a593Smuzhiyun int crypto_aead_decrypt(struct aead_request *req);
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun /**
365*4882a593Smuzhiyun * DOC: Asynchronous AEAD Request Handle
366*4882a593Smuzhiyun *
367*4882a593Smuzhiyun * The aead_request data structure contains all pointers to data required for
368*4882a593Smuzhiyun * the AEAD cipher operation. This includes the cipher handle (which can be
369*4882a593Smuzhiyun * used by multiple aead_request instances), pointer to plaintext and
370*4882a593Smuzhiyun * ciphertext, asynchronous callback function, etc. It acts as a handle to the
371*4882a593Smuzhiyun * aead_request_* API calls in a similar way as AEAD handle to the
372*4882a593Smuzhiyun * crypto_aead_* API calls.
373*4882a593Smuzhiyun */
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun /**
376*4882a593Smuzhiyun * crypto_aead_reqsize() - obtain size of the request data structure
377*4882a593Smuzhiyun * @tfm: cipher handle
378*4882a593Smuzhiyun *
379*4882a593Smuzhiyun * Return: number of bytes
380*4882a593Smuzhiyun */
crypto_aead_reqsize(struct crypto_aead * tfm)381*4882a593Smuzhiyun static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun return tfm->reqsize;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /**
387*4882a593Smuzhiyun * aead_request_set_tfm() - update cipher handle reference in request
388*4882a593Smuzhiyun * @req: request handle to be modified
389*4882a593Smuzhiyun * @tfm: cipher handle that shall be added to the request handle
390*4882a593Smuzhiyun *
391*4882a593Smuzhiyun * Allow the caller to replace the existing aead handle in the request
392*4882a593Smuzhiyun * data structure with a different one.
393*4882a593Smuzhiyun */
aead_request_set_tfm(struct aead_request * req,struct crypto_aead * tfm)394*4882a593Smuzhiyun static inline void aead_request_set_tfm(struct aead_request *req,
395*4882a593Smuzhiyun struct crypto_aead *tfm)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun req->base.tfm = crypto_aead_tfm(tfm);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun /**
401*4882a593Smuzhiyun * aead_request_alloc() - allocate request data structure
402*4882a593Smuzhiyun * @tfm: cipher handle to be registered with the request
403*4882a593Smuzhiyun * @gfp: memory allocation flag that is handed to kmalloc by the API call.
404*4882a593Smuzhiyun *
405*4882a593Smuzhiyun * Allocate the request data structure that must be used with the AEAD
406*4882a593Smuzhiyun * encrypt and decrypt API calls. During the allocation, the provided aead
407*4882a593Smuzhiyun * handle is registered in the request data structure.
408*4882a593Smuzhiyun *
409*4882a593Smuzhiyun * Return: allocated request handle in case of success, or NULL if out of memory
410*4882a593Smuzhiyun */
aead_request_alloc(struct crypto_aead * tfm,gfp_t gfp)411*4882a593Smuzhiyun static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
412*4882a593Smuzhiyun gfp_t gfp)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun struct aead_request *req;
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun if (likely(req))
419*4882a593Smuzhiyun aead_request_set_tfm(req, tfm);
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun return req;
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun /**
425*4882a593Smuzhiyun * aead_request_free() - zeroize and free request data structure
426*4882a593Smuzhiyun * @req: request data structure cipher handle to be freed
427*4882a593Smuzhiyun */
aead_request_free(struct aead_request * req)428*4882a593Smuzhiyun static inline void aead_request_free(struct aead_request *req)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun kfree_sensitive(req);
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun /**
434*4882a593Smuzhiyun * aead_request_set_callback() - set asynchronous callback function
435*4882a593Smuzhiyun * @req: request handle
436*4882a593Smuzhiyun * @flags: specify zero or an ORing of the flags
437*4882a593Smuzhiyun * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
438*4882a593Smuzhiyun * increase the wait queue beyond the initial maximum size;
439*4882a593Smuzhiyun * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
440*4882a593Smuzhiyun * @compl: callback function pointer to be registered with the request handle
441*4882a593Smuzhiyun * @data: The data pointer refers to memory that is not used by the kernel
442*4882a593Smuzhiyun * crypto API, but provided to the callback function for it to use. Here,
443*4882a593Smuzhiyun * the caller can provide a reference to memory the callback function can
444*4882a593Smuzhiyun * operate on. As the callback function is invoked asynchronously to the
445*4882a593Smuzhiyun * related functionality, it may need to access data structures of the
446*4882a593Smuzhiyun * related functionality which can be referenced using this pointer. The
447*4882a593Smuzhiyun * callback function can access the memory via the "data" field in the
448*4882a593Smuzhiyun * crypto_async_request data structure provided to the callback function.
449*4882a593Smuzhiyun *
450*4882a593Smuzhiyun * Setting the callback function that is triggered once the cipher operation
451*4882a593Smuzhiyun * completes
452*4882a593Smuzhiyun *
453*4882a593Smuzhiyun * The callback function is registered with the aead_request handle and
454*4882a593Smuzhiyun * must comply with the following template::
455*4882a593Smuzhiyun *
456*4882a593Smuzhiyun * void callback_function(struct crypto_async_request *req, int error)
457*4882a593Smuzhiyun */
aead_request_set_callback(struct aead_request * req,u32 flags,crypto_completion_t compl,void * data)458*4882a593Smuzhiyun static inline void aead_request_set_callback(struct aead_request *req,
459*4882a593Smuzhiyun u32 flags,
460*4882a593Smuzhiyun crypto_completion_t compl,
461*4882a593Smuzhiyun void *data)
462*4882a593Smuzhiyun {
463*4882a593Smuzhiyun req->base.complete = compl;
464*4882a593Smuzhiyun req->base.data = data;
465*4882a593Smuzhiyun req->base.flags = flags;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun /**
469*4882a593Smuzhiyun * aead_request_set_crypt - set data buffers
470*4882a593Smuzhiyun * @req: request handle
471*4882a593Smuzhiyun * @src: source scatter / gather list
472*4882a593Smuzhiyun * @dst: destination scatter / gather list
473*4882a593Smuzhiyun * @cryptlen: number of bytes to process from @src
474*4882a593Smuzhiyun * @iv: IV for the cipher operation which must comply with the IV size defined
475*4882a593Smuzhiyun * by crypto_aead_ivsize()
476*4882a593Smuzhiyun *
477*4882a593Smuzhiyun * Setting the source data and destination data scatter / gather lists which
478*4882a593Smuzhiyun * hold the associated data concatenated with the plaintext or ciphertext. See
479*4882a593Smuzhiyun * below for the authentication tag.
480*4882a593Smuzhiyun *
481*4882a593Smuzhiyun * For encryption, the source is treated as the plaintext and the
482*4882a593Smuzhiyun * destination is the ciphertext. For a decryption operation, the use is
483*4882a593Smuzhiyun * reversed - the source is the ciphertext and the destination is the plaintext.
484*4882a593Smuzhiyun *
485*4882a593Smuzhiyun * The memory structure for cipher operation has the following structure:
486*4882a593Smuzhiyun *
487*4882a593Smuzhiyun * - AEAD encryption input: assoc data || plaintext
488*4882a593Smuzhiyun * - AEAD encryption output: assoc data || cipherntext || auth tag
489*4882a593Smuzhiyun * - AEAD decryption input: assoc data || ciphertext || auth tag
490*4882a593Smuzhiyun * - AEAD decryption output: assoc data || plaintext
491*4882a593Smuzhiyun *
492*4882a593Smuzhiyun * Albeit the kernel requires the presence of the AAD buffer, however,
493*4882a593Smuzhiyun * the kernel does not fill the AAD buffer in the output case. If the
494*4882a593Smuzhiyun * caller wants to have that data buffer filled, the caller must either
495*4882a593Smuzhiyun * use an in-place cipher operation (i.e. same memory location for
496*4882a593Smuzhiyun * input/output memory location).
497*4882a593Smuzhiyun */
aead_request_set_crypt(struct aead_request * req,struct scatterlist * src,struct scatterlist * dst,unsigned int cryptlen,u8 * iv)498*4882a593Smuzhiyun static inline void aead_request_set_crypt(struct aead_request *req,
499*4882a593Smuzhiyun struct scatterlist *src,
500*4882a593Smuzhiyun struct scatterlist *dst,
501*4882a593Smuzhiyun unsigned int cryptlen, u8 *iv)
502*4882a593Smuzhiyun {
503*4882a593Smuzhiyun req->src = src;
504*4882a593Smuzhiyun req->dst = dst;
505*4882a593Smuzhiyun req->cryptlen = cryptlen;
506*4882a593Smuzhiyun req->iv = iv;
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun /**
510*4882a593Smuzhiyun * aead_request_set_ad - set associated data information
511*4882a593Smuzhiyun * @req: request handle
512*4882a593Smuzhiyun * @assoclen: number of bytes in associated data
513*4882a593Smuzhiyun *
514*4882a593Smuzhiyun * Setting the AD information. This function sets the length of
515*4882a593Smuzhiyun * the associated data.
516*4882a593Smuzhiyun */
aead_request_set_ad(struct aead_request * req,unsigned int assoclen)517*4882a593Smuzhiyun static inline void aead_request_set_ad(struct aead_request *req,
518*4882a593Smuzhiyun unsigned int assoclen)
519*4882a593Smuzhiyun {
520*4882a593Smuzhiyun req->assoclen = assoclen;
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun #endif /* _CRYPTO_AEAD_H */
524