![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0-or-later */ 0002 /* 0003 * AEAD: Authenticated Encryption with Associated Data 0004 * 0005 * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au> 0006 */ 0007 0008 #ifndef _CRYPTO_AEAD_H 0009 #define _CRYPTO_AEAD_H 0010 0011 #include <linux/container_of.h> 0012 #include <linux/crypto.h> 0013 #include <linux/slab.h> 0014 #include <linux/types.h> 0015 0016 /** 0017 * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API 0018 * 0019 * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD 0020 * (listed as type "aead" in /proc/crypto) 0021 * 0022 * The most prominent examples for this type of encryption is GCM and CCM. 0023 * However, the kernel supports other types of AEAD ciphers which are defined 0024 * with the following cipher string: 0025 * 0026 * authenc(keyed message digest, block cipher) 0027 * 0028 * For example: authenc(hmac(sha256), cbc(aes)) 0029 * 0030 * The example code provided for the symmetric key cipher operation 0031 * applies here as well. Naturally all *skcipher* symbols must be exchanged 0032 * the *aead* pendants discussed in the following. In addition, for the AEAD 0033 * operation, the aead_request_set_ad function must be used to set the 0034 * pointer to the associated data memory location before performing the 0035 * encryption or decryption operation. In case of an encryption, the associated 0036 * data memory is filled during the encryption operation. For decryption, the 0037 * associated data memory must contain data that is used to verify the integrity 0038 * of the decrypted data. Another deviation from the asynchronous block cipher 0039 * operation is that the caller should explicitly check for -EBADMSG of the 0040 * crypto_aead_decrypt. That error indicates an authentication error, i.e. 0041 * a breach in the integrity of the message. In essence, that -EBADMSG error 0042 * code is the key bonus an AEAD cipher has over "standard" block chaining 0043 * modes. 0044 * 0045 * Memory Structure: 0046 * 0047 * The source scatterlist must contain the concatenation of 0048 * associated data || plaintext or ciphertext. 0049 * 0050 * The destination scatterlist has the same layout, except that the plaintext 0051 * (resp. ciphertext) will grow (resp. shrink) by the authentication tag size 0052 * during encryption (resp. decryption). 0053 * 0054 * In-place encryption/decryption is enabled by using the same scatterlist 0055 * pointer for both the source and destination. 0056 * 0057 * Even in the out-of-place case, space must be reserved in the destination for 0058 * the associated data, even though it won't be written to. This makes the 0059 * in-place and out-of-place cases more consistent. It is permissible for the 0060 * "destination" associated data to alias the "source" associated data. 0061 * 0062 * As with the other scatterlist crypto APIs, zero-length scatterlist elements 0063 * are not allowed in the used part of the scatterlist. Thus, if there is no 0064 * associated data, the first element must point to the plaintext/ciphertext. 0065 * 0066 * To meet the needs of IPsec, a special quirk applies to rfc4106, rfc4309, 0067 * rfc4543, and rfc7539esp ciphers. For these ciphers, the final 'ivsize' bytes 0068 * of the associated data buffer must contain a second copy of the IV. This is 0069 * in addition to the copy passed to aead_request_set_crypt(). These two IV 0070 * copies must not differ; different implementations of the same algorithm may 0071 * behave differently in that case. Note that the algorithm might not actually 0072 * treat the IV as associated data; nevertheless the length passed to 0073 * aead_request_set_ad() must include it. 0074 */ 0075 0076 struct crypto_aead; 0077 struct scatterlist; 0078 0079 /** 0080 * struct aead_request - AEAD request 0081 * @base: Common attributes for async crypto requests 0082 * @assoclen: Length in bytes of associated data for authentication 0083 * @cryptlen: Length of data to be encrypted or decrypted 0084 * @iv: Initialisation vector 0085 * @src: Source data 0086 * @dst: Destination data 0087 * @__ctx: Start of private context data 0088 */ 0089 struct aead_request { 0090 struct crypto_async_request base; 0091 0092 unsigned int assoclen; 0093 unsigned int cryptlen; 0094 0095 u8 *iv; 0096 0097 struct scatterlist *src; 0098 struct scatterlist *dst; 0099 0100 void *__ctx[] CRYPTO_MINALIGN_ATTR; 0101 }; 0102 0103 /** 0104 * struct aead_alg - AEAD cipher definition 0105 * @maxauthsize: Set the maximum authentication tag size supported by the 0106 * transformation. A transformation may support smaller tag sizes. 0107 * As the authentication tag is a message digest to ensure the 0108 * integrity of the encrypted data, a consumer typically wants the 0109 * largest authentication tag possible as defined by this 0110 * variable. 0111 * @setauthsize: Set authentication size for the AEAD transformation. This 0112 * function is used to specify the consumer requested size of the 0113 * authentication tag to be either generated by the transformation 0114 * during encryption or the size of the authentication tag to be 0115 * supplied during the decryption operation. This function is also 0116 * responsible for checking the authentication tag size for 0117 * validity. 0118 * @setkey: see struct skcipher_alg 0119 * @encrypt: see struct skcipher_alg 0120 * @decrypt: see struct skcipher_alg 0121 * @ivsize: see struct skcipher_alg 0122 * @chunksize: see struct skcipher_alg 0123 * @init: Initialize the cryptographic transformation object. This function 0124 * is used to initialize the cryptographic transformation object. 0125 * This function is called only once at the instantiation time, right 0126 * after the transformation context was allocated. In case the 0127 * cryptographic hardware has some special requirements which need to 0128 * be handled by software, this function shall check for the precise 0129 * requirement of the transformation and put any software fallbacks 0130 * in place. 0131 * @exit: Deinitialize the cryptographic transformation object. This is a 0132 * counterpart to @init, used to remove various changes set in 0133 * @init. 0134 * @base: Definition of a generic crypto cipher algorithm. 0135 * 0136 * All fields except @ivsize is mandatory and must be filled. 0137 */ 0138 struct aead_alg { 0139 int (*setkey)(struct crypto_aead *tfm, const u8 *key, 0140 unsigned int keylen); 0141 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize); 0142 int (*encrypt)(struct aead_request *req); 0143 int (*decrypt)(struct aead_request *req); 0144 int (*init)(struct crypto_aead *tfm); 0145 void (*exit)(struct crypto_aead *tfm); 0146 0147 unsigned int ivsize; 0148 unsigned int maxauthsize; 0149 unsigned int chunksize; 0150 0151 struct crypto_alg base; 0152 }; 0153 0154 struct crypto_aead { 0155 unsigned int authsize; 0156 unsigned int reqsize; 0157 0158 struct crypto_tfm base; 0159 }; 0160 0161 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm) 0162 { 0163 return container_of(tfm, struct crypto_aead, base); 0164 } 0165 0166 /** 0167 * crypto_alloc_aead() - allocate AEAD cipher handle 0168 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 0169 * AEAD cipher 0170 * @type: specifies the type of the cipher 0171 * @mask: specifies the mask for the cipher 0172 * 0173 * Allocate a cipher handle for an AEAD. The returned struct 0174 * crypto_aead is the cipher handle that is required for any subsequent 0175 * API invocation for that AEAD. 0176 * 0177 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 0178 * of an error, PTR_ERR() returns the error code. 0179 */ 0180 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask); 0181 0182 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm) 0183 { 0184 return &tfm->base; 0185 } 0186 0187 /** 0188 * crypto_free_aead() - zeroize and free aead handle 0189 * @tfm: cipher handle to be freed 0190 * 0191 * If @tfm is a NULL or error pointer, this function does nothing. 0192 */ 0193 static inline void crypto_free_aead(struct crypto_aead *tfm) 0194 { 0195 crypto_destroy_tfm(tfm, crypto_aead_tfm(tfm)); 0196 } 0197 0198 static inline const char *crypto_aead_driver_name(struct crypto_aead *tfm) 0199 { 0200 return crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)); 0201 } 0202 0203 static inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm) 0204 { 0205 return container_of(crypto_aead_tfm(tfm)->__crt_alg, 0206 struct aead_alg, base); 0207 } 0208 0209 static inline unsigned int crypto_aead_alg_ivsize(struct aead_alg *alg) 0210 { 0211 return alg->ivsize; 0212 } 0213 0214 /** 0215 * crypto_aead_ivsize() - obtain IV size 0216 * @tfm: cipher handle 0217 * 0218 * The size of the IV for the aead referenced by the cipher handle is 0219 * returned. This IV size may be zero if the cipher does not need an IV. 0220 * 0221 * Return: IV size in bytes 0222 */ 0223 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm) 0224 { 0225 return crypto_aead_alg_ivsize(crypto_aead_alg(tfm)); 0226 } 0227 0228 /** 0229 * crypto_aead_authsize() - obtain maximum authentication data size 0230 * @tfm: cipher handle 0231 * 0232 * The maximum size of the authentication data for the AEAD cipher referenced 0233 * by the AEAD cipher handle is returned. The authentication data size may be 0234 * zero if the cipher implements a hard-coded maximum. 0235 * 0236 * The authentication data may also be known as "tag value". 0237 * 0238 * Return: authentication data size / tag size in bytes 0239 */ 0240 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm) 0241 { 0242 return tfm->authsize; 0243 } 0244 0245 static inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg) 0246 { 0247 return alg->maxauthsize; 0248 } 0249 0250 static inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead) 0251 { 0252 return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead)); 0253 } 0254 0255 /** 0256 * crypto_aead_blocksize() - obtain block size of cipher 0257 * @tfm: cipher handle 0258 * 0259 * The block size for the AEAD referenced with the cipher handle is returned. 0260 * The caller may use that information to allocate appropriate memory for the 0261 * data returned by the encryption or decryption operation 0262 * 0263 * Return: block size of cipher 0264 */ 0265 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm) 0266 { 0267 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm)); 0268 } 0269 0270 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm) 0271 { 0272 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm)); 0273 } 0274 0275 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm) 0276 { 0277 return crypto_tfm_get_flags(crypto_aead_tfm(tfm)); 0278 } 0279 0280 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags) 0281 { 0282 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags); 0283 } 0284 0285 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags) 0286 { 0287 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags); 0288 } 0289 0290 /** 0291 * crypto_aead_setkey() - set key for cipher 0292 * @tfm: cipher handle 0293 * @key: buffer holding the key 0294 * @keylen: length of the key in bytes 0295 * 0296 * The caller provided key is set for the AEAD referenced by the cipher 0297 * handle. 0298 * 0299 * Note, the key length determines the cipher type. Many block ciphers implement 0300 * different cipher modes depending on the key size, such as AES-128 vs AES-192 0301 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 0302 * is performed. 0303 * 0304 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 0305 */ 0306 int crypto_aead_setkey(struct crypto_aead *tfm, 0307 const u8 *key, unsigned int keylen); 0308 0309 /** 0310 * crypto_aead_setauthsize() - set authentication data size 0311 * @tfm: cipher handle 0312 * @authsize: size of the authentication data / tag in bytes 0313 * 0314 * Set the authentication data size / tag size. AEAD requires an authentication 0315 * tag (or MAC) in addition to the associated data. 0316 * 0317 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 0318 */ 0319 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize); 0320 0321 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req) 0322 { 0323 return __crypto_aead_cast(req->base.tfm); 0324 } 0325 0326 /** 0327 * crypto_aead_encrypt() - encrypt plaintext 0328 * @req: reference to the aead_request handle that holds all information 0329 * needed to perform the cipher operation 0330 * 0331 * Encrypt plaintext data using the aead_request handle. That data structure 0332 * and how it is filled with data is discussed with the aead_request_* 0333 * functions. 0334 * 0335 * IMPORTANT NOTE The encryption operation creates the authentication data / 0336 * tag. That data is concatenated with the created ciphertext. 0337 * The ciphertext memory size is therefore the given number of 0338 * block cipher blocks + the size defined by the 0339 * crypto_aead_setauthsize invocation. The caller must ensure 0340 * that sufficient memory is available for the ciphertext and 0341 * the authentication tag. 0342 * 0343 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 0344 */ 0345 int crypto_aead_encrypt(struct aead_request *req); 0346 0347 /** 0348 * crypto_aead_decrypt() - decrypt ciphertext 0349 * @req: reference to the aead_request handle that holds all information 0350 * needed to perform the cipher operation 0351 * 0352 * Decrypt ciphertext data using the aead_request handle. That data structure 0353 * and how it is filled with data is discussed with the aead_request_* 0354 * functions. 0355 * 0356 * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the 0357 * authentication data / tag. That authentication data / tag 0358 * must have the size defined by the crypto_aead_setauthsize 0359 * invocation. 0360 * 0361 * 0362 * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD 0363 * cipher operation performs the authentication of the data during the 0364 * decryption operation. Therefore, the function returns this error if 0365 * the authentication of the ciphertext was unsuccessful (i.e. the 0366 * integrity of the ciphertext or the associated data was violated); 0367 * < 0 if an error occurred. 0368 */ 0369 int crypto_aead_decrypt(struct aead_request *req); 0370 0371 /** 0372 * DOC: Asynchronous AEAD Request Handle 0373 * 0374 * The aead_request data structure contains all pointers to data required for 0375 * the AEAD cipher operation. This includes the cipher handle (which can be 0376 * used by multiple aead_request instances), pointer to plaintext and 0377 * ciphertext, asynchronous callback function, etc. It acts as a handle to the 0378 * aead_request_* API calls in a similar way as AEAD handle to the 0379 * crypto_aead_* API calls. 0380 */ 0381 0382 /** 0383 * crypto_aead_reqsize() - obtain size of the request data structure 0384 * @tfm: cipher handle 0385 * 0386 * Return: number of bytes 0387 */ 0388 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm) 0389 { 0390 return tfm->reqsize; 0391 } 0392 0393 /** 0394 * aead_request_set_tfm() - update cipher handle reference in request 0395 * @req: request handle to be modified 0396 * @tfm: cipher handle that shall be added to the request handle 0397 * 0398 * Allow the caller to replace the existing aead handle in the request 0399 * data structure with a different one. 0400 */ 0401 static inline void aead_request_set_tfm(struct aead_request *req, 0402 struct crypto_aead *tfm) 0403 { 0404 req->base.tfm = crypto_aead_tfm(tfm); 0405 } 0406 0407 /** 0408 * aead_request_alloc() - allocate request data structure 0409 * @tfm: cipher handle to be registered with the request 0410 * @gfp: memory allocation flag that is handed to kmalloc by the API call. 0411 * 0412 * Allocate the request data structure that must be used with the AEAD 0413 * encrypt and decrypt API calls. During the allocation, the provided aead 0414 * handle is registered in the request data structure. 0415 * 0416 * Return: allocated request handle in case of success, or NULL if out of memory 0417 */ 0418 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm, 0419 gfp_t gfp) 0420 { 0421 struct aead_request *req; 0422 0423 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp); 0424 0425 if (likely(req)) 0426 aead_request_set_tfm(req, tfm); 0427 0428 return req; 0429 } 0430 0431 /** 0432 * aead_request_free() - zeroize and free request data structure 0433 * @req: request data structure cipher handle to be freed 0434 */ 0435 static inline void aead_request_free(struct aead_request *req) 0436 { 0437 kfree_sensitive(req); 0438 } 0439 0440 /** 0441 * aead_request_set_callback() - set asynchronous callback function 0442 * @req: request handle 0443 * @flags: specify zero or an ORing of the flags 0444 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and 0445 * increase the wait queue beyond the initial maximum size; 0446 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep 0447 * @compl: callback function pointer to be registered with the request handle 0448 * @data: The data pointer refers to memory that is not used by the kernel 0449 * crypto API, but provided to the callback function for it to use. Here, 0450 * the caller can provide a reference to memory the callback function can 0451 * operate on. As the callback function is invoked asynchronously to the 0452 * related functionality, it may need to access data structures of the 0453 * related functionality which can be referenced using this pointer. The 0454 * callback function can access the memory via the "data" field in the 0455 * crypto_async_request data structure provided to the callback function. 0456 * 0457 * Setting the callback function that is triggered once the cipher operation 0458 * completes 0459 * 0460 * The callback function is registered with the aead_request handle and 0461 * must comply with the following template:: 0462 * 0463 * void callback_function(struct crypto_async_request *req, int error) 0464 */ 0465 static inline void aead_request_set_callback(struct aead_request *req, 0466 u32 flags, 0467 crypto_completion_t compl, 0468 void *data) 0469 { 0470 req->base.complete = compl; 0471 req->base.data = data; 0472 req->base.flags = flags; 0473 } 0474 0475 /** 0476 * aead_request_set_crypt - set data buffers 0477 * @req: request handle 0478 * @src: source scatter / gather list 0479 * @dst: destination scatter / gather list 0480 * @cryptlen: number of bytes to process from @src 0481 * @iv: IV for the cipher operation which must comply with the IV size defined 0482 * by crypto_aead_ivsize() 0483 * 0484 * Setting the source data and destination data scatter / gather lists which 0485 * hold the associated data concatenated with the plaintext or ciphertext. See 0486 * below for the authentication tag. 0487 * 0488 * For encryption, the source is treated as the plaintext and the 0489 * destination is the ciphertext. For a decryption operation, the use is 0490 * reversed - the source is the ciphertext and the destination is the plaintext. 0491 * 0492 * The memory structure for cipher operation has the following structure: 0493 * 0494 * - AEAD encryption input: assoc data || plaintext 0495 * - AEAD encryption output: assoc data || ciphertext || auth tag 0496 * - AEAD decryption input: assoc data || ciphertext || auth tag 0497 * - AEAD decryption output: assoc data || plaintext 0498 * 0499 * Albeit the kernel requires the presence of the AAD buffer, however, 0500 * the kernel does not fill the AAD buffer in the output case. If the 0501 * caller wants to have that data buffer filled, the caller must either 0502 * use an in-place cipher operation (i.e. same memory location for 0503 * input/output memory location). 0504 */ 0505 static inline void aead_request_set_crypt(struct aead_request *req, 0506 struct scatterlist *src, 0507 struct scatterlist *dst, 0508 unsigned int cryptlen, u8 *iv) 0509 { 0510 req->src = src; 0511 req->dst = dst; 0512 req->cryptlen = cryptlen; 0513 req->iv = iv; 0514 } 0515 0516 /** 0517 * aead_request_set_ad - set associated data information 0518 * @req: request handle 0519 * @assoclen: number of bytes in associated data 0520 * 0521 * Setting the AD information. This function sets the length of 0522 * the associated data. 0523 */ 0524 static inline void aead_request_set_ad(struct aead_request *req, 0525 unsigned int assoclen) 0526 { 0527 req->assoclen = assoclen; 0528 } 0529 0530 #endif /* _CRYPTO_AEAD_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |