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 0x0000000f
33 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
34 #define CRYPTO_ALG_TYPE_DIGEST 0x00000002
35 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000004
37 #define CRYPTO_ALG_LARVAL 0x00000010
38 #define CRYPTO_ALG_DEAD 0x00000020
39 #define CRYPTO_ALG_DYING 0x00000040
42 * Transform masks and values (for crt_flags).
44 #define CRYPTO_TFM_MODE_MASK 0x000000ff
45 #define CRYPTO_TFM_REQ_MASK 0x000fff00
46 #define CRYPTO_TFM_RES_MASK 0xfff00000
48 #define CRYPTO_TFM_MODE_ECB 0x00000001
49 #define CRYPTO_TFM_MODE_CBC 0x00000002
50 #define CRYPTO_TFM_MODE_CFB 0x00000004
51 #define CRYPTO_TFM_MODE_CTR 0x00000008
53 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
54 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
55 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
56 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
57 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
58 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
59 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
62 * Miscellaneous stuff.
64 #define CRYPTO_UNSPEC 0
65 #define CRYPTO_MAX_ALG_NAME 64
67 #define CRYPTO_DIR_ENCRYPT 1
68 #define CRYPTO_DIR_DECRYPT 0
71 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
72 * declaration) is used to ensure that the crypto_tfm context structure is
73 * aligned correctly for the given architecture so that there are no alignment
74 * faults for C data types. In particular, this is required on platforms such
75 * as arm where pointers are 32-bit aligned but there are data types such as
76 * u64 which require 64-bit alignment.
78 #if defined(ARCH_KMALLOC_MINALIGN)
79 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
80 #elif defined(ARCH_SLAB_MINALIGN)
81 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
84 #ifdef CRYPTO_MINALIGN
85 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
87 #define CRYPTO_MINALIGN_ATTR
94 struct crypto_tfm
*tfm
;
95 void (*crfn
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
96 unsigned int (*prfn
)(const struct cipher_desc
*desc
, u8
*dst
,
97 const u8
*src
, unsigned int nbytes
);
102 * Algorithms: modular crypto algorithm implementations, managed
103 * via crypto_register_alg() and crypto_unregister_alg().
106 unsigned int cia_min_keysize
;
107 unsigned int cia_max_keysize
;
108 int (*cia_setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
109 unsigned int keylen
, u32
*flags
);
110 void (*cia_encrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
111 void (*cia_decrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
113 unsigned int (*cia_encrypt_ecb
)(const struct cipher_desc
*desc
,
114 u8
*dst
, const u8
*src
,
115 unsigned int nbytes
);
116 unsigned int (*cia_decrypt_ecb
)(const struct cipher_desc
*desc
,
117 u8
*dst
, const u8
*src
,
118 unsigned int nbytes
);
119 unsigned int (*cia_encrypt_cbc
)(const struct cipher_desc
*desc
,
120 u8
*dst
, const u8
*src
,
121 unsigned int nbytes
);
122 unsigned int (*cia_decrypt_cbc
)(const struct cipher_desc
*desc
,
123 u8
*dst
, const u8
*src
,
124 unsigned int nbytes
);
128 unsigned int dia_digestsize
;
129 void (*dia_init
)(struct crypto_tfm
*tfm
);
130 void (*dia_update
)(struct crypto_tfm
*tfm
, const u8
*data
,
132 void (*dia_final
)(struct crypto_tfm
*tfm
, u8
*out
);
133 int (*dia_setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
134 unsigned int keylen
, u32
*flags
);
137 struct compress_alg
{
138 int (*coa_compress
)(struct crypto_tfm
*tfm
, const u8
*src
,
139 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
140 int (*coa_decompress
)(struct crypto_tfm
*tfm
, const u8
*src
,
141 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
144 #define cra_cipher cra_u.cipher
145 #define cra_digest cra_u.digest
146 #define cra_compress cra_u.compress
149 struct list_head cra_list
;
150 struct list_head cra_users
;
153 unsigned int cra_blocksize
;
154 unsigned int cra_ctxsize
;
155 unsigned int cra_alignmask
;
160 char cra_name
[CRYPTO_MAX_ALG_NAME
];
161 char cra_driver_name
[CRYPTO_MAX_ALG_NAME
];
164 struct cipher_alg cipher
;
165 struct digest_alg digest
;
166 struct compress_alg compress
;
169 int (*cra_init
)(struct crypto_tfm
*tfm
);
170 void (*cra_exit
)(struct crypto_tfm
*tfm
);
171 void (*cra_destroy
)(struct crypto_alg
*alg
);
173 struct module
*cra_module
;
177 * Algorithm registration interface.
179 int crypto_register_alg(struct crypto_alg
*alg
);
180 int crypto_unregister_alg(struct crypto_alg
*alg
);
183 * Algorithm query interface.
186 int crypto_alg_available(const char *name
, u32 flags
);
188 static inline int crypto_alg_available(const char *name
, u32 flags
)
195 * Transforms: user-instantiated objects which encapsulate algorithms
196 * and core processing logic. Managed via crypto_alloc_tfm() and
197 * crypto_free_tfm(), as well as the various helpers below.
202 unsigned int cit_ivsize
;
204 int (*cit_setkey
)(struct crypto_tfm
*tfm
,
205 const u8
*key
, unsigned int keylen
);
206 int (*cit_encrypt
)(struct crypto_tfm
*tfm
,
207 struct scatterlist
*dst
,
208 struct scatterlist
*src
,
209 unsigned int nbytes
);
210 int (*cit_encrypt_iv
)(struct crypto_tfm
*tfm
,
211 struct scatterlist
*dst
,
212 struct scatterlist
*src
,
213 unsigned int nbytes
, u8
*iv
);
214 int (*cit_decrypt
)(struct crypto_tfm
*tfm
,
215 struct scatterlist
*dst
,
216 struct scatterlist
*src
,
217 unsigned int nbytes
);
218 int (*cit_decrypt_iv
)(struct crypto_tfm
*tfm
,
219 struct scatterlist
*dst
,
220 struct scatterlist
*src
,
221 unsigned int nbytes
, u8
*iv
);
222 void (*cit_xor_block
)(u8
*dst
, const u8
*src
);
226 void (*dit_init
)(struct crypto_tfm
*tfm
);
227 void (*dit_update
)(struct crypto_tfm
*tfm
,
228 struct scatterlist
*sg
, unsigned int nsg
);
229 void (*dit_final
)(struct crypto_tfm
*tfm
, u8
*out
);
230 void (*dit_digest
)(struct crypto_tfm
*tfm
, struct scatterlist
*sg
,
231 unsigned int nsg
, u8
*out
);
232 int (*dit_setkey
)(struct crypto_tfm
*tfm
,
233 const u8
*key
, unsigned int keylen
);
234 #ifdef CONFIG_CRYPTO_HMAC
235 void *dit_hmac_block
;
239 struct compress_tfm
{
240 int (*cot_compress
)(struct crypto_tfm
*tfm
,
241 const u8
*src
, unsigned int slen
,
242 u8
*dst
, unsigned int *dlen
);
243 int (*cot_decompress
)(struct crypto_tfm
*tfm
,
244 const u8
*src
, unsigned int slen
,
245 u8
*dst
, unsigned int *dlen
);
248 #define crt_cipher crt_u.cipher
249 #define crt_digest crt_u.digest
250 #define crt_compress crt_u.compress
257 struct cipher_tfm cipher
;
258 struct digest_tfm digest
;
259 struct compress_tfm compress
;
262 struct crypto_alg
*__crt_alg
;
264 void *__crt_ctx
[] CRYPTO_MINALIGN_ATTR
;
272 struct crypto_attr_alg
{
273 char name
[CRYPTO_MAX_ALG_NAME
];
277 * Transform user interface.
281 * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
282 * If that fails and the kernel supports dynamically loadable modules, it
283 * will then attempt to load a module of the same name or alias. A refcount
284 * is grabbed on the algorithm which is then associated with the new transform.
286 * crypto_free_tfm() frees up the transform and any associated resources,
287 * then drops the refcount on the associated algorithm.
289 struct crypto_tfm
*crypto_alloc_tfm(const char *alg_name
, u32 tfm_flags
);
290 void crypto_free_tfm(struct crypto_tfm
*tfm
);
293 * Transform helpers which query the underlying algorithm.
295 static inline const char *crypto_tfm_alg_name(struct crypto_tfm
*tfm
)
297 return tfm
->__crt_alg
->cra_name
;
300 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm
*tfm
)
302 return module_name(tfm
->__crt_alg
->cra_module
);
305 static inline u32
crypto_tfm_alg_type(struct crypto_tfm
*tfm
)
307 return tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
;
310 static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm
*tfm
)
312 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
313 return tfm
->__crt_alg
->cra_cipher
.cia_min_keysize
;
316 static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm
*tfm
)
318 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
319 return tfm
->__crt_alg
->cra_cipher
.cia_max_keysize
;
322 static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm
*tfm
)
324 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
325 return tfm
->crt_cipher
.cit_ivsize
;
328 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm
*tfm
)
330 return tfm
->__crt_alg
->cra_blocksize
;
333 static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm
*tfm
)
335 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
336 return tfm
->__crt_alg
->cra_digest
.dia_digestsize
;
339 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm
*tfm
)
341 return tfm
->__crt_alg
->cra_alignmask
;
344 static inline void *crypto_tfm_ctx(struct crypto_tfm
*tfm
)
346 return tfm
->__crt_ctx
;
349 static inline unsigned int crypto_tfm_ctx_alignment(void)
351 struct crypto_tfm
*tfm
;
352 return __alignof__(tfm
->__crt_ctx
);
358 static inline void crypto_digest_init(struct crypto_tfm
*tfm
)
360 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
361 tfm
->crt_digest
.dit_init(tfm
);
364 static inline void crypto_digest_update(struct crypto_tfm
*tfm
,
365 struct scatterlist
*sg
,
368 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
369 tfm
->crt_digest
.dit_update(tfm
, sg
, nsg
);
372 static inline void crypto_digest_final(struct crypto_tfm
*tfm
, u8
*out
)
374 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
375 tfm
->crt_digest
.dit_final(tfm
, out
);
378 static inline void crypto_digest_digest(struct crypto_tfm
*tfm
,
379 struct scatterlist
*sg
,
380 unsigned int nsg
, u8
*out
)
382 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
383 tfm
->crt_digest
.dit_digest(tfm
, sg
, nsg
, out
);
386 static inline int crypto_digest_setkey(struct crypto_tfm
*tfm
,
387 const u8
*key
, unsigned int keylen
)
389 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
390 if (tfm
->crt_digest
.dit_setkey
== NULL
)
392 return tfm
->crt_digest
.dit_setkey(tfm
, key
, keylen
);
395 static inline int crypto_cipher_setkey(struct crypto_tfm
*tfm
,
396 const u8
*key
, unsigned int keylen
)
398 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
399 return tfm
->crt_cipher
.cit_setkey(tfm
, key
, keylen
);
402 static inline int crypto_cipher_encrypt(struct crypto_tfm
*tfm
,
403 struct scatterlist
*dst
,
404 struct scatterlist
*src
,
407 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
408 return tfm
->crt_cipher
.cit_encrypt(tfm
, dst
, src
, nbytes
);
411 static inline int crypto_cipher_encrypt_iv(struct crypto_tfm
*tfm
,
412 struct scatterlist
*dst
,
413 struct scatterlist
*src
,
414 unsigned int nbytes
, u8
*iv
)
416 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
417 BUG_ON(tfm
->crt_cipher
.cit_mode
== CRYPTO_TFM_MODE_ECB
);
418 return tfm
->crt_cipher
.cit_encrypt_iv(tfm
, dst
, src
, nbytes
, iv
);
421 static inline int crypto_cipher_decrypt(struct crypto_tfm
*tfm
,
422 struct scatterlist
*dst
,
423 struct scatterlist
*src
,
426 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
427 return tfm
->crt_cipher
.cit_decrypt(tfm
, dst
, src
, nbytes
);
430 static inline int crypto_cipher_decrypt_iv(struct crypto_tfm
*tfm
,
431 struct scatterlist
*dst
,
432 struct scatterlist
*src
,
433 unsigned int nbytes
, u8
*iv
)
435 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
436 BUG_ON(tfm
->crt_cipher
.cit_mode
== CRYPTO_TFM_MODE_ECB
);
437 return tfm
->crt_cipher
.cit_decrypt_iv(tfm
, dst
, src
, nbytes
, iv
);
440 static inline void crypto_cipher_set_iv(struct crypto_tfm
*tfm
,
441 const u8
*src
, unsigned int len
)
443 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
444 memcpy(tfm
->crt_cipher
.cit_iv
, src
, len
);
447 static inline void crypto_cipher_get_iv(struct crypto_tfm
*tfm
,
448 u8
*dst
, unsigned int len
)
450 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
451 memcpy(dst
, tfm
->crt_cipher
.cit_iv
, len
);
454 static inline int crypto_comp_compress(struct crypto_tfm
*tfm
,
455 const u8
*src
, unsigned int slen
,
456 u8
*dst
, unsigned int *dlen
)
458 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_COMPRESS
);
459 return tfm
->crt_compress
.cot_compress(tfm
, src
, slen
, dst
, dlen
);
462 static inline int crypto_comp_decompress(struct crypto_tfm
*tfm
,
463 const u8
*src
, unsigned int slen
,
464 u8
*dst
, unsigned int *dlen
)
466 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_COMPRESS
);
467 return tfm
->crt_compress
.cot_decompress(tfm
, src
, slen
, dst
, dlen
);
473 #ifdef CONFIG_CRYPTO_HMAC
474 void crypto_hmac_init(struct crypto_tfm
*tfm
, u8
*key
, unsigned int *keylen
);
475 void crypto_hmac_update(struct crypto_tfm
*tfm
,
476 struct scatterlist
*sg
, unsigned int nsg
);
477 void crypto_hmac_final(struct crypto_tfm
*tfm
, u8
*key
,
478 unsigned int *keylen
, u8
*out
);
479 void crypto_hmac(struct crypto_tfm
*tfm
, u8
*key
, unsigned int *keylen
,
480 struct scatterlist
*sg
, unsigned int nsg
, u8
*out
);
481 #endif /* CONFIG_CRYPTO_HMAC */
483 #endif /* _LINUX_CRYPTO_H */
This page took 0.042263 seconds and 6 git commands to generate.