1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Scatterlist Cryptographic API.
5 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
6 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
7 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
9 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
10 * and Nettle, by Niels Möller.
12 #ifndef _LINUX_CRYPTO_H
13 #define _LINUX_CRYPTO_H
15 #include <linux/atomic.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/bug.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <linux/uaccess.h>
22 #include <linux/completion.h>
25 * Autoloaded crypto modules should only use a prefixed name to avoid allowing
26 * arbitrary modules to be loaded. Loading from userspace may still need the
27 * unprefixed names, so retains those aliases as well.
28 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
29 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
30 * expands twice on the same line. Instead, use a separate base name for the
33 #define MODULE_ALIAS_CRYPTO(name) \
34 __MODULE_INFO(alias, alias_userspace, name); \
35 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
38 * Algorithm masks and types.
40 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
41 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
42 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
43 #define CRYPTO_ALG_TYPE_AEAD 0x00000003
44 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
45 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
46 #define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005
47 #define CRYPTO_ALG_TYPE_KPP 0x00000008
48 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a
49 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b
50 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
51 #define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
52 #define CRYPTO_ALG_TYPE_HASH 0x0000000e
53 #define CRYPTO_ALG_TYPE_SHASH 0x0000000e
54 #define CRYPTO_ALG_TYPE_AHASH 0x0000000f
56 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
57 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
58 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
59 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e
61 #define CRYPTO_ALG_LARVAL 0x00000010
62 #define CRYPTO_ALG_DEAD 0x00000020
63 #define CRYPTO_ALG_DYING 0x00000040
64 #define CRYPTO_ALG_ASYNC 0x00000080
67 * Set this bit if and only if the algorithm requires another algorithm of
68 * the same type to handle corner cases.
70 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
73 * Set if the algorithm has passed automated run-time testing. Note that
74 * if there is no run-time testing for a given algorithm it is considered
78 #define CRYPTO_ALG_TESTED 0x00000400
81 * Set if the algorithm is an instance that is built from templates.
83 #define CRYPTO_ALG_INSTANCE 0x00000800
85 /* Set this bit if the algorithm provided is hardware accelerated but
86 * not available to userspace via instruction set or so.
88 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
91 * Mark a cipher as a service implementation only usable by another
92 * cipher and never by a normal user of the kernel crypto API
94 #define CRYPTO_ALG_INTERNAL 0x00002000
97 * Set if the algorithm has a ->setkey() method but can be used without
98 * calling it first, i.e. there is a default key.
100 #define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
103 * Don't trigger module loading
105 #define CRYPTO_NOLOAD 0x00008000
108 * Transform masks and values (for crt_flags).
110 #define CRYPTO_TFM_NEED_KEY 0x00000001
112 #define CRYPTO_TFM_REQ_MASK 0x000fff00
113 #define CRYPTO_TFM_RES_MASK 0xfff00000
115 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x00000100
116 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
117 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
118 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
119 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
120 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
121 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
122 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
125 * Miscellaneous stuff.
127 #define CRYPTO_MAX_ALG_NAME 128
130 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
131 * declaration) is used to ensure that the crypto_tfm context structure is
132 * aligned correctly for the given architecture so that there are no alignment
133 * faults for C data types. In particular, this is required on platforms such
134 * as arm where pointers are 32-bit aligned but there are data types such as
135 * u64 which require 64-bit alignment.
137 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
139 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
142 struct crypto_ablkcipher
;
143 struct crypto_async_request
;
144 struct crypto_blkcipher
;
148 typedef void (*crypto_completion_t
)(struct crypto_async_request
*req
, int err
);
151 * DOC: Block Cipher Context Data Structures
153 * These data structures define the operating context for each block cipher
157 struct crypto_async_request
{
158 struct list_head list
;
159 crypto_completion_t complete
;
161 struct crypto_tfm
*tfm
;
166 struct ablkcipher_request
{
167 struct crypto_async_request base
;
173 struct scatterlist
*src
;
174 struct scatterlist
*dst
;
176 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
179 struct blkcipher_desc
{
180 struct crypto_blkcipher
*tfm
;
186 * DOC: Block Cipher Algorithm Definitions
188 * These data structures define modular crypto algorithm implementations,
189 * managed via crypto_register_alg() and crypto_unregister_alg().
193 * struct ablkcipher_alg - asynchronous block cipher definition
194 * @min_keysize: Minimum key size supported by the transformation. This is the
195 * smallest key length supported by this transformation algorithm.
196 * This must be set to one of the pre-defined values as this is
197 * not hardware specific. Possible values for this field can be
198 * found via git grep "_MIN_KEY_SIZE" include/crypto/
199 * @max_keysize: Maximum key size supported by the transformation. This is the
200 * largest key length supported by this transformation algorithm.
201 * This must be set to one of the pre-defined values as this is
202 * not hardware specific. Possible values for this field can be
203 * found via git grep "_MAX_KEY_SIZE" include/crypto/
204 * @setkey: Set key for the transformation. This function is used to either
205 * program a supplied key into the hardware or store the key in the
206 * transformation context for programming it later. Note that this
207 * function does modify the transformation context. This function can
208 * be called multiple times during the existence of the transformation
209 * object, so one must make sure the key is properly reprogrammed into
210 * the hardware. This function is also responsible for checking the key
211 * length for validity. In case a software fallback was put in place in
212 * the @cra_init call, this function might need to use the fallback if
213 * the algorithm doesn't support all of the key sizes.
214 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
215 * the supplied scatterlist containing the blocks of data. The crypto
216 * API consumer is responsible for aligning the entries of the
217 * scatterlist properly and making sure the chunks are correctly
218 * sized. In case a software fallback was put in place in the
219 * @cra_init call, this function might need to use the fallback if
220 * the algorithm doesn't support all of the key sizes. In case the
221 * key was stored in transformation context, the key might need to be
222 * re-programmed into the hardware in this function. This function
223 * shall not modify the transformation context, as this function may
224 * be called in parallel with the same transformation object.
225 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
226 * and the conditions are exactly the same.
227 * @ivsize: IV size applicable for transformation. The consumer must provide an
228 * IV of exactly that size to perform the encrypt or decrypt operation.
230 * All fields except @ivsize are mandatory and must be filled.
232 struct ablkcipher_alg
{
233 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
234 unsigned int keylen
);
235 int (*encrypt
)(struct ablkcipher_request
*req
);
236 int (*decrypt
)(struct ablkcipher_request
*req
);
238 unsigned int min_keysize
;
239 unsigned int max_keysize
;
244 * struct blkcipher_alg - synchronous block cipher definition
245 * @min_keysize: see struct ablkcipher_alg
246 * @max_keysize: see struct ablkcipher_alg
247 * @setkey: see struct ablkcipher_alg
248 * @encrypt: see struct ablkcipher_alg
249 * @decrypt: see struct ablkcipher_alg
250 * @ivsize: see struct ablkcipher_alg
252 * All fields except @ivsize are mandatory and must be filled.
254 struct blkcipher_alg
{
255 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
256 unsigned int keylen
);
257 int (*encrypt
)(struct blkcipher_desc
*desc
,
258 struct scatterlist
*dst
, struct scatterlist
*src
,
259 unsigned int nbytes
);
260 int (*decrypt
)(struct blkcipher_desc
*desc
,
261 struct scatterlist
*dst
, struct scatterlist
*src
,
262 unsigned int nbytes
);
264 unsigned int min_keysize
;
265 unsigned int max_keysize
;
270 * struct cipher_alg - single-block symmetric ciphers definition
271 * @cia_min_keysize: Minimum key size supported by the transformation. This is
272 * the smallest key length supported by this transformation
273 * algorithm. This must be set to one of the pre-defined
274 * values as this is not hardware specific. Possible values
275 * for this field can be found via git grep "_MIN_KEY_SIZE"
277 * @cia_max_keysize: Maximum key size supported by the transformation. This is
278 * the largest key length supported by this transformation
279 * algorithm. This must be set to one of the pre-defined values
280 * as this is not hardware specific. Possible values for this
281 * field can be found via git grep "_MAX_KEY_SIZE"
283 * @cia_setkey: Set key for the transformation. This function is used to either
284 * program a supplied key into the hardware or store the key in the
285 * transformation context for programming it later. Note that this
286 * function does modify the transformation context. This function
287 * can be called multiple times during the existence of the
288 * transformation object, so one must make sure the key is properly
289 * reprogrammed into the hardware. This function is also
290 * responsible for checking the key length for validity.
291 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
292 * single block of data, which must be @cra_blocksize big. This
293 * always operates on a full @cra_blocksize and it is not possible
294 * to encrypt a block of smaller size. The supplied buffers must
295 * therefore also be at least of @cra_blocksize size. Both the
296 * input and output buffers are always aligned to @cra_alignmask.
297 * In case either of the input or output buffer supplied by user
298 * of the crypto API is not aligned to @cra_alignmask, the crypto
299 * API will re-align the buffers. The re-alignment means that a
300 * new buffer will be allocated, the data will be copied into the
301 * new buffer, then the processing will happen on the new buffer,
302 * then the data will be copied back into the original buffer and
303 * finally the new buffer will be freed. In case a software
304 * fallback was put in place in the @cra_init call, this function
305 * might need to use the fallback if the algorithm doesn't support
306 * all of the key sizes. In case the key was stored in
307 * transformation context, the key might need to be re-programmed
308 * into the hardware in this function. This function shall not
309 * modify the transformation context, as this function may be
310 * called in parallel with the same transformation object.
311 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
312 * @cia_encrypt, and the conditions are exactly the same.
314 * All fields are mandatory and must be filled.
317 unsigned int cia_min_keysize
;
318 unsigned int cia_max_keysize
;
319 int (*cia_setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
320 unsigned int keylen
);
321 void (*cia_encrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
322 void (*cia_decrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
326 * struct compress_alg - compression/decompression algorithm
327 * @coa_compress: Compress a buffer of specified length, storing the resulting
328 * data in the specified buffer. Return the length of the
329 * compressed data in dlen.
330 * @coa_decompress: Decompress the source buffer, storing the uncompressed
331 * data in the specified buffer. The length of the data is
334 * All fields are mandatory.
336 struct compress_alg
{
337 int (*coa_compress
)(struct crypto_tfm
*tfm
, const u8
*src
,
338 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
339 int (*coa_decompress
)(struct crypto_tfm
*tfm
, const u8
*src
,
340 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
343 #ifdef CONFIG_CRYPTO_STATS
345 * struct crypto_istat_aead - statistics for AEAD algorithm
346 * @encrypt_cnt: number of encrypt requests
347 * @encrypt_tlen: total data size handled by encrypt requests
348 * @decrypt_cnt: number of decrypt requests
349 * @decrypt_tlen: total data size handled by decrypt requests
350 * @err_cnt: number of error for AEAD requests
352 struct crypto_istat_aead
{
353 atomic64_t encrypt_cnt
;
354 atomic64_t encrypt_tlen
;
355 atomic64_t decrypt_cnt
;
356 atomic64_t decrypt_tlen
;
361 * struct crypto_istat_akcipher - statistics for akcipher algorithm
362 * @encrypt_cnt: number of encrypt requests
363 * @encrypt_tlen: total data size handled by encrypt requests
364 * @decrypt_cnt: number of decrypt requests
365 * @decrypt_tlen: total data size handled by decrypt requests
366 * @verify_cnt: number of verify operation
367 * @sign_cnt: number of sign requests
368 * @err_cnt: number of error for akcipher requests
370 struct crypto_istat_akcipher
{
371 atomic64_t encrypt_cnt
;
372 atomic64_t encrypt_tlen
;
373 atomic64_t decrypt_cnt
;
374 atomic64_t decrypt_tlen
;
375 atomic64_t verify_cnt
;
381 * struct crypto_istat_cipher - statistics for cipher algorithm
382 * @encrypt_cnt: number of encrypt requests
383 * @encrypt_tlen: total data size handled by encrypt requests
384 * @decrypt_cnt: number of decrypt requests
385 * @decrypt_tlen: total data size handled by decrypt requests
386 * @err_cnt: number of error for cipher requests
388 struct crypto_istat_cipher
{
389 atomic64_t encrypt_cnt
;
390 atomic64_t encrypt_tlen
;
391 atomic64_t decrypt_cnt
;
392 atomic64_t decrypt_tlen
;
397 * struct crypto_istat_compress - statistics for compress algorithm
398 * @compress_cnt: number of compress requests
399 * @compress_tlen: total data size handled by compress requests
400 * @decompress_cnt: number of decompress requests
401 * @decompress_tlen: total data size handled by decompress requests
402 * @err_cnt: number of error for compress requests
404 struct crypto_istat_compress
{
405 atomic64_t compress_cnt
;
406 atomic64_t compress_tlen
;
407 atomic64_t decompress_cnt
;
408 atomic64_t decompress_tlen
;
413 * struct crypto_istat_hash - statistics for has algorithm
414 * @hash_cnt: number of hash requests
415 * @hash_tlen: total data size hashed
416 * @err_cnt: number of error for hash requests
418 struct crypto_istat_hash
{
420 atomic64_t hash_tlen
;
425 * struct crypto_istat_kpp - statistics for KPP algorithm
426 * @setsecret_cnt: number of setsecrey operation
427 * @generate_public_key_cnt: number of generate_public_key operation
428 * @compute_shared_secret_cnt: number of compute_shared_secret operation
429 * @err_cnt: number of error for KPP requests
431 struct crypto_istat_kpp
{
432 atomic64_t setsecret_cnt
;
433 atomic64_t generate_public_key_cnt
;
434 atomic64_t compute_shared_secret_cnt
;
439 * struct crypto_istat_rng: statistics for RNG algorithm
440 * @generate_cnt: number of RNG generate requests
441 * @generate_tlen: total data size of generated data by the RNG
442 * @seed_cnt: number of times the RNG was seeded
443 * @err_cnt: number of error for RNG requests
445 struct crypto_istat_rng
{
446 atomic64_t generate_cnt
;
447 atomic64_t generate_tlen
;
451 #endif /* CONFIG_CRYPTO_STATS */
453 #define cra_ablkcipher cra_u.ablkcipher
454 #define cra_blkcipher cra_u.blkcipher
455 #define cra_cipher cra_u.cipher
456 #define cra_compress cra_u.compress
459 * struct crypto_alg - definition of a cryptograpic cipher algorithm
460 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
461 * CRYPTO_ALG_* flags for the flags which go in here. Those are
462 * used for fine-tuning the description of the transformation
464 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
465 * of the smallest possible unit which can be transformed with
466 * this algorithm. The users must respect this value.
467 * In case of HASH transformation, it is possible for a smaller
468 * block than @cra_blocksize to be passed to the crypto API for
469 * transformation, in case of any other transformation type, an
470 * error will be returned upon any attempt to transform smaller
471 * than @cra_blocksize chunks.
472 * @cra_ctxsize: Size of the operational context of the transformation. This
473 * value informs the kernel crypto API about the memory size
474 * needed to be allocated for the transformation context.
475 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
476 * buffer containing the input data for the algorithm must be
477 * aligned to this alignment mask. The data buffer for the
478 * output data must be aligned to this alignment mask. Note that
479 * the Crypto API will do the re-alignment in software, but
480 * only under special conditions and there is a performance hit.
481 * The re-alignment happens at these occasions for different
482 * @cra_u types: cipher -- For both input data and output data
483 * buffer; ahash -- For output hash destination buf; shash --
484 * For output hash destination buf.
485 * This is needed on hardware which is flawed by design and
486 * cannot pick data from arbitrary addresses.
487 * @cra_priority: Priority of this transformation implementation. In case
488 * multiple transformations with same @cra_name are available to
489 * the Crypto API, the kernel will use the one with highest
491 * @cra_name: Generic name (usable by multiple implementations) of the
492 * transformation algorithm. This is the name of the transformation
493 * itself. This field is used by the kernel when looking up the
494 * providers of particular transformation.
495 * @cra_driver_name: Unique name of the transformation provider. This is the
496 * name of the provider of the transformation. This can be any
497 * arbitrary value, but in the usual case, this contains the
498 * name of the chip or provider and the name of the
499 * transformation algorithm.
500 * @cra_type: Type of the cryptographic transformation. This is a pointer to
501 * struct crypto_type, which implements callbacks common for all
502 * transformation types. There are multiple options:
503 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
504 * &crypto_ahash_type, &crypto_rng_type.
505 * This field might be empty. In that case, there are no common
506 * callbacks. This is the case for: cipher, compress, shash.
507 * @cra_u: Callbacks implementing the transformation. This is a union of
508 * multiple structures. Depending on the type of transformation selected
509 * by @cra_type and @cra_flags above, the associated structure must be
510 * filled with callbacks. This field might be empty. This is the case
512 * @cra_init: Initialize the cryptographic transformation object. This function
513 * is used to initialize the cryptographic transformation object.
514 * This function is called only once at the instantiation time, right
515 * after the transformation context was allocated. In case the
516 * cryptographic hardware has some special requirements which need to
517 * be handled by software, this function shall check for the precise
518 * requirement of the transformation and put any software fallbacks
520 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
521 * counterpart to @cra_init, used to remove various changes set in
523 * @cra_u.ablkcipher: Union member which contains an asynchronous block cipher
524 * definition. See @struct @ablkcipher_alg.
525 * @cra_u.blkcipher: Union member which contains a synchronous block cipher
526 * definition See @struct @blkcipher_alg.
527 * @cra_u.cipher: Union member which contains a single-block symmetric cipher
528 * definition. See @struct @cipher_alg.
529 * @cra_u.compress: Union member which contains a (de)compression algorithm.
530 * See @struct @compress_alg.
531 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
532 * @cra_list: internally used
533 * @cra_users: internally used
534 * @cra_refcnt: internally used
535 * @cra_destroy: internally used
537 * @stats: union of all possible crypto_istat_xxx structures
538 * @stats.aead: statistics for AEAD algorithm
539 * @stats.akcipher: statistics for akcipher algorithm
540 * @stats.cipher: statistics for cipher algorithm
541 * @stats.compress: statistics for compress algorithm
542 * @stats.hash: statistics for hash algorithm
543 * @stats.rng: statistics for rng algorithm
544 * @stats.kpp: statistics for KPP algorithm
546 * The struct crypto_alg describes a generic Crypto API algorithm and is common
547 * for all of the transformations. Any variable not documented here shall not
548 * be used by a cipher implementation as it is internal to the Crypto API.
551 struct list_head cra_list
;
552 struct list_head cra_users
;
555 unsigned int cra_blocksize
;
556 unsigned int cra_ctxsize
;
557 unsigned int cra_alignmask
;
560 refcount_t cra_refcnt
;
562 char cra_name
[CRYPTO_MAX_ALG_NAME
];
563 char cra_driver_name
[CRYPTO_MAX_ALG_NAME
];
565 const struct crypto_type
*cra_type
;
568 struct ablkcipher_alg ablkcipher
;
569 struct blkcipher_alg blkcipher
;
570 struct cipher_alg cipher
;
571 struct compress_alg compress
;
574 int (*cra_init
)(struct crypto_tfm
*tfm
);
575 void (*cra_exit
)(struct crypto_tfm
*tfm
);
576 void (*cra_destroy
)(struct crypto_alg
*alg
);
578 struct module
*cra_module
;
580 #ifdef CONFIG_CRYPTO_STATS
582 struct crypto_istat_aead aead
;
583 struct crypto_istat_akcipher akcipher
;
584 struct crypto_istat_cipher cipher
;
585 struct crypto_istat_compress compress
;
586 struct crypto_istat_hash hash
;
587 struct crypto_istat_rng rng
;
588 struct crypto_istat_kpp kpp
;
590 #endif /* CONFIG_CRYPTO_STATS */
592 } CRYPTO_MINALIGN_ATTR
;
594 #ifdef CONFIG_CRYPTO_STATS
595 void crypto_stats_init(struct crypto_alg
*alg
);
596 void crypto_stats_get(struct crypto_alg
*alg
);
597 void crypto_stats_ablkcipher_encrypt(unsigned int nbytes
, int ret
, struct crypto_alg
*alg
);
598 void crypto_stats_ablkcipher_decrypt(unsigned int nbytes
, int ret
, struct crypto_alg
*alg
);
599 void crypto_stats_aead_encrypt(unsigned int cryptlen
, struct crypto_alg
*alg
, int ret
);
600 void crypto_stats_aead_decrypt(unsigned int cryptlen
, struct crypto_alg
*alg
, int ret
);
601 void crypto_stats_ahash_update(unsigned int nbytes
, int ret
, struct crypto_alg
*alg
);
602 void crypto_stats_ahash_final(unsigned int nbytes
, int ret
, struct crypto_alg
*alg
);
603 void crypto_stats_akcipher_encrypt(unsigned int src_len
, int ret
, struct crypto_alg
*alg
);
604 void crypto_stats_akcipher_decrypt(unsigned int src_len
, int ret
, struct crypto_alg
*alg
);
605 void crypto_stats_akcipher_sign(int ret
, struct crypto_alg
*alg
);
606 void crypto_stats_akcipher_verify(int ret
, struct crypto_alg
*alg
);
607 void crypto_stats_compress(unsigned int slen
, int ret
, struct crypto_alg
*alg
);
608 void crypto_stats_decompress(unsigned int slen
, int ret
, struct crypto_alg
*alg
);
609 void crypto_stats_kpp_set_secret(struct crypto_alg
*alg
, int ret
);
610 void crypto_stats_kpp_generate_public_key(struct crypto_alg
*alg
, int ret
);
611 void crypto_stats_kpp_compute_shared_secret(struct crypto_alg
*alg
, int ret
);
612 void crypto_stats_rng_seed(struct crypto_alg
*alg
, int ret
);
613 void crypto_stats_rng_generate(struct crypto_alg
*alg
, unsigned int dlen
, int ret
);
614 void crypto_stats_skcipher_encrypt(unsigned int cryptlen
, int ret
, struct crypto_alg
*alg
);
615 void crypto_stats_skcipher_decrypt(unsigned int cryptlen
, int ret
, struct crypto_alg
*alg
);
617 static inline void crypto_stats_init(struct crypto_alg
*alg
)
619 static inline void crypto_stats_get(struct crypto_alg
*alg
)
621 static inline void crypto_stats_ablkcipher_encrypt(unsigned int nbytes
, int ret
, struct crypto_alg
*alg
)
623 static inline void crypto_stats_ablkcipher_decrypt(unsigned int nbytes
, int ret
, struct crypto_alg
*alg
)
625 static inline void crypto_stats_aead_encrypt(unsigned int cryptlen
, struct crypto_alg
*alg
, int ret
)
627 static inline void crypto_stats_aead_decrypt(unsigned int cryptlen
, struct crypto_alg
*alg
, int ret
)
629 static inline void crypto_stats_ahash_update(unsigned int nbytes
, int ret
, struct crypto_alg
*alg
)
631 static inline void crypto_stats_ahash_final(unsigned int nbytes
, int ret
, struct crypto_alg
*alg
)
633 static inline void crypto_stats_akcipher_encrypt(unsigned int src_len
, int ret
, struct crypto_alg
*alg
)
635 static inline void crypto_stats_akcipher_decrypt(unsigned int src_len
, int ret
, struct crypto_alg
*alg
)
637 static inline void crypto_stats_akcipher_sign(int ret
, struct crypto_alg
*alg
)
639 static inline void crypto_stats_akcipher_verify(int ret
, struct crypto_alg
*alg
)
641 static inline void crypto_stats_compress(unsigned int slen
, int ret
, struct crypto_alg
*alg
)
643 static inline void crypto_stats_decompress(unsigned int slen
, int ret
, struct crypto_alg
*alg
)
645 static inline void crypto_stats_kpp_set_secret(struct crypto_alg
*alg
, int ret
)
647 static inline void crypto_stats_kpp_generate_public_key(struct crypto_alg
*alg
, int ret
)
649 static inline void crypto_stats_kpp_compute_shared_secret(struct crypto_alg
*alg
, int ret
)
651 static inline void crypto_stats_rng_seed(struct crypto_alg
*alg
, int ret
)
653 static inline void crypto_stats_rng_generate(struct crypto_alg
*alg
, unsigned int dlen
, int ret
)
655 static inline void crypto_stats_skcipher_encrypt(unsigned int cryptlen
, int ret
, struct crypto_alg
*alg
)
657 static inline void crypto_stats_skcipher_decrypt(unsigned int cryptlen
, int ret
, struct crypto_alg
*alg
)
661 * A helper struct for waiting for completion of async crypto ops
664 struct completion completion
;
669 * Macro for declaring a crypto op async wait object on stack
671 #define DECLARE_CRYPTO_WAIT(_wait) \
672 struct crypto_wait _wait = { \
673 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
676 * Async ops completion helper functioons
678 void crypto_req_done(struct crypto_async_request
*req
, int err
);
680 static inline int crypto_wait_req(int err
, struct crypto_wait
*wait
)
685 wait_for_completion(&wait
->completion
);
686 reinit_completion(&wait
->completion
);
694 static inline void crypto_init_wait(struct crypto_wait
*wait
)
696 init_completion(&wait
->completion
);
700 * Algorithm registration interface.
702 int crypto_register_alg(struct crypto_alg
*alg
);
703 int crypto_unregister_alg(struct crypto_alg
*alg
);
704 int crypto_register_algs(struct crypto_alg
*algs
, int count
);
705 int crypto_unregister_algs(struct crypto_alg
*algs
, int count
);
708 * Algorithm query interface.
710 int crypto_has_alg(const char *name
, u32 type
, u32 mask
);
713 * Transforms: user-instantiated objects which encapsulate algorithms
714 * and core processing logic. Managed via crypto_alloc_*() and
715 * crypto_free_*(), as well as the various helpers below.
718 struct ablkcipher_tfm
{
719 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
720 unsigned int keylen
);
721 int (*encrypt
)(struct ablkcipher_request
*req
);
722 int (*decrypt
)(struct ablkcipher_request
*req
);
724 struct crypto_ablkcipher
*base
;
727 unsigned int reqsize
;
730 struct blkcipher_tfm
{
732 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
733 unsigned int keylen
);
734 int (*encrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
735 struct scatterlist
*src
, unsigned int nbytes
);
736 int (*decrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
737 struct scatterlist
*src
, unsigned int nbytes
);
741 int (*cit_setkey
)(struct crypto_tfm
*tfm
,
742 const u8
*key
, unsigned int keylen
);
743 void (*cit_encrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
744 void (*cit_decrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
747 struct compress_tfm
{
748 int (*cot_compress
)(struct crypto_tfm
*tfm
,
749 const u8
*src
, unsigned int slen
,
750 u8
*dst
, unsigned int *dlen
);
751 int (*cot_decompress
)(struct crypto_tfm
*tfm
,
752 const u8
*src
, unsigned int slen
,
753 u8
*dst
, unsigned int *dlen
);
756 #define crt_ablkcipher crt_u.ablkcipher
757 #define crt_blkcipher crt_u.blkcipher
758 #define crt_cipher crt_u.cipher
759 #define crt_compress crt_u.compress
766 struct ablkcipher_tfm ablkcipher
;
767 struct blkcipher_tfm blkcipher
;
768 struct cipher_tfm cipher
;
769 struct compress_tfm compress
;
772 void (*exit
)(struct crypto_tfm
*tfm
);
774 struct crypto_alg
*__crt_alg
;
776 void *__crt_ctx
[] CRYPTO_MINALIGN_ATTR
;
779 struct crypto_ablkcipher
{
780 struct crypto_tfm base
;
783 struct crypto_blkcipher
{
784 struct crypto_tfm base
;
787 struct crypto_cipher
{
788 struct crypto_tfm base
;
792 struct crypto_tfm base
;
803 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
805 /* Maximum number of (rtattr) parameters for each template. */
806 #define CRYPTO_MAX_ATTRS 32
808 struct crypto_attr_alg
{
809 char name
[CRYPTO_MAX_ALG_NAME
];
812 struct crypto_attr_type
{
817 struct crypto_attr_u32
{
822 * Transform user interface.
825 struct crypto_tfm
*crypto_alloc_base(const char *alg_name
, u32 type
, u32 mask
);
826 void crypto_destroy_tfm(void *mem
, struct crypto_tfm
*tfm
);
828 static inline void crypto_free_tfm(struct crypto_tfm
*tfm
)
830 return crypto_destroy_tfm(tfm
, tfm
);
833 int alg_test(const char *driver
, const char *alg
, u32 type
, u32 mask
);
836 * Transform helpers which query the underlying algorithm.
838 static inline const char *crypto_tfm_alg_name(struct crypto_tfm
*tfm
)
840 return tfm
->__crt_alg
->cra_name
;
843 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm
*tfm
)
845 return tfm
->__crt_alg
->cra_driver_name
;
848 static inline int crypto_tfm_alg_priority(struct crypto_tfm
*tfm
)
850 return tfm
->__crt_alg
->cra_priority
;
853 static inline u32
crypto_tfm_alg_type(struct crypto_tfm
*tfm
)
855 return tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
;
858 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm
*tfm
)
860 return tfm
->__crt_alg
->cra_blocksize
;
863 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm
*tfm
)
865 return tfm
->__crt_alg
->cra_alignmask
;
868 static inline u32
crypto_tfm_get_flags(struct crypto_tfm
*tfm
)
870 return tfm
->crt_flags
;
873 static inline void crypto_tfm_set_flags(struct crypto_tfm
*tfm
, u32 flags
)
875 tfm
->crt_flags
|= flags
;
878 static inline void crypto_tfm_clear_flags(struct crypto_tfm
*tfm
, u32 flags
)
880 tfm
->crt_flags
&= ~flags
;
883 static inline void *crypto_tfm_ctx(struct crypto_tfm
*tfm
)
885 return tfm
->__crt_ctx
;
888 static inline unsigned int crypto_tfm_ctx_alignment(void)
890 struct crypto_tfm
*tfm
;
891 return __alignof__(tfm
->__crt_ctx
);
897 static inline struct crypto_ablkcipher
*__crypto_ablkcipher_cast(
898 struct crypto_tfm
*tfm
)
900 return (struct crypto_ablkcipher
*)tfm
;
903 static inline u32
crypto_skcipher_type(u32 type
)
905 type
&= ~CRYPTO_ALG_TYPE_MASK
;
906 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
910 static inline u32
crypto_skcipher_mask(u32 mask
)
912 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
913 mask
|= CRYPTO_ALG_TYPE_BLKCIPHER_MASK
;
918 * DOC: Asynchronous Block Cipher API
920 * Asynchronous block cipher API is used with the ciphers of type
921 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
923 * Asynchronous cipher operations imply that the function invocation for a
924 * cipher request returns immediately before the completion of the operation.
925 * The cipher request is scheduled as a separate kernel thread and therefore
926 * load-balanced on the different CPUs via the process scheduler. To allow
927 * the kernel crypto API to inform the caller about the completion of a cipher
928 * request, the caller must provide a callback function. That function is
929 * invoked with the cipher handle when the request completes.
931 * To support the asynchronous operation, additional information than just the
932 * cipher handle must be supplied to the kernel crypto API. That additional
933 * information is given by filling in the ablkcipher_request data structure.
935 * For the asynchronous block cipher API, the state is maintained with the tfm
936 * cipher handle. A single tfm can be used across multiple calls and in
937 * parallel. For asynchronous block cipher calls, context data supplied and
938 * only used by the caller can be referenced the request data structure in
939 * addition to the IV used for the cipher request. The maintenance of such
940 * state information would be important for a crypto driver implementer to
941 * have, because when calling the callback function upon completion of the
942 * cipher operation, that callback function may need some information about
943 * which operation just finished if it invoked multiple in parallel. This
944 * state information is unused by the kernel crypto API.
947 static inline struct crypto_tfm
*crypto_ablkcipher_tfm(
948 struct crypto_ablkcipher
*tfm
)
954 * crypto_free_ablkcipher() - zeroize and free cipher handle
955 * @tfm: cipher handle to be freed
957 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher
*tfm
)
959 crypto_free_tfm(crypto_ablkcipher_tfm(tfm
));
963 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
964 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
966 * @type: specifies the type of the cipher
967 * @mask: specifies the mask for the cipher
969 * Return: true when the ablkcipher is known to the kernel crypto API; false
972 static inline int crypto_has_ablkcipher(const char *alg_name
, u32 type
,
975 return crypto_has_alg(alg_name
, crypto_skcipher_type(type
),
976 crypto_skcipher_mask(mask
));
979 static inline struct ablkcipher_tfm
*crypto_ablkcipher_crt(
980 struct crypto_ablkcipher
*tfm
)
982 return &crypto_ablkcipher_tfm(tfm
)->crt_ablkcipher
;
986 * crypto_ablkcipher_ivsize() - obtain IV size
987 * @tfm: cipher handle
989 * The size of the IV for the ablkcipher referenced by the cipher handle is
990 * returned. This IV size may be zero if the cipher does not need an IV.
992 * Return: IV size in bytes
994 static inline unsigned int crypto_ablkcipher_ivsize(
995 struct crypto_ablkcipher
*tfm
)
997 return crypto_ablkcipher_crt(tfm
)->ivsize
;
1001 * crypto_ablkcipher_blocksize() - obtain block size of cipher
1002 * @tfm: cipher handle
1004 * The block size for the ablkcipher referenced with the cipher handle is
1005 * returned. The caller may use that information to allocate appropriate
1006 * memory for the data returned by the encryption or decryption operation
1008 * Return: block size of cipher
1010 static inline unsigned int crypto_ablkcipher_blocksize(
1011 struct crypto_ablkcipher
*tfm
)
1013 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm
));
1016 static inline unsigned int crypto_ablkcipher_alignmask(
1017 struct crypto_ablkcipher
*tfm
)
1019 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm
));
1022 static inline u32
crypto_ablkcipher_get_flags(struct crypto_ablkcipher
*tfm
)
1024 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm
));
1027 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher
*tfm
,
1030 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm
), flags
);
1033 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher
*tfm
,
1036 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm
), flags
);
1040 * crypto_ablkcipher_setkey() - set key for cipher
1041 * @tfm: cipher handle
1042 * @key: buffer holding the key
1043 * @keylen: length of the key in bytes
1045 * The caller provided key is set for the ablkcipher referenced by the cipher
1048 * Note, the key length determines the cipher type. Many block ciphers implement
1049 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1050 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1053 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1055 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher
*tfm
,
1056 const u8
*key
, unsigned int keylen
)
1058 struct ablkcipher_tfm
*crt
= crypto_ablkcipher_crt(tfm
);
1060 return crt
->setkey(crt
->base
, key
, keylen
);
1064 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
1065 * @req: ablkcipher_request out of which the cipher handle is to be obtained
1067 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
1070 * Return: crypto_ablkcipher handle
1072 static inline struct crypto_ablkcipher
*crypto_ablkcipher_reqtfm(
1073 struct ablkcipher_request
*req
)
1075 return __crypto_ablkcipher_cast(req
->base
.tfm
);
1079 * crypto_ablkcipher_encrypt() - encrypt plaintext
1080 * @req: reference to the ablkcipher_request handle that holds all information
1081 * needed to perform the cipher operation
1083 * Encrypt plaintext data using the ablkcipher_request handle. That data
1084 * structure and how it is filled with data is discussed with the
1085 * ablkcipher_request_* functions.
1087 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1089 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request
*req
)
1091 struct ablkcipher_tfm
*crt
=
1092 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
1093 struct crypto_alg
*alg
= crt
->base
->base
.__crt_alg
;
1094 unsigned int nbytes
= req
->nbytes
;
1097 crypto_stats_get(alg
);
1098 ret
= crt
->encrypt(req
);
1099 crypto_stats_ablkcipher_encrypt(nbytes
, ret
, alg
);
1104 * crypto_ablkcipher_decrypt() - decrypt ciphertext
1105 * @req: reference to the ablkcipher_request handle that holds all information
1106 * needed to perform the cipher operation
1108 * Decrypt ciphertext data using the ablkcipher_request handle. That data
1109 * structure and how it is filled with data is discussed with the
1110 * ablkcipher_request_* functions.
1112 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1114 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request
*req
)
1116 struct ablkcipher_tfm
*crt
=
1117 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
1118 struct crypto_alg
*alg
= crt
->base
->base
.__crt_alg
;
1119 unsigned int nbytes
= req
->nbytes
;
1122 crypto_stats_get(alg
);
1123 ret
= crt
->decrypt(req
);
1124 crypto_stats_ablkcipher_decrypt(nbytes
, ret
, alg
);
1129 * DOC: Asynchronous Cipher Request Handle
1131 * The ablkcipher_request data structure contains all pointers to data
1132 * required for the asynchronous cipher operation. This includes the cipher
1133 * handle (which can be used by multiple ablkcipher_request instances), pointer
1134 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
1135 * as a handle to the ablkcipher_request_* API calls in a similar way as
1136 * ablkcipher handle to the crypto_ablkcipher_* API calls.
1140 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
1141 * @tfm: cipher handle
1143 * Return: number of bytes
1145 static inline unsigned int crypto_ablkcipher_reqsize(
1146 struct crypto_ablkcipher
*tfm
)
1148 return crypto_ablkcipher_crt(tfm
)->reqsize
;
1152 * ablkcipher_request_set_tfm() - update cipher handle reference in request
1153 * @req: request handle to be modified
1154 * @tfm: cipher handle that shall be added to the request handle
1156 * Allow the caller to replace the existing ablkcipher handle in the request
1157 * data structure with a different one.
1159 static inline void ablkcipher_request_set_tfm(
1160 struct ablkcipher_request
*req
, struct crypto_ablkcipher
*tfm
)
1162 req
->base
.tfm
= crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm
)->base
);
1165 static inline struct ablkcipher_request
*ablkcipher_request_cast(
1166 struct crypto_async_request
*req
)
1168 return container_of(req
, struct ablkcipher_request
, base
);
1172 * ablkcipher_request_alloc() - allocate request data structure
1173 * @tfm: cipher handle to be registered with the request
1174 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1176 * Allocate the request data structure that must be used with the ablkcipher
1177 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1178 * handle is registered in the request data structure.
1180 * Return: allocated request handle in case of success, or NULL if out of memory
1182 static inline struct ablkcipher_request
*ablkcipher_request_alloc(
1183 struct crypto_ablkcipher
*tfm
, gfp_t gfp
)
1185 struct ablkcipher_request
*req
;
1187 req
= kmalloc(sizeof(struct ablkcipher_request
) +
1188 crypto_ablkcipher_reqsize(tfm
), gfp
);
1191 ablkcipher_request_set_tfm(req
, tfm
);
1197 * ablkcipher_request_free() - zeroize and free request data structure
1198 * @req: request data structure cipher handle to be freed
1200 static inline void ablkcipher_request_free(struct ablkcipher_request
*req
)
1206 * ablkcipher_request_set_callback() - set asynchronous callback function
1207 * @req: request handle
1208 * @flags: specify zero or an ORing of the flags
1209 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1210 * increase the wait queue beyond the initial maximum size;
1211 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1212 * @compl: callback function pointer to be registered with the request handle
1213 * @data: The data pointer refers to memory that is not used by the kernel
1214 * crypto API, but provided to the callback function for it to use. Here,
1215 * the caller can provide a reference to memory the callback function can
1216 * operate on. As the callback function is invoked asynchronously to the
1217 * related functionality, it may need to access data structures of the
1218 * related functionality which can be referenced using this pointer. The
1219 * callback function can access the memory via the "data" field in the
1220 * crypto_async_request data structure provided to the callback function.
1222 * This function allows setting the callback function that is triggered once the
1223 * cipher operation completes.
1225 * The callback function is registered with the ablkcipher_request handle and
1226 * must comply with the following template::
1228 * void callback_function(struct crypto_async_request *req, int error)
1230 static inline void ablkcipher_request_set_callback(
1231 struct ablkcipher_request
*req
,
1232 u32 flags
, crypto_completion_t
compl, void *data
)
1234 req
->base
.complete
= compl;
1235 req
->base
.data
= data
;
1236 req
->base
.flags
= flags
;
1240 * ablkcipher_request_set_crypt() - set data buffers
1241 * @req: request handle
1242 * @src: source scatter / gather list
1243 * @dst: destination scatter / gather list
1244 * @nbytes: number of bytes to process from @src
1245 * @iv: IV for the cipher operation which must comply with the IV size defined
1246 * by crypto_ablkcipher_ivsize
1248 * This function allows setting of the source data and destination data
1249 * scatter / gather lists.
1251 * For encryption, the source is treated as the plaintext and the
1252 * destination is the ciphertext. For a decryption operation, the use is
1253 * reversed - the source is the ciphertext and the destination is the plaintext.
1255 static inline void ablkcipher_request_set_crypt(
1256 struct ablkcipher_request
*req
,
1257 struct scatterlist
*src
, struct scatterlist
*dst
,
1258 unsigned int nbytes
, void *iv
)
1262 req
->nbytes
= nbytes
;
1267 * DOC: Synchronous Block Cipher API
1269 * The synchronous block cipher API is used with the ciphers of type
1270 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1272 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1273 * used in multiple calls and in parallel, this info should not be changeable
1274 * (unless a lock is used). This applies, for example, to the symmetric key.
1275 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1276 * structure for synchronous blkcipher api. So, its the only state info that can
1277 * be kept for synchronous calls without using a big lock across a tfm.
1279 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1280 * consisting of a template (a block chaining mode) and a single block cipher
1281 * primitive (e.g. AES).
1283 * The plaintext data buffer and the ciphertext data buffer are pointed to
1284 * by using scatter/gather lists. The cipher operation is performed
1285 * on all segments of the provided scatter/gather lists.
1287 * The kernel crypto API supports a cipher operation "in-place" which means that
1288 * the caller may provide the same scatter/gather list for the plaintext and
1289 * cipher text. After the completion of the cipher operation, the plaintext
1290 * data is replaced with the ciphertext data in case of an encryption and vice
1291 * versa for a decryption. The caller must ensure that the scatter/gather lists
1292 * for the output data point to sufficiently large buffers, i.e. multiples of
1293 * the block size of the cipher.
1296 static inline struct crypto_blkcipher
*__crypto_blkcipher_cast(
1297 struct crypto_tfm
*tfm
)
1299 return (struct crypto_blkcipher
*)tfm
;
1302 static inline struct crypto_blkcipher
*crypto_blkcipher_cast(
1303 struct crypto_tfm
*tfm
)
1305 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_BLKCIPHER
);
1306 return __crypto_blkcipher_cast(tfm
);
1310 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1311 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1313 * @type: specifies the type of the cipher
1314 * @mask: specifies the mask for the cipher
1316 * Allocate a cipher handle for a block cipher. The returned struct
1317 * crypto_blkcipher is the cipher handle that is required for any subsequent
1318 * API invocation for that block cipher.
1320 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1321 * of an error, PTR_ERR() returns the error code.
1323 static inline struct crypto_blkcipher
*crypto_alloc_blkcipher(
1324 const char *alg_name
, u32 type
, u32 mask
)
1326 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1327 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
1328 mask
|= CRYPTO_ALG_TYPE_MASK
;
1330 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
1333 static inline struct crypto_tfm
*crypto_blkcipher_tfm(
1334 struct crypto_blkcipher
*tfm
)
1340 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1341 * @tfm: cipher handle to be freed
1343 static inline void crypto_free_blkcipher(struct crypto_blkcipher
*tfm
)
1345 crypto_free_tfm(crypto_blkcipher_tfm(tfm
));
1349 * crypto_has_blkcipher() - Search for the availability of a block cipher
1350 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1352 * @type: specifies the type of the cipher
1353 * @mask: specifies the mask for the cipher
1355 * Return: true when the block cipher is known to the kernel crypto API; false
1358 static inline int crypto_has_blkcipher(const char *alg_name
, u32 type
, u32 mask
)
1360 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1361 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
1362 mask
|= CRYPTO_ALG_TYPE_MASK
;
1364 return crypto_has_alg(alg_name
, type
, mask
);
1368 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1369 * @tfm: cipher handle
1371 * Return: The character string holding the name of the cipher
1373 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher
*tfm
)
1375 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm
));
1378 static inline struct blkcipher_tfm
*crypto_blkcipher_crt(
1379 struct crypto_blkcipher
*tfm
)
1381 return &crypto_blkcipher_tfm(tfm
)->crt_blkcipher
;
1384 static inline struct blkcipher_alg
*crypto_blkcipher_alg(
1385 struct crypto_blkcipher
*tfm
)
1387 return &crypto_blkcipher_tfm(tfm
)->__crt_alg
->cra_blkcipher
;
1391 * crypto_blkcipher_ivsize() - obtain IV size
1392 * @tfm: cipher handle
1394 * The size of the IV for the block cipher referenced by the cipher handle is
1395 * returned. This IV size may be zero if the cipher does not need an IV.
1397 * Return: IV size in bytes
1399 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher
*tfm
)
1401 return crypto_blkcipher_alg(tfm
)->ivsize
;
1405 * crypto_blkcipher_blocksize() - obtain block size of cipher
1406 * @tfm: cipher handle
1408 * The block size for the block cipher referenced with the cipher handle is
1409 * returned. The caller may use that information to allocate appropriate
1410 * memory for the data returned by the encryption or decryption operation.
1412 * Return: block size of cipher
1414 static inline unsigned int crypto_blkcipher_blocksize(
1415 struct crypto_blkcipher
*tfm
)
1417 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm
));
1420 static inline unsigned int crypto_blkcipher_alignmask(
1421 struct crypto_blkcipher
*tfm
)
1423 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm
));
1426 static inline u32
crypto_blkcipher_get_flags(struct crypto_blkcipher
*tfm
)
1428 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm
));
1431 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher
*tfm
,
1434 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm
), flags
);
1437 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher
*tfm
,
1440 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm
), flags
);
1444 * crypto_blkcipher_setkey() - set key for cipher
1445 * @tfm: cipher handle
1446 * @key: buffer holding the key
1447 * @keylen: length of the key in bytes
1449 * The caller provided key is set for the block cipher referenced by the cipher
1452 * Note, the key length determines the cipher type. Many block ciphers implement
1453 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1454 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1457 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1459 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher
*tfm
,
1460 const u8
*key
, unsigned int keylen
)
1462 return crypto_blkcipher_crt(tfm
)->setkey(crypto_blkcipher_tfm(tfm
),
1467 * crypto_blkcipher_encrypt() - encrypt plaintext
1468 * @desc: reference to the block cipher handle with meta data
1469 * @dst: scatter/gather list that is filled by the cipher operation with the
1471 * @src: scatter/gather list that holds the plaintext
1472 * @nbytes: number of bytes of the plaintext to encrypt.
1474 * Encrypt plaintext data using the IV set by the caller with a preceding
1475 * call of crypto_blkcipher_set_iv.
1477 * The blkcipher_desc data structure must be filled by the caller and can
1478 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1479 * with the block cipher handle; desc.flags is filled with either
1480 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1482 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1484 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc
*desc
,
1485 struct scatterlist
*dst
,
1486 struct scatterlist
*src
,
1487 unsigned int nbytes
)
1489 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
1490 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
1494 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1495 * @desc: reference to the block cipher handle with meta data
1496 * @dst: scatter/gather list that is filled by the cipher operation with the
1498 * @src: scatter/gather list that holds the plaintext
1499 * @nbytes: number of bytes of the plaintext to encrypt.
1501 * Encrypt plaintext data with the use of an IV that is solely used for this
1502 * cipher operation. Any previously set IV is not used.
1504 * The blkcipher_desc data structure must be filled by the caller and can
1505 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1506 * with the block cipher handle; desc.info is filled with the IV to be used for
1507 * the current operation; desc.flags is filled with either
1508 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1510 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1512 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc
*desc
,
1513 struct scatterlist
*dst
,
1514 struct scatterlist
*src
,
1515 unsigned int nbytes
)
1517 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
1521 * crypto_blkcipher_decrypt() - decrypt ciphertext
1522 * @desc: reference to the block cipher handle with meta data
1523 * @dst: scatter/gather list that is filled by the cipher operation with the
1525 * @src: scatter/gather list that holds the ciphertext
1526 * @nbytes: number of bytes of the ciphertext to decrypt.
1528 * Decrypt ciphertext data using the IV set by the caller with a preceding
1529 * call of crypto_blkcipher_set_iv.
1531 * The blkcipher_desc data structure must be filled by the caller as documented
1532 * for the crypto_blkcipher_encrypt call above.
1534 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1537 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc
*desc
,
1538 struct scatterlist
*dst
,
1539 struct scatterlist
*src
,
1540 unsigned int nbytes
)
1542 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
1543 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
1547 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1548 * @desc: reference to the block cipher handle with meta data
1549 * @dst: scatter/gather list that is filled by the cipher operation with the
1551 * @src: scatter/gather list that holds the ciphertext
1552 * @nbytes: number of bytes of the ciphertext to decrypt.
1554 * Decrypt ciphertext data with the use of an IV that is solely used for this
1555 * cipher operation. Any previously set IV is not used.
1557 * The blkcipher_desc data structure must be filled by the caller as documented
1558 * for the crypto_blkcipher_encrypt_iv call above.
1560 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1562 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc
*desc
,
1563 struct scatterlist
*dst
,
1564 struct scatterlist
*src
,
1565 unsigned int nbytes
)
1567 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
1571 * crypto_blkcipher_set_iv() - set IV for cipher
1572 * @tfm: cipher handle
1573 * @src: buffer holding the IV
1574 * @len: length of the IV in bytes
1576 * The caller provided IV is set for the block cipher referenced by the cipher
1579 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher
*tfm
,
1580 const u8
*src
, unsigned int len
)
1582 memcpy(crypto_blkcipher_crt(tfm
)->iv
, src
, len
);
1586 * crypto_blkcipher_get_iv() - obtain IV from cipher
1587 * @tfm: cipher handle
1588 * @dst: buffer filled with the IV
1589 * @len: length of the buffer dst
1591 * The caller can obtain the IV set for the block cipher referenced by the
1592 * cipher handle and store it into the user-provided buffer. If the buffer
1593 * has an insufficient space, the IV is truncated to fit the buffer.
1595 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher
*tfm
,
1596 u8
*dst
, unsigned int len
)
1598 memcpy(dst
, crypto_blkcipher_crt(tfm
)->iv
, len
);
1602 * DOC: Single Block Cipher API
1604 * The single block cipher API is used with the ciphers of type
1605 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1607 * Using the single block cipher API calls, operations with the basic cipher
1608 * primitive can be implemented. These cipher primitives exclude any block
1609 * chaining operations including IV handling.
1611 * The purpose of this single block cipher API is to support the implementation
1612 * of templates or other concepts that only need to perform the cipher operation
1613 * on one block at a time. Templates invoke the underlying cipher primitive
1614 * block-wise and process either the input or the output data of these cipher
1618 static inline struct crypto_cipher
*__crypto_cipher_cast(struct crypto_tfm
*tfm
)
1620 return (struct crypto_cipher
*)tfm
;
1623 static inline struct crypto_cipher
*crypto_cipher_cast(struct crypto_tfm
*tfm
)
1625 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
1626 return __crypto_cipher_cast(tfm
);
1630 * crypto_alloc_cipher() - allocate single block cipher handle
1631 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1632 * single block cipher
1633 * @type: specifies the type of the cipher
1634 * @mask: specifies the mask for the cipher
1636 * Allocate a cipher handle for a single block cipher. The returned struct
1637 * crypto_cipher is the cipher handle that is required for any subsequent API
1638 * invocation for that single block cipher.
1640 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1641 * of an error, PTR_ERR() returns the error code.
1643 static inline struct crypto_cipher
*crypto_alloc_cipher(const char *alg_name
,
1646 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1647 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1648 mask
|= CRYPTO_ALG_TYPE_MASK
;
1650 return __crypto_cipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
1653 static inline struct crypto_tfm
*crypto_cipher_tfm(struct crypto_cipher
*tfm
)
1659 * crypto_free_cipher() - zeroize and free the single block cipher handle
1660 * @tfm: cipher handle to be freed
1662 static inline void crypto_free_cipher(struct crypto_cipher
*tfm
)
1664 crypto_free_tfm(crypto_cipher_tfm(tfm
));
1668 * crypto_has_cipher() - Search for the availability of a single block cipher
1669 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1670 * single block cipher
1671 * @type: specifies the type of the cipher
1672 * @mask: specifies the mask for the cipher
1674 * Return: true when the single block cipher is known to the kernel crypto API;
1677 static inline int crypto_has_cipher(const char *alg_name
, u32 type
, u32 mask
)
1679 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1680 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1681 mask
|= CRYPTO_ALG_TYPE_MASK
;
1683 return crypto_has_alg(alg_name
, type
, mask
);
1686 static inline struct cipher_tfm
*crypto_cipher_crt(struct crypto_cipher
*tfm
)
1688 return &crypto_cipher_tfm(tfm
)->crt_cipher
;
1692 * crypto_cipher_blocksize() - obtain block size for cipher
1693 * @tfm: cipher handle
1695 * The block size for the single block cipher referenced with the cipher handle
1696 * tfm is returned. The caller may use that information to allocate appropriate
1697 * memory for the data returned by the encryption or decryption operation
1699 * Return: block size of cipher
1701 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher
*tfm
)
1703 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm
));
1706 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher
*tfm
)
1708 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm
));
1711 static inline u32
crypto_cipher_get_flags(struct crypto_cipher
*tfm
)
1713 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm
));
1716 static inline void crypto_cipher_set_flags(struct crypto_cipher
*tfm
,
1719 crypto_tfm_set_flags(crypto_cipher_tfm(tfm
), flags
);
1722 static inline void crypto_cipher_clear_flags(struct crypto_cipher
*tfm
,
1725 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm
), flags
);
1729 * crypto_cipher_setkey() - set key for cipher
1730 * @tfm: cipher handle
1731 * @key: buffer holding the key
1732 * @keylen: length of the key in bytes
1734 * The caller provided key is set for the single block cipher referenced by the
1737 * Note, the key length determines the cipher type. Many block ciphers implement
1738 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1739 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1742 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1744 static inline int crypto_cipher_setkey(struct crypto_cipher
*tfm
,
1745 const u8
*key
, unsigned int keylen
)
1747 return crypto_cipher_crt(tfm
)->cit_setkey(crypto_cipher_tfm(tfm
),
1752 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1753 * @tfm: cipher handle
1754 * @dst: points to the buffer that will be filled with the ciphertext
1755 * @src: buffer holding the plaintext to be encrypted
1757 * Invoke the encryption operation of one block. The caller must ensure that
1758 * the plaintext and ciphertext buffers are at least one block in size.
1760 static inline void crypto_cipher_encrypt_one(struct crypto_cipher
*tfm
,
1761 u8
*dst
, const u8
*src
)
1763 crypto_cipher_crt(tfm
)->cit_encrypt_one(crypto_cipher_tfm(tfm
),
1768 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1769 * @tfm: cipher handle
1770 * @dst: points to the buffer that will be filled with the plaintext
1771 * @src: buffer holding the ciphertext to be decrypted
1773 * Invoke the decryption operation of one block. The caller must ensure that
1774 * the plaintext and ciphertext buffers are at least one block in size.
1776 static inline void crypto_cipher_decrypt_one(struct crypto_cipher
*tfm
,
1777 u8
*dst
, const u8
*src
)
1779 crypto_cipher_crt(tfm
)->cit_decrypt_one(crypto_cipher_tfm(tfm
),
1783 static inline struct crypto_comp
*__crypto_comp_cast(struct crypto_tfm
*tfm
)
1785 return (struct crypto_comp
*)tfm
;
1788 static inline struct crypto_comp
*crypto_comp_cast(struct crypto_tfm
*tfm
)
1790 BUG_ON((crypto_tfm_alg_type(tfm
) ^ CRYPTO_ALG_TYPE_COMPRESS
) &
1791 CRYPTO_ALG_TYPE_MASK
);
1792 return __crypto_comp_cast(tfm
);
1795 static inline struct crypto_comp
*crypto_alloc_comp(const char *alg_name
,
1798 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1799 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
1800 mask
|= CRYPTO_ALG_TYPE_MASK
;
1802 return __crypto_comp_cast(crypto_alloc_base(alg_name
, type
, mask
));
1805 static inline struct crypto_tfm
*crypto_comp_tfm(struct crypto_comp
*tfm
)
1810 static inline void crypto_free_comp(struct crypto_comp
*tfm
)
1812 crypto_free_tfm(crypto_comp_tfm(tfm
));
1815 static inline int crypto_has_comp(const char *alg_name
, u32 type
, u32 mask
)
1817 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1818 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
1819 mask
|= CRYPTO_ALG_TYPE_MASK
;
1821 return crypto_has_alg(alg_name
, type
, mask
);
1824 static inline const char *crypto_comp_name(struct crypto_comp
*tfm
)
1826 return crypto_tfm_alg_name(crypto_comp_tfm(tfm
));
1829 static inline struct compress_tfm
*crypto_comp_crt(struct crypto_comp
*tfm
)
1831 return &crypto_comp_tfm(tfm
)->crt_compress
;
1834 static inline int crypto_comp_compress(struct crypto_comp
*tfm
,
1835 const u8
*src
, unsigned int slen
,
1836 u8
*dst
, unsigned int *dlen
)
1838 return crypto_comp_crt(tfm
)->cot_compress(crypto_comp_tfm(tfm
),
1839 src
, slen
, dst
, dlen
);
1842 static inline int crypto_comp_decompress(struct crypto_comp
*tfm
,
1843 const u8
*src
, unsigned int slen
,
1844 u8
*dst
, unsigned int *dlen
)
1846 return crypto_comp_crt(tfm
)->cot_decompress(crypto_comp_tfm(tfm
),
1847 src
, slen
, dst
, dlen
);
1850 #endif /* _LINUX_CRYPTO_H */