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