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