1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Scatterlist Cryptographic API.
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_SKCIPHER 0x00000005
45 #define CRYPTO_ALG_TYPE_KPP 0x00000008
46 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a
47 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b
48 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
49 #define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
50 #define CRYPTO_ALG_TYPE_HASH 0x0000000e
51 #define CRYPTO_ALG_TYPE_SHASH 0x0000000e
52 #define CRYPTO_ALG_TYPE_AHASH 0x0000000f
54 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
55 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
56 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e
58 #define CRYPTO_ALG_LARVAL 0x00000010
59 #define CRYPTO_ALG_DEAD 0x00000020
60 #define CRYPTO_ALG_DYING 0x00000040
61 #define CRYPTO_ALG_ASYNC 0x00000080
64 * Set this bit if and only if the algorithm requires another algorithm of
65 * the same type to handle corner cases.
67 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
70 * Set if the algorithm has passed automated run-time testing. Note that
71 * if there is no run-time testing for a given algorithm it is considered
75 #define CRYPTO_ALG_TESTED 0x00000400
78 * Set if the algorithm is an instance that is built from templates.
80 #define CRYPTO_ALG_INSTANCE 0x00000800
82 /* Set this bit if the algorithm provided is hardware accelerated but
83 * not available to userspace via instruction set or so.
85 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
88 * Mark a cipher as a service implementation only usable by another
89 * cipher and never by a normal user of the kernel crypto API
91 #define CRYPTO_ALG_INTERNAL 0x00002000
94 * Set if the algorithm has a ->setkey() method but can be used without
95 * calling it first, i.e. there is a default key.
97 #define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
100 * Don't trigger module loading
102 #define CRYPTO_NOLOAD 0x00008000
105 * Transform masks and values (for crt_flags).
107 #define CRYPTO_TFM_NEED_KEY 0x00000001
109 #define CRYPTO_TFM_REQ_MASK 0x000fff00
110 #define CRYPTO_TFM_RES_MASK 0xfff00000
112 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x00000100
113 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
114 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
115 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
116 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
117 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
118 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
119 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
122 * Miscellaneous stuff.
124 #define CRYPTO_MAX_ALG_NAME 128
127 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
128 * declaration) is used to ensure that the crypto_tfm context structure is
129 * aligned correctly for the given architecture so that there are no alignment
130 * faults for C data types. In particular, this is required on platforms such
131 * as arm where pointers are 32-bit aligned but there are data types such as
132 * u64 which require 64-bit alignment.
134 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
136 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
139 struct crypto_async_request;
143 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
146 * DOC: Block Cipher Context Data Structures
148 * These data structures define the operating context for each block cipher
152 struct crypto_async_request {
153 struct list_head list;
154 crypto_completion_t complete;
156 struct crypto_tfm *tfm;
162 * DOC: Block Cipher Algorithm Definitions
164 * These data structures define modular crypto algorithm implementations,
165 * managed via crypto_register_alg() and crypto_unregister_alg().
169 * struct cipher_alg - single-block symmetric ciphers definition
170 * @cia_min_keysize: Minimum key size supported by the transformation. This is
171 * the smallest key length supported by this transformation
172 * algorithm. This must be set to one of the pre-defined
173 * values as this is not hardware specific. Possible values
174 * for this field can be found via git grep "_MIN_KEY_SIZE"
176 * @cia_max_keysize: Maximum key size supported by the transformation. This is
177 * the largest key length supported by this transformation
178 * algorithm. This must be set to one of the pre-defined values
179 * as this is not hardware specific. Possible values for this
180 * field can be found via git grep "_MAX_KEY_SIZE"
182 * @cia_setkey: Set key for the transformation. This function is used to either
183 * program a supplied key into the hardware or store the key in the
184 * transformation context for programming it later. Note that this
185 * function does modify the transformation context. This function
186 * can be called multiple times during the existence of the
187 * transformation object, so one must make sure the key is properly
188 * reprogrammed into the hardware. This function is also
189 * responsible for checking the key length for validity.
190 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
191 * single block of data, which must be @cra_blocksize big. This
192 * always operates on a full @cra_blocksize and it is not possible
193 * to encrypt a block of smaller size. The supplied buffers must
194 * therefore also be at least of @cra_blocksize size. Both the
195 * input and output buffers are always aligned to @cra_alignmask.
196 * In case either of the input or output buffer supplied by user
197 * of the crypto API is not aligned to @cra_alignmask, the crypto
198 * API will re-align the buffers. The re-alignment means that a
199 * new buffer will be allocated, the data will be copied into the
200 * new buffer, then the processing will happen on the new buffer,
201 * then the data will be copied back into the original buffer and
202 * finally the new buffer will be freed. In case a software
203 * fallback was put in place in the @cra_init call, this function
204 * might need to use the fallback if the algorithm doesn't support
205 * all of the key sizes. In case the key was stored in
206 * transformation context, the key might need to be re-programmed
207 * into the hardware in this function. This function shall not
208 * modify the transformation context, as this function may be
209 * called in parallel with the same transformation object.
210 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
211 * @cia_encrypt, and the conditions are exactly the same.
213 * All fields are mandatory and must be filled.
216 unsigned int cia_min_keysize;
217 unsigned int cia_max_keysize;
218 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
219 unsigned int keylen);
220 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
221 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
225 * struct compress_alg - compression/decompression algorithm
226 * @coa_compress: Compress a buffer of specified length, storing the resulting
227 * data in the specified buffer. Return the length of the
228 * compressed data in dlen.
229 * @coa_decompress: Decompress the source buffer, storing the uncompressed
230 * data in the specified buffer. The length of the data is
233 * All fields are mandatory.
235 struct compress_alg {
236 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
237 unsigned int slen, u8 *dst, unsigned int *dlen);
238 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
239 unsigned int slen, u8 *dst, unsigned int *dlen);
242 #ifdef CONFIG_CRYPTO_STATS
244 * struct crypto_istat_aead - statistics for AEAD algorithm
245 * @encrypt_cnt: number of encrypt requests
246 * @encrypt_tlen: total data size handled by encrypt requests
247 * @decrypt_cnt: number of decrypt requests
248 * @decrypt_tlen: total data size handled by decrypt requests
249 * @err_cnt: number of error for AEAD requests
251 struct crypto_istat_aead {
252 atomic64_t encrypt_cnt;
253 atomic64_t encrypt_tlen;
254 atomic64_t decrypt_cnt;
255 atomic64_t decrypt_tlen;
260 * struct crypto_istat_akcipher - statistics for akcipher algorithm
261 * @encrypt_cnt: number of encrypt requests
262 * @encrypt_tlen: total data size handled by encrypt requests
263 * @decrypt_cnt: number of decrypt requests
264 * @decrypt_tlen: total data size handled by decrypt requests
265 * @verify_cnt: number of verify operation
266 * @sign_cnt: number of sign requests
267 * @err_cnt: number of error for akcipher requests
269 struct crypto_istat_akcipher {
270 atomic64_t encrypt_cnt;
271 atomic64_t encrypt_tlen;
272 atomic64_t decrypt_cnt;
273 atomic64_t decrypt_tlen;
274 atomic64_t verify_cnt;
280 * struct crypto_istat_cipher - statistics for cipher algorithm
281 * @encrypt_cnt: number of encrypt requests
282 * @encrypt_tlen: total data size handled by encrypt requests
283 * @decrypt_cnt: number of decrypt requests
284 * @decrypt_tlen: total data size handled by decrypt requests
285 * @err_cnt: number of error for cipher requests
287 struct crypto_istat_cipher {
288 atomic64_t encrypt_cnt;
289 atomic64_t encrypt_tlen;
290 atomic64_t decrypt_cnt;
291 atomic64_t decrypt_tlen;
296 * struct crypto_istat_compress - statistics for compress algorithm
297 * @compress_cnt: number of compress requests
298 * @compress_tlen: total data size handled by compress requests
299 * @decompress_cnt: number of decompress requests
300 * @decompress_tlen: total data size handled by decompress requests
301 * @err_cnt: number of error for compress requests
303 struct crypto_istat_compress {
304 atomic64_t compress_cnt;
305 atomic64_t compress_tlen;
306 atomic64_t decompress_cnt;
307 atomic64_t decompress_tlen;
312 * struct crypto_istat_hash - statistics for has algorithm
313 * @hash_cnt: number of hash requests
314 * @hash_tlen: total data size hashed
315 * @err_cnt: number of error for hash requests
317 struct crypto_istat_hash {
319 atomic64_t hash_tlen;
324 * struct crypto_istat_kpp - statistics for KPP algorithm
325 * @setsecret_cnt: number of setsecrey operation
326 * @generate_public_key_cnt: number of generate_public_key operation
327 * @compute_shared_secret_cnt: number of compute_shared_secret operation
328 * @err_cnt: number of error for KPP requests
330 struct crypto_istat_kpp {
331 atomic64_t setsecret_cnt;
332 atomic64_t generate_public_key_cnt;
333 atomic64_t compute_shared_secret_cnt;
338 * struct crypto_istat_rng: statistics for RNG algorithm
339 * @generate_cnt: number of RNG generate requests
340 * @generate_tlen: total data size of generated data by the RNG
341 * @seed_cnt: number of times the RNG was seeded
342 * @err_cnt: number of error for RNG requests
344 struct crypto_istat_rng {
345 atomic64_t generate_cnt;
346 atomic64_t generate_tlen;
350 #endif /* CONFIG_CRYPTO_STATS */
352 #define cra_cipher cra_u.cipher
353 #define cra_compress cra_u.compress
356 * struct crypto_alg - definition of a cryptograpic cipher algorithm
357 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
358 * CRYPTO_ALG_* flags for the flags which go in here. Those are
359 * used for fine-tuning the description of the transformation
361 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
362 * of the smallest possible unit which can be transformed with
363 * this algorithm. The users must respect this value.
364 * In case of HASH transformation, it is possible for a smaller
365 * block than @cra_blocksize to be passed to the crypto API for
366 * transformation, in case of any other transformation type, an
367 * error will be returned upon any attempt to transform smaller
368 * than @cra_blocksize chunks.
369 * @cra_ctxsize: Size of the operational context of the transformation. This
370 * value informs the kernel crypto API about the memory size
371 * needed to be allocated for the transformation context.
372 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
373 * buffer containing the input data for the algorithm must be
374 * aligned to this alignment mask. The data buffer for the
375 * output data must be aligned to this alignment mask. Note that
376 * the Crypto API will do the re-alignment in software, but
377 * only under special conditions and there is a performance hit.
378 * The re-alignment happens at these occasions for different
379 * @cra_u types: cipher -- For both input data and output data
380 * buffer; ahash -- For output hash destination buf; shash --
381 * For output hash destination buf.
382 * This is needed on hardware which is flawed by design and
383 * cannot pick data from arbitrary addresses.
384 * @cra_priority: Priority of this transformation implementation. In case
385 * multiple transformations with same @cra_name are available to
386 * the Crypto API, the kernel will use the one with highest
388 * @cra_name: Generic name (usable by multiple implementations) of the
389 * transformation algorithm. This is the name of the transformation
390 * itself. This field is used by the kernel when looking up the
391 * providers of particular transformation.
392 * @cra_driver_name: Unique name of the transformation provider. This is the
393 * name of the provider of the transformation. This can be any
394 * arbitrary value, but in the usual case, this contains the
395 * name of the chip or provider and the name of the
396 * transformation algorithm.
397 * @cra_type: Type of the cryptographic transformation. This is a pointer to
398 * struct crypto_type, which implements callbacks common for all
399 * transformation types. There are multiple options, such as
400 * &crypto_skcipher_type, &crypto_ahash_type, &crypto_rng_type.
401 * This field might be empty. In that case, there are no common
402 * callbacks. This is the case for: cipher, compress, shash.
403 * @cra_u: Callbacks implementing the transformation. This is a union of
404 * multiple structures. Depending on the type of transformation selected
405 * by @cra_type and @cra_flags above, the associated structure must be
406 * filled with callbacks. This field might be empty. This is the case
408 * @cra_init: Initialize the cryptographic transformation object. This function
409 * is used to initialize the cryptographic transformation object.
410 * This function is called only once at the instantiation time, right
411 * after the transformation context was allocated. In case the
412 * cryptographic hardware has some special requirements which need to
413 * be handled by software, this function shall check for the precise
414 * requirement of the transformation and put any software fallbacks
416 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
417 * counterpart to @cra_init, used to remove various changes set in
419 * @cra_u.cipher: Union member which contains a single-block symmetric cipher
420 * definition. See @struct @cipher_alg.
421 * @cra_u.compress: Union member which contains a (de)compression algorithm.
422 * See @struct @compress_alg.
423 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
424 * @cra_list: internally used
425 * @cra_users: internally used
426 * @cra_refcnt: internally used
427 * @cra_destroy: internally used
429 * @stats: union of all possible crypto_istat_xxx structures
430 * @stats.aead: statistics for AEAD algorithm
431 * @stats.akcipher: statistics for akcipher algorithm
432 * @stats.cipher: statistics for cipher algorithm
433 * @stats.compress: statistics for compress algorithm
434 * @stats.hash: statistics for hash algorithm
435 * @stats.rng: statistics for rng algorithm
436 * @stats.kpp: statistics for KPP algorithm
438 * The struct crypto_alg describes a generic Crypto API algorithm and is common
439 * for all of the transformations. Any variable not documented here shall not
440 * be used by a cipher implementation as it is internal to the Crypto API.
443 struct list_head cra_list;
444 struct list_head cra_users;
447 unsigned int cra_blocksize;
448 unsigned int cra_ctxsize;
449 unsigned int cra_alignmask;
452 refcount_t cra_refcnt;
454 char cra_name[CRYPTO_MAX_ALG_NAME];
455 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
457 const struct crypto_type *cra_type;
460 struct cipher_alg cipher;
461 struct compress_alg compress;
464 int (*cra_init)(struct crypto_tfm *tfm);
465 void (*cra_exit)(struct crypto_tfm *tfm);
466 void (*cra_destroy)(struct crypto_alg *alg);
468 struct module *cra_module;
470 #ifdef CONFIG_CRYPTO_STATS
472 struct crypto_istat_aead aead;
473 struct crypto_istat_akcipher akcipher;
474 struct crypto_istat_cipher cipher;
475 struct crypto_istat_compress compress;
476 struct crypto_istat_hash hash;
477 struct crypto_istat_rng rng;
478 struct crypto_istat_kpp kpp;
480 #endif /* CONFIG_CRYPTO_STATS */
482 } CRYPTO_MINALIGN_ATTR;
484 #ifdef CONFIG_CRYPTO_STATS
485 void crypto_stats_init(struct crypto_alg *alg);
486 void crypto_stats_get(struct crypto_alg *alg);
487 void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
488 void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
489 void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg);
490 void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg);
491 void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
492 void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
493 void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg);
494 void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg);
495 void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg);
496 void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg);
497 void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret);
498 void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret);
499 void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret);
500 void crypto_stats_rng_seed(struct crypto_alg *alg, int ret);
501 void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret);
502 void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
503 void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
505 static inline void crypto_stats_init(struct crypto_alg *alg)
507 static inline void crypto_stats_get(struct crypto_alg *alg)
509 static inline void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
511 static inline void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
513 static inline void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg)
515 static inline void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg)
517 static inline void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
519 static inline void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
521 static inline void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg)
523 static inline void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg)
525 static inline void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
527 static inline void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
529 static inline void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
531 static inline void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret)
533 static inline void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret)
535 static inline void crypto_stats_rng_seed(struct crypto_alg *alg, int ret)
537 static inline void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret)
539 static inline void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
541 static inline void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
545 * A helper struct for waiting for completion of async crypto ops
548 struct completion completion;
553 * Macro for declaring a crypto op async wait object on stack
555 #define DECLARE_CRYPTO_WAIT(_wait) \
556 struct crypto_wait _wait = { \
557 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
560 * Async ops completion helper functioons
562 void crypto_req_done(struct crypto_async_request *req, int err);
564 static inline int crypto_wait_req(int err, struct crypto_wait *wait)
569 wait_for_completion(&wait->completion);
570 reinit_completion(&wait->completion);
578 static inline void crypto_init_wait(struct crypto_wait *wait)
580 init_completion(&wait->completion);
584 * Algorithm registration interface.
586 int crypto_register_alg(struct crypto_alg *alg);
587 int crypto_unregister_alg(struct crypto_alg *alg);
588 int crypto_register_algs(struct crypto_alg *algs, int count);
589 int crypto_unregister_algs(struct crypto_alg *algs, int count);
592 * Algorithm query interface.
594 int crypto_has_alg(const char *name, u32 type, u32 mask);
597 * Transforms: user-instantiated objects which encapsulate algorithms
598 * and core processing logic. Managed via crypto_alloc_*() and
599 * crypto_free_*(), as well as the various helpers below.
603 int (*cit_setkey)(struct crypto_tfm *tfm,
604 const u8 *key, unsigned int keylen);
605 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
606 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
609 struct compress_tfm {
610 int (*cot_compress)(struct crypto_tfm *tfm,
611 const u8 *src, unsigned int slen,
612 u8 *dst, unsigned int *dlen);
613 int (*cot_decompress)(struct crypto_tfm *tfm,
614 const u8 *src, unsigned int slen,
615 u8 *dst, unsigned int *dlen);
618 #define crt_cipher crt_u.cipher
619 #define crt_compress crt_u.compress
626 struct cipher_tfm cipher;
627 struct compress_tfm compress;
630 void (*exit)(struct crypto_tfm *tfm);
632 struct crypto_alg *__crt_alg;
634 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
637 struct crypto_cipher {
638 struct crypto_tfm base;
642 struct crypto_tfm base;
653 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
655 /* Maximum number of (rtattr) parameters for each template. */
656 #define CRYPTO_MAX_ATTRS 32
658 struct crypto_attr_alg {
659 char name[CRYPTO_MAX_ALG_NAME];
662 struct crypto_attr_type {
667 struct crypto_attr_u32 {
672 * Transform user interface.
675 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
676 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
678 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
680 return crypto_destroy_tfm(tfm, tfm);
683 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
686 * Transform helpers which query the underlying algorithm.
688 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
690 return tfm->__crt_alg->cra_name;
693 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
695 return tfm->__crt_alg->cra_driver_name;
698 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
700 return tfm->__crt_alg->cra_priority;
703 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
705 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
708 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
710 return tfm->__crt_alg->cra_blocksize;
713 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
715 return tfm->__crt_alg->cra_alignmask;
718 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
720 return tfm->crt_flags;
723 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
725 tfm->crt_flags |= flags;
728 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
730 tfm->crt_flags &= ~flags;
733 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
735 return tfm->__crt_ctx;
738 static inline unsigned int crypto_tfm_ctx_alignment(void)
740 struct crypto_tfm *tfm;
741 return __alignof__(tfm->__crt_ctx);
745 * DOC: Single Block Cipher API
747 * The single block cipher API is used with the ciphers of type
748 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
750 * Using the single block cipher API calls, operations with the basic cipher
751 * primitive can be implemented. These cipher primitives exclude any block
752 * chaining operations including IV handling.
754 * The purpose of this single block cipher API is to support the implementation
755 * of templates or other concepts that only need to perform the cipher operation
756 * on one block at a time. Templates invoke the underlying cipher primitive
757 * block-wise and process either the input or the output data of these cipher
761 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
763 return (struct crypto_cipher *)tfm;
766 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
768 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
769 return __crypto_cipher_cast(tfm);
773 * crypto_alloc_cipher() - allocate single block cipher handle
774 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
775 * single block cipher
776 * @type: specifies the type of the cipher
777 * @mask: specifies the mask for the cipher
779 * Allocate a cipher handle for a single block cipher. The returned struct
780 * crypto_cipher is the cipher handle that is required for any subsequent API
781 * invocation for that single block cipher.
783 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
784 * of an error, PTR_ERR() returns the error code.
786 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
789 type &= ~CRYPTO_ALG_TYPE_MASK;
790 type |= CRYPTO_ALG_TYPE_CIPHER;
791 mask |= CRYPTO_ALG_TYPE_MASK;
793 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
796 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
802 * crypto_free_cipher() - zeroize and free the single block cipher handle
803 * @tfm: cipher handle to be freed
805 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
807 crypto_free_tfm(crypto_cipher_tfm(tfm));
811 * crypto_has_cipher() - Search for the availability of a single block cipher
812 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
813 * single block cipher
814 * @type: specifies the type of the cipher
815 * @mask: specifies the mask for the cipher
817 * Return: true when the single block cipher is known to the kernel crypto API;
820 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
822 type &= ~CRYPTO_ALG_TYPE_MASK;
823 type |= CRYPTO_ALG_TYPE_CIPHER;
824 mask |= CRYPTO_ALG_TYPE_MASK;
826 return crypto_has_alg(alg_name, type, mask);
829 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
831 return &crypto_cipher_tfm(tfm)->crt_cipher;
835 * crypto_cipher_blocksize() - obtain block size for cipher
836 * @tfm: cipher handle
838 * The block size for the single block cipher referenced with the cipher handle
839 * tfm is returned. The caller may use that information to allocate appropriate
840 * memory for the data returned by the encryption or decryption operation
842 * Return: block size of cipher
844 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
846 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
849 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
851 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
854 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
856 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
859 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
862 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
865 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
868 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
872 * crypto_cipher_setkey() - set key for cipher
873 * @tfm: cipher handle
874 * @key: buffer holding the key
875 * @keylen: length of the key in bytes
877 * The caller provided key is set for the single block cipher referenced by the
880 * Note, the key length determines the cipher type. Many block ciphers implement
881 * different cipher modes depending on the key size, such as AES-128 vs AES-192
882 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
885 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
887 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
888 const u8 *key, unsigned int keylen)
890 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
895 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
896 * @tfm: cipher handle
897 * @dst: points to the buffer that will be filled with the ciphertext
898 * @src: buffer holding the plaintext to be encrypted
900 * Invoke the encryption operation of one block. The caller must ensure that
901 * the plaintext and ciphertext buffers are at least one block in size.
903 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
904 u8 *dst, const u8 *src)
906 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
911 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
912 * @tfm: cipher handle
913 * @dst: points to the buffer that will be filled with the plaintext
914 * @src: buffer holding the ciphertext to be decrypted
916 * Invoke the decryption operation of one block. The caller must ensure that
917 * the plaintext and ciphertext buffers are at least one block in size.
919 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
920 u8 *dst, const u8 *src)
922 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
926 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
928 return (struct crypto_comp *)tfm;
931 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
933 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
934 CRYPTO_ALG_TYPE_MASK);
935 return __crypto_comp_cast(tfm);
938 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
941 type &= ~CRYPTO_ALG_TYPE_MASK;
942 type |= CRYPTO_ALG_TYPE_COMPRESS;
943 mask |= CRYPTO_ALG_TYPE_MASK;
945 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
948 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
953 static inline void crypto_free_comp(struct crypto_comp *tfm)
955 crypto_free_tfm(crypto_comp_tfm(tfm));
958 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
960 type &= ~CRYPTO_ALG_TYPE_MASK;
961 type |= CRYPTO_ALG_TYPE_COMPRESS;
962 mask |= CRYPTO_ALG_TYPE_MASK;
964 return crypto_has_alg(alg_name, type, mask);
967 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
969 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
972 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
974 return &crypto_comp_tfm(tfm)->crt_compress;
977 static inline int crypto_comp_compress(struct crypto_comp *tfm,
978 const u8 *src, unsigned int slen,
979 u8 *dst, unsigned int *dlen)
981 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
982 src, slen, dst, dlen);
985 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
986 const u8 *src, unsigned int slen,
987 u8 *dst, unsigned int *dlen)
989 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
990 src, slen, dst, dlen);
993 #endif /* _LINUX_CRYPTO_H */