221 lines · c
1/* SPDX-License-Identifier: GPL-2.0-or-later */2/*3 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>4 * Copyright (c) 2002 David S. Miller (davem@redhat.com)5 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>6 *7 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>8 * and Nettle, by Niels Möller.9 */10 11#ifndef _CRYPTO_INTERNAL_CIPHER_H12#define _CRYPTO_INTERNAL_CIPHER_H13 14#include <crypto/algapi.h>15 16struct crypto_cipher {17 struct crypto_tfm base;18};19 20/**21 * DOC: Single Block Cipher API22 *23 * The single block cipher API is used with the ciphers of type24 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).25 *26 * Using the single block cipher API calls, operations with the basic cipher27 * primitive can be implemented. These cipher primitives exclude any block28 * chaining operations including IV handling.29 *30 * The purpose of this single block cipher API is to support the implementation31 * of templates or other concepts that only need to perform the cipher operation32 * on one block at a time. Templates invoke the underlying cipher primitive33 * block-wise and process either the input or the output data of these cipher34 * operations.35 */36 37static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)38{39 return (struct crypto_cipher *)tfm;40}41 42/**43 * crypto_alloc_cipher() - allocate single block cipher handle44 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the45 * single block cipher46 * @type: specifies the type of the cipher47 * @mask: specifies the mask for the cipher48 *49 * Allocate a cipher handle for a single block cipher. The returned struct50 * crypto_cipher is the cipher handle that is required for any subsequent API51 * invocation for that single block cipher.52 *53 * Return: allocated cipher handle in case of success; IS_ERR() is true in case54 * of an error, PTR_ERR() returns the error code.55 */56static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,57 u32 type, u32 mask)58{59 type &= ~CRYPTO_ALG_TYPE_MASK;60 type |= CRYPTO_ALG_TYPE_CIPHER;61 mask |= CRYPTO_ALG_TYPE_MASK;62 63 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));64}65 66static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)67{68 return &tfm->base;69}70 71/**72 * crypto_free_cipher() - zeroize and free the single block cipher handle73 * @tfm: cipher handle to be freed74 */75static inline void crypto_free_cipher(struct crypto_cipher *tfm)76{77 crypto_free_tfm(crypto_cipher_tfm(tfm));78}79 80/**81 * crypto_has_cipher() - Search for the availability of a single block cipher82 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the83 * single block cipher84 * @type: specifies the type of the cipher85 * @mask: specifies the mask for the cipher86 *87 * Return: true when the single block cipher is known to the kernel crypto API;88 * false otherwise89 */90static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)91{92 type &= ~CRYPTO_ALG_TYPE_MASK;93 type |= CRYPTO_ALG_TYPE_CIPHER;94 mask |= CRYPTO_ALG_TYPE_MASK;95 96 return crypto_has_alg(alg_name, type, mask);97}98 99/**100 * crypto_cipher_blocksize() - obtain block size for cipher101 * @tfm: cipher handle102 *103 * The block size for the single block cipher referenced with the cipher handle104 * tfm is returned. The caller may use that information to allocate appropriate105 * memory for the data returned by the encryption or decryption operation106 *107 * Return: block size of cipher108 */109static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)110{111 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));112}113 114static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)115{116 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));117}118 119static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)120{121 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));122}123 124static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,125 u32 flags)126{127 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);128}129 130static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,131 u32 flags)132{133 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);134}135 136/**137 * crypto_cipher_setkey() - set key for cipher138 * @tfm: cipher handle139 * @key: buffer holding the key140 * @keylen: length of the key in bytes141 *142 * The caller provided key is set for the single block cipher referenced by the143 * cipher handle.144 *145 * Note, the key length determines the cipher type. Many block ciphers implement146 * different cipher modes depending on the key size, such as AES-128 vs AES-192147 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128148 * is performed.149 *150 * Return: 0 if the setting of the key was successful; < 0 if an error occurred151 */152int crypto_cipher_setkey(struct crypto_cipher *tfm,153 const u8 *key, unsigned int keylen);154 155/**156 * crypto_cipher_encrypt_one() - encrypt one block of plaintext157 * @tfm: cipher handle158 * @dst: points to the buffer that will be filled with the ciphertext159 * @src: buffer holding the plaintext to be encrypted160 *161 * Invoke the encryption operation of one block. The caller must ensure that162 * the plaintext and ciphertext buffers are at least one block in size.163 */164void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,165 u8 *dst, const u8 *src);166 167/**168 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext169 * @tfm: cipher handle170 * @dst: points to the buffer that will be filled with the plaintext171 * @src: buffer holding the ciphertext to be decrypted172 *173 * Invoke the decryption operation of one block. The caller must ensure that174 * the plaintext and ciphertext buffers are at least one block in size.175 */176void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,177 u8 *dst, const u8 *src);178 179struct crypto_cipher *crypto_clone_cipher(struct crypto_cipher *cipher);180 181struct crypto_cipher_spawn {182 struct crypto_spawn base;183};184 185static inline int crypto_grab_cipher(struct crypto_cipher_spawn *spawn,186 struct crypto_instance *inst,187 const char *name, u32 type, u32 mask)188{189 type &= ~CRYPTO_ALG_TYPE_MASK;190 type |= CRYPTO_ALG_TYPE_CIPHER;191 mask |= CRYPTO_ALG_TYPE_MASK;192 return crypto_grab_spawn(&spawn->base, inst, name, type, mask);193}194 195static inline void crypto_drop_cipher(struct crypto_cipher_spawn *spawn)196{197 crypto_drop_spawn(&spawn->base);198}199 200static inline struct crypto_alg *crypto_spawn_cipher_alg(201 struct crypto_cipher_spawn *spawn)202{203 return spawn->base.alg;204}205 206static inline struct crypto_cipher *crypto_spawn_cipher(207 struct crypto_cipher_spawn *spawn)208{209 u32 type = CRYPTO_ALG_TYPE_CIPHER;210 u32 mask = CRYPTO_ALG_TYPE_MASK;211 212 return __crypto_cipher_cast(crypto_spawn_tfm(&spawn->base, type, mask));213}214 215static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)216{217 return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;218}219 220#endif221