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