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