• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/bug.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
27 #include <linux/completion.h>
28 
29 /*
30  * Autoloaded crypto modules should only use a prefixed name to avoid allowing
31  * arbitrary modules to be loaded. Loading from userspace may still need the
32  * unprefixed names, so retains those aliases as well.
33  * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
34  * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
35  * expands twice on the same line. Instead, use a separate base name for the
36  * alias.
37  */
38 #define MODULE_ALIAS_CRYPTO(name)	\
39 		__MODULE_INFO(alias, alias_userspace, name);	\
40 		__MODULE_INFO(alias, alias_crypto, "crypto-" name)
41 
42 /*
43  * Algorithm masks and types.
44  */
45 #define CRYPTO_ALG_TYPE_MASK		0x0000000f
46 #define CRYPTO_ALG_TYPE_CIPHER		0x00000001
47 #define CRYPTO_ALG_TYPE_COMPRESS	0x00000002
48 #define CRYPTO_ALG_TYPE_AEAD		0x00000003
49 #define CRYPTO_ALG_TYPE_BLKCIPHER	0x00000004
50 #define CRYPTO_ALG_TYPE_ABLKCIPHER	0x00000005
51 #define CRYPTO_ALG_TYPE_SKCIPHER	0x00000005
52 #define CRYPTO_ALG_TYPE_GIVCIPHER	0x00000006
53 #define CRYPTO_ALG_TYPE_KPP		0x00000008
54 #define CRYPTO_ALG_TYPE_ACOMPRESS	0x0000000a
55 #define CRYPTO_ALG_TYPE_SCOMPRESS	0x0000000b
56 #define CRYPTO_ALG_TYPE_RNG		0x0000000c
57 #define CRYPTO_ALG_TYPE_AKCIPHER	0x0000000d
58 #define CRYPTO_ALG_TYPE_DIGEST		0x0000000e
59 #define CRYPTO_ALG_TYPE_HASH		0x0000000e
60 #define CRYPTO_ALG_TYPE_SHASH		0x0000000e
61 #define CRYPTO_ALG_TYPE_AHASH		0x0000000f
62 
63 #define CRYPTO_ALG_TYPE_HASH_MASK	0x0000000e
64 #define CRYPTO_ALG_TYPE_AHASH_MASK	0x0000000e
65 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK	0x0000000c
66 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK	0x0000000e
67 
68 #define CRYPTO_ALG_LARVAL		0x00000010
69 #define CRYPTO_ALG_DEAD			0x00000020
70 #define CRYPTO_ALG_DYING		0x00000040
71 #define CRYPTO_ALG_ASYNC		0x00000080
72 
73 /*
74  * Set this bit if and only if the algorithm requires another algorithm of
75  * the same type to handle corner cases.
76  */
77 #define CRYPTO_ALG_NEED_FALLBACK	0x00000100
78 
79 /*
80  * This bit is set for symmetric key ciphers that have already been wrapped
81  * with a generic IV generator to prevent them from being wrapped again.
82  */
83 #define CRYPTO_ALG_GENIV		0x00000200
84 
85 /*
86  * Set if the algorithm has passed automated run-time testing.  Note that
87  * if there is no run-time testing for a given algorithm it is considered
88  * to have passed.
89  */
90 
91 #define CRYPTO_ALG_TESTED		0x00000400
92 
93 /*
94  * Set if the algorithm is an instance that is built from templates.
95  */
96 #define CRYPTO_ALG_INSTANCE		0x00000800
97 
98 /* Set this bit if the algorithm provided is hardware accelerated but
99  * not available to userspace via instruction set or so.
100  */
101 #define CRYPTO_ALG_KERN_DRIVER_ONLY	0x00001000
102 
103 /*
104  * Mark a cipher as a service implementation only usable by another
105  * cipher and never by a normal user of the kernel crypto API
106  */
107 #define CRYPTO_ALG_INTERNAL		0x00002000
108 
109 /*
110  * Set if the algorithm has a ->setkey() method but can be used without
111  * calling it first, i.e. there is a default key.
112  */
113 #define CRYPTO_ALG_OPTIONAL_KEY		0x00004000
114 
115 /*
116  * Don't trigger module loading
117  */
118 #define CRYPTO_NOLOAD			0x00008000
119 
120 /*
121  * Transform masks and values (for crt_flags).
122  */
123 #define CRYPTO_TFM_NEED_KEY		0x00000001
124 
125 #define CRYPTO_TFM_REQ_MASK		0x000fff00
126 #define CRYPTO_TFM_RES_MASK		0xfff00000
127 
128 #define CRYPTO_TFM_REQ_WEAK_KEY		0x00000100
129 #define CRYPTO_TFM_REQ_MAY_SLEEP	0x00000200
130 #define CRYPTO_TFM_REQ_MAY_BACKLOG	0x00000400
131 #define CRYPTO_TFM_RES_WEAK_KEY		0x00100000
132 #define CRYPTO_TFM_RES_BAD_KEY_LEN   	0x00200000
133 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 	0x00400000
134 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 	0x00800000
135 #define CRYPTO_TFM_RES_BAD_FLAGS 	0x01000000
136 
137 /*
138  * Miscellaneous stuff.
139  */
140 #define CRYPTO_MAX_ALG_NAME		128
141 
142 /*
143  * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
144  * declaration) is used to ensure that the crypto_tfm context structure is
145  * aligned correctly for the given architecture so that there are no alignment
146  * faults for C data types.  In particular, this is required on platforms such
147  * as arm where pointers are 32-bit aligned but there are data types such as
148  * u64 which require 64-bit alignment.
149  */
150 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
151 
152 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
153 
154 struct scatterlist;
155 struct crypto_ablkcipher;
156 struct crypto_async_request;
157 struct crypto_blkcipher;
158 struct crypto_tfm;
159 struct crypto_type;
160 struct skcipher_givcrypt_request;
161 
162 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
163 
164 /**
165  * DOC: Block Cipher Context Data Structures
166  *
167  * These data structures define the operating context for each block cipher
168  * type.
169  */
170 
171 struct crypto_async_request {
172 	struct list_head list;
173 	crypto_completion_t complete;
174 	void *data;
175 	struct crypto_tfm *tfm;
176 
177 	u32 flags;
178 };
179 
180 struct ablkcipher_request {
181 	struct crypto_async_request base;
182 
183 	unsigned int nbytes;
184 
185 	void *info;
186 
187 	struct scatterlist *src;
188 	struct scatterlist *dst;
189 
190 	void *__ctx[] CRYPTO_MINALIGN_ATTR;
191 };
192 
193 struct blkcipher_desc {
194 	struct crypto_blkcipher *tfm;
195 	void *info;
196 	u32 flags;
197 };
198 
199 struct cipher_desc {
200 	struct crypto_tfm *tfm;
201 	void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
202 	unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
203 			     const u8 *src, unsigned int nbytes);
204 	void *info;
205 };
206 
207 /**
208  * DOC: Block Cipher Algorithm Definitions
209  *
210  * These data structures define modular crypto algorithm implementations,
211  * managed via crypto_register_alg() and crypto_unregister_alg().
212  */
213 
214 /**
215  * struct ablkcipher_alg - asynchronous block cipher definition
216  * @min_keysize: Minimum key size supported by the transformation. This is the
217  *		 smallest key length supported by this transformation algorithm.
218  *		 This must be set to one of the pre-defined values as this is
219  *		 not hardware specific. Possible values for this field can be
220  *		 found via git grep "_MIN_KEY_SIZE" include/crypto/
221  * @max_keysize: Maximum key size supported by the transformation. This is the
222  *		 largest key length supported by this transformation algorithm.
223  *		 This must be set to one of the pre-defined values as this is
224  *		 not hardware specific. Possible values for this field can be
225  *		 found via git grep "_MAX_KEY_SIZE" include/crypto/
226  * @setkey: Set key for the transformation. This function is used to either
227  *	    program a supplied key into the hardware or store the key in the
228  *	    transformation context for programming it later. Note that this
229  *	    function does modify the transformation context. This function can
230  *	    be called multiple times during the existence of the transformation
231  *	    object, so one must make sure the key is properly reprogrammed into
232  *	    the hardware. This function is also responsible for checking the key
233  *	    length for validity. In case a software fallback was put in place in
234  *	    the @cra_init call, this function might need to use the fallback if
235  *	    the algorithm doesn't support all of the key sizes.
236  * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
237  *	     the supplied scatterlist containing the blocks of data. The crypto
238  *	     API consumer is responsible for aligning the entries of the
239  *	     scatterlist properly and making sure the chunks are correctly
240  *	     sized. In case a software fallback was put in place in the
241  *	     @cra_init call, this function might need to use the fallback if
242  *	     the algorithm doesn't support all of the key sizes. In case the
243  *	     key was stored in transformation context, the key might need to be
244  *	     re-programmed into the hardware in this function. This function
245  *	     shall not modify the transformation context, as this function may
246  *	     be called in parallel with the same transformation object.
247  * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
248  *	     and the conditions are exactly the same.
249  * @givencrypt: Update the IV for encryption. With this function, a cipher
250  *	        implementation may provide the function on how to update the IV
251  *	        for encryption.
252  * @givdecrypt: Update the IV for decryption. This is the reverse of
253  *	        @givencrypt .
254  * @geniv: The transformation implementation may use an "IV generator" provided
255  *	   by the kernel crypto API. Several use cases have a predefined
256  *	   approach how IVs are to be updated. For such use cases, the kernel
257  *	   crypto API provides ready-to-use implementations that can be
258  *	   referenced with this variable.
259  * @ivsize: IV size applicable for transformation. The consumer must provide an
260  *	    IV of exactly that size to perform the encrypt or decrypt operation.
261  *
262  * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
263  * mandatory and must be filled.
264  */
265 struct ablkcipher_alg {
266 	int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
267 	              unsigned int keylen);
268 	int (*encrypt)(struct ablkcipher_request *req);
269 	int (*decrypt)(struct ablkcipher_request *req);
270 	int (*givencrypt)(struct skcipher_givcrypt_request *req);
271 	int (*givdecrypt)(struct skcipher_givcrypt_request *req);
272 
273 	const char *geniv;
274 
275 	unsigned int min_keysize;
276 	unsigned int max_keysize;
277 	unsigned int ivsize;
278 };
279 
280 /**
281  * struct blkcipher_alg - synchronous block cipher definition
282  * @min_keysize: see struct ablkcipher_alg
283  * @max_keysize: see struct ablkcipher_alg
284  * @setkey: see struct ablkcipher_alg
285  * @encrypt: see struct ablkcipher_alg
286  * @decrypt: see struct ablkcipher_alg
287  * @geniv: see struct ablkcipher_alg
288  * @ivsize: see struct ablkcipher_alg
289  *
290  * All fields except @geniv and @ivsize are mandatory and must be filled.
291  */
292 struct blkcipher_alg {
293 	int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
294 	              unsigned int keylen);
295 	int (*encrypt)(struct blkcipher_desc *desc,
296 		       struct scatterlist *dst, struct scatterlist *src,
297 		       unsigned int nbytes);
298 	int (*decrypt)(struct blkcipher_desc *desc,
299 		       struct scatterlist *dst, struct scatterlist *src,
300 		       unsigned int nbytes);
301 
302 	const char *geniv;
303 
304 	unsigned int min_keysize;
305 	unsigned int max_keysize;
306 	unsigned int ivsize;
307 };
308 
309 /**
310  * struct cipher_alg - single-block symmetric ciphers definition
311  * @cia_min_keysize: Minimum key size supported by the transformation. This is
312  *		     the smallest key length supported by this transformation
313  *		     algorithm. This must be set to one of the pre-defined
314  *		     values as this is not hardware specific. Possible values
315  *		     for this field can be found via git grep "_MIN_KEY_SIZE"
316  *		     include/crypto/
317  * @cia_max_keysize: Maximum key size supported by the transformation. This is
318  *		    the largest key length supported by this transformation
319  *		    algorithm. This must be set to one of the pre-defined values
320  *		    as this is not hardware specific. Possible values for this
321  *		    field can be found via git grep "_MAX_KEY_SIZE"
322  *		    include/crypto/
323  * @cia_setkey: Set key for the transformation. This function is used to either
324  *	        program a supplied key into the hardware or store the key in the
325  *	        transformation context for programming it later. Note that this
326  *	        function does modify the transformation context. This function
327  *	        can be called multiple times during the existence of the
328  *	        transformation object, so one must make sure the key is properly
329  *	        reprogrammed into the hardware. This function is also
330  *	        responsible for checking the key length for validity.
331  * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
332  *		 single block of data, which must be @cra_blocksize big. This
333  *		 always operates on a full @cra_blocksize and it is not possible
334  *		 to encrypt a block of smaller size. The supplied buffers must
335  *		 therefore also be at least of @cra_blocksize size. Both the
336  *		 input and output buffers are always aligned to @cra_alignmask.
337  *		 In case either of the input or output buffer supplied by user
338  *		 of the crypto API is not aligned to @cra_alignmask, the crypto
339  *		 API will re-align the buffers. The re-alignment means that a
340  *		 new buffer will be allocated, the data will be copied into the
341  *		 new buffer, then the processing will happen on the new buffer,
342  *		 then the data will be copied back into the original buffer and
343  *		 finally the new buffer will be freed. In case a software
344  *		 fallback was put in place in the @cra_init call, this function
345  *		 might need to use the fallback if the algorithm doesn't support
346  *		 all of the key sizes. In case the key was stored in
347  *		 transformation context, the key might need to be re-programmed
348  *		 into the hardware in this function. This function shall not
349  *		 modify the transformation context, as this function may be
350  *		 called in parallel with the same transformation object.
351  * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
352  *		 @cia_encrypt, and the conditions are exactly the same.
353  *
354  * All fields are mandatory and must be filled.
355  */
356 struct cipher_alg {
357 	unsigned int cia_min_keysize;
358 	unsigned int cia_max_keysize;
359 	int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
360 	                  unsigned int keylen);
361 	void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
362 	void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
363 };
364 
365 struct compress_alg {
366 	int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
367 			    unsigned int slen, u8 *dst, unsigned int *dlen);
368 	int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
369 			      unsigned int slen, u8 *dst, unsigned int *dlen);
370 };
371 
372 
373 #define cra_ablkcipher	cra_u.ablkcipher
374 #define cra_blkcipher	cra_u.blkcipher
375 #define cra_cipher	cra_u.cipher
376 #define cra_compress	cra_u.compress
377 
378 /**
379  * struct crypto_alg - definition of a cryptograpic cipher algorithm
380  * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
381  *	       CRYPTO_ALG_* flags for the flags which go in here. Those are
382  *	       used for fine-tuning the description of the transformation
383  *	       algorithm.
384  * @cra_blocksize: Minimum block size of this transformation. The size in bytes
385  *		   of the smallest possible unit which can be transformed with
386  *		   this algorithm. The users must respect this value.
387  *		   In case of HASH transformation, it is possible for a smaller
388  *		   block than @cra_blocksize to be passed to the crypto API for
389  *		   transformation, in case of any other transformation type, an
390  * 		   error will be returned upon any attempt to transform smaller
391  *		   than @cra_blocksize chunks.
392  * @cra_ctxsize: Size of the operational context of the transformation. This
393  *		 value informs the kernel crypto API about the memory size
394  *		 needed to be allocated for the transformation context.
395  * @cra_alignmask: Alignment mask for the input and output data buffer. The data
396  *		   buffer containing the input data for the algorithm must be
397  *		   aligned to this alignment mask. The data buffer for the
398  *		   output data must be aligned to this alignment mask. Note that
399  *		   the Crypto API will do the re-alignment in software, but
400  *		   only under special conditions and there is a performance hit.
401  *		   The re-alignment happens at these occasions for different
402  *		   @cra_u types: cipher -- For both input data and output data
403  *		   buffer; ahash -- For output hash destination buf; shash --
404  *		   For output hash destination buf.
405  *		   This is needed on hardware which is flawed by design and
406  *		   cannot pick data from arbitrary addresses.
407  * @cra_priority: Priority of this transformation implementation. In case
408  *		  multiple transformations with same @cra_name are available to
409  *		  the Crypto API, the kernel will use the one with highest
410  *		  @cra_priority.
411  * @cra_name: Generic name (usable by multiple implementations) of the
412  *	      transformation algorithm. This is the name of the transformation
413  *	      itself. This field is used by the kernel when looking up the
414  *	      providers of particular transformation.
415  * @cra_driver_name: Unique name of the transformation provider. This is the
416  *		     name of the provider of the transformation. This can be any
417  *		     arbitrary value, but in the usual case, this contains the
418  *		     name of the chip or provider and the name of the
419  *		     transformation algorithm.
420  * @cra_type: Type of the cryptographic transformation. This is a pointer to
421  *	      struct crypto_type, which implements callbacks common for all
422  *	      transformation types. There are multiple options:
423  *	      &crypto_blkcipher_type, &crypto_ablkcipher_type,
424  *	      &crypto_ahash_type, &crypto_rng_type.
425  *	      This field might be empty. In that case, there are no common
426  *	      callbacks. This is the case for: cipher, compress, shash.
427  * @cra_u: Callbacks implementing the transformation. This is a union of
428  *	   multiple structures. Depending on the type of transformation selected
429  *	   by @cra_type and @cra_flags above, the associated structure must be
430  *	   filled with callbacks. This field might be empty. This is the case
431  *	   for ahash, shash.
432  * @cra_init: Initialize the cryptographic transformation object. This function
433  *	      is used to initialize the cryptographic transformation object.
434  *	      This function is called only once at the instantiation time, right
435  *	      after the transformation context was allocated. In case the
436  *	      cryptographic hardware has some special requirements which need to
437  *	      be handled by software, this function shall check for the precise
438  *	      requirement of the transformation and put any software fallbacks
439  *	      in place.
440  * @cra_exit: Deinitialize the cryptographic transformation object. This is a
441  *	      counterpart to @cra_init, used to remove various changes set in
442  *	      @cra_init.
443  * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
444  * @cra_list: internally used
445  * @cra_users: internally used
446  * @cra_refcnt: internally used
447  * @cra_destroy: internally used
448  *
449  * The struct crypto_alg describes a generic Crypto API algorithm and is common
450  * for all of the transformations. Any variable not documented here shall not
451  * be used by a cipher implementation as it is internal to the Crypto API.
452  */
453 struct crypto_alg {
454 	struct list_head cra_list;
455 	struct list_head cra_users;
456 
457 	u32 cra_flags;
458 	unsigned int cra_blocksize;
459 	unsigned int cra_ctxsize;
460 	unsigned int cra_alignmask;
461 
462 	int cra_priority;
463 	atomic_t cra_refcnt;
464 
465 	char cra_name[CRYPTO_MAX_ALG_NAME];
466 	char cra_driver_name[CRYPTO_MAX_ALG_NAME];
467 
468 	const struct crypto_type *cra_type;
469 
470 	union {
471 		struct ablkcipher_alg ablkcipher;
472 		struct blkcipher_alg blkcipher;
473 		struct cipher_alg cipher;
474 		struct compress_alg compress;
475 	} cra_u;
476 
477 	int (*cra_init)(struct crypto_tfm *tfm);
478 	void (*cra_exit)(struct crypto_tfm *tfm);
479 	void (*cra_destroy)(struct crypto_alg *alg);
480 
481 	struct module *cra_module;
482 } CRYPTO_MINALIGN_ATTR;
483 
484 /*
485  * A helper struct for waiting for completion of async crypto ops
486  */
487 struct crypto_wait {
488 	struct completion completion;
489 	int err;
490 };
491 
492 /*
493  * Macro for declaring a crypto op async wait object on stack
494  */
495 #define DECLARE_CRYPTO_WAIT(_wait) \
496 	struct crypto_wait _wait = { \
497 		COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
498 
499 /*
500  * Async ops completion helper functioons
501  */
502 void crypto_req_done(struct crypto_async_request *req, int err);
503 
crypto_wait_req(int err,struct crypto_wait * wait)504 static inline int crypto_wait_req(int err, struct crypto_wait *wait)
505 {
506 	switch (err) {
507 	case -EINPROGRESS:
508 	case -EBUSY:
509 		wait_for_completion(&wait->completion);
510 		reinit_completion(&wait->completion);
511 		err = wait->err;
512 		break;
513 	};
514 
515 	return err;
516 }
517 
crypto_init_wait(struct crypto_wait * wait)518 static inline void crypto_init_wait(struct crypto_wait *wait)
519 {
520 	init_completion(&wait->completion);
521 }
522 
523 /*
524  * Algorithm registration interface.
525  */
526 int crypto_register_alg(struct crypto_alg *alg);
527 int crypto_unregister_alg(struct crypto_alg *alg);
528 int crypto_register_algs(struct crypto_alg *algs, int count);
529 int crypto_unregister_algs(struct crypto_alg *algs, int count);
530 
531 /*
532  * Algorithm query interface.
533  */
534 int crypto_has_alg(const char *name, u32 type, u32 mask);
535 
536 /*
537  * Transforms: user-instantiated objects which encapsulate algorithms
538  * and core processing logic.  Managed via crypto_alloc_*() and
539  * crypto_free_*(), as well as the various helpers below.
540  */
541 
542 struct ablkcipher_tfm {
543 	int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
544 	              unsigned int keylen);
545 	int (*encrypt)(struct ablkcipher_request *req);
546 	int (*decrypt)(struct ablkcipher_request *req);
547 
548 	struct crypto_ablkcipher *base;
549 
550 	unsigned int ivsize;
551 	unsigned int reqsize;
552 };
553 
554 struct blkcipher_tfm {
555 	void *iv;
556 	int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
557 		      unsigned int keylen);
558 	int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
559 		       struct scatterlist *src, unsigned int nbytes);
560 	int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
561 		       struct scatterlist *src, unsigned int nbytes);
562 };
563 
564 struct cipher_tfm {
565 	int (*cit_setkey)(struct crypto_tfm *tfm,
566 	                  const u8 *key, unsigned int keylen);
567 	void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
568 	void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
569 };
570 
571 struct compress_tfm {
572 	int (*cot_compress)(struct crypto_tfm *tfm,
573 	                    const u8 *src, unsigned int slen,
574 	                    u8 *dst, unsigned int *dlen);
575 	int (*cot_decompress)(struct crypto_tfm *tfm,
576 	                      const u8 *src, unsigned int slen,
577 	                      u8 *dst, unsigned int *dlen);
578 };
579 
580 #define crt_ablkcipher	crt_u.ablkcipher
581 #define crt_blkcipher	crt_u.blkcipher
582 #define crt_cipher	crt_u.cipher
583 #define crt_compress	crt_u.compress
584 
585 struct crypto_tfm {
586 
587 	u32 crt_flags;
588 
589 	union {
590 		struct ablkcipher_tfm ablkcipher;
591 		struct blkcipher_tfm blkcipher;
592 		struct cipher_tfm cipher;
593 		struct compress_tfm compress;
594 	} crt_u;
595 
596 	void (*exit)(struct crypto_tfm *tfm);
597 
598 	struct crypto_alg *__crt_alg;
599 
600 	void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
601 };
602 
603 struct crypto_ablkcipher {
604 	struct crypto_tfm base;
605 };
606 
607 struct crypto_blkcipher {
608 	struct crypto_tfm base;
609 };
610 
611 struct crypto_cipher {
612 	struct crypto_tfm base;
613 };
614 
615 struct crypto_comp {
616 	struct crypto_tfm base;
617 };
618 
619 enum {
620 	CRYPTOA_UNSPEC,
621 	CRYPTOA_ALG,
622 	CRYPTOA_TYPE,
623 	CRYPTOA_U32,
624 	__CRYPTOA_MAX,
625 };
626 
627 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
628 
629 /* Maximum number of (rtattr) parameters for each template. */
630 #define CRYPTO_MAX_ATTRS 32
631 
632 struct crypto_attr_alg {
633 	char name[CRYPTO_MAX_ALG_NAME];
634 };
635 
636 struct crypto_attr_type {
637 	u32 type;
638 	u32 mask;
639 };
640 
641 struct crypto_attr_u32 {
642 	u32 num;
643 };
644 
645 /*
646  * Transform user interface.
647  */
648 
649 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
650 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
651 
crypto_free_tfm(struct crypto_tfm * tfm)652 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
653 {
654 	return crypto_destroy_tfm(tfm, tfm);
655 }
656 
657 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
658 
659 /*
660  * Transform helpers which query the underlying algorithm.
661  */
crypto_tfm_alg_name(struct crypto_tfm * tfm)662 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
663 {
664 	return tfm->__crt_alg->cra_name;
665 }
666 
crypto_tfm_alg_driver_name(struct crypto_tfm * tfm)667 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
668 {
669 	return tfm->__crt_alg->cra_driver_name;
670 }
671 
crypto_tfm_alg_priority(struct crypto_tfm * tfm)672 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
673 {
674 	return tfm->__crt_alg->cra_priority;
675 }
676 
crypto_tfm_alg_type(struct crypto_tfm * tfm)677 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
678 {
679 	return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
680 }
681 
crypto_tfm_alg_blocksize(struct crypto_tfm * tfm)682 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
683 {
684 	return tfm->__crt_alg->cra_blocksize;
685 }
686 
crypto_tfm_alg_alignmask(struct crypto_tfm * tfm)687 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
688 {
689 	return tfm->__crt_alg->cra_alignmask;
690 }
691 
crypto_tfm_get_flags(struct crypto_tfm * tfm)692 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
693 {
694 	return tfm->crt_flags;
695 }
696 
crypto_tfm_set_flags(struct crypto_tfm * tfm,u32 flags)697 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
698 {
699 	tfm->crt_flags |= flags;
700 }
701 
crypto_tfm_clear_flags(struct crypto_tfm * tfm,u32 flags)702 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
703 {
704 	tfm->crt_flags &= ~flags;
705 }
706 
crypto_tfm_ctx(struct crypto_tfm * tfm)707 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
708 {
709 	return tfm->__crt_ctx;
710 }
711 
crypto_tfm_ctx_alignment(void)712 static inline unsigned int crypto_tfm_ctx_alignment(void)
713 {
714 	struct crypto_tfm *tfm;
715 	return __alignof__(tfm->__crt_ctx);
716 }
717 
718 /*
719  * API wrappers.
720  */
__crypto_ablkcipher_cast(struct crypto_tfm * tfm)721 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
722 	struct crypto_tfm *tfm)
723 {
724 	return (struct crypto_ablkcipher *)tfm;
725 }
726 
crypto_skcipher_type(u32 type)727 static inline u32 crypto_skcipher_type(u32 type)
728 {
729 	type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
730 	type |= CRYPTO_ALG_TYPE_BLKCIPHER;
731 	return type;
732 }
733 
crypto_skcipher_mask(u32 mask)734 static inline u32 crypto_skcipher_mask(u32 mask)
735 {
736 	mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
737 	mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
738 	return mask;
739 }
740 
741 /**
742  * DOC: Asynchronous Block Cipher API
743  *
744  * Asynchronous block cipher API is used with the ciphers of type
745  * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
746  *
747  * Asynchronous cipher operations imply that the function invocation for a
748  * cipher request returns immediately before the completion of the operation.
749  * The cipher request is scheduled as a separate kernel thread and therefore
750  * load-balanced on the different CPUs via the process scheduler. To allow
751  * the kernel crypto API to inform the caller about the completion of a cipher
752  * request, the caller must provide a callback function. That function is
753  * invoked with the cipher handle when the request completes.
754  *
755  * To support the asynchronous operation, additional information than just the
756  * cipher handle must be supplied to the kernel crypto API. That additional
757  * information is given by filling in the ablkcipher_request data structure.
758  *
759  * For the asynchronous block cipher API, the state is maintained with the tfm
760  * cipher handle. A single tfm can be used across multiple calls and in
761  * parallel. For asynchronous block cipher calls, context data supplied and
762  * only used by the caller can be referenced the request data structure in
763  * addition to the IV used for the cipher request. The maintenance of such
764  * state information would be important for a crypto driver implementer to
765  * have, because when calling the callback function upon completion of the
766  * cipher operation, that callback function may need some information about
767  * which operation just finished if it invoked multiple in parallel. This
768  * state information is unused by the kernel crypto API.
769  */
770 
crypto_ablkcipher_tfm(struct crypto_ablkcipher * tfm)771 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
772 	struct crypto_ablkcipher *tfm)
773 {
774 	return &tfm->base;
775 }
776 
777 /**
778  * crypto_free_ablkcipher() - zeroize and free cipher handle
779  * @tfm: cipher handle to be freed
780  */
crypto_free_ablkcipher(struct crypto_ablkcipher * tfm)781 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
782 {
783 	crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
784 }
785 
786 /**
787  * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
788  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
789  *	      ablkcipher
790  * @type: specifies the type of the cipher
791  * @mask: specifies the mask for the cipher
792  *
793  * Return: true when the ablkcipher is known to the kernel crypto API; false
794  *	   otherwise
795  */
crypto_has_ablkcipher(const char * alg_name,u32 type,u32 mask)796 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
797 					u32 mask)
798 {
799 	return crypto_has_alg(alg_name, crypto_skcipher_type(type),
800 			      crypto_skcipher_mask(mask));
801 }
802 
crypto_ablkcipher_crt(struct crypto_ablkcipher * tfm)803 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
804 	struct crypto_ablkcipher *tfm)
805 {
806 	return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
807 }
808 
809 /**
810  * crypto_ablkcipher_ivsize() - obtain IV size
811  * @tfm: cipher handle
812  *
813  * The size of the IV for the ablkcipher referenced by the cipher handle is
814  * returned. This IV size may be zero if the cipher does not need an IV.
815  *
816  * Return: IV size in bytes
817  */
crypto_ablkcipher_ivsize(struct crypto_ablkcipher * tfm)818 static inline unsigned int crypto_ablkcipher_ivsize(
819 	struct crypto_ablkcipher *tfm)
820 {
821 	return crypto_ablkcipher_crt(tfm)->ivsize;
822 }
823 
824 /**
825  * crypto_ablkcipher_blocksize() - obtain block size of cipher
826  * @tfm: cipher handle
827  *
828  * The block size for the ablkcipher referenced with the cipher handle is
829  * returned. The caller may use that information to allocate appropriate
830  * memory for the data returned by the encryption or decryption operation
831  *
832  * Return: block size of cipher
833  */
crypto_ablkcipher_blocksize(struct crypto_ablkcipher * tfm)834 static inline unsigned int crypto_ablkcipher_blocksize(
835 	struct crypto_ablkcipher *tfm)
836 {
837 	return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
838 }
839 
crypto_ablkcipher_alignmask(struct crypto_ablkcipher * tfm)840 static inline unsigned int crypto_ablkcipher_alignmask(
841 	struct crypto_ablkcipher *tfm)
842 {
843 	return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
844 }
845 
crypto_ablkcipher_get_flags(struct crypto_ablkcipher * tfm)846 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
847 {
848 	return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
849 }
850 
crypto_ablkcipher_set_flags(struct crypto_ablkcipher * tfm,u32 flags)851 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
852 					       u32 flags)
853 {
854 	crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
855 }
856 
crypto_ablkcipher_clear_flags(struct crypto_ablkcipher * tfm,u32 flags)857 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
858 						 u32 flags)
859 {
860 	crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
861 }
862 
863 /**
864  * crypto_ablkcipher_setkey() - set key for cipher
865  * @tfm: cipher handle
866  * @key: buffer holding the key
867  * @keylen: length of the key in bytes
868  *
869  * The caller provided key is set for the ablkcipher referenced by the cipher
870  * handle.
871  *
872  * Note, the key length determines the cipher type. Many block ciphers implement
873  * different cipher modes depending on the key size, such as AES-128 vs AES-192
874  * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
875  * is performed.
876  *
877  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
878  */
crypto_ablkcipher_setkey(struct crypto_ablkcipher * tfm,const u8 * key,unsigned int keylen)879 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
880 					   const u8 *key, unsigned int keylen)
881 {
882 	struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
883 
884 	return crt->setkey(crt->base, key, keylen);
885 }
886 
887 /**
888  * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
889  * @req: ablkcipher_request out of which the cipher handle is to be obtained
890  *
891  * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
892  * data structure.
893  *
894  * Return: crypto_ablkcipher handle
895  */
crypto_ablkcipher_reqtfm(struct ablkcipher_request * req)896 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
897 	struct ablkcipher_request *req)
898 {
899 	return __crypto_ablkcipher_cast(req->base.tfm);
900 }
901 
902 /**
903  * crypto_ablkcipher_encrypt() - encrypt plaintext
904  * @req: reference to the ablkcipher_request handle that holds all information
905  *	 needed to perform the cipher operation
906  *
907  * Encrypt plaintext data using the ablkcipher_request handle. That data
908  * structure and how it is filled with data is discussed with the
909  * ablkcipher_request_* functions.
910  *
911  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
912  */
crypto_ablkcipher_encrypt(struct ablkcipher_request * req)913 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
914 {
915 	struct ablkcipher_tfm *crt =
916 		crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
917 	return crt->encrypt(req);
918 }
919 
920 /**
921  * crypto_ablkcipher_decrypt() - decrypt ciphertext
922  * @req: reference to the ablkcipher_request handle that holds all information
923  *	 needed to perform the cipher operation
924  *
925  * Decrypt ciphertext data using the ablkcipher_request handle. That data
926  * structure and how it is filled with data is discussed with the
927  * ablkcipher_request_* functions.
928  *
929  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
930  */
crypto_ablkcipher_decrypt(struct ablkcipher_request * req)931 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
932 {
933 	struct ablkcipher_tfm *crt =
934 		crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
935 	return crt->decrypt(req);
936 }
937 
938 /**
939  * DOC: Asynchronous Cipher Request Handle
940  *
941  * The ablkcipher_request data structure contains all pointers to data
942  * required for the asynchronous cipher operation. This includes the cipher
943  * handle (which can be used by multiple ablkcipher_request instances), pointer
944  * to plaintext and ciphertext, asynchronous callback function, etc. It acts
945  * as a handle to the ablkcipher_request_* API calls in a similar way as
946  * ablkcipher handle to the crypto_ablkcipher_* API calls.
947  */
948 
949 /**
950  * crypto_ablkcipher_reqsize() - obtain size of the request data structure
951  * @tfm: cipher handle
952  *
953  * Return: number of bytes
954  */
crypto_ablkcipher_reqsize(struct crypto_ablkcipher * tfm)955 static inline unsigned int crypto_ablkcipher_reqsize(
956 	struct crypto_ablkcipher *tfm)
957 {
958 	return crypto_ablkcipher_crt(tfm)->reqsize;
959 }
960 
961 /**
962  * ablkcipher_request_set_tfm() - update cipher handle reference in request
963  * @req: request handle to be modified
964  * @tfm: cipher handle that shall be added to the request handle
965  *
966  * Allow the caller to replace the existing ablkcipher handle in the request
967  * data structure with a different one.
968  */
ablkcipher_request_set_tfm(struct ablkcipher_request * req,struct crypto_ablkcipher * tfm)969 static inline void ablkcipher_request_set_tfm(
970 	struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
971 {
972 	req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
973 }
974 
ablkcipher_request_cast(struct crypto_async_request * req)975 static inline struct ablkcipher_request *ablkcipher_request_cast(
976 	struct crypto_async_request *req)
977 {
978 	return container_of(req, struct ablkcipher_request, base);
979 }
980 
981 /**
982  * ablkcipher_request_alloc() - allocate request data structure
983  * @tfm: cipher handle to be registered with the request
984  * @gfp: memory allocation flag that is handed to kmalloc by the API call.
985  *
986  * Allocate the request data structure that must be used with the ablkcipher
987  * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
988  * handle is registered in the request data structure.
989  *
990  * Return: allocated request handle in case of success, or NULL if out of memory
991  */
ablkcipher_request_alloc(struct crypto_ablkcipher * tfm,gfp_t gfp)992 static inline struct ablkcipher_request *ablkcipher_request_alloc(
993 	struct crypto_ablkcipher *tfm, gfp_t gfp)
994 {
995 	struct ablkcipher_request *req;
996 
997 	req = kmalloc(sizeof(struct ablkcipher_request) +
998 		      crypto_ablkcipher_reqsize(tfm), gfp);
999 
1000 	if (likely(req))
1001 		ablkcipher_request_set_tfm(req, tfm);
1002 
1003 	return req;
1004 }
1005 
1006 /**
1007  * ablkcipher_request_free() - zeroize and free request data structure
1008  * @req: request data structure cipher handle to be freed
1009  */
ablkcipher_request_free(struct ablkcipher_request * req)1010 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1011 {
1012 	kzfree(req);
1013 }
1014 
1015 /**
1016  * ablkcipher_request_set_callback() - set asynchronous callback function
1017  * @req: request handle
1018  * @flags: specify zero or an ORing of the flags
1019  *	   CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1020  *	   increase the wait queue beyond the initial maximum size;
1021  *	   CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1022  * @compl: callback function pointer to be registered with the request handle
1023  * @data: The data pointer refers to memory that is not used by the kernel
1024  *	  crypto API, but provided to the callback function for it to use. Here,
1025  *	  the caller can provide a reference to memory the callback function can
1026  *	  operate on. As the callback function is invoked asynchronously to the
1027  *	  related functionality, it may need to access data structures of the
1028  *	  related functionality which can be referenced using this pointer. The
1029  *	  callback function can access the memory via the "data" field in the
1030  *	  crypto_async_request data structure provided to the callback function.
1031  *
1032  * This function allows setting the callback function that is triggered once the
1033  * cipher operation completes.
1034  *
1035  * The callback function is registered with the ablkcipher_request handle and
1036  * must comply with the following template::
1037  *
1038  *	void callback_function(struct crypto_async_request *req, int error)
1039  */
ablkcipher_request_set_callback(struct ablkcipher_request * req,u32 flags,crypto_completion_t compl,void * data)1040 static inline void ablkcipher_request_set_callback(
1041 	struct ablkcipher_request *req,
1042 	u32 flags, crypto_completion_t compl, void *data)
1043 {
1044 	req->base.complete = compl;
1045 	req->base.data = data;
1046 	req->base.flags = flags;
1047 }
1048 
1049 /**
1050  * ablkcipher_request_set_crypt() - set data buffers
1051  * @req: request handle
1052  * @src: source scatter / gather list
1053  * @dst: destination scatter / gather list
1054  * @nbytes: number of bytes to process from @src
1055  * @iv: IV for the cipher operation which must comply with the IV size defined
1056  *      by crypto_ablkcipher_ivsize
1057  *
1058  * This function allows setting of the source data and destination data
1059  * scatter / gather lists.
1060  *
1061  * For encryption, the source is treated as the plaintext and the
1062  * destination is the ciphertext. For a decryption operation, the use is
1063  * reversed - the source is the ciphertext and the destination is the plaintext.
1064  */
ablkcipher_request_set_crypt(struct ablkcipher_request * req,struct scatterlist * src,struct scatterlist * dst,unsigned int nbytes,void * iv)1065 static inline void ablkcipher_request_set_crypt(
1066 	struct ablkcipher_request *req,
1067 	struct scatterlist *src, struct scatterlist *dst,
1068 	unsigned int nbytes, void *iv)
1069 {
1070 	req->src = src;
1071 	req->dst = dst;
1072 	req->nbytes = nbytes;
1073 	req->info = iv;
1074 }
1075 
1076 /**
1077  * DOC: Synchronous Block Cipher API
1078  *
1079  * The synchronous block cipher API is used with the ciphers of type
1080  * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1081  *
1082  * Synchronous calls, have a context in the tfm. But since a single tfm can be
1083  * used in multiple calls and in parallel, this info should not be changeable
1084  * (unless a lock is used). This applies, for example, to the symmetric key.
1085  * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1086  * structure for synchronous blkcipher api. So, its the only state info that can
1087  * be kept for synchronous calls without using a big lock across a tfm.
1088  *
1089  * The block cipher API allows the use of a complete cipher, i.e. a cipher
1090  * consisting of a template (a block chaining mode) and a single block cipher
1091  * primitive (e.g. AES).
1092  *
1093  * The plaintext data buffer and the ciphertext data buffer are pointed to
1094  * by using scatter/gather lists. The cipher operation is performed
1095  * on all segments of the provided scatter/gather lists.
1096  *
1097  * The kernel crypto API supports a cipher operation "in-place" which means that
1098  * the caller may provide the same scatter/gather list for the plaintext and
1099  * cipher text. After the completion of the cipher operation, the plaintext
1100  * data is replaced with the ciphertext data in case of an encryption and vice
1101  * versa for a decryption. The caller must ensure that the scatter/gather lists
1102  * for the output data point to sufficiently large buffers, i.e. multiples of
1103  * the block size of the cipher.
1104  */
1105 
__crypto_blkcipher_cast(struct crypto_tfm * tfm)1106 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1107 	struct crypto_tfm *tfm)
1108 {
1109 	return (struct crypto_blkcipher *)tfm;
1110 }
1111 
crypto_blkcipher_cast(struct crypto_tfm * tfm)1112 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1113 	struct crypto_tfm *tfm)
1114 {
1115 	BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1116 	return __crypto_blkcipher_cast(tfm);
1117 }
1118 
1119 /**
1120  * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1121  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1122  *	      blkcipher cipher
1123  * @type: specifies the type of the cipher
1124  * @mask: specifies the mask for the cipher
1125  *
1126  * Allocate a cipher handle for a block cipher. The returned struct
1127  * crypto_blkcipher is the cipher handle that is required for any subsequent
1128  * API invocation for that block cipher.
1129  *
1130  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1131  *	   of an error, PTR_ERR() returns the error code.
1132  */
crypto_alloc_blkcipher(const char * alg_name,u32 type,u32 mask)1133 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1134 	const char *alg_name, u32 type, u32 mask)
1135 {
1136 	type &= ~CRYPTO_ALG_TYPE_MASK;
1137 	type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1138 	mask |= CRYPTO_ALG_TYPE_MASK;
1139 
1140 	return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1141 }
1142 
crypto_blkcipher_tfm(struct crypto_blkcipher * tfm)1143 static inline struct crypto_tfm *crypto_blkcipher_tfm(
1144 	struct crypto_blkcipher *tfm)
1145 {
1146 	return &tfm->base;
1147 }
1148 
1149 /**
1150  * crypto_free_blkcipher() - zeroize and free the block cipher handle
1151  * @tfm: cipher handle to be freed
1152  */
crypto_free_blkcipher(struct crypto_blkcipher * tfm)1153 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1154 {
1155 	crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1156 }
1157 
1158 /**
1159  * crypto_has_blkcipher() - Search for the availability of a block cipher
1160  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1161  *	      block cipher
1162  * @type: specifies the type of the cipher
1163  * @mask: specifies the mask for the cipher
1164  *
1165  * Return: true when the block cipher is known to the kernel crypto API; false
1166  *	   otherwise
1167  */
crypto_has_blkcipher(const char * alg_name,u32 type,u32 mask)1168 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1169 {
1170 	type &= ~CRYPTO_ALG_TYPE_MASK;
1171 	type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1172 	mask |= CRYPTO_ALG_TYPE_MASK;
1173 
1174 	return crypto_has_alg(alg_name, type, mask);
1175 }
1176 
1177 /**
1178  * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1179  * @tfm: cipher handle
1180  *
1181  * Return: The character string holding the name of the cipher
1182  */
crypto_blkcipher_name(struct crypto_blkcipher * tfm)1183 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1184 {
1185 	return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1186 }
1187 
crypto_blkcipher_crt(struct crypto_blkcipher * tfm)1188 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1189 	struct crypto_blkcipher *tfm)
1190 {
1191 	return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1192 }
1193 
crypto_blkcipher_alg(struct crypto_blkcipher * tfm)1194 static inline struct blkcipher_alg *crypto_blkcipher_alg(
1195 	struct crypto_blkcipher *tfm)
1196 {
1197 	return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1198 }
1199 
1200 /**
1201  * crypto_blkcipher_ivsize() - obtain IV size
1202  * @tfm: cipher handle
1203  *
1204  * The size of the IV for the block cipher referenced by the cipher handle is
1205  * returned. This IV size may be zero if the cipher does not need an IV.
1206  *
1207  * Return: IV size in bytes
1208  */
crypto_blkcipher_ivsize(struct crypto_blkcipher * tfm)1209 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1210 {
1211 	return crypto_blkcipher_alg(tfm)->ivsize;
1212 }
1213 
1214 /**
1215  * crypto_blkcipher_blocksize() - obtain block size of cipher
1216  * @tfm: cipher handle
1217  *
1218  * The block size for the block cipher referenced with the cipher handle is
1219  * returned. The caller may use that information to allocate appropriate
1220  * memory for the data returned by the encryption or decryption operation.
1221  *
1222  * Return: block size of cipher
1223  */
crypto_blkcipher_blocksize(struct crypto_blkcipher * tfm)1224 static inline unsigned int crypto_blkcipher_blocksize(
1225 	struct crypto_blkcipher *tfm)
1226 {
1227 	return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1228 }
1229 
crypto_blkcipher_alignmask(struct crypto_blkcipher * tfm)1230 static inline unsigned int crypto_blkcipher_alignmask(
1231 	struct crypto_blkcipher *tfm)
1232 {
1233 	return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1234 }
1235 
crypto_blkcipher_get_flags(struct crypto_blkcipher * tfm)1236 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1237 {
1238 	return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1239 }
1240 
crypto_blkcipher_set_flags(struct crypto_blkcipher * tfm,u32 flags)1241 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1242 					      u32 flags)
1243 {
1244 	crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1245 }
1246 
crypto_blkcipher_clear_flags(struct crypto_blkcipher * tfm,u32 flags)1247 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1248 						u32 flags)
1249 {
1250 	crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1251 }
1252 
1253 /**
1254  * crypto_blkcipher_setkey() - set key for cipher
1255  * @tfm: cipher handle
1256  * @key: buffer holding the key
1257  * @keylen: length of the key in bytes
1258  *
1259  * The caller provided key is set for the block cipher referenced by the cipher
1260  * handle.
1261  *
1262  * Note, the key length determines the cipher type. Many block ciphers implement
1263  * different cipher modes depending on the key size, such as AES-128 vs AES-192
1264  * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1265  * is performed.
1266  *
1267  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1268  */
crypto_blkcipher_setkey(struct crypto_blkcipher * tfm,const u8 * key,unsigned int keylen)1269 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1270 					  const u8 *key, unsigned int keylen)
1271 {
1272 	return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1273 						 key, keylen);
1274 }
1275 
1276 /**
1277  * crypto_blkcipher_encrypt() - encrypt plaintext
1278  * @desc: reference to the block cipher handle with meta data
1279  * @dst: scatter/gather list that is filled by the cipher operation with the
1280  *	ciphertext
1281  * @src: scatter/gather list that holds the plaintext
1282  * @nbytes: number of bytes of the plaintext to encrypt.
1283  *
1284  * Encrypt plaintext data using the IV set by the caller with a preceding
1285  * call of crypto_blkcipher_set_iv.
1286  *
1287  * The blkcipher_desc data structure must be filled by the caller and can
1288  * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1289  * with the block cipher handle; desc.flags is filled with either
1290  * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1291  *
1292  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1293  */
crypto_blkcipher_encrypt(struct blkcipher_desc * desc,struct scatterlist * dst,struct scatterlist * src,unsigned int nbytes)1294 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1295 					   struct scatterlist *dst,
1296 					   struct scatterlist *src,
1297 					   unsigned int nbytes)
1298 {
1299 	desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1300 	return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1301 }
1302 
1303 /**
1304  * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1305  * @desc: reference to the block cipher handle with meta data
1306  * @dst: scatter/gather list that is filled by the cipher operation with the
1307  *	ciphertext
1308  * @src: scatter/gather list that holds the plaintext
1309  * @nbytes: number of bytes of the plaintext to encrypt.
1310  *
1311  * Encrypt plaintext data with the use of an IV that is solely used for this
1312  * cipher operation. Any previously set IV is not used.
1313  *
1314  * The blkcipher_desc data structure must be filled by the caller and can
1315  * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1316  * with the block cipher handle; desc.info is filled with the IV to be used for
1317  * the current operation; desc.flags is filled with either
1318  * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1319  *
1320  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1321  */
crypto_blkcipher_encrypt_iv(struct blkcipher_desc * desc,struct scatterlist * dst,struct scatterlist * src,unsigned int nbytes)1322 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1323 					      struct scatterlist *dst,
1324 					      struct scatterlist *src,
1325 					      unsigned int nbytes)
1326 {
1327 	return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1328 }
1329 
1330 /**
1331  * crypto_blkcipher_decrypt() - decrypt ciphertext
1332  * @desc: reference to the block cipher handle with meta data
1333  * @dst: scatter/gather list that is filled by the cipher operation with the
1334  *	plaintext
1335  * @src: scatter/gather list that holds the ciphertext
1336  * @nbytes: number of bytes of the ciphertext to decrypt.
1337  *
1338  * Decrypt ciphertext data using the IV set by the caller with a preceding
1339  * call of crypto_blkcipher_set_iv.
1340  *
1341  * The blkcipher_desc data structure must be filled by the caller as documented
1342  * for the crypto_blkcipher_encrypt call above.
1343  *
1344  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1345  *
1346  */
crypto_blkcipher_decrypt(struct blkcipher_desc * desc,struct scatterlist * dst,struct scatterlist * src,unsigned int nbytes)1347 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1348 					   struct scatterlist *dst,
1349 					   struct scatterlist *src,
1350 					   unsigned int nbytes)
1351 {
1352 	desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1353 	return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1354 }
1355 
1356 /**
1357  * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1358  * @desc: reference to the block cipher handle with meta data
1359  * @dst: scatter/gather list that is filled by the cipher operation with the
1360  *	plaintext
1361  * @src: scatter/gather list that holds the ciphertext
1362  * @nbytes: number of bytes of the ciphertext to decrypt.
1363  *
1364  * Decrypt ciphertext data with the use of an IV that is solely used for this
1365  * cipher operation. Any previously set IV is not used.
1366  *
1367  * The blkcipher_desc data structure must be filled by the caller as documented
1368  * for the crypto_blkcipher_encrypt_iv call above.
1369  *
1370  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1371  */
crypto_blkcipher_decrypt_iv(struct blkcipher_desc * desc,struct scatterlist * dst,struct scatterlist * src,unsigned int nbytes)1372 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1373 					      struct scatterlist *dst,
1374 					      struct scatterlist *src,
1375 					      unsigned int nbytes)
1376 {
1377 	return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1378 }
1379 
1380 /**
1381  * crypto_blkcipher_set_iv() - set IV for cipher
1382  * @tfm: cipher handle
1383  * @src: buffer holding the IV
1384  * @len: length of the IV in bytes
1385  *
1386  * The caller provided IV is set for the block cipher referenced by the cipher
1387  * handle.
1388  */
crypto_blkcipher_set_iv(struct crypto_blkcipher * tfm,const u8 * src,unsigned int len)1389 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1390 					   const u8 *src, unsigned int len)
1391 {
1392 	memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1393 }
1394 
1395 /**
1396  * crypto_blkcipher_get_iv() - obtain IV from cipher
1397  * @tfm: cipher handle
1398  * @dst: buffer filled with the IV
1399  * @len: length of the buffer dst
1400  *
1401  * The caller can obtain the IV set for the block cipher referenced by the
1402  * cipher handle and store it into the user-provided buffer. If the buffer
1403  * has an insufficient space, the IV is truncated to fit the buffer.
1404  */
crypto_blkcipher_get_iv(struct crypto_blkcipher * tfm,u8 * dst,unsigned int len)1405 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1406 					   u8 *dst, unsigned int len)
1407 {
1408 	memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1409 }
1410 
1411 /**
1412  * DOC: Single Block Cipher API
1413  *
1414  * The single block cipher API is used with the ciphers of type
1415  * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1416  *
1417  * Using the single block cipher API calls, operations with the basic cipher
1418  * primitive can be implemented. These cipher primitives exclude any block
1419  * chaining operations including IV handling.
1420  *
1421  * The purpose of this single block cipher API is to support the implementation
1422  * of templates or other concepts that only need to perform the cipher operation
1423  * on one block at a time. Templates invoke the underlying cipher primitive
1424  * block-wise and process either the input or the output data of these cipher
1425  * operations.
1426  */
1427 
__crypto_cipher_cast(struct crypto_tfm * tfm)1428 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1429 {
1430 	return (struct crypto_cipher *)tfm;
1431 }
1432 
crypto_cipher_cast(struct crypto_tfm * tfm)1433 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1434 {
1435 	BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1436 	return __crypto_cipher_cast(tfm);
1437 }
1438 
1439 /**
1440  * crypto_alloc_cipher() - allocate single block cipher handle
1441  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1442  *	     single block cipher
1443  * @type: specifies the type of the cipher
1444  * @mask: specifies the mask for the cipher
1445  *
1446  * Allocate a cipher handle for a single block cipher. The returned struct
1447  * crypto_cipher is the cipher handle that is required for any subsequent API
1448  * invocation for that single block cipher.
1449  *
1450  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1451  *	   of an error, PTR_ERR() returns the error code.
1452  */
crypto_alloc_cipher(const char * alg_name,u32 type,u32 mask)1453 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1454 							u32 type, u32 mask)
1455 {
1456 	type &= ~CRYPTO_ALG_TYPE_MASK;
1457 	type |= CRYPTO_ALG_TYPE_CIPHER;
1458 	mask |= CRYPTO_ALG_TYPE_MASK;
1459 
1460 	return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1461 }
1462 
crypto_cipher_tfm(struct crypto_cipher * tfm)1463 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1464 {
1465 	return &tfm->base;
1466 }
1467 
1468 /**
1469  * crypto_free_cipher() - zeroize and free the single block cipher handle
1470  * @tfm: cipher handle to be freed
1471  */
crypto_free_cipher(struct crypto_cipher * tfm)1472 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1473 {
1474 	crypto_free_tfm(crypto_cipher_tfm(tfm));
1475 }
1476 
1477 /**
1478  * crypto_has_cipher() - Search for the availability of a single block cipher
1479  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1480  *	     single block cipher
1481  * @type: specifies the type of the cipher
1482  * @mask: specifies the mask for the cipher
1483  *
1484  * Return: true when the single block cipher is known to the kernel crypto API;
1485  *	   false otherwise
1486  */
crypto_has_cipher(const char * alg_name,u32 type,u32 mask)1487 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1488 {
1489 	type &= ~CRYPTO_ALG_TYPE_MASK;
1490 	type |= CRYPTO_ALG_TYPE_CIPHER;
1491 	mask |= CRYPTO_ALG_TYPE_MASK;
1492 
1493 	return crypto_has_alg(alg_name, type, mask);
1494 }
1495 
crypto_cipher_crt(struct crypto_cipher * tfm)1496 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1497 {
1498 	return &crypto_cipher_tfm(tfm)->crt_cipher;
1499 }
1500 
1501 /**
1502  * crypto_cipher_blocksize() - obtain block size for cipher
1503  * @tfm: cipher handle
1504  *
1505  * The block size for the single block cipher referenced with the cipher handle
1506  * tfm is returned. The caller may use that information to allocate appropriate
1507  * memory for the data returned by the encryption or decryption operation
1508  *
1509  * Return: block size of cipher
1510  */
crypto_cipher_blocksize(struct crypto_cipher * tfm)1511 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1512 {
1513 	return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1514 }
1515 
crypto_cipher_alignmask(struct crypto_cipher * tfm)1516 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1517 {
1518 	return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1519 }
1520 
crypto_cipher_get_flags(struct crypto_cipher * tfm)1521 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1522 {
1523 	return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1524 }
1525 
crypto_cipher_set_flags(struct crypto_cipher * tfm,u32 flags)1526 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1527 					   u32 flags)
1528 {
1529 	crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1530 }
1531 
crypto_cipher_clear_flags(struct crypto_cipher * tfm,u32 flags)1532 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1533 					     u32 flags)
1534 {
1535 	crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1536 }
1537 
1538 /**
1539  * crypto_cipher_setkey() - set key for cipher
1540  * @tfm: cipher handle
1541  * @key: buffer holding the key
1542  * @keylen: length of the key in bytes
1543  *
1544  * The caller provided key is set for the single block cipher referenced by the
1545  * cipher handle.
1546  *
1547  * Note, the key length determines the cipher type. Many block ciphers implement
1548  * different cipher modes depending on the key size, such as AES-128 vs AES-192
1549  * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1550  * is performed.
1551  *
1552  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1553  */
crypto_cipher_setkey(struct crypto_cipher * tfm,const u8 * key,unsigned int keylen)1554 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1555                                        const u8 *key, unsigned int keylen)
1556 {
1557 	return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1558 						  key, keylen);
1559 }
1560 
1561 /**
1562  * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1563  * @tfm: cipher handle
1564  * @dst: points to the buffer that will be filled with the ciphertext
1565  * @src: buffer holding the plaintext to be encrypted
1566  *
1567  * Invoke the encryption operation of one block. The caller must ensure that
1568  * the plaintext and ciphertext buffers are at least one block in size.
1569  */
crypto_cipher_encrypt_one(struct crypto_cipher * tfm,u8 * dst,const u8 * src)1570 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1571 					     u8 *dst, const u8 *src)
1572 {
1573 	crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1574 						dst, src);
1575 }
1576 
1577 /**
1578  * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1579  * @tfm: cipher handle
1580  * @dst: points to the buffer that will be filled with the plaintext
1581  * @src: buffer holding the ciphertext to be decrypted
1582  *
1583  * Invoke the decryption operation of one block. The caller must ensure that
1584  * the plaintext and ciphertext buffers are at least one block in size.
1585  */
crypto_cipher_decrypt_one(struct crypto_cipher * tfm,u8 * dst,const u8 * src)1586 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1587 					     u8 *dst, const u8 *src)
1588 {
1589 	crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1590 						dst, src);
1591 }
1592 
__crypto_comp_cast(struct crypto_tfm * tfm)1593 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1594 {
1595 	return (struct crypto_comp *)tfm;
1596 }
1597 
crypto_comp_cast(struct crypto_tfm * tfm)1598 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1599 {
1600 	BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1601 	       CRYPTO_ALG_TYPE_MASK);
1602 	return __crypto_comp_cast(tfm);
1603 }
1604 
crypto_alloc_comp(const char * alg_name,u32 type,u32 mask)1605 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1606 						    u32 type, u32 mask)
1607 {
1608 	type &= ~CRYPTO_ALG_TYPE_MASK;
1609 	type |= CRYPTO_ALG_TYPE_COMPRESS;
1610 	mask |= CRYPTO_ALG_TYPE_MASK;
1611 
1612 	return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1613 }
1614 
crypto_comp_tfm(struct crypto_comp * tfm)1615 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1616 {
1617 	return &tfm->base;
1618 }
1619 
crypto_free_comp(struct crypto_comp * tfm)1620 static inline void crypto_free_comp(struct crypto_comp *tfm)
1621 {
1622 	crypto_free_tfm(crypto_comp_tfm(tfm));
1623 }
1624 
crypto_has_comp(const char * alg_name,u32 type,u32 mask)1625 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1626 {
1627 	type &= ~CRYPTO_ALG_TYPE_MASK;
1628 	type |= CRYPTO_ALG_TYPE_COMPRESS;
1629 	mask |= CRYPTO_ALG_TYPE_MASK;
1630 
1631 	return crypto_has_alg(alg_name, type, mask);
1632 }
1633 
crypto_comp_name(struct crypto_comp * tfm)1634 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1635 {
1636 	return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1637 }
1638 
crypto_comp_crt(struct crypto_comp * tfm)1639 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1640 {
1641 	return &crypto_comp_tfm(tfm)->crt_compress;
1642 }
1643 
crypto_comp_compress(struct crypto_comp * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen)1644 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1645                                        const u8 *src, unsigned int slen,
1646                                        u8 *dst, unsigned int *dlen)
1647 {
1648 	return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1649 						  src, slen, dst, dlen);
1650 }
1651 
crypto_comp_decompress(struct crypto_comp * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen)1652 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1653                                          const u8 *src, unsigned int slen,
1654                                          u8 *dst, unsigned int *dlen)
1655 {
1656 	return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1657 						    src, slen, dst, dlen);
1658 }
1659 
1660 #endif	/* _LINUX_CRYPTO_H */
1661 
1662