2 * AEAD: Authenticated Encryption with Associated Data
4 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
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)
13 #ifndef _CRYPTO_AEAD_H
14 #define _CRYPTO_AEAD_H
16 #include <linux/crypto.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
21 * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API
23 * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD
24 * (listed as type "aead" in /proc/crypto)
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:
30 * authenc(keyed message digest, block cipher)
32 * For example: authenc(hmac(sha256), cbc(aes))
34 * The example code provided for the asynchronous block cipher operation
35 * applies here as well. Naturally all *ablkcipher* symbols must be exchanged
36 * the *aead* pendants discussed in the following. In addtion, for the AEAD
37 * operation, the aead_request_set_assoc 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
51 * struct aead_request - AEAD request
52 * @base: Common attributes for async crypto requests
53 * @assoclen: Length in bytes of associated data for authentication
54 * @cryptlen: Length of data to be encrypted or decrypted
55 * @cryptoff: Bytes to skip after AD before plain/cipher text
56 * @iv: Initialisation vector
57 * @assoc: Associated data
59 * @dst: Destination data
60 * @__ctx: Start of private context data
63 struct crypto_async_request base
;
67 unsigned int assoclen
;
68 unsigned int cryptlen
;
69 unsigned int cryptoff
;
73 struct scatterlist
*assoc
;
74 struct scatterlist
*src
;
75 struct scatterlist
*dst
;
77 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
81 * struct aead_givcrypt_request - AEAD request with IV generation
82 * @seq: Sequence number for IV generation
83 * @giv: Space for generated IV
84 * @areq: The AEAD request itself
86 struct aead_givcrypt_request
{
90 struct aead_request areq
;
94 * struct aead_alg - AEAD cipher definition
95 * @maxauthsize: Set the maximum authentication tag size supported by the
96 * transformation. A transformation may support smaller tag sizes.
97 * As the authentication tag is a message digest to ensure the
98 * integrity of the encrypted data, a consumer typically wants the
99 * largest authentication tag possible as defined by this
101 * @setauthsize: Set authentication size for the AEAD transformation. This
102 * function is used to specify the consumer requested size of the
103 * authentication tag to be either generated by the transformation
104 * during encryption or the size of the authentication tag to be
105 * supplied during the decryption operation. This function is also
106 * responsible for checking the authentication tag size for
108 * @setkey: see struct ablkcipher_alg
109 * @encrypt: see struct ablkcipher_alg
110 * @decrypt: see struct ablkcipher_alg
111 * @geniv: see struct ablkcipher_alg
112 * @ivsize: see struct ablkcipher_alg
114 * All fields except @ivsize is mandatory and must be filled.
117 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
118 unsigned int keylen
);
119 int (*setauthsize
)(struct crypto_aead
*tfm
, unsigned int authsize
);
120 int (*encrypt
)(struct aead_request
*req
);
121 int (*decrypt
)(struct aead_request
*req
);
126 unsigned int maxauthsize
;
128 struct crypto_alg base
;
132 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
133 unsigned int keylen
);
134 int (*setauthsize
)(struct crypto_aead
*tfm
, unsigned int authsize
);
135 int (*encrypt
)(struct aead_request
*req
);
136 int (*decrypt
)(struct aead_request
*req
);
137 int (*givencrypt
)(struct aead_givcrypt_request
*req
);
138 int (*givdecrypt
)(struct aead_givcrypt_request
*req
);
140 struct crypto_aead
*child
;
142 unsigned int authsize
;
143 unsigned int reqsize
;
145 struct crypto_tfm base
;
148 static inline struct crypto_aead
*__crypto_aead_cast(struct crypto_tfm
*tfm
)
150 return container_of(tfm
, struct crypto_aead
, base
);
154 * crypto_alloc_aead() - allocate AEAD cipher handle
155 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
157 * @type: specifies the type of the cipher
158 * @mask: specifies the mask for the cipher
160 * Allocate a cipher handle for an AEAD. The returned struct
161 * crypto_aead is the cipher handle that is required for any subsequent
162 * API invocation for that AEAD.
164 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
165 * of an error, PTR_ERR() returns the error code.
167 struct crypto_aead
*crypto_alloc_aead(const char *alg_name
, u32 type
, u32 mask
);
169 static inline struct crypto_tfm
*crypto_aead_tfm(struct crypto_aead
*tfm
)
175 * crypto_free_aead() - zeroize and free aead handle
176 * @tfm: cipher handle to be freed
178 static inline void crypto_free_aead(struct crypto_aead
*tfm
)
180 crypto_destroy_tfm(tfm
, crypto_aead_tfm(tfm
));
183 static inline struct crypto_aead
*crypto_aead_crt(struct crypto_aead
*tfm
)
188 static inline struct old_aead_alg
*crypto_old_aead_alg(struct crypto_aead
*tfm
)
190 return &crypto_aead_tfm(tfm
)->__crt_alg
->cra_aead
;
193 static inline struct aead_alg
*crypto_aead_alg(struct crypto_aead
*tfm
)
195 return container_of(crypto_aead_tfm(tfm
)->__crt_alg
,
196 struct aead_alg
, base
);
199 static inline unsigned int crypto_aead_alg_ivsize(struct aead_alg
*alg
)
201 return alg
->base
.cra_aead
.encrypt
? alg
->base
.cra_aead
.ivsize
:
206 * crypto_aead_ivsize() - obtain IV size
207 * @tfm: cipher handle
209 * The size of the IV for the aead referenced by the cipher handle is
210 * returned. This IV size may be zero if the cipher does not need an IV.
212 * Return: IV size in bytes
214 static inline unsigned int crypto_aead_ivsize(struct crypto_aead
*tfm
)
216 return crypto_aead_alg_ivsize(crypto_aead_alg(tfm
));
220 * crypto_aead_authsize() - obtain maximum authentication data size
221 * @tfm: cipher handle
223 * The maximum size of the authentication data for the AEAD cipher referenced
224 * by the AEAD cipher handle is returned. The authentication data size may be
225 * zero if the cipher implements a hard-coded maximum.
227 * The authentication data may also be known as "tag value".
229 * Return: authentication data size / tag size in bytes
231 static inline unsigned int crypto_aead_authsize(struct crypto_aead
*tfm
)
233 return tfm
->authsize
;
237 * crypto_aead_blocksize() - obtain block size of cipher
238 * @tfm: cipher handle
240 * The block size for the AEAD referenced with the cipher handle is returned.
241 * The caller may use that information to allocate appropriate memory for the
242 * data returned by the encryption or decryption operation
244 * Return: block size of cipher
246 static inline unsigned int crypto_aead_blocksize(struct crypto_aead
*tfm
)
248 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm
));
251 static inline unsigned int crypto_aead_alignmask(struct crypto_aead
*tfm
)
253 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm
));
256 static inline u32
crypto_aead_get_flags(struct crypto_aead
*tfm
)
258 return crypto_tfm_get_flags(crypto_aead_tfm(tfm
));
261 static inline void crypto_aead_set_flags(struct crypto_aead
*tfm
, u32 flags
)
263 crypto_tfm_set_flags(crypto_aead_tfm(tfm
), flags
);
266 static inline void crypto_aead_clear_flags(struct crypto_aead
*tfm
, u32 flags
)
268 crypto_tfm_clear_flags(crypto_aead_tfm(tfm
), flags
);
272 * crypto_aead_setkey() - set key for cipher
273 * @tfm: cipher handle
274 * @key: buffer holding the key
275 * @keylen: length of the key in bytes
277 * The caller provided key is set for the AEAD referenced by the cipher
280 * Note, the key length determines the cipher type. Many block ciphers implement
281 * different cipher modes depending on the key size, such as AES-128 vs AES-192
282 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
285 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
287 int crypto_aead_setkey(struct crypto_aead
*tfm
,
288 const u8
*key
, unsigned int keylen
);
291 * crypto_aead_setauthsize() - set authentication data size
292 * @tfm: cipher handle
293 * @authsize: size of the authentication data / tag in bytes
295 * Set the authentication data size / tag size. AEAD requires an authentication
296 * tag (or MAC) in addition to the associated data.
298 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
300 int crypto_aead_setauthsize(struct crypto_aead
*tfm
, unsigned int authsize
);
302 static inline struct crypto_aead
*crypto_aead_reqtfm(struct aead_request
*req
)
304 return __crypto_aead_cast(req
->base
.tfm
);
308 * crypto_aead_encrypt() - encrypt plaintext
309 * @req: reference to the aead_request handle that holds all information
310 * needed to perform the cipher operation
312 * Encrypt plaintext data using the aead_request handle. That data structure
313 * and how it is filled with data is discussed with the aead_request_*
316 * IMPORTANT NOTE The encryption operation creates the authentication data /
317 * tag. That data is concatenated with the created ciphertext.
318 * The ciphertext memory size is therefore the given number of
319 * block cipher blocks + the size defined by the
320 * crypto_aead_setauthsize invocation. The caller must ensure
321 * that sufficient memory is available for the ciphertext and
322 * the authentication tag.
324 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
326 static inline int crypto_aead_encrypt(struct aead_request
*req
)
328 return crypto_aead_reqtfm(req
)->encrypt(req
);
332 * crypto_aead_decrypt() - decrypt ciphertext
333 * @req: reference to the ablkcipher_request handle that holds all information
334 * needed to perform the cipher operation
336 * Decrypt ciphertext data using the aead_request handle. That data structure
337 * and how it is filled with data is discussed with the aead_request_*
340 * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
341 * authentication data / tag. That authentication data / tag
342 * must have the size defined by the crypto_aead_setauthsize
346 * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
347 * cipher operation performs the authentication of the data during the
348 * decryption operation. Therefore, the function returns this error if
349 * the authentication of the ciphertext was unsuccessful (i.e. the
350 * integrity of the ciphertext or the associated data was violated);
351 * < 0 if an error occurred.
353 static inline int crypto_aead_decrypt(struct aead_request
*req
)
355 if (req
->cryptlen
< crypto_aead_authsize(crypto_aead_reqtfm(req
)))
358 return crypto_aead_reqtfm(req
)->decrypt(req
);
362 * DOC: Asynchronous AEAD Request Handle
364 * The aead_request data structure contains all pointers to data required for
365 * the AEAD cipher operation. This includes the cipher handle (which can be
366 * used by multiple aead_request instances), pointer to plaintext and
367 * ciphertext, asynchronous callback function, etc. It acts as a handle to the
368 * aead_request_* API calls in a similar way as AEAD handle to the
369 * crypto_aead_* API calls.
373 * crypto_aead_reqsize() - obtain size of the request data structure
374 * @tfm: cipher handle
376 * Return: number of bytes
378 unsigned int crypto_aead_reqsize(struct crypto_aead
*tfm
);
381 * aead_request_set_tfm() - update cipher handle reference in request
382 * @req: request handle to be modified
383 * @tfm: cipher handle that shall be added to the request handle
385 * Allow the caller to replace the existing aead handle in the request
386 * data structure with a different one.
388 static inline void aead_request_set_tfm(struct aead_request
*req
,
389 struct crypto_aead
*tfm
)
391 req
->base
.tfm
= crypto_aead_tfm(tfm
->child
);
395 * aead_request_alloc() - allocate request data structure
396 * @tfm: cipher handle to be registered with the request
397 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
399 * Allocate the request data structure that must be used with the AEAD
400 * encrypt and decrypt API calls. During the allocation, the provided aead
401 * handle is registered in the request data structure.
403 * Return: allocated request handle in case of success; IS_ERR() is true in case
404 * of an error, PTR_ERR() returns the error code.
406 static inline struct aead_request
*aead_request_alloc(struct crypto_aead
*tfm
,
409 struct aead_request
*req
;
411 req
= kmalloc(sizeof(*req
) + crypto_aead_reqsize(tfm
), gfp
);
414 aead_request_set_tfm(req
, tfm
);
420 * aead_request_free() - zeroize and free request data structure
421 * @req: request data structure cipher handle to be freed
423 static inline void aead_request_free(struct aead_request
*req
)
429 * aead_request_set_callback() - set asynchronous callback function
430 * @req: request handle
431 * @flags: specify zero or an ORing of the flags
432 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
433 * increase the wait queue beyond the initial maximum size;
434 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
435 * @compl: callback function pointer to be registered with the request handle
436 * @data: The data pointer refers to memory that is not used by the kernel
437 * crypto API, but provided to the callback function for it to use. Here,
438 * the caller can provide a reference to memory the callback function can
439 * operate on. As the callback function is invoked asynchronously to the
440 * related functionality, it may need to access data structures of the
441 * related functionality which can be referenced using this pointer. The
442 * callback function can access the memory via the "data" field in the
443 * crypto_async_request data structure provided to the callback function.
445 * Setting the callback function that is triggered once the cipher operation
448 * The callback function is registered with the aead_request handle and
449 * must comply with the following template
451 * void callback_function(struct crypto_async_request *req, int error)
453 static inline void aead_request_set_callback(struct aead_request
*req
,
455 crypto_completion_t
compl,
458 req
->base
.complete
= compl;
459 req
->base
.data
= data
;
460 req
->base
.flags
= flags
;
464 * aead_request_set_crypt - set data buffers
465 * @req: request handle
466 * @src: source scatter / gather list
467 * @dst: destination scatter / gather list
468 * @cryptlen: number of bytes to process from @src
469 * @iv: IV for the cipher operation which must comply with the IV size defined
470 * by crypto_aead_ivsize()
472 * Setting the source data and destination data scatter / gather lists.
474 * For encryption, the source is treated as the plaintext and the
475 * destination is the ciphertext. For a decryption operation, the use is
476 * reversed - the source is the ciphertext and the destination is the plaintext.
478 * For both src/dst the layout is associated data, skipped data,
479 * plain/cipher text, authentication tag.
481 * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption,
482 * the caller must concatenate the ciphertext followed by the
483 * authentication tag and provide the entire data stream to the
484 * decryption operation (i.e. the data length used for the
485 * initialization of the scatterlist and the data length for the
486 * decryption operation is identical). For encryption, however,
487 * the authentication tag is created while encrypting the data.
488 * The destination buffer must hold sufficient space for the
489 * ciphertext and the authentication tag while the encryption
490 * invocation must only point to the plaintext data size. The
491 * following code snippet illustrates the memory usage
492 * buffer = kmalloc(ptbuflen + (enc ? authsize : 0));
493 * sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0));
494 * aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv);
496 static inline void aead_request_set_crypt(struct aead_request
*req
,
497 struct scatterlist
*src
,
498 struct scatterlist
*dst
,
499 unsigned int cryptlen
, u8
*iv
)
503 req
->cryptlen
= cryptlen
;
508 * aead_request_set_assoc() - set the associated data scatter / gather list
509 * @req: request handle
510 * @assoc: associated data scatter / gather list
511 * @assoclen: number of bytes to process from @assoc
513 * Obsolete, do not use.
515 static inline void aead_request_set_assoc(struct aead_request
*req
,
516 struct scatterlist
*assoc
,
517 unsigned int assoclen
)
520 req
->assoclen
= assoclen
;
525 * aead_request_set_ad - set associated data information
526 * @req: request handle
527 * @assoclen: number of bytes in associated data
528 * @cryptoff: Number of bytes to skip after AD before plain/cipher text
530 * Setting the AD information. This function sets the length of
531 * the associated data and the number of bytes to skip after it to
532 * access the plain/cipher text.
534 static inline void aead_request_set_ad(struct aead_request
*req
,
535 unsigned int assoclen
,
536 unsigned int cryptoff
)
538 req
->assoclen
= assoclen
;
539 req
->cryptoff
= cryptoff
;
543 static inline struct crypto_aead
*aead_givcrypt_reqtfm(
544 struct aead_givcrypt_request
*req
)
546 return crypto_aead_reqtfm(&req
->areq
);
549 static inline int crypto_aead_givencrypt(struct aead_givcrypt_request
*req
)
551 return aead_givcrypt_reqtfm(req
)->givencrypt(req
);
554 static inline int crypto_aead_givdecrypt(struct aead_givcrypt_request
*req
)
556 return aead_givcrypt_reqtfm(req
)->givdecrypt(req
);
559 static inline void aead_givcrypt_set_tfm(struct aead_givcrypt_request
*req
,
560 struct crypto_aead
*tfm
)
562 req
->areq
.base
.tfm
= crypto_aead_tfm(tfm
);
565 static inline struct aead_givcrypt_request
*aead_givcrypt_alloc(
566 struct crypto_aead
*tfm
, gfp_t gfp
)
568 struct aead_givcrypt_request
*req
;
570 req
= kmalloc(sizeof(struct aead_givcrypt_request
) +
571 crypto_aead_reqsize(tfm
), gfp
);
574 aead_givcrypt_set_tfm(req
, tfm
);
579 static inline void aead_givcrypt_free(struct aead_givcrypt_request
*req
)
584 static inline void aead_givcrypt_set_callback(
585 struct aead_givcrypt_request
*req
, u32 flags
,
586 crypto_completion_t
compl, void *data
)
588 aead_request_set_callback(&req
->areq
, flags
, compl, data
);
591 static inline void aead_givcrypt_set_crypt(struct aead_givcrypt_request
*req
,
592 struct scatterlist
*src
,
593 struct scatterlist
*dst
,
594 unsigned int nbytes
, void *iv
)
596 aead_request_set_crypt(&req
->areq
, src
, dst
, nbytes
, iv
);
599 static inline void aead_givcrypt_set_assoc(struct aead_givcrypt_request
*req
,
600 struct scatterlist
*assoc
,
601 unsigned int assoclen
)
603 aead_request_set_assoc(&req
->areq
, assoc
, assoclen
);
606 static inline void aead_givcrypt_set_giv(struct aead_givcrypt_request
*req
,
613 #endif /* _CRYPTO_AEAD_H */