]> err.no Git - linux-2.6/blob - include/linux/crypto.h
[PATCH] Revert bd_mount_mutex back to a semaphore
[linux-2.6] / 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/list.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
27
28 /*
29  * Algorithm masks and types.
30  */
31 #define CRYPTO_ALG_TYPE_MASK            0x0000000f
32 #define CRYPTO_ALG_TYPE_CIPHER          0x00000001
33 #define CRYPTO_ALG_TYPE_DIGEST          0x00000002
34 #define CRYPTO_ALG_TYPE_HASH            0x00000003
35 #define CRYPTO_ALG_TYPE_BLKCIPHER       0x00000004
36 #define CRYPTO_ALG_TYPE_COMPRESS        0x00000005
37
38 #define CRYPTO_ALG_TYPE_HASH_MASK       0x0000000e
39
40 #define CRYPTO_ALG_LARVAL               0x00000010
41 #define CRYPTO_ALG_DEAD                 0x00000020
42 #define CRYPTO_ALG_DYING                0x00000040
43 #define CRYPTO_ALG_ASYNC                0x00000080
44
45 /*
46  * Set this bit if and only if the algorithm requires another algorithm of
47  * the same type to handle corner cases.
48  */
49 #define CRYPTO_ALG_NEED_FALLBACK        0x00000100
50
51 /*
52  * Transform masks and values (for crt_flags).
53  */
54 #define CRYPTO_TFM_MODE_MASK            0x000000ff
55 #define CRYPTO_TFM_REQ_MASK             0x000fff00
56 #define CRYPTO_TFM_RES_MASK             0xfff00000
57
58 #define CRYPTO_TFM_MODE_ECB             0x00000001
59 #define CRYPTO_TFM_MODE_CBC             0x00000002
60 #define CRYPTO_TFM_MODE_CFB             0x00000004
61 #define CRYPTO_TFM_MODE_CTR             0x00000008
62
63 #define CRYPTO_TFM_REQ_WEAK_KEY         0x00000100
64 #define CRYPTO_TFM_REQ_MAY_SLEEP        0x00000200
65 #define CRYPTO_TFM_RES_WEAK_KEY         0x00100000
66 #define CRYPTO_TFM_RES_BAD_KEY_LEN      0x00200000
67 #define CRYPTO_TFM_RES_BAD_KEY_SCHED    0x00400000
68 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN    0x00800000
69 #define CRYPTO_TFM_RES_BAD_FLAGS        0x01000000
70
71 /*
72  * Miscellaneous stuff.
73  */
74 #define CRYPTO_UNSPEC                   0
75 #define CRYPTO_MAX_ALG_NAME             64
76
77 #define CRYPTO_DIR_ENCRYPT              1
78 #define CRYPTO_DIR_DECRYPT              0
79
80 /*
81  * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
82  * declaration) is used to ensure that the crypto_tfm context structure is
83  * aligned correctly for the given architecture so that there are no alignment
84  * faults for C data types.  In particular, this is required on platforms such
85  * as arm where pointers are 32-bit aligned but there are data types such as
86  * u64 which require 64-bit alignment.
87  */
88 #if defined(ARCH_KMALLOC_MINALIGN)
89 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
90 #elif defined(ARCH_SLAB_MINALIGN)
91 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
92 #endif
93
94 #ifdef CRYPTO_MINALIGN
95 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
96 #else
97 #define CRYPTO_MINALIGN_ATTR
98 #endif
99
100 struct scatterlist;
101 struct crypto_blkcipher;
102 struct crypto_hash;
103 struct crypto_tfm;
104 struct crypto_type;
105
106 struct blkcipher_desc {
107         struct crypto_blkcipher *tfm;
108         void *info;
109         u32 flags;
110 };
111
112 struct cipher_desc {
113         struct crypto_tfm *tfm;
114         void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
115         unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
116                              const u8 *src, unsigned int nbytes);
117         void *info;
118 };
119
120 struct hash_desc {
121         struct crypto_hash *tfm;
122         u32 flags;
123 };
124
125 /*
126  * Algorithms: modular crypto algorithm implementations, managed
127  * via crypto_register_alg() and crypto_unregister_alg().
128  */
129 struct blkcipher_alg {
130         int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
131                       unsigned int keylen);
132         int (*encrypt)(struct blkcipher_desc *desc,
133                        struct scatterlist *dst, struct scatterlist *src,
134                        unsigned int nbytes);
135         int (*decrypt)(struct blkcipher_desc *desc,
136                        struct scatterlist *dst, struct scatterlist *src,
137                        unsigned int nbytes);
138
139         unsigned int min_keysize;
140         unsigned int max_keysize;
141         unsigned int ivsize;
142 };
143
144 struct cipher_alg {
145         unsigned int cia_min_keysize;
146         unsigned int cia_max_keysize;
147         int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
148                           unsigned int keylen);
149         void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
150         void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
151
152         unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
153                                         u8 *dst, const u8 *src,
154                                         unsigned int nbytes) __deprecated;
155         unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
156                                         u8 *dst, const u8 *src,
157                                         unsigned int nbytes) __deprecated;
158         unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
159                                         u8 *dst, const u8 *src,
160                                         unsigned int nbytes) __deprecated;
161         unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
162                                         u8 *dst, const u8 *src,
163                                         unsigned int nbytes) __deprecated;
164 };
165
166 struct digest_alg {
167         unsigned int dia_digestsize;
168         void (*dia_init)(struct crypto_tfm *tfm);
169         void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
170                            unsigned int len);
171         void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
172         int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
173                           unsigned int keylen);
174 };
175
176 struct hash_alg {
177         int (*init)(struct hash_desc *desc);
178         int (*update)(struct hash_desc *desc, struct scatterlist *sg,
179                       unsigned int nbytes);
180         int (*final)(struct hash_desc *desc, u8 *out);
181         int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
182                       unsigned int nbytes, u8 *out);
183         int (*setkey)(struct crypto_hash *tfm, const u8 *key,
184                       unsigned int keylen);
185
186         unsigned int digestsize;
187 };
188
189 struct compress_alg {
190         int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
191                             unsigned int slen, u8 *dst, unsigned int *dlen);
192         int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
193                               unsigned int slen, u8 *dst, unsigned int *dlen);
194 };
195
196 #define cra_blkcipher   cra_u.blkcipher
197 #define cra_cipher      cra_u.cipher
198 #define cra_digest      cra_u.digest
199 #define cra_hash        cra_u.hash
200 #define cra_compress    cra_u.compress
201
202 struct crypto_alg {
203         struct list_head cra_list;
204         struct list_head cra_users;
205
206         u32 cra_flags;
207         unsigned int cra_blocksize;
208         unsigned int cra_ctxsize;
209         unsigned int cra_alignmask;
210
211         int cra_priority;
212         atomic_t cra_refcnt;
213
214         char cra_name[CRYPTO_MAX_ALG_NAME];
215         char cra_driver_name[CRYPTO_MAX_ALG_NAME];
216
217         const struct crypto_type *cra_type;
218
219         union {
220                 struct blkcipher_alg blkcipher;
221                 struct cipher_alg cipher;
222                 struct digest_alg digest;
223                 struct hash_alg hash;
224                 struct compress_alg compress;
225         } cra_u;
226
227         int (*cra_init)(struct crypto_tfm *tfm);
228         void (*cra_exit)(struct crypto_tfm *tfm);
229         void (*cra_destroy)(struct crypto_alg *alg);
230         
231         struct module *cra_module;
232 };
233
234 /*
235  * Algorithm registration interface.
236  */
237 int crypto_register_alg(struct crypto_alg *alg);
238 int crypto_unregister_alg(struct crypto_alg *alg);
239
240 /*
241  * Algorithm query interface.
242  */
243 #ifdef CONFIG_CRYPTO
244 int crypto_has_alg(const char *name, u32 type, u32 mask);
245 #else
246 static inline int crypto_alg_available(const char *name, u32 flags)
247 {
248         return 0;
249 }
250
251 static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
252 {
253         return 0;
254 }
255 #endif
256
257 /*
258  * Transforms: user-instantiated objects which encapsulate algorithms
259  * and core processing logic.  Managed via crypto_alloc_*() and
260  * crypto_free_*(), as well as the various helpers below.
261  */
262
263 struct blkcipher_tfm {
264         void *iv;
265         int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
266                       unsigned int keylen);
267         int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
268                        struct scatterlist *src, unsigned int nbytes);
269         int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
270                        struct scatterlist *src, unsigned int nbytes);
271 };
272
273 struct cipher_tfm {
274         void *cit_iv;
275         unsigned int cit_ivsize;
276         u32 cit_mode;
277         int (*cit_setkey)(struct crypto_tfm *tfm,
278                           const u8 *key, unsigned int keylen);
279         int (*cit_encrypt)(struct crypto_tfm *tfm,
280                            struct scatterlist *dst,
281                            struct scatterlist *src,
282                            unsigned int nbytes);
283         int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
284                               struct scatterlist *dst,
285                               struct scatterlist *src,
286                               unsigned int nbytes, u8 *iv);
287         int (*cit_decrypt)(struct crypto_tfm *tfm,
288                            struct scatterlist *dst,
289                            struct scatterlist *src,
290                            unsigned int nbytes);
291         int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
292                            struct scatterlist *dst,
293                            struct scatterlist *src,
294                            unsigned int nbytes, u8 *iv);
295         void (*cit_xor_block)(u8 *dst, const u8 *src);
296         void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
297         void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
298 };
299
300 struct hash_tfm {
301         int (*init)(struct hash_desc *desc);
302         int (*update)(struct hash_desc *desc,
303                       struct scatterlist *sg, unsigned int nsg);
304         int (*final)(struct hash_desc *desc, u8 *out);
305         int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
306                       unsigned int nsg, u8 *out);
307         int (*setkey)(struct crypto_hash *tfm, const u8 *key,
308                       unsigned int keylen);
309         unsigned int digestsize;
310 };
311
312 struct compress_tfm {
313         int (*cot_compress)(struct crypto_tfm *tfm,
314                             const u8 *src, unsigned int slen,
315                             u8 *dst, unsigned int *dlen);
316         int (*cot_decompress)(struct crypto_tfm *tfm,
317                               const u8 *src, unsigned int slen,
318                               u8 *dst, unsigned int *dlen);
319 };
320
321 #define crt_blkcipher   crt_u.blkcipher
322 #define crt_cipher      crt_u.cipher
323 #define crt_hash        crt_u.hash
324 #define crt_compress    crt_u.compress
325
326 struct crypto_tfm {
327
328         u32 crt_flags;
329         
330         union {
331                 struct blkcipher_tfm blkcipher;
332                 struct cipher_tfm cipher;
333                 struct hash_tfm hash;
334                 struct compress_tfm compress;
335         } crt_u;
336         
337         struct crypto_alg *__crt_alg;
338
339         void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
340 };
341
342 #define crypto_cipher crypto_tfm
343 #define crypto_comp crypto_tfm
344
345 struct crypto_blkcipher {
346         struct crypto_tfm base;
347 };
348
349 struct crypto_hash {
350         struct crypto_tfm base;
351 };
352
353 enum {
354         CRYPTOA_UNSPEC,
355         CRYPTOA_ALG,
356 };
357
358 struct crypto_attr_alg {
359         char name[CRYPTO_MAX_ALG_NAME];
360 };
361
362 /* 
363  * Transform user interface.
364  */
365  
366 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
367 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
368 void crypto_free_tfm(struct crypto_tfm *tfm);
369
370 /*
371  * Transform helpers which query the underlying algorithm.
372  */
373 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
374 {
375         return tfm->__crt_alg->cra_name;
376 }
377
378 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
379 {
380         return tfm->__crt_alg->cra_driver_name;
381 }
382
383 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
384 {
385         return tfm->__crt_alg->cra_priority;
386 }
387
388 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
389 {
390         return module_name(tfm->__crt_alg->cra_module);
391 }
392
393 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
394 {
395         return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
396 }
397
398 static unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
399         __deprecated;
400 static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
401 {
402         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
403         return tfm->__crt_alg->cra_cipher.cia_min_keysize;
404 }
405
406 static unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
407         __deprecated;
408 static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
409 {
410         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
411         return tfm->__crt_alg->cra_cipher.cia_max_keysize;
412 }
413
414 static unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm) __deprecated;
415 static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
416 {
417         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
418         return tfm->crt_cipher.cit_ivsize;
419 }
420
421 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
422 {
423         return tfm->__crt_alg->cra_blocksize;
424 }
425
426 static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
427 {
428         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
429         return tfm->__crt_alg->cra_digest.dia_digestsize;
430 }
431
432 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
433 {
434         return tfm->__crt_alg->cra_alignmask;
435 }
436
437 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
438 {
439         return tfm->crt_flags;
440 }
441
442 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
443 {
444         tfm->crt_flags |= flags;
445 }
446
447 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
448 {
449         tfm->crt_flags &= ~flags;
450 }
451
452 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
453 {
454         return tfm->__crt_ctx;
455 }
456
457 static inline unsigned int crypto_tfm_ctx_alignment(void)
458 {
459         struct crypto_tfm *tfm;
460         return __alignof__(tfm->__crt_ctx);
461 }
462
463 /*
464  * API wrappers.
465  */
466 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
467         struct crypto_tfm *tfm)
468 {
469         return (struct crypto_blkcipher *)tfm;
470 }
471
472 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
473         struct crypto_tfm *tfm)
474 {
475         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
476         return __crypto_blkcipher_cast(tfm);
477 }
478
479 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
480         const char *alg_name, u32 type, u32 mask)
481 {
482         type &= ~CRYPTO_ALG_TYPE_MASK;
483         type |= CRYPTO_ALG_TYPE_BLKCIPHER;
484         mask |= CRYPTO_ALG_TYPE_MASK;
485
486         return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
487 }
488
489 static inline struct crypto_tfm *crypto_blkcipher_tfm(
490         struct crypto_blkcipher *tfm)
491 {
492         return &tfm->base;
493 }
494
495 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
496 {
497         crypto_free_tfm(crypto_blkcipher_tfm(tfm));
498 }
499
500 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
501 {
502         type &= ~CRYPTO_ALG_TYPE_MASK;
503         type |= CRYPTO_ALG_TYPE_BLKCIPHER;
504         mask |= CRYPTO_ALG_TYPE_MASK;
505
506         return crypto_has_alg(alg_name, type, mask);
507 }
508
509 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
510 {
511         return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
512 }
513
514 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
515         struct crypto_blkcipher *tfm)
516 {
517         return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
518 }
519
520 static inline struct blkcipher_alg *crypto_blkcipher_alg(
521         struct crypto_blkcipher *tfm)
522 {
523         return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
524 }
525
526 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
527 {
528         return crypto_blkcipher_alg(tfm)->ivsize;
529 }
530
531 static inline unsigned int crypto_blkcipher_blocksize(
532         struct crypto_blkcipher *tfm)
533 {
534         return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
535 }
536
537 static inline unsigned int crypto_blkcipher_alignmask(
538         struct crypto_blkcipher *tfm)
539 {
540         return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
541 }
542
543 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
544 {
545         return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
546 }
547
548 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
549                                               u32 flags)
550 {
551         crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
552 }
553
554 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
555                                                 u32 flags)
556 {
557         crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
558 }
559
560 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
561                                           const u8 *key, unsigned int keylen)
562 {
563         return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
564                                                  key, keylen);
565 }
566
567 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
568                                            struct scatterlist *dst,
569                                            struct scatterlist *src,
570                                            unsigned int nbytes)
571 {
572         desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
573         return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
574 }
575
576 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
577                                               struct scatterlist *dst,
578                                               struct scatterlist *src,
579                                               unsigned int nbytes)
580 {
581         return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
582 }
583
584 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
585                                            struct scatterlist *dst,
586                                            struct scatterlist *src,
587                                            unsigned int nbytes)
588 {
589         desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
590         return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
591 }
592
593 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
594                                               struct scatterlist *dst,
595                                               struct scatterlist *src,
596                                               unsigned int nbytes)
597 {
598         return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
599 }
600
601 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
602                                            const u8 *src, unsigned int len)
603 {
604         memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
605 }
606
607 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
608                                            u8 *dst, unsigned int len)
609 {
610         memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
611 }
612
613 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
614 {
615         return (struct crypto_cipher *)tfm;
616 }
617
618 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
619 {
620         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
621         return __crypto_cipher_cast(tfm);
622 }
623
624 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
625                                                         u32 type, u32 mask)
626 {
627         type &= ~CRYPTO_ALG_TYPE_MASK;
628         type |= CRYPTO_ALG_TYPE_CIPHER;
629         mask |= CRYPTO_ALG_TYPE_MASK;
630
631         return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
632 }
633
634 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
635 {
636         return tfm;
637 }
638
639 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
640 {
641         crypto_free_tfm(crypto_cipher_tfm(tfm));
642 }
643
644 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
645 {
646         type &= ~CRYPTO_ALG_TYPE_MASK;
647         type |= CRYPTO_ALG_TYPE_CIPHER;
648         mask |= CRYPTO_ALG_TYPE_MASK;
649
650         return crypto_has_alg(alg_name, type, mask);
651 }
652
653 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
654 {
655         return &crypto_cipher_tfm(tfm)->crt_cipher;
656 }
657
658 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
659 {
660         return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
661 }
662
663 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
664 {
665         return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
666 }
667
668 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
669 {
670         return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
671 }
672
673 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
674                                            u32 flags)
675 {
676         crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
677 }
678
679 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
680                                              u32 flags)
681 {
682         crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
683 }
684
685 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
686                                        const u8 *key, unsigned int keylen)
687 {
688         return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
689                                                   key, keylen);
690 }
691
692 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
693                                              u8 *dst, const u8 *src)
694 {
695         crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
696                                                 dst, src);
697 }
698
699 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
700                                              u8 *dst, const u8 *src)
701 {
702         crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
703                                                 dst, src);
704 }
705
706 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
707 {
708         return (struct crypto_hash *)tfm;
709 }
710
711 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
712 {
713         BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
714                CRYPTO_ALG_TYPE_HASH_MASK);
715         return __crypto_hash_cast(tfm);
716 }
717
718 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
719                                                     u32 type, u32 mask)
720 {
721         type &= ~CRYPTO_ALG_TYPE_MASK;
722         type |= CRYPTO_ALG_TYPE_HASH;
723         mask |= CRYPTO_ALG_TYPE_HASH_MASK;
724
725         return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
726 }
727
728 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
729 {
730         return &tfm->base;
731 }
732
733 static inline void crypto_free_hash(struct crypto_hash *tfm)
734 {
735         crypto_free_tfm(crypto_hash_tfm(tfm));
736 }
737
738 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
739 {
740         type &= ~CRYPTO_ALG_TYPE_MASK;
741         type |= CRYPTO_ALG_TYPE_HASH;
742         mask |= CRYPTO_ALG_TYPE_HASH_MASK;
743
744         return crypto_has_alg(alg_name, type, mask);
745 }
746
747 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
748 {
749         return &crypto_hash_tfm(tfm)->crt_hash;
750 }
751
752 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
753 {
754         return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
755 }
756
757 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
758 {
759         return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
760 }
761
762 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
763 {
764         return crypto_hash_crt(tfm)->digestsize;
765 }
766
767 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
768 {
769         return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
770 }
771
772 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
773 {
774         crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
775 }
776
777 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
778 {
779         crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
780 }
781
782 static inline int crypto_hash_init(struct hash_desc *desc)
783 {
784         return crypto_hash_crt(desc->tfm)->init(desc);
785 }
786
787 static inline int crypto_hash_update(struct hash_desc *desc,
788                                      struct scatterlist *sg,
789                                      unsigned int nbytes)
790 {
791         return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
792 }
793
794 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
795 {
796         return crypto_hash_crt(desc->tfm)->final(desc, out);
797 }
798
799 static inline int crypto_hash_digest(struct hash_desc *desc,
800                                      struct scatterlist *sg,
801                                      unsigned int nbytes, u8 *out)
802 {
803         return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
804 }
805
806 static inline int crypto_hash_setkey(struct crypto_hash *hash,
807                                      const u8 *key, unsigned int keylen)
808 {
809         return crypto_hash_crt(hash)->setkey(hash, key, keylen);
810 }
811
812 static int crypto_cipher_encrypt(struct crypto_tfm *tfm,
813                                  struct scatterlist *dst,
814                                  struct scatterlist *src,
815                                  unsigned int nbytes) __deprecated;
816 static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
817                                         struct scatterlist *dst,
818                                         struct scatterlist *src,
819                                         unsigned int nbytes)
820 {
821         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
822         return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
823 }                                        
824
825 static int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
826                                     struct scatterlist *dst,
827                                     struct scatterlist *src,
828                                     unsigned int nbytes, u8 *iv) __deprecated;
829 static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
830                                            struct scatterlist *dst,
831                                            struct scatterlist *src,
832                                            unsigned int nbytes, u8 *iv)
833 {
834         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
835         return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
836 }                                        
837
838 static int crypto_cipher_decrypt(struct crypto_tfm *tfm,
839                                  struct scatterlist *dst,
840                                  struct scatterlist *src,
841                                  unsigned int nbytes) __deprecated;
842 static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
843                                         struct scatterlist *dst,
844                                         struct scatterlist *src,
845                                         unsigned int nbytes)
846 {
847         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
848         return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
849 }
850
851 static int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
852                                     struct scatterlist *dst,
853                                     struct scatterlist *src,
854                                     unsigned int nbytes, u8 *iv) __deprecated;
855 static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
856                                            struct scatterlist *dst,
857                                            struct scatterlist *src,
858                                            unsigned int nbytes, u8 *iv)
859 {
860         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
861         return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
862 }
863
864 static void crypto_cipher_set_iv(struct crypto_tfm *tfm,
865                                  const u8 *src, unsigned int len) __deprecated;
866 static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
867                                         const u8 *src, unsigned int len)
868 {
869         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
870         memcpy(tfm->crt_cipher.cit_iv, src, len);
871 }
872
873 static void crypto_cipher_get_iv(struct crypto_tfm *tfm,
874                                  u8 *dst, unsigned int len) __deprecated;
875 static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
876                                         u8 *dst, unsigned int len)
877 {
878         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
879         memcpy(dst, tfm->crt_cipher.cit_iv, len);
880 }
881
882 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
883 {
884         return (struct crypto_comp *)tfm;
885 }
886
887 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
888 {
889         BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
890                CRYPTO_ALG_TYPE_MASK);
891         return __crypto_comp_cast(tfm);
892 }
893
894 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
895                                                     u32 type, u32 mask)
896 {
897         type &= ~CRYPTO_ALG_TYPE_MASK;
898         type |= CRYPTO_ALG_TYPE_COMPRESS;
899         mask |= CRYPTO_ALG_TYPE_MASK;
900
901         return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
902 }
903
904 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
905 {
906         return tfm;
907 }
908
909 static inline void crypto_free_comp(struct crypto_comp *tfm)
910 {
911         crypto_free_tfm(crypto_comp_tfm(tfm));
912 }
913
914 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
915 {
916         type &= ~CRYPTO_ALG_TYPE_MASK;
917         type |= CRYPTO_ALG_TYPE_COMPRESS;
918         mask |= CRYPTO_ALG_TYPE_MASK;
919
920         return crypto_has_alg(alg_name, type, mask);
921 }
922
923 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
924 {
925         return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
926 }
927
928 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
929 {
930         return &crypto_comp_tfm(tfm)->crt_compress;
931 }
932
933 static inline int crypto_comp_compress(struct crypto_comp *tfm,
934                                        const u8 *src, unsigned int slen,
935                                        u8 *dst, unsigned int *dlen)
936 {
937         return crypto_comp_crt(tfm)->cot_compress(tfm, src, slen, dst, dlen);
938 }
939
940 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
941                                          const u8 *src, unsigned int slen,
942                                          u8 *dst, unsigned int *dlen)
943 {
944         return crypto_comp_crt(tfm)->cot_decompress(tfm, src, slen, dst, dlen);
945 }
946
947 #endif  /* _LINUX_CRYPTO_H */
948