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