162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 462306a36Sopenharmony_ci * Copyright (c) 2002 David S. Miller (davem@redhat.com) 562306a36Sopenharmony_ci * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no> 862306a36Sopenharmony_ci * and Nettle, by Niels Möller. 962306a36Sopenharmony_ci */ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#ifndef _CRYPTO_INTERNAL_CIPHER_H 1262306a36Sopenharmony_ci#define _CRYPTO_INTERNAL_CIPHER_H 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#include <crypto/algapi.h> 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_cistruct crypto_cipher { 1762306a36Sopenharmony_ci struct crypto_tfm base; 1862306a36Sopenharmony_ci}; 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci/** 2162306a36Sopenharmony_ci * DOC: Single Block Cipher API 2262306a36Sopenharmony_ci * 2362306a36Sopenharmony_ci * The single block cipher API is used with the ciphers of type 2462306a36Sopenharmony_ci * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto). 2562306a36Sopenharmony_ci * 2662306a36Sopenharmony_ci * Using the single block cipher API calls, operations with the basic cipher 2762306a36Sopenharmony_ci * primitive can be implemented. These cipher primitives exclude any block 2862306a36Sopenharmony_ci * chaining operations including IV handling. 2962306a36Sopenharmony_ci * 3062306a36Sopenharmony_ci * The purpose of this single block cipher API is to support the implementation 3162306a36Sopenharmony_ci * of templates or other concepts that only need to perform the cipher operation 3262306a36Sopenharmony_ci * on one block at a time. Templates invoke the underlying cipher primitive 3362306a36Sopenharmony_ci * block-wise and process either the input or the output data of these cipher 3462306a36Sopenharmony_ci * operations. 3562306a36Sopenharmony_ci */ 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_cistatic inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm) 3862306a36Sopenharmony_ci{ 3962306a36Sopenharmony_ci return (struct crypto_cipher *)tfm; 4062306a36Sopenharmony_ci} 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci/** 4362306a36Sopenharmony_ci * crypto_alloc_cipher() - allocate single block cipher handle 4462306a36Sopenharmony_ci * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 4562306a36Sopenharmony_ci * single block cipher 4662306a36Sopenharmony_ci * @type: specifies the type of the cipher 4762306a36Sopenharmony_ci * @mask: specifies the mask for the cipher 4862306a36Sopenharmony_ci * 4962306a36Sopenharmony_ci * Allocate a cipher handle for a single block cipher. The returned struct 5062306a36Sopenharmony_ci * crypto_cipher is the cipher handle that is required for any subsequent API 5162306a36Sopenharmony_ci * invocation for that single block cipher. 5262306a36Sopenharmony_ci * 5362306a36Sopenharmony_ci * Return: allocated cipher handle in case of success; IS_ERR() is true in case 5462306a36Sopenharmony_ci * of an error, PTR_ERR() returns the error code. 5562306a36Sopenharmony_ci */ 5662306a36Sopenharmony_cistatic inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name, 5762306a36Sopenharmony_ci u32 type, u32 mask) 5862306a36Sopenharmony_ci{ 5962306a36Sopenharmony_ci type &= ~CRYPTO_ALG_TYPE_MASK; 6062306a36Sopenharmony_ci type |= CRYPTO_ALG_TYPE_CIPHER; 6162306a36Sopenharmony_ci mask |= CRYPTO_ALG_TYPE_MASK; 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask)); 6462306a36Sopenharmony_ci} 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_cistatic inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm) 6762306a36Sopenharmony_ci{ 6862306a36Sopenharmony_ci return &tfm->base; 6962306a36Sopenharmony_ci} 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci/** 7262306a36Sopenharmony_ci * crypto_free_cipher() - zeroize and free the single block cipher handle 7362306a36Sopenharmony_ci * @tfm: cipher handle to be freed 7462306a36Sopenharmony_ci */ 7562306a36Sopenharmony_cistatic inline void crypto_free_cipher(struct crypto_cipher *tfm) 7662306a36Sopenharmony_ci{ 7762306a36Sopenharmony_ci crypto_free_tfm(crypto_cipher_tfm(tfm)); 7862306a36Sopenharmony_ci} 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci/** 8162306a36Sopenharmony_ci * crypto_has_cipher() - Search for the availability of a single block cipher 8262306a36Sopenharmony_ci * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 8362306a36Sopenharmony_ci * single block cipher 8462306a36Sopenharmony_ci * @type: specifies the type of the cipher 8562306a36Sopenharmony_ci * @mask: specifies the mask for the cipher 8662306a36Sopenharmony_ci * 8762306a36Sopenharmony_ci * Return: true when the single block cipher is known to the kernel crypto API; 8862306a36Sopenharmony_ci * false otherwise 8962306a36Sopenharmony_ci */ 9062306a36Sopenharmony_cistatic inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask) 9162306a36Sopenharmony_ci{ 9262306a36Sopenharmony_ci type &= ~CRYPTO_ALG_TYPE_MASK; 9362306a36Sopenharmony_ci type |= CRYPTO_ALG_TYPE_CIPHER; 9462306a36Sopenharmony_ci mask |= CRYPTO_ALG_TYPE_MASK; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci return crypto_has_alg(alg_name, type, mask); 9762306a36Sopenharmony_ci} 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci/** 10062306a36Sopenharmony_ci * crypto_cipher_blocksize() - obtain block size for cipher 10162306a36Sopenharmony_ci * @tfm: cipher handle 10262306a36Sopenharmony_ci * 10362306a36Sopenharmony_ci * The block size for the single block cipher referenced with the cipher handle 10462306a36Sopenharmony_ci * tfm is returned. The caller may use that information to allocate appropriate 10562306a36Sopenharmony_ci * memory for the data returned by the encryption or decryption operation 10662306a36Sopenharmony_ci * 10762306a36Sopenharmony_ci * Return: block size of cipher 10862306a36Sopenharmony_ci */ 10962306a36Sopenharmony_cistatic inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm) 11062306a36Sopenharmony_ci{ 11162306a36Sopenharmony_ci return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm)); 11262306a36Sopenharmony_ci} 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_cistatic inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm) 11562306a36Sopenharmony_ci{ 11662306a36Sopenharmony_ci return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm)); 11762306a36Sopenharmony_ci} 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_cistatic inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm) 12062306a36Sopenharmony_ci{ 12162306a36Sopenharmony_ci return crypto_tfm_get_flags(crypto_cipher_tfm(tfm)); 12262306a36Sopenharmony_ci} 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_cistatic inline void crypto_cipher_set_flags(struct crypto_cipher *tfm, 12562306a36Sopenharmony_ci u32 flags) 12662306a36Sopenharmony_ci{ 12762306a36Sopenharmony_ci crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags); 12862306a36Sopenharmony_ci} 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_cistatic inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm, 13162306a36Sopenharmony_ci u32 flags) 13262306a36Sopenharmony_ci{ 13362306a36Sopenharmony_ci crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags); 13462306a36Sopenharmony_ci} 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci/** 13762306a36Sopenharmony_ci * crypto_cipher_setkey() - set key for cipher 13862306a36Sopenharmony_ci * @tfm: cipher handle 13962306a36Sopenharmony_ci * @key: buffer holding the key 14062306a36Sopenharmony_ci * @keylen: length of the key in bytes 14162306a36Sopenharmony_ci * 14262306a36Sopenharmony_ci * The caller provided key is set for the single block cipher referenced by the 14362306a36Sopenharmony_ci * cipher handle. 14462306a36Sopenharmony_ci * 14562306a36Sopenharmony_ci * Note, the key length determines the cipher type. Many block ciphers implement 14662306a36Sopenharmony_ci * different cipher modes depending on the key size, such as AES-128 vs AES-192 14762306a36Sopenharmony_ci * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 14862306a36Sopenharmony_ci * is performed. 14962306a36Sopenharmony_ci * 15062306a36Sopenharmony_ci * Return: 0 if the setting of the key was successful; < 0 if an error occurred 15162306a36Sopenharmony_ci */ 15262306a36Sopenharmony_ciint crypto_cipher_setkey(struct crypto_cipher *tfm, 15362306a36Sopenharmony_ci const u8 *key, unsigned int keylen); 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci/** 15662306a36Sopenharmony_ci * crypto_cipher_encrypt_one() - encrypt one block of plaintext 15762306a36Sopenharmony_ci * @tfm: cipher handle 15862306a36Sopenharmony_ci * @dst: points to the buffer that will be filled with the ciphertext 15962306a36Sopenharmony_ci * @src: buffer holding the plaintext to be encrypted 16062306a36Sopenharmony_ci * 16162306a36Sopenharmony_ci * Invoke the encryption operation of one block. The caller must ensure that 16262306a36Sopenharmony_ci * the plaintext and ciphertext buffers are at least one block in size. 16362306a36Sopenharmony_ci */ 16462306a36Sopenharmony_civoid crypto_cipher_encrypt_one(struct crypto_cipher *tfm, 16562306a36Sopenharmony_ci u8 *dst, const u8 *src); 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci/** 16862306a36Sopenharmony_ci * crypto_cipher_decrypt_one() - decrypt one block of ciphertext 16962306a36Sopenharmony_ci * @tfm: cipher handle 17062306a36Sopenharmony_ci * @dst: points to the buffer that will be filled with the plaintext 17162306a36Sopenharmony_ci * @src: buffer holding the ciphertext to be decrypted 17262306a36Sopenharmony_ci * 17362306a36Sopenharmony_ci * Invoke the decryption operation of one block. The caller must ensure that 17462306a36Sopenharmony_ci * the plaintext and ciphertext buffers are at least one block in size. 17562306a36Sopenharmony_ci */ 17662306a36Sopenharmony_civoid crypto_cipher_decrypt_one(struct crypto_cipher *tfm, 17762306a36Sopenharmony_ci u8 *dst, const u8 *src); 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_cistruct crypto_cipher *crypto_clone_cipher(struct crypto_cipher *cipher); 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_cistruct crypto_cipher_spawn { 18262306a36Sopenharmony_ci struct crypto_spawn base; 18362306a36Sopenharmony_ci}; 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_cistatic inline int crypto_grab_cipher(struct crypto_cipher_spawn *spawn, 18662306a36Sopenharmony_ci struct crypto_instance *inst, 18762306a36Sopenharmony_ci const char *name, u32 type, u32 mask) 18862306a36Sopenharmony_ci{ 18962306a36Sopenharmony_ci type &= ~CRYPTO_ALG_TYPE_MASK; 19062306a36Sopenharmony_ci type |= CRYPTO_ALG_TYPE_CIPHER; 19162306a36Sopenharmony_ci mask |= CRYPTO_ALG_TYPE_MASK; 19262306a36Sopenharmony_ci return crypto_grab_spawn(&spawn->base, inst, name, type, mask); 19362306a36Sopenharmony_ci} 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_cistatic inline void crypto_drop_cipher(struct crypto_cipher_spawn *spawn) 19662306a36Sopenharmony_ci{ 19762306a36Sopenharmony_ci crypto_drop_spawn(&spawn->base); 19862306a36Sopenharmony_ci} 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_cistatic inline struct crypto_alg *crypto_spawn_cipher_alg( 20162306a36Sopenharmony_ci struct crypto_cipher_spawn *spawn) 20262306a36Sopenharmony_ci{ 20362306a36Sopenharmony_ci return spawn->base.alg; 20462306a36Sopenharmony_ci} 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_cistatic inline struct crypto_cipher *crypto_spawn_cipher( 20762306a36Sopenharmony_ci struct crypto_cipher_spawn *spawn) 20862306a36Sopenharmony_ci{ 20962306a36Sopenharmony_ci u32 type = CRYPTO_ALG_TYPE_CIPHER; 21062306a36Sopenharmony_ci u32 mask = CRYPTO_ALG_TYPE_MASK; 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci return __crypto_cipher_cast(crypto_spawn_tfm(&spawn->base, type, mask)); 21362306a36Sopenharmony_ci} 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_cistatic inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm) 21662306a36Sopenharmony_ci{ 21762306a36Sopenharmony_ci return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher; 21862306a36Sopenharmony_ci} 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci#endif 221