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