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