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