/* 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};160161static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)162{163return container_of(tfm, struct crypto_aead, base);164}165166/**167* crypto_alloc_aead() - allocate AEAD cipher handle168* @alg_name: is the cra_name / name or cra_driver_name / driver name of the169* AEAD cipher170* @type: specifies the type of the cipher171* @mask: specifies the mask for the cipher172*173* Allocate a cipher handle for an AEAD. The returned struct174* crypto_aead is the cipher handle that is required for any subsequent175* API invocation for that AEAD.176*177* Return: allocated cipher handle in case of success; IS_ERR() is true in case178* of an error, PTR_ERR() returns the error code.179*/180struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);181182static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)183{184return &tfm->base;185}186187/**188* crypto_free_aead() - zeroize and free aead handle189* @tfm: cipher handle to be freed190*191* If @tfm is a NULL or error pointer, this function does nothing.192*/193static inline void crypto_free_aead(struct crypto_aead *tfm)194{195crypto_destroy_tfm(tfm, crypto_aead_tfm(tfm));196}197198/**199* crypto_has_aead() - Search for the availability of an aead.200* @alg_name: is the cra_name / name or cra_driver_name / driver name of the201* aead202* @type: specifies the type of the aead203* @mask: specifies the mask for the aead204*205* Return: true when the aead is known to the kernel crypto API; false206* otherwise207*/208int crypto_has_aead(const char *alg_name, u32 type, u32 mask);209210static inline const char *crypto_aead_driver_name(struct crypto_aead *tfm)211{212return crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));213}214215static inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm)216{217return container_of(crypto_aead_tfm(tfm)->__crt_alg,218struct aead_alg, base);219}220221static inline unsigned int crypto_aead_alg_ivsize(struct aead_alg *alg)222{223return alg->ivsize;224}225226/**227* crypto_aead_ivsize() - obtain IV size228* @tfm: cipher handle229*230* The size of the IV for the aead referenced by the cipher handle is231* returned. This IV size may be zero if the cipher does not need an IV.232*233* Return: IV size in bytes234*/235static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)236{237return crypto_aead_alg_ivsize(crypto_aead_alg(tfm));238}239240/**241* crypto_aead_authsize() - obtain maximum authentication data size242* @tfm: cipher handle243*244* The maximum size of the authentication data for the AEAD cipher referenced245* by the AEAD cipher handle is returned. The authentication data size may be246* zero if the cipher implements a hard-coded maximum.247*248* The authentication data may also be known as "tag value".249*250* Return: authentication data size / tag size in bytes251*/252static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)253{254return tfm->authsize;255}256257static inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg)258{259return alg->maxauthsize;260}261262static inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead)263{264return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead));265}266267/**268* crypto_aead_blocksize() - obtain block size of cipher269* @tfm: cipher handle270*271* The block size for the AEAD referenced with the cipher handle is returned.272* The caller may use that information to allocate appropriate memory for the273* data returned by the encryption or decryption operation274*275* Return: block size of cipher276*/277static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)278{279return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));280}281282static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)283{284return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));285}286287static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)288{289return crypto_tfm_get_flags(crypto_aead_tfm(tfm));290}291292static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)293{294crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);295}296297static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)298{299crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);300}301302/**303* crypto_aead_setkey() - set key for cipher304* @tfm: cipher handle305* @key: buffer holding the key306* @keylen: length of the key in bytes307*308* The caller provided key is set for the AEAD referenced by the cipher309* handle.310*311* Note, the key length determines the cipher type. Many block ciphers implement312* different cipher modes depending on the key size, such as AES-128 vs AES-192313* vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128314* is performed.315*316* Return: 0 if the setting of the key was successful; < 0 if an error occurred317*/318int crypto_aead_setkey(struct crypto_aead *tfm,319const u8 *key, unsigned int keylen);320321/**322* crypto_aead_setauthsize() - set authentication data size323* @tfm: cipher handle324* @authsize: size of the authentication data / tag in bytes325*326* Set the authentication data size / tag size. AEAD requires an authentication327* tag (or MAC) in addition to the associated data.328*329* Return: 0 if the setting of the key was successful; < 0 if an error occurred330*/331int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);332333static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)334{335return __crypto_aead_cast(req->base.tfm);336}337338/**339* crypto_aead_encrypt() - encrypt plaintext340* @req: reference to the aead_request handle that holds all information341* needed to perform the cipher operation342*343* Encrypt plaintext data using the aead_request handle. That data structure344* and how it is filled with data is discussed with the aead_request_*345* functions.346*347* IMPORTANT NOTE The encryption operation creates the authentication data /348* tag. That data is concatenated with the created ciphertext.349* The ciphertext memory size is therefore the given number of350* block cipher blocks + the size defined by the351* crypto_aead_setauthsize invocation. The caller must ensure352* that sufficient memory is available for the ciphertext and353* the authentication tag.354*355* Return: 0 if the cipher operation was successful; < 0 if an error occurred356*/357int crypto_aead_encrypt(struct aead_request *req);358359/**360* crypto_aead_decrypt() - decrypt ciphertext361* @req: reference to the aead_request handle that holds all information362* needed to perform the cipher operation363*364* Decrypt ciphertext data using the aead_request handle. That data structure365* and how it is filled with data is discussed with the aead_request_*366* functions.367*368* IMPORTANT NOTE The caller must concatenate the ciphertext followed by the369* authentication data / tag. That authentication data / tag370* must have the size defined by the crypto_aead_setauthsize371* invocation.372*373*374* Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD375* cipher operation performs the authentication of the data during the376* decryption operation. Therefore, the function returns this error if377* the authentication of the ciphertext was unsuccessful (i.e. the378* integrity of the ciphertext or the associated data was violated);379* < 0 if an error occurred.380*/381int crypto_aead_decrypt(struct aead_request *req);382383/**384* DOC: Asynchronous AEAD Request Handle385*386* The aead_request data structure contains all pointers to data required for387* the AEAD cipher operation. This includes the cipher handle (which can be388* used by multiple aead_request instances), pointer to plaintext and389* ciphertext, asynchronous callback function, etc. It acts as a handle to the390* aead_request_* API calls in a similar way as AEAD handle to the391* crypto_aead_* API calls.392*/393394/**395* crypto_aead_reqsize() - obtain size of the request data structure396* @tfm: cipher handle397*398* Return: number of bytes399*/400static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)401{402return tfm->reqsize;403}404405/**406* aead_request_set_tfm() - update cipher handle reference in request407* @req: request handle to be modified408* @tfm: cipher handle that shall be added to the request handle409*410* Allow the caller to replace the existing aead handle in the request411* data structure with a different one.412*/413static inline void aead_request_set_tfm(struct aead_request *req,414struct crypto_aead *tfm)415{416req->base.tfm = crypto_aead_tfm(tfm);417}418419/**420* aead_request_alloc() - allocate request data structure421* @tfm: cipher handle to be registered with the request422* @gfp: memory allocation flag that is handed to kmalloc by the API call.423*424* Allocate the request data structure that must be used with the AEAD425* encrypt and decrypt API calls. During the allocation, the provided aead426* handle is registered in the request data structure.427*428* Return: allocated request handle in case of success, or NULL if out of memory429*/430static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,431gfp_t gfp)432{433struct aead_request *req;434435req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);436437if (likely(req))438aead_request_set_tfm(req, tfm);439440return req;441}442443/**444* aead_request_free() - zeroize and free request data structure445* @req: request data structure cipher handle to be freed446*/447static inline void aead_request_free(struct aead_request *req)448{449kfree_sensitive(req);450}451452/**453* aead_request_set_callback() - set asynchronous callback function454* @req: request handle455* @flags: specify zero or an ORing of the flags456* CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and457* increase the wait queue beyond the initial maximum size;458* CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep459* @compl: callback function pointer to be registered with the request handle460* @data: The data pointer refers to memory that is not used by the kernel461* crypto API, but provided to the callback function for it to use. Here,462* the caller can provide a reference to memory the callback function can463* operate on. As the callback function is invoked asynchronously to the464* related functionality, it may need to access data structures of the465* related functionality which can be referenced using this pointer. The466* callback function can access the memory via the "data" field in the467* crypto_async_request data structure provided to the callback function.468*469* Setting the callback function that is triggered once the cipher operation470* completes471*472* The callback function is registered with the aead_request handle and473* must comply with the following template::474*475* void callback_function(struct crypto_async_request *req, int error)476*/477static inline void aead_request_set_callback(struct aead_request *req,478u32 flags,479crypto_completion_t compl,480void *data)481{482req->base.complete = compl;483req->base.data = data;484req->base.flags = flags;485}486487/**488* aead_request_set_crypt - set data buffers489* @req: request handle490* @src: source scatter / gather list491* @dst: destination scatter / gather list492* @cryptlen: number of bytes to process from @src493* @iv: IV for the cipher operation which must comply with the IV size defined494* by crypto_aead_ivsize()495*496* Setting the source data and destination data scatter / gather lists which497* hold the associated data concatenated with the plaintext or ciphertext. See498* below for the authentication tag.499*500* For encryption, the source is treated as the plaintext and the501* destination is the ciphertext. For a decryption operation, the use is502* reversed - the source is the ciphertext and the destination is the plaintext.503*504* The memory structure for cipher operation has the following structure:505*506* - AEAD encryption input: assoc data || plaintext507* - AEAD encryption output: assoc data || ciphertext || auth tag508* - AEAD decryption input: assoc data || ciphertext || auth tag509* - AEAD decryption output: assoc data || plaintext510*511* Albeit the kernel requires the presence of the AAD buffer, however,512* the kernel does not fill the AAD buffer in the output case. If the513* caller wants to have that data buffer filled, the caller must either514* use an in-place cipher operation (i.e. same memory location for515* input/output memory location).516*/517static inline void aead_request_set_crypt(struct aead_request *req,518struct scatterlist *src,519struct scatterlist *dst,520unsigned int cryptlen, u8 *iv)521{522req->src = src;523req->dst = dst;524req->cryptlen = cryptlen;525req->iv = iv;526}527528/**529* aead_request_set_ad - set associated data information530* @req: request handle531* @assoclen: number of bytes in associated data532*533* Setting the AD information. This function sets the length of534* the associated data.535*/536static inline void aead_request_set_ad(struct aead_request *req,537unsigned int assoclen)538{539req->assoclen = assoclen;540}541542#endif /* _CRYPTO_AEAD_H */543544545