2 * Scatterlist Cryptographic API.
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
7 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
8 * and Nettle, by Niels Möller.
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)
16 #ifndef _LINUX_CRYPTO_H
17 #define _LINUX_CRYPTO_H
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/types.h>
23 #include <linux/list.h>
24 #include <linux/string.h>
28 * Algorithm masks and types.
30 #define CRYPTO_ALG_TYPE_MASK 0x000000ff
31 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
32 #define CRYPTO_ALG_TYPE_DIGEST 0x00000002
33 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000004
36 * Transform masks and values (for crt_flags).
38 #define CRYPTO_TFM_MODE_MASK 0x000000ff
39 #define CRYPTO_TFM_REQ_MASK 0x000fff00
40 #define CRYPTO_TFM_RES_MASK 0xfff00000
42 #define CRYPTO_TFM_MODE_ECB 0x00000001
43 #define CRYPTO_TFM_MODE_CBC 0x00000002
44 #define CRYPTO_TFM_MODE_CFB 0x00000004
45 #define CRYPTO_TFM_MODE_CTR 0x00000008
47 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
48 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
49 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
50 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
51 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
52 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
55 * Miscellaneous stuff.
57 #define CRYPTO_UNSPEC 0
58 #define CRYPTO_MAX_ALG_NAME 64
60 #define CRYPTO_DIR_ENCRYPT 1
61 #define CRYPTO_DIR_DECRYPT 0
67 struct crypto_tfm
*tfm
;
68 void (*crfn
)(void *ctx
, u8
*dst
, const u8
*src
);
69 unsigned int (*prfn
)(const struct cipher_desc
*desc
, u8
*dst
,
70 const u8
*src
, unsigned int nbytes
);
75 * Algorithms: modular crypto algorithm implementations, managed
76 * via crypto_register_alg() and crypto_unregister_alg().
79 unsigned int cia_min_keysize
;
80 unsigned int cia_max_keysize
;
81 int (*cia_setkey
)(void *ctx
, const u8
*key
,
82 unsigned int keylen
, u32
*flags
);
83 void (*cia_encrypt
)(void *ctx
, u8
*dst
, const u8
*src
);
84 void (*cia_decrypt
)(void *ctx
, u8
*dst
, const u8
*src
);
86 unsigned int (*cia_encrypt_ecb
)(const struct cipher_desc
*desc
,
87 u8
*dst
, const u8
*src
,
89 unsigned int (*cia_decrypt_ecb
)(const struct cipher_desc
*desc
,
90 u8
*dst
, const u8
*src
,
92 unsigned int (*cia_encrypt_cbc
)(const struct cipher_desc
*desc
,
93 u8
*dst
, const u8
*src
,
95 unsigned int (*cia_decrypt_cbc
)(const struct cipher_desc
*desc
,
96 u8
*dst
, const u8
*src
,
101 unsigned int dia_digestsize
;
102 void (*dia_init
)(void *ctx
);
103 void (*dia_update
)(void *ctx
, const u8
*data
, unsigned int len
);
104 void (*dia_final
)(void *ctx
, u8
*out
);
105 int (*dia_setkey
)(void *ctx
, const u8
*key
,
106 unsigned int keylen
, u32
*flags
);
109 struct compress_alg
{
110 int (*coa_init
)(void *ctx
);
111 void (*coa_exit
)(void *ctx
);
112 int (*coa_compress
)(void *ctx
, const u8
*src
, unsigned int slen
,
113 u8
*dst
, unsigned int *dlen
);
114 int (*coa_decompress
)(void *ctx
, const u8
*src
, unsigned int slen
,
115 u8
*dst
, unsigned int *dlen
);
118 #define cra_cipher cra_u.cipher
119 #define cra_digest cra_u.digest
120 #define cra_compress cra_u.compress
123 struct list_head cra_list
;
125 unsigned int cra_blocksize
;
126 unsigned int cra_ctxsize
;
127 const char cra_name
[CRYPTO_MAX_ALG_NAME
];
130 struct cipher_alg cipher
;
131 struct digest_alg digest
;
132 struct compress_alg compress
;
135 struct module
*cra_module
;
139 * Algorithm registration interface.
141 int crypto_register_alg(struct crypto_alg
*alg
);
142 int crypto_unregister_alg(struct crypto_alg
*alg
);
145 * Algorithm query interface.
148 int crypto_alg_available(const char *name
, u32 flags
);
150 static inline int crypto_alg_available(const char *name
, u32 flags
)
157 * Transforms: user-instantiated objects which encapsulate algorithms
158 * and core processing logic. Managed via crypto_alloc_tfm() and
159 * crypto_free_tfm(), as well as the various helpers below.
164 unsigned int cit_ivsize
;
166 int (*cit_setkey
)(struct crypto_tfm
*tfm
,
167 const u8
*key
, unsigned int keylen
);
168 int (*cit_encrypt
)(struct crypto_tfm
*tfm
,
169 struct scatterlist
*dst
,
170 struct scatterlist
*src
,
171 unsigned int nbytes
);
172 int (*cit_encrypt_iv
)(struct crypto_tfm
*tfm
,
173 struct scatterlist
*dst
,
174 struct scatterlist
*src
,
175 unsigned int nbytes
, u8
*iv
);
176 int (*cit_decrypt
)(struct crypto_tfm
*tfm
,
177 struct scatterlist
*dst
,
178 struct scatterlist
*src
,
179 unsigned int nbytes
);
180 int (*cit_decrypt_iv
)(struct crypto_tfm
*tfm
,
181 struct scatterlist
*dst
,
182 struct scatterlist
*src
,
183 unsigned int nbytes
, u8
*iv
);
184 void (*cit_xor_block
)(u8
*dst
, const u8
*src
);
188 void (*dit_init
)(struct crypto_tfm
*tfm
);
189 void (*dit_update
)(struct crypto_tfm
*tfm
,
190 struct scatterlist
*sg
, unsigned int nsg
);
191 void (*dit_final
)(struct crypto_tfm
*tfm
, u8
*out
);
192 void (*dit_digest
)(struct crypto_tfm
*tfm
, struct scatterlist
*sg
,
193 unsigned int nsg
, u8
*out
);
194 int (*dit_setkey
)(struct crypto_tfm
*tfm
,
195 const u8
*key
, unsigned int keylen
);
196 #ifdef CONFIG_CRYPTO_HMAC
197 void *dit_hmac_block
;
201 struct compress_tfm
{
202 int (*cot_compress
)(struct crypto_tfm
*tfm
,
203 const u8
*src
, unsigned int slen
,
204 u8
*dst
, unsigned int *dlen
);
205 int (*cot_decompress
)(struct crypto_tfm
*tfm
,
206 const u8
*src
, unsigned int slen
,
207 u8
*dst
, unsigned int *dlen
);
210 #define crt_cipher crt_u.cipher
211 #define crt_digest crt_u.digest
212 #define crt_compress crt_u.compress
219 struct cipher_tfm cipher
;
220 struct digest_tfm digest
;
221 struct compress_tfm compress
;
224 struct crypto_alg
*__crt_alg
;
228 * Transform user interface.
232 * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
233 * If that fails and the kernel supports dynamically loadable modules, it
234 * will then attempt to load a module of the same name or alias. A refcount
235 * is grabbed on the algorithm which is then associated with the new transform.
237 * crypto_free_tfm() frees up the transform and any associated resources,
238 * then drops the refcount on the associated algorithm.
240 struct crypto_tfm
*crypto_alloc_tfm(const char *alg_name
, u32 tfm_flags
);
241 void crypto_free_tfm(struct crypto_tfm
*tfm
);
244 * Transform helpers which query the underlying algorithm.
246 static inline const char *crypto_tfm_alg_name(struct crypto_tfm
*tfm
)
248 return tfm
->__crt_alg
->cra_name
;
251 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm
*tfm
)
253 return module_name(tfm
->__crt_alg
->cra_module
);
256 static inline u32
crypto_tfm_alg_type(struct crypto_tfm
*tfm
)
258 return tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
;
261 static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm
*tfm
)
263 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
264 return tfm
->__crt_alg
->cra_cipher
.cia_min_keysize
;
267 static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm
*tfm
)
269 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
270 return tfm
->__crt_alg
->cra_cipher
.cia_max_keysize
;
273 static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm
*tfm
)
275 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
276 return tfm
->crt_cipher
.cit_ivsize
;
279 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm
*tfm
)
281 return tfm
->__crt_alg
->cra_blocksize
;
284 static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm
*tfm
)
286 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
287 return tfm
->__crt_alg
->cra_digest
.dia_digestsize
;
290 static inline void *crypto_tfm_ctx(struct crypto_tfm
*tfm
)
292 return (void *)&tfm
[1];
298 static inline void crypto_digest_init(struct crypto_tfm
*tfm
)
300 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
301 tfm
->crt_digest
.dit_init(tfm
);
304 static inline void crypto_digest_update(struct crypto_tfm
*tfm
,
305 struct scatterlist
*sg
,
308 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
309 tfm
->crt_digest
.dit_update(tfm
, sg
, nsg
);
312 static inline void crypto_digest_final(struct crypto_tfm
*tfm
, u8
*out
)
314 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
315 tfm
->crt_digest
.dit_final(tfm
, out
);
318 static inline void crypto_digest_digest(struct crypto_tfm
*tfm
,
319 struct scatterlist
*sg
,
320 unsigned int nsg
, u8
*out
)
322 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
323 tfm
->crt_digest
.dit_digest(tfm
, sg
, nsg
, out
);
326 static inline int crypto_digest_setkey(struct crypto_tfm
*tfm
,
327 const u8
*key
, unsigned int keylen
)
329 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
330 if (tfm
->crt_digest
.dit_setkey
== NULL
)
332 return tfm
->crt_digest
.dit_setkey(tfm
, key
, keylen
);
335 static inline int crypto_cipher_setkey(struct crypto_tfm
*tfm
,
336 const u8
*key
, unsigned int keylen
)
338 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
339 return tfm
->crt_cipher
.cit_setkey(tfm
, key
, keylen
);
342 static inline int crypto_cipher_encrypt(struct crypto_tfm
*tfm
,
343 struct scatterlist
*dst
,
344 struct scatterlist
*src
,
347 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
348 return tfm
->crt_cipher
.cit_encrypt(tfm
, dst
, src
, nbytes
);
351 static inline int crypto_cipher_encrypt_iv(struct crypto_tfm
*tfm
,
352 struct scatterlist
*dst
,
353 struct scatterlist
*src
,
354 unsigned int nbytes
, u8
*iv
)
356 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
357 BUG_ON(tfm
->crt_cipher
.cit_mode
== CRYPTO_TFM_MODE_ECB
);
358 return tfm
->crt_cipher
.cit_encrypt_iv(tfm
, dst
, src
, nbytes
, iv
);
361 static inline int crypto_cipher_decrypt(struct crypto_tfm
*tfm
,
362 struct scatterlist
*dst
,
363 struct scatterlist
*src
,
366 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
367 return tfm
->crt_cipher
.cit_decrypt(tfm
, dst
, src
, nbytes
);
370 static inline int crypto_cipher_decrypt_iv(struct crypto_tfm
*tfm
,
371 struct scatterlist
*dst
,
372 struct scatterlist
*src
,
373 unsigned int nbytes
, u8
*iv
)
375 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
376 BUG_ON(tfm
->crt_cipher
.cit_mode
== CRYPTO_TFM_MODE_ECB
);
377 return tfm
->crt_cipher
.cit_decrypt_iv(tfm
, dst
, src
, nbytes
, iv
);
380 static inline void crypto_cipher_set_iv(struct crypto_tfm
*tfm
,
381 const u8
*src
, unsigned int len
)
383 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
384 memcpy(tfm
->crt_cipher
.cit_iv
, src
, len
);
387 static inline void crypto_cipher_get_iv(struct crypto_tfm
*tfm
,
388 u8
*dst
, unsigned int len
)
390 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
391 memcpy(dst
, tfm
->crt_cipher
.cit_iv
, len
);
394 static inline int crypto_comp_compress(struct crypto_tfm
*tfm
,
395 const u8
*src
, unsigned int slen
,
396 u8
*dst
, unsigned int *dlen
)
398 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_COMPRESS
);
399 return tfm
->crt_compress
.cot_compress(tfm
, src
, slen
, dst
, dlen
);
402 static inline int crypto_comp_decompress(struct crypto_tfm
*tfm
,
403 const u8
*src
, unsigned int slen
,
404 u8
*dst
, unsigned int *dlen
)
406 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_COMPRESS
);
407 return tfm
->crt_compress
.cot_decompress(tfm
, src
, slen
, dst
, dlen
);
413 #ifdef CONFIG_CRYPTO_HMAC
414 void crypto_hmac_init(struct crypto_tfm
*tfm
, u8
*key
, unsigned int *keylen
);
415 void crypto_hmac_update(struct crypto_tfm
*tfm
,
416 struct scatterlist
*sg
, unsigned int nsg
);
417 void crypto_hmac_final(struct crypto_tfm
*tfm
, u8
*key
,
418 unsigned int *keylen
, u8
*out
);
419 void crypto_hmac(struct crypto_tfm
*tfm
, u8
*key
, unsigned int *keylen
,
420 struct scatterlist
*sg
, unsigned int nsg
, u8
*out
);
421 #endif /* CONFIG_CRYPTO_HMAC */
423 #endif /* _LINUX_CRYPTO_H */
This page took 0.039906 seconds and 6 git commands to generate.