/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* AEAD: Authenticated Encryption with Associated Data3*4* Copyright (c) 2007-2015 Herbert Xu <[email protected]>5*/67#ifndef _CRYPTO_AEAD_H8#define _CRYPTO_AEAD_H910#include <linux/atomic.h>11#include <linux/container_of.h>12#include <linux/crypto.h>13#include <linux/slab.h>14#include <linux/types.h>1516/**17* DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API18*19* The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD20* (listed as type "aead" in /proc/crypto)21*22* The most prominent examples for this type of encryption is GCM and CCM.23* However, the kernel supports other types of AEAD ciphers which are defined24* with the following cipher string:25*26* authenc(keyed message digest, block cipher)27*28* For example: authenc(hmac(sha256), cbc(aes))29*30* The example code provided for the symmetric key cipher operation applies31* here as well. Naturally all *skcipher* symbols must be exchanged the *aead*32* pendants discussed in the following. In addition, for the AEAD operation,33* the aead_request_set_ad function must be used to set the pointer to the34* associated data memory location before performing the encryption or35* decryption operation. Another deviation from the asynchronous block cipher36* operation is that the caller should explicitly check for -EBADMSG of the37* crypto_aead_decrypt. That error indicates an authentication error, i.e.38* a breach in the integrity of the message. In essence, that -EBADMSG error39* code is the key bonus an AEAD cipher has over "standard" block chaining40* modes.41*42* Memory Structure:43*44* The source scatterlist must contain the concatenation of45* associated data || plaintext or ciphertext.46*47* The destination scatterlist has the same layout, except that the plaintext48* (resp. ciphertext) will grow (resp. shrink) by the authentication tag size49* during encryption (resp. decryption). The authentication tag is generated50* during the encryption operation and appended to the ciphertext. During51* decryption, the authentication tag is consumed along with the ciphertext and52* used to verify the integrity of the plaintext and the associated data.53*54* In-place encryption/decryption is enabled by using the same scatterlist55* pointer for both the source and destination.56*57* Even in the out-of-place case, space must be reserved in the destination for58* the associated data, even though it won't be written to. This makes the59* in-place and out-of-place cases more consistent. It is permissible for the60* "destination" associated data to alias the "source" associated data.61*62* As with the other scatterlist crypto APIs, zero-length scatterlist elements63* are not allowed in the used part of the scatterlist. Thus, if there is no64* associated data, the first element must point to the plaintext/ciphertext.65*66* To meet the needs of IPsec, a special quirk applies to rfc4106, rfc4309,67* rfc4543, and rfc7539esp ciphers. For these ciphers, the final 'ivsize' bytes68* of the associated data buffer must contain a second copy of the IV. This is69* in addition to the copy passed to aead_request_set_crypt(). These two IV70* copies must not differ; different implementations of the same algorithm may71* behave differently in that case. Note that the algorithm might not actually72* treat the IV as associated data; nevertheless the length passed to73* aead_request_set_ad() must include it.74*/7576struct crypto_aead;77struct scatterlist;7879/**80* struct aead_request - AEAD request81* @base: Common attributes for async crypto requests82* @assoclen: Length in bytes of associated data for authentication83* @cryptlen: Length of data to be encrypted or decrypted84* @iv: Initialisation vector85* @src: Source data86* @dst: Destination data87* @__ctx: Start of private context data88*/89struct aead_request {90struct crypto_async_request base;9192unsigned int assoclen;93unsigned int cryptlen;9495u8 *iv;9697struct scatterlist *src;98struct scatterlist *dst;99100void *__ctx[] CRYPTO_MINALIGN_ATTR;101};102103/**104* struct aead_alg - AEAD cipher definition105* @maxauthsize: Set the maximum authentication tag size supported by the106* transformation. A transformation may support smaller tag sizes.107* As the authentication tag is a message digest to ensure the108* integrity of the encrypted data, a consumer typically wants the109* largest authentication tag possible as defined by this110* variable.111* @setauthsize: Set authentication size for the AEAD transformation. This112* function is used to specify the consumer requested size of the113* authentication tag to be either generated by the transformation114* during encryption or the size of the authentication tag to be115* supplied during the decryption operation. This function is also116* responsible for checking the authentication tag size for117* validity.118* @setkey: see struct skcipher_alg119* @encrypt: see struct skcipher_alg120* @decrypt: see struct skcipher_alg121* @ivsize: see struct skcipher_alg122* @chunksize: see struct skcipher_alg123* @init: Initialize the cryptographic transformation object. This function124* is used to initialize the cryptographic transformation object.125* This function is called only once at the instantiation time, right126* after the transformation context was allocated. In case the127* cryptographic hardware has some special requirements which need to128* be handled by software, this function shall check for the precise129* requirement of the transformation and put any software fallbacks130* in place.131* @exit: Deinitialize the cryptographic transformation object. This is a132* counterpart to @init, used to remove various changes set in133* @init.134* @base: Definition of a generic crypto cipher algorithm.135*136* All fields except @ivsize is mandatory and must be filled.137*/138struct aead_alg {139int (*setkey)(struct crypto_aead *tfm, const u8 *key,140unsigned int keylen);141int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);142int (*encrypt)(struct aead_request *req);143int (*decrypt)(struct aead_request *req);144int (*init)(struct crypto_aead *tfm);145void (*exit)(struct crypto_aead *tfm);146147unsigned int ivsize;148unsigned int maxauthsize;149unsigned int chunksize;150151struct crypto_alg base;152};153154struct crypto_aead {155unsigned int authsize;156unsigned int reqsize;157158struct crypto_tfm base;159};160161struct crypto_sync_aead {162struct crypto_aead base;163};164165#define MAX_SYNC_AEAD_REQSIZE 384166167#define SYNC_AEAD_REQUEST_ON_STACK(name, _tfm) \168char __##name##_desc[sizeof(struct aead_request) + \169MAX_SYNC_AEAD_REQSIZE \170] CRYPTO_MINALIGN_ATTR; \171struct aead_request *name = \172(((struct aead_request *)__##name##_desc)->base.tfm = \173crypto_sync_aead_tfm((_tfm)), \174(void *)__##name##_desc)175176static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)177{178return container_of(tfm, struct crypto_aead, base);179}180181/**182* crypto_alloc_aead() - allocate AEAD cipher handle183* @alg_name: is the cra_name / name or cra_driver_name / driver name of the184* AEAD cipher185* @type: specifies the type of the cipher186* @mask: specifies the mask for the cipher187*188* Allocate a cipher handle for an AEAD. The returned struct189* crypto_aead is the cipher handle that is required for any subsequent190* API invocation for that AEAD.191*192* Return: allocated cipher handle in case of success; IS_ERR() is true in case193* of an error, PTR_ERR() returns the error code.194*/195struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);196197struct crypto_sync_aead *crypto_alloc_sync_aead(const char *alg_name, u32 type, u32 mask);198199static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)200{201return &tfm->base;202}203204static inline struct crypto_tfm *crypto_sync_aead_tfm(struct crypto_sync_aead *tfm)205{206return crypto_aead_tfm(&tfm->base);207}208209/**210* crypto_free_aead() - zeroize and free aead handle211* @tfm: cipher handle to be freed212*213* If @tfm is a NULL or error pointer, this function does nothing.214*/215static inline void crypto_free_aead(struct crypto_aead *tfm)216{217crypto_destroy_tfm(tfm, crypto_aead_tfm(tfm));218}219220static inline void crypto_free_sync_aead(struct crypto_sync_aead *tfm)221{222crypto_free_aead(&tfm->base);223}224225/**226* crypto_has_aead() - Search for the availability of an aead.227* @alg_name: is the cra_name / name or cra_driver_name / driver name of the228* aead229* @type: specifies the type of the aead230* @mask: specifies the mask for the aead231*232* Return: true when the aead is known to the kernel crypto API; false233* otherwise234*/235int crypto_has_aead(const char *alg_name, u32 type, u32 mask);236237static inline const char *crypto_aead_driver_name(struct crypto_aead *tfm)238{239return crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));240}241242static inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm)243{244return container_of(crypto_aead_tfm(tfm)->__crt_alg,245struct aead_alg, base);246}247248static inline unsigned int crypto_aead_alg_ivsize(struct aead_alg *alg)249{250return alg->ivsize;251}252253/**254* crypto_aead_ivsize() - obtain IV size255* @tfm: cipher handle256*257* The size of the IV for the aead referenced by the cipher handle is258* returned. This IV size may be zero if the cipher does not need an IV.259*260* Return: IV size in bytes261*/262static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)263{264return crypto_aead_alg_ivsize(crypto_aead_alg(tfm));265}266267static inline unsigned int crypto_sync_aead_ivsize(struct crypto_sync_aead *tfm)268{269return crypto_aead_ivsize(&tfm->base);270}271272/**273* crypto_aead_authsize() - obtain maximum authentication data size274* @tfm: cipher handle275*276* The maximum size of the authentication data for the AEAD cipher referenced277* by the AEAD cipher handle is returned. The authentication data size may be278* zero if the cipher implements a hard-coded maximum.279*280* The authentication data may also be known as "tag value".281*282* Return: authentication data size / tag size in bytes283*/284static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)285{286return tfm->authsize;287}288289static inline unsigned int crypto_sync_aead_authsize(struct crypto_sync_aead *tfm)290{291return crypto_aead_authsize(&tfm->base);292}293294static inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg)295{296return alg->maxauthsize;297}298299static inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead)300{301return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead));302}303304static inline unsigned int crypto_sync_aead_maxauthsize(struct crypto_sync_aead *tfm)305{306return crypto_aead_maxauthsize(&tfm->base);307}308309/**310* crypto_aead_blocksize() - obtain block size of cipher311* @tfm: cipher handle312*313* The block size for the AEAD referenced with the cipher handle is returned.314* The caller may use that information to allocate appropriate memory for the315* data returned by the encryption or decryption operation316*317* Return: block size of cipher318*/319static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)320{321return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));322}323324static inline unsigned int crypto_sync_aead_blocksize(struct crypto_sync_aead *tfm)325{326return crypto_aead_blocksize(&tfm->base);327}328329static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)330{331return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));332}333334static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)335{336return crypto_tfm_get_flags(crypto_aead_tfm(tfm));337}338339static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)340{341crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);342}343344static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)345{346crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);347}348349static inline u32 crypto_sync_aead_get_flags(struct crypto_sync_aead *tfm)350{351return crypto_aead_get_flags(&tfm->base);352}353354static inline void crypto_sync_aead_set_flags(struct crypto_sync_aead *tfm, u32 flags)355{356crypto_aead_set_flags(&tfm->base, flags);357}358359static inline void crypto_sync_aead_clear_flags(struct crypto_sync_aead *tfm, u32 flags)360{361crypto_aead_clear_flags(&tfm->base, flags);362}363364/**365* crypto_aead_setkey() - set key for cipher366* @tfm: cipher handle367* @key: buffer holding the key368* @keylen: length of the key in bytes369*370* The caller provided key is set for the AEAD referenced by the cipher371* handle.372*373* Note, the key length determines the cipher type. Many block ciphers implement374* different cipher modes depending on the key size, such as AES-128 vs AES-192375* vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128376* is performed.377*378* Return: 0 if the setting of the key was successful; < 0 if an error occurred379*/380int crypto_aead_setkey(struct crypto_aead *tfm,381const u8 *key, unsigned int keylen);382383static inline int crypto_sync_aead_setkey(struct crypto_sync_aead *tfm,384const u8 *key, unsigned int keylen)385{386return crypto_aead_setkey(&tfm->base, key, keylen);387}388389/**390* crypto_aead_setauthsize() - set authentication data size391* @tfm: cipher handle392* @authsize: size of the authentication data / tag in bytes393*394* Set the authentication data size / tag size. AEAD requires an authentication395* tag (or MAC) in addition to the associated data.396*397* Return: 0 if the setting of the key was successful; < 0 if an error occurred398*/399int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);400401static inline int crypto_sync_aead_setauthsize(struct crypto_sync_aead *tfm,402unsigned int authsize)403{404return crypto_aead_setauthsize(&tfm->base, authsize);405}406407static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)408{409return __crypto_aead_cast(req->base.tfm);410}411412static inline struct crypto_sync_aead *crypto_sync_aead_reqtfm(struct aead_request *req)413{414struct crypto_aead *tfm = crypto_aead_reqtfm(req);415416return container_of(tfm, struct crypto_sync_aead, base);417}418419/**420* crypto_aead_encrypt() - encrypt plaintext421* @req: reference to the aead_request handle that holds all information422* needed to perform the cipher operation423*424* Encrypt plaintext data using the aead_request handle. That data structure425* and how it is filled with data is discussed with the aead_request_*426* functions.427*428* IMPORTANT NOTE The encryption operation creates the authentication data /429* tag. That data is concatenated with the created ciphertext.430* The ciphertext memory size is therefore the given number of431* block cipher blocks + the size defined by the432* crypto_aead_setauthsize invocation. The caller must ensure433* that sufficient memory is available for the ciphertext and434* the authentication tag.435*436* Return: 0 if the cipher operation was successful; < 0 if an error occurred437*/438int crypto_aead_encrypt(struct aead_request *req);439440/**441* crypto_aead_decrypt() - decrypt ciphertext442* @req: reference to the aead_request handle that holds all information443* needed to perform the cipher operation444*445* Decrypt ciphertext data using the aead_request handle. That data structure446* and how it is filled with data is discussed with the aead_request_*447* functions.448*449* IMPORTANT NOTE The caller must concatenate the ciphertext followed by the450* authentication data / tag. That authentication data / tag451* must have the size defined by the crypto_aead_setauthsize452* invocation.453*454*455* Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD456* cipher operation performs the authentication of the data during the457* decryption operation. Therefore, the function returns this error if458* the authentication of the ciphertext was unsuccessful (i.e. the459* integrity of the ciphertext or the associated data was violated);460* < 0 if an error occurred.461*/462int crypto_aead_decrypt(struct aead_request *req);463464/**465* DOC: Asynchronous AEAD Request Handle466*467* The aead_request data structure contains all pointers to data required for468* the AEAD cipher operation. This includes the cipher handle (which can be469* used by multiple aead_request instances), pointer to plaintext and470* ciphertext, asynchronous callback function, etc. It acts as a handle to the471* aead_request_* API calls in a similar way as AEAD handle to the472* crypto_aead_* API calls.473*/474475/**476* crypto_aead_reqsize() - obtain size of the request data structure477* @tfm: cipher handle478*479* Return: number of bytes480*/481static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)482{483return tfm->reqsize;484}485486/**487* aead_request_set_tfm() - update cipher handle reference in request488* @req: request handle to be modified489* @tfm: cipher handle that shall be added to the request handle490*491* Allow the caller to replace the existing aead handle in the request492* data structure with a different one.493*/494static inline void aead_request_set_tfm(struct aead_request *req,495struct crypto_aead *tfm)496{497req->base.tfm = crypto_aead_tfm(tfm);498}499500static inline void aead_request_set_sync_tfm(struct aead_request *req,501struct crypto_sync_aead *tfm)502{503aead_request_set_tfm(req, &tfm->base);504}505506/**507* aead_request_alloc() - allocate request data structure508* @tfm: cipher handle to be registered with the request509* @gfp: memory allocation flag that is handed to kmalloc by the API call.510*511* Allocate the request data structure that must be used with the AEAD512* encrypt and decrypt API calls. During the allocation, the provided aead513* handle is registered in the request data structure.514*515* Return: allocated request handle in case of success, or NULL if out of memory516*/517static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,518gfp_t gfp)519{520struct aead_request *req;521522req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);523524if (likely(req))525aead_request_set_tfm(req, tfm);526527return req;528}529530/**531* aead_request_free() - zeroize and free request data structure532* @req: request data structure cipher handle to be freed533*/534static inline void aead_request_free(struct aead_request *req)535{536kfree_sensitive(req);537}538539/**540* aead_request_set_callback() - set asynchronous callback function541* @req: request handle542* @flags: specify zero or an ORing of the flags543* CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and544* increase the wait queue beyond the initial maximum size;545* CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep546* @compl: callback function pointer to be registered with the request handle547* @data: The data pointer refers to memory that is not used by the kernel548* crypto API, but provided to the callback function for it to use. Here,549* the caller can provide a reference to memory the callback function can550* operate on. As the callback function is invoked asynchronously to the551* related functionality, it may need to access data structures of the552* related functionality which can be referenced using this pointer. The553* callback function can access the memory via the "data" field in the554* crypto_async_request data structure provided to the callback function.555*556* Setting the callback function that is triggered once the cipher operation557* completes558*559* The callback function is registered with the aead_request handle and560* must comply with the following template::561*562* void callback_function(struct crypto_async_request *req, int error)563*/564static inline void aead_request_set_callback(struct aead_request *req,565u32 flags,566crypto_completion_t compl,567void *data)568{569req->base.complete = compl;570req->base.data = data;571req->base.flags = flags;572}573574/**575* aead_request_set_crypt - set data buffers576* @req: request handle577* @src: source scatter / gather list578* @dst: destination scatter / gather list579* @cryptlen: number of bytes to process from @src580* @iv: IV for the cipher operation which must comply with the IV size defined581* by crypto_aead_ivsize()582*583* Setting the source data and destination data scatter / gather lists which584* hold the associated data concatenated with the plaintext or ciphertext. See585* below for the authentication tag.586*587* For encryption, the source is treated as the plaintext and the588* destination is the ciphertext. For a decryption operation, the use is589* reversed - the source is the ciphertext and the destination is the plaintext.590*591* The memory structure for cipher operation has the following structure:592*593* - AEAD encryption input: assoc data || plaintext594* - AEAD encryption output: assoc data || ciphertext || auth tag595* - AEAD decryption input: assoc data || ciphertext || auth tag596* - AEAD decryption output: assoc data || plaintext597*598* Albeit the kernel requires the presence of the AAD buffer, however,599* the kernel does not fill the AAD buffer in the output case. If the600* caller wants to have that data buffer filled, the caller must either601* use an in-place cipher operation (i.e. same memory location for602* input/output memory location).603*/604static inline void aead_request_set_crypt(struct aead_request *req,605struct scatterlist *src,606struct scatterlist *dst,607unsigned int cryptlen, u8 *iv)608{609req->src = src;610req->dst = dst;611req->cryptlen = cryptlen;612req->iv = iv;613}614615/**616* aead_request_set_ad - set associated data information617* @req: request handle618* @assoclen: number of bytes in associated data619*620* Setting the AD information. This function sets the length of621* the associated data.622*/623static inline void aead_request_set_ad(struct aead_request *req,624unsigned int assoclen)625{626req->assoclen = assoclen;627}628629#endif /* _CRYPTO_AEAD_H */630631632