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