1 /* SPDX-License-Identifier: GPL-2.0-or-later * 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 2 /* 3 * Scatterlist Cryptographic API. 3 * Scatterlist Cryptographic API. 4 * 4 * 5 * Copyright (c) 2002 James Morris <jmorris@in 5 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 6 * Copyright (c) 2002 David S. Miller (davem@r 6 * Copyright (c) 2002 David S. Miller (davem@redhat.com) 7 * Copyright (c) 2005 Herbert Xu <herbert@gond 7 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> 8 * 8 * 9 * Portions derived from Cryptoapi, by Alexand 9 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no> 10 * and Nettle, by Niels Möller. 10 * and Nettle, by Niels Möller. 11 */ 11 */ 12 #ifndef _LINUX_CRYPTO_H 12 #ifndef _LINUX_CRYPTO_H 13 #define _LINUX_CRYPTO_H 13 #define _LINUX_CRYPTO_H 14 14 15 #include <linux/completion.h> !! 15 #include <linux/atomic.h> >> 16 #include <linux/kernel.h> >> 17 #include <linux/list.h> >> 18 #include <linux/bug.h> 16 #include <linux/refcount.h> 19 #include <linux/refcount.h> 17 #include <linux/slab.h> 20 #include <linux/slab.h> 18 #include <linux/types.h> !! 21 #include <linux/completion.h> >> 22 >> 23 /* >> 24 * Autoloaded crypto modules should only use a prefixed name to avoid allowing >> 25 * arbitrary modules to be loaded. Loading from userspace may still need the >> 26 * unprefixed names, so retains those aliases as well. >> 27 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3 >> 28 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro >> 29 * expands twice on the same line. Instead, use a separate base name for the >> 30 * alias. >> 31 */ >> 32 #define MODULE_ALIAS_CRYPTO(name) \ >> 33 __MODULE_INFO(alias, alias_userspace, name); \ >> 34 __MODULE_INFO(alias, alias_crypto, "crypto-" name) 19 35 20 /* 36 /* 21 * Algorithm masks and types. 37 * Algorithm masks and types. 22 */ 38 */ 23 #define CRYPTO_ALG_TYPE_MASK 0x0000 39 #define CRYPTO_ALG_TYPE_MASK 0x0000000f 24 #define CRYPTO_ALG_TYPE_CIPHER 0x0000 40 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001 25 #define CRYPTO_ALG_TYPE_COMPRESS 0x0000 41 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002 26 #define CRYPTO_ALG_TYPE_AEAD 0x0000 42 #define CRYPTO_ALG_TYPE_AEAD 0x00000003 27 #define CRYPTO_ALG_TYPE_LSKCIPHER 0x0000 << 28 #define CRYPTO_ALG_TYPE_SKCIPHER 0x0000 43 #define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005 29 #define CRYPTO_ALG_TYPE_AKCIPHER 0x0000 << 30 #define CRYPTO_ALG_TYPE_SIG 0x0000 << 31 #define CRYPTO_ALG_TYPE_KPP 0x0000 44 #define CRYPTO_ALG_TYPE_KPP 0x00000008 32 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000 45 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a 33 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000 46 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b 34 #define CRYPTO_ALG_TYPE_RNG 0x0000 47 #define CRYPTO_ALG_TYPE_RNG 0x0000000c >> 48 #define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d 35 #define CRYPTO_ALG_TYPE_HASH 0x0000 49 #define CRYPTO_ALG_TYPE_HASH 0x0000000e 36 #define CRYPTO_ALG_TYPE_SHASH 0x0000 50 #define CRYPTO_ALG_TYPE_SHASH 0x0000000e 37 #define CRYPTO_ALG_TYPE_AHASH 0x0000 51 #define CRYPTO_ALG_TYPE_AHASH 0x0000000f 38 52 >> 53 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e >> 54 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e 39 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000 55 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e 40 56 41 #define CRYPTO_ALG_LARVAL 0x0000 57 #define CRYPTO_ALG_LARVAL 0x00000010 42 #define CRYPTO_ALG_DEAD 0x0000 58 #define CRYPTO_ALG_DEAD 0x00000020 43 #define CRYPTO_ALG_DYING 0x0000 59 #define CRYPTO_ALG_DYING 0x00000040 44 #define CRYPTO_ALG_ASYNC 0x0000 60 #define CRYPTO_ALG_ASYNC 0x00000080 45 61 46 /* 62 /* 47 * Set if the algorithm (or an algorithm which 63 * Set if the algorithm (or an algorithm which it uses) requires another 48 * algorithm of the same type to handle corner 64 * algorithm of the same type to handle corner cases. 49 */ 65 */ 50 #define CRYPTO_ALG_NEED_FALLBACK 0x0000 66 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100 51 67 52 /* 68 /* 53 * Set if the algorithm has passed automated r 69 * Set if the algorithm has passed automated run-time testing. Note that 54 * if there is no run-time testing for a given 70 * if there is no run-time testing for a given algorithm it is considered 55 * to have passed. 71 * to have passed. 56 */ 72 */ 57 73 58 #define CRYPTO_ALG_TESTED 0x0000 74 #define CRYPTO_ALG_TESTED 0x00000400 59 75 60 /* 76 /* 61 * Set if the algorithm is an instance that is 77 * Set if the algorithm is an instance that is built from templates. 62 */ 78 */ 63 #define CRYPTO_ALG_INSTANCE 0x0000 79 #define CRYPTO_ALG_INSTANCE 0x00000800 64 80 65 /* Set this bit if the algorithm provided is h 81 /* Set this bit if the algorithm provided is hardware accelerated but 66 * not available to userspace via instruction 82 * not available to userspace via instruction set or so. 67 */ 83 */ 68 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x0000 84 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000 69 85 70 /* 86 /* 71 * Mark a cipher as a service implementation o 87 * Mark a cipher as a service implementation only usable by another 72 * cipher and never by a normal user of the ke 88 * cipher and never by a normal user of the kernel crypto API 73 */ 89 */ 74 #define CRYPTO_ALG_INTERNAL 0x0000 90 #define CRYPTO_ALG_INTERNAL 0x00002000 75 91 76 /* 92 /* 77 * Set if the algorithm has a ->setkey() metho 93 * Set if the algorithm has a ->setkey() method but can be used without 78 * calling it first, i.e. there is a default k 94 * calling it first, i.e. there is a default key. 79 */ 95 */ 80 #define CRYPTO_ALG_OPTIONAL_KEY 0x0000 96 #define CRYPTO_ALG_OPTIONAL_KEY 0x00004000 81 97 82 /* 98 /* 83 * Don't trigger module loading 99 * Don't trigger module loading 84 */ 100 */ 85 #define CRYPTO_NOLOAD 0x0000 101 #define CRYPTO_NOLOAD 0x00008000 86 102 87 /* 103 /* 88 * The algorithm may allocate memory during re 104 * The algorithm may allocate memory during request processing, i.e. during 89 * encryption, decryption, or hashing. Users 105 * encryption, decryption, or hashing. Users can request an algorithm with this 90 * flag unset if they can't handle memory allo 106 * flag unset if they can't handle memory allocation failures. 91 * 107 * 92 * This flag is currently only implemented for 108 * This flag is currently only implemented for algorithms of type "skcipher", 93 * "aead", "ahash", "shash", and "cipher". Al 109 * "aead", "ahash", "shash", and "cipher". Algorithms of other types might not 94 * have this flag set even if they allocate me 110 * have this flag set even if they allocate memory. 95 * 111 * 96 * In some edge cases, algorithms can allocate 112 * In some edge cases, algorithms can allocate memory regardless of this flag. 97 * To avoid these cases, users must obey the f 113 * To avoid these cases, users must obey the following usage constraints: 98 * skcipher: 114 * skcipher: 99 * - The IV buffer and all scatterlist el 115 * - The IV buffer and all scatterlist elements must be aligned to the 100 * algorithm's alignmask. 116 * algorithm's alignmask. 101 * - If the data were to be divided into 117 * - If the data were to be divided into chunks of size 102 * crypto_skcipher_walksize() (with any 118 * crypto_skcipher_walksize() (with any remainder going at the end), no 103 * chunk can cross a page boundary or a 119 * chunk can cross a page boundary or a scatterlist element boundary. 104 * aead: 120 * aead: 105 * - The IV buffer and all scatterlist el 121 * - The IV buffer and all scatterlist elements must be aligned to the 106 * algorithm's alignmask. 122 * algorithm's alignmask. 107 * - The first scatterlist element must c 123 * - The first scatterlist element must contain all the associated data, 108 * and its pages must be !PageHighMem. 124 * and its pages must be !PageHighMem. 109 * - If the plaintext/ciphertext were to 125 * - If the plaintext/ciphertext were to be divided into chunks of size 110 * crypto_aead_walksize() (with the rem 126 * crypto_aead_walksize() (with the remainder going at the end), no chunk 111 * can cross a page boundary or a scatt 127 * can cross a page boundary or a scatterlist element boundary. 112 * ahash: 128 * ahash: >> 129 * - The result buffer must be aligned to the algorithm's alignmask. 113 * - crypto_ahash_finup() must not be use 130 * - crypto_ahash_finup() must not be used unless the algorithm implements 114 * ->finup() natively. 131 * ->finup() natively. 115 */ 132 */ 116 #define CRYPTO_ALG_ALLOCATES_MEMORY 0x0001 133 #define CRYPTO_ALG_ALLOCATES_MEMORY 0x00010000 117 134 118 /* 135 /* 119 * Mark an algorithm as a service implementati << 120 * template and never by a normal user of the << 121 * This is intended to be used by algorithms t << 122 * not FIPS-approved but may instead be used t << 123 * a FIPS-approved algorithm (e.g., dh vs. ffd << 124 */ << 125 #define CRYPTO_ALG_FIPS_INTERNAL 0x0002 << 126 << 127 /* << 128 * Transform masks and values (for crt_flags). 136 * Transform masks and values (for crt_flags). 129 */ 137 */ 130 #define CRYPTO_TFM_NEED_KEY 0x0000 138 #define CRYPTO_TFM_NEED_KEY 0x00000001 131 139 132 #define CRYPTO_TFM_REQ_MASK 0x000f 140 #define CRYPTO_TFM_REQ_MASK 0x000fff00 133 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x0000 141 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x00000100 134 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x0000 142 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 135 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x0000 143 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400 136 144 137 /* 145 /* 138 * Miscellaneous stuff. 146 * Miscellaneous stuff. 139 */ 147 */ 140 #define CRYPTO_MAX_ALG_NAME 128 148 #define CRYPTO_MAX_ALG_NAME 128 141 149 142 /* 150 /* 143 * The macro CRYPTO_MINALIGN_ATTR (along with 151 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual 144 * declaration) is used to ensure that the cry 152 * declaration) is used to ensure that the crypto_tfm context structure is 145 * aligned correctly for the given architectur 153 * aligned correctly for the given architecture so that there are no alignment 146 * faults for C data types. On architectures !! 154 * faults for C data types. In particular, this is required on platforms such 147 * DMA, such as ARM or arm64, it also takes in !! 155 * as arm where pointers are 32-bit aligned but there are data types such as 148 * that is required to ensure that the context !! 156 * u64 which require 64-bit alignment. 149 * cachelines with the rest of the struct. Thi << 150 * maintenance for non-coherent DMA (cache inv << 151 * affect data that may be accessed by the CPU << 152 */ 157 */ 153 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN 158 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN 154 159 155 #define CRYPTO_MINALIGN_ATTR __attribute__ ((_ 160 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN))) 156 161 >> 162 struct scatterlist; >> 163 struct crypto_async_request; 157 struct crypto_tfm; 164 struct crypto_tfm; 158 struct crypto_type; 165 struct crypto_type; 159 struct module; << 160 166 161 typedef void (*crypto_completion_t)(void *req, !! 167 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err); 162 168 163 /** 169 /** 164 * DOC: Block Cipher Context Data Structures 170 * DOC: Block Cipher Context Data Structures 165 * 171 * 166 * These data structures define the operating 172 * These data structures define the operating context for each block cipher 167 * type. 173 * type. 168 */ 174 */ 169 175 170 struct crypto_async_request { 176 struct crypto_async_request { 171 struct list_head list; 177 struct list_head list; 172 crypto_completion_t complete; 178 crypto_completion_t complete; 173 void *data; 179 void *data; 174 struct crypto_tfm *tfm; 180 struct crypto_tfm *tfm; 175 181 176 u32 flags; 182 u32 flags; 177 }; 183 }; 178 184 179 /** 185 /** 180 * DOC: Block Cipher Algorithm Definitions 186 * DOC: Block Cipher Algorithm Definitions 181 * 187 * 182 * These data structures define modular crypto 188 * These data structures define modular crypto algorithm implementations, 183 * managed via crypto_register_alg() and crypt 189 * managed via crypto_register_alg() and crypto_unregister_alg(). 184 */ 190 */ 185 191 186 /** 192 /** 187 * struct cipher_alg - single-block symmetric 193 * struct cipher_alg - single-block symmetric ciphers definition 188 * @cia_min_keysize: Minimum key size supporte 194 * @cia_min_keysize: Minimum key size supported by the transformation. This is 189 * the smallest key length s 195 * the smallest key length supported by this transformation 190 * algorithm. This must be s 196 * algorithm. This must be set to one of the pre-defined 191 * values as this is not har 197 * values as this is not hardware specific. Possible values 192 * for this field can be fou 198 * for this field can be found via git grep "_MIN_KEY_SIZE" 193 * include/crypto/ 199 * include/crypto/ 194 * @cia_max_keysize: Maximum key size supporte 200 * @cia_max_keysize: Maximum key size supported by the transformation. This is 195 * the largest key length sup 201 * the largest key length supported by this transformation 196 * algorithm. This must be se 202 * algorithm. This must be set to one of the pre-defined values 197 * as this is not hardware sp 203 * as this is not hardware specific. Possible values for this 198 * field can be found via git 204 * field can be found via git grep "_MAX_KEY_SIZE" 199 * include/crypto/ 205 * include/crypto/ 200 * @cia_setkey: Set key for the transformation 206 * @cia_setkey: Set key for the transformation. This function is used to either 201 * program a supplied key into th 207 * program a supplied key into the hardware or store the key in the 202 * transformation context for pro 208 * transformation context for programming it later. Note that this 203 * function does modify the trans 209 * function does modify the transformation context. This function 204 * can be called multiple times d 210 * can be called multiple times during the existence of the 205 * transformation object, so one 211 * transformation object, so one must make sure the key is properly 206 * reprogrammed into the hardware 212 * reprogrammed into the hardware. This function is also 207 * responsible for checking the k 213 * responsible for checking the key length for validity. 208 * @cia_encrypt: Encrypt a single block. This 214 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a 209 * single block of data, which m 215 * single block of data, which must be @cra_blocksize big. This 210 * always operates on a full @cr 216 * always operates on a full @cra_blocksize and it is not possible 211 * to encrypt a block of smaller 217 * to encrypt a block of smaller size. The supplied buffers must 212 * therefore also be at least of 218 * therefore also be at least of @cra_blocksize size. Both the 213 * input and output buffers are 219 * input and output buffers are always aligned to @cra_alignmask. 214 * In case either of the input o 220 * In case either of the input or output buffer supplied by user 215 * of the crypto API is not alig 221 * of the crypto API is not aligned to @cra_alignmask, the crypto 216 * API will re-align the buffers 222 * API will re-align the buffers. The re-alignment means that a 217 * new buffer will be allocated, 223 * new buffer will be allocated, the data will be copied into the 218 * new buffer, then the processi 224 * new buffer, then the processing will happen on the new buffer, 219 * then the data will be copied 225 * then the data will be copied back into the original buffer and 220 * finally the new buffer will b 226 * finally the new buffer will be freed. In case a software 221 * fallback was put in place in 227 * fallback was put in place in the @cra_init call, this function 222 * might need to use the fallbac 228 * might need to use the fallback if the algorithm doesn't support 223 * all of the key sizes. In case 229 * all of the key sizes. In case the key was stored in 224 * transformation context, the k 230 * transformation context, the key might need to be re-programmed 225 * into the hardware in this fun 231 * into the hardware in this function. This function shall not 226 * modify the transformation con 232 * modify the transformation context, as this function may be 227 * called in parallel with the s 233 * called in parallel with the same transformation object. 228 * @cia_decrypt: Decrypt a single block. This 234 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to 229 * @cia_encrypt, and the conditi 235 * @cia_encrypt, and the conditions are exactly the same. 230 * 236 * 231 * All fields are mandatory and must be filled 237 * All fields are mandatory and must be filled. 232 */ 238 */ 233 struct cipher_alg { 239 struct cipher_alg { 234 unsigned int cia_min_keysize; 240 unsigned int cia_min_keysize; 235 unsigned int cia_max_keysize; 241 unsigned int cia_max_keysize; 236 int (*cia_setkey)(struct crypto_tfm *t 242 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key, 237 unsigned int keylen) 243 unsigned int keylen); 238 void (*cia_encrypt)(struct crypto_tfm 244 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 239 void (*cia_decrypt)(struct crypto_tfm 245 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 240 }; 246 }; 241 247 242 /** 248 /** 243 * struct compress_alg - compression/decompres 249 * struct compress_alg - compression/decompression algorithm 244 * @coa_compress: Compress a buffer of specifi 250 * @coa_compress: Compress a buffer of specified length, storing the resulting 245 * data in the specified buffer 251 * data in the specified buffer. Return the length of the 246 * compressed data in dlen. 252 * compressed data in dlen. 247 * @coa_decompress: Decompress the source buff 253 * @coa_decompress: Decompress the source buffer, storing the uncompressed 248 * data in the specified buff 254 * data in the specified buffer. The length of the data is 249 * returned in dlen. 255 * returned in dlen. 250 * 256 * 251 * All fields are mandatory. 257 * All fields are mandatory. 252 */ 258 */ 253 struct compress_alg { 259 struct compress_alg { 254 int (*coa_compress)(struct crypto_tfm 260 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src, 255 unsigned int slen, 261 unsigned int slen, u8 *dst, unsigned int *dlen); 256 int (*coa_decompress)(struct crypto_tf 262 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src, 257 unsigned int sle 263 unsigned int slen, u8 *dst, unsigned int *dlen); 258 }; 264 }; 259 265 >> 266 #ifdef CONFIG_CRYPTO_STATS >> 267 /* >> 268 * struct crypto_istat_aead - statistics for AEAD algorithm >> 269 * @encrypt_cnt: number of encrypt requests >> 270 * @encrypt_tlen: total data size handled by encrypt requests >> 271 * @decrypt_cnt: number of decrypt requests >> 272 * @decrypt_tlen: total data size handled by decrypt requests >> 273 * @err_cnt: number of error for AEAD requests >> 274 */ >> 275 struct crypto_istat_aead { >> 276 atomic64_t encrypt_cnt; >> 277 atomic64_t encrypt_tlen; >> 278 atomic64_t decrypt_cnt; >> 279 atomic64_t decrypt_tlen; >> 280 atomic64_t err_cnt; >> 281 }; >> 282 >> 283 /* >> 284 * struct crypto_istat_akcipher - statistics for akcipher algorithm >> 285 * @encrypt_cnt: number of encrypt requests >> 286 * @encrypt_tlen: total data size handled by encrypt requests >> 287 * @decrypt_cnt: number of decrypt requests >> 288 * @decrypt_tlen: total data size handled by decrypt requests >> 289 * @verify_cnt: number of verify operation >> 290 * @sign_cnt: number of sign requests >> 291 * @err_cnt: number of error for akcipher requests >> 292 */ >> 293 struct crypto_istat_akcipher { >> 294 atomic64_t encrypt_cnt; >> 295 atomic64_t encrypt_tlen; >> 296 atomic64_t decrypt_cnt; >> 297 atomic64_t decrypt_tlen; >> 298 atomic64_t verify_cnt; >> 299 atomic64_t sign_cnt; >> 300 atomic64_t err_cnt; >> 301 }; >> 302 >> 303 /* >> 304 * struct crypto_istat_cipher - statistics for cipher algorithm >> 305 * @encrypt_cnt: number of encrypt requests >> 306 * @encrypt_tlen: total data size handled by encrypt requests >> 307 * @decrypt_cnt: number of decrypt requests >> 308 * @decrypt_tlen: total data size handled by decrypt requests >> 309 * @err_cnt: number of error for cipher requests >> 310 */ >> 311 struct crypto_istat_cipher { >> 312 atomic64_t encrypt_cnt; >> 313 atomic64_t encrypt_tlen; >> 314 atomic64_t decrypt_cnt; >> 315 atomic64_t decrypt_tlen; >> 316 atomic64_t err_cnt; >> 317 }; >> 318 >> 319 /* >> 320 * struct crypto_istat_compress - statistics for compress algorithm >> 321 * @compress_cnt: number of compress requests >> 322 * @compress_tlen: total data size handled by compress requests >> 323 * @decompress_cnt: number of decompress requests >> 324 * @decompress_tlen: total data size handled by decompress requests >> 325 * @err_cnt: number of error for compress requests >> 326 */ >> 327 struct crypto_istat_compress { >> 328 atomic64_t compress_cnt; >> 329 atomic64_t compress_tlen; >> 330 atomic64_t decompress_cnt; >> 331 atomic64_t decompress_tlen; >> 332 atomic64_t err_cnt; >> 333 }; >> 334 >> 335 /* >> 336 * struct crypto_istat_hash - statistics for has algorithm >> 337 * @hash_cnt: number of hash requests >> 338 * @hash_tlen: total data size hashed >> 339 * @err_cnt: number of error for hash requests >> 340 */ >> 341 struct crypto_istat_hash { >> 342 atomic64_t hash_cnt; >> 343 atomic64_t hash_tlen; >> 344 atomic64_t err_cnt; >> 345 }; >> 346 >> 347 /* >> 348 * struct crypto_istat_kpp - statistics for KPP algorithm >> 349 * @setsecret_cnt: number of setsecrey operation >> 350 * @generate_public_key_cnt: number of generate_public_key operation >> 351 * @compute_shared_secret_cnt: number of compute_shared_secret operation >> 352 * @err_cnt: number of error for KPP requests >> 353 */ >> 354 struct crypto_istat_kpp { >> 355 atomic64_t setsecret_cnt; >> 356 atomic64_t generate_public_key_cnt; >> 357 atomic64_t compute_shared_secret_cnt; >> 358 atomic64_t err_cnt; >> 359 }; >> 360 >> 361 /* >> 362 * struct crypto_istat_rng: statistics for RNG algorithm >> 363 * @generate_cnt: number of RNG generate requests >> 364 * @generate_tlen: total data size of generated data by the RNG >> 365 * @seed_cnt: number of times the RNG was seeded >> 366 * @err_cnt: number of error for RNG requests >> 367 */ >> 368 struct crypto_istat_rng { >> 369 atomic64_t generate_cnt; >> 370 atomic64_t generate_tlen; >> 371 atomic64_t seed_cnt; >> 372 atomic64_t err_cnt; >> 373 }; >> 374 #endif /* CONFIG_CRYPTO_STATS */ >> 375 260 #define cra_cipher cra_u.cipher 376 #define cra_cipher cra_u.cipher 261 #define cra_compress cra_u.compress 377 #define cra_compress cra_u.compress 262 378 263 /** 379 /** 264 * struct crypto_alg - definition of a cryptog 380 * struct crypto_alg - definition of a cryptograpic cipher algorithm 265 * @cra_flags: Flags describing this transform 381 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h 266 * CRYPTO_ALG_* flags for the flag 382 * CRYPTO_ALG_* flags for the flags which go in here. Those are 267 * used for fine-tuning the descri 383 * used for fine-tuning the description of the transformation 268 * algorithm. 384 * algorithm. 269 * @cra_blocksize: Minimum block size of this 385 * @cra_blocksize: Minimum block size of this transformation. The size in bytes 270 * of the smallest possible un 386 * of the smallest possible unit which can be transformed with 271 * this algorithm. The users m 387 * this algorithm. The users must respect this value. 272 * In case of HASH transformat 388 * In case of HASH transformation, it is possible for a smaller 273 * block than @cra_blocksize t 389 * block than @cra_blocksize to be passed to the crypto API for 274 * transformation, in case of 390 * transformation, in case of any other transformation type, an 275 * error will be returned upon 391 * error will be returned upon any attempt to transform smaller 276 * than @cra_blocksize chunks. 392 * than @cra_blocksize chunks. 277 * @cra_ctxsize: Size of the operational conte 393 * @cra_ctxsize: Size of the operational context of the transformation. This 278 * value informs the kernel cryp 394 * value informs the kernel crypto API about the memory size 279 * needed to be allocated for th 395 * needed to be allocated for the transformation context. 280 * @cra_alignmask: For cipher, skcipher, lskci !! 396 * @cra_alignmask: Alignment mask for the input and output data buffer. The data 281 * 1 less than the alignment, !! 397 * buffer containing the input data for the algorithm must be 282 * implementation requires for !! 398 * aligned to this alignment mask. The data buffer for the 283 * the crypto API is invoked w !! 399 * output data must be aligned to this alignment mask. Note that 284 * to this alignment, the cryp !! 400 * the Crypto API will do the re-alignment in software, but 285 * appropriately aligned tempo !! 401 * only under special conditions and there is a performance hit. 286 * the algorithm needs. (For !! 402 * The re-alignment happens at these occasions for different 287 * the algorithm uses the skci !! 403 * @cra_u types: cipher -- For both input data and output data 288 * misalignment handling carri !! 404 * buffer; ahash -- For output hash destination buf; shash -- 289 * preferred that algorithms d !! 405 * For output hash destination buf. 290 * Also, crypto API users may !! 406 * This is needed on hardware which is flawed by design and 291 * to the alignmask of the alg !! 407 * cannot pick data from arbitrary addresses. 292 * avoid the API having to rea << 293 * not supported for hash algo << 294 * @cra_priority: Priority of this transformat 408 * @cra_priority: Priority of this transformation implementation. In case 295 * multiple transformations wit 409 * multiple transformations with same @cra_name are available to 296 * the Crypto API, the kernel w 410 * the Crypto API, the kernel will use the one with highest 297 * @cra_priority. 411 * @cra_priority. 298 * @cra_name: Generic name (usable by multiple 412 * @cra_name: Generic name (usable by multiple implementations) of the 299 * transformation algorithm. This i 413 * transformation algorithm. This is the name of the transformation 300 * itself. This field is used by th 414 * itself. This field is used by the kernel when looking up the 301 * providers of particular transfor 415 * providers of particular transformation. 302 * @cra_driver_name: Unique name of the transf 416 * @cra_driver_name: Unique name of the transformation provider. This is the 303 * name of the provider of t 417 * name of the provider of the transformation. This can be any 304 * arbitrary value, but in t 418 * arbitrary value, but in the usual case, this contains the 305 * name of the chip or provi 419 * name of the chip or provider and the name of the 306 * transformation algorithm. 420 * transformation algorithm. 307 * @cra_type: Type of the cryptographic transf 421 * @cra_type: Type of the cryptographic transformation. This is a pointer to 308 * struct crypto_type, which implem 422 * struct crypto_type, which implements callbacks common for all 309 * transformation types. There are 423 * transformation types. There are multiple options, such as 310 * &crypto_skcipher_type, &crypto_a 424 * &crypto_skcipher_type, &crypto_ahash_type, &crypto_rng_type. 311 * This field might be empty. In th 425 * This field might be empty. In that case, there are no common 312 * callbacks. This is the case for: 426 * callbacks. This is the case for: cipher, compress, shash. 313 * @cra_u: Callbacks implementing the transfor 427 * @cra_u: Callbacks implementing the transformation. This is a union of 314 * multiple structures. Depending on t 428 * multiple structures. Depending on the type of transformation selected 315 * by @cra_type and @cra_flags above, 429 * by @cra_type and @cra_flags above, the associated structure must be 316 * filled with callbacks. This field m 430 * filled with callbacks. This field might be empty. This is the case 317 * for ahash, shash. 431 * for ahash, shash. 318 * @cra_init: Initialize the cryptographic tra 432 * @cra_init: Initialize the cryptographic transformation object. This function 319 * is used to initialize the crypto 433 * is used to initialize the cryptographic transformation object. 320 * This function is called only onc 434 * This function is called only once at the instantiation time, right 321 * after the transformation context 435 * after the transformation context was allocated. In case the 322 * cryptographic hardware has some 436 * cryptographic hardware has some special requirements which need to 323 * be handled by software, this fun 437 * be handled by software, this function shall check for the precise 324 * requirement of the transformatio 438 * requirement of the transformation and put any software fallbacks 325 * in place. 439 * in place. 326 * @cra_exit: Deinitialize the cryptographic t 440 * @cra_exit: Deinitialize the cryptographic transformation object. This is a 327 * counterpart to @cra_init, used t 441 * counterpart to @cra_init, used to remove various changes set in 328 * @cra_init. 442 * @cra_init. 329 * @cra_u.cipher: Union member which contains 443 * @cra_u.cipher: Union member which contains a single-block symmetric cipher 330 * definition. See @struct @cip 444 * definition. See @struct @cipher_alg. 331 * @cra_u.compress: Union member which contain 445 * @cra_u.compress: Union member which contains a (de)compression algorithm. 332 * See @struct @compress_alg. 446 * See @struct @compress_alg. 333 * @cra_module: Owner of this transformation i 447 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE 334 * @cra_list: internally used 448 * @cra_list: internally used 335 * @cra_users: internally used 449 * @cra_users: internally used 336 * @cra_refcnt: internally used 450 * @cra_refcnt: internally used 337 * @cra_destroy: internally used 451 * @cra_destroy: internally used 338 * 452 * >> 453 * @stats: union of all possible crypto_istat_xxx structures >> 454 * @stats.aead: statistics for AEAD algorithm >> 455 * @stats.akcipher: statistics for akcipher algorithm >> 456 * @stats.cipher: statistics for cipher algorithm >> 457 * @stats.compress: statistics for compress algorithm >> 458 * @stats.hash: statistics for hash algorithm >> 459 * @stats.rng: statistics for rng algorithm >> 460 * @stats.kpp: statistics for KPP algorithm >> 461 * 339 * The struct crypto_alg describes a generic C 462 * The struct crypto_alg describes a generic Crypto API algorithm and is common 340 * for all of the transformations. Any variabl 463 * for all of the transformations. Any variable not documented here shall not 341 * be used by a cipher implementation as it is 464 * be used by a cipher implementation as it is internal to the Crypto API. 342 */ 465 */ 343 struct crypto_alg { 466 struct crypto_alg { 344 struct list_head cra_list; 467 struct list_head cra_list; 345 struct list_head cra_users; 468 struct list_head cra_users; 346 469 347 u32 cra_flags; 470 u32 cra_flags; 348 unsigned int cra_blocksize; 471 unsigned int cra_blocksize; 349 unsigned int cra_ctxsize; 472 unsigned int cra_ctxsize; 350 unsigned int cra_alignmask; 473 unsigned int cra_alignmask; 351 474 352 int cra_priority; 475 int cra_priority; 353 refcount_t cra_refcnt; 476 refcount_t cra_refcnt; 354 477 355 char cra_name[CRYPTO_MAX_ALG_NAME]; 478 char cra_name[CRYPTO_MAX_ALG_NAME]; 356 char cra_driver_name[CRYPTO_MAX_ALG_NA 479 char cra_driver_name[CRYPTO_MAX_ALG_NAME]; 357 480 358 const struct crypto_type *cra_type; 481 const struct crypto_type *cra_type; 359 482 360 union { 483 union { 361 struct cipher_alg cipher; 484 struct cipher_alg cipher; 362 struct compress_alg compress; 485 struct compress_alg compress; 363 } cra_u; 486 } cra_u; 364 487 365 int (*cra_init)(struct crypto_tfm *tfm 488 int (*cra_init)(struct crypto_tfm *tfm); 366 void (*cra_exit)(struct crypto_tfm *tf 489 void (*cra_exit)(struct crypto_tfm *tfm); 367 void (*cra_destroy)(struct crypto_alg 490 void (*cra_destroy)(struct crypto_alg *alg); 368 491 369 struct module *cra_module; 492 struct module *cra_module; >> 493 >> 494 #ifdef CONFIG_CRYPTO_STATS >> 495 union { >> 496 struct crypto_istat_aead aead; >> 497 struct crypto_istat_akcipher akcipher; >> 498 struct crypto_istat_cipher cipher; >> 499 struct crypto_istat_compress compress; >> 500 struct crypto_istat_hash hash; >> 501 struct crypto_istat_rng rng; >> 502 struct crypto_istat_kpp kpp; >> 503 } stats; >> 504 #endif /* CONFIG_CRYPTO_STATS */ >> 505 370 } CRYPTO_MINALIGN_ATTR; 506 } CRYPTO_MINALIGN_ATTR; 371 507 >> 508 #ifdef CONFIG_CRYPTO_STATS >> 509 void crypto_stats_init(struct crypto_alg *alg); >> 510 void crypto_stats_get(struct crypto_alg *alg); >> 511 void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret); >> 512 void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret); >> 513 void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg); >> 514 void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg); >> 515 void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg); >> 516 void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg); >> 517 void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg); >> 518 void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg); >> 519 void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg); >> 520 void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg); >> 521 void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret); >> 522 void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret); >> 523 void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret); >> 524 void crypto_stats_rng_seed(struct crypto_alg *alg, int ret); >> 525 void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret); >> 526 void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg); >> 527 void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg); >> 528 #else >> 529 static inline void crypto_stats_init(struct crypto_alg *alg) >> 530 {} >> 531 static inline void crypto_stats_get(struct crypto_alg *alg) >> 532 {} >> 533 static inline void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret) >> 534 {} >> 535 static inline void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret) >> 536 {} >> 537 static inline void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg) >> 538 {} >> 539 static inline void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg) >> 540 {} >> 541 static inline void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg) >> 542 {} >> 543 static inline void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg) >> 544 {} >> 545 static inline void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg) >> 546 {} >> 547 static inline void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg) >> 548 {} >> 549 static inline void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg) >> 550 {} >> 551 static inline void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg) >> 552 {} >> 553 static inline void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret) >> 554 {} >> 555 static inline void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret) >> 556 {} >> 557 static inline void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret) >> 558 {} >> 559 static inline void crypto_stats_rng_seed(struct crypto_alg *alg, int ret) >> 560 {} >> 561 static inline void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret) >> 562 {} >> 563 static inline void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg) >> 564 {} >> 565 static inline void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg) >> 566 {} >> 567 #endif 372 /* 568 /* 373 * A helper struct for waiting for completion 569 * A helper struct for waiting for completion of async crypto ops 374 */ 570 */ 375 struct crypto_wait { 571 struct crypto_wait { 376 struct completion completion; 572 struct completion completion; 377 int err; 573 int err; 378 }; 574 }; 379 575 380 /* 576 /* 381 * Macro for declaring a crypto op async wait 577 * Macro for declaring a crypto op async wait object on stack 382 */ 578 */ 383 #define DECLARE_CRYPTO_WAIT(_wait) \ 579 #define DECLARE_CRYPTO_WAIT(_wait) \ 384 struct crypto_wait _wait = { \ 580 struct crypto_wait _wait = { \ 385 COMPLETION_INITIALIZER_ONSTACK 581 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 } 386 582 387 /* 583 /* 388 * Async ops completion helper functioons 584 * Async ops completion helper functioons 389 */ 585 */ 390 void crypto_req_done(void *req, int err); !! 586 void crypto_req_done(struct crypto_async_request *req, int err); 391 587 392 static inline int crypto_wait_req(int err, str 588 static inline int crypto_wait_req(int err, struct crypto_wait *wait) 393 { 589 { 394 switch (err) { 590 switch (err) { 395 case -EINPROGRESS: 591 case -EINPROGRESS: 396 case -EBUSY: 592 case -EBUSY: 397 wait_for_completion(&wait->com 593 wait_for_completion(&wait->completion); 398 reinit_completion(&wait->compl 594 reinit_completion(&wait->completion); 399 err = wait->err; 595 err = wait->err; 400 break; 596 break; 401 } 597 } 402 598 403 return err; 599 return err; 404 } 600 } 405 601 406 static inline void crypto_init_wait(struct cry 602 static inline void crypto_init_wait(struct crypto_wait *wait) 407 { 603 { 408 init_completion(&wait->completion); 604 init_completion(&wait->completion); 409 } 605 } 410 606 411 /* 607 /* >> 608 * Algorithm registration interface. >> 609 */ >> 610 int crypto_register_alg(struct crypto_alg *alg); >> 611 void crypto_unregister_alg(struct crypto_alg *alg); >> 612 int crypto_register_algs(struct crypto_alg *algs, int count); >> 613 void crypto_unregister_algs(struct crypto_alg *algs, int count); >> 614 >> 615 /* 412 * Algorithm query interface. 616 * Algorithm query interface. 413 */ 617 */ 414 int crypto_has_alg(const char *name, u32 type, 618 int crypto_has_alg(const char *name, u32 type, u32 mask); 415 619 416 /* 620 /* 417 * Transforms: user-instantiated objects which 621 * Transforms: user-instantiated objects which encapsulate algorithms 418 * and core processing logic. Managed via cry 622 * and core processing logic. Managed via crypto_alloc_*() and 419 * crypto_free_*(), as well as the various hel 623 * crypto_free_*(), as well as the various helpers below. 420 */ 624 */ 421 625 422 struct crypto_tfm { 626 struct crypto_tfm { 423 refcount_t refcnt; << 424 627 425 u32 crt_flags; 628 u32 crt_flags; 426 629 427 int node; 630 int node; 428 631 429 void (*exit)(struct crypto_tfm *tfm); 632 void (*exit)(struct crypto_tfm *tfm); 430 633 431 struct crypto_alg *__crt_alg; 634 struct crypto_alg *__crt_alg; 432 635 433 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR 636 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; 434 }; 637 }; 435 638 >> 639 struct crypto_cipher { >> 640 struct crypto_tfm base; >> 641 }; >> 642 436 struct crypto_comp { 643 struct crypto_comp { 437 struct crypto_tfm base; 644 struct crypto_tfm base; 438 }; 645 }; 439 646 >> 647 enum { >> 648 CRYPTOA_UNSPEC, >> 649 CRYPTOA_ALG, >> 650 CRYPTOA_TYPE, >> 651 CRYPTOA_U32, >> 652 __CRYPTOA_MAX, >> 653 }; >> 654 >> 655 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1) >> 656 >> 657 /* Maximum number of (rtattr) parameters for each template. */ >> 658 #define CRYPTO_MAX_ATTRS 32 >> 659 >> 660 struct crypto_attr_alg { >> 661 char name[CRYPTO_MAX_ALG_NAME]; >> 662 }; >> 663 >> 664 struct crypto_attr_type { >> 665 u32 type; >> 666 u32 mask; >> 667 }; >> 668 >> 669 struct crypto_attr_u32 { >> 670 u32 num; >> 671 }; >> 672 440 /* 673 /* 441 * Transform user interface. 674 * Transform user interface. 442 */ 675 */ 443 676 444 struct crypto_tfm *crypto_alloc_base(const cha 677 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask); 445 void crypto_destroy_tfm(void *mem, struct cryp 678 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm); 446 679 447 static inline void crypto_free_tfm(struct cryp 680 static inline void crypto_free_tfm(struct crypto_tfm *tfm) 448 { 681 { 449 return crypto_destroy_tfm(tfm, tfm); 682 return crypto_destroy_tfm(tfm, tfm); 450 } 683 } 451 684 >> 685 int alg_test(const char *driver, const char *alg, u32 type, u32 mask); >> 686 452 /* 687 /* 453 * Transform helpers which query the underlyin 688 * Transform helpers which query the underlying algorithm. 454 */ 689 */ 455 static inline const char *crypto_tfm_alg_name( 690 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm) 456 { 691 { 457 return tfm->__crt_alg->cra_name; 692 return tfm->__crt_alg->cra_name; 458 } 693 } 459 694 460 static inline const char *crypto_tfm_alg_drive 695 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm) 461 { 696 { 462 return tfm->__crt_alg->cra_driver_name 697 return tfm->__crt_alg->cra_driver_name; 463 } 698 } 464 699 >> 700 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm) >> 701 { >> 702 return tfm->__crt_alg->cra_priority; >> 703 } >> 704 >> 705 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm) >> 706 { >> 707 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK; >> 708 } >> 709 465 static inline unsigned int crypto_tfm_alg_bloc 710 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm) 466 { 711 { 467 return tfm->__crt_alg->cra_blocksize; 712 return tfm->__crt_alg->cra_blocksize; 468 } 713 } 469 714 470 static inline unsigned int crypto_tfm_alg_alig 715 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm) 471 { 716 { 472 return tfm->__crt_alg->cra_alignmask; 717 return tfm->__crt_alg->cra_alignmask; 473 } 718 } 474 719 475 static inline u32 crypto_tfm_get_flags(struct 720 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm) 476 { 721 { 477 return tfm->crt_flags; 722 return tfm->crt_flags; 478 } 723 } 479 724 480 static inline void crypto_tfm_set_flags(struct 725 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags) 481 { 726 { 482 tfm->crt_flags |= flags; 727 tfm->crt_flags |= flags; 483 } 728 } 484 729 485 static inline void crypto_tfm_clear_flags(stru 730 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags) 486 { 731 { 487 tfm->crt_flags &= ~flags; 732 tfm->crt_flags &= ~flags; 488 } 733 } 489 734 >> 735 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm) >> 736 { >> 737 return tfm->__crt_ctx; >> 738 } >> 739 490 static inline unsigned int crypto_tfm_ctx_alig 740 static inline unsigned int crypto_tfm_ctx_alignment(void) 491 { 741 { 492 struct crypto_tfm *tfm; 742 struct crypto_tfm *tfm; 493 return __alignof__(tfm->__crt_ctx); 743 return __alignof__(tfm->__crt_ctx); 494 } 744 } >> 745 >> 746 /** >> 747 * DOC: Single Block Cipher API >> 748 * >> 749 * The single block cipher API is used with the ciphers of type >> 750 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto). >> 751 * >> 752 * Using the single block cipher API calls, operations with the basic cipher >> 753 * primitive can be implemented. These cipher primitives exclude any block >> 754 * chaining operations including IV handling. >> 755 * >> 756 * The purpose of this single block cipher API is to support the implementation >> 757 * of templates or other concepts that only need to perform the cipher operation >> 758 * on one block at a time. Templates invoke the underlying cipher primitive >> 759 * block-wise and process either the input or the output data of these cipher >> 760 * operations. >> 761 */ >> 762 >> 763 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm) >> 764 { >> 765 return (struct crypto_cipher *)tfm; >> 766 } >> 767 >> 768 /** >> 769 * crypto_alloc_cipher() - allocate single block cipher handle >> 770 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the >> 771 * single block cipher >> 772 * @type: specifies the type of the cipher >> 773 * @mask: specifies the mask for the cipher >> 774 * >> 775 * Allocate a cipher handle for a single block cipher. The returned struct >> 776 * crypto_cipher is the cipher handle that is required for any subsequent API >> 777 * invocation for that single block cipher. >> 778 * >> 779 * Return: allocated cipher handle in case of success; IS_ERR() is true in case >> 780 * of an error, PTR_ERR() returns the error code. >> 781 */ >> 782 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name, >> 783 u32 type, u32 mask) >> 784 { >> 785 type &= ~CRYPTO_ALG_TYPE_MASK; >> 786 type |= CRYPTO_ALG_TYPE_CIPHER; >> 787 mask |= CRYPTO_ALG_TYPE_MASK; >> 788 >> 789 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask)); >> 790 } >> 791 >> 792 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm) >> 793 { >> 794 return &tfm->base; >> 795 } >> 796 >> 797 /** >> 798 * crypto_free_cipher() - zeroize and free the single block cipher handle >> 799 * @tfm: cipher handle to be freed >> 800 */ >> 801 static inline void crypto_free_cipher(struct crypto_cipher *tfm) >> 802 { >> 803 crypto_free_tfm(crypto_cipher_tfm(tfm)); >> 804 } >> 805 >> 806 /** >> 807 * crypto_has_cipher() - Search for the availability of a single block cipher >> 808 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the >> 809 * single block cipher >> 810 * @type: specifies the type of the cipher >> 811 * @mask: specifies the mask for the cipher >> 812 * >> 813 * Return: true when the single block cipher is known to the kernel crypto API; >> 814 * false otherwise >> 815 */ >> 816 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask) >> 817 { >> 818 type &= ~CRYPTO_ALG_TYPE_MASK; >> 819 type |= CRYPTO_ALG_TYPE_CIPHER; >> 820 mask |= CRYPTO_ALG_TYPE_MASK; >> 821 >> 822 return crypto_has_alg(alg_name, type, mask); >> 823 } >> 824 >> 825 /** >> 826 * crypto_cipher_blocksize() - obtain block size for cipher >> 827 * @tfm: cipher handle >> 828 * >> 829 * The block size for the single block cipher referenced with the cipher handle >> 830 * tfm is returned. The caller may use that information to allocate appropriate >> 831 * memory for the data returned by the encryption or decryption operation >> 832 * >> 833 * Return: block size of cipher >> 834 */ >> 835 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm) >> 836 { >> 837 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm)); >> 838 } >> 839 >> 840 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm) >> 841 { >> 842 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm)); >> 843 } >> 844 >> 845 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm) >> 846 { >> 847 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm)); >> 848 } >> 849 >> 850 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm, >> 851 u32 flags) >> 852 { >> 853 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags); >> 854 } >> 855 >> 856 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm, >> 857 u32 flags) >> 858 { >> 859 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags); >> 860 } >> 861 >> 862 /** >> 863 * crypto_cipher_setkey() - set key for cipher >> 864 * @tfm: cipher handle >> 865 * @key: buffer holding the key >> 866 * @keylen: length of the key in bytes >> 867 * >> 868 * The caller provided key is set for the single block cipher referenced by the >> 869 * cipher handle. >> 870 * >> 871 * Note, the key length determines the cipher type. Many block ciphers implement >> 872 * different cipher modes depending on the key size, such as AES-128 vs AES-192 >> 873 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 >> 874 * is performed. >> 875 * >> 876 * Return: 0 if the setting of the key was successful; < 0 if an error occurred >> 877 */ >> 878 int crypto_cipher_setkey(struct crypto_cipher *tfm, >> 879 const u8 *key, unsigned int keylen); >> 880 >> 881 /** >> 882 * crypto_cipher_encrypt_one() - encrypt one block of plaintext >> 883 * @tfm: cipher handle >> 884 * @dst: points to the buffer that will be filled with the ciphertext >> 885 * @src: buffer holding the plaintext to be encrypted >> 886 * >> 887 * Invoke the encryption operation of one block. The caller must ensure that >> 888 * the plaintext and ciphertext buffers are at least one block in size. >> 889 */ >> 890 void crypto_cipher_encrypt_one(struct crypto_cipher *tfm, >> 891 u8 *dst, const u8 *src); >> 892 >> 893 /** >> 894 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext >> 895 * @tfm: cipher handle >> 896 * @dst: points to the buffer that will be filled with the plaintext >> 897 * @src: buffer holding the ciphertext to be decrypted >> 898 * >> 899 * Invoke the decryption operation of one block. The caller must ensure that >> 900 * the plaintext and ciphertext buffers are at least one block in size. >> 901 */ >> 902 void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, >> 903 u8 *dst, const u8 *src); 495 904 496 static inline struct crypto_comp *__crypto_com 905 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm) 497 { 906 { 498 return (struct crypto_comp *)tfm; 907 return (struct crypto_comp *)tfm; 499 } 908 } 500 909 501 static inline struct crypto_comp *crypto_alloc 910 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name, 502 911 u32 type, u32 mask) 503 { 912 { 504 type &= ~CRYPTO_ALG_TYPE_MASK; 913 type &= ~CRYPTO_ALG_TYPE_MASK; 505 type |= CRYPTO_ALG_TYPE_COMPRESS; 914 type |= CRYPTO_ALG_TYPE_COMPRESS; 506 mask |= CRYPTO_ALG_TYPE_MASK; 915 mask |= CRYPTO_ALG_TYPE_MASK; 507 916 508 return __crypto_comp_cast(crypto_alloc 917 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask)); 509 } 918 } 510 919 511 static inline struct crypto_tfm *crypto_comp_t 920 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm) 512 { 921 { 513 return &tfm->base; 922 return &tfm->base; 514 } 923 } 515 924 516 static inline void crypto_free_comp(struct cry 925 static inline void crypto_free_comp(struct crypto_comp *tfm) 517 { 926 { 518 crypto_free_tfm(crypto_comp_tfm(tfm)); 927 crypto_free_tfm(crypto_comp_tfm(tfm)); 519 } 928 } 520 929 521 static inline int crypto_has_comp(const char * 930 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask) 522 { 931 { 523 type &= ~CRYPTO_ALG_TYPE_MASK; 932 type &= ~CRYPTO_ALG_TYPE_MASK; 524 type |= CRYPTO_ALG_TYPE_COMPRESS; 933 type |= CRYPTO_ALG_TYPE_COMPRESS; 525 mask |= CRYPTO_ALG_TYPE_MASK; 934 mask |= CRYPTO_ALG_TYPE_MASK; 526 935 527 return crypto_has_alg(alg_name, type, 936 return crypto_has_alg(alg_name, type, mask); 528 } 937 } 529 938 530 static inline const char *crypto_comp_name(str 939 static inline const char *crypto_comp_name(struct crypto_comp *tfm) 531 { 940 { 532 return crypto_tfm_alg_name(crypto_comp 941 return crypto_tfm_alg_name(crypto_comp_tfm(tfm)); 533 } 942 } 534 943 535 int crypto_comp_compress(struct crypto_comp *t 944 int crypto_comp_compress(struct crypto_comp *tfm, 536 const u8 *src, unsign 945 const u8 *src, unsigned int slen, 537 u8 *dst, unsigned int 946 u8 *dst, unsigned int *dlen); 538 947 539 int crypto_comp_decompress(struct crypto_comp 948 int crypto_comp_decompress(struct crypto_comp *tfm, 540 const u8 *src, unsi 949 const u8 *src, unsigned int slen, 541 u8 *dst, unsigned i 950 u8 *dst, unsigned int *dlen); 542 951 543 #endif /* _LINUX_CRYPTO_H */ 952 #endif /* _LINUX_CRYPTO_H */ 544 953 545 954
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.