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