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