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