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