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