Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Scatterlist Cryptographic API. |
| 3 | * |
| 4 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
| 5 | * Copyright (c) 2002 David S. Miller (davem@redhat.com) |
Herbert Xu | 5cb1454 | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 6 | * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no> |
John Anthony Kazos Jr | 18735dd | 2007-10-19 23:07:36 +0200 | [diff] [blame] | 9 | * and Nettle, by Niels Möller. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by the Free |
| 13 | * Software Foundation; either version 2 of the License, or (at your option) |
| 14 | * any later version. |
| 15 | * |
| 16 | */ |
| 17 | #ifndef _LINUX_CRYPTO_H |
| 18 | #define _LINUX_CRYPTO_H |
| 19 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 20 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/list.h> |
Paul Gortmaker | 187f188 | 2011-11-23 20:12:59 -0500 | [diff] [blame] | 23 | #include <linux/bug.h> |
Herbert Xu | 7991110 | 2006-08-21 21:03:52 +1000 | [diff] [blame] | 24 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/string.h> |
Herbert Xu | 7991110 | 2006-08-21 21:03:52 +1000 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
Kees Cook | e635e0d | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 29 | * Autoloaded crypto modules should only use a prefixed name to avoid allowing |
| 30 | * arbitrary modules to be loaded. Loading from userspace may still need the |
| 31 | * unprefixed names, so retains those aliases as well. |
| 32 | * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3 |
| 33 | * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro |
| 34 | * expands twice on the same line. Instead, use a separate base name for the |
| 35 | * alias. |
| 36 | */ |
| 37 | #define MODULE_ALIAS_CRYPTO(name) \ |
| 38 | __MODULE_INFO(alias, alias_userspace, name); \ |
| 39 | __MODULE_INFO(alias, alias_crypto, "crypto-" name) |
| 40 | |
| 41 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | * Algorithm masks and types. |
| 43 | */ |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 44 | #define CRYPTO_ALG_TYPE_MASK 0x0000000f |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #define CRYPTO_ALG_TYPE_CIPHER 0x00000001 |
Loc Ho | 004a403 | 2008-05-14 20:41:47 +0800 | [diff] [blame] | 46 | #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002 |
| 47 | #define CRYPTO_ALG_TYPE_AEAD 0x00000003 |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 48 | #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004 |
Herbert Xu | 332f8840 | 2007-11-15 22:36:07 +0800 | [diff] [blame] | 49 | #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005 |
Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 50 | #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006 |
Loc Ho | 004a403 | 2008-05-14 20:41:47 +0800 | [diff] [blame] | 51 | #define CRYPTO_ALG_TYPE_DIGEST 0x00000008 |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 52 | #define CRYPTO_ALG_TYPE_HASH 0x00000008 |
| 53 | #define CRYPTO_ALG_TYPE_SHASH 0x00000009 |
Loc Ho | 004a403 | 2008-05-14 20:41:47 +0800 | [diff] [blame] | 54 | #define CRYPTO_ALG_TYPE_AHASH 0x0000000a |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 55 | #define CRYPTO_ALG_TYPE_RNG 0x0000000c |
Geert Uytterhoeven | a1d2f09 | 2009-03-04 15:05:33 +0800 | [diff] [blame] | 56 | #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 57 | |
| 58 | #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e |
Loc Ho | 004a403 | 2008-05-14 20:41:47 +0800 | [diff] [blame] | 59 | #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c |
Herbert Xu | 332f8840 | 2007-11-15 22:36:07 +0800 | [diff] [blame] | 60 | #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 62 | #define CRYPTO_ALG_LARVAL 0x00000010 |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 63 | #define CRYPTO_ALG_DEAD 0x00000020 |
| 64 | #define CRYPTO_ALG_DYING 0x00000040 |
Herbert Xu | f3f632d | 2006-08-06 23:12:59 +1000 | [diff] [blame] | 65 | #define CRYPTO_ALG_ASYNC 0x00000080 |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* |
Herbert Xu | 6010439 | 2006-08-26 18:34:10 +1000 | [diff] [blame] | 68 | * Set this bit if and only if the algorithm requires another algorithm of |
| 69 | * the same type to handle corner cases. |
| 70 | */ |
| 71 | #define CRYPTO_ALG_NEED_FALLBACK 0x00000100 |
| 72 | |
| 73 | /* |
Herbert Xu | ecfc432 | 2007-12-05 21:08:36 +1100 | [diff] [blame] | 74 | * This bit is set for symmetric key ciphers that have already been wrapped |
| 75 | * with a generic IV generator to prevent them from being wrapped again. |
| 76 | */ |
| 77 | #define CRYPTO_ALG_GENIV 0x00000200 |
| 78 | |
| 79 | /* |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 80 | * Set if the algorithm has passed automated run-time testing. Note that |
| 81 | * if there is no run-time testing for a given algorithm it is considered |
| 82 | * to have passed. |
| 83 | */ |
| 84 | |
| 85 | #define CRYPTO_ALG_TESTED 0x00000400 |
| 86 | |
| 87 | /* |
Steffen Klassert | 64a947b | 2011-09-27 07:21:26 +0200 | [diff] [blame] | 88 | * Set if the algorithm is an instance that is build from templates. |
| 89 | */ |
| 90 | #define CRYPTO_ALG_INSTANCE 0x00000800 |
| 91 | |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 92 | /* Set this bit if the algorithm provided is hardware accelerated but |
| 93 | * not available to userspace via instruction set or so. |
| 94 | */ |
| 95 | #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000 |
| 96 | |
Steffen Klassert | 64a947b | 2011-09-27 07:21:26 +0200 | [diff] [blame] | 97 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | * Transform masks and values (for crt_flags). |
| 99 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | #define CRYPTO_TFM_REQ_MASK 0x000fff00 |
| 101 | #define CRYPTO_TFM_RES_MASK 0xfff00000 |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100 |
Herbert Xu | 64baf3c | 2005-09-01 17:43:05 -0700 | [diff] [blame] | 104 | #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 105 | #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000 |
| 107 | #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000 |
| 108 | #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000 |
| 109 | #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000 |
| 110 | #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000 |
| 111 | |
| 112 | /* |
| 113 | * Miscellaneous stuff. |
| 114 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | #define CRYPTO_MAX_ALG_NAME 64 |
| 116 | |
Herbert Xu | 7991110 | 2006-08-21 21:03:52 +1000 | [diff] [blame] | 117 | /* |
| 118 | * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual |
| 119 | * declaration) is used to ensure that the crypto_tfm context structure is |
| 120 | * aligned correctly for the given architecture so that there are no alignment |
| 121 | * faults for C data types. In particular, this is required on platforms such |
| 122 | * as arm where pointers are 32-bit aligned but there are data types such as |
| 123 | * u64 which require 64-bit alignment. |
| 124 | */ |
Herbert Xu | 7991110 | 2006-08-21 21:03:52 +1000 | [diff] [blame] | 125 | #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN |
Herbert Xu | 7991110 | 2006-08-21 21:03:52 +1000 | [diff] [blame] | 126 | |
Herbert Xu | 7991110 | 2006-08-21 21:03:52 +1000 | [diff] [blame] | 127 | #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN))) |
Herbert Xu | 7991110 | 2006-08-21 21:03:52 +1000 | [diff] [blame] | 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | struct scatterlist; |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 130 | struct crypto_ablkcipher; |
| 131 | struct crypto_async_request; |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 132 | struct crypto_aead; |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 133 | struct crypto_blkcipher; |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 134 | struct crypto_hash; |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 135 | struct crypto_rng; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 136 | struct crypto_tfm; |
Herbert Xu | e853c3c | 2006-08-22 00:06:54 +1000 | [diff] [blame] | 137 | struct crypto_type; |
Herbert Xu | 743edf5 | 2007-12-10 16:18:01 +0800 | [diff] [blame] | 138 | struct aead_givcrypt_request; |
Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 139 | struct skcipher_givcrypt_request; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 140 | |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 141 | typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err); |
| 142 | |
| 143 | struct crypto_async_request { |
| 144 | struct list_head list; |
| 145 | crypto_completion_t complete; |
| 146 | void *data; |
| 147 | struct crypto_tfm *tfm; |
| 148 | |
| 149 | u32 flags; |
| 150 | }; |
| 151 | |
| 152 | struct ablkcipher_request { |
| 153 | struct crypto_async_request base; |
| 154 | |
| 155 | unsigned int nbytes; |
| 156 | |
| 157 | void *info; |
| 158 | |
| 159 | struct scatterlist *src; |
| 160 | struct scatterlist *dst; |
| 161 | |
| 162 | void *__ctx[] CRYPTO_MINALIGN_ATTR; |
| 163 | }; |
| 164 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 165 | /** |
| 166 | * struct aead_request - AEAD request |
| 167 | * @base: Common attributes for async crypto requests |
| 168 | * @assoclen: Length in bytes of associated data for authentication |
| 169 | * @cryptlen: Length of data to be encrypted or decrypted |
| 170 | * @iv: Initialisation vector |
| 171 | * @assoc: Associated data |
| 172 | * @src: Source data |
| 173 | * @dst: Destination data |
| 174 | * @__ctx: Start of private context data |
| 175 | */ |
| 176 | struct aead_request { |
| 177 | struct crypto_async_request base; |
| 178 | |
| 179 | unsigned int assoclen; |
| 180 | unsigned int cryptlen; |
| 181 | |
| 182 | u8 *iv; |
| 183 | |
| 184 | struct scatterlist *assoc; |
| 185 | struct scatterlist *src; |
| 186 | struct scatterlist *dst; |
| 187 | |
| 188 | void *__ctx[] CRYPTO_MINALIGN_ATTR; |
| 189 | }; |
| 190 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 191 | struct blkcipher_desc { |
| 192 | struct crypto_blkcipher *tfm; |
| 193 | void *info; |
| 194 | u32 flags; |
| 195 | }; |
| 196 | |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 197 | struct cipher_desc { |
| 198 | struct crypto_tfm *tfm; |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 199 | void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 200 | unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst, |
| 201 | const u8 *src, unsigned int nbytes); |
| 202 | void *info; |
| 203 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 205 | struct hash_desc { |
| 206 | struct crypto_hash *tfm; |
| 207 | u32 flags; |
| 208 | }; |
| 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /* |
| 211 | * Algorithms: modular crypto algorithm implementations, managed |
| 212 | * via crypto_register_alg() and crypto_unregister_alg(). |
| 213 | */ |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 214 | struct ablkcipher_alg { |
| 215 | int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, |
| 216 | unsigned int keylen); |
| 217 | int (*encrypt)(struct ablkcipher_request *req); |
| 218 | int (*decrypt)(struct ablkcipher_request *req); |
Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 219 | int (*givencrypt)(struct skcipher_givcrypt_request *req); |
| 220 | int (*givdecrypt)(struct skcipher_givcrypt_request *req); |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 221 | |
Herbert Xu | 23508e1 | 2007-11-27 21:33:24 +0800 | [diff] [blame] | 222 | const char *geniv; |
| 223 | |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 224 | unsigned int min_keysize; |
| 225 | unsigned int max_keysize; |
| 226 | unsigned int ivsize; |
| 227 | }; |
| 228 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 229 | struct aead_alg { |
| 230 | int (*setkey)(struct crypto_aead *tfm, const u8 *key, |
| 231 | unsigned int keylen); |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 232 | int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 233 | int (*encrypt)(struct aead_request *req); |
| 234 | int (*decrypt)(struct aead_request *req); |
Herbert Xu | 743edf5 | 2007-12-10 16:18:01 +0800 | [diff] [blame] | 235 | int (*givencrypt)(struct aead_givcrypt_request *req); |
| 236 | int (*givdecrypt)(struct aead_givcrypt_request *req); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 237 | |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 238 | const char *geniv; |
| 239 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 240 | unsigned int ivsize; |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 241 | unsigned int maxauthsize; |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 242 | }; |
| 243 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 244 | struct blkcipher_alg { |
| 245 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, |
| 246 | unsigned int keylen); |
| 247 | int (*encrypt)(struct blkcipher_desc *desc, |
| 248 | struct scatterlist *dst, struct scatterlist *src, |
| 249 | unsigned int nbytes); |
| 250 | int (*decrypt)(struct blkcipher_desc *desc, |
| 251 | struct scatterlist *dst, struct scatterlist *src, |
| 252 | unsigned int nbytes); |
| 253 | |
Herbert Xu | 23508e1 | 2007-11-27 21:33:24 +0800 | [diff] [blame] | 254 | const char *geniv; |
| 255 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 256 | unsigned int min_keysize; |
| 257 | unsigned int max_keysize; |
| 258 | unsigned int ivsize; |
| 259 | }; |
| 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | struct cipher_alg { |
| 262 | unsigned int cia_min_keysize; |
| 263 | unsigned int cia_max_keysize; |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 264 | int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 265 | unsigned int keylen); |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 266 | void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
| 267 | void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | }; |
| 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | struct compress_alg { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 271 | int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src, |
| 272 | unsigned int slen, u8 *dst, unsigned int *dlen); |
| 273 | int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src, |
| 274 | unsigned int slen, u8 *dst, unsigned int *dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 277 | struct rng_alg { |
| 278 | int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata, |
| 279 | unsigned int dlen); |
| 280 | int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen); |
| 281 | |
| 282 | unsigned int seedsize; |
| 283 | }; |
| 284 | |
| 285 | |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 286 | #define cra_ablkcipher cra_u.ablkcipher |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 287 | #define cra_aead cra_u.aead |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 288 | #define cra_blkcipher cra_u.blkcipher |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | #define cra_cipher cra_u.cipher |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | #define cra_compress cra_u.compress |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 291 | #define cra_rng cra_u.rng |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | struct crypto_alg { |
| 294 | struct list_head cra_list; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 295 | struct list_head cra_users; |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | u32 cra_flags; |
| 298 | unsigned int cra_blocksize; |
| 299 | unsigned int cra_ctxsize; |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 300 | unsigned int cra_alignmask; |
Herbert Xu | 5cb1454 | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 301 | |
| 302 | int cra_priority; |
Herbert Xu | 6521f30 | 2006-08-06 20:28:44 +1000 | [diff] [blame] | 303 | atomic_t cra_refcnt; |
Herbert Xu | 5cb1454 | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 304 | |
Herbert Xu | d913ea0 | 2006-05-21 08:45:26 +1000 | [diff] [blame] | 305 | char cra_name[CRYPTO_MAX_ALG_NAME]; |
| 306 | char cra_driver_name[CRYPTO_MAX_ALG_NAME]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Herbert Xu | e853c3c | 2006-08-22 00:06:54 +1000 | [diff] [blame] | 308 | const struct crypto_type *cra_type; |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | union { |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 311 | struct ablkcipher_alg ablkcipher; |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 312 | struct aead_alg aead; |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 313 | struct blkcipher_alg blkcipher; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | struct cipher_alg cipher; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | struct compress_alg compress; |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 316 | struct rng_alg rng; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } cra_u; |
Herbert Xu | c7fc059 | 2006-05-24 13:02:26 +1000 | [diff] [blame] | 318 | |
| 319 | int (*cra_init)(struct crypto_tfm *tfm); |
| 320 | void (*cra_exit)(struct crypto_tfm *tfm); |
Herbert Xu | 6521f30 | 2006-08-06 20:28:44 +1000 | [diff] [blame] | 321 | void (*cra_destroy)(struct crypto_alg *alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
| 323 | struct module *cra_module; |
| 324 | }; |
| 325 | |
| 326 | /* |
| 327 | * Algorithm registration interface. |
| 328 | */ |
| 329 | int crypto_register_alg(struct crypto_alg *alg); |
| 330 | int crypto_unregister_alg(struct crypto_alg *alg); |
Mark Brown | 4b00434 | 2012-01-17 23:34:26 +0000 | [diff] [blame] | 331 | int crypto_register_algs(struct crypto_alg *algs, int count); |
| 332 | int crypto_unregister_algs(struct crypto_alg *algs, int count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | /* |
| 335 | * Algorithm query interface. |
| 336 | */ |
Herbert Xu | fce32d7 | 2006-08-26 17:35:45 +1000 | [diff] [blame] | 337 | int crypto_has_alg(const char *name, u32 type, u32 mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * Transforms: user-instantiated objects which encapsulate algorithms |
Herbert Xu | 6d7d684 | 2006-07-30 11:53:01 +1000 | [diff] [blame] | 341 | * and core processing logic. Managed via crypto_alloc_*() and |
| 342 | * crypto_free_*(), as well as the various helpers below. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 345 | struct ablkcipher_tfm { |
| 346 | int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, |
| 347 | unsigned int keylen); |
| 348 | int (*encrypt)(struct ablkcipher_request *req); |
| 349 | int (*decrypt)(struct ablkcipher_request *req); |
Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 350 | int (*givencrypt)(struct skcipher_givcrypt_request *req); |
| 351 | int (*givdecrypt)(struct skcipher_givcrypt_request *req); |
| 352 | |
Herbert Xu | ecfc432 | 2007-12-05 21:08:36 +1100 | [diff] [blame] | 353 | struct crypto_ablkcipher *base; |
| 354 | |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 355 | unsigned int ivsize; |
| 356 | unsigned int reqsize; |
| 357 | }; |
| 358 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 359 | struct aead_tfm { |
| 360 | int (*setkey)(struct crypto_aead *tfm, const u8 *key, |
| 361 | unsigned int keylen); |
| 362 | int (*encrypt)(struct aead_request *req); |
| 363 | int (*decrypt)(struct aead_request *req); |
Herbert Xu | 743edf5 | 2007-12-10 16:18:01 +0800 | [diff] [blame] | 364 | int (*givencrypt)(struct aead_givcrypt_request *req); |
| 365 | int (*givdecrypt)(struct aead_givcrypt_request *req); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 366 | |
| 367 | struct crypto_aead *base; |
| 368 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 369 | unsigned int ivsize; |
| 370 | unsigned int authsize; |
| 371 | unsigned int reqsize; |
| 372 | }; |
| 373 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 374 | struct blkcipher_tfm { |
| 375 | void *iv; |
| 376 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, |
| 377 | unsigned int keylen); |
| 378 | int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, |
| 379 | struct scatterlist *src, unsigned int nbytes); |
| 380 | int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, |
| 381 | struct scatterlist *src, unsigned int nbytes); |
| 382 | }; |
| 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | struct cipher_tfm { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | int (*cit_setkey)(struct crypto_tfm *tfm, |
| 386 | const u8 *key, unsigned int keylen); |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 387 | void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
| 388 | 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] | 389 | }; |
| 390 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 391 | struct hash_tfm { |
| 392 | int (*init)(struct hash_desc *desc); |
| 393 | int (*update)(struct hash_desc *desc, |
| 394 | struct scatterlist *sg, unsigned int nsg); |
| 395 | int (*final)(struct hash_desc *desc, u8 *out); |
| 396 | int (*digest)(struct hash_desc *desc, struct scatterlist *sg, |
| 397 | unsigned int nsg, u8 *out); |
| 398 | int (*setkey)(struct crypto_hash *tfm, const u8 *key, |
| 399 | unsigned int keylen); |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 400 | unsigned int digestsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | struct compress_tfm { |
| 404 | int (*cot_compress)(struct crypto_tfm *tfm, |
| 405 | const u8 *src, unsigned int slen, |
| 406 | u8 *dst, unsigned int *dlen); |
| 407 | int (*cot_decompress)(struct crypto_tfm *tfm, |
| 408 | const u8 *src, unsigned int slen, |
| 409 | u8 *dst, unsigned int *dlen); |
| 410 | }; |
| 411 | |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 412 | struct rng_tfm { |
| 413 | int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata, |
| 414 | unsigned int dlen); |
| 415 | int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen); |
| 416 | }; |
| 417 | |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 418 | #define crt_ablkcipher crt_u.ablkcipher |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 419 | #define crt_aead crt_u.aead |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 420 | #define crt_blkcipher crt_u.blkcipher |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | #define crt_cipher crt_u.cipher |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 422 | #define crt_hash crt_u.hash |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | #define crt_compress crt_u.compress |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 424 | #define crt_rng crt_u.rng |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | struct crypto_tfm { |
| 427 | |
| 428 | u32 crt_flags; |
| 429 | |
| 430 | union { |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 431 | struct ablkcipher_tfm ablkcipher; |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 432 | struct aead_tfm aead; |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 433 | struct blkcipher_tfm blkcipher; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | struct cipher_tfm cipher; |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 435 | struct hash_tfm hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | struct compress_tfm compress; |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 437 | struct rng_tfm rng; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } crt_u; |
Herbert Xu | 4a77948 | 2008-09-13 18:19:03 -0700 | [diff] [blame] | 439 | |
| 440 | void (*exit)(struct crypto_tfm *tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | struct crypto_alg *__crt_alg; |
Herbert Xu | f10b789 | 2006-01-25 22:34:01 +1100 | [diff] [blame] | 443 | |
Herbert Xu | 7991110 | 2006-08-21 21:03:52 +1000 | [diff] [blame] | 444 | void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | }; |
| 446 | |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 447 | struct crypto_ablkcipher { |
| 448 | struct crypto_tfm base; |
| 449 | }; |
| 450 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 451 | struct crypto_aead { |
| 452 | struct crypto_tfm base; |
| 453 | }; |
| 454 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 455 | struct crypto_blkcipher { |
| 456 | struct crypto_tfm base; |
| 457 | }; |
| 458 | |
Herbert Xu | 78a1fe4 | 2006-12-24 10:02:00 +1100 | [diff] [blame] | 459 | struct crypto_cipher { |
| 460 | struct crypto_tfm base; |
| 461 | }; |
| 462 | |
| 463 | struct crypto_comp { |
| 464 | struct crypto_tfm base; |
| 465 | }; |
| 466 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 467 | struct crypto_hash { |
| 468 | struct crypto_tfm base; |
| 469 | }; |
| 470 | |
Neil Horman | 17f0f4a | 2008-08-14 22:15:52 +1000 | [diff] [blame] | 471 | struct crypto_rng { |
| 472 | struct crypto_tfm base; |
| 473 | }; |
| 474 | |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 475 | enum { |
| 476 | CRYPTOA_UNSPEC, |
| 477 | CRYPTOA_ALG, |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 478 | CRYPTOA_TYPE, |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 479 | CRYPTOA_U32, |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 480 | __CRYPTOA_MAX, |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 481 | }; |
| 482 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 483 | #define CRYPTOA_MAX (__CRYPTOA_MAX - 1) |
| 484 | |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 485 | /* Maximum number of (rtattr) parameters for each template. */ |
| 486 | #define CRYPTO_MAX_ATTRS 32 |
| 487 | |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 488 | struct crypto_attr_alg { |
| 489 | char name[CRYPTO_MAX_ALG_NAME]; |
| 490 | }; |
| 491 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 492 | struct crypto_attr_type { |
| 493 | u32 type; |
| 494 | u32 mask; |
| 495 | }; |
| 496 | |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 497 | struct crypto_attr_u32 { |
| 498 | u32 num; |
| 499 | }; |
| 500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | /* |
| 502 | * Transform user interface. |
| 503 | */ |
| 504 | |
Herbert Xu | 6d7d684 | 2006-07-30 11:53:01 +1000 | [diff] [blame] | 505 | 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] | 506 | void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm); |
| 507 | |
| 508 | static inline void crypto_free_tfm(struct crypto_tfm *tfm) |
| 509 | { |
| 510 | return crypto_destroy_tfm(tfm, tfm); |
| 511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
Herbert Xu | da7f033 | 2008-07-31 17:08:25 +0800 | [diff] [blame] | 513 | int alg_test(const char *driver, const char *alg, u32 type, u32 mask); |
| 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | /* |
| 516 | * Transform helpers which query the underlying algorithm. |
| 517 | */ |
| 518 | static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm) |
| 519 | { |
| 520 | return tfm->__crt_alg->cra_name; |
| 521 | } |
| 522 | |
Michal Ludvig | b14cdd6 | 2006-07-09 09:02:24 +1000 | [diff] [blame] | 523 | static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm) |
| 524 | { |
| 525 | return tfm->__crt_alg->cra_driver_name; |
| 526 | } |
| 527 | |
| 528 | static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm) |
| 529 | { |
| 530 | return tfm->__crt_alg->cra_priority; |
| 531 | } |
| 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm) |
| 534 | { |
| 535 | return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK; |
| 536 | } |
| 537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm) |
| 539 | { |
| 540 | return tfm->__crt_alg->cra_blocksize; |
| 541 | } |
| 542 | |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 543 | static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm) |
| 544 | { |
| 545 | return tfm->__crt_alg->cra_alignmask; |
| 546 | } |
| 547 | |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 548 | static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm) |
| 549 | { |
| 550 | return tfm->crt_flags; |
| 551 | } |
| 552 | |
| 553 | static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags) |
| 554 | { |
| 555 | tfm->crt_flags |= flags; |
| 556 | } |
| 557 | |
| 558 | static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags) |
| 559 | { |
| 560 | tfm->crt_flags &= ~flags; |
| 561 | } |
| 562 | |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 563 | static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm) |
| 564 | { |
Herbert Xu | f10b789 | 2006-01-25 22:34:01 +1100 | [diff] [blame] | 565 | return tfm->__crt_ctx; |
| 566 | } |
| 567 | |
| 568 | static inline unsigned int crypto_tfm_ctx_alignment(void) |
| 569 | { |
| 570 | struct crypto_tfm *tfm; |
| 571 | return __alignof__(tfm->__crt_ctx); |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | /* |
| 575 | * API wrappers. |
| 576 | */ |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 577 | static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast( |
| 578 | struct crypto_tfm *tfm) |
| 579 | { |
| 580 | return (struct crypto_ablkcipher *)tfm; |
| 581 | } |
| 582 | |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 583 | static inline u32 crypto_skcipher_type(u32 type) |
| 584 | { |
Herbert Xu | ecfc432 | 2007-12-05 21:08:36 +1100 | [diff] [blame] | 585 | type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 586 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; |
| 587 | return type; |
| 588 | } |
| 589 | |
| 590 | static inline u32 crypto_skcipher_mask(u32 mask) |
| 591 | { |
Herbert Xu | ecfc432 | 2007-12-05 21:08:36 +1100 | [diff] [blame] | 592 | mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 593 | mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK; |
| 594 | return mask; |
| 595 | } |
| 596 | |
Herbert Xu | b9c55aa | 2007-12-04 12:46:48 +1100 | [diff] [blame] | 597 | struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name, |
| 598 | u32 type, u32 mask); |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 599 | |
| 600 | static inline struct crypto_tfm *crypto_ablkcipher_tfm( |
| 601 | struct crypto_ablkcipher *tfm) |
| 602 | { |
| 603 | return &tfm->base; |
| 604 | } |
| 605 | |
| 606 | static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm) |
| 607 | { |
| 608 | crypto_free_tfm(crypto_ablkcipher_tfm(tfm)); |
| 609 | } |
| 610 | |
| 611 | static inline int crypto_has_ablkcipher(const char *alg_name, u32 type, |
| 612 | u32 mask) |
| 613 | { |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 614 | return crypto_has_alg(alg_name, crypto_skcipher_type(type), |
| 615 | crypto_skcipher_mask(mask)); |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static inline struct ablkcipher_tfm *crypto_ablkcipher_crt( |
| 619 | struct crypto_ablkcipher *tfm) |
| 620 | { |
| 621 | return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher; |
| 622 | } |
| 623 | |
| 624 | static inline unsigned int crypto_ablkcipher_ivsize( |
| 625 | struct crypto_ablkcipher *tfm) |
| 626 | { |
| 627 | return crypto_ablkcipher_crt(tfm)->ivsize; |
| 628 | } |
| 629 | |
| 630 | static inline unsigned int crypto_ablkcipher_blocksize( |
| 631 | struct crypto_ablkcipher *tfm) |
| 632 | { |
| 633 | return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm)); |
| 634 | } |
| 635 | |
| 636 | static inline unsigned int crypto_ablkcipher_alignmask( |
| 637 | struct crypto_ablkcipher *tfm) |
| 638 | { |
| 639 | return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm)); |
| 640 | } |
| 641 | |
| 642 | static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm) |
| 643 | { |
| 644 | return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm)); |
| 645 | } |
| 646 | |
| 647 | static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm, |
| 648 | u32 flags) |
| 649 | { |
| 650 | crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags); |
| 651 | } |
| 652 | |
| 653 | static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm, |
| 654 | u32 flags) |
| 655 | { |
| 656 | crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags); |
| 657 | } |
| 658 | |
| 659 | static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, |
| 660 | const u8 *key, unsigned int keylen) |
| 661 | { |
Herbert Xu | ecfc432 | 2007-12-05 21:08:36 +1100 | [diff] [blame] | 662 | struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm); |
| 663 | |
| 664 | return crt->setkey(crt->base, key, keylen); |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm( |
| 668 | struct ablkcipher_request *req) |
| 669 | { |
| 670 | return __crypto_ablkcipher_cast(req->base.tfm); |
| 671 | } |
| 672 | |
| 673 | static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req) |
| 674 | { |
| 675 | struct ablkcipher_tfm *crt = |
| 676 | crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); |
| 677 | return crt->encrypt(req); |
| 678 | } |
| 679 | |
| 680 | static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req) |
| 681 | { |
| 682 | struct ablkcipher_tfm *crt = |
| 683 | crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); |
| 684 | return crt->decrypt(req); |
| 685 | } |
| 686 | |
Herbert Xu | b16c3a2 | 2007-08-29 19:02:04 +0800 | [diff] [blame] | 687 | static inline unsigned int crypto_ablkcipher_reqsize( |
| 688 | struct crypto_ablkcipher *tfm) |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 689 | { |
| 690 | return crypto_ablkcipher_crt(tfm)->reqsize; |
| 691 | } |
| 692 | |
Herbert Xu | e196d62 | 2007-04-14 16:09:14 +1000 | [diff] [blame] | 693 | static inline void ablkcipher_request_set_tfm( |
| 694 | struct ablkcipher_request *req, struct crypto_ablkcipher *tfm) |
| 695 | { |
Herbert Xu | ecfc432 | 2007-12-05 21:08:36 +1100 | [diff] [blame] | 696 | req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base); |
Herbert Xu | e196d62 | 2007-04-14 16:09:14 +1000 | [diff] [blame] | 697 | } |
| 698 | |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 699 | static inline struct ablkcipher_request *ablkcipher_request_cast( |
| 700 | struct crypto_async_request *req) |
| 701 | { |
| 702 | return container_of(req, struct ablkcipher_request, base); |
| 703 | } |
| 704 | |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 705 | static inline struct ablkcipher_request *ablkcipher_request_alloc( |
| 706 | struct crypto_ablkcipher *tfm, gfp_t gfp) |
| 707 | { |
| 708 | struct ablkcipher_request *req; |
| 709 | |
| 710 | req = kmalloc(sizeof(struct ablkcipher_request) + |
| 711 | crypto_ablkcipher_reqsize(tfm), gfp); |
| 712 | |
| 713 | if (likely(req)) |
Herbert Xu | e196d62 | 2007-04-14 16:09:14 +1000 | [diff] [blame] | 714 | ablkcipher_request_set_tfm(req, tfm); |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 715 | |
| 716 | return req; |
| 717 | } |
| 718 | |
| 719 | static inline void ablkcipher_request_free(struct ablkcipher_request *req) |
| 720 | { |
Herbert Xu | aef73cf | 2009-07-11 22:22:14 +0800 | [diff] [blame] | 721 | kzfree(req); |
Herbert Xu | 32e3983 | 2007-03-24 14:35:34 +1100 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | static inline void ablkcipher_request_set_callback( |
| 725 | struct ablkcipher_request *req, |
| 726 | u32 flags, crypto_completion_t complete, void *data) |
| 727 | { |
| 728 | req->base.complete = complete; |
| 729 | req->base.data = data; |
| 730 | req->base.flags = flags; |
| 731 | } |
| 732 | |
| 733 | static inline void ablkcipher_request_set_crypt( |
| 734 | struct ablkcipher_request *req, |
| 735 | struct scatterlist *src, struct scatterlist *dst, |
| 736 | unsigned int nbytes, void *iv) |
| 737 | { |
| 738 | req->src = src; |
| 739 | req->dst = dst; |
| 740 | req->nbytes = nbytes; |
| 741 | req->info = iv; |
| 742 | } |
| 743 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 744 | static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm) |
| 745 | { |
| 746 | return (struct crypto_aead *)tfm; |
| 747 | } |
| 748 | |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 749 | 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] | 750 | |
| 751 | static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm) |
| 752 | { |
| 753 | return &tfm->base; |
| 754 | } |
| 755 | |
| 756 | static inline void crypto_free_aead(struct crypto_aead *tfm) |
| 757 | { |
| 758 | crypto_free_tfm(crypto_aead_tfm(tfm)); |
| 759 | } |
| 760 | |
| 761 | static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm) |
| 762 | { |
| 763 | return &crypto_aead_tfm(tfm)->crt_aead; |
| 764 | } |
| 765 | |
| 766 | static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm) |
| 767 | { |
| 768 | return crypto_aead_crt(tfm)->ivsize; |
| 769 | } |
| 770 | |
| 771 | static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm) |
| 772 | { |
| 773 | return crypto_aead_crt(tfm)->authsize; |
| 774 | } |
| 775 | |
| 776 | static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm) |
| 777 | { |
| 778 | return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm)); |
| 779 | } |
| 780 | |
| 781 | static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm) |
| 782 | { |
| 783 | return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm)); |
| 784 | } |
| 785 | |
| 786 | static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm) |
| 787 | { |
| 788 | return crypto_tfm_get_flags(crypto_aead_tfm(tfm)); |
| 789 | } |
| 790 | |
| 791 | static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags) |
| 792 | { |
| 793 | crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags); |
| 794 | } |
| 795 | |
| 796 | static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags) |
| 797 | { |
| 798 | crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags); |
| 799 | } |
| 800 | |
| 801 | static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key, |
| 802 | unsigned int keylen) |
| 803 | { |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 804 | struct aead_tfm *crt = crypto_aead_crt(tfm); |
| 805 | |
| 806 | return crt->setkey(crt->base, key, keylen); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 807 | } |
| 808 | |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 809 | int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize); |
| 810 | |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 811 | static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req) |
| 812 | { |
| 813 | return __crypto_aead_cast(req->base.tfm); |
| 814 | } |
| 815 | |
| 816 | static inline int crypto_aead_encrypt(struct aead_request *req) |
| 817 | { |
| 818 | return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req); |
| 819 | } |
| 820 | |
| 821 | static inline int crypto_aead_decrypt(struct aead_request *req) |
| 822 | { |
| 823 | return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req); |
| 824 | } |
| 825 | |
Herbert Xu | b16c3a2 | 2007-08-29 19:02:04 +0800 | [diff] [blame] | 826 | static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm) |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 827 | { |
| 828 | return crypto_aead_crt(tfm)->reqsize; |
| 829 | } |
| 830 | |
| 831 | static inline void aead_request_set_tfm(struct aead_request *req, |
| 832 | struct crypto_aead *tfm) |
| 833 | { |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 834 | req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm, |
| 838 | gfp_t gfp) |
| 839 | { |
| 840 | struct aead_request *req; |
| 841 | |
| 842 | req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp); |
| 843 | |
| 844 | if (likely(req)) |
| 845 | aead_request_set_tfm(req, tfm); |
| 846 | |
| 847 | return req; |
| 848 | } |
| 849 | |
| 850 | static inline void aead_request_free(struct aead_request *req) |
| 851 | { |
Herbert Xu | aef73cf | 2009-07-11 22:22:14 +0800 | [diff] [blame] | 852 | kzfree(req); |
Herbert Xu | 1ae9782 | 2007-08-30 15:36:14 +0800 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | static inline void aead_request_set_callback(struct aead_request *req, |
| 856 | u32 flags, |
| 857 | crypto_completion_t complete, |
| 858 | void *data) |
| 859 | { |
| 860 | req->base.complete = complete; |
| 861 | req->base.data = data; |
| 862 | req->base.flags = flags; |
| 863 | } |
| 864 | |
| 865 | static inline void aead_request_set_crypt(struct aead_request *req, |
| 866 | struct scatterlist *src, |
| 867 | struct scatterlist *dst, |
| 868 | unsigned int cryptlen, u8 *iv) |
| 869 | { |
| 870 | req->src = src; |
| 871 | req->dst = dst; |
| 872 | req->cryptlen = cryptlen; |
| 873 | req->iv = iv; |
| 874 | } |
| 875 | |
| 876 | static inline void aead_request_set_assoc(struct aead_request *req, |
| 877 | struct scatterlist *assoc, |
| 878 | unsigned int assoclen) |
| 879 | { |
| 880 | req->assoc = assoc; |
| 881 | req->assoclen = assoclen; |
| 882 | } |
| 883 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 884 | static inline struct crypto_blkcipher *__crypto_blkcipher_cast( |
| 885 | struct crypto_tfm *tfm) |
| 886 | { |
| 887 | return (struct crypto_blkcipher *)tfm; |
| 888 | } |
| 889 | |
| 890 | static inline struct crypto_blkcipher *crypto_blkcipher_cast( |
| 891 | struct crypto_tfm *tfm) |
| 892 | { |
| 893 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER); |
| 894 | return __crypto_blkcipher_cast(tfm); |
| 895 | } |
| 896 | |
| 897 | static inline struct crypto_blkcipher *crypto_alloc_blkcipher( |
| 898 | const char *alg_name, u32 type, u32 mask) |
| 899 | { |
Herbert Xu | 332f8840 | 2007-11-15 22:36:07 +0800 | [diff] [blame] | 900 | type &= ~CRYPTO_ALG_TYPE_MASK; |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 901 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; |
Herbert Xu | 332f8840 | 2007-11-15 22:36:07 +0800 | [diff] [blame] | 902 | mask |= CRYPTO_ALG_TYPE_MASK; |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 903 | |
| 904 | return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask)); |
| 905 | } |
| 906 | |
| 907 | static inline struct crypto_tfm *crypto_blkcipher_tfm( |
| 908 | struct crypto_blkcipher *tfm) |
| 909 | { |
| 910 | return &tfm->base; |
| 911 | } |
| 912 | |
| 913 | static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm) |
| 914 | { |
| 915 | crypto_free_tfm(crypto_blkcipher_tfm(tfm)); |
| 916 | } |
| 917 | |
Herbert Xu | fce32d7 | 2006-08-26 17:35:45 +1000 | [diff] [blame] | 918 | static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask) |
| 919 | { |
Herbert Xu | 332f8840 | 2007-11-15 22:36:07 +0800 | [diff] [blame] | 920 | type &= ~CRYPTO_ALG_TYPE_MASK; |
Herbert Xu | fce32d7 | 2006-08-26 17:35:45 +1000 | [diff] [blame] | 921 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; |
Herbert Xu | 332f8840 | 2007-11-15 22:36:07 +0800 | [diff] [blame] | 922 | mask |= CRYPTO_ALG_TYPE_MASK; |
Herbert Xu | fce32d7 | 2006-08-26 17:35:45 +1000 | [diff] [blame] | 923 | |
| 924 | return crypto_has_alg(alg_name, type, mask); |
| 925 | } |
| 926 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 927 | static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm) |
| 928 | { |
| 929 | return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm)); |
| 930 | } |
| 931 | |
| 932 | static inline struct blkcipher_tfm *crypto_blkcipher_crt( |
| 933 | struct crypto_blkcipher *tfm) |
| 934 | { |
| 935 | return &crypto_blkcipher_tfm(tfm)->crt_blkcipher; |
| 936 | } |
| 937 | |
| 938 | static inline struct blkcipher_alg *crypto_blkcipher_alg( |
| 939 | struct crypto_blkcipher *tfm) |
| 940 | { |
| 941 | return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher; |
| 942 | } |
| 943 | |
| 944 | static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm) |
| 945 | { |
| 946 | return crypto_blkcipher_alg(tfm)->ivsize; |
| 947 | } |
| 948 | |
| 949 | static inline unsigned int crypto_blkcipher_blocksize( |
| 950 | struct crypto_blkcipher *tfm) |
| 951 | { |
| 952 | return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm)); |
| 953 | } |
| 954 | |
| 955 | static inline unsigned int crypto_blkcipher_alignmask( |
| 956 | struct crypto_blkcipher *tfm) |
| 957 | { |
| 958 | return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm)); |
| 959 | } |
| 960 | |
| 961 | static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm) |
| 962 | { |
| 963 | return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm)); |
| 964 | } |
| 965 | |
| 966 | static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm, |
| 967 | u32 flags) |
| 968 | { |
| 969 | crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags); |
| 970 | } |
| 971 | |
| 972 | static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm, |
| 973 | u32 flags) |
| 974 | { |
| 975 | crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags); |
| 976 | } |
| 977 | |
| 978 | static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm, |
| 979 | const u8 *key, unsigned int keylen) |
| 980 | { |
| 981 | return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm), |
| 982 | key, keylen); |
| 983 | } |
| 984 | |
| 985 | static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc, |
| 986 | struct scatterlist *dst, |
| 987 | struct scatterlist *src, |
| 988 | unsigned int nbytes) |
| 989 | { |
| 990 | desc->info = crypto_blkcipher_crt(desc->tfm)->iv; |
| 991 | return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); |
| 992 | } |
| 993 | |
| 994 | static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc, |
| 995 | struct scatterlist *dst, |
| 996 | struct scatterlist *src, |
| 997 | unsigned int nbytes) |
| 998 | { |
| 999 | return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); |
| 1000 | } |
| 1001 | |
| 1002 | static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc, |
| 1003 | struct scatterlist *dst, |
| 1004 | struct scatterlist *src, |
| 1005 | unsigned int nbytes) |
| 1006 | { |
| 1007 | desc->info = crypto_blkcipher_crt(desc->tfm)->iv; |
| 1008 | return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); |
| 1009 | } |
| 1010 | |
| 1011 | static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc, |
| 1012 | struct scatterlist *dst, |
| 1013 | struct scatterlist *src, |
| 1014 | unsigned int nbytes) |
| 1015 | { |
| 1016 | return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); |
| 1017 | } |
| 1018 | |
| 1019 | static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm, |
| 1020 | const u8 *src, unsigned int len) |
| 1021 | { |
| 1022 | memcpy(crypto_blkcipher_crt(tfm)->iv, src, len); |
| 1023 | } |
| 1024 | |
| 1025 | static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm, |
| 1026 | u8 *dst, unsigned int len) |
| 1027 | { |
| 1028 | memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len); |
| 1029 | } |
| 1030 | |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 1031 | static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm) |
| 1032 | { |
| 1033 | return (struct crypto_cipher *)tfm; |
| 1034 | } |
| 1035 | |
| 1036 | static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm) |
| 1037 | { |
| 1038 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
| 1039 | return __crypto_cipher_cast(tfm); |
| 1040 | } |
| 1041 | |
| 1042 | static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name, |
| 1043 | u32 type, u32 mask) |
| 1044 | { |
| 1045 | type &= ~CRYPTO_ALG_TYPE_MASK; |
| 1046 | type |= CRYPTO_ALG_TYPE_CIPHER; |
| 1047 | mask |= CRYPTO_ALG_TYPE_MASK; |
| 1048 | |
| 1049 | return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask)); |
| 1050 | } |
| 1051 | |
| 1052 | static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm) |
| 1053 | { |
Herbert Xu | 78a1fe4 | 2006-12-24 10:02:00 +1100 | [diff] [blame] | 1054 | return &tfm->base; |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | static inline void crypto_free_cipher(struct crypto_cipher *tfm) |
| 1058 | { |
| 1059 | crypto_free_tfm(crypto_cipher_tfm(tfm)); |
| 1060 | } |
| 1061 | |
Herbert Xu | fce32d7 | 2006-08-26 17:35:45 +1000 | [diff] [blame] | 1062 | static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask) |
| 1063 | { |
| 1064 | type &= ~CRYPTO_ALG_TYPE_MASK; |
| 1065 | type |= CRYPTO_ALG_TYPE_CIPHER; |
| 1066 | mask |= CRYPTO_ALG_TYPE_MASK; |
| 1067 | |
| 1068 | return crypto_has_alg(alg_name, type, mask); |
| 1069 | } |
| 1070 | |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 1071 | static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm) |
| 1072 | { |
| 1073 | return &crypto_cipher_tfm(tfm)->crt_cipher; |
| 1074 | } |
| 1075 | |
| 1076 | static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm) |
| 1077 | { |
| 1078 | return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm)); |
| 1079 | } |
| 1080 | |
| 1081 | static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm) |
| 1082 | { |
| 1083 | return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm)); |
| 1084 | } |
| 1085 | |
| 1086 | static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm) |
| 1087 | { |
| 1088 | return crypto_tfm_get_flags(crypto_cipher_tfm(tfm)); |
| 1089 | } |
| 1090 | |
| 1091 | static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm, |
| 1092 | u32 flags) |
| 1093 | { |
| 1094 | crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags); |
| 1095 | } |
| 1096 | |
| 1097 | static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm, |
| 1098 | u32 flags) |
| 1099 | { |
| 1100 | crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags); |
| 1101 | } |
| 1102 | |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 1103 | static inline int crypto_cipher_setkey(struct crypto_cipher *tfm, |
| 1104 | const u8 *key, unsigned int keylen) |
| 1105 | { |
| 1106 | return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm), |
| 1107 | key, keylen); |
| 1108 | } |
| 1109 | |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 1110 | static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm, |
| 1111 | u8 *dst, const u8 *src) |
| 1112 | { |
| 1113 | crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm), |
| 1114 | dst, src); |
| 1115 | } |
| 1116 | |
| 1117 | static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, |
| 1118 | u8 *dst, const u8 *src) |
| 1119 | { |
| 1120 | crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm), |
| 1121 | dst, src); |
| 1122 | } |
| 1123 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 1124 | static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | { |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 1126 | return (struct crypto_hash *)tfm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 1129 | static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | { |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 1131 | BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) & |
| 1132 | CRYPTO_ALG_TYPE_HASH_MASK); |
| 1133 | return __crypto_hash_cast(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 1136 | static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name, |
| 1137 | u32 type, u32 mask) |
| 1138 | { |
| 1139 | type &= ~CRYPTO_ALG_TYPE_MASK; |
Herbert Xu | 551a09a | 2007-12-01 21:47:07 +1100 | [diff] [blame] | 1140 | mask &= ~CRYPTO_ALG_TYPE_MASK; |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 1141 | type |= CRYPTO_ALG_TYPE_HASH; |
| 1142 | mask |= CRYPTO_ALG_TYPE_HASH_MASK; |
| 1143 | |
| 1144 | return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask)); |
| 1145 | } |
| 1146 | |
| 1147 | static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm) |
| 1148 | { |
| 1149 | return &tfm->base; |
| 1150 | } |
| 1151 | |
| 1152 | static inline void crypto_free_hash(struct crypto_hash *tfm) |
| 1153 | { |
| 1154 | crypto_free_tfm(crypto_hash_tfm(tfm)); |
| 1155 | } |
| 1156 | |
Herbert Xu | fce32d7 | 2006-08-26 17:35:45 +1000 | [diff] [blame] | 1157 | static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask) |
| 1158 | { |
| 1159 | type &= ~CRYPTO_ALG_TYPE_MASK; |
Herbert Xu | 551a09a | 2007-12-01 21:47:07 +1100 | [diff] [blame] | 1160 | mask &= ~CRYPTO_ALG_TYPE_MASK; |
Herbert Xu | fce32d7 | 2006-08-26 17:35:45 +1000 | [diff] [blame] | 1161 | type |= CRYPTO_ALG_TYPE_HASH; |
| 1162 | mask |= CRYPTO_ALG_TYPE_HASH_MASK; |
| 1163 | |
| 1164 | return crypto_has_alg(alg_name, type, mask); |
| 1165 | } |
| 1166 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 1167 | static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm) |
| 1168 | { |
| 1169 | return &crypto_hash_tfm(tfm)->crt_hash; |
| 1170 | } |
| 1171 | |
| 1172 | static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm) |
| 1173 | { |
| 1174 | return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm)); |
| 1175 | } |
| 1176 | |
| 1177 | static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm) |
| 1178 | { |
| 1179 | return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm)); |
| 1180 | } |
| 1181 | |
| 1182 | static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm) |
| 1183 | { |
| 1184 | return crypto_hash_crt(tfm)->digestsize; |
| 1185 | } |
| 1186 | |
| 1187 | static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm) |
| 1188 | { |
| 1189 | return crypto_tfm_get_flags(crypto_hash_tfm(tfm)); |
| 1190 | } |
| 1191 | |
| 1192 | static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags) |
| 1193 | { |
| 1194 | crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags); |
| 1195 | } |
| 1196 | |
| 1197 | static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags) |
| 1198 | { |
| 1199 | crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags); |
| 1200 | } |
| 1201 | |
| 1202 | static inline int crypto_hash_init(struct hash_desc *desc) |
| 1203 | { |
| 1204 | return crypto_hash_crt(desc->tfm)->init(desc); |
| 1205 | } |
| 1206 | |
| 1207 | static inline int crypto_hash_update(struct hash_desc *desc, |
| 1208 | struct scatterlist *sg, |
| 1209 | unsigned int nbytes) |
| 1210 | { |
| 1211 | return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes); |
| 1212 | } |
| 1213 | |
| 1214 | static inline int crypto_hash_final(struct hash_desc *desc, u8 *out) |
| 1215 | { |
| 1216 | return crypto_hash_crt(desc->tfm)->final(desc, out); |
| 1217 | } |
| 1218 | |
| 1219 | static inline int crypto_hash_digest(struct hash_desc *desc, |
| 1220 | struct scatterlist *sg, |
| 1221 | unsigned int nbytes, u8 *out) |
| 1222 | { |
| 1223 | return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out); |
| 1224 | } |
| 1225 | |
| 1226 | static inline int crypto_hash_setkey(struct crypto_hash *hash, |
| 1227 | const u8 *key, unsigned int keylen) |
| 1228 | { |
| 1229 | return crypto_hash_crt(hash)->setkey(hash, key, keylen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Herbert Xu | fce32d7 | 2006-08-26 17:35:45 +1000 | [diff] [blame] | 1232 | static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm) |
| 1233 | { |
| 1234 | return (struct crypto_comp *)tfm; |
| 1235 | } |
| 1236 | |
| 1237 | static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm) |
| 1238 | { |
| 1239 | BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) & |
| 1240 | CRYPTO_ALG_TYPE_MASK); |
| 1241 | return __crypto_comp_cast(tfm); |
| 1242 | } |
| 1243 | |
| 1244 | static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name, |
| 1245 | u32 type, u32 mask) |
| 1246 | { |
| 1247 | type &= ~CRYPTO_ALG_TYPE_MASK; |
| 1248 | type |= CRYPTO_ALG_TYPE_COMPRESS; |
| 1249 | mask |= CRYPTO_ALG_TYPE_MASK; |
| 1250 | |
| 1251 | return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask)); |
| 1252 | } |
| 1253 | |
| 1254 | static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm) |
| 1255 | { |
Herbert Xu | 78a1fe4 | 2006-12-24 10:02:00 +1100 | [diff] [blame] | 1256 | return &tfm->base; |
Herbert Xu | fce32d7 | 2006-08-26 17:35:45 +1000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | static inline void crypto_free_comp(struct crypto_comp *tfm) |
| 1260 | { |
| 1261 | crypto_free_tfm(crypto_comp_tfm(tfm)); |
| 1262 | } |
| 1263 | |
| 1264 | static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask) |
| 1265 | { |
| 1266 | type &= ~CRYPTO_ALG_TYPE_MASK; |
| 1267 | type |= CRYPTO_ALG_TYPE_COMPRESS; |
| 1268 | mask |= CRYPTO_ALG_TYPE_MASK; |
| 1269 | |
| 1270 | return crypto_has_alg(alg_name, type, mask); |
| 1271 | } |
| 1272 | |
Herbert Xu | e4d5b79 | 2006-08-26 18:12:40 +1000 | [diff] [blame] | 1273 | static inline const char *crypto_comp_name(struct crypto_comp *tfm) |
| 1274 | { |
| 1275 | return crypto_tfm_alg_name(crypto_comp_tfm(tfm)); |
| 1276 | } |
| 1277 | |
Herbert Xu | fce32d7 | 2006-08-26 17:35:45 +1000 | [diff] [blame] | 1278 | static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm) |
| 1279 | { |
| 1280 | return &crypto_comp_tfm(tfm)->crt_compress; |
| 1281 | } |
| 1282 | |
| 1283 | static inline int crypto_comp_compress(struct crypto_comp *tfm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | const u8 *src, unsigned int slen, |
| 1285 | u8 *dst, unsigned int *dlen) |
| 1286 | { |
Herbert Xu | 78a1fe4 | 2006-12-24 10:02:00 +1100 | [diff] [blame] | 1287 | return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm), |
| 1288 | src, slen, dst, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Herbert Xu | fce32d7 | 2006-08-26 17:35:45 +1000 | [diff] [blame] | 1291 | static inline int crypto_comp_decompress(struct crypto_comp *tfm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | const u8 *src, unsigned int slen, |
| 1293 | u8 *dst, unsigned int *dlen) |
| 1294 | { |
Herbert Xu | 78a1fe4 | 2006-12-24 10:02:00 +1100 | [diff] [blame] | 1295 | return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm), |
| 1296 | src, slen, dst, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | #endif /* _LINUX_CRYPTO_H */ |
| 1300 | |