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