[CRYPTO] api: Add crypto_alg reference counting
[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 <asm/atomic.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/types.h>
24 #include <linux/list.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/uaccess.h>
28
29 /*
30  * Algorithm masks and types.
31  */
32 #define CRYPTO_ALG_TYPE_MASK            0x000000ff
33 #define CRYPTO_ALG_TYPE_CIPHER          0x00000001
34 #define CRYPTO_ALG_TYPE_DIGEST          0x00000002
35 #define CRYPTO_ALG_TYPE_COMPRESS        0x00000004
36
37 /*
38  * Transform masks and values (for crt_flags).
39  */
40 #define CRYPTO_TFM_MODE_MASK            0x000000ff
41 #define CRYPTO_TFM_REQ_MASK             0x000fff00
42 #define CRYPTO_TFM_RES_MASK             0xfff00000
43
44 #define CRYPTO_TFM_MODE_ECB             0x00000001
45 #define CRYPTO_TFM_MODE_CBC             0x00000002
46 #define CRYPTO_TFM_MODE_CFB             0x00000004
47 #define CRYPTO_TFM_MODE_CTR             0x00000008
48
49 #define CRYPTO_TFM_REQ_WEAK_KEY         0x00000100
50 #define CRYPTO_TFM_REQ_MAY_SLEEP        0x00000200
51 #define CRYPTO_TFM_RES_WEAK_KEY         0x00100000
52 #define CRYPTO_TFM_RES_BAD_KEY_LEN      0x00200000
53 #define CRYPTO_TFM_RES_BAD_KEY_SCHED    0x00400000
54 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN    0x00800000
55 #define CRYPTO_TFM_RES_BAD_FLAGS        0x01000000
56
57 /*
58  * Miscellaneous stuff.
59  */
60 #define CRYPTO_UNSPEC                   0
61 #define CRYPTO_MAX_ALG_NAME             64
62
63 #define CRYPTO_DIR_ENCRYPT              1
64 #define CRYPTO_DIR_DECRYPT              0
65
66 /*
67  * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
68  * declaration) is used to ensure that the crypto_tfm context structure is
69  * aligned correctly for the given architecture so that there are no alignment
70  * faults for C data types.  In particular, this is required on platforms such
71  * as arm where pointers are 32-bit aligned but there are data types such as
72  * u64 which require 64-bit alignment.
73  */
74 #if defined(ARCH_KMALLOC_MINALIGN)
75 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
76 #elif defined(ARCH_SLAB_MINALIGN)
77 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
78 #endif
79
80 #ifdef CRYPTO_MINALIGN
81 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
82 #else
83 #define CRYPTO_MINALIGN_ATTR
84 #endif
85
86 struct scatterlist;
87 struct crypto_tfm;
88
89 struct cipher_desc {
90         struct crypto_tfm *tfm;
91         void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
92         unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
93                              const u8 *src, unsigned int nbytes);
94         void *info;
95 };
96
97 /*
98  * Algorithms: modular crypto algorithm implementations, managed
99  * via crypto_register_alg() and crypto_unregister_alg().
100  */
101 struct cipher_alg {
102         unsigned int cia_min_keysize;
103         unsigned int cia_max_keysize;
104         int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
105                           unsigned int keylen, u32 *flags);
106         void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
107         void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
108
109         unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
110                                         u8 *dst, const u8 *src,
111                                         unsigned int nbytes);
112         unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
113                                         u8 *dst, const u8 *src,
114                                         unsigned int nbytes);
115         unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
116                                         u8 *dst, const u8 *src,
117                                         unsigned int nbytes);
118         unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
119                                         u8 *dst, const u8 *src,
120                                         unsigned int nbytes);
121 };
122
123 struct digest_alg {
124         unsigned int dia_digestsize;
125         void (*dia_init)(struct crypto_tfm *tfm);
126         void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
127                            unsigned int len);
128         void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
129         int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
130                           unsigned int keylen, u32 *flags);
131 };
132
133 struct compress_alg {
134         int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
135                             unsigned int slen, u8 *dst, unsigned int *dlen);
136         int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
137                               unsigned int slen, u8 *dst, unsigned int *dlen);
138 };
139
140 #define cra_cipher      cra_u.cipher
141 #define cra_digest      cra_u.digest
142 #define cra_compress    cra_u.compress
143
144 struct crypto_alg {
145         struct list_head cra_list;
146         u32 cra_flags;
147         unsigned int cra_blocksize;
148         unsigned int cra_ctxsize;
149         unsigned int cra_alignmask;
150
151         int cra_priority;
152         atomic_t cra_refcnt;
153
154         char cra_name[CRYPTO_MAX_ALG_NAME];
155         char cra_driver_name[CRYPTO_MAX_ALG_NAME];
156
157         union {
158                 struct cipher_alg cipher;
159                 struct digest_alg digest;
160                 struct compress_alg compress;
161         } cra_u;
162
163         int (*cra_init)(struct crypto_tfm *tfm);
164         void (*cra_exit)(struct crypto_tfm *tfm);
165         void (*cra_destroy)(struct crypto_alg *alg);
166         
167         struct module *cra_module;
168 };
169
170 /*
171  * Algorithm registration interface.
172  */
173 int crypto_register_alg(struct crypto_alg *alg);
174 int crypto_unregister_alg(struct crypto_alg *alg);
175
176 /*
177  * Algorithm query interface.
178  */
179 #ifdef CONFIG_CRYPTO
180 int crypto_alg_available(const char *name, u32 flags);
181 #else
182 static inline int crypto_alg_available(const char *name, u32 flags)
183 {
184         return 0;
185 }
186 #endif
187
188 /*
189  * Transforms: user-instantiated objects which encapsulate algorithms
190  * and core processing logic.  Managed via crypto_alloc_tfm() and
191  * crypto_free_tfm(), as well as the various helpers below.
192  */
193
194 struct cipher_tfm {
195         void *cit_iv;
196         unsigned int cit_ivsize;
197         u32 cit_mode;
198         int (*cit_setkey)(struct crypto_tfm *tfm,
199                           const u8 *key, unsigned int keylen);
200         int (*cit_encrypt)(struct crypto_tfm *tfm,
201                            struct scatterlist *dst,
202                            struct scatterlist *src,
203                            unsigned int nbytes);
204         int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
205                               struct scatterlist *dst,
206                               struct scatterlist *src,
207                               unsigned int nbytes, u8 *iv);
208         int (*cit_decrypt)(struct crypto_tfm *tfm,
209                            struct scatterlist *dst,
210                            struct scatterlist *src,
211                            unsigned int nbytes);
212         int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
213                            struct scatterlist *dst,
214                            struct scatterlist *src,
215                            unsigned int nbytes, u8 *iv);
216         void (*cit_xor_block)(u8 *dst, const u8 *src);
217 };
218
219 struct digest_tfm {
220         void (*dit_init)(struct crypto_tfm *tfm);
221         void (*dit_update)(struct crypto_tfm *tfm,
222                            struct scatterlist *sg, unsigned int nsg);
223         void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
224         void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
225                            unsigned int nsg, u8 *out);
226         int (*dit_setkey)(struct crypto_tfm *tfm,
227                           const u8 *key, unsigned int keylen);
228 #ifdef CONFIG_CRYPTO_HMAC
229         void *dit_hmac_block;
230 #endif
231 };
232
233 struct compress_tfm {
234         int (*cot_compress)(struct crypto_tfm *tfm,
235                             const u8 *src, unsigned int slen,
236                             u8 *dst, unsigned int *dlen);
237         int (*cot_decompress)(struct crypto_tfm *tfm,
238                               const u8 *src, unsigned int slen,
239                               u8 *dst, unsigned int *dlen);
240 };
241
242 #define crt_cipher      crt_u.cipher
243 #define crt_digest      crt_u.digest
244 #define crt_compress    crt_u.compress
245
246 struct crypto_tfm {
247
248         u32 crt_flags;
249         
250         union {
251                 struct cipher_tfm cipher;
252                 struct digest_tfm digest;
253                 struct compress_tfm compress;
254         } crt_u;
255         
256         struct crypto_alg *__crt_alg;
257
258         void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
259 };
260
261 /* 
262  * Transform user interface.
263  */
264  
265 /*
266  * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
267  * If that fails and the kernel supports dynamically loadable modules, it
268  * will then attempt to load a module of the same name or alias.  A refcount
269  * is grabbed on the algorithm which is then associated with the new transform.
270  *
271  * crypto_free_tfm() frees up the transform and any associated resources,
272  * then drops the refcount on the associated algorithm.
273  */
274 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
275 void crypto_free_tfm(struct crypto_tfm *tfm);
276
277 /*
278  * Transform helpers which query the underlying algorithm.
279  */
280 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
281 {
282         return tfm->__crt_alg->cra_name;
283 }
284
285 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
286 {
287         return module_name(tfm->__crt_alg->cra_module);
288 }
289
290 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
291 {
292         return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
293 }
294
295 static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
296 {
297         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
298         return tfm->__crt_alg->cra_cipher.cia_min_keysize;
299 }
300
301 static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
302 {
303         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
304         return tfm->__crt_alg->cra_cipher.cia_max_keysize;
305 }
306
307 static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
308 {
309         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
310         return tfm->crt_cipher.cit_ivsize;
311 }
312
313 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
314 {
315         return tfm->__crt_alg->cra_blocksize;
316 }
317
318 static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
319 {
320         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
321         return tfm->__crt_alg->cra_digest.dia_digestsize;
322 }
323
324 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
325 {
326         return tfm->__crt_alg->cra_alignmask;
327 }
328
329 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
330 {
331         return tfm->__crt_ctx;
332 }
333
334 static inline unsigned int crypto_tfm_ctx_alignment(void)
335 {
336         struct crypto_tfm *tfm;
337         return __alignof__(tfm->__crt_ctx);
338 }
339
340 /*
341  * API wrappers.
342  */
343 static inline void crypto_digest_init(struct crypto_tfm *tfm)
344 {
345         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
346         tfm->crt_digest.dit_init(tfm);
347 }
348
349 static inline void crypto_digest_update(struct crypto_tfm *tfm,
350                                         struct scatterlist *sg,
351                                         unsigned int nsg)
352 {
353         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
354         tfm->crt_digest.dit_update(tfm, sg, nsg);
355 }
356
357 static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
358 {
359         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
360         tfm->crt_digest.dit_final(tfm, out);
361 }
362
363 static inline void crypto_digest_digest(struct crypto_tfm *tfm,
364                                         struct scatterlist *sg,
365                                         unsigned int nsg, u8 *out)
366 {
367         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
368         tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
369 }
370
371 static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
372                                        const u8 *key, unsigned int keylen)
373 {
374         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
375         if (tfm->crt_digest.dit_setkey == NULL)
376                 return -ENOSYS;
377         return tfm->crt_digest.dit_setkey(tfm, key, keylen);
378 }
379
380 static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
381                                        const u8 *key, unsigned int keylen)
382 {
383         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
384         return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
385 }
386
387 static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
388                                         struct scatterlist *dst,
389                                         struct scatterlist *src,
390                                         unsigned int nbytes)
391 {
392         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
393         return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
394 }                                        
395
396 static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
397                                            struct scatterlist *dst,
398                                            struct scatterlist *src,
399                                            unsigned int nbytes, u8 *iv)
400 {
401         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
402         BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
403         return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
404 }                                        
405
406 static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
407                                         struct scatterlist *dst,
408                                         struct scatterlist *src,
409                                         unsigned int nbytes)
410 {
411         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
412         return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
413 }
414
415 static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
416                                            struct scatterlist *dst,
417                                            struct scatterlist *src,
418                                            unsigned int nbytes, u8 *iv)
419 {
420         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
421         BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
422         return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
423 }
424
425 static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
426                                         const u8 *src, unsigned int len)
427 {
428         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
429         memcpy(tfm->crt_cipher.cit_iv, src, len);
430 }
431
432 static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
433                                         u8 *dst, unsigned int len)
434 {
435         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
436         memcpy(dst, tfm->crt_cipher.cit_iv, len);
437 }
438
439 static inline int crypto_comp_compress(struct crypto_tfm *tfm,
440                                        const u8 *src, unsigned int slen,
441                                        u8 *dst, unsigned int *dlen)
442 {
443         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
444         return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
445 }
446
447 static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
448                                          const u8 *src, unsigned int slen,
449                                          u8 *dst, unsigned int *dlen)
450 {
451         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
452         return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
453 }
454
455 /*
456  * HMAC support.
457  */
458 #ifdef CONFIG_CRYPTO_HMAC
459 void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
460 void crypto_hmac_update(struct crypto_tfm *tfm,
461                         struct scatterlist *sg, unsigned int nsg);
462 void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
463                        unsigned int *keylen, u8 *out);
464 void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
465                  struct scatterlist *sg, unsigned int nsg, u8 *out);
466 #endif  /* CONFIG_CRYPTO_HMAC */
467
468 #endif  /* _LINUX_CRYPTO_H */
469