18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * AEAD: Authenticated Encryption with Associated Data 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#ifndef _CRYPTO_AEAD_H 98c2ecf20Sopenharmony_ci#define _CRYPTO_AEAD_H 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/crypto.h> 128c2ecf20Sopenharmony_ci#include <linux/kernel.h> 138c2ecf20Sopenharmony_ci#include <linux/slab.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci/** 168c2ecf20Sopenharmony_ci * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD 198c2ecf20Sopenharmony_ci * (listed as type "aead" in /proc/crypto) 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * The most prominent examples for this type of encryption is GCM and CCM. 228c2ecf20Sopenharmony_ci * However, the kernel supports other types of AEAD ciphers which are defined 238c2ecf20Sopenharmony_ci * with the following cipher string: 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci * authenc(keyed message digest, block cipher) 268c2ecf20Sopenharmony_ci * 278c2ecf20Sopenharmony_ci * For example: authenc(hmac(sha256), cbc(aes)) 288c2ecf20Sopenharmony_ci * 298c2ecf20Sopenharmony_ci * The example code provided for the symmetric key cipher operation 308c2ecf20Sopenharmony_ci * applies here as well. Naturally all *skcipher* symbols must be exchanged 318c2ecf20Sopenharmony_ci * the *aead* pendants discussed in the following. In addition, for the AEAD 328c2ecf20Sopenharmony_ci * operation, the aead_request_set_ad function must be used to set the 338c2ecf20Sopenharmony_ci * pointer to the associated data memory location before performing the 348c2ecf20Sopenharmony_ci * encryption or decryption operation. In case of an encryption, the associated 358c2ecf20Sopenharmony_ci * data memory is filled during the encryption operation. For decryption, the 368c2ecf20Sopenharmony_ci * associated data memory must contain data that is used to verify the integrity 378c2ecf20Sopenharmony_ci * of the decrypted data. Another deviation from the asynchronous block cipher 388c2ecf20Sopenharmony_ci * operation is that the caller should explicitly check for -EBADMSG of the 398c2ecf20Sopenharmony_ci * crypto_aead_decrypt. That error indicates an authentication error, i.e. 408c2ecf20Sopenharmony_ci * a breach in the integrity of the message. In essence, that -EBADMSG error 418c2ecf20Sopenharmony_ci * code is the key bonus an AEAD cipher has over "standard" block chaining 428c2ecf20Sopenharmony_ci * modes. 438c2ecf20Sopenharmony_ci * 448c2ecf20Sopenharmony_ci * Memory Structure: 458c2ecf20Sopenharmony_ci * 468c2ecf20Sopenharmony_ci * The source scatterlist must contain the concatenation of 478c2ecf20Sopenharmony_ci * associated data || plaintext or ciphertext. 488c2ecf20Sopenharmony_ci * 498c2ecf20Sopenharmony_ci * The destination scatterlist has the same layout, except that the plaintext 508c2ecf20Sopenharmony_ci * (resp. ciphertext) will grow (resp. shrink) by the authentication tag size 518c2ecf20Sopenharmony_ci * during encryption (resp. decryption). 528c2ecf20Sopenharmony_ci * 538c2ecf20Sopenharmony_ci * In-place encryption/decryption is enabled by using the same scatterlist 548c2ecf20Sopenharmony_ci * pointer for both the source and destination. 558c2ecf20Sopenharmony_ci * 568c2ecf20Sopenharmony_ci * Even in the out-of-place case, space must be reserved in the destination for 578c2ecf20Sopenharmony_ci * the associated data, even though it won't be written to. This makes the 588c2ecf20Sopenharmony_ci * in-place and out-of-place cases more consistent. It is permissible for the 598c2ecf20Sopenharmony_ci * "destination" associated data to alias the "source" associated data. 608c2ecf20Sopenharmony_ci * 618c2ecf20Sopenharmony_ci * As with the other scatterlist crypto APIs, zero-length scatterlist elements 628c2ecf20Sopenharmony_ci * are not allowed in the used part of the scatterlist. Thus, if there is no 638c2ecf20Sopenharmony_ci * associated data, the first element must point to the plaintext/ciphertext. 648c2ecf20Sopenharmony_ci * 658c2ecf20Sopenharmony_ci * To meet the needs of IPsec, a special quirk applies to rfc4106, rfc4309, 668c2ecf20Sopenharmony_ci * rfc4543, and rfc7539esp ciphers. For these ciphers, the final 'ivsize' bytes 678c2ecf20Sopenharmony_ci * of the associated data buffer must contain a second copy of the IV. This is 688c2ecf20Sopenharmony_ci * in addition to the copy passed to aead_request_set_crypt(). These two IV 698c2ecf20Sopenharmony_ci * copies must not differ; different implementations of the same algorithm may 708c2ecf20Sopenharmony_ci * behave differently in that case. Note that the algorithm might not actually 718c2ecf20Sopenharmony_ci * treat the IV as associated data; nevertheless the length passed to 728c2ecf20Sopenharmony_ci * aead_request_set_ad() must include it. 738c2ecf20Sopenharmony_ci */ 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_cistruct crypto_aead; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci/** 788c2ecf20Sopenharmony_ci * struct aead_request - AEAD request 798c2ecf20Sopenharmony_ci * @base: Common attributes for async crypto requests 808c2ecf20Sopenharmony_ci * @assoclen: Length in bytes of associated data for authentication 818c2ecf20Sopenharmony_ci * @cryptlen: Length of data to be encrypted or decrypted 828c2ecf20Sopenharmony_ci * @iv: Initialisation vector 838c2ecf20Sopenharmony_ci * @src: Source data 848c2ecf20Sopenharmony_ci * @dst: Destination data 858c2ecf20Sopenharmony_ci * @__ctx: Start of private context data 868c2ecf20Sopenharmony_ci */ 878c2ecf20Sopenharmony_cistruct aead_request { 888c2ecf20Sopenharmony_ci struct crypto_async_request base; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci unsigned int assoclen; 918c2ecf20Sopenharmony_ci unsigned int cryptlen; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci u8 *iv; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci struct scatterlist *src; 968c2ecf20Sopenharmony_ci struct scatterlist *dst; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci void *__ctx[] CRYPTO_MINALIGN_ATTR; 998c2ecf20Sopenharmony_ci}; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci/** 1028c2ecf20Sopenharmony_ci * struct aead_alg - AEAD cipher definition 1038c2ecf20Sopenharmony_ci * @maxauthsize: Set the maximum authentication tag size supported by the 1048c2ecf20Sopenharmony_ci * transformation. A transformation may support smaller tag sizes. 1058c2ecf20Sopenharmony_ci * As the authentication tag is a message digest to ensure the 1068c2ecf20Sopenharmony_ci * integrity of the encrypted data, a consumer typically wants the 1078c2ecf20Sopenharmony_ci * largest authentication tag possible as defined by this 1088c2ecf20Sopenharmony_ci * variable. 1098c2ecf20Sopenharmony_ci * @setauthsize: Set authentication size for the AEAD transformation. This 1108c2ecf20Sopenharmony_ci * function is used to specify the consumer requested size of the 1118c2ecf20Sopenharmony_ci * authentication tag to be either generated by the transformation 1128c2ecf20Sopenharmony_ci * during encryption or the size of the authentication tag to be 1138c2ecf20Sopenharmony_ci * supplied during the decryption operation. This function is also 1148c2ecf20Sopenharmony_ci * responsible for checking the authentication tag size for 1158c2ecf20Sopenharmony_ci * validity. 1168c2ecf20Sopenharmony_ci * @setkey: see struct skcipher_alg 1178c2ecf20Sopenharmony_ci * @encrypt: see struct skcipher_alg 1188c2ecf20Sopenharmony_ci * @decrypt: see struct skcipher_alg 1198c2ecf20Sopenharmony_ci * @ivsize: see struct skcipher_alg 1208c2ecf20Sopenharmony_ci * @chunksize: see struct skcipher_alg 1218c2ecf20Sopenharmony_ci * @init: Initialize the cryptographic transformation object. This function 1228c2ecf20Sopenharmony_ci * is used to initialize the cryptographic transformation object. 1238c2ecf20Sopenharmony_ci * This function is called only once at the instantiation time, right 1248c2ecf20Sopenharmony_ci * after the transformation context was allocated. In case the 1258c2ecf20Sopenharmony_ci * cryptographic hardware has some special requirements which need to 1268c2ecf20Sopenharmony_ci * be handled by software, this function shall check for the precise 1278c2ecf20Sopenharmony_ci * requirement of the transformation and put any software fallbacks 1288c2ecf20Sopenharmony_ci * in place. 1298c2ecf20Sopenharmony_ci * @exit: Deinitialize the cryptographic transformation object. This is a 1308c2ecf20Sopenharmony_ci * counterpart to @init, used to remove various changes set in 1318c2ecf20Sopenharmony_ci * @init. 1328c2ecf20Sopenharmony_ci * @base: Definition of a generic crypto cipher algorithm. 1338c2ecf20Sopenharmony_ci * 1348c2ecf20Sopenharmony_ci * All fields except @ivsize is mandatory and must be filled. 1358c2ecf20Sopenharmony_ci */ 1368c2ecf20Sopenharmony_cistruct aead_alg { 1378c2ecf20Sopenharmony_ci int (*setkey)(struct crypto_aead *tfm, const u8 *key, 1388c2ecf20Sopenharmony_ci unsigned int keylen); 1398c2ecf20Sopenharmony_ci int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize); 1408c2ecf20Sopenharmony_ci int (*encrypt)(struct aead_request *req); 1418c2ecf20Sopenharmony_ci int (*decrypt)(struct aead_request *req); 1428c2ecf20Sopenharmony_ci int (*init)(struct crypto_aead *tfm); 1438c2ecf20Sopenharmony_ci void (*exit)(struct crypto_aead *tfm); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci unsigned int ivsize; 1468c2ecf20Sopenharmony_ci unsigned int maxauthsize; 1478c2ecf20Sopenharmony_ci unsigned int chunksize; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci struct crypto_alg base; 1508c2ecf20Sopenharmony_ci}; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistruct crypto_aead { 1538c2ecf20Sopenharmony_ci unsigned int authsize; 1548c2ecf20Sopenharmony_ci unsigned int reqsize; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci struct crypto_tfm base; 1578c2ecf20Sopenharmony_ci}; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_cistatic inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci return container_of(tfm, struct crypto_aead, base); 1628c2ecf20Sopenharmony_ci} 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci/** 1658c2ecf20Sopenharmony_ci * crypto_alloc_aead() - allocate AEAD cipher handle 1668c2ecf20Sopenharmony_ci * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1678c2ecf20Sopenharmony_ci * AEAD cipher 1688c2ecf20Sopenharmony_ci * @type: specifies the type of the cipher 1698c2ecf20Sopenharmony_ci * @mask: specifies the mask for the cipher 1708c2ecf20Sopenharmony_ci * 1718c2ecf20Sopenharmony_ci * Allocate a cipher handle for an AEAD. The returned struct 1728c2ecf20Sopenharmony_ci * crypto_aead is the cipher handle that is required for any subsequent 1738c2ecf20Sopenharmony_ci * API invocation for that AEAD. 1748c2ecf20Sopenharmony_ci * 1758c2ecf20Sopenharmony_ci * Return: allocated cipher handle in case of success; IS_ERR() is true in case 1768c2ecf20Sopenharmony_ci * of an error, PTR_ERR() returns the error code. 1778c2ecf20Sopenharmony_ci */ 1788c2ecf20Sopenharmony_cistruct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask); 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_cistatic inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci return &tfm->base; 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci/** 1868c2ecf20Sopenharmony_ci * crypto_free_aead() - zeroize and free aead handle 1878c2ecf20Sopenharmony_ci * @tfm: cipher handle to be freed 1888c2ecf20Sopenharmony_ci * 1898c2ecf20Sopenharmony_ci * If @tfm is a NULL or error pointer, this function does nothing. 1908c2ecf20Sopenharmony_ci */ 1918c2ecf20Sopenharmony_cistatic inline void crypto_free_aead(struct crypto_aead *tfm) 1928c2ecf20Sopenharmony_ci{ 1938c2ecf20Sopenharmony_ci crypto_destroy_tfm(tfm, crypto_aead_tfm(tfm)); 1948c2ecf20Sopenharmony_ci} 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_cistatic inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm) 1978c2ecf20Sopenharmony_ci{ 1988c2ecf20Sopenharmony_ci return container_of(crypto_aead_tfm(tfm)->__crt_alg, 1998c2ecf20Sopenharmony_ci struct aead_alg, base); 2008c2ecf20Sopenharmony_ci} 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_cistatic inline unsigned int crypto_aead_alg_ivsize(struct aead_alg *alg) 2038c2ecf20Sopenharmony_ci{ 2048c2ecf20Sopenharmony_ci return alg->ivsize; 2058c2ecf20Sopenharmony_ci} 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci/** 2088c2ecf20Sopenharmony_ci * crypto_aead_ivsize() - obtain IV size 2098c2ecf20Sopenharmony_ci * @tfm: cipher handle 2108c2ecf20Sopenharmony_ci * 2118c2ecf20Sopenharmony_ci * The size of the IV for the aead referenced by the cipher handle is 2128c2ecf20Sopenharmony_ci * returned. This IV size may be zero if the cipher does not need an IV. 2138c2ecf20Sopenharmony_ci * 2148c2ecf20Sopenharmony_ci * Return: IV size in bytes 2158c2ecf20Sopenharmony_ci */ 2168c2ecf20Sopenharmony_cistatic inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm) 2178c2ecf20Sopenharmony_ci{ 2188c2ecf20Sopenharmony_ci return crypto_aead_alg_ivsize(crypto_aead_alg(tfm)); 2198c2ecf20Sopenharmony_ci} 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci/** 2228c2ecf20Sopenharmony_ci * crypto_aead_authsize() - obtain maximum authentication data size 2238c2ecf20Sopenharmony_ci * @tfm: cipher handle 2248c2ecf20Sopenharmony_ci * 2258c2ecf20Sopenharmony_ci * The maximum size of the authentication data for the AEAD cipher referenced 2268c2ecf20Sopenharmony_ci * by the AEAD cipher handle is returned. The authentication data size may be 2278c2ecf20Sopenharmony_ci * zero if the cipher implements a hard-coded maximum. 2288c2ecf20Sopenharmony_ci * 2298c2ecf20Sopenharmony_ci * The authentication data may also be known as "tag value". 2308c2ecf20Sopenharmony_ci * 2318c2ecf20Sopenharmony_ci * Return: authentication data size / tag size in bytes 2328c2ecf20Sopenharmony_ci */ 2338c2ecf20Sopenharmony_cistatic inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm) 2348c2ecf20Sopenharmony_ci{ 2358c2ecf20Sopenharmony_ci return tfm->authsize; 2368c2ecf20Sopenharmony_ci} 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_cistatic inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg) 2398c2ecf20Sopenharmony_ci{ 2408c2ecf20Sopenharmony_ci return alg->maxauthsize; 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_cistatic inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead) 2448c2ecf20Sopenharmony_ci{ 2458c2ecf20Sopenharmony_ci return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead)); 2468c2ecf20Sopenharmony_ci} 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci/** 2498c2ecf20Sopenharmony_ci * crypto_aead_blocksize() - obtain block size of cipher 2508c2ecf20Sopenharmony_ci * @tfm: cipher handle 2518c2ecf20Sopenharmony_ci * 2528c2ecf20Sopenharmony_ci * The block size for the AEAD referenced with the cipher handle is returned. 2538c2ecf20Sopenharmony_ci * The caller may use that information to allocate appropriate memory for the 2548c2ecf20Sopenharmony_ci * data returned by the encryption or decryption operation 2558c2ecf20Sopenharmony_ci * 2568c2ecf20Sopenharmony_ci * Return: block size of cipher 2578c2ecf20Sopenharmony_ci */ 2588c2ecf20Sopenharmony_cistatic inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm) 2598c2ecf20Sopenharmony_ci{ 2608c2ecf20Sopenharmony_ci return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm)); 2618c2ecf20Sopenharmony_ci} 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_cistatic inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm) 2648c2ecf20Sopenharmony_ci{ 2658c2ecf20Sopenharmony_ci return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm)); 2668c2ecf20Sopenharmony_ci} 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_cistatic inline u32 crypto_aead_get_flags(struct crypto_aead *tfm) 2698c2ecf20Sopenharmony_ci{ 2708c2ecf20Sopenharmony_ci return crypto_tfm_get_flags(crypto_aead_tfm(tfm)); 2718c2ecf20Sopenharmony_ci} 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_cistatic inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags) 2748c2ecf20Sopenharmony_ci{ 2758c2ecf20Sopenharmony_ci crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags); 2768c2ecf20Sopenharmony_ci} 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_cistatic inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags) 2798c2ecf20Sopenharmony_ci{ 2808c2ecf20Sopenharmony_ci crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags); 2818c2ecf20Sopenharmony_ci} 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci/** 2848c2ecf20Sopenharmony_ci * crypto_aead_setkey() - set key for cipher 2858c2ecf20Sopenharmony_ci * @tfm: cipher handle 2868c2ecf20Sopenharmony_ci * @key: buffer holding the key 2878c2ecf20Sopenharmony_ci * @keylen: length of the key in bytes 2888c2ecf20Sopenharmony_ci * 2898c2ecf20Sopenharmony_ci * The caller provided key is set for the AEAD referenced by the cipher 2908c2ecf20Sopenharmony_ci * handle. 2918c2ecf20Sopenharmony_ci * 2928c2ecf20Sopenharmony_ci * Note, the key length determines the cipher type. Many block ciphers implement 2938c2ecf20Sopenharmony_ci * different cipher modes depending on the key size, such as AES-128 vs AES-192 2948c2ecf20Sopenharmony_ci * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 2958c2ecf20Sopenharmony_ci * is performed. 2968c2ecf20Sopenharmony_ci * 2978c2ecf20Sopenharmony_ci * Return: 0 if the setting of the key was successful; < 0 if an error occurred 2988c2ecf20Sopenharmony_ci */ 2998c2ecf20Sopenharmony_ciint crypto_aead_setkey(struct crypto_aead *tfm, 3008c2ecf20Sopenharmony_ci const u8 *key, unsigned int keylen); 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci/** 3038c2ecf20Sopenharmony_ci * crypto_aead_setauthsize() - set authentication data size 3048c2ecf20Sopenharmony_ci * @tfm: cipher handle 3058c2ecf20Sopenharmony_ci * @authsize: size of the authentication data / tag in bytes 3068c2ecf20Sopenharmony_ci * 3078c2ecf20Sopenharmony_ci * Set the authentication data size / tag size. AEAD requires an authentication 3088c2ecf20Sopenharmony_ci * tag (or MAC) in addition to the associated data. 3098c2ecf20Sopenharmony_ci * 3108c2ecf20Sopenharmony_ci * Return: 0 if the setting of the key was successful; < 0 if an error occurred 3118c2ecf20Sopenharmony_ci */ 3128c2ecf20Sopenharmony_ciint crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize); 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_cistatic inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req) 3158c2ecf20Sopenharmony_ci{ 3168c2ecf20Sopenharmony_ci return __crypto_aead_cast(req->base.tfm); 3178c2ecf20Sopenharmony_ci} 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci/** 3208c2ecf20Sopenharmony_ci * crypto_aead_encrypt() - encrypt plaintext 3218c2ecf20Sopenharmony_ci * @req: reference to the aead_request handle that holds all information 3228c2ecf20Sopenharmony_ci * needed to perform the cipher operation 3238c2ecf20Sopenharmony_ci * 3248c2ecf20Sopenharmony_ci * Encrypt plaintext data using the aead_request handle. That data structure 3258c2ecf20Sopenharmony_ci * and how it is filled with data is discussed with the aead_request_* 3268c2ecf20Sopenharmony_ci * functions. 3278c2ecf20Sopenharmony_ci * 3288c2ecf20Sopenharmony_ci * IMPORTANT NOTE The encryption operation creates the authentication data / 3298c2ecf20Sopenharmony_ci * tag. That data is concatenated with the created ciphertext. 3308c2ecf20Sopenharmony_ci * The ciphertext memory size is therefore the given number of 3318c2ecf20Sopenharmony_ci * block cipher blocks + the size defined by the 3328c2ecf20Sopenharmony_ci * crypto_aead_setauthsize invocation. The caller must ensure 3338c2ecf20Sopenharmony_ci * that sufficient memory is available for the ciphertext and 3348c2ecf20Sopenharmony_ci * the authentication tag. 3358c2ecf20Sopenharmony_ci * 3368c2ecf20Sopenharmony_ci * Return: 0 if the cipher operation was successful; < 0 if an error occurred 3378c2ecf20Sopenharmony_ci */ 3388c2ecf20Sopenharmony_ciint crypto_aead_encrypt(struct aead_request *req); 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci/** 3418c2ecf20Sopenharmony_ci * crypto_aead_decrypt() - decrypt ciphertext 3428c2ecf20Sopenharmony_ci * @req: reference to the aead_request handle that holds all information 3438c2ecf20Sopenharmony_ci * needed to perform the cipher operation 3448c2ecf20Sopenharmony_ci * 3458c2ecf20Sopenharmony_ci * Decrypt ciphertext data using the aead_request handle. That data structure 3468c2ecf20Sopenharmony_ci * and how it is filled with data is discussed with the aead_request_* 3478c2ecf20Sopenharmony_ci * functions. 3488c2ecf20Sopenharmony_ci * 3498c2ecf20Sopenharmony_ci * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the 3508c2ecf20Sopenharmony_ci * authentication data / tag. That authentication data / tag 3518c2ecf20Sopenharmony_ci * must have the size defined by the crypto_aead_setauthsize 3528c2ecf20Sopenharmony_ci * invocation. 3538c2ecf20Sopenharmony_ci * 3548c2ecf20Sopenharmony_ci * 3558c2ecf20Sopenharmony_ci * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD 3568c2ecf20Sopenharmony_ci * cipher operation performs the authentication of the data during the 3578c2ecf20Sopenharmony_ci * decryption operation. Therefore, the function returns this error if 3588c2ecf20Sopenharmony_ci * the authentication of the ciphertext was unsuccessful (i.e. the 3598c2ecf20Sopenharmony_ci * integrity of the ciphertext or the associated data was violated); 3608c2ecf20Sopenharmony_ci * < 0 if an error occurred. 3618c2ecf20Sopenharmony_ci */ 3628c2ecf20Sopenharmony_ciint crypto_aead_decrypt(struct aead_request *req); 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci/** 3658c2ecf20Sopenharmony_ci * DOC: Asynchronous AEAD Request Handle 3668c2ecf20Sopenharmony_ci * 3678c2ecf20Sopenharmony_ci * The aead_request data structure contains all pointers to data required for 3688c2ecf20Sopenharmony_ci * the AEAD cipher operation. This includes the cipher handle (which can be 3698c2ecf20Sopenharmony_ci * used by multiple aead_request instances), pointer to plaintext and 3708c2ecf20Sopenharmony_ci * ciphertext, asynchronous callback function, etc. It acts as a handle to the 3718c2ecf20Sopenharmony_ci * aead_request_* API calls in a similar way as AEAD handle to the 3728c2ecf20Sopenharmony_ci * crypto_aead_* API calls. 3738c2ecf20Sopenharmony_ci */ 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci/** 3768c2ecf20Sopenharmony_ci * crypto_aead_reqsize() - obtain size of the request data structure 3778c2ecf20Sopenharmony_ci * @tfm: cipher handle 3788c2ecf20Sopenharmony_ci * 3798c2ecf20Sopenharmony_ci * Return: number of bytes 3808c2ecf20Sopenharmony_ci */ 3818c2ecf20Sopenharmony_cistatic inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm) 3828c2ecf20Sopenharmony_ci{ 3838c2ecf20Sopenharmony_ci return tfm->reqsize; 3848c2ecf20Sopenharmony_ci} 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci/** 3878c2ecf20Sopenharmony_ci * aead_request_set_tfm() - update cipher handle reference in request 3888c2ecf20Sopenharmony_ci * @req: request handle to be modified 3898c2ecf20Sopenharmony_ci * @tfm: cipher handle that shall be added to the request handle 3908c2ecf20Sopenharmony_ci * 3918c2ecf20Sopenharmony_ci * Allow the caller to replace the existing aead handle in the request 3928c2ecf20Sopenharmony_ci * data structure with a different one. 3938c2ecf20Sopenharmony_ci */ 3948c2ecf20Sopenharmony_cistatic inline void aead_request_set_tfm(struct aead_request *req, 3958c2ecf20Sopenharmony_ci struct crypto_aead *tfm) 3968c2ecf20Sopenharmony_ci{ 3978c2ecf20Sopenharmony_ci req->base.tfm = crypto_aead_tfm(tfm); 3988c2ecf20Sopenharmony_ci} 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci/** 4018c2ecf20Sopenharmony_ci * aead_request_alloc() - allocate request data structure 4028c2ecf20Sopenharmony_ci * @tfm: cipher handle to be registered with the request 4038c2ecf20Sopenharmony_ci * @gfp: memory allocation flag that is handed to kmalloc by the API call. 4048c2ecf20Sopenharmony_ci * 4058c2ecf20Sopenharmony_ci * Allocate the request data structure that must be used with the AEAD 4068c2ecf20Sopenharmony_ci * encrypt and decrypt API calls. During the allocation, the provided aead 4078c2ecf20Sopenharmony_ci * handle is registered in the request data structure. 4088c2ecf20Sopenharmony_ci * 4098c2ecf20Sopenharmony_ci * Return: allocated request handle in case of success, or NULL if out of memory 4108c2ecf20Sopenharmony_ci */ 4118c2ecf20Sopenharmony_cistatic inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm, 4128c2ecf20Sopenharmony_ci gfp_t gfp) 4138c2ecf20Sopenharmony_ci{ 4148c2ecf20Sopenharmony_ci struct aead_request *req; 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp); 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci if (likely(req)) 4198c2ecf20Sopenharmony_ci aead_request_set_tfm(req, tfm); 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci return req; 4228c2ecf20Sopenharmony_ci} 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci/** 4258c2ecf20Sopenharmony_ci * aead_request_free() - zeroize and free request data structure 4268c2ecf20Sopenharmony_ci * @req: request data structure cipher handle to be freed 4278c2ecf20Sopenharmony_ci */ 4288c2ecf20Sopenharmony_cistatic inline void aead_request_free(struct aead_request *req) 4298c2ecf20Sopenharmony_ci{ 4308c2ecf20Sopenharmony_ci kfree_sensitive(req); 4318c2ecf20Sopenharmony_ci} 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci/** 4348c2ecf20Sopenharmony_ci * aead_request_set_callback() - set asynchronous callback function 4358c2ecf20Sopenharmony_ci * @req: request handle 4368c2ecf20Sopenharmony_ci * @flags: specify zero or an ORing of the flags 4378c2ecf20Sopenharmony_ci * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and 4388c2ecf20Sopenharmony_ci * increase the wait queue beyond the initial maximum size; 4398c2ecf20Sopenharmony_ci * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep 4408c2ecf20Sopenharmony_ci * @compl: callback function pointer to be registered with the request handle 4418c2ecf20Sopenharmony_ci * @data: The data pointer refers to memory that is not used by the kernel 4428c2ecf20Sopenharmony_ci * crypto API, but provided to the callback function for it to use. Here, 4438c2ecf20Sopenharmony_ci * the caller can provide a reference to memory the callback function can 4448c2ecf20Sopenharmony_ci * operate on. As the callback function is invoked asynchronously to the 4458c2ecf20Sopenharmony_ci * related functionality, it may need to access data structures of the 4468c2ecf20Sopenharmony_ci * related functionality which can be referenced using this pointer. The 4478c2ecf20Sopenharmony_ci * callback function can access the memory via the "data" field in the 4488c2ecf20Sopenharmony_ci * crypto_async_request data structure provided to the callback function. 4498c2ecf20Sopenharmony_ci * 4508c2ecf20Sopenharmony_ci * Setting the callback function that is triggered once the cipher operation 4518c2ecf20Sopenharmony_ci * completes 4528c2ecf20Sopenharmony_ci * 4538c2ecf20Sopenharmony_ci * The callback function is registered with the aead_request handle and 4548c2ecf20Sopenharmony_ci * must comply with the following template:: 4558c2ecf20Sopenharmony_ci * 4568c2ecf20Sopenharmony_ci * void callback_function(struct crypto_async_request *req, int error) 4578c2ecf20Sopenharmony_ci */ 4588c2ecf20Sopenharmony_cistatic inline void aead_request_set_callback(struct aead_request *req, 4598c2ecf20Sopenharmony_ci u32 flags, 4608c2ecf20Sopenharmony_ci crypto_completion_t compl, 4618c2ecf20Sopenharmony_ci void *data) 4628c2ecf20Sopenharmony_ci{ 4638c2ecf20Sopenharmony_ci req->base.complete = compl; 4648c2ecf20Sopenharmony_ci req->base.data = data; 4658c2ecf20Sopenharmony_ci req->base.flags = flags; 4668c2ecf20Sopenharmony_ci} 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci/** 4698c2ecf20Sopenharmony_ci * aead_request_set_crypt - set data buffers 4708c2ecf20Sopenharmony_ci * @req: request handle 4718c2ecf20Sopenharmony_ci * @src: source scatter / gather list 4728c2ecf20Sopenharmony_ci * @dst: destination scatter / gather list 4738c2ecf20Sopenharmony_ci * @cryptlen: number of bytes to process from @src 4748c2ecf20Sopenharmony_ci * @iv: IV for the cipher operation which must comply with the IV size defined 4758c2ecf20Sopenharmony_ci * by crypto_aead_ivsize() 4768c2ecf20Sopenharmony_ci * 4778c2ecf20Sopenharmony_ci * Setting the source data and destination data scatter / gather lists which 4788c2ecf20Sopenharmony_ci * hold the associated data concatenated with the plaintext or ciphertext. See 4798c2ecf20Sopenharmony_ci * below for the authentication tag. 4808c2ecf20Sopenharmony_ci * 4818c2ecf20Sopenharmony_ci * For encryption, the source is treated as the plaintext and the 4828c2ecf20Sopenharmony_ci * destination is the ciphertext. For a decryption operation, the use is 4838c2ecf20Sopenharmony_ci * reversed - the source is the ciphertext and the destination is the plaintext. 4848c2ecf20Sopenharmony_ci * 4858c2ecf20Sopenharmony_ci * The memory structure for cipher operation has the following structure: 4868c2ecf20Sopenharmony_ci * 4878c2ecf20Sopenharmony_ci * - AEAD encryption input: assoc data || plaintext 4888c2ecf20Sopenharmony_ci * - AEAD encryption output: assoc data || cipherntext || auth tag 4898c2ecf20Sopenharmony_ci * - AEAD decryption input: assoc data || ciphertext || auth tag 4908c2ecf20Sopenharmony_ci * - AEAD decryption output: assoc data || plaintext 4918c2ecf20Sopenharmony_ci * 4928c2ecf20Sopenharmony_ci * Albeit the kernel requires the presence of the AAD buffer, however, 4938c2ecf20Sopenharmony_ci * the kernel does not fill the AAD buffer in the output case. If the 4948c2ecf20Sopenharmony_ci * caller wants to have that data buffer filled, the caller must either 4958c2ecf20Sopenharmony_ci * use an in-place cipher operation (i.e. same memory location for 4968c2ecf20Sopenharmony_ci * input/output memory location). 4978c2ecf20Sopenharmony_ci */ 4988c2ecf20Sopenharmony_cistatic inline void aead_request_set_crypt(struct aead_request *req, 4998c2ecf20Sopenharmony_ci struct scatterlist *src, 5008c2ecf20Sopenharmony_ci struct scatterlist *dst, 5018c2ecf20Sopenharmony_ci unsigned int cryptlen, u8 *iv) 5028c2ecf20Sopenharmony_ci{ 5038c2ecf20Sopenharmony_ci req->src = src; 5048c2ecf20Sopenharmony_ci req->dst = dst; 5058c2ecf20Sopenharmony_ci req->cryptlen = cryptlen; 5068c2ecf20Sopenharmony_ci req->iv = iv; 5078c2ecf20Sopenharmony_ci} 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci/** 5108c2ecf20Sopenharmony_ci * aead_request_set_ad - set associated data information 5118c2ecf20Sopenharmony_ci * @req: request handle 5128c2ecf20Sopenharmony_ci * @assoclen: number of bytes in associated data 5138c2ecf20Sopenharmony_ci * 5148c2ecf20Sopenharmony_ci * Setting the AD information. This function sets the length of 5158c2ecf20Sopenharmony_ci * the associated data. 5168c2ecf20Sopenharmony_ci */ 5178c2ecf20Sopenharmony_cistatic inline void aead_request_set_ad(struct aead_request *req, 5188c2ecf20Sopenharmony_ci unsigned int assoclen) 5198c2ecf20Sopenharmony_ci{ 5208c2ecf20Sopenharmony_ci req->assoclen = assoclen; 5218c2ecf20Sopenharmony_ci} 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci#endif /* _CRYPTO_AEAD_H */ 524