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