2 * Scatterlist Cryptographic API.
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
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)
17 #ifndef _LINUX_CRYPTO_H
18 #define _LINUX_CRYPTO_H
20 #include <asm/atomic.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/types.h>
24 #include <linux/list.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/uaccess.h>
30 * Algorithm masks and types.
32 #define CRYPTO_ALG_TYPE_MASK 0x000000ff
33 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
34 #define CRYPTO_ALG_TYPE_DIGEST 0x00000002
35 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000004
38 * Transform masks and values (for crt_flags).
40 #define CRYPTO_TFM_MODE_MASK 0x000000ff
41 #define CRYPTO_TFM_REQ_MASK 0x000fff00
42 #define CRYPTO_TFM_RES_MASK 0xfff00000
44 #define CRYPTO_TFM_MODE_ECB 0x00000001
45 #define CRYPTO_TFM_MODE_CBC 0x00000002
46 #define CRYPTO_TFM_MODE_CFB 0x00000004
47 #define CRYPTO_TFM_MODE_CTR 0x00000008
49 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
50 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
51 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
52 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
53 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
54 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
55 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
58 * Miscellaneous stuff.
60 #define CRYPTO_UNSPEC 0
61 #define CRYPTO_MAX_ALG_NAME 64
63 #define CRYPTO_DIR_ENCRYPT 1
64 #define CRYPTO_DIR_DECRYPT 0
67 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
68 * declaration) is used to ensure that the crypto_tfm context structure is
69 * aligned correctly for the given architecture so that there are no alignment
70 * faults for C data types. In particular, this is required on platforms such
71 * as arm where pointers are 32-bit aligned but there are data types such as
72 * u64 which require 64-bit alignment.
74 #if defined(ARCH_KMALLOC_MINALIGN)
75 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
76 #elif defined(ARCH_SLAB_MINALIGN)
77 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
80 #ifdef CRYPTO_MINALIGN
81 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
83 #define CRYPTO_MINALIGN_ATTR
90 struct crypto_tfm *tfm;
91 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
92 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
93 const u8 *src, unsigned int nbytes);
98 * Algorithms: modular crypto algorithm implementations, managed
99 * via crypto_register_alg() and crypto_unregister_alg().
102 unsigned int cia_min_keysize;
103 unsigned int cia_max_keysize;
104 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
105 unsigned int keylen, u32 *flags);
106 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
107 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
109 unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
110 u8 *dst, const u8 *src,
111 unsigned int nbytes);
112 unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
113 u8 *dst, const u8 *src,
114 unsigned int nbytes);
115 unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
116 u8 *dst, const u8 *src,
117 unsigned int nbytes);
118 unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
119 u8 *dst, const u8 *src,
120 unsigned int nbytes);
124 unsigned int dia_digestsize;
125 void (*dia_init)(struct crypto_tfm *tfm);
126 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
128 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
129 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
130 unsigned int keylen, u32 *flags);
133 struct compress_alg {
134 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
135 unsigned int slen, u8 *dst, unsigned int *dlen);
136 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
137 unsigned int slen, u8 *dst, unsigned int *dlen);
140 #define cra_cipher cra_u.cipher
141 #define cra_digest cra_u.digest
142 #define cra_compress cra_u.compress
145 struct list_head cra_list;
147 unsigned int cra_blocksize;
148 unsigned int cra_ctxsize;
149 unsigned int cra_alignmask;
154 char cra_name[CRYPTO_MAX_ALG_NAME];
155 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
158 struct cipher_alg cipher;
159 struct digest_alg digest;
160 struct compress_alg compress;
163 int (*cra_init)(struct crypto_tfm *tfm);
164 void (*cra_exit)(struct crypto_tfm *tfm);
165 void (*cra_destroy)(struct crypto_alg *alg);
167 struct module *cra_module;
171 * Algorithm registration interface.
173 int crypto_register_alg(struct crypto_alg *alg);
174 int crypto_unregister_alg(struct crypto_alg *alg);
177 * Algorithm query interface.
180 int crypto_alg_available(const char *name, u32 flags);
182 static inline int crypto_alg_available(const char *name, u32 flags)
189 * Transforms: user-instantiated objects which encapsulate algorithms
190 * and core processing logic. Managed via crypto_alloc_tfm() and
191 * crypto_free_tfm(), as well as the various helpers below.
196 unsigned int cit_ivsize;
198 int (*cit_setkey)(struct crypto_tfm *tfm,
199 const u8 *key, unsigned int keylen);
200 int (*cit_encrypt)(struct crypto_tfm *tfm,
201 struct scatterlist *dst,
202 struct scatterlist *src,
203 unsigned int nbytes);
204 int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
205 struct scatterlist *dst,
206 struct scatterlist *src,
207 unsigned int nbytes, u8 *iv);
208 int (*cit_decrypt)(struct crypto_tfm *tfm,
209 struct scatterlist *dst,
210 struct scatterlist *src,
211 unsigned int nbytes);
212 int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
213 struct scatterlist *dst,
214 struct scatterlist *src,
215 unsigned int nbytes, u8 *iv);
216 void (*cit_xor_block)(u8 *dst, const u8 *src);
220 void (*dit_init)(struct crypto_tfm *tfm);
221 void (*dit_update)(struct crypto_tfm *tfm,
222 struct scatterlist *sg, unsigned int nsg);
223 void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
224 void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
225 unsigned int nsg, u8 *out);
226 int (*dit_setkey)(struct crypto_tfm *tfm,
227 const u8 *key, unsigned int keylen);
228 #ifdef CONFIG_CRYPTO_HMAC
229 void *dit_hmac_block;
233 struct compress_tfm {
234 int (*cot_compress)(struct crypto_tfm *tfm,
235 const u8 *src, unsigned int slen,
236 u8 *dst, unsigned int *dlen);
237 int (*cot_decompress)(struct crypto_tfm *tfm,
238 const u8 *src, unsigned int slen,
239 u8 *dst, unsigned int *dlen);
242 #define crt_cipher crt_u.cipher
243 #define crt_digest crt_u.digest
244 #define crt_compress crt_u.compress
251 struct cipher_tfm cipher;
252 struct digest_tfm digest;
253 struct compress_tfm compress;
256 struct crypto_alg *__crt_alg;
258 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
262 * Transform user interface.
266 * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
267 * If that fails and the kernel supports dynamically loadable modules, it
268 * will then attempt to load a module of the same name or alias. A refcount
269 * is grabbed on the algorithm which is then associated with the new transform.
271 * crypto_free_tfm() frees up the transform and any associated resources,
272 * then drops the refcount on the associated algorithm.
274 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
275 void crypto_free_tfm(struct crypto_tfm *tfm);
278 * Transform helpers which query the underlying algorithm.
280 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
282 return tfm->__crt_alg->cra_name;
285 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
287 return module_name(tfm->__crt_alg->cra_module);
290 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
292 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
295 static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
297 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
298 return tfm->__crt_alg->cra_cipher.cia_min_keysize;
301 static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
303 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
304 return tfm->__crt_alg->cra_cipher.cia_max_keysize;
307 static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
309 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
310 return tfm->crt_cipher.cit_ivsize;
313 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
315 return tfm->__crt_alg->cra_blocksize;
318 static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
320 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
321 return tfm->__crt_alg->cra_digest.dia_digestsize;
324 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
326 return tfm->__crt_alg->cra_alignmask;
329 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
331 return tfm->__crt_ctx;
334 static inline unsigned int crypto_tfm_ctx_alignment(void)
336 struct crypto_tfm *tfm;
337 return __alignof__(tfm->__crt_ctx);
343 static inline void crypto_digest_init(struct crypto_tfm *tfm)
345 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
346 tfm->crt_digest.dit_init(tfm);
349 static inline void crypto_digest_update(struct crypto_tfm *tfm,
350 struct scatterlist *sg,
353 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
354 tfm->crt_digest.dit_update(tfm, sg, nsg);
357 static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
359 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
360 tfm->crt_digest.dit_final(tfm, out);
363 static inline void crypto_digest_digest(struct crypto_tfm *tfm,
364 struct scatterlist *sg,
365 unsigned int nsg, u8 *out)
367 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
368 tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
371 static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
372 const u8 *key, unsigned int keylen)
374 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
375 if (tfm->crt_digest.dit_setkey == NULL)
377 return tfm->crt_digest.dit_setkey(tfm, key, keylen);
380 static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
381 const u8 *key, unsigned int keylen)
383 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
384 return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
387 static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
388 struct scatterlist *dst,
389 struct scatterlist *src,
392 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
393 return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
396 static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
397 struct scatterlist *dst,
398 struct scatterlist *src,
399 unsigned int nbytes, u8 *iv)
401 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
402 BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
403 return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
406 static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
407 struct scatterlist *dst,
408 struct scatterlist *src,
411 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
412 return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
415 static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
416 struct scatterlist *dst,
417 struct scatterlist *src,
418 unsigned int nbytes, u8 *iv)
420 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
421 BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
422 return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
425 static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
426 const u8 *src, unsigned int len)
428 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
429 memcpy(tfm->crt_cipher.cit_iv, src, len);
432 static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
433 u8 *dst, unsigned int len)
435 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
436 memcpy(dst, tfm->crt_cipher.cit_iv, len);
439 static inline int crypto_comp_compress(struct crypto_tfm *tfm,
440 const u8 *src, unsigned int slen,
441 u8 *dst, unsigned int *dlen)
443 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
444 return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
447 static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
448 const u8 *src, unsigned int slen,
449 u8 *dst, unsigned int *dlen)
451 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
452 return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
458 #ifdef CONFIG_CRYPTO_HMAC
459 void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
460 void crypto_hmac_update(struct crypto_tfm *tfm,
461 struct scatterlist *sg, unsigned int nsg);
462 void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
463 unsigned int *keylen, u8 *out);
464 void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
465 struct scatterlist *sg, unsigned int nsg, u8 *out);
466 #endif /* CONFIG_CRYPTO_HMAC */
468 #endif /* _LINUX_CRYPTO_H */