1 /* SPDX-License-Identifier: GPL-2.0-or-later * << 2 /* 1 /* 3 * Scatterlist Cryptographic API. 2 * Scatterlist Cryptographic API. 4 * 3 * 5 * Copyright (c) 2002 James Morris <jmorris@in 4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 6 * Copyright (c) 2002 David S. Miller (davem@r 5 * Copyright (c) 2002 David S. Miller (davem@redhat.com) 7 * Copyright (c) 2005 Herbert Xu <herbert@gond 6 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> 8 * 7 * 9 * Portions derived from Cryptoapi, by Alexand 8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no> 10 * and Nettle, by Niels Möller. 9 * and Nettle, by Niels Möller. >> 10 * >> 11 * This program is free software; you can redistribute it and/or modify it >> 12 * under the terms of the GNU General Public License as published by the Free >> 13 * Software Foundation; either version 2 of the License, or (at your option) >> 14 * any later version. >> 15 * 11 */ 16 */ 12 #ifndef _LINUX_CRYPTO_H 17 #ifndef _LINUX_CRYPTO_H 13 #define _LINUX_CRYPTO_H 18 #define _LINUX_CRYPTO_H 14 19 15 #include <linux/completion.h> !! 20 #include <linux/atomic.h> 16 #include <linux/refcount.h> !! 21 #include <linux/kernel.h> >> 22 #include <linux/list.h> >> 23 #include <linux/bug.h> 17 #include <linux/slab.h> 24 #include <linux/slab.h> 18 #include <linux/types.h> !! 25 #include <linux/string.h> >> 26 #include <linux/uaccess.h> >> 27 #include <linux/completion.h> >> 28 >> 29 /* >> 30 * Autoloaded crypto modules should only use a prefixed name to avoid allowing >> 31 * arbitrary modules to be loaded. Loading from userspace may still need the >> 32 * unprefixed names, so retains those aliases as well. >> 33 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3 >> 34 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro >> 35 * expands twice on the same line. Instead, use a separate base name for the >> 36 * alias. >> 37 */ >> 38 #define MODULE_ALIAS_CRYPTO(name) \ >> 39 __MODULE_INFO(alias, alias_userspace, name); \ >> 40 __MODULE_INFO(alias, alias_crypto, "crypto-" name) 19 41 20 /* 42 /* 21 * Algorithm masks and types. 43 * Algorithm masks and types. 22 */ 44 */ 23 #define CRYPTO_ALG_TYPE_MASK 0x0000 45 #define CRYPTO_ALG_TYPE_MASK 0x0000000f 24 #define CRYPTO_ALG_TYPE_CIPHER 0x0000 46 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001 25 #define CRYPTO_ALG_TYPE_COMPRESS 0x0000 47 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002 26 #define CRYPTO_ALG_TYPE_AEAD 0x0000 48 #define CRYPTO_ALG_TYPE_AEAD 0x00000003 27 #define CRYPTO_ALG_TYPE_LSKCIPHER 0x0000 !! 49 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004 >> 50 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005 28 #define CRYPTO_ALG_TYPE_SKCIPHER 0x0000 51 #define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005 29 #define CRYPTO_ALG_TYPE_AKCIPHER 0x0000 !! 52 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006 30 #define CRYPTO_ALG_TYPE_SIG 0x0000 << 31 #define CRYPTO_ALG_TYPE_KPP 0x0000 53 #define CRYPTO_ALG_TYPE_KPP 0x00000008 32 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000 54 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a 33 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000 55 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b 34 #define CRYPTO_ALG_TYPE_RNG 0x0000 56 #define CRYPTO_ALG_TYPE_RNG 0x0000000c >> 57 #define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d >> 58 #define CRYPTO_ALG_TYPE_DIGEST 0x0000000e 35 #define CRYPTO_ALG_TYPE_HASH 0x0000 59 #define CRYPTO_ALG_TYPE_HASH 0x0000000e 36 #define CRYPTO_ALG_TYPE_SHASH 0x0000 60 #define CRYPTO_ALG_TYPE_SHASH 0x0000000e 37 #define CRYPTO_ALG_TYPE_AHASH 0x0000 61 #define CRYPTO_ALG_TYPE_AHASH 0x0000000f 38 62 >> 63 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e >> 64 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e >> 65 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c 39 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000 66 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e 40 67 41 #define CRYPTO_ALG_LARVAL 0x0000 68 #define CRYPTO_ALG_LARVAL 0x00000010 42 #define CRYPTO_ALG_DEAD 0x0000 69 #define CRYPTO_ALG_DEAD 0x00000020 43 #define CRYPTO_ALG_DYING 0x0000 70 #define CRYPTO_ALG_DYING 0x00000040 44 #define CRYPTO_ALG_ASYNC 0x0000 71 #define CRYPTO_ALG_ASYNC 0x00000080 45 72 46 /* 73 /* 47 * Set if the algorithm (or an algorithm which !! 74 * Set this bit if and only if the algorithm requires another algorithm of 48 * algorithm of the same type to handle corner !! 75 * the same type to handle corner cases. 49 */ 76 */ 50 #define CRYPTO_ALG_NEED_FALLBACK 0x0000 77 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100 51 78 52 /* 79 /* >> 80 * This bit is set for symmetric key ciphers that have already been wrapped >> 81 * with a generic IV generator to prevent them from being wrapped again. >> 82 */ >> 83 #define CRYPTO_ALG_GENIV 0x00000200 >> 84 >> 85 /* 53 * Set if the algorithm has passed automated r 86 * Set if the algorithm has passed automated run-time testing. Note that 54 * if there is no run-time testing for a given 87 * if there is no run-time testing for a given algorithm it is considered 55 * to have passed. 88 * to have passed. 56 */ 89 */ 57 90 58 #define CRYPTO_ALG_TESTED 0x0000 91 #define CRYPTO_ALG_TESTED 0x00000400 59 92 60 /* 93 /* 61 * Set if the algorithm is an instance that is 94 * Set if the algorithm is an instance that is built from templates. 62 */ 95 */ 63 #define CRYPTO_ALG_INSTANCE 0x0000 96 #define CRYPTO_ALG_INSTANCE 0x00000800 64 97 65 /* Set this bit if the algorithm provided is h 98 /* Set this bit if the algorithm provided is hardware accelerated but 66 * not available to userspace via instruction 99 * not available to userspace via instruction set or so. 67 */ 100 */ 68 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x0000 101 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000 69 102 70 /* 103 /* 71 * Mark a cipher as a service implementation o 104 * Mark a cipher as a service implementation only usable by another 72 * cipher and never by a normal user of the ke 105 * cipher and never by a normal user of the kernel crypto API 73 */ 106 */ 74 #define CRYPTO_ALG_INTERNAL 0x0000 107 #define CRYPTO_ALG_INTERNAL 0x00002000 75 108 76 /* 109 /* 77 * Set if the algorithm has a ->setkey() metho 110 * Set if the algorithm has a ->setkey() method but can be used without 78 * calling it first, i.e. there is a default k 111 * calling it first, i.e. there is a default key. 79 */ 112 */ 80 #define CRYPTO_ALG_OPTIONAL_KEY 0x0000 113 #define CRYPTO_ALG_OPTIONAL_KEY 0x00004000 81 114 82 /* 115 /* 83 * Don't trigger module loading << 84 */ << 85 #define CRYPTO_NOLOAD 0x0000 << 86 << 87 /* << 88 * The algorithm may allocate memory during re << 89 * encryption, decryption, or hashing. Users << 90 * flag unset if they can't handle memory allo << 91 * << 92 * This flag is currently only implemented for << 93 * "aead", "ahash", "shash", and "cipher". Al << 94 * have this flag set even if they allocate me << 95 * << 96 * In some edge cases, algorithms can allocate << 97 * To avoid these cases, users must obey the f << 98 * skcipher: << 99 * - The IV buffer and all scatterlist el << 100 * algorithm's alignmask. << 101 * - If the data were to be divided into << 102 * crypto_skcipher_walksize() (with any << 103 * chunk can cross a page boundary or a << 104 * aead: << 105 * - The IV buffer and all scatterlist el << 106 * algorithm's alignmask. << 107 * - The first scatterlist element must c << 108 * and its pages must be !PageHighMem. << 109 * - If the plaintext/ciphertext were to << 110 * crypto_aead_walksize() (with the rem << 111 * can cross a page boundary or a scatt << 112 * ahash: << 113 * - crypto_ahash_finup() must not be use << 114 * ->finup() natively. << 115 */ << 116 #define CRYPTO_ALG_ALLOCATES_MEMORY 0x0001 << 117 << 118 /* << 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). 116 * Transform masks and values (for crt_flags). 129 */ 117 */ 130 #define CRYPTO_TFM_NEED_KEY 0x0000 118 #define CRYPTO_TFM_NEED_KEY 0x00000001 131 119 132 #define CRYPTO_TFM_REQ_MASK 0x000f 120 #define CRYPTO_TFM_REQ_MASK 0x000fff00 133 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x0000 !! 121 #define CRYPTO_TFM_RES_MASK 0xfff00000 >> 122 >> 123 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100 134 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x0000 124 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 135 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x0000 125 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400 >> 126 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000 >> 127 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000 >> 128 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000 >> 129 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000 >> 130 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000 136 131 137 /* 132 /* 138 * Miscellaneous stuff. 133 * Miscellaneous stuff. 139 */ 134 */ 140 #define CRYPTO_MAX_ALG_NAME 128 135 #define CRYPTO_MAX_ALG_NAME 128 141 136 142 /* 137 /* 143 * The macro CRYPTO_MINALIGN_ATTR (along with 138 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual 144 * declaration) is used to ensure that the cry 139 * declaration) is used to ensure that the crypto_tfm context structure is 145 * aligned correctly for the given architectur 140 * aligned correctly for the given architecture so that there are no alignment 146 * faults for C data types. On architectures !! 141 * faults for C data types. In particular, this is required on platforms such 147 * DMA, such as ARM or arm64, it also takes in !! 142 * as arm where pointers are 32-bit aligned but there are data types such as 148 * that is required to ensure that the context !! 143 * 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 */ 144 */ 153 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN 145 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN 154 146 155 #define CRYPTO_MINALIGN_ATTR __attribute__ ((_ 147 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN))) 156 148 >> 149 struct scatterlist; >> 150 struct crypto_ablkcipher; >> 151 struct crypto_async_request; >> 152 struct crypto_blkcipher; 157 struct crypto_tfm; 153 struct crypto_tfm; 158 struct crypto_type; 154 struct crypto_type; 159 struct module; !! 155 struct skcipher_givcrypt_request; 160 156 161 typedef void (*crypto_completion_t)(void *req, !! 157 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err); 162 158 163 /** 159 /** 164 * DOC: Block Cipher Context Data Structures 160 * DOC: Block Cipher Context Data Structures 165 * 161 * 166 * These data structures define the operating 162 * These data structures define the operating context for each block cipher 167 * type. 163 * type. 168 */ 164 */ 169 165 170 struct crypto_async_request { 166 struct crypto_async_request { 171 struct list_head list; 167 struct list_head list; 172 crypto_completion_t complete; 168 crypto_completion_t complete; 173 void *data; 169 void *data; 174 struct crypto_tfm *tfm; 170 struct crypto_tfm *tfm; 175 171 176 u32 flags; 172 u32 flags; 177 }; 173 }; 178 174 >> 175 struct ablkcipher_request { >> 176 struct crypto_async_request base; >> 177 >> 178 unsigned int nbytes; >> 179 >> 180 void *info; >> 181 >> 182 struct scatterlist *src; >> 183 struct scatterlist *dst; >> 184 >> 185 void *__ctx[] CRYPTO_MINALIGN_ATTR; >> 186 }; >> 187 >> 188 struct blkcipher_desc { >> 189 struct crypto_blkcipher *tfm; >> 190 void *info; >> 191 u32 flags; >> 192 }; >> 193 >> 194 struct cipher_desc { >> 195 struct crypto_tfm *tfm; >> 196 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); >> 197 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst, >> 198 const u8 *src, unsigned int nbytes); >> 199 void *info; >> 200 }; >> 201 179 /** 202 /** 180 * DOC: Block Cipher Algorithm Definitions 203 * DOC: Block Cipher Algorithm Definitions 181 * 204 * 182 * These data structures define modular crypto 205 * These data structures define modular crypto algorithm implementations, 183 * managed via crypto_register_alg() and crypt 206 * managed via crypto_register_alg() and crypto_unregister_alg(). 184 */ 207 */ 185 208 186 /** 209 /** >> 210 * struct ablkcipher_alg - asynchronous block cipher definition >> 211 * @min_keysize: Minimum key size supported by the transformation. This is the >> 212 * smallest key length supported by this transformation algorithm. >> 213 * This must be set to one of the pre-defined values as this is >> 214 * not hardware specific. Possible values for this field can be >> 215 * found via git grep "_MIN_KEY_SIZE" include/crypto/ >> 216 * @max_keysize: Maximum key size supported by the transformation. This is the >> 217 * largest key length supported by this transformation algorithm. >> 218 * This must be set to one of the pre-defined values as this is >> 219 * not hardware specific. Possible values for this field can be >> 220 * found via git grep "_MAX_KEY_SIZE" include/crypto/ >> 221 * @setkey: Set key for the transformation. This function is used to either >> 222 * program a supplied key into the hardware or store the key in the >> 223 * transformation context for programming it later. Note that this >> 224 * function does modify the transformation context. This function can >> 225 * be called multiple times during the existence of the transformation >> 226 * object, so one must make sure the key is properly reprogrammed into >> 227 * the hardware. This function is also responsible for checking the key >> 228 * length for validity. In case a software fallback was put in place in >> 229 * the @cra_init call, this function might need to use the fallback if >> 230 * the algorithm doesn't support all of the key sizes. >> 231 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt >> 232 * the supplied scatterlist containing the blocks of data. The crypto >> 233 * API consumer is responsible for aligning the entries of the >> 234 * scatterlist properly and making sure the chunks are correctly >> 235 * sized. In case a software fallback was put in place in the >> 236 * @cra_init call, this function might need to use the fallback if >> 237 * the algorithm doesn't support all of the key sizes. In case the >> 238 * key was stored in transformation context, the key might need to be >> 239 * re-programmed into the hardware in this function. This function >> 240 * shall not modify the transformation context, as this function may >> 241 * be called in parallel with the same transformation object. >> 242 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt >> 243 * and the conditions are exactly the same. >> 244 * @givencrypt: Update the IV for encryption. With this function, a cipher >> 245 * implementation may provide the function on how to update the IV >> 246 * for encryption. >> 247 * @givdecrypt: Update the IV for decryption. This is the reverse of >> 248 * @givencrypt . >> 249 * @geniv: The transformation implementation may use an "IV generator" provided >> 250 * by the kernel crypto API. Several use cases have a predefined >> 251 * approach how IVs are to be updated. For such use cases, the kernel >> 252 * crypto API provides ready-to-use implementations that can be >> 253 * referenced with this variable. >> 254 * @ivsize: IV size applicable for transformation. The consumer must provide an >> 255 * IV of exactly that size to perform the encrypt or decrypt operation. >> 256 * >> 257 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are >> 258 * mandatory and must be filled. >> 259 */ >> 260 struct ablkcipher_alg { >> 261 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, >> 262 unsigned int keylen); >> 263 int (*encrypt)(struct ablkcipher_request *req); >> 264 int (*decrypt)(struct ablkcipher_request *req); >> 265 int (*givencrypt)(struct skcipher_givcrypt_request *req); >> 266 int (*givdecrypt)(struct skcipher_givcrypt_request *req); >> 267 >> 268 const char *geniv; >> 269 >> 270 unsigned int min_keysize; >> 271 unsigned int max_keysize; >> 272 unsigned int ivsize; >> 273 }; >> 274 >> 275 /** >> 276 * struct blkcipher_alg - synchronous block cipher definition >> 277 * @min_keysize: see struct ablkcipher_alg >> 278 * @max_keysize: see struct ablkcipher_alg >> 279 * @setkey: see struct ablkcipher_alg >> 280 * @encrypt: see struct ablkcipher_alg >> 281 * @decrypt: see struct ablkcipher_alg >> 282 * @geniv: see struct ablkcipher_alg >> 283 * @ivsize: see struct ablkcipher_alg >> 284 * >> 285 * All fields except @geniv and @ivsize are mandatory and must be filled. >> 286 */ >> 287 struct blkcipher_alg { >> 288 int (*setkey)(struct crypto_tfm *tfm, const u8 *key, >> 289 unsigned int keylen); >> 290 int (*encrypt)(struct blkcipher_desc *desc, >> 291 struct scatterlist *dst, struct scatterlist *src, >> 292 unsigned int nbytes); >> 293 int (*decrypt)(struct blkcipher_desc *desc, >> 294 struct scatterlist *dst, struct scatterlist *src, >> 295 unsigned int nbytes); >> 296 >> 297 const char *geniv; >> 298 >> 299 unsigned int min_keysize; >> 300 unsigned int max_keysize; >> 301 unsigned int ivsize; >> 302 }; >> 303 >> 304 /** 187 * struct cipher_alg - single-block symmetric 305 * struct cipher_alg - single-block symmetric ciphers definition 188 * @cia_min_keysize: Minimum key size supporte 306 * @cia_min_keysize: Minimum key size supported by the transformation. This is 189 * the smallest key length s 307 * the smallest key length supported by this transformation 190 * algorithm. This must be s 308 * algorithm. This must be set to one of the pre-defined 191 * values as this is not har 309 * values as this is not hardware specific. Possible values 192 * for this field can be fou 310 * for this field can be found via git grep "_MIN_KEY_SIZE" 193 * include/crypto/ 311 * include/crypto/ 194 * @cia_max_keysize: Maximum key size supporte 312 * @cia_max_keysize: Maximum key size supported by the transformation. This is 195 * the largest key length sup 313 * the largest key length supported by this transformation 196 * algorithm. This must be se 314 * algorithm. This must be set to one of the pre-defined values 197 * as this is not hardware sp 315 * as this is not hardware specific. Possible values for this 198 * field can be found via git 316 * field can be found via git grep "_MAX_KEY_SIZE" 199 * include/crypto/ 317 * include/crypto/ 200 * @cia_setkey: Set key for the transformation 318 * @cia_setkey: Set key for the transformation. This function is used to either 201 * program a supplied key into th 319 * program a supplied key into the hardware or store the key in the 202 * transformation context for pro 320 * transformation context for programming it later. Note that this 203 * function does modify the trans 321 * function does modify the transformation context. This function 204 * can be called multiple times d 322 * can be called multiple times during the existence of the 205 * transformation object, so one 323 * transformation object, so one must make sure the key is properly 206 * reprogrammed into the hardware 324 * reprogrammed into the hardware. This function is also 207 * responsible for checking the k 325 * responsible for checking the key length for validity. 208 * @cia_encrypt: Encrypt a single block. This 326 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a 209 * single block of data, which m 327 * single block of data, which must be @cra_blocksize big. This 210 * always operates on a full @cr 328 * always operates on a full @cra_blocksize and it is not possible 211 * to encrypt a block of smaller 329 * to encrypt a block of smaller size. The supplied buffers must 212 * therefore also be at least of 330 * therefore also be at least of @cra_blocksize size. Both the 213 * input and output buffers are 331 * input and output buffers are always aligned to @cra_alignmask. 214 * In case either of the input o 332 * In case either of the input or output buffer supplied by user 215 * of the crypto API is not alig 333 * of the crypto API is not aligned to @cra_alignmask, the crypto 216 * API will re-align the buffers 334 * API will re-align the buffers. The re-alignment means that a 217 * new buffer will be allocated, 335 * new buffer will be allocated, the data will be copied into the 218 * new buffer, then the processi 336 * new buffer, then the processing will happen on the new buffer, 219 * then the data will be copied 337 * then the data will be copied back into the original buffer and 220 * finally the new buffer will b 338 * finally the new buffer will be freed. In case a software 221 * fallback was put in place in 339 * fallback was put in place in the @cra_init call, this function 222 * might need to use the fallbac 340 * might need to use the fallback if the algorithm doesn't support 223 * all of the key sizes. In case 341 * all of the key sizes. In case the key was stored in 224 * transformation context, the k 342 * transformation context, the key might need to be re-programmed 225 * into the hardware in this fun 343 * into the hardware in this function. This function shall not 226 * modify the transformation con 344 * modify the transformation context, as this function may be 227 * called in parallel with the s 345 * called in parallel with the same transformation object. 228 * @cia_decrypt: Decrypt a single block. This 346 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to 229 * @cia_encrypt, and the conditi 347 * @cia_encrypt, and the conditions are exactly the same. 230 * 348 * 231 * All fields are mandatory and must be filled 349 * All fields are mandatory and must be filled. 232 */ 350 */ 233 struct cipher_alg { 351 struct cipher_alg { 234 unsigned int cia_min_keysize; 352 unsigned int cia_min_keysize; 235 unsigned int cia_max_keysize; 353 unsigned int cia_max_keysize; 236 int (*cia_setkey)(struct crypto_tfm *t 354 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key, 237 unsigned int keylen) 355 unsigned int keylen); 238 void (*cia_encrypt)(struct crypto_tfm 356 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 239 void (*cia_decrypt)(struct crypto_tfm 357 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 240 }; 358 }; 241 359 242 /** << 243 * struct compress_alg - compression/decompres << 244 * @coa_compress: Compress a buffer of specifi << 245 * data in the specified buffer << 246 * compressed data in dlen. << 247 * @coa_decompress: Decompress the source buff << 248 * data in the specified buff << 249 * returned in dlen. << 250 * << 251 * All fields are mandatory. << 252 */ << 253 struct compress_alg { 360 struct compress_alg { 254 int (*coa_compress)(struct crypto_tfm 361 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src, 255 unsigned int slen, 362 unsigned int slen, u8 *dst, unsigned int *dlen); 256 int (*coa_decompress)(struct crypto_tf 363 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src, 257 unsigned int sle 364 unsigned int slen, u8 *dst, unsigned int *dlen); 258 }; 365 }; 259 366 >> 367 >> 368 #define cra_ablkcipher cra_u.ablkcipher >> 369 #define cra_blkcipher cra_u.blkcipher 260 #define cra_cipher cra_u.cipher 370 #define cra_cipher cra_u.cipher 261 #define cra_compress cra_u.compress 371 #define cra_compress cra_u.compress 262 372 263 /** 373 /** 264 * struct crypto_alg - definition of a cryptog 374 * struct crypto_alg - definition of a cryptograpic cipher algorithm 265 * @cra_flags: Flags describing this transform 375 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h 266 * CRYPTO_ALG_* flags for the flag 376 * CRYPTO_ALG_* flags for the flags which go in here. Those are 267 * used for fine-tuning the descri 377 * used for fine-tuning the description of the transformation 268 * algorithm. 378 * algorithm. 269 * @cra_blocksize: Minimum block size of this 379 * @cra_blocksize: Minimum block size of this transformation. The size in bytes 270 * of the smallest possible un 380 * of the smallest possible unit which can be transformed with 271 * this algorithm. The users m 381 * this algorithm. The users must respect this value. 272 * In case of HASH transformat 382 * In case of HASH transformation, it is possible for a smaller 273 * block than @cra_blocksize t 383 * block than @cra_blocksize to be passed to the crypto API for 274 * transformation, in case of 384 * transformation, in case of any other transformation type, an 275 * error will be returned upon 385 * error will be returned upon any attempt to transform smaller 276 * than @cra_blocksize chunks. 386 * than @cra_blocksize chunks. 277 * @cra_ctxsize: Size of the operational conte 387 * @cra_ctxsize: Size of the operational context of the transformation. This 278 * value informs the kernel cryp 388 * value informs the kernel crypto API about the memory size 279 * needed to be allocated for th 389 * needed to be allocated for the transformation context. 280 * @cra_alignmask: For cipher, skcipher, lskci !! 390 * @cra_alignmask: Alignment mask for the input and output data buffer. The data 281 * 1 less than the alignment, !! 391 * buffer containing the input data for the algorithm must be 282 * implementation requires for !! 392 * aligned to this alignment mask. The data buffer for the 283 * the crypto API is invoked w !! 393 * output data must be aligned to this alignment mask. Note that 284 * to this alignment, the cryp !! 394 * the Crypto API will do the re-alignment in software, but 285 * appropriately aligned tempo !! 395 * only under special conditions and there is a performance hit. 286 * the algorithm needs. (For !! 396 * The re-alignment happens at these occasions for different 287 * the algorithm uses the skci !! 397 * @cra_u types: cipher -- For both input data and output data 288 * misalignment handling carri !! 398 * buffer; ahash -- For output hash destination buf; shash -- 289 * preferred that algorithms d !! 399 * For output hash destination buf. 290 * Also, crypto API users may !! 400 * This is needed on hardware which is flawed by design and 291 * to the alignmask of the alg !! 401 * 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 402 * @cra_priority: Priority of this transformation implementation. In case 295 * multiple transformations wit 403 * multiple transformations with same @cra_name are available to 296 * the Crypto API, the kernel w 404 * the Crypto API, the kernel will use the one with highest 297 * @cra_priority. 405 * @cra_priority. 298 * @cra_name: Generic name (usable by multiple 406 * @cra_name: Generic name (usable by multiple implementations) of the 299 * transformation algorithm. This i 407 * transformation algorithm. This is the name of the transformation 300 * itself. This field is used by th 408 * itself. This field is used by the kernel when looking up the 301 * providers of particular transfor 409 * providers of particular transformation. 302 * @cra_driver_name: Unique name of the transf 410 * @cra_driver_name: Unique name of the transformation provider. This is the 303 * name of the provider of t 411 * name of the provider of the transformation. This can be any 304 * arbitrary value, but in t 412 * arbitrary value, but in the usual case, this contains the 305 * name of the chip or provi 413 * name of the chip or provider and the name of the 306 * transformation algorithm. 414 * transformation algorithm. 307 * @cra_type: Type of the cryptographic transf 415 * @cra_type: Type of the cryptographic transformation. This is a pointer to 308 * struct crypto_type, which implem 416 * struct crypto_type, which implements callbacks common for all 309 * transformation types. There are !! 417 * transformation types. There are multiple options: 310 * &crypto_skcipher_type, &crypto_a !! 418 * &crypto_blkcipher_type, &crypto_ablkcipher_type, >> 419 * &crypto_ahash_type, &crypto_rng_type. 311 * This field might be empty. In th 420 * This field might be empty. In that case, there are no common 312 * callbacks. This is the case for: 421 * callbacks. This is the case for: cipher, compress, shash. 313 * @cra_u: Callbacks implementing the transfor 422 * @cra_u: Callbacks implementing the transformation. This is a union of 314 * multiple structures. Depending on t 423 * multiple structures. Depending on the type of transformation selected 315 * by @cra_type and @cra_flags above, 424 * by @cra_type and @cra_flags above, the associated structure must be 316 * filled with callbacks. This field m 425 * filled with callbacks. This field might be empty. This is the case 317 * for ahash, shash. 426 * for ahash, shash. 318 * @cra_init: Initialize the cryptographic tra 427 * @cra_init: Initialize the cryptographic transformation object. This function 319 * is used to initialize the crypto 428 * is used to initialize the cryptographic transformation object. 320 * This function is called only onc 429 * This function is called only once at the instantiation time, right 321 * after the transformation context 430 * after the transformation context was allocated. In case the 322 * cryptographic hardware has some 431 * cryptographic hardware has some special requirements which need to 323 * be handled by software, this fun 432 * be handled by software, this function shall check for the precise 324 * requirement of the transformatio 433 * requirement of the transformation and put any software fallbacks 325 * in place. 434 * in place. 326 * @cra_exit: Deinitialize the cryptographic t 435 * @cra_exit: Deinitialize the cryptographic transformation object. This is a 327 * counterpart to @cra_init, used t 436 * counterpart to @cra_init, used to remove various changes set in 328 * @cra_init. 437 * @cra_init. >> 438 * @cra_u.ablkcipher: Union member which contains an asynchronous block cipher >> 439 * definition. See @struct @ablkcipher_alg. >> 440 * @cra_u.blkcipher: Union member which contains a synchronous block cipher >> 441 * definition See @struct @blkcipher_alg. 329 * @cra_u.cipher: Union member which contains 442 * @cra_u.cipher: Union member which contains a single-block symmetric cipher 330 * definition. See @struct @cip 443 * definition. See @struct @cipher_alg. 331 * @cra_u.compress: Union member which contain 444 * @cra_u.compress: Union member which contains a (de)compression algorithm. 332 * See @struct @compress_alg. 445 * See @struct @compress_alg. 333 * @cra_module: Owner of this transformation i 446 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE 334 * @cra_list: internally used 447 * @cra_list: internally used 335 * @cra_users: internally used 448 * @cra_users: internally used 336 * @cra_refcnt: internally used 449 * @cra_refcnt: internally used 337 * @cra_destroy: internally used 450 * @cra_destroy: internally used 338 * 451 * 339 * The struct crypto_alg describes a generic C 452 * The struct crypto_alg describes a generic Crypto API algorithm and is common 340 * for all of the transformations. Any variabl 453 * for all of the transformations. Any variable not documented here shall not 341 * be used by a cipher implementation as it is 454 * be used by a cipher implementation as it is internal to the Crypto API. 342 */ 455 */ 343 struct crypto_alg { 456 struct crypto_alg { 344 struct list_head cra_list; 457 struct list_head cra_list; 345 struct list_head cra_users; 458 struct list_head cra_users; 346 459 347 u32 cra_flags; 460 u32 cra_flags; 348 unsigned int cra_blocksize; 461 unsigned int cra_blocksize; 349 unsigned int cra_ctxsize; 462 unsigned int cra_ctxsize; 350 unsigned int cra_alignmask; 463 unsigned int cra_alignmask; 351 464 352 int cra_priority; 465 int cra_priority; 353 refcount_t cra_refcnt; 466 refcount_t cra_refcnt; 354 467 355 char cra_name[CRYPTO_MAX_ALG_NAME]; 468 char cra_name[CRYPTO_MAX_ALG_NAME]; 356 char cra_driver_name[CRYPTO_MAX_ALG_NA 469 char cra_driver_name[CRYPTO_MAX_ALG_NAME]; 357 470 358 const struct crypto_type *cra_type; 471 const struct crypto_type *cra_type; 359 472 360 union { 473 union { >> 474 struct ablkcipher_alg ablkcipher; >> 475 struct blkcipher_alg blkcipher; 361 struct cipher_alg cipher; 476 struct cipher_alg cipher; 362 struct compress_alg compress; 477 struct compress_alg compress; 363 } cra_u; 478 } cra_u; 364 479 365 int (*cra_init)(struct crypto_tfm *tfm 480 int (*cra_init)(struct crypto_tfm *tfm); 366 void (*cra_exit)(struct crypto_tfm *tf 481 void (*cra_exit)(struct crypto_tfm *tfm); 367 void (*cra_destroy)(struct crypto_alg 482 void (*cra_destroy)(struct crypto_alg *alg); 368 483 369 struct module *cra_module; 484 struct module *cra_module; 370 } CRYPTO_MINALIGN_ATTR; 485 } CRYPTO_MINALIGN_ATTR; 371 486 372 /* 487 /* 373 * A helper struct for waiting for completion 488 * A helper struct for waiting for completion of async crypto ops 374 */ 489 */ 375 struct crypto_wait { 490 struct crypto_wait { 376 struct completion completion; 491 struct completion completion; 377 int err; 492 int err; 378 }; 493 }; 379 494 380 /* 495 /* 381 * Macro for declaring a crypto op async wait 496 * Macro for declaring a crypto op async wait object on stack 382 */ 497 */ 383 #define DECLARE_CRYPTO_WAIT(_wait) \ 498 #define DECLARE_CRYPTO_WAIT(_wait) \ 384 struct crypto_wait _wait = { \ 499 struct crypto_wait _wait = { \ 385 COMPLETION_INITIALIZER_ONSTACK 500 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 } 386 501 387 /* 502 /* 388 * Async ops completion helper functioons 503 * Async ops completion helper functioons 389 */ 504 */ 390 void crypto_req_done(void *req, int err); !! 505 void crypto_req_done(struct crypto_async_request *req, int err); 391 506 392 static inline int crypto_wait_req(int err, str 507 static inline int crypto_wait_req(int err, struct crypto_wait *wait) 393 { 508 { 394 switch (err) { 509 switch (err) { 395 case -EINPROGRESS: 510 case -EINPROGRESS: 396 case -EBUSY: 511 case -EBUSY: 397 wait_for_completion(&wait->com 512 wait_for_completion(&wait->completion); 398 reinit_completion(&wait->compl 513 reinit_completion(&wait->completion); 399 err = wait->err; 514 err = wait->err; 400 break; 515 break; 401 } !! 516 }; 402 517 403 return err; 518 return err; 404 } 519 } 405 520 406 static inline void crypto_init_wait(struct cry 521 static inline void crypto_init_wait(struct crypto_wait *wait) 407 { 522 { 408 init_completion(&wait->completion); 523 init_completion(&wait->completion); 409 } 524 } 410 525 411 /* 526 /* >> 527 * Algorithm registration interface. >> 528 */ >> 529 int crypto_register_alg(struct crypto_alg *alg); >> 530 int crypto_unregister_alg(struct crypto_alg *alg); >> 531 int crypto_register_algs(struct crypto_alg *algs, int count); >> 532 int crypto_unregister_algs(struct crypto_alg *algs, int count); >> 533 >> 534 /* 412 * Algorithm query interface. 535 * Algorithm query interface. 413 */ 536 */ 414 int crypto_has_alg(const char *name, u32 type, 537 int crypto_has_alg(const char *name, u32 type, u32 mask); 415 538 416 /* 539 /* 417 * Transforms: user-instantiated objects which 540 * Transforms: user-instantiated objects which encapsulate algorithms 418 * and core processing logic. Managed via cry 541 * and core processing logic. Managed via crypto_alloc_*() and 419 * crypto_free_*(), as well as the various hel 542 * crypto_free_*(), as well as the various helpers below. 420 */ 543 */ 421 544 >> 545 struct ablkcipher_tfm { >> 546 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, >> 547 unsigned int keylen); >> 548 int (*encrypt)(struct ablkcipher_request *req); >> 549 int (*decrypt)(struct ablkcipher_request *req); >> 550 >> 551 struct crypto_ablkcipher *base; >> 552 >> 553 unsigned int ivsize; >> 554 unsigned int reqsize; >> 555 }; >> 556 >> 557 struct blkcipher_tfm { >> 558 void *iv; >> 559 int (*setkey)(struct crypto_tfm *tfm, const u8 *key, >> 560 unsigned int keylen); >> 561 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, >> 562 struct scatterlist *src, unsigned int nbytes); >> 563 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, >> 564 struct scatterlist *src, unsigned int nbytes); >> 565 }; >> 566 >> 567 struct cipher_tfm { >> 568 int (*cit_setkey)(struct crypto_tfm *tfm, >> 569 const u8 *key, unsigned int keylen); >> 570 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); >> 571 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); >> 572 }; >> 573 >> 574 struct compress_tfm { >> 575 int (*cot_compress)(struct crypto_tfm *tfm, >> 576 const u8 *src, unsigned int slen, >> 577 u8 *dst, unsigned int *dlen); >> 578 int (*cot_decompress)(struct crypto_tfm *tfm, >> 579 const u8 *src, unsigned int slen, >> 580 u8 *dst, unsigned int *dlen); >> 581 }; >> 582 >> 583 #define crt_ablkcipher crt_u.ablkcipher >> 584 #define crt_blkcipher crt_u.blkcipher >> 585 #define crt_cipher crt_u.cipher >> 586 #define crt_compress crt_u.compress >> 587 422 struct crypto_tfm { 588 struct crypto_tfm { 423 refcount_t refcnt; << 424 589 425 u32 crt_flags; 590 u32 crt_flags; 426 << 427 int node; << 428 591 >> 592 union { >> 593 struct ablkcipher_tfm ablkcipher; >> 594 struct blkcipher_tfm blkcipher; >> 595 struct cipher_tfm cipher; >> 596 struct compress_tfm compress; >> 597 } crt_u; >> 598 429 void (*exit)(struct crypto_tfm *tfm); 599 void (*exit)(struct crypto_tfm *tfm); 430 600 431 struct crypto_alg *__crt_alg; 601 struct crypto_alg *__crt_alg; 432 602 433 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR 603 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; 434 }; 604 }; 435 605 >> 606 struct crypto_ablkcipher { >> 607 struct crypto_tfm base; >> 608 }; >> 609 >> 610 struct crypto_blkcipher { >> 611 struct crypto_tfm base; >> 612 }; >> 613 >> 614 struct crypto_cipher { >> 615 struct crypto_tfm base; >> 616 }; >> 617 436 struct crypto_comp { 618 struct crypto_comp { 437 struct crypto_tfm base; 619 struct crypto_tfm base; 438 }; 620 }; 439 621 >> 622 enum { >> 623 CRYPTOA_UNSPEC, >> 624 CRYPTOA_ALG, >> 625 CRYPTOA_TYPE, >> 626 CRYPTOA_U32, >> 627 __CRYPTOA_MAX, >> 628 }; >> 629 >> 630 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1) >> 631 >> 632 /* Maximum number of (rtattr) parameters for each template. */ >> 633 #define CRYPTO_MAX_ATTRS 32 >> 634 >> 635 struct crypto_attr_alg { >> 636 char name[CRYPTO_MAX_ALG_NAME]; >> 637 }; >> 638 >> 639 struct crypto_attr_type { >> 640 u32 type; >> 641 u32 mask; >> 642 }; >> 643 >> 644 struct crypto_attr_u32 { >> 645 u32 num; >> 646 }; >> 647 440 /* 648 /* 441 * Transform user interface. 649 * Transform user interface. 442 */ 650 */ 443 651 444 struct crypto_tfm *crypto_alloc_base(const cha 652 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask); 445 void crypto_destroy_tfm(void *mem, struct cryp 653 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm); 446 654 447 static inline void crypto_free_tfm(struct cryp 655 static inline void crypto_free_tfm(struct crypto_tfm *tfm) 448 { 656 { 449 return crypto_destroy_tfm(tfm, tfm); 657 return crypto_destroy_tfm(tfm, tfm); 450 } 658 } 451 659 >> 660 int alg_test(const char *driver, const char *alg, u32 type, u32 mask); >> 661 452 /* 662 /* 453 * Transform helpers which query the underlyin 663 * Transform helpers which query the underlying algorithm. 454 */ 664 */ 455 static inline const char *crypto_tfm_alg_name( 665 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm) 456 { 666 { 457 return tfm->__crt_alg->cra_name; 667 return tfm->__crt_alg->cra_name; 458 } 668 } 459 669 460 static inline const char *crypto_tfm_alg_drive 670 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm) 461 { 671 { 462 return tfm->__crt_alg->cra_driver_name 672 return tfm->__crt_alg->cra_driver_name; 463 } 673 } 464 674 >> 675 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm) >> 676 { >> 677 return tfm->__crt_alg->cra_priority; >> 678 } >> 679 >> 680 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm) >> 681 { >> 682 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK; >> 683 } >> 684 465 static inline unsigned int crypto_tfm_alg_bloc 685 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm) 466 { 686 { 467 return tfm->__crt_alg->cra_blocksize; 687 return tfm->__crt_alg->cra_blocksize; 468 } 688 } 469 689 470 static inline unsigned int crypto_tfm_alg_alig 690 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm) 471 { 691 { 472 return tfm->__crt_alg->cra_alignmask; 692 return tfm->__crt_alg->cra_alignmask; 473 } 693 } 474 694 475 static inline u32 crypto_tfm_get_flags(struct 695 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm) 476 { 696 { 477 return tfm->crt_flags; 697 return tfm->crt_flags; 478 } 698 } 479 699 480 static inline void crypto_tfm_set_flags(struct 700 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags) 481 { 701 { 482 tfm->crt_flags |= flags; 702 tfm->crt_flags |= flags; 483 } 703 } 484 704 485 static inline void crypto_tfm_clear_flags(stru 705 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags) 486 { 706 { 487 tfm->crt_flags &= ~flags; 707 tfm->crt_flags &= ~flags; 488 } 708 } 489 709 >> 710 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm) >> 711 { >> 712 return tfm->__crt_ctx; >> 713 } >> 714 490 static inline unsigned int crypto_tfm_ctx_alig 715 static inline unsigned int crypto_tfm_ctx_alignment(void) 491 { 716 { 492 struct crypto_tfm *tfm; 717 struct crypto_tfm *tfm; 493 return __alignof__(tfm->__crt_ctx); 718 return __alignof__(tfm->__crt_ctx); 494 } 719 } 495 720 >> 721 /* >> 722 * API wrappers. >> 723 */ >> 724 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast( >> 725 struct crypto_tfm *tfm) >> 726 { >> 727 return (struct crypto_ablkcipher *)tfm; >> 728 } >> 729 >> 730 static inline u32 crypto_skcipher_type(u32 type) >> 731 { >> 732 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); >> 733 type |= CRYPTO_ALG_TYPE_BLKCIPHER; >> 734 return type; >> 735 } >> 736 >> 737 static inline u32 crypto_skcipher_mask(u32 mask) >> 738 { >> 739 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); >> 740 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK; >> 741 return mask; >> 742 } >> 743 >> 744 /** >> 745 * DOC: Asynchronous Block Cipher API >> 746 * >> 747 * Asynchronous block cipher API is used with the ciphers of type >> 748 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto). >> 749 * >> 750 * Asynchronous cipher operations imply that the function invocation for a >> 751 * cipher request returns immediately before the completion of the operation. >> 752 * The cipher request is scheduled as a separate kernel thread and therefore >> 753 * load-balanced on the different CPUs via the process scheduler. To allow >> 754 * the kernel crypto API to inform the caller about the completion of a cipher >> 755 * request, the caller must provide a callback function. That function is >> 756 * invoked with the cipher handle when the request completes. >> 757 * >> 758 * To support the asynchronous operation, additional information than just the >> 759 * cipher handle must be supplied to the kernel crypto API. That additional >> 760 * information is given by filling in the ablkcipher_request data structure. >> 761 * >> 762 * For the asynchronous block cipher API, the state is maintained with the tfm >> 763 * cipher handle. A single tfm can be used across multiple calls and in >> 764 * parallel. For asynchronous block cipher calls, context data supplied and >> 765 * only used by the caller can be referenced the request data structure in >> 766 * addition to the IV used for the cipher request. The maintenance of such >> 767 * state information would be important for a crypto driver implementer to >> 768 * have, because when calling the callback function upon completion of the >> 769 * cipher operation, that callback function may need some information about >> 770 * which operation just finished if it invoked multiple in parallel. This >> 771 * state information is unused by the kernel crypto API. >> 772 */ >> 773 >> 774 static inline struct crypto_tfm *crypto_ablkcipher_tfm( >> 775 struct crypto_ablkcipher *tfm) >> 776 { >> 777 return &tfm->base; >> 778 } >> 779 >> 780 /** >> 781 * crypto_free_ablkcipher() - zeroize and free cipher handle >> 782 * @tfm: cipher handle to be freed >> 783 */ >> 784 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm) >> 785 { >> 786 crypto_free_tfm(crypto_ablkcipher_tfm(tfm)); >> 787 } >> 788 >> 789 /** >> 790 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher. >> 791 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the >> 792 * ablkcipher >> 793 * @type: specifies the type of the cipher >> 794 * @mask: specifies the mask for the cipher >> 795 * >> 796 * Return: true when the ablkcipher is known to the kernel crypto API; false >> 797 * otherwise >> 798 */ >> 799 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type, >> 800 u32 mask) >> 801 { >> 802 return crypto_has_alg(alg_name, crypto_skcipher_type(type), >> 803 crypto_skcipher_mask(mask)); >> 804 } >> 805 >> 806 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt( >> 807 struct crypto_ablkcipher *tfm) >> 808 { >> 809 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher; >> 810 } >> 811 >> 812 /** >> 813 * crypto_ablkcipher_ivsize() - obtain IV size >> 814 * @tfm: cipher handle >> 815 * >> 816 * The size of the IV for the ablkcipher referenced by the cipher handle is >> 817 * returned. This IV size may be zero if the cipher does not need an IV. >> 818 * >> 819 * Return: IV size in bytes >> 820 */ >> 821 static inline unsigned int crypto_ablkcipher_ivsize( >> 822 struct crypto_ablkcipher *tfm) >> 823 { >> 824 return crypto_ablkcipher_crt(tfm)->ivsize; >> 825 } >> 826 >> 827 /** >> 828 * crypto_ablkcipher_blocksize() - obtain block size of cipher >> 829 * @tfm: cipher handle >> 830 * >> 831 * The block size for the ablkcipher referenced with the cipher handle is >> 832 * returned. The caller may use that information to allocate appropriate >> 833 * memory for the data returned by the encryption or decryption operation >> 834 * >> 835 * Return: block size of cipher >> 836 */ >> 837 static inline unsigned int crypto_ablkcipher_blocksize( >> 838 struct crypto_ablkcipher *tfm) >> 839 { >> 840 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm)); >> 841 } >> 842 >> 843 static inline unsigned int crypto_ablkcipher_alignmask( >> 844 struct crypto_ablkcipher *tfm) >> 845 { >> 846 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm)); >> 847 } >> 848 >> 849 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm) >> 850 { >> 851 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm)); >> 852 } >> 853 >> 854 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm, >> 855 u32 flags) >> 856 { >> 857 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags); >> 858 } >> 859 >> 860 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm, >> 861 u32 flags) >> 862 { >> 863 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags); >> 864 } >> 865 >> 866 /** >> 867 * crypto_ablkcipher_setkey() - set key for cipher >> 868 * @tfm: cipher handle >> 869 * @key: buffer holding the key >> 870 * @keylen: length of the key in bytes >> 871 * >> 872 * The caller provided key is set for the ablkcipher referenced by the cipher >> 873 * handle. >> 874 * >> 875 * Note, the key length determines the cipher type. Many block ciphers implement >> 876 * different cipher modes depending on the key size, such as AES-128 vs AES-192 >> 877 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 >> 878 * is performed. >> 879 * >> 880 * Return: 0 if the setting of the key was successful; < 0 if an error occurred >> 881 */ >> 882 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, >> 883 const u8 *key, unsigned int keylen) >> 884 { >> 885 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm); >> 886 >> 887 return crt->setkey(crt->base, key, keylen); >> 888 } >> 889 >> 890 /** >> 891 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request >> 892 * @req: ablkcipher_request out of which the cipher handle is to be obtained >> 893 * >> 894 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request >> 895 * data structure. >> 896 * >> 897 * Return: crypto_ablkcipher handle >> 898 */ >> 899 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm( >> 900 struct ablkcipher_request *req) >> 901 { >> 902 return __crypto_ablkcipher_cast(req->base.tfm); >> 903 } >> 904 >> 905 /** >> 906 * crypto_ablkcipher_encrypt() - encrypt plaintext >> 907 * @req: reference to the ablkcipher_request handle that holds all information >> 908 * needed to perform the cipher operation >> 909 * >> 910 * Encrypt plaintext data using the ablkcipher_request handle. That data >> 911 * structure and how it is filled with data is discussed with the >> 912 * ablkcipher_request_* functions. >> 913 * >> 914 * Return: 0 if the cipher operation was successful; < 0 if an error occurred >> 915 */ >> 916 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req) >> 917 { >> 918 struct ablkcipher_tfm *crt = >> 919 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); >> 920 return crt->encrypt(req); >> 921 } >> 922 >> 923 /** >> 924 * crypto_ablkcipher_decrypt() - decrypt ciphertext >> 925 * @req: reference to the ablkcipher_request handle that holds all information >> 926 * needed to perform the cipher operation >> 927 * >> 928 * Decrypt ciphertext data using the ablkcipher_request handle. That data >> 929 * structure and how it is filled with data is discussed with the >> 930 * ablkcipher_request_* functions. >> 931 * >> 932 * Return: 0 if the cipher operation was successful; < 0 if an error occurred >> 933 */ >> 934 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req) >> 935 { >> 936 struct ablkcipher_tfm *crt = >> 937 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); >> 938 return crt->decrypt(req); >> 939 } >> 940 >> 941 /** >> 942 * DOC: Asynchronous Cipher Request Handle >> 943 * >> 944 * The ablkcipher_request data structure contains all pointers to data >> 945 * required for the asynchronous cipher operation. This includes the cipher >> 946 * handle (which can be used by multiple ablkcipher_request instances), pointer >> 947 * to plaintext and ciphertext, asynchronous callback function, etc. It acts >> 948 * as a handle to the ablkcipher_request_* API calls in a similar way as >> 949 * ablkcipher handle to the crypto_ablkcipher_* API calls. >> 950 */ >> 951 >> 952 /** >> 953 * crypto_ablkcipher_reqsize() - obtain size of the request data structure >> 954 * @tfm: cipher handle >> 955 * >> 956 * Return: number of bytes >> 957 */ >> 958 static inline unsigned int crypto_ablkcipher_reqsize( >> 959 struct crypto_ablkcipher *tfm) >> 960 { >> 961 return crypto_ablkcipher_crt(tfm)->reqsize; >> 962 } >> 963 >> 964 /** >> 965 * ablkcipher_request_set_tfm() - update cipher handle reference in request >> 966 * @req: request handle to be modified >> 967 * @tfm: cipher handle that shall be added to the request handle >> 968 * >> 969 * Allow the caller to replace the existing ablkcipher handle in the request >> 970 * data structure with a different one. >> 971 */ >> 972 static inline void ablkcipher_request_set_tfm( >> 973 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm) >> 974 { >> 975 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base); >> 976 } >> 977 >> 978 static inline struct ablkcipher_request *ablkcipher_request_cast( >> 979 struct crypto_async_request *req) >> 980 { >> 981 return container_of(req, struct ablkcipher_request, base); >> 982 } >> 983 >> 984 /** >> 985 * ablkcipher_request_alloc() - allocate request data structure >> 986 * @tfm: cipher handle to be registered with the request >> 987 * @gfp: memory allocation flag that is handed to kmalloc by the API call. >> 988 * >> 989 * Allocate the request data structure that must be used with the ablkcipher >> 990 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher >> 991 * handle is registered in the request data structure. >> 992 * >> 993 * Return: allocated request handle in case of success, or NULL if out of memory >> 994 */ >> 995 static inline struct ablkcipher_request *ablkcipher_request_alloc( >> 996 struct crypto_ablkcipher *tfm, gfp_t gfp) >> 997 { >> 998 struct ablkcipher_request *req; >> 999 >> 1000 req = kmalloc(sizeof(struct ablkcipher_request) + >> 1001 crypto_ablkcipher_reqsize(tfm), gfp); >> 1002 >> 1003 if (likely(req)) >> 1004 ablkcipher_request_set_tfm(req, tfm); >> 1005 >> 1006 return req; >> 1007 } >> 1008 >> 1009 /** >> 1010 * ablkcipher_request_free() - zeroize and free request data structure >> 1011 * @req: request data structure cipher handle to be freed >> 1012 */ >> 1013 static inline void ablkcipher_request_free(struct ablkcipher_request *req) >> 1014 { >> 1015 kzfree(req); >> 1016 } >> 1017 >> 1018 /** >> 1019 * ablkcipher_request_set_callback() - set asynchronous callback function >> 1020 * @req: request handle >> 1021 * @flags: specify zero or an ORing of the flags >> 1022 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and >> 1023 * increase the wait queue beyond the initial maximum size; >> 1024 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep >> 1025 * @compl: callback function pointer to be registered with the request handle >> 1026 * @data: The data pointer refers to memory that is not used by the kernel >> 1027 * crypto API, but provided to the callback function for it to use. Here, >> 1028 * the caller can provide a reference to memory the callback function can >> 1029 * operate on. As the callback function is invoked asynchronously to the >> 1030 * related functionality, it may need to access data structures of the >> 1031 * related functionality which can be referenced using this pointer. The >> 1032 * callback function can access the memory via the "data" field in the >> 1033 * crypto_async_request data structure provided to the callback function. >> 1034 * >> 1035 * This function allows setting the callback function that is triggered once the >> 1036 * cipher operation completes. >> 1037 * >> 1038 * The callback function is registered with the ablkcipher_request handle and >> 1039 * must comply with the following template:: >> 1040 * >> 1041 * void callback_function(struct crypto_async_request *req, int error) >> 1042 */ >> 1043 static inline void ablkcipher_request_set_callback( >> 1044 struct ablkcipher_request *req, >> 1045 u32 flags, crypto_completion_t compl, void *data) >> 1046 { >> 1047 req->base.complete = compl; >> 1048 req->base.data = data; >> 1049 req->base.flags = flags; >> 1050 } >> 1051 >> 1052 /** >> 1053 * ablkcipher_request_set_crypt() - set data buffers >> 1054 * @req: request handle >> 1055 * @src: source scatter / gather list >> 1056 * @dst: destination scatter / gather list >> 1057 * @nbytes: number of bytes to process from @src >> 1058 * @iv: IV for the cipher operation which must comply with the IV size defined >> 1059 * by crypto_ablkcipher_ivsize >> 1060 * >> 1061 * This function allows setting of the source data and destination data >> 1062 * scatter / gather lists. >> 1063 * >> 1064 * For encryption, the source is treated as the plaintext and the >> 1065 * destination is the ciphertext. For a decryption operation, the use is >> 1066 * reversed - the source is the ciphertext and the destination is the plaintext. >> 1067 */ >> 1068 static inline void ablkcipher_request_set_crypt( >> 1069 struct ablkcipher_request *req, >> 1070 struct scatterlist *src, struct scatterlist *dst, >> 1071 unsigned int nbytes, void *iv) >> 1072 { >> 1073 req->src = src; >> 1074 req->dst = dst; >> 1075 req->nbytes = nbytes; >> 1076 req->info = iv; >> 1077 } >> 1078 >> 1079 /** >> 1080 * DOC: Synchronous Block Cipher API >> 1081 * >> 1082 * The synchronous block cipher API is used with the ciphers of type >> 1083 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto) >> 1084 * >> 1085 * Synchronous calls, have a context in the tfm. But since a single tfm can be >> 1086 * used in multiple calls and in parallel, this info should not be changeable >> 1087 * (unless a lock is used). This applies, for example, to the symmetric key. >> 1088 * However, the IV is changeable, so there is an iv field in blkcipher_tfm >> 1089 * structure for synchronous blkcipher api. So, its the only state info that can >> 1090 * be kept for synchronous calls without using a big lock across a tfm. >> 1091 * >> 1092 * The block cipher API allows the use of a complete cipher, i.e. a cipher >> 1093 * consisting of a template (a block chaining mode) and a single block cipher >> 1094 * primitive (e.g. AES). >> 1095 * >> 1096 * The plaintext data buffer and the ciphertext data buffer are pointed to >> 1097 * by using scatter/gather lists. The cipher operation is performed >> 1098 * on all segments of the provided scatter/gather lists. >> 1099 * >> 1100 * The kernel crypto API supports a cipher operation "in-place" which means that >> 1101 * the caller may provide the same scatter/gather list for the plaintext and >> 1102 * cipher text. After the completion of the cipher operation, the plaintext >> 1103 * data is replaced with the ciphertext data in case of an encryption and vice >> 1104 * versa for a decryption. The caller must ensure that the scatter/gather lists >> 1105 * for the output data point to sufficiently large buffers, i.e. multiples of >> 1106 * the block size of the cipher. >> 1107 */ >> 1108 >> 1109 static inline struct crypto_blkcipher *__crypto_blkcipher_cast( >> 1110 struct crypto_tfm *tfm) >> 1111 { >> 1112 return (struct crypto_blkcipher *)tfm; >> 1113 } >> 1114 >> 1115 static inline struct crypto_blkcipher *crypto_blkcipher_cast( >> 1116 struct crypto_tfm *tfm) >> 1117 { >> 1118 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER); >> 1119 return __crypto_blkcipher_cast(tfm); >> 1120 } >> 1121 >> 1122 /** >> 1123 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle >> 1124 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the >> 1125 * blkcipher cipher >> 1126 * @type: specifies the type of the cipher >> 1127 * @mask: specifies the mask for the cipher >> 1128 * >> 1129 * Allocate a cipher handle for a block cipher. The returned struct >> 1130 * crypto_blkcipher is the cipher handle that is required for any subsequent >> 1131 * API invocation for that block cipher. >> 1132 * >> 1133 * Return: allocated cipher handle in case of success; IS_ERR() is true in case >> 1134 * of an error, PTR_ERR() returns the error code. >> 1135 */ >> 1136 static inline struct crypto_blkcipher *crypto_alloc_blkcipher( >> 1137 const char *alg_name, u32 type, u32 mask) >> 1138 { >> 1139 type &= ~CRYPTO_ALG_TYPE_MASK; >> 1140 type |= CRYPTO_ALG_TYPE_BLKCIPHER; >> 1141 mask |= CRYPTO_ALG_TYPE_MASK; >> 1142 >> 1143 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask)); >> 1144 } >> 1145 >> 1146 static inline struct crypto_tfm *crypto_blkcipher_tfm( >> 1147 struct crypto_blkcipher *tfm) >> 1148 { >> 1149 return &tfm->base; >> 1150 } >> 1151 >> 1152 /** >> 1153 * crypto_free_blkcipher() - zeroize and free the block cipher handle >> 1154 * @tfm: cipher handle to be freed >> 1155 */ >> 1156 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm) >> 1157 { >> 1158 crypto_free_tfm(crypto_blkcipher_tfm(tfm)); >> 1159 } >> 1160 >> 1161 /** >> 1162 * crypto_has_blkcipher() - Search for the availability of a block cipher >> 1163 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the >> 1164 * block cipher >> 1165 * @type: specifies the type of the cipher >> 1166 * @mask: specifies the mask for the cipher >> 1167 * >> 1168 * Return: true when the block cipher is known to the kernel crypto API; false >> 1169 * otherwise >> 1170 */ >> 1171 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask) >> 1172 { >> 1173 type &= ~CRYPTO_ALG_TYPE_MASK; >> 1174 type |= CRYPTO_ALG_TYPE_BLKCIPHER; >> 1175 mask |= CRYPTO_ALG_TYPE_MASK; >> 1176 >> 1177 return crypto_has_alg(alg_name, type, mask); >> 1178 } >> 1179 >> 1180 /** >> 1181 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle >> 1182 * @tfm: cipher handle >> 1183 * >> 1184 * Return: The character string holding the name of the cipher >> 1185 */ >> 1186 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm) >> 1187 { >> 1188 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm)); >> 1189 } >> 1190 >> 1191 static inline struct blkcipher_tfm *crypto_blkcipher_crt( >> 1192 struct crypto_blkcipher *tfm) >> 1193 { >> 1194 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher; >> 1195 } >> 1196 >> 1197 static inline struct blkcipher_alg *crypto_blkcipher_alg( >> 1198 struct crypto_blkcipher *tfm) >> 1199 { >> 1200 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher; >> 1201 } >> 1202 >> 1203 /** >> 1204 * crypto_blkcipher_ivsize() - obtain IV size >> 1205 * @tfm: cipher handle >> 1206 * >> 1207 * The size of the IV for the block cipher referenced by the cipher handle is >> 1208 * returned. This IV size may be zero if the cipher does not need an IV. >> 1209 * >> 1210 * Return: IV size in bytes >> 1211 */ >> 1212 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm) >> 1213 { >> 1214 return crypto_blkcipher_alg(tfm)->ivsize; >> 1215 } >> 1216 >> 1217 /** >> 1218 * crypto_blkcipher_blocksize() - obtain block size of cipher >> 1219 * @tfm: cipher handle >> 1220 * >> 1221 * The block size for the block cipher referenced with the cipher handle is >> 1222 * returned. The caller may use that information to allocate appropriate >> 1223 * memory for the data returned by the encryption or decryption operation. >> 1224 * >> 1225 * Return: block size of cipher >> 1226 */ >> 1227 static inline unsigned int crypto_blkcipher_blocksize( >> 1228 struct crypto_blkcipher *tfm) >> 1229 { >> 1230 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm)); >> 1231 } >> 1232 >> 1233 static inline unsigned int crypto_blkcipher_alignmask( >> 1234 struct crypto_blkcipher *tfm) >> 1235 { >> 1236 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm)); >> 1237 } >> 1238 >> 1239 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm) >> 1240 { >> 1241 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm)); >> 1242 } >> 1243 >> 1244 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm, >> 1245 u32 flags) >> 1246 { >> 1247 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags); >> 1248 } >> 1249 >> 1250 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm, >> 1251 u32 flags) >> 1252 { >> 1253 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags); >> 1254 } >> 1255 >> 1256 /** >> 1257 * crypto_blkcipher_setkey() - set key for cipher >> 1258 * @tfm: cipher handle >> 1259 * @key: buffer holding the key >> 1260 * @keylen: length of the key in bytes >> 1261 * >> 1262 * The caller provided key is set for the block cipher referenced by the cipher >> 1263 * handle. >> 1264 * >> 1265 * Note, the key length determines the cipher type. Many block ciphers implement >> 1266 * different cipher modes depending on the key size, such as AES-128 vs AES-192 >> 1267 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 >> 1268 * is performed. >> 1269 * >> 1270 * Return: 0 if the setting of the key was successful; < 0 if an error occurred >> 1271 */ >> 1272 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm, >> 1273 const u8 *key, unsigned int keylen) >> 1274 { >> 1275 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm), >> 1276 key, keylen); >> 1277 } >> 1278 >> 1279 /** >> 1280 * crypto_blkcipher_encrypt() - encrypt plaintext >> 1281 * @desc: reference to the block cipher handle with meta data >> 1282 * @dst: scatter/gather list that is filled by the cipher operation with the >> 1283 * ciphertext >> 1284 * @src: scatter/gather list that holds the plaintext >> 1285 * @nbytes: number of bytes of the plaintext to encrypt. >> 1286 * >> 1287 * Encrypt plaintext data using the IV set by the caller with a preceding >> 1288 * call of crypto_blkcipher_set_iv. >> 1289 * >> 1290 * The blkcipher_desc data structure must be filled by the caller and can >> 1291 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled >> 1292 * with the block cipher handle; desc.flags is filled with either >> 1293 * CRYPTO_TFM_REQ_MAY_SLEEP or 0. >> 1294 * >> 1295 * Return: 0 if the cipher operation was successful; < 0 if an error occurred >> 1296 */ >> 1297 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc, >> 1298 struct scatterlist *dst, >> 1299 struct scatterlist *src, >> 1300 unsigned int nbytes) >> 1301 { >> 1302 desc->info = crypto_blkcipher_crt(desc->tfm)->iv; >> 1303 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); >> 1304 } >> 1305 >> 1306 /** >> 1307 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV >> 1308 * @desc: reference to the block cipher handle with meta data >> 1309 * @dst: scatter/gather list that is filled by the cipher operation with the >> 1310 * ciphertext >> 1311 * @src: scatter/gather list that holds the plaintext >> 1312 * @nbytes: number of bytes of the plaintext to encrypt. >> 1313 * >> 1314 * Encrypt plaintext data with the use of an IV that is solely used for this >> 1315 * cipher operation. Any previously set IV is not used. >> 1316 * >> 1317 * The blkcipher_desc data structure must be filled by the caller and can >> 1318 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled >> 1319 * with the block cipher handle; desc.info is filled with the IV to be used for >> 1320 * the current operation; desc.flags is filled with either >> 1321 * CRYPTO_TFM_REQ_MAY_SLEEP or 0. >> 1322 * >> 1323 * Return: 0 if the cipher operation was successful; < 0 if an error occurred >> 1324 */ >> 1325 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc, >> 1326 struct scatterlist *dst, >> 1327 struct scatterlist *src, >> 1328 unsigned int nbytes) >> 1329 { >> 1330 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); >> 1331 } >> 1332 >> 1333 /** >> 1334 * crypto_blkcipher_decrypt() - decrypt ciphertext >> 1335 * @desc: reference to the block cipher handle with meta data >> 1336 * @dst: scatter/gather list that is filled by the cipher operation with the >> 1337 * plaintext >> 1338 * @src: scatter/gather list that holds the ciphertext >> 1339 * @nbytes: number of bytes of the ciphertext to decrypt. >> 1340 * >> 1341 * Decrypt ciphertext data using the IV set by the caller with a preceding >> 1342 * call of crypto_blkcipher_set_iv. >> 1343 * >> 1344 * The blkcipher_desc data structure must be filled by the caller as documented >> 1345 * for the crypto_blkcipher_encrypt call above. >> 1346 * >> 1347 * Return: 0 if the cipher operation was successful; < 0 if an error occurred >> 1348 * >> 1349 */ >> 1350 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc, >> 1351 struct scatterlist *dst, >> 1352 struct scatterlist *src, >> 1353 unsigned int nbytes) >> 1354 { >> 1355 desc->info = crypto_blkcipher_crt(desc->tfm)->iv; >> 1356 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); >> 1357 } >> 1358 >> 1359 /** >> 1360 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV >> 1361 * @desc: reference to the block cipher handle with meta data >> 1362 * @dst: scatter/gather list that is filled by the cipher operation with the >> 1363 * plaintext >> 1364 * @src: scatter/gather list that holds the ciphertext >> 1365 * @nbytes: number of bytes of the ciphertext to decrypt. >> 1366 * >> 1367 * Decrypt ciphertext data with the use of an IV that is solely used for this >> 1368 * cipher operation. Any previously set IV is not used. >> 1369 * >> 1370 * The blkcipher_desc data structure must be filled by the caller as documented >> 1371 * for the crypto_blkcipher_encrypt_iv call above. >> 1372 * >> 1373 * Return: 0 if the cipher operation was successful; < 0 if an error occurred >> 1374 */ >> 1375 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc, >> 1376 struct scatterlist *dst, >> 1377 struct scatterlist *src, >> 1378 unsigned int nbytes) >> 1379 { >> 1380 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); >> 1381 } >> 1382 >> 1383 /** >> 1384 * crypto_blkcipher_set_iv() - set IV for cipher >> 1385 * @tfm: cipher handle >> 1386 * @src: buffer holding the IV >> 1387 * @len: length of the IV in bytes >> 1388 * >> 1389 * The caller provided IV is set for the block cipher referenced by the cipher >> 1390 * handle. >> 1391 */ >> 1392 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm, >> 1393 const u8 *src, unsigned int len) >> 1394 { >> 1395 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len); >> 1396 } >> 1397 >> 1398 /** >> 1399 * crypto_blkcipher_get_iv() - obtain IV from cipher >> 1400 * @tfm: cipher handle >> 1401 * @dst: buffer filled with the IV >> 1402 * @len: length of the buffer dst >> 1403 * >> 1404 * The caller can obtain the IV set for the block cipher referenced by the >> 1405 * cipher handle and store it into the user-provided buffer. If the buffer >> 1406 * has an insufficient space, the IV is truncated to fit the buffer. >> 1407 */ >> 1408 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm, >> 1409 u8 *dst, unsigned int len) >> 1410 { >> 1411 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len); >> 1412 } >> 1413 >> 1414 /** >> 1415 * DOC: Single Block Cipher API >> 1416 * >> 1417 * The single block cipher API is used with the ciphers of type >> 1418 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto). >> 1419 * >> 1420 * Using the single block cipher API calls, operations with the basic cipher >> 1421 * primitive can be implemented. These cipher primitives exclude any block >> 1422 * chaining operations including IV handling. >> 1423 * >> 1424 * The purpose of this single block cipher API is to support the implementation >> 1425 * of templates or other concepts that only need to perform the cipher operation >> 1426 * on one block at a time. Templates invoke the underlying cipher primitive >> 1427 * block-wise and process either the input or the output data of these cipher >> 1428 * operations. >> 1429 */ >> 1430 >> 1431 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm) >> 1432 { >> 1433 return (struct crypto_cipher *)tfm; >> 1434 } >> 1435 >> 1436 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm) >> 1437 { >> 1438 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); >> 1439 return __crypto_cipher_cast(tfm); >> 1440 } >> 1441 >> 1442 /** >> 1443 * crypto_alloc_cipher() - allocate single block cipher handle >> 1444 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the >> 1445 * single block cipher >> 1446 * @type: specifies the type of the cipher >> 1447 * @mask: specifies the mask for the cipher >> 1448 * >> 1449 * Allocate a cipher handle for a single block cipher. The returned struct >> 1450 * crypto_cipher is the cipher handle that is required for any subsequent API >> 1451 * invocation for that single block cipher. >> 1452 * >> 1453 * Return: allocated cipher handle in case of success; IS_ERR() is true in case >> 1454 * of an error, PTR_ERR() returns the error code. >> 1455 */ >> 1456 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name, >> 1457 u32 type, u32 mask) >> 1458 { >> 1459 type &= ~CRYPTO_ALG_TYPE_MASK; >> 1460 type |= CRYPTO_ALG_TYPE_CIPHER; >> 1461 mask |= CRYPTO_ALG_TYPE_MASK; >> 1462 >> 1463 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask)); >> 1464 } >> 1465 >> 1466 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm) >> 1467 { >> 1468 return &tfm->base; >> 1469 } >> 1470 >> 1471 /** >> 1472 * crypto_free_cipher() - zeroize and free the single block cipher handle >> 1473 * @tfm: cipher handle to be freed >> 1474 */ >> 1475 static inline void crypto_free_cipher(struct crypto_cipher *tfm) >> 1476 { >> 1477 crypto_free_tfm(crypto_cipher_tfm(tfm)); >> 1478 } >> 1479 >> 1480 /** >> 1481 * crypto_has_cipher() - Search for the availability of a single block cipher >> 1482 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the >> 1483 * single block cipher >> 1484 * @type: specifies the type of the cipher >> 1485 * @mask: specifies the mask for the cipher >> 1486 * >> 1487 * Return: true when the single block cipher is known to the kernel crypto API; >> 1488 * false otherwise >> 1489 */ >> 1490 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask) >> 1491 { >> 1492 type &= ~CRYPTO_ALG_TYPE_MASK; >> 1493 type |= CRYPTO_ALG_TYPE_CIPHER; >> 1494 mask |= CRYPTO_ALG_TYPE_MASK; >> 1495 >> 1496 return crypto_has_alg(alg_name, type, mask); >> 1497 } >> 1498 >> 1499 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm) >> 1500 { >> 1501 return &crypto_cipher_tfm(tfm)->crt_cipher; >> 1502 } >> 1503 >> 1504 /** >> 1505 * crypto_cipher_blocksize() - obtain block size for cipher >> 1506 * @tfm: cipher handle >> 1507 * >> 1508 * The block size for the single block cipher referenced with the cipher handle >> 1509 * tfm is returned. The caller may use that information to allocate appropriate >> 1510 * memory for the data returned by the encryption or decryption operation >> 1511 * >> 1512 * Return: block size of cipher >> 1513 */ >> 1514 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm) >> 1515 { >> 1516 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm)); >> 1517 } >> 1518 >> 1519 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm) >> 1520 { >> 1521 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm)); >> 1522 } >> 1523 >> 1524 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm) >> 1525 { >> 1526 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm)); >> 1527 } >> 1528 >> 1529 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm, >> 1530 u32 flags) >> 1531 { >> 1532 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags); >> 1533 } >> 1534 >> 1535 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm, >> 1536 u32 flags) >> 1537 { >> 1538 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags); >> 1539 } >> 1540 >> 1541 /** >> 1542 * crypto_cipher_setkey() - set key for cipher >> 1543 * @tfm: cipher handle >> 1544 * @key: buffer holding the key >> 1545 * @keylen: length of the key in bytes >> 1546 * >> 1547 * The caller provided key is set for the single block cipher referenced by the >> 1548 * cipher handle. >> 1549 * >> 1550 * Note, the key length determines the cipher type. Many block ciphers implement >> 1551 * different cipher modes depending on the key size, such as AES-128 vs AES-192 >> 1552 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 >> 1553 * is performed. >> 1554 * >> 1555 * Return: 0 if the setting of the key was successful; < 0 if an error occurred >> 1556 */ >> 1557 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm, >> 1558 const u8 *key, unsigned int keylen) >> 1559 { >> 1560 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm), >> 1561 key, keylen); >> 1562 } >> 1563 >> 1564 /** >> 1565 * crypto_cipher_encrypt_one() - encrypt one block of plaintext >> 1566 * @tfm: cipher handle >> 1567 * @dst: points to the buffer that will be filled with the ciphertext >> 1568 * @src: buffer holding the plaintext to be encrypted >> 1569 * >> 1570 * Invoke the encryption operation of one block. The caller must ensure that >> 1571 * the plaintext and ciphertext buffers are at least one block in size. >> 1572 */ >> 1573 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm, >> 1574 u8 *dst, const u8 *src) >> 1575 { >> 1576 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm), >> 1577 dst, src); >> 1578 } >> 1579 >> 1580 /** >> 1581 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext >> 1582 * @tfm: cipher handle >> 1583 * @dst: points to the buffer that will be filled with the plaintext >> 1584 * @src: buffer holding the ciphertext to be decrypted >> 1585 * >> 1586 * Invoke the decryption operation of one block. The caller must ensure that >> 1587 * the plaintext and ciphertext buffers are at least one block in size. >> 1588 */ >> 1589 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, >> 1590 u8 *dst, const u8 *src) >> 1591 { >> 1592 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm), >> 1593 dst, src); >> 1594 } >> 1595 496 static inline struct crypto_comp *__crypto_com 1596 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm) 497 { 1597 { 498 return (struct crypto_comp *)tfm; 1598 return (struct crypto_comp *)tfm; 499 } 1599 } 500 1600 >> 1601 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm) >> 1602 { >> 1603 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) & >> 1604 CRYPTO_ALG_TYPE_MASK); >> 1605 return __crypto_comp_cast(tfm); >> 1606 } >> 1607 501 static inline struct crypto_comp *crypto_alloc 1608 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name, 502 1609 u32 type, u32 mask) 503 { 1610 { 504 type &= ~CRYPTO_ALG_TYPE_MASK; 1611 type &= ~CRYPTO_ALG_TYPE_MASK; 505 type |= CRYPTO_ALG_TYPE_COMPRESS; 1612 type |= CRYPTO_ALG_TYPE_COMPRESS; 506 mask |= CRYPTO_ALG_TYPE_MASK; 1613 mask |= CRYPTO_ALG_TYPE_MASK; 507 1614 508 return __crypto_comp_cast(crypto_alloc 1615 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask)); 509 } 1616 } 510 1617 511 static inline struct crypto_tfm *crypto_comp_t 1618 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm) 512 { 1619 { 513 return &tfm->base; 1620 return &tfm->base; 514 } 1621 } 515 1622 516 static inline void crypto_free_comp(struct cry 1623 static inline void crypto_free_comp(struct crypto_comp *tfm) 517 { 1624 { 518 crypto_free_tfm(crypto_comp_tfm(tfm)); 1625 crypto_free_tfm(crypto_comp_tfm(tfm)); 519 } 1626 } 520 1627 521 static inline int crypto_has_comp(const char * 1628 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask) 522 { 1629 { 523 type &= ~CRYPTO_ALG_TYPE_MASK; 1630 type &= ~CRYPTO_ALG_TYPE_MASK; 524 type |= CRYPTO_ALG_TYPE_COMPRESS; 1631 type |= CRYPTO_ALG_TYPE_COMPRESS; 525 mask |= CRYPTO_ALG_TYPE_MASK; 1632 mask |= CRYPTO_ALG_TYPE_MASK; 526 1633 527 return crypto_has_alg(alg_name, type, 1634 return crypto_has_alg(alg_name, type, mask); 528 } 1635 } 529 1636 530 static inline const char *crypto_comp_name(str 1637 static inline const char *crypto_comp_name(struct crypto_comp *tfm) 531 { 1638 { 532 return crypto_tfm_alg_name(crypto_comp 1639 return crypto_tfm_alg_name(crypto_comp_tfm(tfm)); 533 } 1640 } 534 1641 535 int crypto_comp_compress(struct crypto_comp *t !! 1642 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm) 536 const u8 *src, unsign !! 1643 { 537 u8 *dst, unsigned int !! 1644 return &crypto_comp_tfm(tfm)->crt_compress; 538 !! 1645 } 539 int crypto_comp_decompress(struct crypto_comp !! 1646 540 const u8 *src, unsi !! 1647 static inline int crypto_comp_compress(struct crypto_comp *tfm, 541 u8 *dst, unsigned i !! 1648 const u8 *src, unsigned int slen, >> 1649 u8 *dst, unsigned int *dlen) >> 1650 { >> 1651 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm), >> 1652 src, slen, dst, dlen); >> 1653 } >> 1654 >> 1655 static inline int crypto_comp_decompress(struct crypto_comp *tfm, >> 1656 const u8 *src, unsigned int slen, >> 1657 u8 *dst, unsigned int *dlen) >> 1658 { >> 1659 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm), >> 1660 src, slen, dst, dlen); >> 1661 } 542 1662 543 #endif /* _LINUX_CRYPTO_H */ 1663 #endif /* _LINUX_CRYPTO_H */ 544 1664 545 1665
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.