2 * Scatterlist Cryptographic API.
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
17 #ifndef _LINUX_CRYPTO_H
18 #define _LINUX_CRYPTO_H
20 #include <linux/atomic.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/bug.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
29 * Autoloaded crypto modules should only use a prefixed name to avoid allowing
30 * arbitrary modules to be loaded. Loading from userspace may still need the
31 * unprefixed names, so retains those aliases as well.
32 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
33 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
34 * expands twice on the same line. Instead, use a separate base name for the
37 #define MODULE_ALIAS_CRYPTO(name) \
38 __MODULE_INFO(alias, alias_userspace, name); \
39 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
42 * Algorithm masks and types.
44 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
45 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
46 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
47 #define CRYPTO_ALG_TYPE_AEAD 0x00000003
48 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
49 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
50 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
51 #define CRYPTO_ALG_TYPE_DIGEST 0x00000008
52 #define CRYPTO_ALG_TYPE_HASH 0x00000008
53 #define CRYPTO_ALG_TYPE_SHASH 0x00000009
54 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
55 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
56 #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
58 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
59 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
60 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
62 #define CRYPTO_ALG_LARVAL 0x00000010
63 #define CRYPTO_ALG_DEAD 0x00000020
64 #define CRYPTO_ALG_DYING 0x00000040
65 #define CRYPTO_ALG_ASYNC 0x00000080
68 * Set this bit if and only if the algorithm requires another algorithm of
69 * the same type to handle corner cases.
71 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
74 * This bit is set for symmetric key ciphers that have already been wrapped
75 * with a generic IV generator to prevent them from being wrapped again.
77 #define CRYPTO_ALG_GENIV 0x00000200
80 * Set if the algorithm has passed automated run-time testing. Note that
81 * if there is no run-time testing for a given algorithm it is considered
85 #define CRYPTO_ALG_TESTED 0x00000400
88 * Set if the algorithm is an instance that is build from templates.
90 #define CRYPTO_ALG_INSTANCE 0x00000800
92 /* Set this bit if the algorithm provided is hardware accelerated but
93 * not available to userspace via instruction set or so.
95 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
98 * Mark a cipher as a service implementation only usable by another
99 * cipher and never by a normal user of the kernel crypto API
101 #define CRYPTO_ALG_INTERNAL 0x00002000
104 * Transform masks and values (for crt_flags).
106 #define CRYPTO_TFM_REQ_MASK 0x000fff00
107 #define CRYPTO_TFM_RES_MASK 0xfff00000
109 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
110 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
111 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
112 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
113 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
114 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
115 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
116 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
119 * Miscellaneous stuff.
121 #define CRYPTO_MAX_ALG_NAME 64
124 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
125 * declaration) is used to ensure that the crypto_tfm context structure is
126 * aligned correctly for the given architecture so that there are no alignment
127 * faults for C data types. In particular, this is required on platforms such
128 * as arm where pointers are 32-bit aligned but there are data types such as
129 * u64 which require 64-bit alignment.
131 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
133 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
136 struct crypto_ablkcipher
;
137 struct crypto_async_request
;
139 struct crypto_blkcipher
;
144 struct aead_givcrypt_request
;
145 struct skcipher_givcrypt_request
;
147 typedef void (*crypto_completion_t
)(struct crypto_async_request
*req
, int err
);
150 * DOC: Block Cipher Context Data Structures
152 * These data structures define the operating context for each block cipher
156 struct crypto_async_request
{
157 struct list_head list
;
158 crypto_completion_t complete
;
160 struct crypto_tfm
*tfm
;
165 struct ablkcipher_request
{
166 struct crypto_async_request base
;
172 struct scatterlist
*src
;
173 struct scatterlist
*dst
;
175 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
179 * struct aead_request - AEAD request
180 * @base: Common attributes for async crypto requests
181 * @assoclen: Length in bytes of associated data for authentication
182 * @cryptlen: Length of data to be encrypted or decrypted
183 * @iv: Initialisation vector
184 * @assoc: Associated data
186 * @dst: Destination data
187 * @__ctx: Start of private context data
189 struct aead_request
{
190 struct crypto_async_request base
;
192 unsigned int assoclen
;
193 unsigned int cryptlen
;
197 struct scatterlist
*assoc
;
198 struct scatterlist
*src
;
199 struct scatterlist
*dst
;
201 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
204 struct blkcipher_desc
{
205 struct crypto_blkcipher
*tfm
;
211 struct crypto_tfm
*tfm
;
212 void (*crfn
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
213 unsigned int (*prfn
)(const struct cipher_desc
*desc
, u8
*dst
,
214 const u8
*src
, unsigned int nbytes
);
219 struct crypto_hash
*tfm
;
224 * DOC: Block Cipher Algorithm Definitions
226 * These data structures define modular crypto algorithm implementations,
227 * managed via crypto_register_alg() and crypto_unregister_alg().
231 * struct ablkcipher_alg - asynchronous block cipher definition
232 * @min_keysize: Minimum key size supported by the transformation. This is the
233 * smallest key length supported by this transformation algorithm.
234 * This must be set to one of the pre-defined values as this is
235 * not hardware specific. Possible values for this field can be
236 * found via git grep "_MIN_KEY_SIZE" include/crypto/
237 * @max_keysize: Maximum key size supported by the transformation. This is the
238 * largest key length supported by this transformation algorithm.
239 * This must be set to one of the pre-defined values as this is
240 * not hardware specific. Possible values for this field can be
241 * found via git grep "_MAX_KEY_SIZE" include/crypto/
242 * @setkey: Set key for the transformation. This function is used to either
243 * program a supplied key into the hardware or store the key in the
244 * transformation context for programming it later. Note that this
245 * function does modify the transformation context. This function can
246 * be called multiple times during the existence of the transformation
247 * object, so one must make sure the key is properly reprogrammed into
248 * the hardware. This function is also responsible for checking the key
249 * length for validity. In case a software fallback was put in place in
250 * the @cra_init call, this function might need to use the fallback if
251 * the algorithm doesn't support all of the key sizes.
252 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
253 * the supplied scatterlist containing the blocks of data. The crypto
254 * API consumer is responsible for aligning the entries of the
255 * scatterlist properly and making sure the chunks are correctly
256 * sized. In case a software fallback was put in place in the
257 * @cra_init call, this function might need to use the fallback if
258 * the algorithm doesn't support all of the key sizes. In case the
259 * key was stored in transformation context, the key might need to be
260 * re-programmed into the hardware in this function. This function
261 * shall not modify the transformation context, as this function may
262 * be called in parallel with the same transformation object.
263 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
264 * and the conditions are exactly the same.
265 * @givencrypt: Update the IV for encryption. With this function, a cipher
266 * implementation may provide the function on how to update the IV
268 * @givdecrypt: Update the IV for decryption. This is the reverse of
270 * @geniv: The transformation implementation may use an "IV generator" provided
271 * by the kernel crypto API. Several use cases have a predefined
272 * approach how IVs are to be updated. For such use cases, the kernel
273 * crypto API provides ready-to-use implementations that can be
274 * referenced with this variable.
275 * @ivsize: IV size applicable for transformation. The consumer must provide an
276 * IV of exactly that size to perform the encrypt or decrypt operation.
278 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
279 * mandatory and must be filled.
281 struct ablkcipher_alg
{
282 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
283 unsigned int keylen
);
284 int (*encrypt
)(struct ablkcipher_request
*req
);
285 int (*decrypt
)(struct ablkcipher_request
*req
);
286 int (*givencrypt
)(struct skcipher_givcrypt_request
*req
);
287 int (*givdecrypt
)(struct skcipher_givcrypt_request
*req
);
291 unsigned int min_keysize
;
292 unsigned int max_keysize
;
297 * struct aead_alg - AEAD cipher definition
298 * @maxauthsize: Set the maximum authentication tag size supported by the
299 * transformation. A transformation may support smaller tag sizes.
300 * As the authentication tag is a message digest to ensure the
301 * integrity of the encrypted data, a consumer typically wants the
302 * largest authentication tag possible as defined by this
304 * @setauthsize: Set authentication size for the AEAD transformation. This
305 * function is used to specify the consumer requested size of the
306 * authentication tag to be either generated by the transformation
307 * during encryption or the size of the authentication tag to be
308 * supplied during the decryption operation. This function is also
309 * responsible for checking the authentication tag size for
311 * @setkey: see struct ablkcipher_alg
312 * @encrypt: see struct ablkcipher_alg
313 * @decrypt: see struct ablkcipher_alg
314 * @givencrypt: see struct ablkcipher_alg
315 * @givdecrypt: see struct ablkcipher_alg
316 * @geniv: see struct ablkcipher_alg
317 * @ivsize: see struct ablkcipher_alg
319 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
320 * mandatory and must be filled.
323 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
324 unsigned int keylen
);
325 int (*setauthsize
)(struct crypto_aead
*tfm
, unsigned int authsize
);
326 int (*encrypt
)(struct aead_request
*req
);
327 int (*decrypt
)(struct aead_request
*req
);
328 int (*givencrypt
)(struct aead_givcrypt_request
*req
);
329 int (*givdecrypt
)(struct aead_givcrypt_request
*req
);
334 unsigned int maxauthsize
;
338 * struct blkcipher_alg - synchronous block cipher definition
339 * @min_keysize: see struct ablkcipher_alg
340 * @max_keysize: see struct ablkcipher_alg
341 * @setkey: see struct ablkcipher_alg
342 * @encrypt: see struct ablkcipher_alg
343 * @decrypt: see struct ablkcipher_alg
344 * @geniv: see struct ablkcipher_alg
345 * @ivsize: see struct ablkcipher_alg
347 * All fields except @geniv and @ivsize are mandatory and must be filled.
349 struct blkcipher_alg
{
350 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
351 unsigned int keylen
);
352 int (*encrypt
)(struct blkcipher_desc
*desc
,
353 struct scatterlist
*dst
, struct scatterlist
*src
,
354 unsigned int nbytes
);
355 int (*decrypt
)(struct blkcipher_desc
*desc
,
356 struct scatterlist
*dst
, struct scatterlist
*src
,
357 unsigned int nbytes
);
361 unsigned int min_keysize
;
362 unsigned int max_keysize
;
367 * struct cipher_alg - single-block symmetric ciphers definition
368 * @cia_min_keysize: Minimum key size supported by the transformation. This is
369 * the smallest key length supported by this transformation
370 * algorithm. This must be set to one of the pre-defined
371 * values as this is not hardware specific. Possible values
372 * for this field can be found via git grep "_MIN_KEY_SIZE"
374 * @cia_max_keysize: Maximum key size supported by the transformation. This is
375 * the largest key length supported by this transformation
376 * algorithm. This must be set to one of the pre-defined values
377 * as this is not hardware specific. Possible values for this
378 * field can be found via git grep "_MAX_KEY_SIZE"
380 * @cia_setkey: Set key for the transformation. This function is used to either
381 * program a supplied key into the hardware or store the key in the
382 * transformation context for programming it later. Note that this
383 * function does modify the transformation context. This function
384 * can be called multiple times during the existence of the
385 * transformation object, so one must make sure the key is properly
386 * reprogrammed into the hardware. This function is also
387 * responsible for checking the key length for validity.
388 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
389 * single block of data, which must be @cra_blocksize big. This
390 * always operates on a full @cra_blocksize and it is not possible
391 * to encrypt a block of smaller size. The supplied buffers must
392 * therefore also be at least of @cra_blocksize size. Both the
393 * input and output buffers are always aligned to @cra_alignmask.
394 * In case either of the input or output buffer supplied by user
395 * of the crypto API is not aligned to @cra_alignmask, the crypto
396 * API will re-align the buffers. The re-alignment means that a
397 * new buffer will be allocated, the data will be copied into the
398 * new buffer, then the processing will happen on the new buffer,
399 * then the data will be copied back into the original buffer and
400 * finally the new buffer will be freed. In case a software
401 * fallback was put in place in the @cra_init call, this function
402 * might need to use the fallback if the algorithm doesn't support
403 * all of the key sizes. In case the key was stored in
404 * transformation context, the key might need to be re-programmed
405 * into the hardware in this function. This function shall not
406 * modify the transformation context, as this function may be
407 * called in parallel with the same transformation object.
408 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
409 * @cia_encrypt, and the conditions are exactly the same.
411 * All fields are mandatory and must be filled.
414 unsigned int cia_min_keysize
;
415 unsigned int cia_max_keysize
;
416 int (*cia_setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
417 unsigned int keylen
);
418 void (*cia_encrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
419 void (*cia_decrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
422 struct compress_alg
{
423 int (*coa_compress
)(struct crypto_tfm
*tfm
, const u8
*src
,
424 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
425 int (*coa_decompress
)(struct crypto_tfm
*tfm
, const u8
*src
,
426 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
430 * struct rng_alg - random number generator definition
431 * @rng_make_random: The function defined by this variable obtains a random
432 * number. The random number generator transform must generate
433 * the random number out of the context provided with this
435 * @rng_reset: Reset of the random number generator by clearing the entire state.
436 * With the invocation of this function call, the random number
437 * generator shall completely reinitialize its state. If the random
438 * number generator requires a seed for setting up a new state,
439 * the seed must be provided by the consumer while invoking this
440 * function. The required size of the seed is defined with
442 * @seedsize: The seed size required for a random number generator
443 * initialization defined with this variable. Some random number
444 * generators like the SP800-90A DRBG does not require a seed as the
445 * seeding is implemented internally without the need of support by
446 * the consumer. In this case, the seed size is set to zero.
449 int (*rng_make_random
)(struct crypto_rng
*tfm
, u8
*rdata
,
451 int (*rng_reset
)(struct crypto_rng
*tfm
, u8
*seed
, unsigned int slen
);
453 unsigned int seedsize
;
457 #define cra_ablkcipher cra_u.ablkcipher
458 #define cra_aead cra_u.aead
459 #define cra_blkcipher cra_u.blkcipher
460 #define cra_cipher cra_u.cipher
461 #define cra_compress cra_u.compress
462 #define cra_rng cra_u.rng
465 * struct crypto_alg - definition of a cryptograpic cipher algorithm
466 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
467 * CRYPTO_ALG_* flags for the flags which go in here. Those are
468 * used for fine-tuning the description of the transformation
470 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
471 * of the smallest possible unit which can be transformed with
472 * this algorithm. The users must respect this value.
473 * In case of HASH transformation, it is possible for a smaller
474 * block than @cra_blocksize to be passed to the crypto API for
475 * transformation, in case of any other transformation type, an
476 * error will be returned upon any attempt to transform smaller
477 * than @cra_blocksize chunks.
478 * @cra_ctxsize: Size of the operational context of the transformation. This
479 * value informs the kernel crypto API about the memory size
480 * needed to be allocated for the transformation context.
481 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
482 * buffer containing the input data for the algorithm must be
483 * aligned to this alignment mask. The data buffer for the
484 * output data must be aligned to this alignment mask. Note that
485 * the Crypto API will do the re-alignment in software, but
486 * only under special conditions and there is a performance hit.
487 * The re-alignment happens at these occasions for different
488 * @cra_u types: cipher -- For both input data and output data
489 * buffer; ahash -- For output hash destination buf; shash --
490 * For output hash destination buf.
491 * This is needed on hardware which is flawed by design and
492 * cannot pick data from arbitrary addresses.
493 * @cra_priority: Priority of this transformation implementation. In case
494 * multiple transformations with same @cra_name are available to
495 * the Crypto API, the kernel will use the one with highest
497 * @cra_name: Generic name (usable by multiple implementations) of the
498 * transformation algorithm. This is the name of the transformation
499 * itself. This field is used by the kernel when looking up the
500 * providers of particular transformation.
501 * @cra_driver_name: Unique name of the transformation provider. This is the
502 * name of the provider of the transformation. This can be any
503 * arbitrary value, but in the usual case, this contains the
504 * name of the chip or provider and the name of the
505 * transformation algorithm.
506 * @cra_type: Type of the cryptographic transformation. This is a pointer to
507 * struct crypto_type, which implements callbacks common for all
508 * trasnformation types. There are multiple options:
509 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
510 * &crypto_ahash_type, &crypto_aead_type, &crypto_rng_type.
511 * This field might be empty. In that case, there are no common
512 * callbacks. This is the case for: cipher, compress, shash.
513 * @cra_u: Callbacks implementing the transformation. This is a union of
514 * multiple structures. Depending on the type of transformation selected
515 * by @cra_type and @cra_flags above, the associated structure must be
516 * filled with callbacks. This field might be empty. This is the case
518 * @cra_init: Initialize the cryptographic transformation object. This function
519 * is used to initialize the cryptographic transformation object.
520 * This function is called only once at the instantiation time, right
521 * after the transformation context was allocated. In case the
522 * cryptographic hardware has some special requirements which need to
523 * be handled by software, this function shall check for the precise
524 * requirement of the transformation and put any software fallbacks
526 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
527 * counterpart to @cra_init, used to remove various changes set in
529 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
530 * @cra_list: internally used
531 * @cra_users: internally used
532 * @cra_refcnt: internally used
533 * @cra_destroy: internally used
535 * The struct crypto_alg describes a generic Crypto API algorithm and is common
536 * for all of the transformations. Any variable not documented here shall not
537 * be used by a cipher implementation as it is internal to the Crypto API.
540 struct list_head cra_list
;
541 struct list_head cra_users
;
544 unsigned int cra_blocksize
;
545 unsigned int cra_ctxsize
;
546 unsigned int cra_alignmask
;
551 char cra_name
[CRYPTO_MAX_ALG_NAME
];
552 char cra_driver_name
[CRYPTO_MAX_ALG_NAME
];
554 const struct crypto_type
*cra_type
;
557 struct ablkcipher_alg ablkcipher
;
558 struct aead_alg aead
;
559 struct blkcipher_alg blkcipher
;
560 struct cipher_alg cipher
;
561 struct compress_alg compress
;
565 int (*cra_init
)(struct crypto_tfm
*tfm
);
566 void (*cra_exit
)(struct crypto_tfm
*tfm
);
567 void (*cra_destroy
)(struct crypto_alg
*alg
);
569 struct module
*cra_module
;
573 * Algorithm registration interface.
575 int crypto_register_alg(struct crypto_alg
*alg
);
576 int crypto_unregister_alg(struct crypto_alg
*alg
);
577 int crypto_register_algs(struct crypto_alg
*algs
, int count
);
578 int crypto_unregister_algs(struct crypto_alg
*algs
, int count
);
581 * Algorithm query interface.
583 int crypto_has_alg(const char *name
, u32 type
, u32 mask
);
586 * Transforms: user-instantiated objects which encapsulate algorithms
587 * and core processing logic. Managed via crypto_alloc_*() and
588 * crypto_free_*(), as well as the various helpers below.
591 struct ablkcipher_tfm
{
592 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
593 unsigned int keylen
);
594 int (*encrypt
)(struct ablkcipher_request
*req
);
595 int (*decrypt
)(struct ablkcipher_request
*req
);
596 int (*givencrypt
)(struct skcipher_givcrypt_request
*req
);
597 int (*givdecrypt
)(struct skcipher_givcrypt_request
*req
);
599 struct crypto_ablkcipher
*base
;
602 unsigned int reqsize
;
606 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
607 unsigned int keylen
);
608 int (*encrypt
)(struct aead_request
*req
);
609 int (*decrypt
)(struct aead_request
*req
);
610 int (*givencrypt
)(struct aead_givcrypt_request
*req
);
611 int (*givdecrypt
)(struct aead_givcrypt_request
*req
);
613 struct crypto_aead
*base
;
616 unsigned int authsize
;
617 unsigned int reqsize
;
620 struct blkcipher_tfm
{
622 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
623 unsigned int keylen
);
624 int (*encrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
625 struct scatterlist
*src
, unsigned int nbytes
);
626 int (*decrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
627 struct scatterlist
*src
, unsigned int nbytes
);
631 int (*cit_setkey
)(struct crypto_tfm
*tfm
,
632 const u8
*key
, unsigned int keylen
);
633 void (*cit_encrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
634 void (*cit_decrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
638 int (*init
)(struct hash_desc
*desc
);
639 int (*update
)(struct hash_desc
*desc
,
640 struct scatterlist
*sg
, unsigned int nsg
);
641 int (*final
)(struct hash_desc
*desc
, u8
*out
);
642 int (*digest
)(struct hash_desc
*desc
, struct scatterlist
*sg
,
643 unsigned int nsg
, u8
*out
);
644 int (*setkey
)(struct crypto_hash
*tfm
, const u8
*key
,
645 unsigned int keylen
);
646 unsigned int digestsize
;
649 struct compress_tfm
{
650 int (*cot_compress
)(struct crypto_tfm
*tfm
,
651 const u8
*src
, unsigned int slen
,
652 u8
*dst
, unsigned int *dlen
);
653 int (*cot_decompress
)(struct crypto_tfm
*tfm
,
654 const u8
*src
, unsigned int slen
,
655 u8
*dst
, unsigned int *dlen
);
658 #define crt_ablkcipher crt_u.ablkcipher
659 #define crt_aead crt_u.aead
660 #define crt_blkcipher crt_u.blkcipher
661 #define crt_cipher crt_u.cipher
662 #define crt_hash crt_u.hash
663 #define crt_compress crt_u.compress
670 struct ablkcipher_tfm ablkcipher
;
671 struct aead_tfm aead
;
672 struct blkcipher_tfm blkcipher
;
673 struct cipher_tfm cipher
;
674 struct hash_tfm hash
;
675 struct compress_tfm compress
;
678 void (*exit
)(struct crypto_tfm
*tfm
);
680 struct crypto_alg
*__crt_alg
;
682 void *__crt_ctx
[] CRYPTO_MINALIGN_ATTR
;
685 struct crypto_ablkcipher
{
686 struct crypto_tfm base
;
690 struct crypto_tfm base
;
693 struct crypto_blkcipher
{
694 struct crypto_tfm base
;
697 struct crypto_cipher
{
698 struct crypto_tfm base
;
702 struct crypto_tfm base
;
706 struct crypto_tfm base
;
717 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
719 /* Maximum number of (rtattr) parameters for each template. */
720 #define CRYPTO_MAX_ATTRS 32
722 struct crypto_attr_alg
{
723 char name
[CRYPTO_MAX_ALG_NAME
];
726 struct crypto_attr_type
{
731 struct crypto_attr_u32
{
736 * Transform user interface.
739 struct crypto_tfm
*crypto_alloc_base(const char *alg_name
, u32 type
, u32 mask
);
740 void crypto_destroy_tfm(void *mem
, struct crypto_tfm
*tfm
);
742 static inline void crypto_free_tfm(struct crypto_tfm
*tfm
)
744 return crypto_destroy_tfm(tfm
, tfm
);
747 int alg_test(const char *driver
, const char *alg
, u32 type
, u32 mask
);
750 * Transform helpers which query the underlying algorithm.
752 static inline const char *crypto_tfm_alg_name(struct crypto_tfm
*tfm
)
754 return tfm
->__crt_alg
->cra_name
;
757 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm
*tfm
)
759 return tfm
->__crt_alg
->cra_driver_name
;
762 static inline int crypto_tfm_alg_priority(struct crypto_tfm
*tfm
)
764 return tfm
->__crt_alg
->cra_priority
;
767 static inline u32
crypto_tfm_alg_type(struct crypto_tfm
*tfm
)
769 return tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
;
772 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm
*tfm
)
774 return tfm
->__crt_alg
->cra_blocksize
;
777 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm
*tfm
)
779 return tfm
->__crt_alg
->cra_alignmask
;
782 static inline u32
crypto_tfm_get_flags(struct crypto_tfm
*tfm
)
784 return tfm
->crt_flags
;
787 static inline void crypto_tfm_set_flags(struct crypto_tfm
*tfm
, u32 flags
)
789 tfm
->crt_flags
|= flags
;
792 static inline void crypto_tfm_clear_flags(struct crypto_tfm
*tfm
, u32 flags
)
794 tfm
->crt_flags
&= ~flags
;
797 static inline void *crypto_tfm_ctx(struct crypto_tfm
*tfm
)
799 return tfm
->__crt_ctx
;
802 static inline unsigned int crypto_tfm_ctx_alignment(void)
804 struct crypto_tfm
*tfm
;
805 return __alignof__(tfm
->__crt_ctx
);
811 static inline struct crypto_ablkcipher
*__crypto_ablkcipher_cast(
812 struct crypto_tfm
*tfm
)
814 return (struct crypto_ablkcipher
*)tfm
;
817 static inline u32
crypto_skcipher_type(u32 type
)
819 type
&= ~(CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
);
820 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
824 static inline u32
crypto_skcipher_mask(u32 mask
)
826 mask
&= ~(CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
);
827 mask
|= CRYPTO_ALG_TYPE_BLKCIPHER_MASK
;
832 * DOC: Asynchronous Block Cipher API
834 * Asynchronous block cipher API is used with the ciphers of type
835 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
837 * Asynchronous cipher operations imply that the function invocation for a
838 * cipher request returns immediately before the completion of the operation.
839 * The cipher request is scheduled as a separate kernel thread and therefore
840 * load-balanced on the different CPUs via the process scheduler. To allow
841 * the kernel crypto API to inform the caller about the completion of a cipher
842 * request, the caller must provide a callback function. That function is
843 * invoked with the cipher handle when the request completes.
845 * To support the asynchronous operation, additional information than just the
846 * cipher handle must be supplied to the kernel crypto API. That additional
847 * information is given by filling in the ablkcipher_request data structure.
849 * For the asynchronous block cipher API, the state is maintained with the tfm
850 * cipher handle. A single tfm can be used across multiple calls and in
851 * parallel. For asynchronous block cipher calls, context data supplied and
852 * only used by the caller can be referenced the request data structure in
853 * addition to the IV used for the cipher request. The maintenance of such
854 * state information would be important for a crypto driver implementer to
855 * have, because when calling the callback function upon completion of the
856 * cipher operation, that callback function may need some information about
857 * which operation just finished if it invoked multiple in parallel. This
858 * state information is unused by the kernel crypto API.
862 * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle
863 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
865 * @type: specifies the type of the cipher
866 * @mask: specifies the mask for the cipher
868 * Allocate a cipher handle for an ablkcipher. The returned struct
869 * crypto_ablkcipher is the cipher handle that is required for any subsequent
870 * API invocation for that ablkcipher.
872 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
873 * of an error, PTR_ERR() returns the error code.
875 struct crypto_ablkcipher
*crypto_alloc_ablkcipher(const char *alg_name
,
878 static inline struct crypto_tfm
*crypto_ablkcipher_tfm(
879 struct crypto_ablkcipher
*tfm
)
885 * crypto_free_ablkcipher() - zeroize and free cipher handle
886 * @tfm: cipher handle to be freed
888 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher
*tfm
)
890 crypto_free_tfm(crypto_ablkcipher_tfm(tfm
));
894 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
895 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
897 * @type: specifies the type of the cipher
898 * @mask: specifies the mask for the cipher
900 * Return: true when the ablkcipher is known to the kernel crypto API; false
903 static inline int crypto_has_ablkcipher(const char *alg_name
, u32 type
,
906 return crypto_has_alg(alg_name
, crypto_skcipher_type(type
),
907 crypto_skcipher_mask(mask
));
910 static inline struct ablkcipher_tfm
*crypto_ablkcipher_crt(
911 struct crypto_ablkcipher
*tfm
)
913 return &crypto_ablkcipher_tfm(tfm
)->crt_ablkcipher
;
917 * crypto_ablkcipher_ivsize() - obtain IV size
918 * @tfm: cipher handle
920 * The size of the IV for the ablkcipher referenced by the cipher handle is
921 * returned. This IV size may be zero if the cipher does not need an IV.
923 * Return: IV size in bytes
925 static inline unsigned int crypto_ablkcipher_ivsize(
926 struct crypto_ablkcipher
*tfm
)
928 return crypto_ablkcipher_crt(tfm
)->ivsize
;
932 * crypto_ablkcipher_blocksize() - obtain block size of cipher
933 * @tfm: cipher handle
935 * The block size for the ablkcipher referenced with the cipher handle is
936 * returned. The caller may use that information to allocate appropriate
937 * memory for the data returned by the encryption or decryption operation
939 * Return: block size of cipher
941 static inline unsigned int crypto_ablkcipher_blocksize(
942 struct crypto_ablkcipher
*tfm
)
944 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm
));
947 static inline unsigned int crypto_ablkcipher_alignmask(
948 struct crypto_ablkcipher
*tfm
)
950 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm
));
953 static inline u32
crypto_ablkcipher_get_flags(struct crypto_ablkcipher
*tfm
)
955 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm
));
958 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher
*tfm
,
961 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm
), flags
);
964 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher
*tfm
,
967 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm
), flags
);
971 * crypto_ablkcipher_setkey() - set key for cipher
972 * @tfm: cipher handle
973 * @key: buffer holding the key
974 * @keylen: length of the key in bytes
976 * The caller provided key is set for the ablkcipher referenced by the cipher
979 * Note, the key length determines the cipher type. Many block ciphers implement
980 * different cipher modes depending on the key size, such as AES-128 vs AES-192
981 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
984 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
986 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher
*tfm
,
987 const u8
*key
, unsigned int keylen
)
989 struct ablkcipher_tfm
*crt
= crypto_ablkcipher_crt(tfm
);
991 return crt
->setkey(crt
->base
, key
, keylen
);
995 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
996 * @req: ablkcipher_request out of which the cipher handle is to be obtained
998 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
1001 * Return: crypto_ablkcipher handle
1003 static inline struct crypto_ablkcipher
*crypto_ablkcipher_reqtfm(
1004 struct ablkcipher_request
*req
)
1006 return __crypto_ablkcipher_cast(req
->base
.tfm
);
1010 * crypto_ablkcipher_encrypt() - encrypt plaintext
1011 * @req: reference to the ablkcipher_request handle that holds all information
1012 * needed to perform the cipher operation
1014 * Encrypt plaintext data using the ablkcipher_request handle. That data
1015 * structure and how it is filled with data is discussed with the
1016 * ablkcipher_request_* functions.
1018 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1020 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request
*req
)
1022 struct ablkcipher_tfm
*crt
=
1023 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
1024 return crt
->encrypt(req
);
1028 * crypto_ablkcipher_decrypt() - decrypt ciphertext
1029 * @req: reference to the ablkcipher_request handle that holds all information
1030 * needed to perform the cipher operation
1032 * Decrypt ciphertext data using the ablkcipher_request handle. That data
1033 * structure and how it is filled with data is discussed with the
1034 * ablkcipher_request_* functions.
1036 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1038 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request
*req
)
1040 struct ablkcipher_tfm
*crt
=
1041 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
1042 return crt
->decrypt(req
);
1046 * DOC: Asynchronous Cipher Request Handle
1048 * The ablkcipher_request data structure contains all pointers to data
1049 * required for the asynchronous cipher operation. This includes the cipher
1050 * handle (which can be used by multiple ablkcipher_request instances), pointer
1051 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
1052 * as a handle to the ablkcipher_request_* API calls in a similar way as
1053 * ablkcipher handle to the crypto_ablkcipher_* API calls.
1057 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
1058 * @tfm: cipher handle
1060 * Return: number of bytes
1062 static inline unsigned int crypto_ablkcipher_reqsize(
1063 struct crypto_ablkcipher
*tfm
)
1065 return crypto_ablkcipher_crt(tfm
)->reqsize
;
1069 * ablkcipher_request_set_tfm() - update cipher handle reference in request
1070 * @req: request handle to be modified
1071 * @tfm: cipher handle that shall be added to the request handle
1073 * Allow the caller to replace the existing ablkcipher handle in the request
1074 * data structure with a different one.
1076 static inline void ablkcipher_request_set_tfm(
1077 struct ablkcipher_request
*req
, struct crypto_ablkcipher
*tfm
)
1079 req
->base
.tfm
= crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm
)->base
);
1082 static inline struct ablkcipher_request
*ablkcipher_request_cast(
1083 struct crypto_async_request
*req
)
1085 return container_of(req
, struct ablkcipher_request
, base
);
1089 * ablkcipher_request_alloc() - allocate request data structure
1090 * @tfm: cipher handle to be registered with the request
1091 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1093 * Allocate the request data structure that must be used with the ablkcipher
1094 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1095 * handle is registered in the request data structure.
1097 * Return: allocated request handle in case of success; IS_ERR() is true in case
1098 * of an error, PTR_ERR() returns the error code.
1100 static inline struct ablkcipher_request
*ablkcipher_request_alloc(
1101 struct crypto_ablkcipher
*tfm
, gfp_t gfp
)
1103 struct ablkcipher_request
*req
;
1105 req
= kmalloc(sizeof(struct ablkcipher_request
) +
1106 crypto_ablkcipher_reqsize(tfm
), gfp
);
1109 ablkcipher_request_set_tfm(req
, tfm
);
1115 * ablkcipher_request_free() - zeroize and free request data structure
1116 * @req: request data structure cipher handle to be freed
1118 static inline void ablkcipher_request_free(struct ablkcipher_request
*req
)
1124 * ablkcipher_request_set_callback() - set asynchronous callback function
1125 * @req: request handle
1126 * @flags: specify zero or an ORing of the flags
1127 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1128 * increase the wait queue beyond the initial maximum size;
1129 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1130 * @compl: callback function pointer to be registered with the request handle
1131 * @data: The data pointer refers to memory that is not used by the kernel
1132 * crypto API, but provided to the callback function for it to use. Here,
1133 * the caller can provide a reference to memory the callback function can
1134 * operate on. As the callback function is invoked asynchronously to the
1135 * related functionality, it may need to access data structures of the
1136 * related functionality which can be referenced using this pointer. The
1137 * callback function can access the memory via the "data" field in the
1138 * crypto_async_request data structure provided to the callback function.
1140 * This function allows setting the callback function that is triggered once the
1141 * cipher operation completes.
1143 * The callback function is registered with the ablkcipher_request handle and
1144 * must comply with the following template
1146 * void callback_function(struct crypto_async_request *req, int error)
1148 static inline void ablkcipher_request_set_callback(
1149 struct ablkcipher_request
*req
,
1150 u32 flags
, crypto_completion_t
compl, void *data
)
1152 req
->base
.complete
= compl;
1153 req
->base
.data
= data
;
1154 req
->base
.flags
= flags
;
1158 * ablkcipher_request_set_crypt() - set data buffers
1159 * @req: request handle
1160 * @src: source scatter / gather list
1161 * @dst: destination scatter / gather list
1162 * @nbytes: number of bytes to process from @src
1163 * @iv: IV for the cipher operation which must comply with the IV size defined
1164 * by crypto_ablkcipher_ivsize
1166 * This function allows setting of the source data and destination data
1167 * scatter / gather lists.
1169 * For encryption, the source is treated as the plaintext and the
1170 * destination is the ciphertext. For a decryption operation, the use is
1171 * reversed - the source is the ciphertext and the destination is the plaintext.
1173 static inline void ablkcipher_request_set_crypt(
1174 struct ablkcipher_request
*req
,
1175 struct scatterlist
*src
, struct scatterlist
*dst
,
1176 unsigned int nbytes
, void *iv
)
1180 req
->nbytes
= nbytes
;
1185 * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API
1187 * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD
1188 * (listed as type "aead" in /proc/crypto)
1190 * The most prominent examples for this type of encryption is GCM and CCM.
1191 * However, the kernel supports other types of AEAD ciphers which are defined
1192 * with the following cipher string:
1194 * authenc(keyed message digest, block cipher)
1196 * For example: authenc(hmac(sha256), cbc(aes))
1198 * The example code provided for the asynchronous block cipher operation
1199 * applies here as well. Naturally all *ablkcipher* symbols must be exchanged
1200 * the *aead* pendants discussed in the following. In addtion, for the AEAD
1201 * operation, the aead_request_set_assoc function must be used to set the
1202 * pointer to the associated data memory location before performing the
1203 * encryption or decryption operation. In case of an encryption, the associated
1204 * data memory is filled during the encryption operation. For decryption, the
1205 * associated data memory must contain data that is used to verify the integrity
1206 * of the decrypted data. Another deviation from the asynchronous block cipher
1207 * operation is that the caller should explicitly check for -EBADMSG of the
1208 * crypto_aead_decrypt. That error indicates an authentication error, i.e.
1209 * a breach in the integrity of the message. In essence, that -EBADMSG error
1210 * code is the key bonus an AEAD cipher has over "standard" block chaining
1214 static inline struct crypto_aead
*__crypto_aead_cast(struct crypto_tfm
*tfm
)
1216 return (struct crypto_aead
*)tfm
;
1220 * crypto_alloc_aead() - allocate AEAD cipher handle
1221 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1223 * @type: specifies the type of the cipher
1224 * @mask: specifies the mask for the cipher
1226 * Allocate a cipher handle for an AEAD. The returned struct
1227 * crypto_aead is the cipher handle that is required for any subsequent
1228 * API invocation for that AEAD.
1230 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1231 * of an error, PTR_ERR() returns the error code.
1233 struct crypto_aead
*crypto_alloc_aead(const char *alg_name
, u32 type
, u32 mask
);
1235 static inline struct crypto_tfm
*crypto_aead_tfm(struct crypto_aead
*tfm
)
1241 * crypto_free_aead() - zeroize and free aead handle
1242 * @tfm: cipher handle to be freed
1244 static inline void crypto_free_aead(struct crypto_aead
*tfm
)
1246 crypto_free_tfm(crypto_aead_tfm(tfm
));
1249 static inline struct aead_tfm
*crypto_aead_crt(struct crypto_aead
*tfm
)
1251 return &crypto_aead_tfm(tfm
)->crt_aead
;
1255 * crypto_aead_ivsize() - obtain IV size
1256 * @tfm: cipher handle
1258 * The size of the IV for the aead referenced by the cipher handle is
1259 * returned. This IV size may be zero if the cipher does not need an IV.
1261 * Return: IV size in bytes
1263 static inline unsigned int crypto_aead_ivsize(struct crypto_aead
*tfm
)
1265 return crypto_aead_crt(tfm
)->ivsize
;
1269 * crypto_aead_authsize() - obtain maximum authentication data size
1270 * @tfm: cipher handle
1272 * The maximum size of the authentication data for the AEAD cipher referenced
1273 * by the AEAD cipher handle is returned. The authentication data size may be
1274 * zero if the cipher implements a hard-coded maximum.
1276 * The authentication data may also be known as "tag value".
1278 * Return: authentication data size / tag size in bytes
1280 static inline unsigned int crypto_aead_authsize(struct crypto_aead
*tfm
)
1282 return crypto_aead_crt(tfm
)->authsize
;
1286 * crypto_aead_blocksize() - obtain block size of cipher
1287 * @tfm: cipher handle
1289 * The block size for the AEAD referenced with the cipher handle is returned.
1290 * The caller may use that information to allocate appropriate memory for the
1291 * data returned by the encryption or decryption operation
1293 * Return: block size of cipher
1295 static inline unsigned int crypto_aead_blocksize(struct crypto_aead
*tfm
)
1297 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm
));
1300 static inline unsigned int crypto_aead_alignmask(struct crypto_aead
*tfm
)
1302 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm
));
1305 static inline u32
crypto_aead_get_flags(struct crypto_aead
*tfm
)
1307 return crypto_tfm_get_flags(crypto_aead_tfm(tfm
));
1310 static inline void crypto_aead_set_flags(struct crypto_aead
*tfm
, u32 flags
)
1312 crypto_tfm_set_flags(crypto_aead_tfm(tfm
), flags
);
1315 static inline void crypto_aead_clear_flags(struct crypto_aead
*tfm
, u32 flags
)
1317 crypto_tfm_clear_flags(crypto_aead_tfm(tfm
), flags
);
1321 * crypto_aead_setkey() - set key for cipher
1322 * @tfm: cipher handle
1323 * @key: buffer holding the key
1324 * @keylen: length of the key in bytes
1326 * The caller provided key is set for the AEAD referenced by the cipher
1329 * Note, the key length determines the cipher type. Many block ciphers implement
1330 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1331 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1334 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1336 static inline int crypto_aead_setkey(struct crypto_aead
*tfm
, const u8
*key
,
1337 unsigned int keylen
)
1339 struct aead_tfm
*crt
= crypto_aead_crt(tfm
);
1341 return crt
->setkey(crt
->base
, key
, keylen
);
1345 * crypto_aead_setauthsize() - set authentication data size
1346 * @tfm: cipher handle
1347 * @authsize: size of the authentication data / tag in bytes
1349 * Set the authentication data size / tag size. AEAD requires an authentication
1350 * tag (or MAC) in addition to the associated data.
1352 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1354 int crypto_aead_setauthsize(struct crypto_aead
*tfm
, unsigned int authsize
);
1356 static inline struct crypto_aead
*crypto_aead_reqtfm(struct aead_request
*req
)
1358 return __crypto_aead_cast(req
->base
.tfm
);
1362 * crypto_aead_encrypt() - encrypt plaintext
1363 * @req: reference to the aead_request handle that holds all information
1364 * needed to perform the cipher operation
1366 * Encrypt plaintext data using the aead_request handle. That data structure
1367 * and how it is filled with data is discussed with the aead_request_*
1370 * IMPORTANT NOTE The encryption operation creates the authentication data /
1371 * tag. That data is concatenated with the created ciphertext.
1372 * The ciphertext memory size is therefore the given number of
1373 * block cipher blocks + the size defined by the
1374 * crypto_aead_setauthsize invocation. The caller must ensure
1375 * that sufficient memory is available for the ciphertext and
1376 * the authentication tag.
1378 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1380 static inline int crypto_aead_encrypt(struct aead_request
*req
)
1382 return crypto_aead_crt(crypto_aead_reqtfm(req
))->encrypt(req
);
1386 * crypto_aead_decrypt() - decrypt ciphertext
1387 * @req: reference to the ablkcipher_request handle that holds all information
1388 * needed to perform the cipher operation
1390 * Decrypt ciphertext data using the aead_request handle. That data structure
1391 * and how it is filled with data is discussed with the aead_request_*
1394 * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
1395 * authentication data / tag. That authentication data / tag
1396 * must have the size defined by the crypto_aead_setauthsize
1400 * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
1401 * cipher operation performs the authentication of the data during the
1402 * decryption operation. Therefore, the function returns this error if
1403 * the authentication of the ciphertext was unsuccessful (i.e. the
1404 * integrity of the ciphertext or the associated data was violated);
1405 * < 0 if an error occurred.
1407 static inline int crypto_aead_decrypt(struct aead_request
*req
)
1409 if (req
->cryptlen
< crypto_aead_authsize(crypto_aead_reqtfm(req
)))
1412 return crypto_aead_crt(crypto_aead_reqtfm(req
))->decrypt(req
);
1416 * DOC: Asynchronous AEAD Request Handle
1418 * The aead_request data structure contains all pointers to data required for
1419 * the AEAD cipher operation. This includes the cipher handle (which can be
1420 * used by multiple aead_request instances), pointer to plaintext and
1421 * ciphertext, asynchronous callback function, etc. It acts as a handle to the
1422 * aead_request_* API calls in a similar way as AEAD handle to the
1423 * crypto_aead_* API calls.
1427 * crypto_aead_reqsize() - obtain size of the request data structure
1428 * @tfm: cipher handle
1430 * Return: number of bytes
1432 static inline unsigned int crypto_aead_reqsize(struct crypto_aead
*tfm
)
1434 return crypto_aead_crt(tfm
)->reqsize
;
1438 * aead_request_set_tfm() - update cipher handle reference in request
1439 * @req: request handle to be modified
1440 * @tfm: cipher handle that shall be added to the request handle
1442 * Allow the caller to replace the existing aead handle in the request
1443 * data structure with a different one.
1445 static inline void aead_request_set_tfm(struct aead_request
*req
,
1446 struct crypto_aead
*tfm
)
1448 req
->base
.tfm
= crypto_aead_tfm(crypto_aead_crt(tfm
)->base
);
1452 * aead_request_alloc() - allocate request data structure
1453 * @tfm: cipher handle to be registered with the request
1454 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1456 * Allocate the request data structure that must be used with the AEAD
1457 * encrypt and decrypt API calls. During the allocation, the provided aead
1458 * handle is registered in the request data structure.
1460 * Return: allocated request handle in case of success; IS_ERR() is true in case
1461 * of an error, PTR_ERR() returns the error code.
1463 static inline struct aead_request
*aead_request_alloc(struct crypto_aead
*tfm
,
1466 struct aead_request
*req
;
1468 req
= kmalloc(sizeof(*req
) + crypto_aead_reqsize(tfm
), gfp
);
1471 aead_request_set_tfm(req
, tfm
);
1477 * aead_request_free() - zeroize and free request data structure
1478 * @req: request data structure cipher handle to be freed
1480 static inline void aead_request_free(struct aead_request
*req
)
1486 * aead_request_set_callback() - set asynchronous callback function
1487 * @req: request handle
1488 * @flags: specify zero or an ORing of the flags
1489 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1490 * increase the wait queue beyond the initial maximum size;
1491 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1492 * @compl: callback function pointer to be registered with the request handle
1493 * @data: The data pointer refers to memory that is not used by the kernel
1494 * crypto API, but provided to the callback function for it to use. Here,
1495 * the caller can provide a reference to memory the callback function can
1496 * operate on. As the callback function is invoked asynchronously to the
1497 * related functionality, it may need to access data structures of the
1498 * related functionality which can be referenced using this pointer. The
1499 * callback function can access the memory via the "data" field in the
1500 * crypto_async_request data structure provided to the callback function.
1502 * Setting the callback function that is triggered once the cipher operation
1505 * The callback function is registered with the aead_request handle and
1506 * must comply with the following template
1508 * void callback_function(struct crypto_async_request *req, int error)
1510 static inline void aead_request_set_callback(struct aead_request
*req
,
1512 crypto_completion_t
compl,
1515 req
->base
.complete
= compl;
1516 req
->base
.data
= data
;
1517 req
->base
.flags
= flags
;
1521 * aead_request_set_crypt - set data buffers
1522 * @req: request handle
1523 * @src: source scatter / gather list
1524 * @dst: destination scatter / gather list
1525 * @cryptlen: number of bytes to process from @src
1526 * @iv: IV for the cipher operation which must comply with the IV size defined
1527 * by crypto_aead_ivsize()
1529 * Setting the source data and destination data scatter / gather lists.
1531 * For encryption, the source is treated as the plaintext and the
1532 * destination is the ciphertext. For a decryption operation, the use is
1533 * reversed - the source is the ciphertext and the destination is the plaintext.
1535 * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption,
1536 * the caller must concatenate the ciphertext followed by the
1537 * authentication tag and provide the entire data stream to the
1538 * decryption operation (i.e. the data length used for the
1539 * initialization of the scatterlist and the data length for the
1540 * decryption operation is identical). For encryption, however,
1541 * the authentication tag is created while encrypting the data.
1542 * The destination buffer must hold sufficient space for the
1543 * ciphertext and the authentication tag while the encryption
1544 * invocation must only point to the plaintext data size. The
1545 * following code snippet illustrates the memory usage
1546 * buffer = kmalloc(ptbuflen + (enc ? authsize : 0));
1547 * sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0));
1548 * aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv);
1550 static inline void aead_request_set_crypt(struct aead_request
*req
,
1551 struct scatterlist
*src
,
1552 struct scatterlist
*dst
,
1553 unsigned int cryptlen
, u8
*iv
)
1557 req
->cryptlen
= cryptlen
;
1562 * aead_request_set_assoc() - set the associated data scatter / gather list
1563 * @req: request handle
1564 * @assoc: associated data scatter / gather list
1565 * @assoclen: number of bytes to process from @assoc
1567 * For encryption, the memory is filled with the associated data. For
1568 * decryption, the memory must point to the associated data.
1570 static inline void aead_request_set_assoc(struct aead_request
*req
,
1571 struct scatterlist
*assoc
,
1572 unsigned int assoclen
)
1575 req
->assoclen
= assoclen
;
1579 * DOC: Synchronous Block Cipher API
1581 * The synchronous block cipher API is used with the ciphers of type
1582 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1584 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1585 * used in multiple calls and in parallel, this info should not be changeable
1586 * (unless a lock is used). This applies, for example, to the symmetric key.
1587 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1588 * structure for synchronous blkcipher api. So, its the only state info that can
1589 * be kept for synchronous calls without using a big lock across a tfm.
1591 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1592 * consisting of a template (a block chaining mode) and a single block cipher
1593 * primitive (e.g. AES).
1595 * The plaintext data buffer and the ciphertext data buffer are pointed to
1596 * by using scatter/gather lists. The cipher operation is performed
1597 * on all segments of the provided scatter/gather lists.
1599 * The kernel crypto API supports a cipher operation "in-place" which means that
1600 * the caller may provide the same scatter/gather list for the plaintext and
1601 * cipher text. After the completion of the cipher operation, the plaintext
1602 * data is replaced with the ciphertext data in case of an encryption and vice
1603 * versa for a decryption. The caller must ensure that the scatter/gather lists
1604 * for the output data point to sufficiently large buffers, i.e. multiples of
1605 * the block size of the cipher.
1608 static inline struct crypto_blkcipher
*__crypto_blkcipher_cast(
1609 struct crypto_tfm
*tfm
)
1611 return (struct crypto_blkcipher
*)tfm
;
1614 static inline struct crypto_blkcipher
*crypto_blkcipher_cast(
1615 struct crypto_tfm
*tfm
)
1617 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_BLKCIPHER
);
1618 return __crypto_blkcipher_cast(tfm
);
1622 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1623 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1625 * @type: specifies the type of the cipher
1626 * @mask: specifies the mask for the cipher
1628 * Allocate a cipher handle for a block cipher. The returned struct
1629 * crypto_blkcipher is the cipher handle that is required for any subsequent
1630 * API invocation for that block cipher.
1632 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1633 * of an error, PTR_ERR() returns the error code.
1635 static inline struct crypto_blkcipher
*crypto_alloc_blkcipher(
1636 const char *alg_name
, u32 type
, u32 mask
)
1638 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1639 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
1640 mask
|= CRYPTO_ALG_TYPE_MASK
;
1642 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
1645 static inline struct crypto_tfm
*crypto_blkcipher_tfm(
1646 struct crypto_blkcipher
*tfm
)
1652 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1653 * @tfm: cipher handle to be freed
1655 static inline void crypto_free_blkcipher(struct crypto_blkcipher
*tfm
)
1657 crypto_free_tfm(crypto_blkcipher_tfm(tfm
));
1661 * crypto_has_blkcipher() - Search for the availability of a block cipher
1662 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1664 * @type: specifies the type of the cipher
1665 * @mask: specifies the mask for the cipher
1667 * Return: true when the block cipher is known to the kernel crypto API; false
1670 static inline int crypto_has_blkcipher(const char *alg_name
, u32 type
, u32 mask
)
1672 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1673 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
1674 mask
|= CRYPTO_ALG_TYPE_MASK
;
1676 return crypto_has_alg(alg_name
, type
, mask
);
1680 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1681 * @tfm: cipher handle
1683 * Return: The character string holding the name of the cipher
1685 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher
*tfm
)
1687 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm
));
1690 static inline struct blkcipher_tfm
*crypto_blkcipher_crt(
1691 struct crypto_blkcipher
*tfm
)
1693 return &crypto_blkcipher_tfm(tfm
)->crt_blkcipher
;
1696 static inline struct blkcipher_alg
*crypto_blkcipher_alg(
1697 struct crypto_blkcipher
*tfm
)
1699 return &crypto_blkcipher_tfm(tfm
)->__crt_alg
->cra_blkcipher
;
1703 * crypto_blkcipher_ivsize() - obtain IV size
1704 * @tfm: cipher handle
1706 * The size of the IV for the block cipher referenced by the cipher handle is
1707 * returned. This IV size may be zero if the cipher does not need an IV.
1709 * Return: IV size in bytes
1711 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher
*tfm
)
1713 return crypto_blkcipher_alg(tfm
)->ivsize
;
1717 * crypto_blkcipher_blocksize() - obtain block size of cipher
1718 * @tfm: cipher handle
1720 * The block size for the block cipher referenced with the cipher handle is
1721 * returned. The caller may use that information to allocate appropriate
1722 * memory for the data returned by the encryption or decryption operation.
1724 * Return: block size of cipher
1726 static inline unsigned int crypto_blkcipher_blocksize(
1727 struct crypto_blkcipher
*tfm
)
1729 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm
));
1732 static inline unsigned int crypto_blkcipher_alignmask(
1733 struct crypto_blkcipher
*tfm
)
1735 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm
));
1738 static inline u32
crypto_blkcipher_get_flags(struct crypto_blkcipher
*tfm
)
1740 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm
));
1743 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher
*tfm
,
1746 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm
), flags
);
1749 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher
*tfm
,
1752 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm
), flags
);
1756 * crypto_blkcipher_setkey() - set key for cipher
1757 * @tfm: cipher handle
1758 * @key: buffer holding the key
1759 * @keylen: length of the key in bytes
1761 * The caller provided key is set for the block cipher referenced by the cipher
1764 * Note, the key length determines the cipher type. Many block ciphers implement
1765 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1766 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1769 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1771 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher
*tfm
,
1772 const u8
*key
, unsigned int keylen
)
1774 return crypto_blkcipher_crt(tfm
)->setkey(crypto_blkcipher_tfm(tfm
),
1779 * crypto_blkcipher_encrypt() - encrypt plaintext
1780 * @desc: reference to the block cipher handle with meta data
1781 * @dst: scatter/gather list that is filled by the cipher operation with the
1783 * @src: scatter/gather list that holds the plaintext
1784 * @nbytes: number of bytes of the plaintext to encrypt.
1786 * Encrypt plaintext data using the IV set by the caller with a preceding
1787 * call of crypto_blkcipher_set_iv.
1789 * The blkcipher_desc data structure must be filled by the caller and can
1790 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1791 * with the block cipher handle; desc.flags is filled with either
1792 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1794 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1796 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc
*desc
,
1797 struct scatterlist
*dst
,
1798 struct scatterlist
*src
,
1799 unsigned int nbytes
)
1801 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
1802 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
1806 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1807 * @desc: reference to the block cipher handle with meta data
1808 * @dst: scatter/gather list that is filled by the cipher operation with the
1810 * @src: scatter/gather list that holds the plaintext
1811 * @nbytes: number of bytes of the plaintext to encrypt.
1813 * Encrypt plaintext data with the use of an IV that is solely used for this
1814 * cipher operation. Any previously set IV is not used.
1816 * The blkcipher_desc data structure must be filled by the caller and can
1817 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1818 * with the block cipher handle; desc.info is filled with the IV to be used for
1819 * the current operation; desc.flags is filled with either
1820 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1822 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1824 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc
*desc
,
1825 struct scatterlist
*dst
,
1826 struct scatterlist
*src
,
1827 unsigned int nbytes
)
1829 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
1833 * crypto_blkcipher_decrypt() - decrypt ciphertext
1834 * @desc: reference to the block cipher handle with meta data
1835 * @dst: scatter/gather list that is filled by the cipher operation with the
1837 * @src: scatter/gather list that holds the ciphertext
1838 * @nbytes: number of bytes of the ciphertext to decrypt.
1840 * Decrypt ciphertext data using the IV set by the caller with a preceding
1841 * call of crypto_blkcipher_set_iv.
1843 * The blkcipher_desc data structure must be filled by the caller as documented
1844 * for the crypto_blkcipher_encrypt call above.
1846 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1849 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc
*desc
,
1850 struct scatterlist
*dst
,
1851 struct scatterlist
*src
,
1852 unsigned int nbytes
)
1854 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
1855 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
1859 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1860 * @desc: reference to the block cipher handle with meta data
1861 * @dst: scatter/gather list that is filled by the cipher operation with the
1863 * @src: scatter/gather list that holds the ciphertext
1864 * @nbytes: number of bytes of the ciphertext to decrypt.
1866 * Decrypt ciphertext data with the use of an IV that is solely used for this
1867 * cipher operation. Any previously set IV is not used.
1869 * The blkcipher_desc data structure must be filled by the caller as documented
1870 * for the crypto_blkcipher_encrypt_iv call above.
1872 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1874 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc
*desc
,
1875 struct scatterlist
*dst
,
1876 struct scatterlist
*src
,
1877 unsigned int nbytes
)
1879 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
1883 * crypto_blkcipher_set_iv() - set IV for cipher
1884 * @tfm: cipher handle
1885 * @src: buffer holding the IV
1886 * @len: length of the IV in bytes
1888 * The caller provided IV is set for the block cipher referenced by the cipher
1891 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher
*tfm
,
1892 const u8
*src
, unsigned int len
)
1894 memcpy(crypto_blkcipher_crt(tfm
)->iv
, src
, len
);
1898 * crypto_blkcipher_get_iv() - obtain IV from cipher
1899 * @tfm: cipher handle
1900 * @dst: buffer filled with the IV
1901 * @len: length of the buffer dst
1903 * The caller can obtain the IV set for the block cipher referenced by the
1904 * cipher handle and store it into the user-provided buffer. If the buffer
1905 * has an insufficient space, the IV is truncated to fit the buffer.
1907 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher
*tfm
,
1908 u8
*dst
, unsigned int len
)
1910 memcpy(dst
, crypto_blkcipher_crt(tfm
)->iv
, len
);
1914 * DOC: Single Block Cipher API
1916 * The single block cipher API is used with the ciphers of type
1917 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1919 * Using the single block cipher API calls, operations with the basic cipher
1920 * primitive can be implemented. These cipher primitives exclude any block
1921 * chaining operations including IV handling.
1923 * The purpose of this single block cipher API is to support the implementation
1924 * of templates or other concepts that only need to perform the cipher operation
1925 * on one block at a time. Templates invoke the underlying cipher primitive
1926 * block-wise and process either the input or the output data of these cipher
1930 static inline struct crypto_cipher
*__crypto_cipher_cast(struct crypto_tfm
*tfm
)
1932 return (struct crypto_cipher
*)tfm
;
1935 static inline struct crypto_cipher
*crypto_cipher_cast(struct crypto_tfm
*tfm
)
1937 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
1938 return __crypto_cipher_cast(tfm
);
1942 * crypto_alloc_cipher() - allocate single block cipher handle
1943 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1944 * single block cipher
1945 * @type: specifies the type of the cipher
1946 * @mask: specifies the mask for the cipher
1948 * Allocate a cipher handle for a single block cipher. The returned struct
1949 * crypto_cipher is the cipher handle that is required for any subsequent API
1950 * invocation for that single block cipher.
1952 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1953 * of an error, PTR_ERR() returns the error code.
1955 static inline struct crypto_cipher
*crypto_alloc_cipher(const char *alg_name
,
1958 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1959 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1960 mask
|= CRYPTO_ALG_TYPE_MASK
;
1962 return __crypto_cipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
1965 static inline struct crypto_tfm
*crypto_cipher_tfm(struct crypto_cipher
*tfm
)
1971 * crypto_free_cipher() - zeroize and free the single block cipher handle
1972 * @tfm: cipher handle to be freed
1974 static inline void crypto_free_cipher(struct crypto_cipher
*tfm
)
1976 crypto_free_tfm(crypto_cipher_tfm(tfm
));
1980 * crypto_has_cipher() - Search for the availability of a single block cipher
1981 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1982 * single block cipher
1983 * @type: specifies the type of the cipher
1984 * @mask: specifies the mask for the cipher
1986 * Return: true when the single block cipher is known to the kernel crypto API;
1989 static inline int crypto_has_cipher(const char *alg_name
, u32 type
, u32 mask
)
1991 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1992 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1993 mask
|= CRYPTO_ALG_TYPE_MASK
;
1995 return crypto_has_alg(alg_name
, type
, mask
);
1998 static inline struct cipher_tfm
*crypto_cipher_crt(struct crypto_cipher
*tfm
)
2000 return &crypto_cipher_tfm(tfm
)->crt_cipher
;
2004 * crypto_cipher_blocksize() - obtain block size for cipher
2005 * @tfm: cipher handle
2007 * The block size for the single block cipher referenced with the cipher handle
2008 * tfm is returned. The caller may use that information to allocate appropriate
2009 * memory for the data returned by the encryption or decryption operation
2011 * Return: block size of cipher
2013 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher
*tfm
)
2015 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm
));
2018 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher
*tfm
)
2020 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm
));
2023 static inline u32
crypto_cipher_get_flags(struct crypto_cipher
*tfm
)
2025 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm
));
2028 static inline void crypto_cipher_set_flags(struct crypto_cipher
*tfm
,
2031 crypto_tfm_set_flags(crypto_cipher_tfm(tfm
), flags
);
2034 static inline void crypto_cipher_clear_flags(struct crypto_cipher
*tfm
,
2037 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm
), flags
);
2041 * crypto_cipher_setkey() - set key for cipher
2042 * @tfm: cipher handle
2043 * @key: buffer holding the key
2044 * @keylen: length of the key in bytes
2046 * The caller provided key is set for the single block cipher referenced by the
2049 * Note, the key length determines the cipher type. Many block ciphers implement
2050 * different cipher modes depending on the key size, such as AES-128 vs AES-192
2051 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
2054 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
2056 static inline int crypto_cipher_setkey(struct crypto_cipher
*tfm
,
2057 const u8
*key
, unsigned int keylen
)
2059 return crypto_cipher_crt(tfm
)->cit_setkey(crypto_cipher_tfm(tfm
),
2064 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
2065 * @tfm: cipher handle
2066 * @dst: points to the buffer that will be filled with the ciphertext
2067 * @src: buffer holding the plaintext to be encrypted
2069 * Invoke the encryption operation of one block. The caller must ensure that
2070 * the plaintext and ciphertext buffers are at least one block in size.
2072 static inline void crypto_cipher_encrypt_one(struct crypto_cipher
*tfm
,
2073 u8
*dst
, const u8
*src
)
2075 crypto_cipher_crt(tfm
)->cit_encrypt_one(crypto_cipher_tfm(tfm
),
2080 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
2081 * @tfm: cipher handle
2082 * @dst: points to the buffer that will be filled with the plaintext
2083 * @src: buffer holding the ciphertext to be decrypted
2085 * Invoke the decryption operation of one block. The caller must ensure that
2086 * the plaintext and ciphertext buffers are at least one block in size.
2088 static inline void crypto_cipher_decrypt_one(struct crypto_cipher
*tfm
,
2089 u8
*dst
, const u8
*src
)
2091 crypto_cipher_crt(tfm
)->cit_decrypt_one(crypto_cipher_tfm(tfm
),
2096 * DOC: Synchronous Message Digest API
2098 * The synchronous message digest API is used with the ciphers of type
2099 * CRYPTO_ALG_TYPE_HASH (listed as type "hash" in /proc/crypto)
2102 static inline struct crypto_hash
*__crypto_hash_cast(struct crypto_tfm
*tfm
)
2104 return (struct crypto_hash
*)tfm
;
2107 static inline struct crypto_hash
*crypto_hash_cast(struct crypto_tfm
*tfm
)
2109 BUG_ON((crypto_tfm_alg_type(tfm
) ^ CRYPTO_ALG_TYPE_HASH
) &
2110 CRYPTO_ALG_TYPE_HASH_MASK
);
2111 return __crypto_hash_cast(tfm
);
2115 * crypto_alloc_hash() - allocate synchronous message digest handle
2116 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
2117 * message digest cipher
2118 * @type: specifies the type of the cipher
2119 * @mask: specifies the mask for the cipher
2121 * Allocate a cipher handle for a message digest. The returned struct
2122 * crypto_hash is the cipher handle that is required for any subsequent
2123 * API invocation for that message digest.
2125 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
2126 * of an error, PTR_ERR() returns the error code.
2128 static inline struct crypto_hash
*crypto_alloc_hash(const char *alg_name
,
2131 type
&= ~CRYPTO_ALG_TYPE_MASK
;
2132 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
2133 type
|= CRYPTO_ALG_TYPE_HASH
;
2134 mask
|= CRYPTO_ALG_TYPE_HASH_MASK
;
2136 return __crypto_hash_cast(crypto_alloc_base(alg_name
, type
, mask
));
2139 static inline struct crypto_tfm
*crypto_hash_tfm(struct crypto_hash
*tfm
)
2145 * crypto_free_hash() - zeroize and free message digest handle
2146 * @tfm: cipher handle to be freed
2148 static inline void crypto_free_hash(struct crypto_hash
*tfm
)
2150 crypto_free_tfm(crypto_hash_tfm(tfm
));
2154 * crypto_has_hash() - Search for the availability of a message digest
2155 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
2156 * message digest cipher
2157 * @type: specifies the type of the cipher
2158 * @mask: specifies the mask for the cipher
2160 * Return: true when the message digest cipher is known to the kernel crypto
2161 * API; false otherwise
2163 static inline int crypto_has_hash(const char *alg_name
, u32 type
, u32 mask
)
2165 type
&= ~CRYPTO_ALG_TYPE_MASK
;
2166 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
2167 type
|= CRYPTO_ALG_TYPE_HASH
;
2168 mask
|= CRYPTO_ALG_TYPE_HASH_MASK
;
2170 return crypto_has_alg(alg_name
, type
, mask
);
2173 static inline struct hash_tfm
*crypto_hash_crt(struct crypto_hash
*tfm
)
2175 return &crypto_hash_tfm(tfm
)->crt_hash
;
2179 * crypto_hash_blocksize() - obtain block size for message digest
2180 * @tfm: cipher handle
2182 * The block size for the message digest cipher referenced with the cipher
2183 * handle is returned.
2185 * Return: block size of cipher
2187 static inline unsigned int crypto_hash_blocksize(struct crypto_hash
*tfm
)
2189 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm
));
2192 static inline unsigned int crypto_hash_alignmask(struct crypto_hash
*tfm
)
2194 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm
));
2198 * crypto_hash_digestsize() - obtain message digest size
2199 * @tfm: cipher handle
2201 * The size for the message digest created by the message digest cipher
2202 * referenced with the cipher handle is returned.
2204 * Return: message digest size
2206 static inline unsigned int crypto_hash_digestsize(struct crypto_hash
*tfm
)
2208 return crypto_hash_crt(tfm
)->digestsize
;
2211 static inline u32
crypto_hash_get_flags(struct crypto_hash
*tfm
)
2213 return crypto_tfm_get_flags(crypto_hash_tfm(tfm
));
2216 static inline void crypto_hash_set_flags(struct crypto_hash
*tfm
, u32 flags
)
2218 crypto_tfm_set_flags(crypto_hash_tfm(tfm
), flags
);
2221 static inline void crypto_hash_clear_flags(struct crypto_hash
*tfm
, u32 flags
)
2223 crypto_tfm_clear_flags(crypto_hash_tfm(tfm
), flags
);
2227 * crypto_hash_init() - (re)initialize message digest handle
2228 * @desc: cipher request handle that to be filled by caller --
2229 * desc.tfm is filled with the hash cipher handle;
2230 * desc.flags is filled with either CRYPTO_TFM_REQ_MAY_SLEEP or 0.
2232 * The call (re-)initializes the message digest referenced by the hash cipher
2233 * request handle. Any potentially existing state created by previous
2234 * operations is discarded.
2236 * Return: 0 if the message digest initialization was successful; < 0 if an
2239 static inline int crypto_hash_init(struct hash_desc
*desc
)
2241 return crypto_hash_crt(desc
->tfm
)->init(desc
);
2245 * crypto_hash_update() - add data to message digest for processing
2246 * @desc: cipher request handle
2247 * @sg: scatter / gather list pointing to the data to be added to the message
2249 * @nbytes: number of bytes to be processed from @sg
2251 * Updates the message digest state of the cipher handle pointed to by the
2252 * hash cipher request handle with the input data pointed to by the
2253 * scatter/gather list.
2255 * Return: 0 if the message digest update was successful; < 0 if an error
2258 static inline int crypto_hash_update(struct hash_desc
*desc
,
2259 struct scatterlist
*sg
,
2260 unsigned int nbytes
)
2262 return crypto_hash_crt(desc
->tfm
)->update(desc
, sg
, nbytes
);
2266 * crypto_hash_final() - calculate message digest
2267 * @desc: cipher request handle
2268 * @out: message digest output buffer -- The caller must ensure that the out
2269 * buffer has a sufficient size (e.g. by using the crypto_hash_digestsize
2272 * Finalize the message digest operation and create the message digest
2273 * based on all data added to the cipher handle. The message digest is placed
2274 * into the output buffer.
2276 * Return: 0 if the message digest creation was successful; < 0 if an error
2279 static inline int crypto_hash_final(struct hash_desc
*desc
, u8
*out
)
2281 return crypto_hash_crt(desc
->tfm
)->final(desc
, out
);
2285 * crypto_hash_digest() - calculate message digest for a buffer
2286 * @desc: see crypto_hash_final()
2287 * @sg: see crypto_hash_update()
2288 * @nbytes: see crypto_hash_update()
2289 * @out: see crypto_hash_final()
2291 * This function is a "short-hand" for the function calls of crypto_hash_init,
2292 * crypto_hash_update and crypto_hash_final. The parameters have the same
2293 * meaning as discussed for those separate three functions.
2295 * Return: 0 if the message digest creation was successful; < 0 if an error
2298 static inline int crypto_hash_digest(struct hash_desc
*desc
,
2299 struct scatterlist
*sg
,
2300 unsigned int nbytes
, u8
*out
)
2302 return crypto_hash_crt(desc
->tfm
)->digest(desc
, sg
, nbytes
, out
);
2306 * crypto_hash_setkey() - set key for message digest
2307 * @hash: cipher handle
2308 * @key: buffer holding the key
2309 * @keylen: length of the key in bytes
2311 * The caller provided key is set for the message digest cipher. The cipher
2312 * handle must point to a keyed hash in order for this function to succeed.
2314 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
2316 static inline int crypto_hash_setkey(struct crypto_hash
*hash
,
2317 const u8
*key
, unsigned int keylen
)
2319 return crypto_hash_crt(hash
)->setkey(hash
, key
, keylen
);
2322 static inline struct crypto_comp
*__crypto_comp_cast(struct crypto_tfm
*tfm
)
2324 return (struct crypto_comp
*)tfm
;
2327 static inline struct crypto_comp
*crypto_comp_cast(struct crypto_tfm
*tfm
)
2329 BUG_ON((crypto_tfm_alg_type(tfm
) ^ CRYPTO_ALG_TYPE_COMPRESS
) &
2330 CRYPTO_ALG_TYPE_MASK
);
2331 return __crypto_comp_cast(tfm
);
2334 static inline struct crypto_comp
*crypto_alloc_comp(const char *alg_name
,
2337 type
&= ~CRYPTO_ALG_TYPE_MASK
;
2338 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
2339 mask
|= CRYPTO_ALG_TYPE_MASK
;
2341 return __crypto_comp_cast(crypto_alloc_base(alg_name
, type
, mask
));
2344 static inline struct crypto_tfm
*crypto_comp_tfm(struct crypto_comp
*tfm
)
2349 static inline void crypto_free_comp(struct crypto_comp
*tfm
)
2351 crypto_free_tfm(crypto_comp_tfm(tfm
));
2354 static inline int crypto_has_comp(const char *alg_name
, u32 type
, u32 mask
)
2356 type
&= ~CRYPTO_ALG_TYPE_MASK
;
2357 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
2358 mask
|= CRYPTO_ALG_TYPE_MASK
;
2360 return crypto_has_alg(alg_name
, type
, mask
);
2363 static inline const char *crypto_comp_name(struct crypto_comp
*tfm
)
2365 return crypto_tfm_alg_name(crypto_comp_tfm(tfm
));
2368 static inline struct compress_tfm
*crypto_comp_crt(struct crypto_comp
*tfm
)
2370 return &crypto_comp_tfm(tfm
)->crt_compress
;
2373 static inline int crypto_comp_compress(struct crypto_comp
*tfm
,
2374 const u8
*src
, unsigned int slen
,
2375 u8
*dst
, unsigned int *dlen
)
2377 return crypto_comp_crt(tfm
)->cot_compress(crypto_comp_tfm(tfm
),
2378 src
, slen
, dst
, dlen
);
2381 static inline int crypto_comp_decompress(struct crypto_comp
*tfm
,
2382 const u8
*src
, unsigned int slen
,
2383 u8
*dst
, unsigned int *dlen
)
2385 return crypto_comp_crt(tfm
)->cot_decompress(crypto_comp_tfm(tfm
),
2386 src
, slen
, dst
, dlen
);
2389 #endif /* _LINUX_CRYPTO_H */