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> |
| 9 | * and Nettle, by Niels Möller. |
| 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 |
| 36 | #define CRYPTO_ALG_TYPE_COMPRESS 0x00000005 |
| 37 | |
| 38 | #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 40 | #define CRYPTO_ALG_LARVAL 0x00000010 |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 41 | #define CRYPTO_ALG_DEAD 0x00000020 |
| 42 | #define CRYPTO_ALG_DYING 0x00000040 |
Herbert Xu | f3f632d | 2006-08-06 23:12:59 +1000 | [diff] [blame] | 43 | #define CRYPTO_ALG_ASYNC 0x00000080 |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | /* |
| 46 | * Transform masks and values (for crt_flags). |
| 47 | */ |
| 48 | #define CRYPTO_TFM_MODE_MASK 0x000000ff |
| 49 | #define CRYPTO_TFM_REQ_MASK 0x000fff00 |
| 50 | #define CRYPTO_TFM_RES_MASK 0xfff00000 |
| 51 | |
| 52 | #define CRYPTO_TFM_MODE_ECB 0x00000001 |
| 53 | #define CRYPTO_TFM_MODE_CBC 0x00000002 |
| 54 | #define CRYPTO_TFM_MODE_CFB 0x00000004 |
| 55 | #define CRYPTO_TFM_MODE_CTR 0x00000008 |
| 56 | |
| 57 | #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100 |
Herbert Xu | 64baf3c | 2005-09-01 17:43:05 -0700 | [diff] [blame] | 58 | #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000 |
| 60 | #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000 |
| 61 | #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000 |
| 62 | #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000 |
| 63 | #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000 |
| 64 | |
| 65 | /* |
| 66 | * Miscellaneous stuff. |
| 67 | */ |
| 68 | #define CRYPTO_UNSPEC 0 |
| 69 | #define CRYPTO_MAX_ALG_NAME 64 |
| 70 | |
| 71 | #define CRYPTO_DIR_ENCRYPT 1 |
| 72 | #define CRYPTO_DIR_DECRYPT 0 |
| 73 | |
Herbert Xu | 7991110 | 2006-08-21 21:03:52 +1000 | [diff] [blame] | 74 | /* |
| 75 | * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual |
| 76 | * declaration) is used to ensure that the crypto_tfm context structure is |
| 77 | * aligned correctly for the given architecture so that there are no alignment |
| 78 | * faults for C data types. In particular, this is required on platforms such |
| 79 | * as arm where pointers are 32-bit aligned but there are data types such as |
| 80 | * u64 which require 64-bit alignment. |
| 81 | */ |
| 82 | #if defined(ARCH_KMALLOC_MINALIGN) |
| 83 | #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN |
| 84 | #elif defined(ARCH_SLAB_MINALIGN) |
| 85 | #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN |
| 86 | #endif |
| 87 | |
| 88 | #ifdef CRYPTO_MINALIGN |
| 89 | #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN))) |
| 90 | #else |
| 91 | #define CRYPTO_MINALIGN_ATTR |
| 92 | #endif |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | struct scatterlist; |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 95 | struct crypto_blkcipher; |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 96 | struct crypto_hash; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 97 | struct crypto_tfm; |
Herbert Xu | e853c3c | 2006-08-22 00:06:54 +1000 | [diff] [blame] | 98 | struct crypto_type; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 99 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 100 | struct blkcipher_desc { |
| 101 | struct crypto_blkcipher *tfm; |
| 102 | void *info; |
| 103 | u32 flags; |
| 104 | }; |
| 105 | |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 106 | struct cipher_desc { |
| 107 | struct crypto_tfm *tfm; |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 108 | void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 109 | unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst, |
| 110 | const u8 *src, unsigned int nbytes); |
| 111 | void *info; |
| 112 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 114 | struct hash_desc { |
| 115 | struct crypto_hash *tfm; |
| 116 | u32 flags; |
| 117 | }; |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | /* |
| 120 | * Algorithms: modular crypto algorithm implementations, managed |
| 121 | * via crypto_register_alg() and crypto_unregister_alg(). |
| 122 | */ |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 123 | struct blkcipher_alg { |
| 124 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, |
| 125 | unsigned int keylen); |
| 126 | int (*encrypt)(struct blkcipher_desc *desc, |
| 127 | struct scatterlist *dst, struct scatterlist *src, |
| 128 | unsigned int nbytes); |
| 129 | int (*decrypt)(struct blkcipher_desc *desc, |
| 130 | struct scatterlist *dst, struct scatterlist *src, |
| 131 | unsigned int nbytes); |
| 132 | |
| 133 | unsigned int min_keysize; |
| 134 | unsigned int max_keysize; |
| 135 | unsigned int ivsize; |
| 136 | }; |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | struct cipher_alg { |
| 139 | unsigned int cia_min_keysize; |
| 140 | unsigned int cia_max_keysize; |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 141 | int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 142 | unsigned int keylen); |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 143 | void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
| 144 | void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 145 | |
| 146 | unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc, |
| 147 | u8 *dst, const u8 *src, |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 148 | unsigned int nbytes) __deprecated; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 149 | unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc, |
| 150 | u8 *dst, const u8 *src, |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 151 | unsigned int nbytes) __deprecated; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 152 | unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc, |
| 153 | u8 *dst, const u8 *src, |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 154 | unsigned int nbytes) __deprecated; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 155 | unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc, |
| 156 | u8 *dst, const u8 *src, |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 157 | unsigned int nbytes) __deprecated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | struct digest_alg { |
| 161 | unsigned int dia_digestsize; |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 162 | void (*dia_init)(struct crypto_tfm *tfm); |
| 163 | void (*dia_update)(struct crypto_tfm *tfm, const u8 *data, |
| 164 | unsigned int len); |
| 165 | void (*dia_final)(struct crypto_tfm *tfm, u8 *out); |
| 166 | int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 167 | unsigned int keylen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 170 | struct hash_alg { |
| 171 | int (*init)(struct hash_desc *desc); |
| 172 | int (*update)(struct hash_desc *desc, struct scatterlist *sg, |
| 173 | unsigned int nbytes); |
| 174 | int (*final)(struct hash_desc *desc, u8 *out); |
| 175 | int (*digest)(struct hash_desc *desc, struct scatterlist *sg, |
| 176 | unsigned int nbytes, u8 *out); |
| 177 | int (*setkey)(struct crypto_hash *tfm, const u8 *key, |
| 178 | unsigned int keylen); |
| 179 | |
| 180 | unsigned int digestsize; |
| 181 | }; |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | struct compress_alg { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 184 | int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src, |
| 185 | unsigned int slen, u8 *dst, unsigned int *dlen); |
| 186 | int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src, |
| 187 | unsigned int slen, u8 *dst, unsigned int *dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 190 | #define cra_blkcipher cra_u.blkcipher |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | #define cra_cipher cra_u.cipher |
| 192 | #define cra_digest cra_u.digest |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 193 | #define cra_hash cra_u.hash |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | #define cra_compress cra_u.compress |
| 195 | |
| 196 | struct crypto_alg { |
| 197 | struct list_head cra_list; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 198 | struct list_head cra_users; |
| 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | u32 cra_flags; |
| 201 | unsigned int cra_blocksize; |
| 202 | unsigned int cra_ctxsize; |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 203 | unsigned int cra_alignmask; |
Herbert Xu | 5cb1454 | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 204 | |
| 205 | int cra_priority; |
Herbert Xu | 6521f30 | 2006-08-06 20:28:44 +1000 | [diff] [blame] | 206 | atomic_t cra_refcnt; |
Herbert Xu | 5cb1454 | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 207 | |
Herbert Xu | d913ea0 | 2006-05-21 08:45:26 +1000 | [diff] [blame] | 208 | char cra_name[CRYPTO_MAX_ALG_NAME]; |
| 209 | char cra_driver_name[CRYPTO_MAX_ALG_NAME]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Herbert Xu | e853c3c | 2006-08-22 00:06:54 +1000 | [diff] [blame] | 211 | const struct crypto_type *cra_type; |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | union { |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 214 | struct blkcipher_alg blkcipher; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | struct cipher_alg cipher; |
| 216 | struct digest_alg digest; |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 217 | struct hash_alg hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | struct compress_alg compress; |
| 219 | } cra_u; |
Herbert Xu | c7fc059 | 2006-05-24 13:02:26 +1000 | [diff] [blame] | 220 | |
| 221 | int (*cra_init)(struct crypto_tfm *tfm); |
| 222 | void (*cra_exit)(struct crypto_tfm *tfm); |
Herbert Xu | 6521f30 | 2006-08-06 20:28:44 +1000 | [diff] [blame] | 223 | void (*cra_destroy)(struct crypto_alg *alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | struct module *cra_module; |
| 226 | }; |
| 227 | |
| 228 | /* |
| 229 | * Algorithm registration interface. |
| 230 | */ |
| 231 | int crypto_register_alg(struct crypto_alg *alg); |
| 232 | int crypto_unregister_alg(struct crypto_alg *alg); |
| 233 | |
| 234 | /* |
| 235 | * Algorithm query interface. |
| 236 | */ |
| 237 | #ifdef CONFIG_CRYPTO |
| 238 | int crypto_alg_available(const char *name, u32 flags); |
| 239 | #else |
| 240 | static inline int crypto_alg_available(const char *name, u32 flags) |
| 241 | { |
| 242 | return 0; |
| 243 | } |
| 244 | #endif |
| 245 | |
| 246 | /* |
| 247 | * Transforms: user-instantiated objects which encapsulate algorithms |
Herbert Xu | 6d7d684 | 2006-07-30 11:53:01 +1000 | [diff] [blame] | 248 | * and core processing logic. Managed via crypto_alloc_*() and |
| 249 | * crypto_free_*(), as well as the various helpers below. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 252 | struct blkcipher_tfm { |
| 253 | void *iv; |
| 254 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, |
| 255 | unsigned int keylen); |
| 256 | int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, |
| 257 | struct scatterlist *src, unsigned int nbytes); |
| 258 | int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, |
| 259 | struct scatterlist *src, unsigned int nbytes); |
| 260 | }; |
| 261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | struct cipher_tfm { |
| 263 | void *cit_iv; |
| 264 | unsigned int cit_ivsize; |
| 265 | u32 cit_mode; |
| 266 | int (*cit_setkey)(struct crypto_tfm *tfm, |
| 267 | const u8 *key, unsigned int keylen); |
| 268 | int (*cit_encrypt)(struct crypto_tfm *tfm, |
| 269 | struct scatterlist *dst, |
| 270 | struct scatterlist *src, |
| 271 | unsigned int nbytes); |
| 272 | int (*cit_encrypt_iv)(struct crypto_tfm *tfm, |
| 273 | struct scatterlist *dst, |
| 274 | struct scatterlist *src, |
| 275 | unsigned int nbytes, u8 *iv); |
| 276 | int (*cit_decrypt)(struct crypto_tfm *tfm, |
| 277 | struct scatterlist *dst, |
| 278 | struct scatterlist *src, |
| 279 | unsigned int nbytes); |
| 280 | int (*cit_decrypt_iv)(struct crypto_tfm *tfm, |
| 281 | struct scatterlist *dst, |
| 282 | struct scatterlist *src, |
| 283 | unsigned int nbytes, u8 *iv); |
| 284 | void (*cit_xor_block)(u8 *dst, const u8 *src); |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 285 | void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
| 286 | 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] | 287 | }; |
| 288 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 289 | struct hash_tfm { |
| 290 | int (*init)(struct hash_desc *desc); |
| 291 | int (*update)(struct hash_desc *desc, |
| 292 | struct scatterlist *sg, unsigned int nsg); |
| 293 | int (*final)(struct hash_desc *desc, u8 *out); |
| 294 | int (*digest)(struct hash_desc *desc, struct scatterlist *sg, |
| 295 | unsigned int nsg, u8 *out); |
| 296 | int (*setkey)(struct crypto_hash *tfm, const u8 *key, |
| 297 | unsigned int keylen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | #ifdef CONFIG_CRYPTO_HMAC |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 299 | void *hmac_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | #endif |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 301 | unsigned int digestsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | struct compress_tfm { |
| 305 | int (*cot_compress)(struct crypto_tfm *tfm, |
| 306 | const u8 *src, unsigned int slen, |
| 307 | u8 *dst, unsigned int *dlen); |
| 308 | int (*cot_decompress)(struct crypto_tfm *tfm, |
| 309 | const u8 *src, unsigned int slen, |
| 310 | u8 *dst, unsigned int *dlen); |
| 311 | }; |
| 312 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 313 | #define crt_blkcipher crt_u.blkcipher |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | #define crt_cipher crt_u.cipher |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 315 | #define crt_hash crt_u.hash |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | #define crt_compress crt_u.compress |
| 317 | |
| 318 | struct crypto_tfm { |
| 319 | |
| 320 | u32 crt_flags; |
| 321 | |
| 322 | union { |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 323 | struct blkcipher_tfm blkcipher; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | struct cipher_tfm cipher; |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 325 | struct hash_tfm hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | struct compress_tfm compress; |
| 327 | } crt_u; |
| 328 | |
| 329 | struct crypto_alg *__crt_alg; |
Herbert Xu | f10b789 | 2006-01-25 22:34:01 +1100 | [diff] [blame] | 330 | |
Herbert Xu | 7991110 | 2006-08-21 21:03:52 +1000 | [diff] [blame] | 331 | void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | }; |
| 333 | |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 334 | #define crypto_cipher crypto_tfm |
| 335 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 336 | struct crypto_blkcipher { |
| 337 | struct crypto_tfm base; |
| 338 | }; |
| 339 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 340 | struct crypto_hash { |
| 341 | struct crypto_tfm base; |
| 342 | }; |
| 343 | |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame] | 344 | enum { |
| 345 | CRYPTOA_UNSPEC, |
| 346 | CRYPTOA_ALG, |
| 347 | }; |
| 348 | |
| 349 | struct crypto_attr_alg { |
| 350 | char name[CRYPTO_MAX_ALG_NAME]; |
| 351 | }; |
| 352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | /* |
| 354 | * Transform user interface. |
| 355 | */ |
| 356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | 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] | 358 | 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] | 359 | void crypto_free_tfm(struct crypto_tfm *tfm); |
| 360 | |
| 361 | /* |
| 362 | * Transform helpers which query the underlying algorithm. |
| 363 | */ |
| 364 | static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm) |
| 365 | { |
| 366 | return tfm->__crt_alg->cra_name; |
| 367 | } |
| 368 | |
Michal Ludvig | b14cdd6 | 2006-07-09 09:02:24 +1000 | [diff] [blame] | 369 | static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm) |
| 370 | { |
| 371 | return tfm->__crt_alg->cra_driver_name; |
| 372 | } |
| 373 | |
| 374 | static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm) |
| 375 | { |
| 376 | return tfm->__crt_alg->cra_priority; |
| 377 | } |
| 378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm) |
| 380 | { |
| 381 | return module_name(tfm->__crt_alg->cra_module); |
| 382 | } |
| 383 | |
| 384 | static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm) |
| 385 | { |
| 386 | return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK; |
| 387 | } |
| 388 | |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 389 | static unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm) |
| 390 | __deprecated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm) |
| 392 | { |
| 393 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
| 394 | return tfm->__crt_alg->cra_cipher.cia_min_keysize; |
| 395 | } |
| 396 | |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 397 | static unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm) |
| 398 | __deprecated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm) |
| 400 | { |
| 401 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
| 402 | return tfm->__crt_alg->cra_cipher.cia_max_keysize; |
| 403 | } |
| 404 | |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 405 | static unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm) __deprecated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm) |
| 407 | { |
| 408 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
| 409 | return tfm->crt_cipher.cit_ivsize; |
| 410 | } |
| 411 | |
| 412 | static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm) |
| 413 | { |
| 414 | return tfm->__crt_alg->cra_blocksize; |
| 415 | } |
| 416 | |
| 417 | static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm) |
| 418 | { |
| 419 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST); |
| 420 | return tfm->__crt_alg->cra_digest.dia_digestsize; |
| 421 | } |
| 422 | |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 423 | static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm) |
| 424 | { |
| 425 | return tfm->__crt_alg->cra_alignmask; |
| 426 | } |
| 427 | |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 428 | static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm) |
| 429 | { |
| 430 | return tfm->crt_flags; |
| 431 | } |
| 432 | |
| 433 | static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags) |
| 434 | { |
| 435 | tfm->crt_flags |= flags; |
| 436 | } |
| 437 | |
| 438 | static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags) |
| 439 | { |
| 440 | tfm->crt_flags &= ~flags; |
| 441 | } |
| 442 | |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 443 | static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm) |
| 444 | { |
Herbert Xu | f10b789 | 2006-01-25 22:34:01 +1100 | [diff] [blame] | 445 | return tfm->__crt_ctx; |
| 446 | } |
| 447 | |
| 448 | static inline unsigned int crypto_tfm_ctx_alignment(void) |
| 449 | { |
| 450 | struct crypto_tfm *tfm; |
| 451 | return __alignof__(tfm->__crt_ctx); |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | /* |
| 455 | * API wrappers. |
| 456 | */ |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 457 | static inline struct crypto_blkcipher *__crypto_blkcipher_cast( |
| 458 | struct crypto_tfm *tfm) |
| 459 | { |
| 460 | return (struct crypto_blkcipher *)tfm; |
| 461 | } |
| 462 | |
| 463 | static inline struct crypto_blkcipher *crypto_blkcipher_cast( |
| 464 | struct crypto_tfm *tfm) |
| 465 | { |
| 466 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER); |
| 467 | return __crypto_blkcipher_cast(tfm); |
| 468 | } |
| 469 | |
| 470 | static inline struct crypto_blkcipher *crypto_alloc_blkcipher( |
| 471 | const char *alg_name, u32 type, u32 mask) |
| 472 | { |
| 473 | type &= ~CRYPTO_ALG_TYPE_MASK; |
| 474 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; |
| 475 | mask |= CRYPTO_ALG_TYPE_MASK; |
| 476 | |
| 477 | return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask)); |
| 478 | } |
| 479 | |
| 480 | static inline struct crypto_tfm *crypto_blkcipher_tfm( |
| 481 | struct crypto_blkcipher *tfm) |
| 482 | { |
| 483 | return &tfm->base; |
| 484 | } |
| 485 | |
| 486 | static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm) |
| 487 | { |
| 488 | crypto_free_tfm(crypto_blkcipher_tfm(tfm)); |
| 489 | } |
| 490 | |
| 491 | static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm) |
| 492 | { |
| 493 | return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm)); |
| 494 | } |
| 495 | |
| 496 | static inline struct blkcipher_tfm *crypto_blkcipher_crt( |
| 497 | struct crypto_blkcipher *tfm) |
| 498 | { |
| 499 | return &crypto_blkcipher_tfm(tfm)->crt_blkcipher; |
| 500 | } |
| 501 | |
| 502 | static inline struct blkcipher_alg *crypto_blkcipher_alg( |
| 503 | struct crypto_blkcipher *tfm) |
| 504 | { |
| 505 | return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher; |
| 506 | } |
| 507 | |
| 508 | static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm) |
| 509 | { |
| 510 | return crypto_blkcipher_alg(tfm)->ivsize; |
| 511 | } |
| 512 | |
| 513 | static inline unsigned int crypto_blkcipher_blocksize( |
| 514 | struct crypto_blkcipher *tfm) |
| 515 | { |
| 516 | return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm)); |
| 517 | } |
| 518 | |
| 519 | static inline unsigned int crypto_blkcipher_alignmask( |
| 520 | struct crypto_blkcipher *tfm) |
| 521 | { |
| 522 | return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm)); |
| 523 | } |
| 524 | |
| 525 | static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm) |
| 526 | { |
| 527 | return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm)); |
| 528 | } |
| 529 | |
| 530 | static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm, |
| 531 | u32 flags) |
| 532 | { |
| 533 | crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags); |
| 534 | } |
| 535 | |
| 536 | static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm, |
| 537 | u32 flags) |
| 538 | { |
| 539 | crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags); |
| 540 | } |
| 541 | |
| 542 | static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm, |
| 543 | const u8 *key, unsigned int keylen) |
| 544 | { |
| 545 | return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm), |
| 546 | key, keylen); |
| 547 | } |
| 548 | |
| 549 | static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc, |
| 550 | struct scatterlist *dst, |
| 551 | struct scatterlist *src, |
| 552 | unsigned int nbytes) |
| 553 | { |
| 554 | desc->info = crypto_blkcipher_crt(desc->tfm)->iv; |
| 555 | return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); |
| 556 | } |
| 557 | |
| 558 | static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc, |
| 559 | struct scatterlist *dst, |
| 560 | struct scatterlist *src, |
| 561 | unsigned int nbytes) |
| 562 | { |
| 563 | return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); |
| 564 | } |
| 565 | |
| 566 | static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc, |
| 567 | struct scatterlist *dst, |
| 568 | struct scatterlist *src, |
| 569 | unsigned int nbytes) |
| 570 | { |
| 571 | desc->info = crypto_blkcipher_crt(desc->tfm)->iv; |
| 572 | return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); |
| 573 | } |
| 574 | |
| 575 | static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc, |
| 576 | struct scatterlist *dst, |
| 577 | struct scatterlist *src, |
| 578 | unsigned int nbytes) |
| 579 | { |
| 580 | return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); |
| 581 | } |
| 582 | |
| 583 | static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm, |
| 584 | const u8 *src, unsigned int len) |
| 585 | { |
| 586 | memcpy(crypto_blkcipher_crt(tfm)->iv, src, len); |
| 587 | } |
| 588 | |
| 589 | static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm, |
| 590 | u8 *dst, unsigned int len) |
| 591 | { |
| 592 | memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len); |
| 593 | } |
| 594 | |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 595 | static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm) |
| 596 | { |
| 597 | return (struct crypto_cipher *)tfm; |
| 598 | } |
| 599 | |
| 600 | static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm) |
| 601 | { |
| 602 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
| 603 | return __crypto_cipher_cast(tfm); |
| 604 | } |
| 605 | |
| 606 | static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name, |
| 607 | u32 type, u32 mask) |
| 608 | { |
| 609 | type &= ~CRYPTO_ALG_TYPE_MASK; |
| 610 | type |= CRYPTO_ALG_TYPE_CIPHER; |
| 611 | mask |= CRYPTO_ALG_TYPE_MASK; |
| 612 | |
| 613 | return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask)); |
| 614 | } |
| 615 | |
| 616 | static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm) |
| 617 | { |
| 618 | return tfm; |
| 619 | } |
| 620 | |
| 621 | static inline void crypto_free_cipher(struct crypto_cipher *tfm) |
| 622 | { |
| 623 | crypto_free_tfm(crypto_cipher_tfm(tfm)); |
| 624 | } |
| 625 | |
| 626 | static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm) |
| 627 | { |
| 628 | return &crypto_cipher_tfm(tfm)->crt_cipher; |
| 629 | } |
| 630 | |
| 631 | static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm) |
| 632 | { |
| 633 | return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm)); |
| 634 | } |
| 635 | |
| 636 | static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm) |
| 637 | { |
| 638 | return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm)); |
| 639 | } |
| 640 | |
| 641 | static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm) |
| 642 | { |
| 643 | return crypto_tfm_get_flags(crypto_cipher_tfm(tfm)); |
| 644 | } |
| 645 | |
| 646 | static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm, |
| 647 | u32 flags) |
| 648 | { |
| 649 | crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags); |
| 650 | } |
| 651 | |
| 652 | static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm, |
| 653 | u32 flags) |
| 654 | { |
| 655 | crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags); |
| 656 | } |
| 657 | |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 658 | static inline int crypto_cipher_setkey(struct crypto_cipher *tfm, |
| 659 | const u8 *key, unsigned int keylen) |
| 660 | { |
| 661 | return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm), |
| 662 | key, keylen); |
| 663 | } |
| 664 | |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 665 | static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm, |
| 666 | u8 *dst, const u8 *src) |
| 667 | { |
| 668 | crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm), |
| 669 | dst, src); |
| 670 | } |
| 671 | |
| 672 | static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, |
| 673 | u8 *dst, const u8 *src) |
| 674 | { |
| 675 | crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm), |
| 676 | dst, src); |
| 677 | } |
| 678 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 679 | void crypto_digest_init(struct crypto_tfm *tfm); |
| 680 | void crypto_digest_update(struct crypto_tfm *tfm, |
| 681 | struct scatterlist *sg, unsigned int nsg); |
| 682 | void crypto_digest_final(struct crypto_tfm *tfm, u8 *out); |
| 683 | void crypto_digest_digest(struct crypto_tfm *tfm, |
| 684 | struct scatterlist *sg, unsigned int nsg, u8 *out); |
| 685 | |
| 686 | static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | { |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 688 | return (struct crypto_hash *)tfm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 691 | static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | { |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 693 | BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) & |
| 694 | CRYPTO_ALG_TYPE_HASH_MASK); |
| 695 | return __crypto_hash_cast(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | static inline int crypto_digest_setkey(struct crypto_tfm *tfm, |
| 699 | const u8 *key, unsigned int keylen) |
| 700 | { |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame^] | 701 | return tfm->crt_hash.setkey(crypto_hash_cast(tfm), key, keylen); |
| 702 | } |
| 703 | |
| 704 | static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name, |
| 705 | u32 type, u32 mask) |
| 706 | { |
| 707 | type &= ~CRYPTO_ALG_TYPE_MASK; |
| 708 | type |= CRYPTO_ALG_TYPE_HASH; |
| 709 | mask |= CRYPTO_ALG_TYPE_HASH_MASK; |
| 710 | |
| 711 | return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask)); |
| 712 | } |
| 713 | |
| 714 | static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm) |
| 715 | { |
| 716 | return &tfm->base; |
| 717 | } |
| 718 | |
| 719 | static inline void crypto_free_hash(struct crypto_hash *tfm) |
| 720 | { |
| 721 | crypto_free_tfm(crypto_hash_tfm(tfm)); |
| 722 | } |
| 723 | |
| 724 | static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm) |
| 725 | { |
| 726 | return &crypto_hash_tfm(tfm)->crt_hash; |
| 727 | } |
| 728 | |
| 729 | static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm) |
| 730 | { |
| 731 | return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm)); |
| 732 | } |
| 733 | |
| 734 | static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm) |
| 735 | { |
| 736 | return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm)); |
| 737 | } |
| 738 | |
| 739 | static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm) |
| 740 | { |
| 741 | return crypto_hash_crt(tfm)->digestsize; |
| 742 | } |
| 743 | |
| 744 | static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm) |
| 745 | { |
| 746 | return crypto_tfm_get_flags(crypto_hash_tfm(tfm)); |
| 747 | } |
| 748 | |
| 749 | static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags) |
| 750 | { |
| 751 | crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags); |
| 752 | } |
| 753 | |
| 754 | static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags) |
| 755 | { |
| 756 | crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags); |
| 757 | } |
| 758 | |
| 759 | static inline int crypto_hash_init(struct hash_desc *desc) |
| 760 | { |
| 761 | return crypto_hash_crt(desc->tfm)->init(desc); |
| 762 | } |
| 763 | |
| 764 | static inline int crypto_hash_update(struct hash_desc *desc, |
| 765 | struct scatterlist *sg, |
| 766 | unsigned int nbytes) |
| 767 | { |
| 768 | return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes); |
| 769 | } |
| 770 | |
| 771 | static inline int crypto_hash_final(struct hash_desc *desc, u8 *out) |
| 772 | { |
| 773 | return crypto_hash_crt(desc->tfm)->final(desc, out); |
| 774 | } |
| 775 | |
| 776 | static inline int crypto_hash_digest(struct hash_desc *desc, |
| 777 | struct scatterlist *sg, |
| 778 | unsigned int nbytes, u8 *out) |
| 779 | { |
| 780 | return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out); |
| 781 | } |
| 782 | |
| 783 | static inline int crypto_hash_setkey(struct crypto_hash *hash, |
| 784 | const u8 *key, unsigned int keylen) |
| 785 | { |
| 786 | return crypto_hash_crt(hash)->setkey(hash, key, keylen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 789 | static int crypto_cipher_encrypt(struct crypto_tfm *tfm, |
| 790 | struct scatterlist *dst, |
| 791 | struct scatterlist *src, |
| 792 | unsigned int nbytes) __deprecated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm, |
| 794 | struct scatterlist *dst, |
| 795 | struct scatterlist *src, |
| 796 | unsigned int nbytes) |
| 797 | { |
| 798 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
| 799 | return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes); |
| 800 | } |
| 801 | |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 802 | static int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm, |
| 803 | struct scatterlist *dst, |
| 804 | struct scatterlist *src, |
| 805 | unsigned int nbytes, u8 *iv) __deprecated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm, |
| 807 | struct scatterlist *dst, |
| 808 | struct scatterlist *src, |
| 809 | unsigned int nbytes, u8 *iv) |
| 810 | { |
| 811 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv); |
| 813 | } |
| 814 | |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 815 | static int crypto_cipher_decrypt(struct crypto_tfm *tfm, |
| 816 | struct scatterlist *dst, |
| 817 | struct scatterlist *src, |
| 818 | unsigned int nbytes) __deprecated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm, |
| 820 | struct scatterlist *dst, |
| 821 | struct scatterlist *src, |
| 822 | unsigned int nbytes) |
| 823 | { |
| 824 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
| 825 | return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes); |
| 826 | } |
| 827 | |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 828 | static int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm, |
| 829 | struct scatterlist *dst, |
| 830 | struct scatterlist *src, |
| 831 | unsigned int nbytes, u8 *iv) __deprecated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm, |
| 833 | struct scatterlist *dst, |
| 834 | struct scatterlist *src, |
| 835 | unsigned int nbytes, u8 *iv) |
| 836 | { |
| 837 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv); |
| 839 | } |
| 840 | |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 841 | static void crypto_cipher_set_iv(struct crypto_tfm *tfm, |
| 842 | const u8 *src, unsigned int len) __deprecated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm, |
| 844 | const u8 *src, unsigned int len) |
| 845 | { |
| 846 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
| 847 | memcpy(tfm->crt_cipher.cit_iv, src, len); |
| 848 | } |
| 849 | |
Herbert Xu | 7226bc87 | 2006-08-21 21:40:49 +1000 | [diff] [blame] | 850 | static void crypto_cipher_get_iv(struct crypto_tfm *tfm, |
| 851 | u8 *dst, unsigned int len) __deprecated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm, |
| 853 | u8 *dst, unsigned int len) |
| 854 | { |
| 855 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
| 856 | memcpy(dst, tfm->crt_cipher.cit_iv, len); |
| 857 | } |
| 858 | |
| 859 | static inline int crypto_comp_compress(struct crypto_tfm *tfm, |
| 860 | const u8 *src, unsigned int slen, |
| 861 | u8 *dst, unsigned int *dlen) |
| 862 | { |
| 863 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS); |
| 864 | return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen); |
| 865 | } |
| 866 | |
| 867 | static inline int crypto_comp_decompress(struct crypto_tfm *tfm, |
| 868 | const u8 *src, unsigned int slen, |
| 869 | u8 *dst, unsigned int *dlen) |
| 870 | { |
| 871 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS); |
| 872 | return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen); |
| 873 | } |
| 874 | |
| 875 | /* |
| 876 | * HMAC support. |
| 877 | */ |
| 878 | #ifdef CONFIG_CRYPTO_HMAC |
| 879 | void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen); |
| 880 | void crypto_hmac_update(struct crypto_tfm *tfm, |
| 881 | struct scatterlist *sg, unsigned int nsg); |
| 882 | void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key, |
| 883 | unsigned int *keylen, u8 *out); |
| 884 | void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen, |
| 885 | struct scatterlist *sg, unsigned int nsg, u8 *out); |
| 886 | #endif /* CONFIG_CRYPTO_HMAC */ |
| 887 | |
| 888 | #endif /* _LINUX_CRYPTO_H */ |
| 889 | |