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