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