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