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