162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Symmetric key ciphers. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au> 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#ifndef _CRYPTO_SKCIPHER_H 962306a36Sopenharmony_ci#define _CRYPTO_SKCIPHER_H 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#include <linux/atomic.h> 1262306a36Sopenharmony_ci#include <linux/container_of.h> 1362306a36Sopenharmony_ci#include <linux/crypto.h> 1462306a36Sopenharmony_ci#include <linux/slab.h> 1562306a36Sopenharmony_ci#include <linux/string.h> 1662306a36Sopenharmony_ci#include <linux/types.h> 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_cistruct scatterlist; 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci/** 2162306a36Sopenharmony_ci * struct skcipher_request - Symmetric key cipher request 2262306a36Sopenharmony_ci * @cryptlen: Number of bytes to encrypt or decrypt 2362306a36Sopenharmony_ci * @iv: Initialisation Vector 2462306a36Sopenharmony_ci * @src: Source SG list 2562306a36Sopenharmony_ci * @dst: Destination SG list 2662306a36Sopenharmony_ci * @base: Underlying async request 2762306a36Sopenharmony_ci * @__ctx: Start of private context data 2862306a36Sopenharmony_ci */ 2962306a36Sopenharmony_cistruct skcipher_request { 3062306a36Sopenharmony_ci unsigned int cryptlen; 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci u8 *iv; 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci struct scatterlist *src; 3562306a36Sopenharmony_ci struct scatterlist *dst; 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci struct crypto_async_request base; 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci void *__ctx[] CRYPTO_MINALIGN_ATTR; 4062306a36Sopenharmony_ci}; 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_cistruct crypto_skcipher { 4362306a36Sopenharmony_ci unsigned int reqsize; 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci struct crypto_tfm base; 4662306a36Sopenharmony_ci}; 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_cistruct crypto_sync_skcipher { 4962306a36Sopenharmony_ci struct crypto_skcipher base; 5062306a36Sopenharmony_ci}; 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci/* 5362306a36Sopenharmony_ci * struct crypto_istat_cipher - statistics for cipher algorithm 5462306a36Sopenharmony_ci * @encrypt_cnt: number of encrypt requests 5562306a36Sopenharmony_ci * @encrypt_tlen: total data size handled by encrypt requests 5662306a36Sopenharmony_ci * @decrypt_cnt: number of decrypt requests 5762306a36Sopenharmony_ci * @decrypt_tlen: total data size handled by decrypt requests 5862306a36Sopenharmony_ci * @err_cnt: number of error for cipher requests 5962306a36Sopenharmony_ci */ 6062306a36Sopenharmony_cistruct crypto_istat_cipher { 6162306a36Sopenharmony_ci atomic64_t encrypt_cnt; 6262306a36Sopenharmony_ci atomic64_t encrypt_tlen; 6362306a36Sopenharmony_ci atomic64_t decrypt_cnt; 6462306a36Sopenharmony_ci atomic64_t decrypt_tlen; 6562306a36Sopenharmony_ci atomic64_t err_cnt; 6662306a36Sopenharmony_ci}; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci/** 6962306a36Sopenharmony_ci * struct skcipher_alg - symmetric key cipher definition 7062306a36Sopenharmony_ci * @min_keysize: Minimum key size supported by the transformation. This is the 7162306a36Sopenharmony_ci * smallest key length supported by this transformation algorithm. 7262306a36Sopenharmony_ci * This must be set to one of the pre-defined values as this is 7362306a36Sopenharmony_ci * not hardware specific. Possible values for this field can be 7462306a36Sopenharmony_ci * found via git grep "_MIN_KEY_SIZE" include/crypto/ 7562306a36Sopenharmony_ci * @max_keysize: Maximum key size supported by the transformation. This is the 7662306a36Sopenharmony_ci * largest key length supported by this transformation algorithm. 7762306a36Sopenharmony_ci * This must be set to one of the pre-defined values as this is 7862306a36Sopenharmony_ci * not hardware specific. Possible values for this field can be 7962306a36Sopenharmony_ci * found via git grep "_MAX_KEY_SIZE" include/crypto/ 8062306a36Sopenharmony_ci * @setkey: Set key for the transformation. This function is used to either 8162306a36Sopenharmony_ci * program a supplied key into the hardware or store the key in the 8262306a36Sopenharmony_ci * transformation context for programming it later. Note that this 8362306a36Sopenharmony_ci * function does modify the transformation context. This function can 8462306a36Sopenharmony_ci * be called multiple times during the existence of the transformation 8562306a36Sopenharmony_ci * object, so one must make sure the key is properly reprogrammed into 8662306a36Sopenharmony_ci * the hardware. This function is also responsible for checking the key 8762306a36Sopenharmony_ci * length for validity. In case a software fallback was put in place in 8862306a36Sopenharmony_ci * the @cra_init call, this function might need to use the fallback if 8962306a36Sopenharmony_ci * the algorithm doesn't support all of the key sizes. 9062306a36Sopenharmony_ci * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt 9162306a36Sopenharmony_ci * the supplied scatterlist containing the blocks of data. The crypto 9262306a36Sopenharmony_ci * API consumer is responsible for aligning the entries of the 9362306a36Sopenharmony_ci * scatterlist properly and making sure the chunks are correctly 9462306a36Sopenharmony_ci * sized. In case a software fallback was put in place in the 9562306a36Sopenharmony_ci * @cra_init call, this function might need to use the fallback if 9662306a36Sopenharmony_ci * the algorithm doesn't support all of the key sizes. In case the 9762306a36Sopenharmony_ci * key was stored in transformation context, the key might need to be 9862306a36Sopenharmony_ci * re-programmed into the hardware in this function. This function 9962306a36Sopenharmony_ci * shall not modify the transformation context, as this function may 10062306a36Sopenharmony_ci * be called in parallel with the same transformation object. 10162306a36Sopenharmony_ci * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt 10262306a36Sopenharmony_ci * and the conditions are exactly the same. 10362306a36Sopenharmony_ci * @init: Initialize the cryptographic transformation object. This function 10462306a36Sopenharmony_ci * is used to initialize the cryptographic transformation object. 10562306a36Sopenharmony_ci * This function is called only once at the instantiation time, right 10662306a36Sopenharmony_ci * after the transformation context was allocated. In case the 10762306a36Sopenharmony_ci * cryptographic hardware has some special requirements which need to 10862306a36Sopenharmony_ci * be handled by software, this function shall check for the precise 10962306a36Sopenharmony_ci * requirement of the transformation and put any software fallbacks 11062306a36Sopenharmony_ci * in place. 11162306a36Sopenharmony_ci * @exit: Deinitialize the cryptographic transformation object. This is a 11262306a36Sopenharmony_ci * counterpart to @init, used to remove various changes set in 11362306a36Sopenharmony_ci * @init. 11462306a36Sopenharmony_ci * @ivsize: IV size applicable for transformation. The consumer must provide an 11562306a36Sopenharmony_ci * IV of exactly that size to perform the encrypt or decrypt operation. 11662306a36Sopenharmony_ci * @chunksize: Equal to the block size except for stream ciphers such as 11762306a36Sopenharmony_ci * CTR where it is set to the underlying block size. 11862306a36Sopenharmony_ci * @walksize: Equal to the chunk size except in cases where the algorithm is 11962306a36Sopenharmony_ci * considerably more efficient if it can operate on multiple chunks 12062306a36Sopenharmony_ci * in parallel. Should be a multiple of chunksize. 12162306a36Sopenharmony_ci * @stat: Statistics for cipher algorithm 12262306a36Sopenharmony_ci * @base: Definition of a generic crypto algorithm. 12362306a36Sopenharmony_ci * 12462306a36Sopenharmony_ci * All fields except @ivsize are mandatory and must be filled. 12562306a36Sopenharmony_ci */ 12662306a36Sopenharmony_cistruct skcipher_alg { 12762306a36Sopenharmony_ci int (*setkey)(struct crypto_skcipher *tfm, const u8 *key, 12862306a36Sopenharmony_ci unsigned int keylen); 12962306a36Sopenharmony_ci int (*encrypt)(struct skcipher_request *req); 13062306a36Sopenharmony_ci int (*decrypt)(struct skcipher_request *req); 13162306a36Sopenharmony_ci int (*init)(struct crypto_skcipher *tfm); 13262306a36Sopenharmony_ci void (*exit)(struct crypto_skcipher *tfm); 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci unsigned int min_keysize; 13562306a36Sopenharmony_ci unsigned int max_keysize; 13662306a36Sopenharmony_ci unsigned int ivsize; 13762306a36Sopenharmony_ci unsigned int chunksize; 13862306a36Sopenharmony_ci unsigned int walksize; 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci#ifdef CONFIG_CRYPTO_STATS 14162306a36Sopenharmony_ci struct crypto_istat_cipher stat; 14262306a36Sopenharmony_ci#endif 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci struct crypto_alg base; 14562306a36Sopenharmony_ci}; 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci#define MAX_SYNC_SKCIPHER_REQSIZE 384 14862306a36Sopenharmony_ci/* 14962306a36Sopenharmony_ci * This performs a type-check against the "tfm" argument to make sure 15062306a36Sopenharmony_ci * all users have the correct skcipher tfm for doing on-stack requests. 15162306a36Sopenharmony_ci */ 15262306a36Sopenharmony_ci#define SYNC_SKCIPHER_REQUEST_ON_STACK(name, tfm) \ 15362306a36Sopenharmony_ci char __##name##_desc[sizeof(struct skcipher_request) + \ 15462306a36Sopenharmony_ci MAX_SYNC_SKCIPHER_REQSIZE + \ 15562306a36Sopenharmony_ci (!(sizeof((struct crypto_sync_skcipher *)1 == \ 15662306a36Sopenharmony_ci (typeof(tfm))1))) \ 15762306a36Sopenharmony_ci ] CRYPTO_MINALIGN_ATTR; \ 15862306a36Sopenharmony_ci struct skcipher_request *name = (void *)__##name##_desc 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci/** 16162306a36Sopenharmony_ci * DOC: Symmetric Key Cipher API 16262306a36Sopenharmony_ci * 16362306a36Sopenharmony_ci * Symmetric key cipher API is used with the ciphers of type 16462306a36Sopenharmony_ci * CRYPTO_ALG_TYPE_SKCIPHER (listed as type "skcipher" in /proc/crypto). 16562306a36Sopenharmony_ci * 16662306a36Sopenharmony_ci * Asynchronous cipher operations imply that the function invocation for a 16762306a36Sopenharmony_ci * cipher request returns immediately before the completion of the operation. 16862306a36Sopenharmony_ci * The cipher request is scheduled as a separate kernel thread and therefore 16962306a36Sopenharmony_ci * load-balanced on the different CPUs via the process scheduler. To allow 17062306a36Sopenharmony_ci * the kernel crypto API to inform the caller about the completion of a cipher 17162306a36Sopenharmony_ci * request, the caller must provide a callback function. That function is 17262306a36Sopenharmony_ci * invoked with the cipher handle when the request completes. 17362306a36Sopenharmony_ci * 17462306a36Sopenharmony_ci * To support the asynchronous operation, additional information than just the 17562306a36Sopenharmony_ci * cipher handle must be supplied to the kernel crypto API. That additional 17662306a36Sopenharmony_ci * information is given by filling in the skcipher_request data structure. 17762306a36Sopenharmony_ci * 17862306a36Sopenharmony_ci * For the symmetric key cipher API, the state is maintained with the tfm 17962306a36Sopenharmony_ci * cipher handle. A single tfm can be used across multiple calls and in 18062306a36Sopenharmony_ci * parallel. For asynchronous block cipher calls, context data supplied and 18162306a36Sopenharmony_ci * only used by the caller can be referenced the request data structure in 18262306a36Sopenharmony_ci * addition to the IV used for the cipher request. The maintenance of such 18362306a36Sopenharmony_ci * state information would be important for a crypto driver implementer to 18462306a36Sopenharmony_ci * have, because when calling the callback function upon completion of the 18562306a36Sopenharmony_ci * cipher operation, that callback function may need some information about 18662306a36Sopenharmony_ci * which operation just finished if it invoked multiple in parallel. This 18762306a36Sopenharmony_ci * state information is unused by the kernel crypto API. 18862306a36Sopenharmony_ci */ 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_cistatic inline struct crypto_skcipher *__crypto_skcipher_cast( 19162306a36Sopenharmony_ci struct crypto_tfm *tfm) 19262306a36Sopenharmony_ci{ 19362306a36Sopenharmony_ci return container_of(tfm, struct crypto_skcipher, base); 19462306a36Sopenharmony_ci} 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_ci/** 19762306a36Sopenharmony_ci * crypto_alloc_skcipher() - allocate symmetric key cipher handle 19862306a36Sopenharmony_ci * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 19962306a36Sopenharmony_ci * skcipher cipher 20062306a36Sopenharmony_ci * @type: specifies the type of the cipher 20162306a36Sopenharmony_ci * @mask: specifies the mask for the cipher 20262306a36Sopenharmony_ci * 20362306a36Sopenharmony_ci * Allocate a cipher handle for an skcipher. The returned struct 20462306a36Sopenharmony_ci * crypto_skcipher is the cipher handle that is required for any subsequent 20562306a36Sopenharmony_ci * API invocation for that skcipher. 20662306a36Sopenharmony_ci * 20762306a36Sopenharmony_ci * Return: allocated cipher handle in case of success; IS_ERR() is true in case 20862306a36Sopenharmony_ci * of an error, PTR_ERR() returns the error code. 20962306a36Sopenharmony_ci */ 21062306a36Sopenharmony_cistruct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name, 21162306a36Sopenharmony_ci u32 type, u32 mask); 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_cistruct crypto_sync_skcipher *crypto_alloc_sync_skcipher(const char *alg_name, 21462306a36Sopenharmony_ci u32 type, u32 mask); 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_cistatic inline struct crypto_tfm *crypto_skcipher_tfm( 21762306a36Sopenharmony_ci struct crypto_skcipher *tfm) 21862306a36Sopenharmony_ci{ 21962306a36Sopenharmony_ci return &tfm->base; 22062306a36Sopenharmony_ci} 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci/** 22362306a36Sopenharmony_ci * crypto_free_skcipher() - zeroize and free cipher handle 22462306a36Sopenharmony_ci * @tfm: cipher handle to be freed 22562306a36Sopenharmony_ci * 22662306a36Sopenharmony_ci * If @tfm is a NULL or error pointer, this function does nothing. 22762306a36Sopenharmony_ci */ 22862306a36Sopenharmony_cistatic inline void crypto_free_skcipher(struct crypto_skcipher *tfm) 22962306a36Sopenharmony_ci{ 23062306a36Sopenharmony_ci crypto_destroy_tfm(tfm, crypto_skcipher_tfm(tfm)); 23162306a36Sopenharmony_ci} 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_cistatic inline void crypto_free_sync_skcipher(struct crypto_sync_skcipher *tfm) 23462306a36Sopenharmony_ci{ 23562306a36Sopenharmony_ci crypto_free_skcipher(&tfm->base); 23662306a36Sopenharmony_ci} 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci/** 23962306a36Sopenharmony_ci * crypto_has_skcipher() - Search for the availability of an skcipher. 24062306a36Sopenharmony_ci * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 24162306a36Sopenharmony_ci * skcipher 24262306a36Sopenharmony_ci * @type: specifies the type of the skcipher 24362306a36Sopenharmony_ci * @mask: specifies the mask for the skcipher 24462306a36Sopenharmony_ci * 24562306a36Sopenharmony_ci * Return: true when the skcipher is known to the kernel crypto API; false 24662306a36Sopenharmony_ci * otherwise 24762306a36Sopenharmony_ci */ 24862306a36Sopenharmony_ciint crypto_has_skcipher(const char *alg_name, u32 type, u32 mask); 24962306a36Sopenharmony_ci 25062306a36Sopenharmony_cistatic inline const char *crypto_skcipher_driver_name( 25162306a36Sopenharmony_ci struct crypto_skcipher *tfm) 25262306a36Sopenharmony_ci{ 25362306a36Sopenharmony_ci return crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)); 25462306a36Sopenharmony_ci} 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_cistatic inline struct skcipher_alg *crypto_skcipher_alg( 25762306a36Sopenharmony_ci struct crypto_skcipher *tfm) 25862306a36Sopenharmony_ci{ 25962306a36Sopenharmony_ci return container_of(crypto_skcipher_tfm(tfm)->__crt_alg, 26062306a36Sopenharmony_ci struct skcipher_alg, base); 26162306a36Sopenharmony_ci} 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_cistatic inline unsigned int crypto_skcipher_alg_ivsize(struct skcipher_alg *alg) 26462306a36Sopenharmony_ci{ 26562306a36Sopenharmony_ci return alg->ivsize; 26662306a36Sopenharmony_ci} 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ci/** 26962306a36Sopenharmony_ci * crypto_skcipher_ivsize() - obtain IV size 27062306a36Sopenharmony_ci * @tfm: cipher handle 27162306a36Sopenharmony_ci * 27262306a36Sopenharmony_ci * The size of the IV for the skcipher referenced by the cipher handle is 27362306a36Sopenharmony_ci * returned. This IV size may be zero if the cipher does not need an IV. 27462306a36Sopenharmony_ci * 27562306a36Sopenharmony_ci * Return: IV size in bytes 27662306a36Sopenharmony_ci */ 27762306a36Sopenharmony_cistatic inline unsigned int crypto_skcipher_ivsize(struct crypto_skcipher *tfm) 27862306a36Sopenharmony_ci{ 27962306a36Sopenharmony_ci return crypto_skcipher_alg(tfm)->ivsize; 28062306a36Sopenharmony_ci} 28162306a36Sopenharmony_ci 28262306a36Sopenharmony_cistatic inline unsigned int crypto_sync_skcipher_ivsize( 28362306a36Sopenharmony_ci struct crypto_sync_skcipher *tfm) 28462306a36Sopenharmony_ci{ 28562306a36Sopenharmony_ci return crypto_skcipher_ivsize(&tfm->base); 28662306a36Sopenharmony_ci} 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ci/** 28962306a36Sopenharmony_ci * crypto_skcipher_blocksize() - obtain block size of cipher 29062306a36Sopenharmony_ci * @tfm: cipher handle 29162306a36Sopenharmony_ci * 29262306a36Sopenharmony_ci * The block size for the skcipher referenced with the cipher handle is 29362306a36Sopenharmony_ci * returned. The caller may use that information to allocate appropriate 29462306a36Sopenharmony_ci * memory for the data returned by the encryption or decryption operation 29562306a36Sopenharmony_ci * 29662306a36Sopenharmony_ci * Return: block size of cipher 29762306a36Sopenharmony_ci */ 29862306a36Sopenharmony_cistatic inline unsigned int crypto_skcipher_blocksize( 29962306a36Sopenharmony_ci struct crypto_skcipher *tfm) 30062306a36Sopenharmony_ci{ 30162306a36Sopenharmony_ci return crypto_tfm_alg_blocksize(crypto_skcipher_tfm(tfm)); 30262306a36Sopenharmony_ci} 30362306a36Sopenharmony_ci 30462306a36Sopenharmony_cistatic inline unsigned int crypto_skcipher_alg_chunksize( 30562306a36Sopenharmony_ci struct skcipher_alg *alg) 30662306a36Sopenharmony_ci{ 30762306a36Sopenharmony_ci return alg->chunksize; 30862306a36Sopenharmony_ci} 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ci/** 31162306a36Sopenharmony_ci * crypto_skcipher_chunksize() - obtain chunk size 31262306a36Sopenharmony_ci * @tfm: cipher handle 31362306a36Sopenharmony_ci * 31462306a36Sopenharmony_ci * The block size is set to one for ciphers such as CTR. However, 31562306a36Sopenharmony_ci * you still need to provide incremental updates in multiples of 31662306a36Sopenharmony_ci * the underlying block size as the IV does not have sub-block 31762306a36Sopenharmony_ci * granularity. This is known in this API as the chunk size. 31862306a36Sopenharmony_ci * 31962306a36Sopenharmony_ci * Return: chunk size in bytes 32062306a36Sopenharmony_ci */ 32162306a36Sopenharmony_cistatic inline unsigned int crypto_skcipher_chunksize( 32262306a36Sopenharmony_ci struct crypto_skcipher *tfm) 32362306a36Sopenharmony_ci{ 32462306a36Sopenharmony_ci return crypto_skcipher_alg_chunksize(crypto_skcipher_alg(tfm)); 32562306a36Sopenharmony_ci} 32662306a36Sopenharmony_ci 32762306a36Sopenharmony_cistatic inline unsigned int crypto_sync_skcipher_blocksize( 32862306a36Sopenharmony_ci struct crypto_sync_skcipher *tfm) 32962306a36Sopenharmony_ci{ 33062306a36Sopenharmony_ci return crypto_skcipher_blocksize(&tfm->base); 33162306a36Sopenharmony_ci} 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_cistatic inline unsigned int crypto_skcipher_alignmask( 33462306a36Sopenharmony_ci struct crypto_skcipher *tfm) 33562306a36Sopenharmony_ci{ 33662306a36Sopenharmony_ci return crypto_tfm_alg_alignmask(crypto_skcipher_tfm(tfm)); 33762306a36Sopenharmony_ci} 33862306a36Sopenharmony_ci 33962306a36Sopenharmony_cistatic inline u32 crypto_skcipher_get_flags(struct crypto_skcipher *tfm) 34062306a36Sopenharmony_ci{ 34162306a36Sopenharmony_ci return crypto_tfm_get_flags(crypto_skcipher_tfm(tfm)); 34262306a36Sopenharmony_ci} 34362306a36Sopenharmony_ci 34462306a36Sopenharmony_cistatic inline void crypto_skcipher_set_flags(struct crypto_skcipher *tfm, 34562306a36Sopenharmony_ci u32 flags) 34662306a36Sopenharmony_ci{ 34762306a36Sopenharmony_ci crypto_tfm_set_flags(crypto_skcipher_tfm(tfm), flags); 34862306a36Sopenharmony_ci} 34962306a36Sopenharmony_ci 35062306a36Sopenharmony_cistatic inline void crypto_skcipher_clear_flags(struct crypto_skcipher *tfm, 35162306a36Sopenharmony_ci u32 flags) 35262306a36Sopenharmony_ci{ 35362306a36Sopenharmony_ci crypto_tfm_clear_flags(crypto_skcipher_tfm(tfm), flags); 35462306a36Sopenharmony_ci} 35562306a36Sopenharmony_ci 35662306a36Sopenharmony_cistatic inline u32 crypto_sync_skcipher_get_flags( 35762306a36Sopenharmony_ci struct crypto_sync_skcipher *tfm) 35862306a36Sopenharmony_ci{ 35962306a36Sopenharmony_ci return crypto_skcipher_get_flags(&tfm->base); 36062306a36Sopenharmony_ci} 36162306a36Sopenharmony_ci 36262306a36Sopenharmony_cistatic inline void crypto_sync_skcipher_set_flags( 36362306a36Sopenharmony_ci struct crypto_sync_skcipher *tfm, u32 flags) 36462306a36Sopenharmony_ci{ 36562306a36Sopenharmony_ci crypto_skcipher_set_flags(&tfm->base, flags); 36662306a36Sopenharmony_ci} 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_cistatic inline void crypto_sync_skcipher_clear_flags( 36962306a36Sopenharmony_ci struct crypto_sync_skcipher *tfm, u32 flags) 37062306a36Sopenharmony_ci{ 37162306a36Sopenharmony_ci crypto_skcipher_clear_flags(&tfm->base, flags); 37262306a36Sopenharmony_ci} 37362306a36Sopenharmony_ci 37462306a36Sopenharmony_ci/** 37562306a36Sopenharmony_ci * crypto_skcipher_setkey() - set key for cipher 37662306a36Sopenharmony_ci * @tfm: cipher handle 37762306a36Sopenharmony_ci * @key: buffer holding the key 37862306a36Sopenharmony_ci * @keylen: length of the key in bytes 37962306a36Sopenharmony_ci * 38062306a36Sopenharmony_ci * The caller provided key is set for the skcipher referenced by the cipher 38162306a36Sopenharmony_ci * handle. 38262306a36Sopenharmony_ci * 38362306a36Sopenharmony_ci * Note, the key length determines the cipher type. Many block ciphers implement 38462306a36Sopenharmony_ci * different cipher modes depending on the key size, such as AES-128 vs AES-192 38562306a36Sopenharmony_ci * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 38662306a36Sopenharmony_ci * is performed. 38762306a36Sopenharmony_ci * 38862306a36Sopenharmony_ci * Return: 0 if the setting of the key was successful; < 0 if an error occurred 38962306a36Sopenharmony_ci */ 39062306a36Sopenharmony_ciint crypto_skcipher_setkey(struct crypto_skcipher *tfm, 39162306a36Sopenharmony_ci const u8 *key, unsigned int keylen); 39262306a36Sopenharmony_ci 39362306a36Sopenharmony_cistatic inline int crypto_sync_skcipher_setkey(struct crypto_sync_skcipher *tfm, 39462306a36Sopenharmony_ci const u8 *key, unsigned int keylen) 39562306a36Sopenharmony_ci{ 39662306a36Sopenharmony_ci return crypto_skcipher_setkey(&tfm->base, key, keylen); 39762306a36Sopenharmony_ci} 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_cistatic inline unsigned int crypto_skcipher_min_keysize( 40062306a36Sopenharmony_ci struct crypto_skcipher *tfm) 40162306a36Sopenharmony_ci{ 40262306a36Sopenharmony_ci return crypto_skcipher_alg(tfm)->min_keysize; 40362306a36Sopenharmony_ci} 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_cistatic inline unsigned int crypto_skcipher_max_keysize( 40662306a36Sopenharmony_ci struct crypto_skcipher *tfm) 40762306a36Sopenharmony_ci{ 40862306a36Sopenharmony_ci return crypto_skcipher_alg(tfm)->max_keysize; 40962306a36Sopenharmony_ci} 41062306a36Sopenharmony_ci 41162306a36Sopenharmony_ci/** 41262306a36Sopenharmony_ci * crypto_skcipher_reqtfm() - obtain cipher handle from request 41362306a36Sopenharmony_ci * @req: skcipher_request out of which the cipher handle is to be obtained 41462306a36Sopenharmony_ci * 41562306a36Sopenharmony_ci * Return the crypto_skcipher handle when furnishing an skcipher_request 41662306a36Sopenharmony_ci * data structure. 41762306a36Sopenharmony_ci * 41862306a36Sopenharmony_ci * Return: crypto_skcipher handle 41962306a36Sopenharmony_ci */ 42062306a36Sopenharmony_cistatic inline struct crypto_skcipher *crypto_skcipher_reqtfm( 42162306a36Sopenharmony_ci struct skcipher_request *req) 42262306a36Sopenharmony_ci{ 42362306a36Sopenharmony_ci return __crypto_skcipher_cast(req->base.tfm); 42462306a36Sopenharmony_ci} 42562306a36Sopenharmony_ci 42662306a36Sopenharmony_cistatic inline struct crypto_sync_skcipher *crypto_sync_skcipher_reqtfm( 42762306a36Sopenharmony_ci struct skcipher_request *req) 42862306a36Sopenharmony_ci{ 42962306a36Sopenharmony_ci struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 43062306a36Sopenharmony_ci 43162306a36Sopenharmony_ci return container_of(tfm, struct crypto_sync_skcipher, base); 43262306a36Sopenharmony_ci} 43362306a36Sopenharmony_ci 43462306a36Sopenharmony_ci/** 43562306a36Sopenharmony_ci * crypto_skcipher_encrypt() - encrypt plaintext 43662306a36Sopenharmony_ci * @req: reference to the skcipher_request handle that holds all information 43762306a36Sopenharmony_ci * needed to perform the cipher operation 43862306a36Sopenharmony_ci * 43962306a36Sopenharmony_ci * Encrypt plaintext data using the skcipher_request handle. That data 44062306a36Sopenharmony_ci * structure and how it is filled with data is discussed with the 44162306a36Sopenharmony_ci * skcipher_request_* functions. 44262306a36Sopenharmony_ci * 44362306a36Sopenharmony_ci * Return: 0 if the cipher operation was successful; < 0 if an error occurred 44462306a36Sopenharmony_ci */ 44562306a36Sopenharmony_ciint crypto_skcipher_encrypt(struct skcipher_request *req); 44662306a36Sopenharmony_ci 44762306a36Sopenharmony_ci/** 44862306a36Sopenharmony_ci * crypto_skcipher_decrypt() - decrypt ciphertext 44962306a36Sopenharmony_ci * @req: reference to the skcipher_request handle that holds all information 45062306a36Sopenharmony_ci * needed to perform the cipher operation 45162306a36Sopenharmony_ci * 45262306a36Sopenharmony_ci * Decrypt ciphertext data using the skcipher_request handle. That data 45362306a36Sopenharmony_ci * structure and how it is filled with data is discussed with the 45462306a36Sopenharmony_ci * skcipher_request_* functions. 45562306a36Sopenharmony_ci * 45662306a36Sopenharmony_ci * Return: 0 if the cipher operation was successful; < 0 if an error occurred 45762306a36Sopenharmony_ci */ 45862306a36Sopenharmony_ciint crypto_skcipher_decrypt(struct skcipher_request *req); 45962306a36Sopenharmony_ci 46062306a36Sopenharmony_ci/** 46162306a36Sopenharmony_ci * DOC: Symmetric Key Cipher Request Handle 46262306a36Sopenharmony_ci * 46362306a36Sopenharmony_ci * The skcipher_request data structure contains all pointers to data 46462306a36Sopenharmony_ci * required for the symmetric key cipher operation. This includes the cipher 46562306a36Sopenharmony_ci * handle (which can be used by multiple skcipher_request instances), pointer 46662306a36Sopenharmony_ci * to plaintext and ciphertext, asynchronous callback function, etc. It acts 46762306a36Sopenharmony_ci * as a handle to the skcipher_request_* API calls in a similar way as 46862306a36Sopenharmony_ci * skcipher handle to the crypto_skcipher_* API calls. 46962306a36Sopenharmony_ci */ 47062306a36Sopenharmony_ci 47162306a36Sopenharmony_ci/** 47262306a36Sopenharmony_ci * crypto_skcipher_reqsize() - obtain size of the request data structure 47362306a36Sopenharmony_ci * @tfm: cipher handle 47462306a36Sopenharmony_ci * 47562306a36Sopenharmony_ci * Return: number of bytes 47662306a36Sopenharmony_ci */ 47762306a36Sopenharmony_cistatic inline unsigned int crypto_skcipher_reqsize(struct crypto_skcipher *tfm) 47862306a36Sopenharmony_ci{ 47962306a36Sopenharmony_ci return tfm->reqsize; 48062306a36Sopenharmony_ci} 48162306a36Sopenharmony_ci 48262306a36Sopenharmony_ci/** 48362306a36Sopenharmony_ci * skcipher_request_set_tfm() - update cipher handle reference in request 48462306a36Sopenharmony_ci * @req: request handle to be modified 48562306a36Sopenharmony_ci * @tfm: cipher handle that shall be added to the request handle 48662306a36Sopenharmony_ci * 48762306a36Sopenharmony_ci * Allow the caller to replace the existing skcipher handle in the request 48862306a36Sopenharmony_ci * data structure with a different one. 48962306a36Sopenharmony_ci */ 49062306a36Sopenharmony_cistatic inline void skcipher_request_set_tfm(struct skcipher_request *req, 49162306a36Sopenharmony_ci struct crypto_skcipher *tfm) 49262306a36Sopenharmony_ci{ 49362306a36Sopenharmony_ci req->base.tfm = crypto_skcipher_tfm(tfm); 49462306a36Sopenharmony_ci} 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_cistatic inline void skcipher_request_set_sync_tfm(struct skcipher_request *req, 49762306a36Sopenharmony_ci struct crypto_sync_skcipher *tfm) 49862306a36Sopenharmony_ci{ 49962306a36Sopenharmony_ci skcipher_request_set_tfm(req, &tfm->base); 50062306a36Sopenharmony_ci} 50162306a36Sopenharmony_ci 50262306a36Sopenharmony_cistatic inline struct skcipher_request *skcipher_request_cast( 50362306a36Sopenharmony_ci struct crypto_async_request *req) 50462306a36Sopenharmony_ci{ 50562306a36Sopenharmony_ci return container_of(req, struct skcipher_request, base); 50662306a36Sopenharmony_ci} 50762306a36Sopenharmony_ci 50862306a36Sopenharmony_ci/** 50962306a36Sopenharmony_ci * skcipher_request_alloc() - allocate request data structure 51062306a36Sopenharmony_ci * @tfm: cipher handle to be registered with the request 51162306a36Sopenharmony_ci * @gfp: memory allocation flag that is handed to kmalloc by the API call. 51262306a36Sopenharmony_ci * 51362306a36Sopenharmony_ci * Allocate the request data structure that must be used with the skcipher 51462306a36Sopenharmony_ci * encrypt and decrypt API calls. During the allocation, the provided skcipher 51562306a36Sopenharmony_ci * handle is registered in the request data structure. 51662306a36Sopenharmony_ci * 51762306a36Sopenharmony_ci * Return: allocated request handle in case of success, or NULL if out of memory 51862306a36Sopenharmony_ci */ 51962306a36Sopenharmony_cistatic inline struct skcipher_request *skcipher_request_alloc( 52062306a36Sopenharmony_ci struct crypto_skcipher *tfm, gfp_t gfp) 52162306a36Sopenharmony_ci{ 52262306a36Sopenharmony_ci struct skcipher_request *req; 52362306a36Sopenharmony_ci 52462306a36Sopenharmony_ci req = kmalloc(sizeof(struct skcipher_request) + 52562306a36Sopenharmony_ci crypto_skcipher_reqsize(tfm), gfp); 52662306a36Sopenharmony_ci 52762306a36Sopenharmony_ci if (likely(req)) 52862306a36Sopenharmony_ci skcipher_request_set_tfm(req, tfm); 52962306a36Sopenharmony_ci 53062306a36Sopenharmony_ci return req; 53162306a36Sopenharmony_ci} 53262306a36Sopenharmony_ci 53362306a36Sopenharmony_ci/** 53462306a36Sopenharmony_ci * skcipher_request_free() - zeroize and free request data structure 53562306a36Sopenharmony_ci * @req: request data structure cipher handle to be freed 53662306a36Sopenharmony_ci */ 53762306a36Sopenharmony_cistatic inline void skcipher_request_free(struct skcipher_request *req) 53862306a36Sopenharmony_ci{ 53962306a36Sopenharmony_ci kfree_sensitive(req); 54062306a36Sopenharmony_ci} 54162306a36Sopenharmony_ci 54262306a36Sopenharmony_cistatic inline void skcipher_request_zero(struct skcipher_request *req) 54362306a36Sopenharmony_ci{ 54462306a36Sopenharmony_ci struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 54562306a36Sopenharmony_ci 54662306a36Sopenharmony_ci memzero_explicit(req, sizeof(*req) + crypto_skcipher_reqsize(tfm)); 54762306a36Sopenharmony_ci} 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci/** 55062306a36Sopenharmony_ci * skcipher_request_set_callback() - set asynchronous callback function 55162306a36Sopenharmony_ci * @req: request handle 55262306a36Sopenharmony_ci * @flags: specify zero or an ORing of the flags 55362306a36Sopenharmony_ci * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and 55462306a36Sopenharmony_ci * increase the wait queue beyond the initial maximum size; 55562306a36Sopenharmony_ci * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep 55662306a36Sopenharmony_ci * @compl: callback function pointer to be registered with the request handle 55762306a36Sopenharmony_ci * @data: The data pointer refers to memory that is not used by the kernel 55862306a36Sopenharmony_ci * crypto API, but provided to the callback function for it to use. Here, 55962306a36Sopenharmony_ci * the caller can provide a reference to memory the callback function can 56062306a36Sopenharmony_ci * operate on. As the callback function is invoked asynchronously to the 56162306a36Sopenharmony_ci * related functionality, it may need to access data structures of the 56262306a36Sopenharmony_ci * related functionality which can be referenced using this pointer. The 56362306a36Sopenharmony_ci * callback function can access the memory via the "data" field in the 56462306a36Sopenharmony_ci * crypto_async_request data structure provided to the callback function. 56562306a36Sopenharmony_ci * 56662306a36Sopenharmony_ci * This function allows setting the callback function that is triggered once the 56762306a36Sopenharmony_ci * cipher operation completes. 56862306a36Sopenharmony_ci * 56962306a36Sopenharmony_ci * The callback function is registered with the skcipher_request handle and 57062306a36Sopenharmony_ci * must comply with the following template:: 57162306a36Sopenharmony_ci * 57262306a36Sopenharmony_ci * void callback_function(struct crypto_async_request *req, int error) 57362306a36Sopenharmony_ci */ 57462306a36Sopenharmony_cistatic inline void skcipher_request_set_callback(struct skcipher_request *req, 57562306a36Sopenharmony_ci u32 flags, 57662306a36Sopenharmony_ci crypto_completion_t compl, 57762306a36Sopenharmony_ci void *data) 57862306a36Sopenharmony_ci{ 57962306a36Sopenharmony_ci req->base.complete = compl; 58062306a36Sopenharmony_ci req->base.data = data; 58162306a36Sopenharmony_ci req->base.flags = flags; 58262306a36Sopenharmony_ci} 58362306a36Sopenharmony_ci 58462306a36Sopenharmony_ci/** 58562306a36Sopenharmony_ci * skcipher_request_set_crypt() - set data buffers 58662306a36Sopenharmony_ci * @req: request handle 58762306a36Sopenharmony_ci * @src: source scatter / gather list 58862306a36Sopenharmony_ci * @dst: destination scatter / gather list 58962306a36Sopenharmony_ci * @cryptlen: number of bytes to process from @src 59062306a36Sopenharmony_ci * @iv: IV for the cipher operation which must comply with the IV size defined 59162306a36Sopenharmony_ci * by crypto_skcipher_ivsize 59262306a36Sopenharmony_ci * 59362306a36Sopenharmony_ci * This function allows setting of the source data and destination data 59462306a36Sopenharmony_ci * scatter / gather lists. 59562306a36Sopenharmony_ci * 59662306a36Sopenharmony_ci * For encryption, the source is treated as the plaintext and the 59762306a36Sopenharmony_ci * destination is the ciphertext. For a decryption operation, the use is 59862306a36Sopenharmony_ci * reversed - the source is the ciphertext and the destination is the plaintext. 59962306a36Sopenharmony_ci */ 60062306a36Sopenharmony_cistatic inline void skcipher_request_set_crypt( 60162306a36Sopenharmony_ci struct skcipher_request *req, 60262306a36Sopenharmony_ci struct scatterlist *src, struct scatterlist *dst, 60362306a36Sopenharmony_ci unsigned int cryptlen, void *iv) 60462306a36Sopenharmony_ci{ 60562306a36Sopenharmony_ci req->src = src; 60662306a36Sopenharmony_ci req->dst = dst; 60762306a36Sopenharmony_ci req->cryptlen = cryptlen; 60862306a36Sopenharmony_ci req->iv = iv; 60962306a36Sopenharmony_ci} 61062306a36Sopenharmony_ci 61162306a36Sopenharmony_ci#endif /* _CRYPTO_SKCIPHER_H */ 61262306a36Sopenharmony_ci 613