[CRYPTO] hash: Add asynchronous hash support
[safe/jmp/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_COMPRESS        0x00000002
34 #define CRYPTO_ALG_TYPE_AEAD            0x00000003
35 #define CRYPTO_ALG_TYPE_BLKCIPHER       0x00000004
36 #define CRYPTO_ALG_TYPE_ABLKCIPHER      0x00000005
37 #define CRYPTO_ALG_TYPE_GIVCIPHER       0x00000006
38 #define CRYPTO_ALG_TYPE_DIGEST          0x00000008
39 #define CRYPTO_ALG_TYPE_HASH            0x00000009
40 #define CRYPTO_ALG_TYPE_AHASH           0x0000000a
41
42 #define CRYPTO_ALG_TYPE_HASH_MASK       0x0000000e
43 #define CRYPTO_ALG_TYPE_AHASH_MASK      0x0000000c
44 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK  0x0000000c
45
46 #define CRYPTO_ALG_LARVAL               0x00000010
47 #define CRYPTO_ALG_DEAD                 0x00000020
48 #define CRYPTO_ALG_DYING                0x00000040
49 #define CRYPTO_ALG_ASYNC                0x00000080
50
51 /*
52  * Set this bit if and only if the algorithm requires another algorithm of
53  * the same type to handle corner cases.
54  */
55 #define CRYPTO_ALG_NEED_FALLBACK        0x00000100
56
57 /*
58  * This bit is set for symmetric key ciphers that have already been wrapped
59  * with a generic IV generator to prevent them from being wrapped again.
60  */
61 #define CRYPTO_ALG_GENIV                0x00000200
62
63 /*
64  * Transform masks and values (for crt_flags).
65  */
66 #define CRYPTO_TFM_REQ_MASK             0x000fff00
67 #define CRYPTO_TFM_RES_MASK             0xfff00000
68
69 #define CRYPTO_TFM_REQ_WEAK_KEY         0x00000100
70 #define CRYPTO_TFM_REQ_MAY_SLEEP        0x00000200
71 #define CRYPTO_TFM_REQ_MAY_BACKLOG      0x00000400
72 #define CRYPTO_TFM_RES_WEAK_KEY         0x00100000
73 #define CRYPTO_TFM_RES_BAD_KEY_LEN      0x00200000
74 #define CRYPTO_TFM_RES_BAD_KEY_SCHED    0x00400000
75 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN    0x00800000
76 #define CRYPTO_TFM_RES_BAD_FLAGS        0x01000000
77
78 /*
79  * Miscellaneous stuff.
80  */
81 #define CRYPTO_MAX_ALG_NAME             64
82
83 /*
84  * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
85  * declaration) is used to ensure that the crypto_tfm context structure is
86  * aligned correctly for the given architecture so that there are no alignment
87  * faults for C data types.  In particular, this is required on platforms such
88  * as arm where pointers are 32-bit aligned but there are data types such as
89  * u64 which require 64-bit alignment.
90  */
91 #if defined(ARCH_KMALLOC_MINALIGN)
92 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
93 #elif defined(ARCH_SLAB_MINALIGN)
94 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
95 #else
96 #define CRYPTO_MINALIGN __alignof__(unsigned long long)
97 #endif
98
99 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
100
101 struct scatterlist;
102 struct crypto_ablkcipher;
103 struct crypto_async_request;
104 struct crypto_aead;
105 struct crypto_blkcipher;
106 struct crypto_hash;
107 struct crypto_ahash;
108 struct crypto_tfm;
109 struct crypto_type;
110 struct aead_givcrypt_request;
111 struct skcipher_givcrypt_request;
112
113 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
114
115 struct crypto_async_request {
116         struct list_head list;
117         crypto_completion_t complete;
118         void *data;
119         struct crypto_tfm *tfm;
120
121         u32 flags;
122 };
123
124 struct ablkcipher_request {
125         struct crypto_async_request base;
126
127         unsigned int nbytes;
128
129         void *info;
130
131         struct scatterlist *src;
132         struct scatterlist *dst;
133
134         void *__ctx[] CRYPTO_MINALIGN_ATTR;
135 };
136
137 struct ahash_request {
138         struct crypto_async_request base;
139
140         void *info;
141
142         unsigned int nbytes;
143         struct scatterlist *src;
144         u8                 *result;
145
146         void *__ctx[] CRYPTO_MINALIGN_ATTR;
147 };
148
149 /**
150  *      struct aead_request - AEAD request
151  *      @base: Common attributes for async crypto requests
152  *      @assoclen: Length in bytes of associated data for authentication
153  *      @cryptlen: Length of data to be encrypted or decrypted
154  *      @iv: Initialisation vector
155  *      @assoc: Associated data
156  *      @src: Source data
157  *      @dst: Destination data
158  *      @__ctx: Start of private context data
159  */
160 struct aead_request {
161         struct crypto_async_request base;
162
163         unsigned int assoclen;
164         unsigned int cryptlen;
165
166         u8 *iv;
167
168         struct scatterlist *assoc;
169         struct scatterlist *src;
170         struct scatterlist *dst;
171
172         void *__ctx[] CRYPTO_MINALIGN_ATTR;
173 };
174
175 struct blkcipher_desc {
176         struct crypto_blkcipher *tfm;
177         void *info;
178         u32 flags;
179 };
180
181 struct cipher_desc {
182         struct crypto_tfm *tfm;
183         void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
184         unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
185                              const u8 *src, unsigned int nbytes);
186         void *info;
187 };
188
189 struct hash_desc {
190         struct crypto_hash *tfm;
191         u32 flags;
192 };
193
194 /*
195  * Algorithms: modular crypto algorithm implementations, managed
196  * via crypto_register_alg() and crypto_unregister_alg().
197  */
198 struct ablkcipher_alg {
199         int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
200                       unsigned int keylen);
201         int (*encrypt)(struct ablkcipher_request *req);
202         int (*decrypt)(struct ablkcipher_request *req);
203         int (*givencrypt)(struct skcipher_givcrypt_request *req);
204         int (*givdecrypt)(struct skcipher_givcrypt_request *req);
205
206         const char *geniv;
207
208         unsigned int min_keysize;
209         unsigned int max_keysize;
210         unsigned int ivsize;
211 };
212
213 struct ahash_alg {
214         int (*init)(struct ahash_request *req);
215         int (*update)(struct ahash_request *req);
216         int (*final)(struct ahash_request *req);
217         int (*digest)(struct ahash_request *req);
218         int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
219                         unsigned int keylen);
220
221         unsigned int digestsize;
222 };
223
224 struct aead_alg {
225         int (*setkey)(struct crypto_aead *tfm, const u8 *key,
226                       unsigned int keylen);
227         int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
228         int (*encrypt)(struct aead_request *req);
229         int (*decrypt)(struct aead_request *req);
230         int (*givencrypt)(struct aead_givcrypt_request *req);
231         int (*givdecrypt)(struct aead_givcrypt_request *req);
232
233         const char *geniv;
234
235         unsigned int ivsize;
236         unsigned int maxauthsize;
237 };
238
239 struct blkcipher_alg {
240         int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
241                       unsigned int keylen);
242         int (*encrypt)(struct blkcipher_desc *desc,
243                        struct scatterlist *dst, struct scatterlist *src,
244                        unsigned int nbytes);
245         int (*decrypt)(struct blkcipher_desc *desc,
246                        struct scatterlist *dst, struct scatterlist *src,
247                        unsigned int nbytes);
248
249         const char *geniv;
250
251         unsigned int min_keysize;
252         unsigned int max_keysize;
253         unsigned int ivsize;
254 };
255
256 struct cipher_alg {
257         unsigned int cia_min_keysize;
258         unsigned int cia_max_keysize;
259         int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
260                           unsigned int keylen);
261         void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
262         void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
263 };
264
265 struct digest_alg {
266         unsigned int dia_digestsize;
267         void (*dia_init)(struct crypto_tfm *tfm);
268         void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
269                            unsigned int len);
270         void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
271         int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
272                           unsigned int keylen);
273 };
274
275 struct hash_alg {
276         int (*init)(struct hash_desc *desc);
277         int (*update)(struct hash_desc *desc, struct scatterlist *sg,
278                       unsigned int nbytes);
279         int (*final)(struct hash_desc *desc, u8 *out);
280         int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
281                       unsigned int nbytes, u8 *out);
282         int (*setkey)(struct crypto_hash *tfm, const u8 *key,
283                       unsigned int keylen);
284
285         unsigned int digestsize;
286 };
287
288 struct compress_alg {
289         int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
290                             unsigned int slen, u8 *dst, unsigned int *dlen);
291         int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
292                               unsigned int slen, u8 *dst, unsigned int *dlen);
293 };
294
295 #define cra_ablkcipher  cra_u.ablkcipher
296 #define cra_aead        cra_u.aead
297 #define cra_blkcipher   cra_u.blkcipher
298 #define cra_cipher      cra_u.cipher
299 #define cra_digest      cra_u.digest
300 #define cra_hash        cra_u.hash
301 #define cra_ahash       cra_u.ahash
302 #define cra_compress    cra_u.compress
303
304 struct crypto_alg {
305         struct list_head cra_list;
306         struct list_head cra_users;
307
308         u32 cra_flags;
309         unsigned int cra_blocksize;
310         unsigned int cra_ctxsize;
311         unsigned int cra_alignmask;
312
313         int cra_priority;
314         atomic_t cra_refcnt;
315
316         char cra_name[CRYPTO_MAX_ALG_NAME];
317         char cra_driver_name[CRYPTO_MAX_ALG_NAME];
318
319         const struct crypto_type *cra_type;
320
321         union {
322                 struct ablkcipher_alg ablkcipher;
323                 struct aead_alg aead;
324                 struct blkcipher_alg blkcipher;
325                 struct cipher_alg cipher;
326                 struct digest_alg digest;
327                 struct hash_alg hash;
328                 struct ahash_alg ahash;
329                 struct compress_alg compress;
330         } cra_u;
331
332         int (*cra_init)(struct crypto_tfm *tfm);
333         void (*cra_exit)(struct crypto_tfm *tfm);
334         void (*cra_destroy)(struct crypto_alg *alg);
335         
336         struct module *cra_module;
337 };
338
339 /*
340  * Algorithm registration interface.
341  */
342 int crypto_register_alg(struct crypto_alg *alg);
343 int crypto_unregister_alg(struct crypto_alg *alg);
344
345 /*
346  * Algorithm query interface.
347  */
348 int crypto_has_alg(const char *name, u32 type, u32 mask);
349
350 /*
351  * Transforms: user-instantiated objects which encapsulate algorithms
352  * and core processing logic.  Managed via crypto_alloc_*() and
353  * crypto_free_*(), as well as the various helpers below.
354  */
355
356 struct ablkcipher_tfm {
357         int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
358                       unsigned int keylen);
359         int (*encrypt)(struct ablkcipher_request *req);
360         int (*decrypt)(struct ablkcipher_request *req);
361         int (*givencrypt)(struct skcipher_givcrypt_request *req);
362         int (*givdecrypt)(struct skcipher_givcrypt_request *req);
363
364         struct crypto_ablkcipher *base;
365
366         unsigned int ivsize;
367         unsigned int reqsize;
368 };
369
370 struct aead_tfm {
371         int (*setkey)(struct crypto_aead *tfm, const u8 *key,
372                       unsigned int keylen);
373         int (*encrypt)(struct aead_request *req);
374         int (*decrypt)(struct aead_request *req);
375         int (*givencrypt)(struct aead_givcrypt_request *req);
376         int (*givdecrypt)(struct aead_givcrypt_request *req);
377
378         struct crypto_aead *base;
379
380         unsigned int ivsize;
381         unsigned int authsize;
382         unsigned int reqsize;
383 };
384
385 struct blkcipher_tfm {
386         void *iv;
387         int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
388                       unsigned int keylen);
389         int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
390                        struct scatterlist *src, unsigned int nbytes);
391         int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
392                        struct scatterlist *src, unsigned int nbytes);
393 };
394
395 struct cipher_tfm {
396         int (*cit_setkey)(struct crypto_tfm *tfm,
397                           const u8 *key, unsigned int keylen);
398         void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
399         void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
400 };
401
402 struct hash_tfm {
403         int (*init)(struct hash_desc *desc);
404         int (*update)(struct hash_desc *desc,
405                       struct scatterlist *sg, unsigned int nsg);
406         int (*final)(struct hash_desc *desc, u8 *out);
407         int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
408                       unsigned int nsg, u8 *out);
409         int (*setkey)(struct crypto_hash *tfm, const u8 *key,
410                       unsigned int keylen);
411         unsigned int digestsize;
412 };
413
414 struct ahash_tfm {
415         int (*init)(struct ahash_request *req);
416         int (*update)(struct ahash_request *req);
417         int (*final)(struct ahash_request *req);
418         int (*digest)(struct ahash_request *req);
419         int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
420                         unsigned int keylen);
421
422         unsigned int digestsize;
423         struct crypto_ahash *base;
424         unsigned int reqsize;
425 };
426
427 struct compress_tfm {
428         int (*cot_compress)(struct crypto_tfm *tfm,
429                             const u8 *src, unsigned int slen,
430                             u8 *dst, unsigned int *dlen);
431         int (*cot_decompress)(struct crypto_tfm *tfm,
432                               const u8 *src, unsigned int slen,
433                               u8 *dst, unsigned int *dlen);
434 };
435
436 #define crt_ablkcipher  crt_u.ablkcipher
437 #define crt_aead        crt_u.aead
438 #define crt_blkcipher   crt_u.blkcipher
439 #define crt_cipher      crt_u.cipher
440 #define crt_hash        crt_u.hash
441 #define crt_ahash       crt_u.ahash
442 #define crt_compress    crt_u.compress
443
444 struct crypto_tfm {
445
446         u32 crt_flags;
447         
448         union {
449                 struct ablkcipher_tfm ablkcipher;
450                 struct aead_tfm aead;
451                 struct blkcipher_tfm blkcipher;
452                 struct cipher_tfm cipher;
453                 struct hash_tfm hash;
454                 struct ahash_tfm ahash;
455                 struct compress_tfm compress;
456         } crt_u;
457         
458         struct crypto_alg *__crt_alg;
459
460         void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
461 };
462
463 struct crypto_ablkcipher {
464         struct crypto_tfm base;
465 };
466
467 struct crypto_aead {
468         struct crypto_tfm base;
469 };
470
471 struct crypto_blkcipher {
472         struct crypto_tfm base;
473 };
474
475 struct crypto_cipher {
476         struct crypto_tfm base;
477 };
478
479 struct crypto_comp {
480         struct crypto_tfm base;
481 };
482
483 struct crypto_hash {
484         struct crypto_tfm base;
485 };
486
487 struct crypto_ahash {
488         struct crypto_tfm base;
489 };
490
491 enum {
492         CRYPTOA_UNSPEC,
493         CRYPTOA_ALG,
494         CRYPTOA_TYPE,
495         CRYPTOA_U32,
496         __CRYPTOA_MAX,
497 };
498
499 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
500
501 /* Maximum number of (rtattr) parameters for each template. */
502 #define CRYPTO_MAX_ATTRS 32
503
504 struct crypto_attr_alg {
505         char name[CRYPTO_MAX_ALG_NAME];
506 };
507
508 struct crypto_attr_type {
509         u32 type;
510         u32 mask;
511 };
512
513 struct crypto_attr_u32 {
514         u32 num;
515 };
516
517 /* 
518  * Transform user interface.
519  */
520  
521 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
522 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
523 void crypto_free_tfm(struct crypto_tfm *tfm);
524
525 /*
526  * Transform helpers which query the underlying algorithm.
527  */
528 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
529 {
530         return tfm->__crt_alg->cra_name;
531 }
532
533 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
534 {
535         return tfm->__crt_alg->cra_driver_name;
536 }
537
538 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
539 {
540         return tfm->__crt_alg->cra_priority;
541 }
542
543 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
544 {
545         return module_name(tfm->__crt_alg->cra_module);
546 }
547
548 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
549 {
550         return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
551 }
552
553 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
554 {
555         return tfm->__crt_alg->cra_blocksize;
556 }
557
558 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
559 {
560         return tfm->__crt_alg->cra_alignmask;
561 }
562
563 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
564 {
565         return tfm->crt_flags;
566 }
567
568 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
569 {
570         tfm->crt_flags |= flags;
571 }
572
573 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
574 {
575         tfm->crt_flags &= ~flags;
576 }
577
578 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
579 {
580         return tfm->__crt_ctx;
581 }
582
583 static inline unsigned int crypto_tfm_ctx_alignment(void)
584 {
585         struct crypto_tfm *tfm;
586         return __alignof__(tfm->__crt_ctx);
587 }
588
589 /*
590  * API wrappers.
591  */
592 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
593         struct crypto_tfm *tfm)
594 {
595         return (struct crypto_ablkcipher *)tfm;
596 }
597
598 static inline u32 crypto_skcipher_type(u32 type)
599 {
600         type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
601         type |= CRYPTO_ALG_TYPE_BLKCIPHER;
602         return type;
603 }
604
605 static inline u32 crypto_skcipher_mask(u32 mask)
606 {
607         mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
608         mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
609         return mask;
610 }
611
612 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
613                                                   u32 type, u32 mask);
614
615 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
616         struct crypto_ablkcipher *tfm)
617 {
618         return &tfm->base;
619 }
620
621 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
622 {
623         crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
624 }
625
626 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
627                                         u32 mask)
628 {
629         return crypto_has_alg(alg_name, crypto_skcipher_type(type),
630                               crypto_skcipher_mask(mask));
631 }
632
633 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
634         struct crypto_ablkcipher *tfm)
635 {
636         return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
637 }
638
639 static inline unsigned int crypto_ablkcipher_ivsize(
640         struct crypto_ablkcipher *tfm)
641 {
642         return crypto_ablkcipher_crt(tfm)->ivsize;
643 }
644
645 static inline unsigned int crypto_ablkcipher_blocksize(
646         struct crypto_ablkcipher *tfm)
647 {
648         return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
649 }
650
651 static inline unsigned int crypto_ablkcipher_alignmask(
652         struct crypto_ablkcipher *tfm)
653 {
654         return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
655 }
656
657 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
658 {
659         return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
660 }
661
662 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
663                                                u32 flags)
664 {
665         crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
666 }
667
668 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
669                                                  u32 flags)
670 {
671         crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
672 }
673
674 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
675                                            const u8 *key, unsigned int keylen)
676 {
677         struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
678
679         return crt->setkey(crt->base, key, keylen);
680 }
681
682 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
683         struct ablkcipher_request *req)
684 {
685         return __crypto_ablkcipher_cast(req->base.tfm);
686 }
687
688 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
689 {
690         struct ablkcipher_tfm *crt =
691                 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
692         return crt->encrypt(req);
693 }
694
695 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
696 {
697         struct ablkcipher_tfm *crt =
698                 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
699         return crt->decrypt(req);
700 }
701
702 static inline unsigned int crypto_ablkcipher_reqsize(
703         struct crypto_ablkcipher *tfm)
704 {
705         return crypto_ablkcipher_crt(tfm)->reqsize;
706 }
707
708 static inline void ablkcipher_request_set_tfm(
709         struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
710 {
711         req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
712 }
713
714 static inline struct ablkcipher_request *ablkcipher_request_cast(
715         struct crypto_async_request *req)
716 {
717         return container_of(req, struct ablkcipher_request, base);
718 }
719
720 static inline struct ablkcipher_request *ablkcipher_request_alloc(
721         struct crypto_ablkcipher *tfm, gfp_t gfp)
722 {
723         struct ablkcipher_request *req;
724
725         req = kmalloc(sizeof(struct ablkcipher_request) +
726                       crypto_ablkcipher_reqsize(tfm), gfp);
727
728         if (likely(req))
729                 ablkcipher_request_set_tfm(req, tfm);
730
731         return req;
732 }
733
734 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
735 {
736         kfree(req);
737 }
738
739 static inline void ablkcipher_request_set_callback(
740         struct ablkcipher_request *req,
741         u32 flags, crypto_completion_t complete, void *data)
742 {
743         req->base.complete = complete;
744         req->base.data = data;
745         req->base.flags = flags;
746 }
747
748 static inline void ablkcipher_request_set_crypt(
749         struct ablkcipher_request *req,
750         struct scatterlist *src, struct scatterlist *dst,
751         unsigned int nbytes, void *iv)
752 {
753         req->src = src;
754         req->dst = dst;
755         req->nbytes = nbytes;
756         req->info = iv;
757 }
758
759 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
760 {
761         return (struct crypto_aead *)tfm;
762 }
763
764 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
765
766 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
767 {
768         return &tfm->base;
769 }
770
771 static inline void crypto_free_aead(struct crypto_aead *tfm)
772 {
773         crypto_free_tfm(crypto_aead_tfm(tfm));
774 }
775
776 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
777 {
778         return &crypto_aead_tfm(tfm)->crt_aead;
779 }
780
781 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
782 {
783         return crypto_aead_crt(tfm)->ivsize;
784 }
785
786 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
787 {
788         return crypto_aead_crt(tfm)->authsize;
789 }
790
791 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
792 {
793         return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
794 }
795
796 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
797 {
798         return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
799 }
800
801 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
802 {
803         return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
804 }
805
806 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
807 {
808         crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
809 }
810
811 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
812 {
813         crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
814 }
815
816 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
817                                      unsigned int keylen)
818 {
819         struct aead_tfm *crt = crypto_aead_crt(tfm);
820
821         return crt->setkey(crt->base, key, keylen);
822 }
823
824 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
825
826 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
827 {
828         return __crypto_aead_cast(req->base.tfm);
829 }
830
831 static inline int crypto_aead_encrypt(struct aead_request *req)
832 {
833         return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
834 }
835
836 static inline int crypto_aead_decrypt(struct aead_request *req)
837 {
838         return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
839 }
840
841 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
842 {
843         return crypto_aead_crt(tfm)->reqsize;
844 }
845
846 static inline void aead_request_set_tfm(struct aead_request *req,
847                                         struct crypto_aead *tfm)
848 {
849         req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
850 }
851
852 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
853                                                       gfp_t gfp)
854 {
855         struct aead_request *req;
856
857         req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
858
859         if (likely(req))
860                 aead_request_set_tfm(req, tfm);
861
862         return req;
863 }
864
865 static inline void aead_request_free(struct aead_request *req)
866 {
867         kfree(req);
868 }
869
870 static inline void aead_request_set_callback(struct aead_request *req,
871                                              u32 flags,
872                                              crypto_completion_t complete,
873                                              void *data)
874 {
875         req->base.complete = complete;
876         req->base.data = data;
877         req->base.flags = flags;
878 }
879
880 static inline void aead_request_set_crypt(struct aead_request *req,
881                                           struct scatterlist *src,
882                                           struct scatterlist *dst,
883                                           unsigned int cryptlen, u8 *iv)
884 {
885         req->src = src;
886         req->dst = dst;
887         req->cryptlen = cryptlen;
888         req->iv = iv;
889 }
890
891 static inline void aead_request_set_assoc(struct aead_request *req,
892                                           struct scatterlist *assoc,
893                                           unsigned int assoclen)
894 {
895         req->assoc = assoc;
896         req->assoclen = assoclen;
897 }
898
899 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
900         struct crypto_tfm *tfm)
901 {
902         return (struct crypto_blkcipher *)tfm;
903 }
904
905 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
906         struct crypto_tfm *tfm)
907 {
908         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
909         return __crypto_blkcipher_cast(tfm);
910 }
911
912 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
913         const char *alg_name, u32 type, u32 mask)
914 {
915         type &= ~CRYPTO_ALG_TYPE_MASK;
916         type |= CRYPTO_ALG_TYPE_BLKCIPHER;
917         mask |= CRYPTO_ALG_TYPE_MASK;
918
919         return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
920 }
921
922 static inline struct crypto_tfm *crypto_blkcipher_tfm(
923         struct crypto_blkcipher *tfm)
924 {
925         return &tfm->base;
926 }
927
928 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
929 {
930         crypto_free_tfm(crypto_blkcipher_tfm(tfm));
931 }
932
933 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
934 {
935         type &= ~CRYPTO_ALG_TYPE_MASK;
936         type |= CRYPTO_ALG_TYPE_BLKCIPHER;
937         mask |= CRYPTO_ALG_TYPE_MASK;
938
939         return crypto_has_alg(alg_name, type, mask);
940 }
941
942 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
943 {
944         return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
945 }
946
947 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
948         struct crypto_blkcipher *tfm)
949 {
950         return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
951 }
952
953 static inline struct blkcipher_alg *crypto_blkcipher_alg(
954         struct crypto_blkcipher *tfm)
955 {
956         return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
957 }
958
959 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
960 {
961         return crypto_blkcipher_alg(tfm)->ivsize;
962 }
963
964 static inline unsigned int crypto_blkcipher_blocksize(
965         struct crypto_blkcipher *tfm)
966 {
967         return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
968 }
969
970 static inline unsigned int crypto_blkcipher_alignmask(
971         struct crypto_blkcipher *tfm)
972 {
973         return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
974 }
975
976 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
977 {
978         return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
979 }
980
981 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
982                                               u32 flags)
983 {
984         crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
985 }
986
987 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
988                                                 u32 flags)
989 {
990         crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
991 }
992
993 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
994                                           const u8 *key, unsigned int keylen)
995 {
996         return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
997                                                  key, keylen);
998 }
999
1000 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1001                                            struct scatterlist *dst,
1002                                            struct scatterlist *src,
1003                                            unsigned int nbytes)
1004 {
1005         desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1006         return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1007 }
1008
1009 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1010                                               struct scatterlist *dst,
1011                                               struct scatterlist *src,
1012                                               unsigned int nbytes)
1013 {
1014         return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1015 }
1016
1017 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1018                                            struct scatterlist *dst,
1019                                            struct scatterlist *src,
1020                                            unsigned int nbytes)
1021 {
1022         desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1023         return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1024 }
1025
1026 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1027                                               struct scatterlist *dst,
1028                                               struct scatterlist *src,
1029                                               unsigned int nbytes)
1030 {
1031         return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1032 }
1033
1034 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1035                                            const u8 *src, unsigned int len)
1036 {
1037         memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1038 }
1039
1040 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1041                                            u8 *dst, unsigned int len)
1042 {
1043         memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1044 }
1045
1046 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1047 {
1048         return (struct crypto_cipher *)tfm;
1049 }
1050
1051 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1052 {
1053         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1054         return __crypto_cipher_cast(tfm);
1055 }
1056
1057 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1058                                                         u32 type, u32 mask)
1059 {
1060         type &= ~CRYPTO_ALG_TYPE_MASK;
1061         type |= CRYPTO_ALG_TYPE_CIPHER;
1062         mask |= CRYPTO_ALG_TYPE_MASK;
1063
1064         return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1065 }
1066
1067 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1068 {
1069         return &tfm->base;
1070 }
1071
1072 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1073 {
1074         crypto_free_tfm(crypto_cipher_tfm(tfm));
1075 }
1076
1077 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1078 {
1079         type &= ~CRYPTO_ALG_TYPE_MASK;
1080         type |= CRYPTO_ALG_TYPE_CIPHER;
1081         mask |= CRYPTO_ALG_TYPE_MASK;
1082
1083         return crypto_has_alg(alg_name, type, mask);
1084 }
1085
1086 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1087 {
1088         return &crypto_cipher_tfm(tfm)->crt_cipher;
1089 }
1090
1091 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1092 {
1093         return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1094 }
1095
1096 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1097 {
1098         return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1099 }
1100
1101 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1102 {
1103         return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1104 }
1105
1106 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1107                                            u32 flags)
1108 {
1109         crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1110 }
1111
1112 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1113                                              u32 flags)
1114 {
1115         crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1116 }
1117
1118 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1119                                        const u8 *key, unsigned int keylen)
1120 {
1121         return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1122                                                   key, keylen);
1123 }
1124
1125 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1126                                              u8 *dst, const u8 *src)
1127 {
1128         crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1129                                                 dst, src);
1130 }
1131
1132 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1133                                              u8 *dst, const u8 *src)
1134 {
1135         crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1136                                                 dst, src);
1137 }
1138
1139 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1140 {
1141         return (struct crypto_hash *)tfm;
1142 }
1143
1144 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1145 {
1146         BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1147                CRYPTO_ALG_TYPE_HASH_MASK);
1148         return __crypto_hash_cast(tfm);
1149 }
1150
1151 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1152                                                     u32 type, u32 mask)
1153 {
1154         type &= ~CRYPTO_ALG_TYPE_MASK;
1155         mask &= ~CRYPTO_ALG_TYPE_MASK;
1156         type |= CRYPTO_ALG_TYPE_HASH;
1157         mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1158
1159         return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1160 }
1161
1162 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1163 {
1164         return &tfm->base;
1165 }
1166
1167 static inline void crypto_free_hash(struct crypto_hash *tfm)
1168 {
1169         crypto_free_tfm(crypto_hash_tfm(tfm));
1170 }
1171
1172 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1173 {
1174         type &= ~CRYPTO_ALG_TYPE_MASK;
1175         mask &= ~CRYPTO_ALG_TYPE_MASK;
1176         type |= CRYPTO_ALG_TYPE_HASH;
1177         mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1178
1179         return crypto_has_alg(alg_name, type, mask);
1180 }
1181
1182 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1183 {
1184         return &crypto_hash_tfm(tfm)->crt_hash;
1185 }
1186
1187 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1188 {
1189         return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1190 }
1191
1192 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1193 {
1194         return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1195 }
1196
1197 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1198 {
1199         return crypto_hash_crt(tfm)->digestsize;
1200 }
1201
1202 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1203 {
1204         return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1205 }
1206
1207 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1208 {
1209         crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1210 }
1211
1212 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1213 {
1214         crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1215 }
1216
1217 static inline int crypto_hash_init(struct hash_desc *desc)
1218 {
1219         return crypto_hash_crt(desc->tfm)->init(desc);
1220 }
1221
1222 static inline int crypto_hash_update(struct hash_desc *desc,
1223                                      struct scatterlist *sg,
1224                                      unsigned int nbytes)
1225 {
1226         return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1227 }
1228
1229 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1230 {
1231         return crypto_hash_crt(desc->tfm)->final(desc, out);
1232 }
1233
1234 static inline int crypto_hash_digest(struct hash_desc *desc,
1235                                      struct scatterlist *sg,
1236                                      unsigned int nbytes, u8 *out)
1237 {
1238         return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1239 }
1240
1241 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1242                                      const u8 *key, unsigned int keylen)
1243 {
1244         return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1245 }
1246
1247 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1248 {
1249         return (struct crypto_comp *)tfm;
1250 }
1251
1252 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1253 {
1254         BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1255                CRYPTO_ALG_TYPE_MASK);
1256         return __crypto_comp_cast(tfm);
1257 }
1258
1259 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1260                                                     u32 type, u32 mask)
1261 {
1262         type &= ~CRYPTO_ALG_TYPE_MASK;
1263         type |= CRYPTO_ALG_TYPE_COMPRESS;
1264         mask |= CRYPTO_ALG_TYPE_MASK;
1265
1266         return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1267 }
1268
1269 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1270 {
1271         return &tfm->base;
1272 }
1273
1274 static inline void crypto_free_comp(struct crypto_comp *tfm)
1275 {
1276         crypto_free_tfm(crypto_comp_tfm(tfm));
1277 }
1278
1279 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1280 {
1281         type &= ~CRYPTO_ALG_TYPE_MASK;
1282         type |= CRYPTO_ALG_TYPE_COMPRESS;
1283         mask |= CRYPTO_ALG_TYPE_MASK;
1284
1285         return crypto_has_alg(alg_name, type, mask);
1286 }
1287
1288 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1289 {
1290         return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1291 }
1292
1293 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1294 {
1295         return &crypto_comp_tfm(tfm)->crt_compress;
1296 }
1297
1298 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1299                                        const u8 *src, unsigned int slen,
1300                                        u8 *dst, unsigned int *dlen)
1301 {
1302         return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1303                                                   src, slen, dst, dlen);
1304 }
1305
1306 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1307                                          const u8 *src, unsigned int slen,
1308                                          u8 *dst, unsigned int *dlen)
1309 {
1310         return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1311                                                     src, slen, dst, dlen);
1312 }
1313
1314 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
1315 {
1316         return (struct crypto_ahash *)tfm;
1317 }
1318
1319 static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name,
1320                                                       u32 type, u32 mask)
1321 {
1322         type &= ~CRYPTO_ALG_TYPE_MASK;
1323         mask &= ~CRYPTO_ALG_TYPE_MASK;
1324         type |= CRYPTO_ALG_TYPE_AHASH;
1325         mask |= CRYPTO_ALG_TYPE_AHASH_MASK;
1326
1327         return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask));
1328 }
1329
1330 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
1331 {
1332         return &tfm->base;
1333 }
1334
1335 static inline void crypto_free_ahash(struct crypto_ahash *tfm)
1336 {
1337         crypto_free_tfm(crypto_ahash_tfm(tfm));
1338 }
1339
1340 static inline unsigned int crypto_ahash_alignmask(
1341         struct crypto_ahash *tfm)
1342 {
1343         return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
1344 }
1345
1346 static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm)
1347 {
1348         return &crypto_ahash_tfm(tfm)->crt_ahash;
1349 }
1350
1351 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
1352 {
1353         return crypto_ahash_crt(tfm)->digestsize;
1354 }
1355
1356 static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
1357 {
1358         return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
1359 }
1360
1361 static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
1362 {
1363         crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
1364 }
1365
1366 static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
1367 {
1368         crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
1369 }
1370
1371 static inline struct crypto_ahash *crypto_ahash_reqtfm(
1372         struct ahash_request *req)
1373 {
1374         return __crypto_ahash_cast(req->base.tfm);
1375 }
1376
1377 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
1378 {
1379         return crypto_ahash_crt(tfm)->reqsize;
1380 }
1381
1382 static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
1383                                       const u8 *key, unsigned int keylen)
1384 {
1385         struct ahash_tfm *crt = crypto_ahash_crt(tfm);
1386
1387         return crt->setkey(crt->base, key, keylen);
1388 }
1389
1390 static inline int crypto_ahash_digest(struct ahash_request *req)
1391 {
1392         struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
1393         return crt->digest(req);
1394 }
1395
1396 static inline void ahash_request_set_tfm(struct ahash_request *req,
1397                                          struct crypto_ahash *tfm)
1398 {
1399         req->base.tfm = crypto_ahash_tfm(crypto_ahash_crt(tfm)->base);
1400 }
1401
1402 static inline struct ahash_request *ahash_request_alloc(
1403         struct crypto_ahash *tfm, gfp_t gfp)
1404 {
1405         struct ahash_request *req;
1406
1407         req = kmalloc(sizeof(struct ahash_request) +
1408                       crypto_ahash_reqsize(tfm), gfp);
1409
1410         if (likely(req))
1411                 ahash_request_set_tfm(req, tfm);
1412
1413         return req;
1414 }
1415
1416 static inline void ahash_request_free(struct ahash_request *req)
1417 {
1418         kfree(req);
1419 }
1420
1421 static inline struct ahash_request *ahash_request_cast(
1422         struct crypto_async_request *req)
1423 {
1424         return container_of(req, struct ahash_request, base);
1425 }
1426
1427 static inline void ahash_request_set_callback(struct ahash_request *req,
1428                                               u32 flags,
1429                                               crypto_completion_t complete,
1430                                               void *data)
1431 {
1432         req->base.complete = complete;
1433         req->base.data = data;
1434         req->base.flags = flags;
1435 }
1436
1437 static inline void ahash_request_set_crypt(struct ahash_request *req,
1438                                            struct scatterlist *src, u8 *result,
1439                                            unsigned int nbytes)
1440 {
1441         req->src = src;
1442         req->nbytes = nbytes;
1443         req->result = result;
1444 }
1445
1446 #endif  /* _LINUX_CRYPTO_H */
1447