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