xref: /linux/include/crypto/internal/cipher.h (revision cdd5b5a9761fd66d17586e4f4ba6588c70e640ea)
10eb76ba2SArd Biesheuvel /* SPDX-License-Identifier: GPL-2.0-or-later */
20eb76ba2SArd Biesheuvel /*
30eb76ba2SArd Biesheuvel  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
40eb76ba2SArd Biesheuvel  * Copyright (c) 2002 David S. Miller (davem@redhat.com)
50eb76ba2SArd Biesheuvel  * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
60eb76ba2SArd Biesheuvel  *
70eb76ba2SArd Biesheuvel  * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
80eb76ba2SArd Biesheuvel  * and Nettle, by Niels Möller.
90eb76ba2SArd Biesheuvel  */
100eb76ba2SArd Biesheuvel 
110eb76ba2SArd Biesheuvel #ifndef _CRYPTO_INTERNAL_CIPHER_H
120eb76ba2SArd Biesheuvel #define _CRYPTO_INTERNAL_CIPHER_H
130eb76ba2SArd Biesheuvel 
140eb76ba2SArd Biesheuvel #include <crypto/algapi.h>
150eb76ba2SArd Biesheuvel 
160eb76ba2SArd Biesheuvel struct crypto_cipher {
170eb76ba2SArd Biesheuvel 	struct crypto_tfm base;
180eb76ba2SArd Biesheuvel };
190eb76ba2SArd Biesheuvel 
200eb76ba2SArd Biesheuvel /**
210eb76ba2SArd Biesheuvel  * DOC: Single Block Cipher API
220eb76ba2SArd Biesheuvel  *
230eb76ba2SArd Biesheuvel  * The single block cipher API is used with the ciphers of type
240eb76ba2SArd Biesheuvel  * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
250eb76ba2SArd Biesheuvel  *
260eb76ba2SArd Biesheuvel  * Using the single block cipher API calls, operations with the basic cipher
270eb76ba2SArd Biesheuvel  * primitive can be implemented. These cipher primitives exclude any block
280eb76ba2SArd Biesheuvel  * chaining operations including IV handling.
290eb76ba2SArd Biesheuvel  *
300eb76ba2SArd Biesheuvel  * The purpose of this single block cipher API is to support the implementation
310eb76ba2SArd Biesheuvel  * of templates or other concepts that only need to perform the cipher operation
320eb76ba2SArd Biesheuvel  * on one block at a time. Templates invoke the underlying cipher primitive
330eb76ba2SArd Biesheuvel  * block-wise and process either the input or the output data of these cipher
340eb76ba2SArd Biesheuvel  * operations.
350eb76ba2SArd Biesheuvel  */
360eb76ba2SArd Biesheuvel 
__crypto_cipher_cast(struct crypto_tfm * tfm)370eb76ba2SArd Biesheuvel static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
380eb76ba2SArd Biesheuvel {
390eb76ba2SArd Biesheuvel 	return (struct crypto_cipher *)tfm;
400eb76ba2SArd Biesheuvel }
410eb76ba2SArd Biesheuvel 
420eb76ba2SArd Biesheuvel /**
430eb76ba2SArd Biesheuvel  * crypto_alloc_cipher() - allocate single block cipher handle
440eb76ba2SArd Biesheuvel  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
450eb76ba2SArd Biesheuvel  *	     single block cipher
460eb76ba2SArd Biesheuvel  * @type: specifies the type of the cipher
470eb76ba2SArd Biesheuvel  * @mask: specifies the mask for the cipher
480eb76ba2SArd Biesheuvel  *
490eb76ba2SArd Biesheuvel  * Allocate a cipher handle for a single block cipher. The returned struct
500eb76ba2SArd Biesheuvel  * crypto_cipher is the cipher handle that is required for any subsequent API
510eb76ba2SArd Biesheuvel  * invocation for that single block cipher.
520eb76ba2SArd Biesheuvel  *
530eb76ba2SArd Biesheuvel  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
540eb76ba2SArd Biesheuvel  *	   of an error, PTR_ERR() returns the error code.
550eb76ba2SArd Biesheuvel  */
crypto_alloc_cipher(const char * alg_name,u32 type,u32 mask)560eb76ba2SArd Biesheuvel static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
570eb76ba2SArd Biesheuvel 							u32 type, u32 mask)
580eb76ba2SArd Biesheuvel {
590eb76ba2SArd Biesheuvel 	type &= ~CRYPTO_ALG_TYPE_MASK;
600eb76ba2SArd Biesheuvel 	type |= CRYPTO_ALG_TYPE_CIPHER;
610eb76ba2SArd Biesheuvel 	mask |= CRYPTO_ALG_TYPE_MASK;
620eb76ba2SArd Biesheuvel 
630eb76ba2SArd Biesheuvel 	return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
640eb76ba2SArd Biesheuvel }
650eb76ba2SArd Biesheuvel 
crypto_cipher_tfm(struct crypto_cipher * tfm)660eb76ba2SArd Biesheuvel static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
670eb76ba2SArd Biesheuvel {
680eb76ba2SArd Biesheuvel 	return &tfm->base;
690eb76ba2SArd Biesheuvel }
700eb76ba2SArd Biesheuvel 
710eb76ba2SArd Biesheuvel /**
720eb76ba2SArd Biesheuvel  * crypto_free_cipher() - zeroize and free the single block cipher handle
730eb76ba2SArd Biesheuvel  * @tfm: cipher handle to be freed
740eb76ba2SArd Biesheuvel  */
crypto_free_cipher(struct crypto_cipher * tfm)750eb76ba2SArd Biesheuvel static inline void crypto_free_cipher(struct crypto_cipher *tfm)
760eb76ba2SArd Biesheuvel {
770eb76ba2SArd Biesheuvel 	crypto_free_tfm(crypto_cipher_tfm(tfm));
780eb76ba2SArd Biesheuvel }
790eb76ba2SArd Biesheuvel 
800eb76ba2SArd Biesheuvel /**
810eb76ba2SArd Biesheuvel  * crypto_has_cipher() - Search for the availability of a single block cipher
820eb76ba2SArd Biesheuvel  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
830eb76ba2SArd Biesheuvel  *	     single block cipher
840eb76ba2SArd Biesheuvel  * @type: specifies the type of the cipher
850eb76ba2SArd Biesheuvel  * @mask: specifies the mask for the cipher
860eb76ba2SArd Biesheuvel  *
870eb76ba2SArd Biesheuvel  * Return: true when the single block cipher is known to the kernel crypto API;
880eb76ba2SArd Biesheuvel  *	   false otherwise
890eb76ba2SArd Biesheuvel  */
crypto_has_cipher(const char * alg_name,u32 type,u32 mask)900eb76ba2SArd Biesheuvel static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
910eb76ba2SArd Biesheuvel {
920eb76ba2SArd Biesheuvel 	type &= ~CRYPTO_ALG_TYPE_MASK;
930eb76ba2SArd Biesheuvel 	type |= CRYPTO_ALG_TYPE_CIPHER;
940eb76ba2SArd Biesheuvel 	mask |= CRYPTO_ALG_TYPE_MASK;
950eb76ba2SArd Biesheuvel 
960eb76ba2SArd Biesheuvel 	return crypto_has_alg(alg_name, type, mask);
970eb76ba2SArd Biesheuvel }
980eb76ba2SArd Biesheuvel 
990eb76ba2SArd Biesheuvel /**
1000eb76ba2SArd Biesheuvel  * crypto_cipher_blocksize() - obtain block size for cipher
1010eb76ba2SArd Biesheuvel  * @tfm: cipher handle
1020eb76ba2SArd Biesheuvel  *
1030eb76ba2SArd Biesheuvel  * The block size for the single block cipher referenced with the cipher handle
1040eb76ba2SArd Biesheuvel  * tfm is returned. The caller may use that information to allocate appropriate
1050eb76ba2SArd Biesheuvel  * memory for the data returned by the encryption or decryption operation
1060eb76ba2SArd Biesheuvel  *
1070eb76ba2SArd Biesheuvel  * Return: block size of cipher
1080eb76ba2SArd Biesheuvel  */
crypto_cipher_blocksize(struct crypto_cipher * tfm)1090eb76ba2SArd Biesheuvel static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1100eb76ba2SArd Biesheuvel {
1110eb76ba2SArd Biesheuvel 	return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1120eb76ba2SArd Biesheuvel }
1130eb76ba2SArd Biesheuvel 
crypto_cipher_alignmask(struct crypto_cipher * tfm)1140eb76ba2SArd Biesheuvel static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1150eb76ba2SArd Biesheuvel {
1160eb76ba2SArd Biesheuvel 	return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1170eb76ba2SArd Biesheuvel }
1180eb76ba2SArd Biesheuvel 
crypto_cipher_get_flags(struct crypto_cipher * tfm)1190eb76ba2SArd Biesheuvel static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1200eb76ba2SArd Biesheuvel {
1210eb76ba2SArd Biesheuvel 	return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1220eb76ba2SArd Biesheuvel }
1230eb76ba2SArd Biesheuvel 
crypto_cipher_set_flags(struct crypto_cipher * tfm,u32 flags)1240eb76ba2SArd Biesheuvel static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1250eb76ba2SArd Biesheuvel 					   u32 flags)
1260eb76ba2SArd Biesheuvel {
1270eb76ba2SArd Biesheuvel 	crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1280eb76ba2SArd Biesheuvel }
1290eb76ba2SArd Biesheuvel 
crypto_cipher_clear_flags(struct crypto_cipher * tfm,u32 flags)1300eb76ba2SArd Biesheuvel static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1310eb76ba2SArd Biesheuvel 					     u32 flags)
1320eb76ba2SArd Biesheuvel {
1330eb76ba2SArd Biesheuvel 	crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1340eb76ba2SArd Biesheuvel }
1350eb76ba2SArd Biesheuvel 
1360eb76ba2SArd Biesheuvel /**
1370eb76ba2SArd Biesheuvel  * crypto_cipher_setkey() - set key for cipher
1380eb76ba2SArd Biesheuvel  * @tfm: cipher handle
1390eb76ba2SArd Biesheuvel  * @key: buffer holding the key
1400eb76ba2SArd Biesheuvel  * @keylen: length of the key in bytes
1410eb76ba2SArd Biesheuvel  *
1420eb76ba2SArd Biesheuvel  * The caller provided key is set for the single block cipher referenced by the
1430eb76ba2SArd Biesheuvel  * cipher handle.
1440eb76ba2SArd Biesheuvel  *
1450eb76ba2SArd Biesheuvel  * Note, the key length determines the cipher type. Many block ciphers implement
1460eb76ba2SArd Biesheuvel  * different cipher modes depending on the key size, such as AES-128 vs AES-192
1470eb76ba2SArd Biesheuvel  * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1480eb76ba2SArd Biesheuvel  * is performed.
1490eb76ba2SArd Biesheuvel  *
1500eb76ba2SArd Biesheuvel  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1510eb76ba2SArd Biesheuvel  */
1520eb76ba2SArd Biesheuvel int crypto_cipher_setkey(struct crypto_cipher *tfm,
1530eb76ba2SArd Biesheuvel 			 const u8 *key, unsigned int keylen);
1540eb76ba2SArd Biesheuvel 
1550eb76ba2SArd Biesheuvel /**
1560eb76ba2SArd Biesheuvel  * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1570eb76ba2SArd Biesheuvel  * @tfm: cipher handle
1580eb76ba2SArd Biesheuvel  * @dst: points to the buffer that will be filled with the ciphertext
1590eb76ba2SArd Biesheuvel  * @src: buffer holding the plaintext to be encrypted
1600eb76ba2SArd Biesheuvel  *
1610eb76ba2SArd Biesheuvel  * Invoke the encryption operation of one block. The caller must ensure that
1620eb76ba2SArd Biesheuvel  * the plaintext and ciphertext buffers are at least one block in size.
1630eb76ba2SArd Biesheuvel  */
1640eb76ba2SArd Biesheuvel void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1650eb76ba2SArd Biesheuvel 			       u8 *dst, const u8 *src);
1660eb76ba2SArd Biesheuvel 
1670eb76ba2SArd Biesheuvel /**
1680eb76ba2SArd Biesheuvel  * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1690eb76ba2SArd Biesheuvel  * @tfm: cipher handle
1700eb76ba2SArd Biesheuvel  * @dst: points to the buffer that will be filled with the plaintext
1710eb76ba2SArd Biesheuvel  * @src: buffer holding the ciphertext to be decrypted
1720eb76ba2SArd Biesheuvel  *
1730eb76ba2SArd Biesheuvel  * Invoke the decryption operation of one block. The caller must ensure that
1740eb76ba2SArd Biesheuvel  * the plaintext and ciphertext buffers are at least one block in size.
1750eb76ba2SArd Biesheuvel  */
1760eb76ba2SArd Biesheuvel void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1770eb76ba2SArd Biesheuvel 			       u8 *dst, const u8 *src);
1780eb76ba2SArd Biesheuvel 
179*51d8d6d0SHerbert Xu struct crypto_cipher *crypto_clone_cipher(struct crypto_cipher *cipher);
180*51d8d6d0SHerbert Xu 
1810eb76ba2SArd Biesheuvel struct crypto_cipher_spawn {
1820eb76ba2SArd Biesheuvel 	struct crypto_spawn base;
1830eb76ba2SArd Biesheuvel };
1840eb76ba2SArd Biesheuvel 
crypto_grab_cipher(struct crypto_cipher_spawn * spawn,struct crypto_instance * inst,const char * name,u32 type,u32 mask)1850eb76ba2SArd Biesheuvel static inline int crypto_grab_cipher(struct crypto_cipher_spawn *spawn,
1860eb76ba2SArd Biesheuvel 				     struct crypto_instance *inst,
1870eb76ba2SArd Biesheuvel 				     const char *name, u32 type, u32 mask)
1880eb76ba2SArd Biesheuvel {
1890eb76ba2SArd Biesheuvel 	type &= ~CRYPTO_ALG_TYPE_MASK;
1900eb76ba2SArd Biesheuvel 	type |= CRYPTO_ALG_TYPE_CIPHER;
1910eb76ba2SArd Biesheuvel 	mask |= CRYPTO_ALG_TYPE_MASK;
1920eb76ba2SArd Biesheuvel 	return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
1930eb76ba2SArd Biesheuvel }
1940eb76ba2SArd Biesheuvel 
crypto_drop_cipher(struct crypto_cipher_spawn * spawn)1950eb76ba2SArd Biesheuvel static inline void crypto_drop_cipher(struct crypto_cipher_spawn *spawn)
1960eb76ba2SArd Biesheuvel {
1970eb76ba2SArd Biesheuvel 	crypto_drop_spawn(&spawn->base);
1980eb76ba2SArd Biesheuvel }
1990eb76ba2SArd Biesheuvel 
crypto_spawn_cipher_alg(struct crypto_cipher_spawn * spawn)2000eb76ba2SArd Biesheuvel static inline struct crypto_alg *crypto_spawn_cipher_alg(
2010eb76ba2SArd Biesheuvel        struct crypto_cipher_spawn *spawn)
2020eb76ba2SArd Biesheuvel {
2030eb76ba2SArd Biesheuvel 	return spawn->base.alg;
2040eb76ba2SArd Biesheuvel }
2050eb76ba2SArd Biesheuvel 
crypto_spawn_cipher(struct crypto_cipher_spawn * spawn)2060eb76ba2SArd Biesheuvel static inline struct crypto_cipher *crypto_spawn_cipher(
2070eb76ba2SArd Biesheuvel 	struct crypto_cipher_spawn *spawn)
2080eb76ba2SArd Biesheuvel {
2090eb76ba2SArd Biesheuvel 	u32 type = CRYPTO_ALG_TYPE_CIPHER;
2100eb76ba2SArd Biesheuvel 	u32 mask = CRYPTO_ALG_TYPE_MASK;
2110eb76ba2SArd Biesheuvel 
2120eb76ba2SArd Biesheuvel 	return __crypto_cipher_cast(crypto_spawn_tfm(&spawn->base, type, mask));
2130eb76ba2SArd Biesheuvel }
2140eb76ba2SArd Biesheuvel 
crypto_cipher_alg(struct crypto_cipher * tfm)2150eb76ba2SArd Biesheuvel static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
2160eb76ba2SArd Biesheuvel {
2170eb76ba2SArd Biesheuvel 	return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
2180eb76ba2SArd Biesheuvel }
2190eb76ba2SArd Biesheuvel 
2200eb76ba2SArd Biesheuvel #endif
221