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_H 12 #define _CRYPTO_INTERNAL_CIPHER_H 13 14 #include <crypto/algapi.h> 15 16 struct crypto_cipher { 17 struct crypto_tfm base; 18 }; 19 20 /** 21 * DOC: Single Block Cipher API 22 * 23 * The single block cipher API is used with the ciphers of type 24 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto). 25 * 26 * Using the single block cipher API calls, operations with the basic cipher 27 * primitive can be implemented. These cipher primitives exclude any block 28 * chaining operations including IV handling. 29 * 30 * The purpose of this single block cipher API is to support the implementation 31 * of templates or other concepts that only need to perform the cipher operation 32 * on one block at a time. Templates invoke the underlying cipher primitive 33 * block-wise and process either the input or the output data of these cipher 34 * operations. 35 */ 36 37 static 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 handle 44 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 45 * single block cipher 46 * @type: specifies the type of the cipher 47 * @mask: specifies the mask for the cipher 48 * 49 * Allocate a cipher handle for a single block cipher. The returned struct 50 * crypto_cipher is the cipher handle that is required for any subsequent API 51 * invocation for that single block cipher. 52 * 53 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 54 * of an error, PTR_ERR() returns the error code. 55 */ 56 static 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 66 static 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 handle 73 * @tfm: cipher handle to be freed 74 */ 75 static 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 cipher 82 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 83 * single block cipher 84 * @type: specifies the type of the cipher 85 * @mask: specifies the mask for the cipher 86 * 87 * Return: true when the single block cipher is known to the kernel crypto API; 88 * false otherwise 89 */ 90 static 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 cipher 101 * @tfm: cipher handle 102 * 103 * The block size for the single block cipher referenced with the cipher handle 104 * tfm is returned. The caller may use that information to allocate appropriate 105 * memory for the data returned by the encryption or decryption operation 106 * 107 * Return: block size of cipher 108 */ 109 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm) 110 { 111 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm)); 112 } 113 114 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm) 115 { 116 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm)); 117 } 118 119 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm) 120 { 121 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm)); 122 } 123 124 static 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 130 static 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 cipher 138 * @tfm: cipher handle 139 * @key: buffer holding the key 140 * @keylen: length of the key in bytes 141 * 142 * The caller provided key is set for the single block cipher referenced by the 143 * cipher handle. 144 * 145 * Note, the key length determines the cipher type. Many block ciphers implement 146 * different cipher modes depending on the key size, such as AES-128 vs AES-192 147 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 148 * is performed. 149 * 150 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 151 */ 152 int 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 plaintext 157 * @tfm: cipher handle 158 * @dst: points to the buffer that will be filled with the ciphertext 159 * @src: buffer holding the plaintext to be encrypted 160 * 161 * Invoke the encryption operation of one block. The caller must ensure that 162 * the plaintext and ciphertext buffers are at least one block in size. 163 */ 164 void 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 ciphertext 169 * @tfm: cipher handle 170 * @dst: points to the buffer that will be filled with the plaintext 171 * @src: buffer holding the ciphertext to be decrypted 172 * 173 * Invoke the decryption operation of one block. The caller must ensure that 174 * the plaintext and ciphertext buffers are at least one block in size. 175 */ 176 void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, 177 u8 *dst, const u8 *src); 178 179 struct crypto_cipher *crypto_clone_cipher(struct crypto_cipher *cipher); 180 181 struct crypto_cipher_spawn { 182 struct crypto_spawn base; 183 }; 184 185 static 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 195 static inline void crypto_drop_cipher(struct crypto_cipher_spawn *spawn) 196 { 197 crypto_drop_spawn(&spawn->base); 198 } 199 200 static inline struct crypto_alg *crypto_spawn_cipher_alg( 201 struct crypto_cipher_spawn *spawn) 202 { 203 return spawn->base.alg; 204 } 205 206 static 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 215 static 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 #endif 221
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.