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 <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/types.h>
23 #include <linux/list.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
29 * Algorithm masks and types.
31 #define CRYPTO_ALG_TYPE_MASK 0x000000ff
32 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
33 #define CRYPTO_ALG_TYPE_DIGEST 0x00000002
34 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000004
37 * Transform masks and values (for crt_flags).
39 #define CRYPTO_TFM_MODE_MASK 0x000000ff
40 #define CRYPTO_TFM_REQ_MASK 0x000fff00
41 #define CRYPTO_TFM_RES_MASK 0xfff00000
43 #define CRYPTO_TFM_MODE_ECB 0x00000001
44 #define CRYPTO_TFM_MODE_CBC 0x00000002
45 #define CRYPTO_TFM_MODE_CFB 0x00000004
46 #define CRYPTO_TFM_MODE_CTR 0x00000008
48 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
49 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
50 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
51 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
52 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
53 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
54 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
57 * Miscellaneous stuff.
59 #define CRYPTO_UNSPEC 0
60 #define CRYPTO_MAX_ALG_NAME 64
62 #define CRYPTO_DIR_ENCRYPT 1
63 #define CRYPTO_DIR_DECRYPT 0
66 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
67 * declaration) is used to ensure that the crypto_tfm context structure is
68 * aligned correctly for the given architecture so that there are no alignment
69 * faults for C data types. In particular, this is required on platforms such
70 * as arm where pointers are 32-bit aligned but there are data types such as
71 * u64 which require 64-bit alignment.
73 #if defined(ARCH_KMALLOC_MINALIGN)
74 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
75 #elif defined(ARCH_SLAB_MINALIGN)
76 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
79 #ifdef CRYPTO_MINALIGN
80 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
82 #define CRYPTO_MINALIGN_ATTR
89 struct crypto_tfm *tfm;
90 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
91 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
92 const u8 *src, unsigned int nbytes);
97 * Algorithms: modular crypto algorithm implementations, managed
98 * via crypto_register_alg() and crypto_unregister_alg().
101 unsigned int cia_min_keysize;
102 unsigned int cia_max_keysize;
103 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
104 unsigned int keylen, u32 *flags);
105 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
106 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
108 unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
109 u8 *dst, const u8 *src,
110 unsigned int nbytes);
111 unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
112 u8 *dst, const u8 *src,
113 unsigned int nbytes);
114 unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
115 u8 *dst, const u8 *src,
116 unsigned int nbytes);
117 unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
118 u8 *dst, const u8 *src,
119 unsigned int nbytes);
123 unsigned int dia_digestsize;
124 void (*dia_init)(struct crypto_tfm *tfm);
125 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
127 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
128 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
129 unsigned int keylen, u32 *flags);
132 struct compress_alg {
133 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
134 unsigned int slen, u8 *dst, unsigned int *dlen);
135 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
136 unsigned int slen, u8 *dst, unsigned int *dlen);
139 #define cra_cipher cra_u.cipher
140 #define cra_digest cra_u.digest
141 #define cra_compress cra_u.compress
144 struct list_head cra_list;
146 unsigned int cra_blocksize;
147 unsigned int cra_ctxsize;
148 unsigned int cra_alignmask;
152 char cra_name[CRYPTO_MAX_ALG_NAME];
153 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
156 struct cipher_alg cipher;
157 struct digest_alg digest;
158 struct compress_alg compress;
161 int (*cra_init)(struct crypto_tfm *tfm);
162 void (*cra_exit)(struct crypto_tfm *tfm);
164 struct module *cra_module;
168 * Algorithm registration interface.
170 int crypto_register_alg(struct crypto_alg *alg);
171 int crypto_unregister_alg(struct crypto_alg *alg);
174 * Algorithm query interface.
177 int crypto_alg_available(const char *name, u32 flags);
179 static inline int crypto_alg_available(const char *name, u32 flags)
186 * Transforms: user-instantiated objects which encapsulate algorithms
187 * and core processing logic. Managed via crypto_alloc_tfm() and
188 * crypto_free_tfm(), as well as the various helpers below.
193 unsigned int cit_ivsize;
195 int (*cit_setkey)(struct crypto_tfm *tfm,
196 const u8 *key, unsigned int keylen);
197 int (*cit_encrypt)(struct crypto_tfm *tfm,
198 struct scatterlist *dst,
199 struct scatterlist *src,
200 unsigned int nbytes);
201 int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
202 struct scatterlist *dst,
203 struct scatterlist *src,
204 unsigned int nbytes, u8 *iv);
205 int (*cit_decrypt)(struct crypto_tfm *tfm,
206 struct scatterlist *dst,
207 struct scatterlist *src,
208 unsigned int nbytes);
209 int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
210 struct scatterlist *dst,
211 struct scatterlist *src,
212 unsigned int nbytes, u8 *iv);
213 void (*cit_xor_block)(u8 *dst, const u8 *src);
217 void (*dit_init)(struct crypto_tfm *tfm);
218 void (*dit_update)(struct crypto_tfm *tfm,
219 struct scatterlist *sg, unsigned int nsg);
220 void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
221 void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
222 unsigned int nsg, u8 *out);
223 int (*dit_setkey)(struct crypto_tfm *tfm,
224 const u8 *key, unsigned int keylen);
225 #ifdef CONFIG_CRYPTO_HMAC
226 void *dit_hmac_block;
230 struct compress_tfm {
231 int (*cot_compress)(struct crypto_tfm *tfm,
232 const u8 *src, unsigned int slen,
233 u8 *dst, unsigned int *dlen);
234 int (*cot_decompress)(struct crypto_tfm *tfm,
235 const u8 *src, unsigned int slen,
236 u8 *dst, unsigned int *dlen);
239 #define crt_cipher crt_u.cipher
240 #define crt_digest crt_u.digest
241 #define crt_compress crt_u.compress
248 struct cipher_tfm cipher;
249 struct digest_tfm digest;
250 struct compress_tfm compress;
253 struct crypto_alg *__crt_alg;
255 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
259 * Transform user interface.
263 * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
264 * If that fails and the kernel supports dynamically loadable modules, it
265 * will then attempt to load a module of the same name or alias. A refcount
266 * is grabbed on the algorithm which is then associated with the new transform.
268 * crypto_free_tfm() frees up the transform and any associated resources,
269 * then drops the refcount on the associated algorithm.
271 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
272 void crypto_free_tfm(struct crypto_tfm *tfm);
275 * Transform helpers which query the underlying algorithm.
277 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
279 return tfm->__crt_alg->cra_name;
282 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
284 return module_name(tfm->__crt_alg->cra_module);
287 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
289 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
292 static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
294 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
295 return tfm->__crt_alg->cra_cipher.cia_min_keysize;
298 static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
300 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
301 return tfm->__crt_alg->cra_cipher.cia_max_keysize;
304 static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
306 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
307 return tfm->crt_cipher.cit_ivsize;
310 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
312 return tfm->__crt_alg->cra_blocksize;
315 static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
317 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
318 return tfm->__crt_alg->cra_digest.dia_digestsize;
321 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
323 return tfm->__crt_alg->cra_alignmask;
326 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
328 return tfm->__crt_ctx;
331 static inline unsigned int crypto_tfm_ctx_alignment(void)
333 struct crypto_tfm *tfm;
334 return __alignof__(tfm->__crt_ctx);
340 static inline void crypto_digest_init(struct crypto_tfm *tfm)
342 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
343 tfm->crt_digest.dit_init(tfm);
346 static inline void crypto_digest_update(struct crypto_tfm *tfm,
347 struct scatterlist *sg,
350 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
351 tfm->crt_digest.dit_update(tfm, sg, nsg);
354 static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
356 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
357 tfm->crt_digest.dit_final(tfm, out);
360 static inline void crypto_digest_digest(struct crypto_tfm *tfm,
361 struct scatterlist *sg,
362 unsigned int nsg, u8 *out)
364 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
365 tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
368 static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
369 const u8 *key, unsigned int keylen)
371 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
372 if (tfm->crt_digest.dit_setkey == NULL)
374 return tfm->crt_digest.dit_setkey(tfm, key, keylen);
377 static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
378 const u8 *key, unsigned int keylen)
380 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
381 return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
384 static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
385 struct scatterlist *dst,
386 struct scatterlist *src,
389 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
390 return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
393 static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
394 struct scatterlist *dst,
395 struct scatterlist *src,
396 unsigned int nbytes, u8 *iv)
398 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
399 BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
400 return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
403 static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
404 struct scatterlist *dst,
405 struct scatterlist *src,
408 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
409 return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
412 static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
413 struct scatterlist *dst,
414 struct scatterlist *src,
415 unsigned int nbytes, u8 *iv)
417 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
418 BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
419 return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
422 static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
423 const u8 *src, unsigned int len)
425 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
426 memcpy(tfm->crt_cipher.cit_iv, src, len);
429 static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
430 u8 *dst, unsigned int len)
432 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
433 memcpy(dst, tfm->crt_cipher.cit_iv, len);
436 static inline int crypto_comp_compress(struct crypto_tfm *tfm,
437 const u8 *src, unsigned int slen,
438 u8 *dst, unsigned int *dlen)
440 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
441 return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
444 static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
445 const u8 *src, unsigned int slen,
446 u8 *dst, unsigned int *dlen)
448 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
449 return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
455 #ifdef CONFIG_CRYPTO_HMAC
456 void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
457 void crypto_hmac_update(struct crypto_tfm *tfm,
458 struct scatterlist *sg, unsigned int nsg);
459 void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
460 unsigned int *keylen, u8 *out);
461 void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
462 struct scatterlist *sg, unsigned int nsg, u8 *out);
463 #endif /* CONFIG_CRYPTO_HMAC */
465 #endif /* _LINUX_CRYPTO_H */