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 unsigned int cra_alignmask
;
128 const char cra_name
[CRYPTO_MAX_ALG_NAME
];
131 struct cipher_alg cipher
;
132 struct digest_alg digest
;
133 struct compress_alg compress
;
136 struct module
*cra_module
;
140 * Algorithm registration interface.
142 int crypto_register_alg(struct crypto_alg
*alg
);
143 int crypto_unregister_alg(struct crypto_alg
*alg
);
146 * Algorithm query interface.
149 int crypto_alg_available(const char *name
, u32 flags
);
151 static inline int crypto_alg_available(const char *name
, u32 flags
)
158 * Transforms: user-instantiated objects which encapsulate algorithms
159 * and core processing logic. Managed via crypto_alloc_tfm() and
160 * crypto_free_tfm(), as well as the various helpers below.
165 unsigned int cit_ivsize
;
167 int (*cit_setkey
)(struct crypto_tfm
*tfm
,
168 const u8
*key
, unsigned int keylen
);
169 int (*cit_encrypt
)(struct crypto_tfm
*tfm
,
170 struct scatterlist
*dst
,
171 struct scatterlist
*src
,
172 unsigned int nbytes
);
173 int (*cit_encrypt_iv
)(struct crypto_tfm
*tfm
,
174 struct scatterlist
*dst
,
175 struct scatterlist
*src
,
176 unsigned int nbytes
, u8
*iv
);
177 int (*cit_decrypt
)(struct crypto_tfm
*tfm
,
178 struct scatterlist
*dst
,
179 struct scatterlist
*src
,
180 unsigned int nbytes
);
181 int (*cit_decrypt_iv
)(struct crypto_tfm
*tfm
,
182 struct scatterlist
*dst
,
183 struct scatterlist
*src
,
184 unsigned int nbytes
, u8
*iv
);
185 void (*cit_xor_block
)(u8
*dst
, const u8
*src
);
189 void (*dit_init
)(struct crypto_tfm
*tfm
);
190 void (*dit_update
)(struct crypto_tfm
*tfm
,
191 struct scatterlist
*sg
, unsigned int nsg
);
192 void (*dit_final
)(struct crypto_tfm
*tfm
, u8
*out
);
193 void (*dit_digest
)(struct crypto_tfm
*tfm
, struct scatterlist
*sg
,
194 unsigned int nsg
, u8
*out
);
195 int (*dit_setkey
)(struct crypto_tfm
*tfm
,
196 const u8
*key
, unsigned int keylen
);
197 #ifdef CONFIG_CRYPTO_HMAC
198 void *dit_hmac_block
;
202 struct compress_tfm
{
203 int (*cot_compress
)(struct crypto_tfm
*tfm
,
204 const u8
*src
, unsigned int slen
,
205 u8
*dst
, unsigned int *dlen
);
206 int (*cot_decompress
)(struct crypto_tfm
*tfm
,
207 const u8
*src
, unsigned int slen
,
208 u8
*dst
, unsigned int *dlen
);
211 #define crt_cipher crt_u.cipher
212 #define crt_digest crt_u.digest
213 #define crt_compress crt_u.compress
220 struct cipher_tfm cipher
;
221 struct digest_tfm digest
;
222 struct compress_tfm compress
;
225 struct crypto_alg
*__crt_alg
;
229 * Transform user interface.
233 * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
234 * If that fails and the kernel supports dynamically loadable modules, it
235 * will then attempt to load a module of the same name or alias. A refcount
236 * is grabbed on the algorithm which is then associated with the new transform.
238 * crypto_free_tfm() frees up the transform and any associated resources,
239 * then drops the refcount on the associated algorithm.
241 struct crypto_tfm
*crypto_alloc_tfm(const char *alg_name
, u32 tfm_flags
);
242 void crypto_free_tfm(struct crypto_tfm
*tfm
);
245 * Transform helpers which query the underlying algorithm.
247 static inline const char *crypto_tfm_alg_name(struct crypto_tfm
*tfm
)
249 return tfm
->__crt_alg
->cra_name
;
252 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm
*tfm
)
254 return module_name(tfm
->__crt_alg
->cra_module
);
257 static inline u32
crypto_tfm_alg_type(struct crypto_tfm
*tfm
)
259 return tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
;
262 static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm
*tfm
)
264 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
265 return tfm
->__crt_alg
->cra_cipher
.cia_min_keysize
;
268 static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm
*tfm
)
270 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
271 return tfm
->__crt_alg
->cra_cipher
.cia_max_keysize
;
274 static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm
*tfm
)
276 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
277 return tfm
->crt_cipher
.cit_ivsize
;
280 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm
*tfm
)
282 return tfm
->__crt_alg
->cra_blocksize
;
285 static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm
*tfm
)
287 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
288 return tfm
->__crt_alg
->cra_digest
.dia_digestsize
;
291 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm
*tfm
)
293 return tfm
->__crt_alg
->cra_alignmask
;
296 static inline void *crypto_tfm_ctx(struct crypto_tfm
*tfm
)
298 return (void *)&tfm
[1];
304 static inline void crypto_digest_init(struct crypto_tfm
*tfm
)
306 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
307 tfm
->crt_digest
.dit_init(tfm
);
310 static inline void crypto_digest_update(struct crypto_tfm
*tfm
,
311 struct scatterlist
*sg
,
314 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
315 tfm
->crt_digest
.dit_update(tfm
, sg
, nsg
);
318 static inline void crypto_digest_final(struct crypto_tfm
*tfm
, u8
*out
)
320 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
321 tfm
->crt_digest
.dit_final(tfm
, out
);
324 static inline void crypto_digest_digest(struct crypto_tfm
*tfm
,
325 struct scatterlist
*sg
,
326 unsigned int nsg
, u8
*out
)
328 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
329 tfm
->crt_digest
.dit_digest(tfm
, sg
, nsg
, out
);
332 static inline int crypto_digest_setkey(struct crypto_tfm
*tfm
,
333 const u8
*key
, unsigned int keylen
)
335 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_DIGEST
);
336 if (tfm
->crt_digest
.dit_setkey
== NULL
)
338 return tfm
->crt_digest
.dit_setkey(tfm
, key
, keylen
);
341 static inline int crypto_cipher_setkey(struct crypto_tfm
*tfm
,
342 const u8
*key
, unsigned int keylen
)
344 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
345 return tfm
->crt_cipher
.cit_setkey(tfm
, key
, keylen
);
348 static inline int crypto_cipher_encrypt(struct crypto_tfm
*tfm
,
349 struct scatterlist
*dst
,
350 struct scatterlist
*src
,
353 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
354 return tfm
->crt_cipher
.cit_encrypt(tfm
, dst
, src
, nbytes
);
357 static inline int crypto_cipher_encrypt_iv(struct crypto_tfm
*tfm
,
358 struct scatterlist
*dst
,
359 struct scatterlist
*src
,
360 unsigned int nbytes
, u8
*iv
)
362 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
363 BUG_ON(tfm
->crt_cipher
.cit_mode
== CRYPTO_TFM_MODE_ECB
);
364 return tfm
->crt_cipher
.cit_encrypt_iv(tfm
, dst
, src
, nbytes
, iv
);
367 static inline int crypto_cipher_decrypt(struct crypto_tfm
*tfm
,
368 struct scatterlist
*dst
,
369 struct scatterlist
*src
,
372 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
373 return tfm
->crt_cipher
.cit_decrypt(tfm
, dst
, src
, nbytes
);
376 static inline int crypto_cipher_decrypt_iv(struct crypto_tfm
*tfm
,
377 struct scatterlist
*dst
,
378 struct scatterlist
*src
,
379 unsigned int nbytes
, u8
*iv
)
381 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
382 BUG_ON(tfm
->crt_cipher
.cit_mode
== CRYPTO_TFM_MODE_ECB
);
383 return tfm
->crt_cipher
.cit_decrypt_iv(tfm
, dst
, src
, nbytes
, iv
);
386 static inline void crypto_cipher_set_iv(struct crypto_tfm
*tfm
,
387 const u8
*src
, unsigned int len
)
389 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
390 memcpy(tfm
->crt_cipher
.cit_iv
, src
, len
);
393 static inline void crypto_cipher_get_iv(struct crypto_tfm
*tfm
,
394 u8
*dst
, unsigned int len
)
396 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
397 memcpy(dst
, tfm
->crt_cipher
.cit_iv
, len
);
400 static inline int crypto_comp_compress(struct crypto_tfm
*tfm
,
401 const u8
*src
, unsigned int slen
,
402 u8
*dst
, unsigned int *dlen
)
404 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_COMPRESS
);
405 return tfm
->crt_compress
.cot_compress(tfm
, src
, slen
, dst
, dlen
);
408 static inline int crypto_comp_decompress(struct crypto_tfm
*tfm
,
409 const u8
*src
, unsigned int slen
,
410 u8
*dst
, unsigned int *dlen
)
412 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_COMPRESS
);
413 return tfm
->crt_compress
.cot_decompress(tfm
, src
, slen
, dst
, dlen
);
419 #ifdef CONFIG_CRYPTO_HMAC
420 void crypto_hmac_init(struct crypto_tfm
*tfm
, u8
*key
, unsigned int *keylen
);
421 void crypto_hmac_update(struct crypto_tfm
*tfm
,
422 struct scatterlist
*sg
, unsigned int nsg
);
423 void crypto_hmac_final(struct crypto_tfm
*tfm
, u8
*key
,
424 unsigned int *keylen
, u8
*out
);
425 void crypto_hmac(struct crypto_tfm
*tfm
, u8
*key
, unsigned int *keylen
,
426 struct scatterlist
*sg
, unsigned int nsg
, u8
*out
);
427 #endif /* CONFIG_CRYPTO_HMAC */
429 #endif /* _LINUX_CRYPTO_H */