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