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