upgrade to fedora-2.6.12-1.1398.FC4 + vserver 2.0.rc7
[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  *
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
63 struct scatterlist;
64
65 /*
66  * Algorithms: modular crypto algorithm implementations, managed
67  * via crypto_register_alg() and crypto_unregister_alg().
68  */
69 struct cipher_alg {
70         unsigned int cia_min_keysize;
71         unsigned int cia_max_keysize;
72         int (*cia_setkey)(void *ctx, const u8 *key,
73                           unsigned int keylen, u32 *flags);
74         void (*cia_encrypt)(void *ctx, u8 *dst, const u8 *src);
75         void (*cia_decrypt)(void *ctx, u8 *dst, const u8 *src);
76 };
77
78 struct digest_alg {
79         unsigned int dia_digestsize;
80         void (*dia_init)(void *ctx);
81         void (*dia_update)(void *ctx, const u8 *data, unsigned int len);
82         void (*dia_final)(void *ctx, u8 *out);
83         int (*dia_setkey)(void *ctx, const u8 *key,
84                           unsigned int keylen, u32 *flags);
85 };
86
87 struct compress_alg {
88         int (*coa_init)(void *ctx);
89         void (*coa_exit)(void *ctx);
90         int (*coa_compress)(void *ctx, const u8 *src, unsigned int slen,
91                             u8 *dst, unsigned int *dlen);
92         int (*coa_decompress)(void *ctx, const u8 *src, unsigned int slen,
93                               u8 *dst, unsigned int *dlen);
94 };
95
96 #define cra_cipher      cra_u.cipher
97 #define cra_digest      cra_u.digest
98 #define cra_compress    cra_u.compress
99
100 struct crypto_alg {
101         struct list_head cra_list;
102         u32 cra_flags;
103         unsigned int cra_blocksize;
104         unsigned int cra_ctxsize;
105         const char cra_name[CRYPTO_MAX_ALG_NAME];
106
107         union {
108                 struct cipher_alg cipher;
109                 struct digest_alg digest;
110                 struct compress_alg compress;
111         } cra_u;
112         
113         struct module *cra_module;
114 };
115
116 /*
117  * Algorithm registration interface.
118  */
119 int crypto_register_alg(struct crypto_alg *alg);
120 int crypto_unregister_alg(struct crypto_alg *alg);
121
122 /*
123  * Algorithm query interface.
124  */
125 #ifdef CONFIG_CRYPTO
126 int crypto_alg_available(const char *name, u32 flags);
127 #else
128 static inline int crypto_alg_available(const char *name, u32 flags)
129 {
130         return 0;
131 }
132 #endif
133
134 /*
135  * Transforms: user-instantiated objects which encapsulate algorithms
136  * and core processing logic.  Managed via crypto_alloc_tfm() and
137  * crypto_free_tfm(), as well as the various helpers below.
138  */
139 struct crypto_tfm;
140
141 struct cipher_tfm {
142         void *cit_iv;
143         unsigned int cit_ivsize;
144         u32 cit_mode;
145         int (*cit_setkey)(struct crypto_tfm *tfm,
146                           const u8 *key, unsigned int keylen);
147         int (*cit_encrypt)(struct crypto_tfm *tfm,
148                            struct scatterlist *dst,
149                            struct scatterlist *src,
150                            unsigned int nbytes);
151         int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
152                               struct scatterlist *dst,
153                               struct scatterlist *src,
154                               unsigned int nbytes, u8 *iv);
155         int (*cit_decrypt)(struct crypto_tfm *tfm,
156                            struct scatterlist *dst,
157                            struct scatterlist *src,
158                            unsigned int nbytes);
159         int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
160                            struct scatterlist *dst,
161                            struct scatterlist *src,
162                            unsigned int nbytes, u8 *iv);
163         void (*cit_xor_block)(u8 *dst, const u8 *src);
164 };
165
166 struct digest_tfm {
167         void (*dit_init)(struct crypto_tfm *tfm);
168         void (*dit_update)(struct crypto_tfm *tfm,
169                            struct scatterlist *sg, unsigned int nsg);
170         void (*dit_update_kernel)(struct crypto_tfm *tfm,
171                                   const void *data, size_t count);
172         void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
173         void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
174                            unsigned int nsg, u8 *out);
175         int (*dit_setkey)(struct crypto_tfm *tfm,
176                           const u8 *key, unsigned int keylen);
177 #ifdef CONFIG_CRYPTO_HMAC
178         void *dit_hmac_block;
179 #endif
180 };
181
182 struct compress_tfm {
183         int (*cot_compress)(struct crypto_tfm *tfm,
184                             const u8 *src, unsigned int slen,
185                             u8 *dst, unsigned int *dlen);
186         int (*cot_decompress)(struct crypto_tfm *tfm,
187                               const u8 *src, unsigned int slen,
188                               u8 *dst, unsigned int *dlen);
189 };
190
191 #define crt_cipher      crt_u.cipher
192 #define crt_digest      crt_u.digest
193 #define crt_compress    crt_u.compress
194
195 struct crypto_tfm {
196
197         u32 crt_flags;
198         
199         union {
200                 struct cipher_tfm cipher;
201                 struct digest_tfm digest;
202                 struct compress_tfm compress;
203         } crt_u;
204         
205         struct crypto_alg *__crt_alg;
206 };
207
208 /* 
209  * Transform user interface.
210  */
211  
212 /*
213  * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
214  * If that fails and the kernel supports dynamically loadable modules, it
215  * will then attempt to load a module of the same name or alias.  A refcount
216  * is grabbed on the algorithm which is then associated with the new transform.
217  *
218  * crypto_alloc_tfm2() is similar, but allows module loading to be suppressed.
219  *
220  * crypto_free_tfm() frees up the transform and any associated resources,
221  * then drops the refcount on the associated algorithm.
222  */
223 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
224 struct crypto_tfm *crypto_alloc_tfm2(const char *alg_name, u32 tfm_flags,
225                                      int nomodload);
226 void crypto_free_tfm(struct crypto_tfm *tfm);
227
228 /*
229  * Transform helpers which query the underlying algorithm.
230  */
231 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
232 {
233         return tfm->__crt_alg->cra_name;
234 }
235
236 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
237 {
238         return module_name(tfm->__crt_alg->cra_module);
239 }
240
241 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
242 {
243         return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
244 }
245
246 static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
247 {
248         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
249         return tfm->__crt_alg->cra_cipher.cia_min_keysize;
250 }
251
252 static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
253 {
254         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
255         return tfm->__crt_alg->cra_cipher.cia_max_keysize;
256 }
257
258 static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
259 {
260         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
261         return tfm->crt_cipher.cit_ivsize;
262 }
263
264 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
265 {
266         return tfm->__crt_alg->cra_blocksize;
267 }
268
269 static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
270 {
271         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
272         return tfm->__crt_alg->cra_digest.dia_digestsize;
273 }
274
275 /*
276  * API wrappers.
277  */
278 static inline void crypto_digest_init(struct crypto_tfm *tfm)
279 {
280         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
281         tfm->crt_digest.dit_init(tfm);
282 }
283
284 static inline void crypto_digest_update(struct crypto_tfm *tfm,
285                                         struct scatterlist *sg,
286                                         unsigned int nsg)
287 {
288         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
289         tfm->crt_digest.dit_update(tfm, sg, nsg);
290 }
291
292 static inline void crypto_digest_update_kernel(struct crypto_tfm *tfm,
293                                                const void *data,
294                                                size_t count)
295 {
296         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
297         tfm->crt_digest.dit_update_kernel(tfm, data, count);
298 }
299
300 static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
301 {
302         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
303         tfm->crt_digest.dit_final(tfm, out);
304 }
305
306 static inline void crypto_digest_digest(struct crypto_tfm *tfm,
307                                         struct scatterlist *sg,
308                                         unsigned int nsg, u8 *out)
309 {
310         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
311         tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
312 }
313
314 static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
315                                        const u8 *key, unsigned int keylen)
316 {
317         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
318         if (tfm->crt_digest.dit_setkey == NULL)
319                 return -ENOSYS;
320         return tfm->crt_digest.dit_setkey(tfm, key, keylen);
321 }
322
323 static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
324                                        const u8 *key, unsigned int keylen)
325 {
326         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
327         return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
328 }
329
330 static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
331                                         struct scatterlist *dst,
332                                         struct scatterlist *src,
333                                         unsigned int nbytes)
334 {
335         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
336         return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
337 }                                        
338
339 static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
340                                            struct scatterlist *dst,
341                                            struct scatterlist *src,
342                                            unsigned int nbytes, u8 *iv)
343 {
344         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
345         BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
346         return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
347 }                                        
348
349 static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
350                                         struct scatterlist *dst,
351                                         struct scatterlist *src,
352                                         unsigned int nbytes)
353 {
354         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
355         return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
356 }
357
358 static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
359                                            struct scatterlist *dst,
360                                            struct scatterlist *src,
361                                            unsigned int nbytes, u8 *iv)
362 {
363         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
364         BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
365         return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
366 }
367
368 static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
369                                         const u8 *src, unsigned int len)
370 {
371         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
372         memcpy(tfm->crt_cipher.cit_iv, src, len);
373 }
374
375 static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
376                                         u8 *dst, unsigned int len)
377 {
378         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
379         memcpy(dst, tfm->crt_cipher.cit_iv, len);
380 }
381
382 static inline int crypto_comp_compress(struct crypto_tfm *tfm,
383                                        const u8 *src, unsigned int slen,
384                                        u8 *dst, unsigned int *dlen)
385 {
386         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
387         return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
388 }
389
390 static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
391                                          const u8 *src, unsigned int slen,
392                                          u8 *dst, unsigned int *dlen)
393 {
394         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
395         return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
396 }
397
398 /*
399  * HMAC support.
400  */
401 #ifdef CONFIG_CRYPTO_HMAC
402 void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
403 void crypto_hmac_update(struct crypto_tfm *tfm,
404                         struct scatterlist *sg, unsigned int nsg);
405 void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
406                        unsigned int *keylen, u8 *out);
407 void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
408                  struct scatterlist *sg, unsigned int nsg, u8 *out);
409 #endif  /* CONFIG_CRYPTO_HMAC */
410
411 #endif  /* _LINUX_CRYPTO_H */
412