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/completion.h> 16 #include <linux/refcount.h> 16 #include <linux/refcount.h> 17 #include <linux/slab.h> 17 #include <linux/slab.h> 18 #include <linux/types.h> 18 #include <linux/types.h> 19 19 20 /* 20 /* 21 * Algorithm masks and types. 21 * Algorithm masks and types. 22 */ 22 */ 23 #define CRYPTO_ALG_TYPE_MASK 0x0000 23 #define CRYPTO_ALG_TYPE_MASK 0x0000000f 24 #define CRYPTO_ALG_TYPE_CIPHER 0x0000 24 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001 25 #define CRYPTO_ALG_TYPE_COMPRESS 0x0000 25 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002 26 #define CRYPTO_ALG_TYPE_AEAD 0x0000 26 #define CRYPTO_ALG_TYPE_AEAD 0x00000003 27 #define CRYPTO_ALG_TYPE_LSKCIPHER 0x0000 27 #define CRYPTO_ALG_TYPE_LSKCIPHER 0x00000004 28 #define CRYPTO_ALG_TYPE_SKCIPHER 0x0000 28 #define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005 29 #define CRYPTO_ALG_TYPE_AKCIPHER 0x0000 29 #define CRYPTO_ALG_TYPE_AKCIPHER 0x00000006 30 #define CRYPTO_ALG_TYPE_SIG 0x0000 30 #define CRYPTO_ALG_TYPE_SIG 0x00000007 31 #define CRYPTO_ALG_TYPE_KPP 0x0000 31 #define CRYPTO_ALG_TYPE_KPP 0x00000008 32 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000 32 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a 33 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000 33 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b 34 #define CRYPTO_ALG_TYPE_RNG 0x0000 34 #define CRYPTO_ALG_TYPE_RNG 0x0000000c 35 #define CRYPTO_ALG_TYPE_HASH 0x0000 35 #define CRYPTO_ALG_TYPE_HASH 0x0000000e 36 #define CRYPTO_ALG_TYPE_SHASH 0x0000 36 #define CRYPTO_ALG_TYPE_SHASH 0x0000000e 37 #define CRYPTO_ALG_TYPE_AHASH 0x0000 37 #define CRYPTO_ALG_TYPE_AHASH 0x0000000f 38 38 39 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000 39 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e 40 40 41 #define CRYPTO_ALG_LARVAL 0x0000 41 #define CRYPTO_ALG_LARVAL 0x00000010 42 #define CRYPTO_ALG_DEAD 0x0000 42 #define CRYPTO_ALG_DEAD 0x00000020 43 #define CRYPTO_ALG_DYING 0x0000 43 #define CRYPTO_ALG_DYING 0x00000040 44 #define CRYPTO_ALG_ASYNC 0x0000 44 #define CRYPTO_ALG_ASYNC 0x00000080 45 45 46 /* 46 /* 47 * Set if the algorithm (or an algorithm which 47 * Set if the algorithm (or an algorithm which it uses) requires another 48 * algorithm of the same type to handle corner 48 * algorithm of the same type to handle corner cases. 49 */ 49 */ 50 #define CRYPTO_ALG_NEED_FALLBACK 0x0000 50 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100 51 51 52 /* 52 /* 53 * Set if the algorithm has passed automated r 53 * Set if the algorithm has passed automated run-time testing. Note that 54 * if there is no run-time testing for a given 54 * if there is no run-time testing for a given algorithm it is considered 55 * to have passed. 55 * to have passed. 56 */ 56 */ 57 57 58 #define CRYPTO_ALG_TESTED 0x0000 58 #define CRYPTO_ALG_TESTED 0x00000400 59 59 60 /* 60 /* 61 * Set if the algorithm is an instance that is 61 * Set if the algorithm is an instance that is built from templates. 62 */ 62 */ 63 #define CRYPTO_ALG_INSTANCE 0x0000 63 #define CRYPTO_ALG_INSTANCE 0x00000800 64 64 65 /* Set this bit if the algorithm provided is h 65 /* Set this bit if the algorithm provided is hardware accelerated but 66 * not available to userspace via instruction 66 * not available to userspace via instruction set or so. 67 */ 67 */ 68 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x0000 68 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000 69 69 70 /* 70 /* 71 * Mark a cipher as a service implementation o 71 * Mark a cipher as a service implementation only usable by another 72 * cipher and never by a normal user of the ke 72 * cipher and never by a normal user of the kernel crypto API 73 */ 73 */ 74 #define CRYPTO_ALG_INTERNAL 0x0000 74 #define CRYPTO_ALG_INTERNAL 0x00002000 75 75 76 /* 76 /* 77 * Set if the algorithm has a ->setkey() metho 77 * Set if the algorithm has a ->setkey() method but can be used without 78 * calling it first, i.e. there is a default k 78 * calling it first, i.e. there is a default key. 79 */ 79 */ 80 #define CRYPTO_ALG_OPTIONAL_KEY 0x0000 80 #define CRYPTO_ALG_OPTIONAL_KEY 0x00004000 81 81 82 /* 82 /* 83 * Don't trigger module loading 83 * Don't trigger module loading 84 */ 84 */ 85 #define CRYPTO_NOLOAD 0x0000 85 #define CRYPTO_NOLOAD 0x00008000 86 86 87 /* 87 /* 88 * The algorithm may allocate memory during re 88 * The algorithm may allocate memory during request processing, i.e. during 89 * encryption, decryption, or hashing. Users 89 * encryption, decryption, or hashing. Users can request an algorithm with this 90 * flag unset if they can't handle memory allo 90 * flag unset if they can't handle memory allocation failures. 91 * 91 * 92 * This flag is currently only implemented for 92 * This flag is currently only implemented for algorithms of type "skcipher", 93 * "aead", "ahash", "shash", and "cipher". Al 93 * "aead", "ahash", "shash", and "cipher". Algorithms of other types might not 94 * have this flag set even if they allocate me 94 * have this flag set even if they allocate memory. 95 * 95 * 96 * In some edge cases, algorithms can allocate 96 * In some edge cases, algorithms can allocate memory regardless of this flag. 97 * To avoid these cases, users must obey the f 97 * To avoid these cases, users must obey the following usage constraints: 98 * skcipher: 98 * skcipher: 99 * - The IV buffer and all scatterlist el 99 * - The IV buffer and all scatterlist elements must be aligned to the 100 * algorithm's alignmask. 100 * algorithm's alignmask. 101 * - If the data were to be divided into 101 * - If the data were to be divided into chunks of size 102 * crypto_skcipher_walksize() (with any 102 * crypto_skcipher_walksize() (with any remainder going at the end), no 103 * chunk can cross a page boundary or a 103 * chunk can cross a page boundary or a scatterlist element boundary. 104 * aead: 104 * aead: 105 * - The IV buffer and all scatterlist el 105 * - The IV buffer and all scatterlist elements must be aligned to the 106 * algorithm's alignmask. 106 * algorithm's alignmask. 107 * - The first scatterlist element must c 107 * - The first scatterlist element must contain all the associated data, 108 * and its pages must be !PageHighMem. 108 * and its pages must be !PageHighMem. 109 * - If the plaintext/ciphertext were to 109 * - If the plaintext/ciphertext were to be divided into chunks of size 110 * crypto_aead_walksize() (with the rem 110 * crypto_aead_walksize() (with the remainder going at the end), no chunk 111 * can cross a page boundary or a scatt 111 * can cross a page boundary or a scatterlist element boundary. 112 * ahash: 112 * ahash: 113 * - crypto_ahash_finup() must not be use 113 * - crypto_ahash_finup() must not be used unless the algorithm implements 114 * ->finup() natively. 114 * ->finup() natively. 115 */ 115 */ 116 #define CRYPTO_ALG_ALLOCATES_MEMORY 0x0001 116 #define CRYPTO_ALG_ALLOCATES_MEMORY 0x00010000 117 117 118 /* 118 /* 119 * Mark an algorithm as a service implementati 119 * Mark an algorithm as a service implementation only usable by a 120 * template and never by a normal user of the 120 * template and never by a normal user of the kernel crypto API. 121 * This is intended to be used by algorithms t 121 * This is intended to be used by algorithms that are themselves 122 * not FIPS-approved but may instead be used t 122 * not FIPS-approved but may instead be used to implement parts of 123 * a FIPS-approved algorithm (e.g., dh vs. ffd 123 * a FIPS-approved algorithm (e.g., dh vs. ffdhe2048(dh)). 124 */ 124 */ 125 #define CRYPTO_ALG_FIPS_INTERNAL 0x0002 125 #define CRYPTO_ALG_FIPS_INTERNAL 0x00020000 126 126 127 /* 127 /* 128 * Transform masks and values (for crt_flags). 128 * Transform masks and values (for crt_flags). 129 */ 129 */ 130 #define CRYPTO_TFM_NEED_KEY 0x0000 130 #define CRYPTO_TFM_NEED_KEY 0x00000001 131 131 132 #define CRYPTO_TFM_REQ_MASK 0x000f 132 #define CRYPTO_TFM_REQ_MASK 0x000fff00 133 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x0000 133 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x00000100 134 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x0000 134 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 135 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x0000 135 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400 136 136 137 /* 137 /* 138 * Miscellaneous stuff. 138 * Miscellaneous stuff. 139 */ 139 */ 140 #define CRYPTO_MAX_ALG_NAME 128 140 #define CRYPTO_MAX_ALG_NAME 128 141 141 142 /* 142 /* 143 * The macro CRYPTO_MINALIGN_ATTR (along with 143 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual 144 * declaration) is used to ensure that the cry 144 * declaration) is used to ensure that the crypto_tfm context structure is 145 * aligned correctly for the given architectur 145 * aligned correctly for the given architecture so that there are no alignment 146 * faults for C data types. On architectures 146 * faults for C data types. On architectures that support non-cache coherent 147 * DMA, such as ARM or arm64, it also takes in 147 * DMA, such as ARM or arm64, it also takes into account the minimal alignment 148 * that is required to ensure that the context 148 * that is required to ensure that the context struct member does not share any 149 * cachelines with the rest of the struct. Thi 149 * cachelines with the rest of the struct. This is needed to ensure that cache 150 * maintenance for non-coherent DMA (cache inv 150 * maintenance for non-coherent DMA (cache invalidation in particular) does not 151 * affect data that may be accessed by the CPU 151 * affect data that may be accessed by the CPU concurrently. 152 */ 152 */ 153 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN 153 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN 154 154 155 #define CRYPTO_MINALIGN_ATTR __attribute__ ((_ 155 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN))) 156 156 157 struct crypto_tfm; 157 struct crypto_tfm; 158 struct crypto_type; 158 struct crypto_type; 159 struct module; 159 struct module; 160 160 161 typedef void (*crypto_completion_t)(void *req, 161 typedef void (*crypto_completion_t)(void *req, int err); 162 162 163 /** 163 /** 164 * DOC: Block Cipher Context Data Structures 164 * DOC: Block Cipher Context Data Structures 165 * 165 * 166 * These data structures define the operating 166 * These data structures define the operating context for each block cipher 167 * type. 167 * type. 168 */ 168 */ 169 169 170 struct crypto_async_request { 170 struct crypto_async_request { 171 struct list_head list; 171 struct list_head list; 172 crypto_completion_t complete; 172 crypto_completion_t complete; 173 void *data; 173 void *data; 174 struct crypto_tfm *tfm; 174 struct crypto_tfm *tfm; 175 175 176 u32 flags; 176 u32 flags; 177 }; 177 }; 178 178 179 /** 179 /** 180 * DOC: Block Cipher Algorithm Definitions 180 * DOC: Block Cipher Algorithm Definitions 181 * 181 * 182 * These data structures define modular crypto 182 * These data structures define modular crypto algorithm implementations, 183 * managed via crypto_register_alg() and crypt 183 * managed via crypto_register_alg() and crypto_unregister_alg(). 184 */ 184 */ 185 185 186 /** 186 /** 187 * struct cipher_alg - single-block symmetric 187 * struct cipher_alg - single-block symmetric ciphers definition 188 * @cia_min_keysize: Minimum key size supporte 188 * @cia_min_keysize: Minimum key size supported by the transformation. This is 189 * the smallest key length s 189 * the smallest key length supported by this transformation 190 * algorithm. This must be s 190 * algorithm. This must be set to one of the pre-defined 191 * values as this is not har 191 * values as this is not hardware specific. Possible values 192 * for this field can be fou 192 * for this field can be found via git grep "_MIN_KEY_SIZE" 193 * include/crypto/ 193 * include/crypto/ 194 * @cia_max_keysize: Maximum key size supporte 194 * @cia_max_keysize: Maximum key size supported by the transformation. This is 195 * the largest key length sup 195 * the largest key length supported by this transformation 196 * algorithm. This must be se 196 * algorithm. This must be set to one of the pre-defined values 197 * as this is not hardware sp 197 * as this is not hardware specific. Possible values for this 198 * field can be found via git 198 * field can be found via git grep "_MAX_KEY_SIZE" 199 * include/crypto/ 199 * include/crypto/ 200 * @cia_setkey: Set key for the transformation 200 * @cia_setkey: Set key for the transformation. This function is used to either 201 * program a supplied key into th 201 * program a supplied key into the hardware or store the key in the 202 * transformation context for pro 202 * transformation context for programming it later. Note that this 203 * function does modify the trans 203 * function does modify the transformation context. This function 204 * can be called multiple times d 204 * can be called multiple times during the existence of the 205 * transformation object, so one 205 * transformation object, so one must make sure the key is properly 206 * reprogrammed into the hardware 206 * reprogrammed into the hardware. This function is also 207 * responsible for checking the k 207 * responsible for checking the key length for validity. 208 * @cia_encrypt: Encrypt a single block. This 208 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a 209 * single block of data, which m 209 * single block of data, which must be @cra_blocksize big. This 210 * always operates on a full @cr 210 * always operates on a full @cra_blocksize and it is not possible 211 * to encrypt a block of smaller 211 * to encrypt a block of smaller size. The supplied buffers must 212 * therefore also be at least of 212 * therefore also be at least of @cra_blocksize size. Both the 213 * input and output buffers are 213 * input and output buffers are always aligned to @cra_alignmask. 214 * In case either of the input o 214 * In case either of the input or output buffer supplied by user 215 * of the crypto API is not alig 215 * of the crypto API is not aligned to @cra_alignmask, the crypto 216 * API will re-align the buffers 216 * API will re-align the buffers. The re-alignment means that a 217 * new buffer will be allocated, 217 * new buffer will be allocated, the data will be copied into the 218 * new buffer, then the processi 218 * new buffer, then the processing will happen on the new buffer, 219 * then the data will be copied 219 * then the data will be copied back into the original buffer and 220 * finally the new buffer will b 220 * finally the new buffer will be freed. In case a software 221 * fallback was put in place in 221 * fallback was put in place in the @cra_init call, this function 222 * might need to use the fallbac 222 * might need to use the fallback if the algorithm doesn't support 223 * all of the key sizes. In case 223 * all of the key sizes. In case the key was stored in 224 * transformation context, the k 224 * transformation context, the key might need to be re-programmed 225 * into the hardware in this fun 225 * into the hardware in this function. This function shall not 226 * modify the transformation con 226 * modify the transformation context, as this function may be 227 * called in parallel with the s 227 * called in parallel with the same transformation object. 228 * @cia_decrypt: Decrypt a single block. This 228 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to 229 * @cia_encrypt, and the conditi 229 * @cia_encrypt, and the conditions are exactly the same. 230 * 230 * 231 * All fields are mandatory and must be filled 231 * All fields are mandatory and must be filled. 232 */ 232 */ 233 struct cipher_alg { 233 struct cipher_alg { 234 unsigned int cia_min_keysize; 234 unsigned int cia_min_keysize; 235 unsigned int cia_max_keysize; 235 unsigned int cia_max_keysize; 236 int (*cia_setkey)(struct crypto_tfm *t 236 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key, 237 unsigned int keylen) 237 unsigned int keylen); 238 void (*cia_encrypt)(struct crypto_tfm 238 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 239 void (*cia_decrypt)(struct crypto_tfm 239 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 240 }; 240 }; 241 241 242 /** 242 /** 243 * struct compress_alg - compression/decompres 243 * struct compress_alg - compression/decompression algorithm 244 * @coa_compress: Compress a buffer of specifi 244 * @coa_compress: Compress a buffer of specified length, storing the resulting 245 * data in the specified buffer 245 * data in the specified buffer. Return the length of the 246 * compressed data in dlen. 246 * compressed data in dlen. 247 * @coa_decompress: Decompress the source buff 247 * @coa_decompress: Decompress the source buffer, storing the uncompressed 248 * data in the specified buff 248 * data in the specified buffer. The length of the data is 249 * returned in dlen. 249 * returned in dlen. 250 * 250 * 251 * All fields are mandatory. 251 * All fields are mandatory. 252 */ 252 */ 253 struct compress_alg { 253 struct compress_alg { 254 int (*coa_compress)(struct crypto_tfm 254 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src, 255 unsigned int slen, 255 unsigned int slen, u8 *dst, unsigned int *dlen); 256 int (*coa_decompress)(struct crypto_tf 256 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src, 257 unsigned int sle 257 unsigned int slen, u8 *dst, unsigned int *dlen); 258 }; 258 }; 259 259 260 #define cra_cipher cra_u.cipher 260 #define cra_cipher cra_u.cipher 261 #define cra_compress cra_u.compress 261 #define cra_compress cra_u.compress 262 262 263 /** 263 /** 264 * struct crypto_alg - definition of a cryptog 264 * struct crypto_alg - definition of a cryptograpic cipher algorithm 265 * @cra_flags: Flags describing this transform 265 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h 266 * CRYPTO_ALG_* flags for the flag 266 * CRYPTO_ALG_* flags for the flags which go in here. Those are 267 * used for fine-tuning the descri 267 * used for fine-tuning the description of the transformation 268 * algorithm. 268 * algorithm. 269 * @cra_blocksize: Minimum block size of this 269 * @cra_blocksize: Minimum block size of this transformation. The size in bytes 270 * of the smallest possible un 270 * of the smallest possible unit which can be transformed with 271 * this algorithm. The users m 271 * this algorithm. The users must respect this value. 272 * In case of HASH transformat 272 * In case of HASH transformation, it is possible for a smaller 273 * block than @cra_blocksize t 273 * block than @cra_blocksize to be passed to the crypto API for 274 * transformation, in case of 274 * transformation, in case of any other transformation type, an 275 * error will be returned upon 275 * error will be returned upon any attempt to transform smaller 276 * than @cra_blocksize chunks. 276 * than @cra_blocksize chunks. 277 * @cra_ctxsize: Size of the operational conte 277 * @cra_ctxsize: Size of the operational context of the transformation. This 278 * value informs the kernel cryp 278 * value informs the kernel crypto API about the memory size 279 * needed to be allocated for th 279 * needed to be allocated for the transformation context. 280 * @cra_alignmask: For cipher, skcipher, lskci 280 * @cra_alignmask: For cipher, skcipher, lskcipher, and aead algorithms this is 281 * 1 less than the alignment, 281 * 1 less than the alignment, in bytes, that the algorithm 282 * implementation requires for 282 * implementation requires for input and output buffers. When 283 * the crypto API is invoked w 283 * the crypto API is invoked with buffers that are not aligned 284 * to this alignment, the cryp 284 * to this alignment, the crypto API automatically utilizes 285 * appropriately aligned tempo 285 * appropriately aligned temporary buffers to comply with what 286 * the algorithm needs. (For 286 * the algorithm needs. (For scatterlists this happens only if 287 * the algorithm uses the skci 287 * the algorithm uses the skcipher_walk helper functions.) This 288 * misalignment handling carri 288 * misalignment handling carries a performance penalty, so it is 289 * preferred that algorithms d 289 * preferred that algorithms do not set a nonzero alignmask. 290 * Also, crypto API users may 290 * Also, crypto API users may wish to allocate buffers aligned 291 * to the alignmask of the alg 291 * to the alignmask of the algorithm being used, in order to 292 * avoid the API having to rea 292 * avoid the API having to realign them. Note: the alignmask is 293 * not supported for hash algo 293 * not supported for hash algorithms and is always 0 for them. 294 * @cra_priority: Priority of this transformat 294 * @cra_priority: Priority of this transformation implementation. In case 295 * multiple transformations wit 295 * multiple transformations with same @cra_name are available to 296 * the Crypto API, the kernel w 296 * the Crypto API, the kernel will use the one with highest 297 * @cra_priority. 297 * @cra_priority. 298 * @cra_name: Generic name (usable by multiple 298 * @cra_name: Generic name (usable by multiple implementations) of the 299 * transformation algorithm. This i 299 * transformation algorithm. This is the name of the transformation 300 * itself. This field is used by th 300 * itself. This field is used by the kernel when looking up the 301 * providers of particular transfor 301 * providers of particular transformation. 302 * @cra_driver_name: Unique name of the transf 302 * @cra_driver_name: Unique name of the transformation provider. This is the 303 * name of the provider of t 303 * name of the provider of the transformation. This can be any 304 * arbitrary value, but in t 304 * arbitrary value, but in the usual case, this contains the 305 * name of the chip or provi 305 * name of the chip or provider and the name of the 306 * transformation algorithm. 306 * transformation algorithm. 307 * @cra_type: Type of the cryptographic transf 307 * @cra_type: Type of the cryptographic transformation. This is a pointer to 308 * struct crypto_type, which implem 308 * struct crypto_type, which implements callbacks common for all 309 * transformation types. There are 309 * transformation types. There are multiple options, such as 310 * &crypto_skcipher_type, &crypto_a 310 * &crypto_skcipher_type, &crypto_ahash_type, &crypto_rng_type. 311 * This field might be empty. In th 311 * This field might be empty. In that case, there are no common 312 * callbacks. This is the case for: 312 * callbacks. This is the case for: cipher, compress, shash. 313 * @cra_u: Callbacks implementing the transfor 313 * @cra_u: Callbacks implementing the transformation. This is a union of 314 * multiple structures. Depending on t 314 * multiple structures. Depending on the type of transformation selected 315 * by @cra_type and @cra_flags above, 315 * by @cra_type and @cra_flags above, the associated structure must be 316 * filled with callbacks. This field m 316 * filled with callbacks. This field might be empty. This is the case 317 * for ahash, shash. 317 * for ahash, shash. 318 * @cra_init: Initialize the cryptographic tra 318 * @cra_init: Initialize the cryptographic transformation object. This function 319 * is used to initialize the crypto 319 * is used to initialize the cryptographic transformation object. 320 * This function is called only onc 320 * This function is called only once at the instantiation time, right 321 * after the transformation context 321 * after the transformation context was allocated. In case the 322 * cryptographic hardware has some 322 * cryptographic hardware has some special requirements which need to 323 * be handled by software, this fun 323 * be handled by software, this function shall check for the precise 324 * requirement of the transformatio 324 * requirement of the transformation and put any software fallbacks 325 * in place. 325 * in place. 326 * @cra_exit: Deinitialize the cryptographic t 326 * @cra_exit: Deinitialize the cryptographic transformation object. This is a 327 * counterpart to @cra_init, used t 327 * counterpart to @cra_init, used to remove various changes set in 328 * @cra_init. 328 * @cra_init. 329 * @cra_u.cipher: Union member which contains 329 * @cra_u.cipher: Union member which contains a single-block symmetric cipher 330 * definition. See @struct @cip 330 * definition. See @struct @cipher_alg. 331 * @cra_u.compress: Union member which contain 331 * @cra_u.compress: Union member which contains a (de)compression algorithm. 332 * See @struct @compress_alg. 332 * See @struct @compress_alg. 333 * @cra_module: Owner of this transformation i 333 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE 334 * @cra_list: internally used 334 * @cra_list: internally used 335 * @cra_users: internally used 335 * @cra_users: internally used 336 * @cra_refcnt: internally used 336 * @cra_refcnt: internally used 337 * @cra_destroy: internally used 337 * @cra_destroy: internally used 338 * 338 * 339 * The struct crypto_alg describes a generic C 339 * The struct crypto_alg describes a generic Crypto API algorithm and is common 340 * for all of the transformations. Any variabl 340 * for all of the transformations. Any variable not documented here shall not 341 * be used by a cipher implementation as it is 341 * be used by a cipher implementation as it is internal to the Crypto API. 342 */ 342 */ 343 struct crypto_alg { 343 struct crypto_alg { 344 struct list_head cra_list; 344 struct list_head cra_list; 345 struct list_head cra_users; 345 struct list_head cra_users; 346 346 347 u32 cra_flags; 347 u32 cra_flags; 348 unsigned int cra_blocksize; 348 unsigned int cra_blocksize; 349 unsigned int cra_ctxsize; 349 unsigned int cra_ctxsize; 350 unsigned int cra_alignmask; 350 unsigned int cra_alignmask; 351 351 352 int cra_priority; 352 int cra_priority; 353 refcount_t cra_refcnt; 353 refcount_t cra_refcnt; 354 354 355 char cra_name[CRYPTO_MAX_ALG_NAME]; 355 char cra_name[CRYPTO_MAX_ALG_NAME]; 356 char cra_driver_name[CRYPTO_MAX_ALG_NA 356 char cra_driver_name[CRYPTO_MAX_ALG_NAME]; 357 357 358 const struct crypto_type *cra_type; 358 const struct crypto_type *cra_type; 359 359 360 union { 360 union { 361 struct cipher_alg cipher; 361 struct cipher_alg cipher; 362 struct compress_alg compress; 362 struct compress_alg compress; 363 } cra_u; 363 } cra_u; 364 364 365 int (*cra_init)(struct crypto_tfm *tfm 365 int (*cra_init)(struct crypto_tfm *tfm); 366 void (*cra_exit)(struct crypto_tfm *tf 366 void (*cra_exit)(struct crypto_tfm *tfm); 367 void (*cra_destroy)(struct crypto_alg 367 void (*cra_destroy)(struct crypto_alg *alg); 368 368 369 struct module *cra_module; 369 struct module *cra_module; 370 } CRYPTO_MINALIGN_ATTR; 370 } CRYPTO_MINALIGN_ATTR; 371 371 372 /* 372 /* 373 * A helper struct for waiting for completion 373 * A helper struct for waiting for completion of async crypto ops 374 */ 374 */ 375 struct crypto_wait { 375 struct crypto_wait { 376 struct completion completion; 376 struct completion completion; 377 int err; 377 int err; 378 }; 378 }; 379 379 380 /* 380 /* 381 * Macro for declaring a crypto op async wait 381 * Macro for declaring a crypto op async wait object on stack 382 */ 382 */ 383 #define DECLARE_CRYPTO_WAIT(_wait) \ 383 #define DECLARE_CRYPTO_WAIT(_wait) \ 384 struct crypto_wait _wait = { \ 384 struct crypto_wait _wait = { \ 385 COMPLETION_INITIALIZER_ONSTACK 385 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 } 386 386 387 /* 387 /* 388 * Async ops completion helper functioons 388 * Async ops completion helper functioons 389 */ 389 */ 390 void crypto_req_done(void *req, int err); 390 void crypto_req_done(void *req, int err); 391 391 392 static inline int crypto_wait_req(int err, str 392 static inline int crypto_wait_req(int err, struct crypto_wait *wait) 393 { 393 { 394 switch (err) { 394 switch (err) { 395 case -EINPROGRESS: 395 case -EINPROGRESS: 396 case -EBUSY: 396 case -EBUSY: 397 wait_for_completion(&wait->com 397 wait_for_completion(&wait->completion); 398 reinit_completion(&wait->compl 398 reinit_completion(&wait->completion); 399 err = wait->err; 399 err = wait->err; 400 break; 400 break; 401 } 401 } 402 402 403 return err; 403 return err; 404 } 404 } 405 405 406 static inline void crypto_init_wait(struct cry 406 static inline void crypto_init_wait(struct crypto_wait *wait) 407 { 407 { 408 init_completion(&wait->completion); 408 init_completion(&wait->completion); 409 } 409 } 410 410 411 /* 411 /* 412 * Algorithm query interface. 412 * Algorithm query interface. 413 */ 413 */ 414 int crypto_has_alg(const char *name, u32 type, 414 int crypto_has_alg(const char *name, u32 type, u32 mask); 415 415 416 /* 416 /* 417 * Transforms: user-instantiated objects which 417 * Transforms: user-instantiated objects which encapsulate algorithms 418 * and core processing logic. Managed via cry 418 * and core processing logic. Managed via crypto_alloc_*() and 419 * crypto_free_*(), as well as the various hel 419 * crypto_free_*(), as well as the various helpers below. 420 */ 420 */ 421 421 422 struct crypto_tfm { 422 struct crypto_tfm { 423 refcount_t refcnt; 423 refcount_t refcnt; 424 424 425 u32 crt_flags; 425 u32 crt_flags; 426 426 427 int node; 427 int node; 428 428 429 void (*exit)(struct crypto_tfm *tfm); 429 void (*exit)(struct crypto_tfm *tfm); 430 430 431 struct crypto_alg *__crt_alg; 431 struct crypto_alg *__crt_alg; 432 432 433 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR 433 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; 434 }; 434 }; 435 435 436 struct crypto_comp { 436 struct crypto_comp { 437 struct crypto_tfm base; 437 struct crypto_tfm base; 438 }; 438 }; 439 439 440 /* 440 /* 441 * Transform user interface. 441 * Transform user interface. 442 */ 442 */ 443 443 444 struct crypto_tfm *crypto_alloc_base(const cha 444 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask); 445 void crypto_destroy_tfm(void *mem, struct cryp 445 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm); 446 446 447 static inline void crypto_free_tfm(struct cryp 447 static inline void crypto_free_tfm(struct crypto_tfm *tfm) 448 { 448 { 449 return crypto_destroy_tfm(tfm, tfm); 449 return crypto_destroy_tfm(tfm, tfm); 450 } 450 } 451 451 452 /* 452 /* 453 * Transform helpers which query the underlyin 453 * Transform helpers which query the underlying algorithm. 454 */ 454 */ 455 static inline const char *crypto_tfm_alg_name( 455 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm) 456 { 456 { 457 return tfm->__crt_alg->cra_name; 457 return tfm->__crt_alg->cra_name; 458 } 458 } 459 459 460 static inline const char *crypto_tfm_alg_drive 460 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm) 461 { 461 { 462 return tfm->__crt_alg->cra_driver_name 462 return tfm->__crt_alg->cra_driver_name; 463 } 463 } 464 464 465 static inline unsigned int crypto_tfm_alg_bloc 465 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm) 466 { 466 { 467 return tfm->__crt_alg->cra_blocksize; 467 return tfm->__crt_alg->cra_blocksize; 468 } 468 } 469 469 470 static inline unsigned int crypto_tfm_alg_alig 470 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm) 471 { 471 { 472 return tfm->__crt_alg->cra_alignmask; 472 return tfm->__crt_alg->cra_alignmask; 473 } 473 } 474 474 475 static inline u32 crypto_tfm_get_flags(struct 475 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm) 476 { 476 { 477 return tfm->crt_flags; 477 return tfm->crt_flags; 478 } 478 } 479 479 480 static inline void crypto_tfm_set_flags(struct 480 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags) 481 { 481 { 482 tfm->crt_flags |= flags; 482 tfm->crt_flags |= flags; 483 } 483 } 484 484 485 static inline void crypto_tfm_clear_flags(stru 485 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags) 486 { 486 { 487 tfm->crt_flags &= ~flags; 487 tfm->crt_flags &= ~flags; 488 } 488 } 489 489 490 static inline unsigned int crypto_tfm_ctx_alig 490 static inline unsigned int crypto_tfm_ctx_alignment(void) 491 { 491 { 492 struct crypto_tfm *tfm; 492 struct crypto_tfm *tfm; 493 return __alignof__(tfm->__crt_ctx); 493 return __alignof__(tfm->__crt_ctx); 494 } 494 } 495 495 496 static inline struct crypto_comp *__crypto_com 496 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm) 497 { 497 { 498 return (struct crypto_comp *)tfm; 498 return (struct crypto_comp *)tfm; 499 } 499 } 500 500 501 static inline struct crypto_comp *crypto_alloc 501 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name, 502 502 u32 type, u32 mask) 503 { 503 { 504 type &= ~CRYPTO_ALG_TYPE_MASK; 504 type &= ~CRYPTO_ALG_TYPE_MASK; 505 type |= CRYPTO_ALG_TYPE_COMPRESS; 505 type |= CRYPTO_ALG_TYPE_COMPRESS; 506 mask |= CRYPTO_ALG_TYPE_MASK; 506 mask |= CRYPTO_ALG_TYPE_MASK; 507 507 508 return __crypto_comp_cast(crypto_alloc 508 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask)); 509 } 509 } 510 510 511 static inline struct crypto_tfm *crypto_comp_t 511 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm) 512 { 512 { 513 return &tfm->base; 513 return &tfm->base; 514 } 514 } 515 515 516 static inline void crypto_free_comp(struct cry 516 static inline void crypto_free_comp(struct crypto_comp *tfm) 517 { 517 { 518 crypto_free_tfm(crypto_comp_tfm(tfm)); 518 crypto_free_tfm(crypto_comp_tfm(tfm)); 519 } 519 } 520 520 521 static inline int crypto_has_comp(const char * 521 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask) 522 { 522 { 523 type &= ~CRYPTO_ALG_TYPE_MASK; 523 type &= ~CRYPTO_ALG_TYPE_MASK; 524 type |= CRYPTO_ALG_TYPE_COMPRESS; 524 type |= CRYPTO_ALG_TYPE_COMPRESS; 525 mask |= CRYPTO_ALG_TYPE_MASK; 525 mask |= CRYPTO_ALG_TYPE_MASK; 526 526 527 return crypto_has_alg(alg_name, type, 527 return crypto_has_alg(alg_name, type, mask); 528 } 528 } 529 529 530 static inline const char *crypto_comp_name(str 530 static inline const char *crypto_comp_name(struct crypto_comp *tfm) 531 { 531 { 532 return crypto_tfm_alg_name(crypto_comp 532 return crypto_tfm_alg_name(crypto_comp_tfm(tfm)); 533 } 533 } 534 534 535 int crypto_comp_compress(struct crypto_comp *t 535 int crypto_comp_compress(struct crypto_comp *tfm, 536 const u8 *src, unsign 536 const u8 *src, unsigned int slen, 537 u8 *dst, unsigned int 537 u8 *dst, unsigned int *dlen); 538 538 539 int crypto_comp_decompress(struct crypto_comp 539 int crypto_comp_decompress(struct crypto_comp *tfm, 540 const u8 *src, unsi 540 const u8 *src, unsigned int slen, 541 u8 *dst, unsigned i 541 u8 *dst, unsigned int *dlen); 542 542 543 #endif /* _LINUX_CRYPTO_H */ 543 #endif /* _LINUX_CRYPTO_H */ 544 544 545 545
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.