blob: 5e2bcc636a02fd4d7a383e661b7661d6ac916169 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Scatterlist Cryptographic API.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6 *
7 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
8 * and Nettle, by Niels Möller.
9 *
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)
13 * any later version.
14 *
15 */
16#ifndef _LINUX_CRYPTO_H
17#define _LINUX_CRYPTO_H
18
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>
25#include <asm/page.h>
26
27/*
28 * Algorithm masks and types.
29 */
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
34
35/*
36 * Transform masks and values (for crt_flags).
37 */
38#define CRYPTO_TFM_MODE_MASK 0x000000ff
39#define CRYPTO_TFM_REQ_MASK 0x000fff00
40#define CRYPTO_TFM_RES_MASK 0xfff00000
41
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
46
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
53
54/*
55 * Miscellaneous stuff.
56 */
57#define CRYPTO_UNSPEC 0
58#define CRYPTO_MAX_ALG_NAME 64
59
60#define CRYPTO_DIR_ENCRYPT 1
61#define CRYPTO_DIR_DECRYPT 0
62
63struct scatterlist;
Herbert Xu40725182005-07-06 13:51:52 -070064struct crypto_tfm;
65
66struct cipher_desc {
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);
71 void *info;
72};
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * Algorithms: modular crypto algorithm implementations, managed
76 * via crypto_register_alg() and crypto_unregister_alg().
77 */
78struct cipher_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);
Herbert Xu40725182005-07-06 13:51:52 -070085
86 unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
87 u8 *dst, const u8 *src,
88 unsigned int nbytes);
89 unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
90 u8 *dst, const u8 *src,
91 unsigned int nbytes);
92 unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
93 u8 *dst, const u8 *src,
94 unsigned int nbytes);
95 unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
96 u8 *dst, const u8 *src,
97 unsigned int nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
100struct digest_alg {
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);
107};
108
109struct 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);
116};
117
118#define cra_cipher cra_u.cipher
119#define cra_digest cra_u.digest
120#define cra_compress cra_u.compress
121
122struct crypto_alg {
123 struct list_head cra_list;
124 u32 cra_flags;
125 unsigned int cra_blocksize;
126 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700127 unsigned int cra_alignmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 const char cra_name[CRYPTO_MAX_ALG_NAME];
129
130 union {
131 struct cipher_alg cipher;
132 struct digest_alg digest;
133 struct compress_alg compress;
134 } cra_u;
135
136 struct module *cra_module;
137};
138
139/*
140 * Algorithm registration interface.
141 */
142int crypto_register_alg(struct crypto_alg *alg);
143int crypto_unregister_alg(struct crypto_alg *alg);
144
145/*
146 * Algorithm query interface.
147 */
148#ifdef CONFIG_CRYPTO
149int crypto_alg_available(const char *name, u32 flags);
150#else
151static inline int crypto_alg_available(const char *name, u32 flags)
152{
153 return 0;
154}
155#endif
156
157/*
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.
161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163struct cipher_tfm {
164 void *cit_iv;
165 unsigned int cit_ivsize;
166 u32 cit_mode;
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);
186};
187
188struct digest_tfm {
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;
199#endif
200};
201
202struct 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);
209};
210
211#define crt_cipher crt_u.cipher
212#define crt_digest crt_u.digest
213#define crt_compress crt_u.compress
214
215struct crypto_tfm {
216
217 u32 crt_flags;
218
219 union {
220 struct cipher_tfm cipher;
221 struct digest_tfm digest;
222 struct compress_tfm compress;
223 } crt_u;
224
225 struct crypto_alg *__crt_alg;
226};
227
228/*
229 * Transform user interface.
230 */
231
232/*
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.
237 *
238 * crypto_free_tfm() frees up the transform and any associated resources,
239 * then drops the refcount on the associated algorithm.
240 */
241struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
242void crypto_free_tfm(struct crypto_tfm *tfm);
243
244/*
245 * Transform helpers which query the underlying algorithm.
246 */
247static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
248{
249 return tfm->__crt_alg->cra_name;
250}
251
252static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
253{
254 return module_name(tfm->__crt_alg->cra_module);
255}
256
257static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
258{
259 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
260}
261
262static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
263{
264 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
265 return tfm->__crt_alg->cra_cipher.cia_min_keysize;
266}
267
268static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
269{
270 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
271 return tfm->__crt_alg->cra_cipher.cia_max_keysize;
272}
273
274static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
275{
276 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
277 return tfm->crt_cipher.cit_ivsize;
278}
279
280static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
281{
282 return tfm->__crt_alg->cra_blocksize;
283}
284
285static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
286{
287 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
288 return tfm->__crt_alg->cra_digest.dia_digestsize;
289}
290
Herbert Xufbdae9f2005-07-06 13:53:29 -0700291static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
292{
293 return tfm->__crt_alg->cra_alignmask;
294}
295
Herbert Xu40725182005-07-06 13:51:52 -0700296static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
297{
298 return (void *)&tfm[1];
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/*
302 * API wrappers.
303 */
304static inline void crypto_digest_init(struct crypto_tfm *tfm)
305{
306 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
307 tfm->crt_digest.dit_init(tfm);
308}
309
310static inline void crypto_digest_update(struct crypto_tfm *tfm,
311 struct scatterlist *sg,
312 unsigned int nsg)
313{
314 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
315 tfm->crt_digest.dit_update(tfm, sg, nsg);
316}
317
318static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
319{
320 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
321 tfm->crt_digest.dit_final(tfm, out);
322}
323
324static inline void crypto_digest_digest(struct crypto_tfm *tfm,
325 struct scatterlist *sg,
326 unsigned int nsg, u8 *out)
327{
328 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
329 tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
330}
331
332static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
333 const u8 *key, unsigned int keylen)
334{
335 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
336 if (tfm->crt_digest.dit_setkey == NULL)
337 return -ENOSYS;
338 return tfm->crt_digest.dit_setkey(tfm, key, keylen);
339}
340
341static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
342 const u8 *key, unsigned int keylen)
343{
344 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
345 return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
346}
347
348static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
349 struct scatterlist *dst,
350 struct scatterlist *src,
351 unsigned int nbytes)
352{
353 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
354 return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
355}
356
357static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
358 struct scatterlist *dst,
359 struct scatterlist *src,
360 unsigned int nbytes, u8 *iv)
361{
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);
365}
366
367static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
368 struct scatterlist *dst,
369 struct scatterlist *src,
370 unsigned int nbytes)
371{
372 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
373 return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
374}
375
376static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
377 struct scatterlist *dst,
378 struct scatterlist *src,
379 unsigned int nbytes, u8 *iv)
380{
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);
384}
385
386static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
387 const u8 *src, unsigned int len)
388{
389 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
390 memcpy(tfm->crt_cipher.cit_iv, src, len);
391}
392
393static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
394 u8 *dst, unsigned int len)
395{
396 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
397 memcpy(dst, tfm->crt_cipher.cit_iv, len);
398}
399
400static inline int crypto_comp_compress(struct crypto_tfm *tfm,
401 const u8 *src, unsigned int slen,
402 u8 *dst, unsigned int *dlen)
403{
404 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
405 return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
406}
407
408static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
409 const u8 *src, unsigned int slen,
410 u8 *dst, unsigned int *dlen)
411{
412 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
413 return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
414}
415
416/*
417 * HMAC support.
418 */
419#ifdef CONFIG_CRYPTO_HMAC
420void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
421void crypto_hmac_update(struct crypto_tfm *tfm,
422 struct scatterlist *sg, unsigned int nsg);
423void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
424 unsigned int *keylen, u8 *out);
425void 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 */
428
429#endif /* _LINUX_CRYPTO_H */
430