Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / include / linux / crypto.h
1 /*
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  * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
7  *
8  * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9  * and Nettle, by Niels Möller.
10  * 
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) 
14  * any later version.
15  *
16  */
17 #ifndef _LINUX_CRYPTO_H
18 #define _LINUX_CRYPTO_H
19
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_REQ_MAY_SLEEP        0x00000200
49 #define CRYPTO_TFM_RES_WEAK_KEY         0x00100000
50 #define CRYPTO_TFM_RES_BAD_KEY_LEN      0x00200000
51 #define CRYPTO_TFM_RES_BAD_KEY_SCHED    0x00400000
52 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN    0x00800000
53 #define CRYPTO_TFM_RES_BAD_FLAGS        0x01000000
54
55 /*
56  * Miscellaneous stuff.
57  */
58 #define CRYPTO_UNSPEC                   0
59 #define CRYPTO_MAX_ALG_NAME             64
60
61 #define CRYPTO_DIR_ENCRYPT              1
62 #define CRYPTO_DIR_DECRYPT              0
63
64 struct scatterlist;
65 struct crypto_tfm;
66
67 struct cipher_desc {
68         struct crypto_tfm *tfm;
69         void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
70         unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
71                              const u8 *src, unsigned int nbytes);
72         void *info;
73 };
74
75 /*
76  * Algorithms: modular crypto algorithm implementations, managed
77  * via crypto_register_alg() and crypto_unregister_alg().
78  */
79 struct cipher_alg {
80         unsigned int cia_min_keysize;
81         unsigned int cia_max_keysize;
82         int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
83                           unsigned int keylen, u32 *flags);
84         void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
85         void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
86
87         unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
88                                         u8 *dst, const u8 *src,
89                                         unsigned int nbytes);
90         unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
91                                         u8 *dst, const u8 *src,
92                                         unsigned int nbytes);
93         unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
94                                         u8 *dst, const u8 *src,
95                                         unsigned int nbytes);
96         unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
97                                         u8 *dst, const u8 *src,
98                                         unsigned int nbytes);
99 };
100
101 struct digest_alg {
102         unsigned int dia_digestsize;
103         void (*dia_init)(struct crypto_tfm *tfm);
104         void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
105                            unsigned int len);
106         void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
107         int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
108                           unsigned int keylen, u32 *flags);
109 };
110
111 struct compress_alg {
112         int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
113                             unsigned int slen, u8 *dst, unsigned int *dlen);
114         int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
115                               unsigned int slen, 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
122 struct crypto_alg {
123         struct list_head cra_list;
124         u32 cra_flags;
125         unsigned int cra_blocksize;
126         unsigned int cra_ctxsize;
127         unsigned int cra_alignmask;
128
129         int cra_priority;
130
131         char cra_name[CRYPTO_MAX_ALG_NAME];
132         char cra_driver_name[CRYPTO_MAX_ALG_NAME];
133
134         union {
135                 struct cipher_alg cipher;
136                 struct digest_alg digest;
137                 struct compress_alg compress;
138         } cra_u;
139
140         int (*cra_init)(struct crypto_tfm *tfm);
141         void (*cra_exit)(struct crypto_tfm *tfm);
142         
143         struct module *cra_module;
144 };
145
146 /*
147  * Algorithm registration interface.
148  */
149 int crypto_register_alg(struct crypto_alg *alg);
150 int crypto_unregister_alg(struct crypto_alg *alg);
151
152 /*
153  * Algorithm query interface.
154  */
155 #ifdef CONFIG_CRYPTO
156 int crypto_alg_available(const char *name, u32 flags);
157 #else
158 static inline int crypto_alg_available(const char *name, u32 flags)
159 {
160         return 0;
161 }
162 #endif
163
164 /*
165  * Transforms: user-instantiated objects which encapsulate algorithms
166  * and core processing logic.  Managed via crypto_alloc_tfm() and
167  * crypto_free_tfm(), as well as the various helpers below.
168  */
169
170 struct cipher_tfm {
171         void *cit_iv;
172         unsigned int cit_ivsize;
173         u32 cit_mode;
174         int (*cit_setkey)(struct crypto_tfm *tfm,
175                           const u8 *key, unsigned int keylen);
176         int (*cit_encrypt)(struct crypto_tfm *tfm,
177                            struct scatterlist *dst,
178                            struct scatterlist *src,
179                            unsigned int nbytes);
180         int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
181                               struct scatterlist *dst,
182                               struct scatterlist *src,
183                               unsigned int nbytes, u8 *iv);
184         int (*cit_decrypt)(struct crypto_tfm *tfm,
185                            struct scatterlist *dst,
186                            struct scatterlist *src,
187                            unsigned int nbytes);
188         int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
189                            struct scatterlist *dst,
190                            struct scatterlist *src,
191                            unsigned int nbytes, u8 *iv);
192         void (*cit_xor_block)(u8 *dst, const u8 *src);
193 };
194
195 struct digest_tfm {
196         void (*dit_init)(struct crypto_tfm *tfm);
197         void (*dit_update)(struct crypto_tfm *tfm,
198                            struct scatterlist *sg, unsigned int nsg);
199         void (*dit_update_kernel)(struct crypto_tfm *tfm,
200                                   const void *data, size_t count);
201         void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
202         void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
203                            unsigned int nsg, u8 *out);
204         int (*dit_setkey)(struct crypto_tfm *tfm,
205                           const u8 *key, unsigned int keylen);
206 #ifdef CONFIG_CRYPTO_HMAC
207         void *dit_hmac_block;
208 #endif
209 };
210
211 struct compress_tfm {
212         int (*cot_compress)(struct crypto_tfm *tfm,
213                             const u8 *src, unsigned int slen,
214                             u8 *dst, unsigned int *dlen);
215         int (*cot_decompress)(struct crypto_tfm *tfm,
216                               const u8 *src, unsigned int slen,
217                               u8 *dst, unsigned int *dlen);
218 };
219
220 #define crt_cipher      crt_u.cipher
221 #define crt_digest      crt_u.digest
222 #define crt_compress    crt_u.compress
223
224 struct crypto_tfm {
225
226         u32 crt_flags;
227         
228         union {
229                 struct cipher_tfm cipher;
230                 struct digest_tfm digest;
231                 struct compress_tfm compress;
232         } crt_u;
233         
234         struct crypto_alg *__crt_alg;
235
236         char __crt_ctx[] __attribute__ ((__aligned__));
237 };
238
239 /* 
240  * Transform user interface.
241  */
242  
243 /*
244  * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
245  * If that fails and the kernel supports dynamically loadable modules, it
246  * will then attempt to load a module of the same name or alias.  A refcount
247  * is grabbed on the algorithm which is then associated with the new transform.
248  *
249  * crypto_alloc_tfm2() is similar, but allows module loading to be suppressed.
250  *
251  * crypto_free_tfm() frees up the transform and any associated resources,
252  * then drops the refcount on the associated algorithm.
253  */
254 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
255 struct crypto_tfm *crypto_alloc_tfm2(const char *alg_name, u32 tfm_flags,
256                                      int nomodload);
257 void crypto_free_tfm(struct crypto_tfm *tfm);
258
259 /*
260  * Transform helpers which query the underlying algorithm.
261  */
262 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
263 {
264         return tfm->__crt_alg->cra_name;
265 }
266
267 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
268 {
269         return module_name(tfm->__crt_alg->cra_module);
270 }
271
272 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
273 {
274         return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
275 }
276
277 static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
278 {
279         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
280         return tfm->__crt_alg->cra_cipher.cia_min_keysize;
281 }
282
283 static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
284 {
285         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
286         return tfm->__crt_alg->cra_cipher.cia_max_keysize;
287 }
288
289 static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
290 {
291         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
292         return tfm->crt_cipher.cit_ivsize;
293 }
294
295 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
296 {
297         return tfm->__crt_alg->cra_blocksize;
298 }
299
300 static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
301 {
302         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
303         return tfm->__crt_alg->cra_digest.dia_digestsize;
304 }
305
306 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
307 {
308         return tfm->__crt_alg->cra_alignmask;
309 }
310
311 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
312 {
313         return tfm->__crt_ctx;
314 }
315
316 static inline unsigned int crypto_tfm_ctx_alignment(void)
317 {
318         struct crypto_tfm *tfm;
319         return __alignof__(tfm->__crt_ctx);
320 }
321
322 /*
323  * API wrappers.
324  */
325 static inline void crypto_digest_init(struct crypto_tfm *tfm)
326 {
327         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
328         tfm->crt_digest.dit_init(tfm);
329 }
330
331 static inline void crypto_digest_update(struct crypto_tfm *tfm,
332                                         struct scatterlist *sg,
333                                         unsigned int nsg)
334 {
335         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
336         tfm->crt_digest.dit_update(tfm, sg, nsg);
337 }
338
339 static inline void crypto_digest_update_kernel(struct crypto_tfm *tfm,
340                                                const void *data,
341                                                size_t count)
342 {
343         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
344         tfm->crt_digest.dit_update_kernel(tfm, data, count);
345 }
346
347 static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
348 {
349         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
350         tfm->crt_digest.dit_final(tfm, out);
351 }
352
353 static inline void crypto_digest_digest(struct crypto_tfm *tfm,
354                                         struct scatterlist *sg,
355                                         unsigned int nsg, u8 *out)
356 {
357         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
358         tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
359 }
360
361 static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
362                                        const u8 *key, unsigned int keylen)
363 {
364         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
365         if (tfm->crt_digest.dit_setkey == NULL)
366                 return -ENOSYS;
367         return tfm->crt_digest.dit_setkey(tfm, key, keylen);
368 }
369
370 static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
371                                        const u8 *key, unsigned int keylen)
372 {
373         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
374         return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
375 }
376
377 static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
378                                         struct scatterlist *dst,
379                                         struct scatterlist *src,
380                                         unsigned int nbytes)
381 {
382         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
383         return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
384 }                                        
385
386 static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
387                                            struct scatterlist *dst,
388                                            struct scatterlist *src,
389                                            unsigned int nbytes, u8 *iv)
390 {
391         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
392         BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
393         return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
394 }                                        
395
396 static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
397                                         struct scatterlist *dst,
398                                         struct scatterlist *src,
399                                         unsigned int nbytes)
400 {
401         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
402         return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
403 }
404
405 static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
406                                            struct scatterlist *dst,
407                                            struct scatterlist *src,
408                                            unsigned int nbytes, u8 *iv)
409 {
410         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
411         BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
412         return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
413 }
414
415 static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
416                                         const u8 *src, unsigned int len)
417 {
418         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
419         memcpy(tfm->crt_cipher.cit_iv, src, len);
420 }
421
422 static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
423                                         u8 *dst, unsigned int len)
424 {
425         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
426         memcpy(dst, tfm->crt_cipher.cit_iv, len);
427 }
428
429 static inline int crypto_comp_compress(struct crypto_tfm *tfm,
430                                        const u8 *src, unsigned int slen,
431                                        u8 *dst, unsigned int *dlen)
432 {
433         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
434         return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
435 }
436
437 static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
438                                          const u8 *src, unsigned int slen,
439                                          u8 *dst, unsigned int *dlen)
440 {
441         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
442         return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
443 }
444
445 /*
446  * HMAC support.
447  */
448 #ifdef CONFIG_CRYPTO_HMAC
449 void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
450 void crypto_hmac_update(struct crypto_tfm *tfm,
451                         struct scatterlist *sg, unsigned int nsg);
452 void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
453                        unsigned int *keylen, u8 *out);
454 void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
455                  struct scatterlist *sg, unsigned int nsg, u8 *out);
456 #endif  /* CONFIG_CRYPTO_HMAC */
457
458 #endif  /* _LINUX_CRYPTO_H */
459