| [2] | 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) |
|---|
| 6 | * |
|---|
| 7 | * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no> |
|---|
| 8 | * and Nettle, by Niels Möller. |
|---|
| 9 | * |
|---|
| 10 | * This program is free software; you can redistribute it and/or modify it |
|---|
| 11 | * under the terms of the GNU General Public License as published by the Free |
|---|
| 12 | * Software Foundation; either version 2 of the License, or (at your option) |
|---|
| 13 | * any later version. |
|---|
| 14 | * |
|---|
| 15 | */ |
|---|
| 16 | #ifndef _LINUX_CRYPTO_H |
|---|
| 17 | #define _LINUX_CRYPTO_H |
|---|
| 18 | |
|---|
| 19 | #include <linux/config.h> |
|---|
| 20 | #include <linux/module.h> |
|---|
| 21 | #include <linux/kernel.h> |
|---|
| 22 | #include <linux/types.h> |
|---|
| 23 | #include <asm/page.h> |
|---|
| 24 | |
|---|
| 25 | /* |
|---|
| 26 | * Algorithm masks and types. |
|---|
| 27 | */ |
|---|
| 28 | #define CRYPTO_ALG_TYPE_MASK 0x000000ff |
|---|
| 29 | #define CRYPTO_ALG_TYPE_CIPHER 0x00000001 |
|---|
| 30 | #define CRYPTO_ALG_TYPE_DIGEST 0x00000002 |
|---|
| 31 | #define CRYPTO_ALG_TYPE_COMPRESS 0x00000004 |
|---|
| 32 | |
|---|
| 33 | /* |
|---|
| 34 | * Transform masks and values (for crt_flags). |
|---|
| 35 | */ |
|---|
| 36 | #define CRYPTO_TFM_MODE_MASK 0x000000ff |
|---|
| 37 | #define CRYPTO_TFM_REQ_MASK 0x000fff00 |
|---|
| 38 | #define CRYPTO_TFM_RES_MASK 0xfff00000 |
|---|
| 39 | |
|---|
| 40 | #define CRYPTO_TFM_MODE_ECB 0x00000001 |
|---|
| 41 | #define CRYPTO_TFM_MODE_CBC 0x00000002 |
|---|
| 42 | #define CRYPTO_TFM_MODE_CFB 0x00000004 |
|---|
| 43 | #define CRYPTO_TFM_MODE_CTR 0x00000008 |
|---|
| 44 | |
|---|
| 45 | #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100 |
|---|
| 46 | #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000 |
|---|
| 47 | #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000 |
|---|
| 48 | #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000 |
|---|
| 49 | #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000 |
|---|
| 50 | #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000 |
|---|
| 51 | |
|---|
| 52 | /* |
|---|
| 53 | * Miscellaneous stuff. |
|---|
| 54 | */ |
|---|
| 55 | #define CRYPTO_UNSPEC 0 |
|---|
| 56 | #define CRYPTO_MAX_ALG_NAME 64 |
|---|
| 57 | |
|---|
| 58 | #define CRYPTO_DIR_ENCRYPT 1 |
|---|
| 59 | #define CRYPTO_DIR_DECRYPT 0 |
|---|
| 60 | |
|---|
| 61 | struct scatterlist; |
|---|
| 62 | |
|---|
| 63 | /* |
|---|
| 64 | * Algorithms: modular crypto algorithm implementations, managed |
|---|
| 65 | * via crypto_register_alg() and crypto_unregister_alg(). |
|---|
| 66 | */ |
|---|
| 67 | struct cipher_alg { |
|---|
| 68 | unsigned int cia_min_keysize; |
|---|
| 69 | unsigned int cia_max_keysize; |
|---|
| 70 | int (*cia_setkey)(void *ctx, const __u8 *key, |
|---|
| 71 | unsigned int keylen, __u32 *flags); |
|---|
| 72 | void (*cia_encrypt)(void *ctx, __u8 *dst, const __u8 *src); |
|---|
| 73 | void (*cia_decrypt)(void *ctx, __u8 *dst, const __u8 *src); |
|---|
| 74 | }; |
|---|
| 75 | |
|---|
| 76 | struct digest_alg { |
|---|
| 77 | unsigned int dia_digestsize; |
|---|
| 78 | void (*dia_init)(void *ctx); |
|---|
| 79 | void (*dia_update)(void *ctx, const __u8 *data, unsigned int len); |
|---|
| 80 | void (*dia_final)(void *ctx, __u8 *out); |
|---|
| 81 | int (*dia_setkey)(void *ctx, const __u8 *key, |
|---|
| 82 | unsigned int keylen, __u32 *flags); |
|---|
| 83 | }; |
|---|
| 84 | |
|---|
| 85 | struct compress_alg { |
|---|
| 86 | int (*coa_init)(void *ctx); |
|---|
| 87 | void (*coa_exit)(void *ctx); |
|---|
| 88 | int (*coa_compress)(void *ctx, const __u8 *src, unsigned int slen, |
|---|
| 89 | __u8 *dst, unsigned int *dlen); |
|---|
| 90 | int (*coa_decompress)(void *ctx, const __u8 *src, unsigned int slen, |
|---|
| 91 | __u8 *dst, unsigned int *dlen); |
|---|
| 92 | }; |
|---|
| 93 | |
|---|
| 94 | #define cra_cipher cra_u.cipher |
|---|
| 95 | #define cra_digest cra_u.digest |
|---|
| 96 | #define cra_compress cra_u.compress |
|---|
| 97 | |
|---|
| 98 | struct crypto_alg { |
|---|
| 99 | struct list_head cra_list; |
|---|
| 100 | __u32 cra_flags; |
|---|
| 101 | unsigned int cra_blocksize; |
|---|
| 102 | unsigned int cra_ctxsize; |
|---|
| 103 | const char cra_name[CRYPTO_MAX_ALG_NAME]; |
|---|
| 104 | |
|---|
| 105 | union { |
|---|
| 106 | struct cipher_alg cipher; |
|---|
| 107 | struct digest_alg digest; |
|---|
| 108 | struct compress_alg compress; |
|---|
| 109 | } cra_u; |
|---|
| 110 | |
|---|
| 111 | struct module *cra_module; |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | /* |
|---|
| 115 | * Algorithm registration interface. |
|---|
| 116 | */ |
|---|
| 117 | int crypto_register_alg(struct crypto_alg *alg); |
|---|
| 118 | int crypto_unregister_alg(struct crypto_alg *alg); |
|---|
| 119 | |
|---|
| 120 | /* |
|---|
| 121 | * Algorithm query interface. |
|---|
| 122 | */ |
|---|
| 123 | #ifdef CONFIG_CRYPTO |
|---|
| 124 | int crypto_alg_available(const char *name, __u32 flags); |
|---|
| 125 | #else |
|---|
| 126 | static inline int crypto_alg_available(const char *name, __u32 flags) |
|---|
| 127 | { |
|---|
| 128 | return 0; |
|---|
| 129 | } |
|---|
| 130 | #endif |
|---|
| 131 | |
|---|
| 132 | /* |
|---|
| 133 | * Transforms: user-instantiated objects which encapsulate algorithms |
|---|
| 134 | * and core processing logic. Managed via crypto_alloc_tfm() and |
|---|
| 135 | * crypto_free_tfm(), as well as the various helpers below. |
|---|
| 136 | */ |
|---|
| 137 | struct crypto_tfm; |
|---|
| 138 | |
|---|
| 139 | struct cipher_tfm { |
|---|
| 140 | void *cit_iv; |
|---|
| 141 | unsigned int cit_ivsize; |
|---|
| 142 | __u32 cit_mode; |
|---|
| 143 | int (*cit_setkey)(struct crypto_tfm *tfm, |
|---|
| 144 | const __u8 *key, unsigned int keylen); |
|---|
| 145 | int (*cit_encrypt)(struct crypto_tfm *tfm, |
|---|
| 146 | struct scatterlist *dst, |
|---|
| 147 | struct scatterlist *src, |
|---|
| 148 | unsigned int nbytes); |
|---|
| 149 | int (*cit_encrypt_iv)(struct crypto_tfm *tfm, |
|---|
| 150 | struct scatterlist *dst, |
|---|
| 151 | struct scatterlist *src, |
|---|
| 152 | unsigned int nbytes, __u8 *iv); |
|---|
| 153 | int (*cit_decrypt)(struct crypto_tfm *tfm, |
|---|
| 154 | struct scatterlist *dst, |
|---|
| 155 | struct scatterlist *src, |
|---|
| 156 | unsigned int nbytes); |
|---|
| 157 | int (*cit_decrypt_iv)(struct crypto_tfm *tfm, |
|---|
| 158 | struct scatterlist *dst, |
|---|
| 159 | struct scatterlist *src, |
|---|
| 160 | unsigned int nbytes, __u8 *iv); |
|---|
| 161 | void (*cit_xor_block)(__u8 *dst, const __u8 *src); |
|---|
| 162 | }; |
|---|
| 163 | |
|---|
| 164 | struct digest_tfm { |
|---|
| 165 | void (*dit_init)(struct crypto_tfm *tfm); |
|---|
| 166 | void (*dit_update)(struct crypto_tfm *tfm, |
|---|
| 167 | struct scatterlist *sg, unsigned int nsg); |
|---|
| 168 | void (*dit_final)(struct crypto_tfm *tfm, __u8 *out); |
|---|
| 169 | void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg, |
|---|
| 170 | unsigned int nsg, __u8 *out); |
|---|
| 171 | int (*dit_setkey)(struct crypto_tfm *tfm, |
|---|
| 172 | const __u8 *key, unsigned int keylen); |
|---|
| 173 | #ifdef CONFIG_CRYPTO_HMAC |
|---|
| 174 | void *dit_hmac_block; |
|---|
| 175 | #endif |
|---|
| 176 | }; |
|---|
| 177 | |
|---|
| 178 | struct compress_tfm { |
|---|
| 179 | int (*cot_compress)(struct crypto_tfm *tfm, |
|---|
| 180 | const __u8 *src, unsigned int slen, |
|---|
| 181 | __u8 *dst, unsigned int *dlen); |
|---|
| 182 | int (*cot_decompress)(struct crypto_tfm *tfm, |
|---|
| 183 | const __u8 *src, unsigned int slen, |
|---|
| 184 | __u8 *dst, unsigned int *dlen); |
|---|
| 185 | }; |
|---|
| 186 | |
|---|
| 187 | #define crt_cipher crt_u.cipher |
|---|
| 188 | #define crt_digest crt_u.digest |
|---|
| 189 | #define crt_compress crt_u.compress |
|---|
| 190 | |
|---|
| 191 | struct crypto_tfm { |
|---|
| 192 | |
|---|
| 193 | __u32 crt_flags; |
|---|
| 194 | |
|---|
| 195 | union { |
|---|
| 196 | struct cipher_tfm cipher; |
|---|
| 197 | struct digest_tfm digest; |
|---|
| 198 | struct compress_tfm compress; |
|---|
| 199 | } crt_u; |
|---|
| 200 | |
|---|
| 201 | struct crypto_alg *__crt_alg; |
|---|
| 202 | }; |
|---|
| 203 | |
|---|
| 204 | /* |
|---|
| 205 | * Transform user interface. |
|---|
| 206 | */ |
|---|
| 207 | |
|---|
| 208 | /* |
|---|
| 209 | * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm. |
|---|
| 210 | * If that fails and the kernel supports dynamically loadable modules, it |
|---|
| 211 | * will then attempt to load a module of the same name or alias. A refcount |
|---|
| 212 | * is grabbed on the algorithm which is then associated with the new transform. |
|---|
| 213 | * |
|---|
| 214 | * crypto_free_tfm() frees up the transform and any associated resources, |
|---|
| 215 | * then drops the refcount on the associated algorithm. |
|---|
| 216 | */ |
|---|
| 217 | struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, __u32 tfm_flags); |
|---|
| 218 | void crypto_free_tfm(struct crypto_tfm *tfm); |
|---|
| 219 | |
|---|
| 220 | /* |
|---|
| 221 | * Transform helpers which query the underlying algorithm. |
|---|
| 222 | */ |
|---|
| 223 | static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm) |
|---|
| 224 | { |
|---|
| 225 | return tfm->__crt_alg->cra_name; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm) |
|---|
| 229 | { |
|---|
| 230 | return module_name(tfm->__crt_alg->cra_module); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | static inline __u32 crypto_tfm_alg_type(struct crypto_tfm *tfm) |
|---|
| 234 | { |
|---|
| 235 | return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm) |
|---|
| 239 | { |
|---|
| 240 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
|---|
| 241 | return tfm->__crt_alg->cra_cipher.cia_min_keysize; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm) |
|---|
| 245 | { |
|---|
| 246 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
|---|
| 247 | return tfm->__crt_alg->cra_cipher.cia_max_keysize; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm) |
|---|
| 251 | { |
|---|
| 252 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
|---|
| 253 | return tfm->crt_cipher.cit_ivsize; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm) |
|---|
| 257 | { |
|---|
| 258 | return tfm->__crt_alg->cra_blocksize; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm) |
|---|
| 262 | { |
|---|
| 263 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST); |
|---|
| 264 | return tfm->__crt_alg->cra_digest.dia_digestsize; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | /* |
|---|
| 268 | * API wrappers. |
|---|
| 269 | */ |
|---|
| 270 | static inline void crypto_digest_init(struct crypto_tfm *tfm) |
|---|
| 271 | { |
|---|
| 272 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST); |
|---|
| 273 | tfm->crt_digest.dit_init(tfm); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | static inline void crypto_digest_update(struct crypto_tfm *tfm, |
|---|
| 277 | struct scatterlist *sg, |
|---|
| 278 | unsigned int nsg) |
|---|
| 279 | { |
|---|
| 280 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST); |
|---|
| 281 | tfm->crt_digest.dit_update(tfm, sg, nsg); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | static inline void crypto_digest_final(struct crypto_tfm *tfm, __u8 *out) |
|---|
| 285 | { |
|---|
| 286 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST); |
|---|
| 287 | tfm->crt_digest.dit_final(tfm, out); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | static inline void crypto_digest_digest(struct crypto_tfm *tfm, |
|---|
| 291 | struct scatterlist *sg, |
|---|
| 292 | unsigned int nsg, __u8 *out) |
|---|
| 293 | { |
|---|
| 294 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST); |
|---|
| 295 | tfm->crt_digest.dit_digest(tfm, sg, nsg, out); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | static inline int crypto_digest_setkey(struct crypto_tfm *tfm, |
|---|
| 299 | const __u8 *key, unsigned int keylen) |
|---|
| 300 | { |
|---|
| 301 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST); |
|---|
| 302 | if (tfm->crt_digest.dit_setkey == NULL) |
|---|
| 303 | return -ENOSYS; |
|---|
| 304 | return tfm->crt_digest.dit_setkey(tfm, key, keylen); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | static inline int crypto_cipher_setkey(struct crypto_tfm *tfm, |
|---|
| 308 | const __u8 *key, unsigned int keylen) |
|---|
| 309 | { |
|---|
| 310 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
|---|
| 311 | return tfm->crt_cipher.cit_setkey(tfm, key, keylen); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm, |
|---|
| 315 | struct scatterlist *dst, |
|---|
| 316 | struct scatterlist *src, |
|---|
| 317 | unsigned int nbytes) |
|---|
| 318 | { |
|---|
| 319 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
|---|
| 320 | return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes); |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm, |
|---|
| 324 | struct scatterlist *dst, |
|---|
| 325 | struct scatterlist *src, |
|---|
| 326 | unsigned int nbytes, __u8 *iv) |
|---|
| 327 | { |
|---|
| 328 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
|---|
| 329 | BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB); |
|---|
| 330 | return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm, |
|---|
| 334 | struct scatterlist *dst, |
|---|
| 335 | struct scatterlist *src, |
|---|
| 336 | unsigned int nbytes) |
|---|
| 337 | { |
|---|
| 338 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
|---|
| 339 | return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes); |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm, |
|---|
| 343 | struct scatterlist *dst, |
|---|
| 344 | struct scatterlist *src, |
|---|
| 345 | unsigned int nbytes, __u8 *iv) |
|---|
| 346 | { |
|---|
| 347 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
|---|
| 348 | BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB); |
|---|
| 349 | return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv); |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm, |
|---|
| 353 | const __u8 *src, unsigned int len) |
|---|
| 354 | { |
|---|
| 355 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
|---|
| 356 | memcpy(tfm->crt_cipher.cit_iv, src, len); |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm, |
|---|
| 360 | __u8 *dst, unsigned int len) |
|---|
| 361 | { |
|---|
| 362 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); |
|---|
| 363 | memcpy(dst, tfm->crt_cipher.cit_iv, len); |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | static inline int crypto_comp_compress(struct crypto_tfm *tfm, |
|---|
| 367 | const __u8 *src, unsigned int slen, |
|---|
| 368 | __u8 *dst, unsigned int *dlen) |
|---|
| 369 | { |
|---|
| 370 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS); |
|---|
| 371 | return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen); |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | static inline int crypto_comp_decompress(struct crypto_tfm *tfm, |
|---|
| 375 | const __u8 *src, unsigned int slen, |
|---|
| 376 | __u8 *dst, unsigned int *dlen) |
|---|
| 377 | { |
|---|
| 378 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS); |
|---|
| 379 | return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen); |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | /* |
|---|
| 383 | * HMAC support. |
|---|
| 384 | */ |
|---|
| 385 | #ifdef CONFIG_CRYPTO_HMAC |
|---|
| 386 | void crypto_hmac_init(struct crypto_tfm *tfm, __u8 *key, unsigned int *keylen); |
|---|
| 387 | void crypto_hmac_update(struct crypto_tfm *tfm, |
|---|
| 388 | struct scatterlist *sg, unsigned int nsg); |
|---|
| 389 | void crypto_hmac_final(struct crypto_tfm *tfm, __u8 *key, |
|---|
| 390 | unsigned int *keylen, __u8 *out); |
|---|
| 391 | void crypto_hmac(struct crypto_tfm *tfm, __u8 *key, unsigned int *keylen, |
|---|
| 392 | struct scatterlist *sg, unsigned int nsg, __u8 *out); |
|---|
| 393 | #endif /* CONFIG_CRYPTO_HMAC */ |
|---|
| 394 | |
|---|
| 395 | #endif /* _LINUX_CRYPTO_H */ |
|---|
| 396 | |
|---|