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