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