/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* Copyright (c) 2002 James Morris <[email protected]>3* Copyright (c) 2002 David S. Miller ([email protected])4* Copyright (c) 2005 Herbert Xu <[email protected]>5*6* Portions derived from Cryptoapi, by Alexander Kjeldaas <[email protected]>7* and Nettle, by Niels Möller.8*/910#ifndef _CRYPTO_INTERNAL_CIPHER_H11#define _CRYPTO_INTERNAL_CIPHER_H1213#include <crypto/algapi.h>1415struct crypto_cipher {16struct crypto_tfm base;17};1819/**20* DOC: Single Block Cipher API21*22* The single block cipher API is used with the ciphers of type23* CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).24*25* Using the single block cipher API calls, operations with the basic cipher26* primitive can be implemented. These cipher primitives exclude any block27* chaining operations including IV handling.28*29* The purpose of this single block cipher API is to support the implementation30* of templates or other concepts that only need to perform the cipher operation31* on one block at a time. Templates invoke the underlying cipher primitive32* block-wise and process either the input or the output data of these cipher33* operations.34*/3536static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)37{38return (struct crypto_cipher *)tfm;39}4041/**42* crypto_alloc_cipher() - allocate single block cipher handle43* @alg_name: is the cra_name / name or cra_driver_name / driver name of the44* single block cipher45* @type: specifies the type of the cipher46* @mask: specifies the mask for the cipher47*48* Allocate a cipher handle for a single block cipher. The returned struct49* crypto_cipher is the cipher handle that is required for any subsequent API50* invocation for that single block cipher.51*52* Return: allocated cipher handle in case of success; IS_ERR() is true in case53* of an error, PTR_ERR() returns the error code.54*/55static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,56u32 type, u32 mask)57{58type &= ~CRYPTO_ALG_TYPE_MASK;59type |= CRYPTO_ALG_TYPE_CIPHER;60mask |= CRYPTO_ALG_TYPE_MASK;6162return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));63}6465static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)66{67return &tfm->base;68}6970/**71* crypto_free_cipher() - zeroize and free the single block cipher handle72* @tfm: cipher handle to be freed73*/74static inline void crypto_free_cipher(struct crypto_cipher *tfm)75{76crypto_free_tfm(crypto_cipher_tfm(tfm));77}7879/**80* crypto_has_cipher() - Search for the availability of a single block cipher81* @alg_name: is the cra_name / name or cra_driver_name / driver name of the82* single block cipher83* @type: specifies the type of the cipher84* @mask: specifies the mask for the cipher85*86* Return: true when the single block cipher is known to the kernel crypto API;87* false otherwise88*/89static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)90{91type &= ~CRYPTO_ALG_TYPE_MASK;92type |= CRYPTO_ALG_TYPE_CIPHER;93mask |= CRYPTO_ALG_TYPE_MASK;9495return crypto_has_alg(alg_name, type, mask);96}9798/**99* crypto_cipher_blocksize() - obtain block size for cipher100* @tfm: cipher handle101*102* The block size for the single block cipher referenced with the cipher handle103* tfm is returned. The caller may use that information to allocate appropriate104* memory for the data returned by the encryption or decryption operation105*106* Return: block size of cipher107*/108static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)109{110return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));111}112113static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)114{115return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));116}117118static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)119{120return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));121}122123static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,124u32 flags)125{126crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);127}128129static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,130u32 flags)131{132crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);133}134135/**136* crypto_cipher_setkey() - set key for cipher137* @tfm: cipher handle138* @key: buffer holding the key139* @keylen: length of the key in bytes140*141* The caller provided key is set for the single block cipher referenced by the142* cipher handle.143*144* Note, the key length determines the cipher type. Many block ciphers implement145* different cipher modes depending on the key size, such as AES-128 vs AES-192146* vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128147* is performed.148*149* Return: 0 if the setting of the key was successful; < 0 if an error occurred150*/151int crypto_cipher_setkey(struct crypto_cipher *tfm,152const u8 *key, unsigned int keylen);153154/**155* crypto_cipher_encrypt_one() - encrypt one block of plaintext156* @tfm: cipher handle157* @dst: points to the buffer that will be filled with the ciphertext158* @src: buffer holding the plaintext to be encrypted159*160* Invoke the encryption operation of one block. The caller must ensure that161* the plaintext and ciphertext buffers are at least one block in size.162*/163void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,164u8 *dst, const u8 *src);165166/**167* crypto_cipher_decrypt_one() - decrypt one block of ciphertext168* @tfm: cipher handle169* @dst: points to the buffer that will be filled with the plaintext170* @src: buffer holding the ciphertext to be decrypted171*172* Invoke the decryption operation of one block. The caller must ensure that173* the plaintext and ciphertext buffers are at least one block in size.174*/175void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,176u8 *dst, const u8 *src);177178struct crypto_cipher *crypto_clone_cipher(struct crypto_cipher *cipher);179180struct crypto_cipher_spawn {181struct crypto_spawn base;182};183184static inline int crypto_grab_cipher(struct crypto_cipher_spawn *spawn,185struct crypto_instance *inst,186const char *name, u32 type, u32 mask)187{188type &= ~CRYPTO_ALG_TYPE_MASK;189type |= CRYPTO_ALG_TYPE_CIPHER;190mask |= CRYPTO_ALG_TYPE_MASK;191return crypto_grab_spawn(&spawn->base, inst, name, type, mask);192}193194static inline void crypto_drop_cipher(struct crypto_cipher_spawn *spawn)195{196crypto_drop_spawn(&spawn->base);197}198199static inline struct crypto_alg *crypto_spawn_cipher_alg(200struct crypto_cipher_spawn *spawn)201{202return spawn->base.alg;203}204205static inline struct crypto_cipher *crypto_spawn_cipher(206struct crypto_cipher_spawn *spawn)207{208u32 type = CRYPTO_ALG_TYPE_CIPHER;209u32 mask = CRYPTO_ALG_TYPE_MASK;210211return __crypto_cipher_cast(crypto_spawn_tfm(&spawn->base, type, mask));212}213214static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)215{216return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;217}218219#endif220221222