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