| .. | .. |
|---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-or-later */ |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * AEAD: Authenticated Encryption with Associated Data |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * 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 | 6 | */ |
|---|
| 12 | 7 | |
|---|
| 13 | 8 | #ifndef _CRYPTO_AEAD_H |
|---|
| .. | .. |
|---|
| 48 | 43 | * |
|---|
| 49 | 44 | * Memory Structure: |
|---|
| 50 | 45 | * |
|---|
| 51 | | - * To support the needs of the most prominent user of AEAD ciphers, namely |
|---|
| 52 | | - * IPSEC, the AEAD ciphers have a special memory layout the caller must adhere |
|---|
| 53 | | - * to. |
|---|
| 46 | + * The source scatterlist must contain the concatenation of |
|---|
| 47 | + * associated data || plaintext or ciphertext. |
|---|
| 54 | 48 | * |
|---|
| 55 | | - * The scatter list pointing to the input data must contain: |
|---|
| 49 | + * The destination scatterlist has the same layout, except that the plaintext |
|---|
| 50 | + * (resp. ciphertext) will grow (resp. shrink) by the authentication tag size |
|---|
| 51 | + * during encryption (resp. decryption). |
|---|
| 56 | 52 | * |
|---|
| 57 | | - * * for RFC4106 ciphers, the concatenation of |
|---|
| 58 | | - * associated authentication data || IV || plaintext or ciphertext. Note, the |
|---|
| 59 | | - * same IV (buffer) is also set with the aead_request_set_crypt call. Note, |
|---|
| 60 | | - * the API call of aead_request_set_ad must provide the length of the AAD and |
|---|
| 61 | | - * the IV. The API call of aead_request_set_crypt only points to the size of |
|---|
| 62 | | - * the input plaintext or ciphertext. |
|---|
| 53 | + * In-place encryption/decryption is enabled by using the same scatterlist |
|---|
| 54 | + * pointer for both the source and destination. |
|---|
| 63 | 55 | * |
|---|
| 64 | | - * * for "normal" AEAD ciphers, the concatenation of |
|---|
| 65 | | - * associated authentication data || plaintext or ciphertext. |
|---|
| 56 | + * Even in the out-of-place case, space must be reserved in the destination for |
|---|
| 57 | + * the associated data, even though it won't be written to. This makes the |
|---|
| 58 | + * in-place and out-of-place cases more consistent. It is permissible for the |
|---|
| 59 | + * "destination" associated data to alias the "source" associated data. |
|---|
| 66 | 60 | * |
|---|
| 67 | | - * It is important to note that if multiple scatter gather list entries form |
|---|
| 68 | | - * the input data mentioned above, the first entry must not point to a NULL |
|---|
| 69 | | - * buffer. If there is any potential where the AAD buffer can be NULL, the |
|---|
| 70 | | - * calling code must contain a precaution to ensure that this does not result |
|---|
| 71 | | - * in the first scatter gather list entry pointing to a NULL buffer. |
|---|
| 61 | + * As with the other scatterlist crypto APIs, zero-length scatterlist elements |
|---|
| 62 | + * are not allowed in the used part of the scatterlist. Thus, if there is no |
|---|
| 63 | + * associated data, the first element must point to the plaintext/ciphertext. |
|---|
| 64 | + * |
|---|
| 65 | + * To meet the needs of IPsec, a special quirk applies to rfc4106, rfc4309, |
|---|
| 66 | + * rfc4543, and rfc7539esp ciphers. For these ciphers, the final 'ivsize' bytes |
|---|
| 67 | + * of the associated data buffer must contain a second copy of the IV. This is |
|---|
| 68 | + * in addition to the copy passed to aead_request_set_crypt(). These two IV |
|---|
| 69 | + * copies must not differ; different implementations of the same algorithm may |
|---|
| 70 | + * behave differently in that case. Note that the algorithm might not actually |
|---|
| 71 | + * treat the IV as associated data; nevertheless the length passed to |
|---|
| 72 | + * aead_request_set_ad() must include it. |
|---|
| 72 | 73 | */ |
|---|
| 73 | 74 | |
|---|
| 74 | 75 | struct crypto_aead; |
|---|
| .. | .. |
|---|
| 115 | 116 | * @setkey: see struct skcipher_alg |
|---|
| 116 | 117 | * @encrypt: see struct skcipher_alg |
|---|
| 117 | 118 | * @decrypt: see struct skcipher_alg |
|---|
| 118 | | - * @geniv: see struct skcipher_alg |
|---|
| 119 | 119 | * @ivsize: see struct skcipher_alg |
|---|
| 120 | 120 | * @chunksize: see struct skcipher_alg |
|---|
| 121 | 121 | * @init: Initialize the cryptographic transformation object. This function |
|---|
| .. | .. |
|---|
| 141 | 141 | int (*decrypt)(struct aead_request *req); |
|---|
| 142 | 142 | int (*init)(struct crypto_aead *tfm); |
|---|
| 143 | 143 | void (*exit)(struct crypto_aead *tfm); |
|---|
| 144 | | - |
|---|
| 145 | | - const char *geniv; |
|---|
| 146 | 144 | |
|---|
| 147 | 145 | unsigned int ivsize; |
|---|
| 148 | 146 | unsigned int maxauthsize; |
|---|
| .. | .. |
|---|
| 237 | 235 | return tfm->authsize; |
|---|
| 238 | 236 | } |
|---|
| 239 | 237 | |
|---|
| 238 | +static inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg) |
|---|
| 239 | +{ |
|---|
| 240 | + return alg->maxauthsize; |
|---|
| 241 | +} |
|---|
| 242 | + |
|---|
| 243 | +static inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead) |
|---|
| 244 | +{ |
|---|
| 245 | + return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead)); |
|---|
| 246 | +} |
|---|
| 247 | + |
|---|
| 240 | 248 | /** |
|---|
| 241 | 249 | * crypto_aead_blocksize() - obtain block size of cipher |
|---|
| 242 | 250 | * @tfm: cipher handle |
|---|
| .. | .. |
|---|
| 327 | 335 | * |
|---|
| 328 | 336 | * Return: 0 if the cipher operation was successful; < 0 if an error occurred |
|---|
| 329 | 337 | */ |
|---|
| 330 | | -static inline int crypto_aead_encrypt(struct aead_request *req) |
|---|
| 331 | | -{ |
|---|
| 332 | | - struct crypto_aead *aead = crypto_aead_reqtfm(req); |
|---|
| 333 | | - |
|---|
| 334 | | - if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY) |
|---|
| 335 | | - return -ENOKEY; |
|---|
| 336 | | - |
|---|
| 337 | | - return crypto_aead_alg(aead)->encrypt(req); |
|---|
| 338 | | -} |
|---|
| 338 | +int crypto_aead_encrypt(struct aead_request *req); |
|---|
| 339 | 339 | |
|---|
| 340 | 340 | /** |
|---|
| 341 | 341 | * crypto_aead_decrypt() - decrypt ciphertext |
|---|
| 342 | | - * @req: reference to the ablkcipher_request handle that holds all information |
|---|
| 342 | + * @req: reference to the aead_request handle that holds all information |
|---|
| 343 | 343 | * needed to perform the cipher operation |
|---|
| 344 | 344 | * |
|---|
| 345 | 345 | * Decrypt ciphertext data using the aead_request handle. That data structure |
|---|
| .. | .. |
|---|
| 359 | 359 | * integrity of the ciphertext or the associated data was violated); |
|---|
| 360 | 360 | * < 0 if an error occurred. |
|---|
| 361 | 361 | */ |
|---|
| 362 | | -static inline int crypto_aead_decrypt(struct aead_request *req) |
|---|
| 363 | | -{ |
|---|
| 364 | | - struct crypto_aead *aead = crypto_aead_reqtfm(req); |
|---|
| 365 | | - |
|---|
| 366 | | - if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY) |
|---|
| 367 | | - return -ENOKEY; |
|---|
| 368 | | - |
|---|
| 369 | | - if (req->cryptlen < crypto_aead_authsize(aead)) |
|---|
| 370 | | - return -EINVAL; |
|---|
| 371 | | - |
|---|
| 372 | | - return crypto_aead_alg(aead)->decrypt(req); |
|---|
| 373 | | -} |
|---|
| 362 | +int crypto_aead_decrypt(struct aead_request *req); |
|---|
| 374 | 363 | |
|---|
| 375 | 364 | /** |
|---|
| 376 | 365 | * DOC: Asynchronous AEAD Request Handle |
|---|
| .. | .. |
|---|
| 438 | 427 | */ |
|---|
| 439 | 428 | static inline void aead_request_free(struct aead_request *req) |
|---|
| 440 | 429 | { |
|---|
| 441 | | - kzfree(req); |
|---|
| 430 | + kfree_sensitive(req); |
|---|
| 442 | 431 | } |
|---|
| 443 | 432 | |
|---|
| 444 | 433 | /** |
|---|