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_SKCIPHER 0x00000005
51 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
52 #define CRYPTO_ALG_TYPE_KPP 0x00000008
53 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a
54 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b
55 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
56 #define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
57 #define CRYPTO_ALG_TYPE_DIGEST 0x0000000e
58 #define CRYPTO_ALG_TYPE_HASH 0x0000000e
59 #define CRYPTO_ALG_TYPE_SHASH 0x0000000e
60 #define CRYPTO_ALG_TYPE_AHASH 0x0000000f
62 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
63 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
64 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
65 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e
67 #define CRYPTO_ALG_LARVAL 0x00000010
68 #define CRYPTO_ALG_DEAD 0x00000020
69 #define CRYPTO_ALG_DYING 0x00000040
70 #define CRYPTO_ALG_ASYNC 0x00000080
73 * Set this bit if and only if the algorithm requires another algorithm of
74 * the same type to handle corner cases.
76 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
79 * This bit is set for symmetric key ciphers that have already been wrapped
80 * with a generic IV generator to prevent them from being wrapped again.
82 #define CRYPTO_ALG_GENIV 0x00000200
85 * Set if the algorithm has passed automated run-time testing. Note that
86 * if there is no run-time testing for a given algorithm it is considered
90 #define CRYPTO_ALG_TESTED 0x00000400
93 * Set if the algorithm is an instance that is built from templates.
95 #define CRYPTO_ALG_INSTANCE 0x00000800
97 /* Set this bit if the algorithm provided is hardware accelerated but
98 * not available to userspace via instruction set or so.
100 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
103 * Mark a cipher as a service implementation only usable by another
104 * cipher and never by a normal user of the kernel crypto API
106 #define CRYPTO_ALG_INTERNAL 0x00002000
109 * Transform masks and values (for crt_flags).
111 #define CRYPTO_TFM_REQ_MASK 0x000fff00
112 #define CRYPTO_TFM_RES_MASK 0xfff00000
114 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
115 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
116 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
117 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
118 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
119 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
120 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
121 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
124 * Miscellaneous stuff.
126 #define CRYPTO_MAX_ALG_NAME 64
129 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
130 * declaration) is used to ensure that the crypto_tfm context structure is
131 * aligned correctly for the given architecture so that there are no alignment
132 * faults for C data types. In particular, this is required on platforms such
133 * as arm where pointers are 32-bit aligned but there are data types such as
134 * u64 which require 64-bit alignment.
136 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
138 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
141 struct crypto_ablkcipher;
142 struct crypto_async_request;
143 struct crypto_blkcipher;
146 struct skcipher_givcrypt_request;
148 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
151 * DOC: Block Cipher Context Data Structures
153 * These data structures define the operating context for each block cipher
157 struct crypto_async_request {
158 struct list_head list;
159 crypto_completion_t complete;
161 struct crypto_tfm *tfm;
166 struct ablkcipher_request {
167 struct crypto_async_request base;
173 struct scatterlist *src;
174 struct scatterlist *dst;
176 void *__ctx[] CRYPTO_MINALIGN_ATTR;
179 struct blkcipher_desc {
180 struct crypto_blkcipher *tfm;
186 struct crypto_tfm *tfm;
187 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
188 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
189 const u8 *src, unsigned int nbytes);
194 * DOC: Block Cipher Algorithm Definitions
196 * These data structures define modular crypto algorithm implementations,
197 * managed via crypto_register_alg() and crypto_unregister_alg().
201 * struct ablkcipher_alg - asynchronous block cipher definition
202 * @min_keysize: Minimum key size supported by the transformation. This is the
203 * smallest key length supported by this transformation algorithm.
204 * This must be set to one of the pre-defined values as this is
205 * not hardware specific. Possible values for this field can be
206 * found via git grep "_MIN_KEY_SIZE" include/crypto/
207 * @max_keysize: Maximum key size supported by the transformation. This is the
208 * largest key length supported by this transformation algorithm.
209 * This must be set to one of the pre-defined values as this is
210 * not hardware specific. Possible values for this field can be
211 * found via git grep "_MAX_KEY_SIZE" include/crypto/
212 * @setkey: Set key for the transformation. This function is used to either
213 * program a supplied key into the hardware or store the key in the
214 * transformation context for programming it later. Note that this
215 * function does modify the transformation context. This function can
216 * be called multiple times during the existence of the transformation
217 * object, so one must make sure the key is properly reprogrammed into
218 * the hardware. This function is also responsible for checking the key
219 * length for validity. In case a software fallback was put in place in
220 * the @cra_init call, this function might need to use the fallback if
221 * the algorithm doesn't support all of the key sizes.
222 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
223 * the supplied scatterlist containing the blocks of data. The crypto
224 * API consumer is responsible for aligning the entries of the
225 * scatterlist properly and making sure the chunks are correctly
226 * sized. In case a software fallback was put in place in the
227 * @cra_init call, this function might need to use the fallback if
228 * the algorithm doesn't support all of the key sizes. In case the
229 * key was stored in transformation context, the key might need to be
230 * re-programmed into the hardware in this function. This function
231 * shall not modify the transformation context, as this function may
232 * be called in parallel with the same transformation object.
233 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
234 * and the conditions are exactly the same.
235 * @givencrypt: Update the IV for encryption. With this function, a cipher
236 * implementation may provide the function on how to update the IV
238 * @givdecrypt: Update the IV for decryption. This is the reverse of
240 * @geniv: The transformation implementation may use an "IV generator" provided
241 * by the kernel crypto API. Several use cases have a predefined
242 * approach how IVs are to be updated. For such use cases, the kernel
243 * crypto API provides ready-to-use implementations that can be
244 * referenced with this variable.
245 * @ivsize: IV size applicable for transformation. The consumer must provide an
246 * IV of exactly that size to perform the encrypt or decrypt operation.
248 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
249 * mandatory and must be filled.
251 struct ablkcipher_alg {
252 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
253 unsigned int keylen);
254 int (*encrypt)(struct ablkcipher_request *req);
255 int (*decrypt)(struct ablkcipher_request *req);
256 int (*givencrypt)(struct skcipher_givcrypt_request *req);
257 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
261 unsigned int min_keysize;
262 unsigned int max_keysize;
267 * struct blkcipher_alg - synchronous block cipher definition
268 * @min_keysize: see struct ablkcipher_alg
269 * @max_keysize: see struct ablkcipher_alg
270 * @setkey: see struct ablkcipher_alg
271 * @encrypt: see struct ablkcipher_alg
272 * @decrypt: see struct ablkcipher_alg
273 * @geniv: see struct ablkcipher_alg
274 * @ivsize: see struct ablkcipher_alg
276 * All fields except @geniv and @ivsize are mandatory and must be filled.
278 struct blkcipher_alg {
279 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
280 unsigned int keylen);
281 int (*encrypt)(struct blkcipher_desc *desc,
282 struct scatterlist *dst, struct scatterlist *src,
283 unsigned int nbytes);
284 int (*decrypt)(struct blkcipher_desc *desc,
285 struct scatterlist *dst, struct scatterlist *src,
286 unsigned int nbytes);
290 unsigned int min_keysize;
291 unsigned int max_keysize;
296 * struct cipher_alg - single-block symmetric ciphers definition
297 * @cia_min_keysize: Minimum key size supported by the transformation. This is
298 * the smallest key length supported by this transformation
299 * algorithm. This must be set to one of the pre-defined
300 * values as this is not hardware specific. Possible values
301 * for this field can be found via git grep "_MIN_KEY_SIZE"
303 * @cia_max_keysize: Maximum key size supported by the transformation. This is
304 * the largest key length supported by this transformation
305 * algorithm. This must be set to one of the pre-defined values
306 * as this is not hardware specific. Possible values for this
307 * field can be found via git grep "_MAX_KEY_SIZE"
309 * @cia_setkey: Set key for the transformation. This function is used to either
310 * program a supplied key into the hardware or store the key in the
311 * transformation context for programming it later. Note that this
312 * function does modify the transformation context. This function
313 * can be called multiple times during the existence of the
314 * transformation object, so one must make sure the key is properly
315 * reprogrammed into the hardware. This function is also
316 * responsible for checking the key length for validity.
317 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
318 * single block of data, which must be @cra_blocksize big. This
319 * always operates on a full @cra_blocksize and it is not possible
320 * to encrypt a block of smaller size. The supplied buffers must
321 * therefore also be at least of @cra_blocksize size. Both the
322 * input and output buffers are always aligned to @cra_alignmask.
323 * In case either of the input or output buffer supplied by user
324 * of the crypto API is not aligned to @cra_alignmask, the crypto
325 * API will re-align the buffers. The re-alignment means that a
326 * new buffer will be allocated, the data will be copied into the
327 * new buffer, then the processing will happen on the new buffer,
328 * then the data will be copied back into the original buffer and
329 * finally the new buffer will be freed. In case a software
330 * fallback was put in place in the @cra_init call, this function
331 * might need to use the fallback if the algorithm doesn't support
332 * all of the key sizes. In case the key was stored in
333 * transformation context, the key might need to be re-programmed
334 * into the hardware in this function. This function shall not
335 * modify the transformation context, as this function may be
336 * called in parallel with the same transformation object.
337 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
338 * @cia_encrypt, and the conditions are exactly the same.
340 * All fields are mandatory and must be filled.
343 unsigned int cia_min_keysize;
344 unsigned int cia_max_keysize;
345 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
346 unsigned int keylen);
347 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
348 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
351 struct compress_alg {
352 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
353 unsigned int slen, u8 *dst, unsigned int *dlen);
354 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
355 unsigned int slen, u8 *dst, unsigned int *dlen);
359 #define cra_ablkcipher cra_u.ablkcipher
360 #define cra_blkcipher cra_u.blkcipher
361 #define cra_cipher cra_u.cipher
362 #define cra_compress cra_u.compress
365 * struct crypto_alg - definition of a cryptograpic cipher algorithm
366 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
367 * CRYPTO_ALG_* flags for the flags which go in here. Those are
368 * used for fine-tuning the description of the transformation
370 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
371 * of the smallest possible unit which can be transformed with
372 * this algorithm. The users must respect this value.
373 * In case of HASH transformation, it is possible for a smaller
374 * block than @cra_blocksize to be passed to the crypto API for
375 * transformation, in case of any other transformation type, an
376 * error will be returned upon any attempt to transform smaller
377 * than @cra_blocksize chunks.
378 * @cra_ctxsize: Size of the operational context of the transformation. This
379 * value informs the kernel crypto API about the memory size
380 * needed to be allocated for the transformation context.
381 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
382 * buffer containing the input data for the algorithm must be
383 * aligned to this alignment mask. The data buffer for the
384 * output data must be aligned to this alignment mask. Note that
385 * the Crypto API will do the re-alignment in software, but
386 * only under special conditions and there is a performance hit.
387 * The re-alignment happens at these occasions for different
388 * @cra_u types: cipher -- For both input data and output data
389 * buffer; ahash -- For output hash destination buf; shash --
390 * For output hash destination buf.
391 * This is needed on hardware which is flawed by design and
392 * cannot pick data from arbitrary addresses.
393 * @cra_priority: Priority of this transformation implementation. In case
394 * multiple transformations with same @cra_name are available to
395 * the Crypto API, the kernel will use the one with highest
397 * @cra_name: Generic name (usable by multiple implementations) of the
398 * transformation algorithm. This is the name of the transformation
399 * itself. This field is used by the kernel when looking up the
400 * providers of particular transformation.
401 * @cra_driver_name: Unique name of the transformation provider. This is the
402 * name of the provider of the transformation. This can be any
403 * arbitrary value, but in the usual case, this contains the
404 * name of the chip or provider and the name of the
405 * transformation algorithm.
406 * @cra_type: Type of the cryptographic transformation. This is a pointer to
407 * struct crypto_type, which implements callbacks common for all
408 * transformation types. There are multiple options:
409 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
410 * &crypto_ahash_type, &crypto_rng_type.
411 * This field might be empty. In that case, there are no common
412 * callbacks. This is the case for: cipher, compress, shash.
413 * @cra_u: Callbacks implementing the transformation. This is a union of
414 * multiple structures. Depending on the type of transformation selected
415 * by @cra_type and @cra_flags above, the associated structure must be
416 * filled with callbacks. This field might be empty. This is the case
418 * @cra_init: Initialize the cryptographic transformation object. This function
419 * is used to initialize the cryptographic transformation object.
420 * This function is called only once at the instantiation time, right
421 * after the transformation context was allocated. In case the
422 * cryptographic hardware has some special requirements which need to
423 * be handled by software, this function shall check for the precise
424 * requirement of the transformation and put any software fallbacks
426 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
427 * counterpart to @cra_init, used to remove various changes set in
429 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
430 * @cra_list: internally used
431 * @cra_users: internally used
432 * @cra_refcnt: internally used
433 * @cra_destroy: internally used
435 * The struct crypto_alg describes a generic Crypto API algorithm and is common
436 * for all of the transformations. Any variable not documented here shall not
437 * be used by a cipher implementation as it is internal to the Crypto API.
440 struct list_head cra_list;
441 struct list_head cra_users;
444 unsigned int cra_blocksize;
445 unsigned int cra_ctxsize;
446 unsigned int cra_alignmask;
451 char cra_name[CRYPTO_MAX_ALG_NAME];
452 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
454 const struct crypto_type *cra_type;
457 struct ablkcipher_alg ablkcipher;
458 struct blkcipher_alg blkcipher;
459 struct cipher_alg cipher;
460 struct compress_alg compress;
463 int (*cra_init)(struct crypto_tfm *tfm);
464 void (*cra_exit)(struct crypto_tfm *tfm);
465 void (*cra_destroy)(struct crypto_alg *alg);
467 struct module *cra_module;
468 } CRYPTO_MINALIGN_ATTR;
471 * Algorithm registration interface.
473 int crypto_register_alg(struct crypto_alg *alg);
474 int crypto_unregister_alg(struct crypto_alg *alg);
475 int crypto_register_algs(struct crypto_alg *algs, int count);
476 int crypto_unregister_algs(struct crypto_alg *algs, int count);
479 * Algorithm query interface.
481 int crypto_has_alg(const char *name, u32 type, u32 mask);
484 * Transforms: user-instantiated objects which encapsulate algorithms
485 * and core processing logic. Managed via crypto_alloc_*() and
486 * crypto_free_*(), as well as the various helpers below.
489 struct ablkcipher_tfm {
490 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
491 unsigned int keylen);
492 int (*encrypt)(struct ablkcipher_request *req);
493 int (*decrypt)(struct ablkcipher_request *req);
495 struct crypto_ablkcipher *base;
498 unsigned int reqsize;
501 struct blkcipher_tfm {
503 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
504 unsigned int keylen);
505 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
506 struct scatterlist *src, unsigned int nbytes);
507 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
508 struct scatterlist *src, unsigned int nbytes);
512 int (*cit_setkey)(struct crypto_tfm *tfm,
513 const u8 *key, unsigned int keylen);
514 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
515 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
518 struct compress_tfm {
519 int (*cot_compress)(struct crypto_tfm *tfm,
520 const u8 *src, unsigned int slen,
521 u8 *dst, unsigned int *dlen);
522 int (*cot_decompress)(struct crypto_tfm *tfm,
523 const u8 *src, unsigned int slen,
524 u8 *dst, unsigned int *dlen);
527 #define crt_ablkcipher crt_u.ablkcipher
528 #define crt_blkcipher crt_u.blkcipher
529 #define crt_cipher crt_u.cipher
530 #define crt_compress crt_u.compress
537 struct ablkcipher_tfm ablkcipher;
538 struct blkcipher_tfm blkcipher;
539 struct cipher_tfm cipher;
540 struct compress_tfm compress;
543 void (*exit)(struct crypto_tfm *tfm);
545 struct crypto_alg *__crt_alg;
547 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
550 struct crypto_ablkcipher {
551 struct crypto_tfm base;
554 struct crypto_blkcipher {
555 struct crypto_tfm base;
558 struct crypto_cipher {
559 struct crypto_tfm base;
563 struct crypto_tfm base;
574 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
576 /* Maximum number of (rtattr) parameters for each template. */
577 #define CRYPTO_MAX_ATTRS 32
579 struct crypto_attr_alg {
580 char name[CRYPTO_MAX_ALG_NAME];
583 struct crypto_attr_type {
588 struct crypto_attr_u32 {
593 * Transform user interface.
596 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
597 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
599 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
601 return crypto_destroy_tfm(tfm, tfm);
604 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
607 * Transform helpers which query the underlying algorithm.
609 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
611 return tfm->__crt_alg->cra_name;
614 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
616 return tfm->__crt_alg->cra_driver_name;
619 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
621 return tfm->__crt_alg->cra_priority;
624 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
626 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
629 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
631 return tfm->__crt_alg->cra_blocksize;
634 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
636 return tfm->__crt_alg->cra_alignmask;
639 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
641 return tfm->crt_flags;
644 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
646 tfm->crt_flags |= flags;
649 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
651 tfm->crt_flags &= ~flags;
654 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
656 return tfm->__crt_ctx;
659 static inline unsigned int crypto_tfm_ctx_alignment(void)
661 struct crypto_tfm *tfm;
662 return __alignof__(tfm->__crt_ctx);
668 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
669 struct crypto_tfm *tfm)
671 return (struct crypto_ablkcipher *)tfm;
674 static inline u32 crypto_skcipher_type(u32 type)
676 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
677 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
681 static inline u32 crypto_skcipher_mask(u32 mask)
683 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
684 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
689 * DOC: Asynchronous Block Cipher API
691 * Asynchronous block cipher API is used with the ciphers of type
692 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
694 * Asynchronous cipher operations imply that the function invocation for a
695 * cipher request returns immediately before the completion of the operation.
696 * The cipher request is scheduled as a separate kernel thread and therefore
697 * load-balanced on the different CPUs via the process scheduler. To allow
698 * the kernel crypto API to inform the caller about the completion of a cipher
699 * request, the caller must provide a callback function. That function is
700 * invoked with the cipher handle when the request completes.
702 * To support the asynchronous operation, additional information than just the
703 * cipher handle must be supplied to the kernel crypto API. That additional
704 * information is given by filling in the ablkcipher_request data structure.
706 * For the asynchronous block cipher API, the state is maintained with the tfm
707 * cipher handle. A single tfm can be used across multiple calls and in
708 * parallel. For asynchronous block cipher calls, context data supplied and
709 * only used by the caller can be referenced the request data structure in
710 * addition to the IV used for the cipher request. The maintenance of such
711 * state information would be important for a crypto driver implementer to
712 * have, because when calling the callback function upon completion of the
713 * cipher operation, that callback function may need some information about
714 * which operation just finished if it invoked multiple in parallel. This
715 * state information is unused by the kernel crypto API.
718 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
719 struct crypto_ablkcipher *tfm)
725 * crypto_free_ablkcipher() - zeroize and free cipher handle
726 * @tfm: cipher handle to be freed
728 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
730 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
734 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
735 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
737 * @type: specifies the type of the cipher
738 * @mask: specifies the mask for the cipher
740 * Return: true when the ablkcipher is known to the kernel crypto API; false
743 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
746 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
747 crypto_skcipher_mask(mask));
750 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
751 struct crypto_ablkcipher *tfm)
753 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
757 * crypto_ablkcipher_ivsize() - obtain IV size
758 * @tfm: cipher handle
760 * The size of the IV for the ablkcipher referenced by the cipher handle is
761 * returned. This IV size may be zero if the cipher does not need an IV.
763 * Return: IV size in bytes
765 static inline unsigned int crypto_ablkcipher_ivsize(
766 struct crypto_ablkcipher *tfm)
768 return crypto_ablkcipher_crt(tfm)->ivsize;
772 * crypto_ablkcipher_blocksize() - obtain block size of cipher
773 * @tfm: cipher handle
775 * The block size for the ablkcipher referenced with the cipher handle is
776 * returned. The caller may use that information to allocate appropriate
777 * memory for the data returned by the encryption or decryption operation
779 * Return: block size of cipher
781 static inline unsigned int crypto_ablkcipher_blocksize(
782 struct crypto_ablkcipher *tfm)
784 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
787 static inline unsigned int crypto_ablkcipher_alignmask(
788 struct crypto_ablkcipher *tfm)
790 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
793 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
795 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
798 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
801 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
804 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
807 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
811 * crypto_ablkcipher_setkey() - set key for cipher
812 * @tfm: cipher handle
813 * @key: buffer holding the key
814 * @keylen: length of the key in bytes
816 * The caller provided key is set for the ablkcipher referenced by the cipher
819 * Note, the key length determines the cipher type. Many block ciphers implement
820 * different cipher modes depending on the key size, such as AES-128 vs AES-192
821 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
824 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
826 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
827 const u8 *key, unsigned int keylen)
829 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
831 return crt->setkey(crt->base, key, keylen);
835 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
836 * @req: ablkcipher_request out of which the cipher handle is to be obtained
838 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
841 * Return: crypto_ablkcipher handle
843 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
844 struct ablkcipher_request *req)
846 return __crypto_ablkcipher_cast(req->base.tfm);
850 * crypto_ablkcipher_encrypt() - encrypt plaintext
851 * @req: reference to the ablkcipher_request handle that holds all information
852 * needed to perform the cipher operation
854 * Encrypt plaintext data using the ablkcipher_request handle. That data
855 * structure and how it is filled with data is discussed with the
856 * ablkcipher_request_* functions.
858 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
860 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
862 struct ablkcipher_tfm *crt =
863 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
864 return crt->encrypt(req);
868 * crypto_ablkcipher_decrypt() - decrypt ciphertext
869 * @req: reference to the ablkcipher_request handle that holds all information
870 * needed to perform the cipher operation
872 * Decrypt ciphertext data using the ablkcipher_request handle. That data
873 * structure and how it is filled with data is discussed with the
874 * ablkcipher_request_* functions.
876 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
878 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
880 struct ablkcipher_tfm *crt =
881 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
882 return crt->decrypt(req);
886 * DOC: Asynchronous Cipher Request Handle
888 * The ablkcipher_request data structure contains all pointers to data
889 * required for the asynchronous cipher operation. This includes the cipher
890 * handle (which can be used by multiple ablkcipher_request instances), pointer
891 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
892 * as a handle to the ablkcipher_request_* API calls in a similar way as
893 * ablkcipher handle to the crypto_ablkcipher_* API calls.
897 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
898 * @tfm: cipher handle
900 * Return: number of bytes
902 static inline unsigned int crypto_ablkcipher_reqsize(
903 struct crypto_ablkcipher *tfm)
905 return crypto_ablkcipher_crt(tfm)->reqsize;
909 * ablkcipher_request_set_tfm() - update cipher handle reference in request
910 * @req: request handle to be modified
911 * @tfm: cipher handle that shall be added to the request handle
913 * Allow the caller to replace the existing ablkcipher handle in the request
914 * data structure with a different one.
916 static inline void ablkcipher_request_set_tfm(
917 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
919 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
922 static inline struct ablkcipher_request *ablkcipher_request_cast(
923 struct crypto_async_request *req)
925 return container_of(req, struct ablkcipher_request, base);
929 * ablkcipher_request_alloc() - allocate request data structure
930 * @tfm: cipher handle to be registered with the request
931 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
933 * Allocate the request data structure that must be used with the ablkcipher
934 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
935 * handle is registered in the request data structure.
937 * Return: allocated request handle in case of success, or NULL if out of memory
939 static inline struct ablkcipher_request *ablkcipher_request_alloc(
940 struct crypto_ablkcipher *tfm, gfp_t gfp)
942 struct ablkcipher_request *req;
944 req = kmalloc(sizeof(struct ablkcipher_request) +
945 crypto_ablkcipher_reqsize(tfm), gfp);
948 ablkcipher_request_set_tfm(req, tfm);
954 * ablkcipher_request_free() - zeroize and free request data structure
955 * @req: request data structure cipher handle to be freed
957 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
963 * ablkcipher_request_set_callback() - set asynchronous callback function
964 * @req: request handle
965 * @flags: specify zero or an ORing of the flags
966 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
967 * increase the wait queue beyond the initial maximum size;
968 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
969 * @compl: callback function pointer to be registered with the request handle
970 * @data: The data pointer refers to memory that is not used by the kernel
971 * crypto API, but provided to the callback function for it to use. Here,
972 * the caller can provide a reference to memory the callback function can
973 * operate on. As the callback function is invoked asynchronously to the
974 * related functionality, it may need to access data structures of the
975 * related functionality which can be referenced using this pointer. The
976 * callback function can access the memory via the "data" field in the
977 * crypto_async_request data structure provided to the callback function.
979 * This function allows setting the callback function that is triggered once the
980 * cipher operation completes.
982 * The callback function is registered with the ablkcipher_request handle and
983 * must comply with the following template::
985 * void callback_function(struct crypto_async_request *req, int error)
987 static inline void ablkcipher_request_set_callback(
988 struct ablkcipher_request *req,
989 u32 flags, crypto_completion_t compl, void *data)
991 req->base.complete = compl;
992 req->base.data = data;
993 req->base.flags = flags;
997 * ablkcipher_request_set_crypt() - set data buffers
998 * @req: request handle
999 * @src: source scatter / gather list
1000 * @dst: destination scatter / gather list
1001 * @nbytes: number of bytes to process from @src
1002 * @iv: IV for the cipher operation which must comply with the IV size defined
1003 * by crypto_ablkcipher_ivsize
1005 * This function allows setting of the source data and destination data
1006 * scatter / gather lists.
1008 * For encryption, the source is treated as the plaintext and the
1009 * destination is the ciphertext. For a decryption operation, the use is
1010 * reversed - the source is the ciphertext and the destination is the plaintext.
1012 static inline void ablkcipher_request_set_crypt(
1013 struct ablkcipher_request *req,
1014 struct scatterlist *src, struct scatterlist *dst,
1015 unsigned int nbytes, void *iv)
1019 req->nbytes = nbytes;
1024 * DOC: Synchronous Block Cipher API
1026 * The synchronous block cipher API is used with the ciphers of type
1027 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1029 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1030 * used in multiple calls and in parallel, this info should not be changeable
1031 * (unless a lock is used). This applies, for example, to the symmetric key.
1032 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1033 * structure for synchronous blkcipher api. So, its the only state info that can
1034 * be kept for synchronous calls without using a big lock across a tfm.
1036 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1037 * consisting of a template (a block chaining mode) and a single block cipher
1038 * primitive (e.g. AES).
1040 * The plaintext data buffer and the ciphertext data buffer are pointed to
1041 * by using scatter/gather lists. The cipher operation is performed
1042 * on all segments of the provided scatter/gather lists.
1044 * The kernel crypto API supports a cipher operation "in-place" which means that
1045 * the caller may provide the same scatter/gather list for the plaintext and
1046 * cipher text. After the completion of the cipher operation, the plaintext
1047 * data is replaced with the ciphertext data in case of an encryption and vice
1048 * versa for a decryption. The caller must ensure that the scatter/gather lists
1049 * for the output data point to sufficiently large buffers, i.e. multiples of
1050 * the block size of the cipher.
1053 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1054 struct crypto_tfm *tfm)
1056 return (struct crypto_blkcipher *)tfm;
1059 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1060 struct crypto_tfm *tfm)
1062 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1063 return __crypto_blkcipher_cast(tfm);
1067 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1068 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1070 * @type: specifies the type of the cipher
1071 * @mask: specifies the mask for the cipher
1073 * Allocate a cipher handle for a block cipher. The returned struct
1074 * crypto_blkcipher is the cipher handle that is required for any subsequent
1075 * API invocation for that block cipher.
1077 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1078 * of an error, PTR_ERR() returns the error code.
1080 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1081 const char *alg_name, u32 type, u32 mask)
1083 type &= ~CRYPTO_ALG_TYPE_MASK;
1084 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1085 mask |= CRYPTO_ALG_TYPE_MASK;
1087 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1090 static inline struct crypto_tfm *crypto_blkcipher_tfm(
1091 struct crypto_blkcipher *tfm)
1097 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1098 * @tfm: cipher handle to be freed
1100 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1102 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1106 * crypto_has_blkcipher() - Search for the availability of a block cipher
1107 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1109 * @type: specifies the type of the cipher
1110 * @mask: specifies the mask for the cipher
1112 * Return: true when the block cipher is known to the kernel crypto API; false
1115 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1117 type &= ~CRYPTO_ALG_TYPE_MASK;
1118 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1119 mask |= CRYPTO_ALG_TYPE_MASK;
1121 return crypto_has_alg(alg_name, type, mask);
1125 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1126 * @tfm: cipher handle
1128 * Return: The character string holding the name of the cipher
1130 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1132 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1135 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1136 struct crypto_blkcipher *tfm)
1138 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1141 static inline struct blkcipher_alg *crypto_blkcipher_alg(
1142 struct crypto_blkcipher *tfm)
1144 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1148 * crypto_blkcipher_ivsize() - obtain IV size
1149 * @tfm: cipher handle
1151 * The size of the IV for the block cipher referenced by the cipher handle is
1152 * returned. This IV size may be zero if the cipher does not need an IV.
1154 * Return: IV size in bytes
1156 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1158 return crypto_blkcipher_alg(tfm)->ivsize;
1162 * crypto_blkcipher_blocksize() - obtain block size of cipher
1163 * @tfm: cipher handle
1165 * The block size for the block cipher referenced with the cipher handle is
1166 * returned. The caller may use that information to allocate appropriate
1167 * memory for the data returned by the encryption or decryption operation.
1169 * Return: block size of cipher
1171 static inline unsigned int crypto_blkcipher_blocksize(
1172 struct crypto_blkcipher *tfm)
1174 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1177 static inline unsigned int crypto_blkcipher_alignmask(
1178 struct crypto_blkcipher *tfm)
1180 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1183 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1185 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1188 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1191 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1194 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1197 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1201 * crypto_blkcipher_setkey() - set key for cipher
1202 * @tfm: cipher handle
1203 * @key: buffer holding the key
1204 * @keylen: length of the key in bytes
1206 * The caller provided key is set for the block cipher referenced by the cipher
1209 * Note, the key length determines the cipher type. Many block ciphers implement
1210 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1211 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1214 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1216 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1217 const u8 *key, unsigned int keylen)
1219 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1224 * crypto_blkcipher_encrypt() - encrypt plaintext
1225 * @desc: reference to the block cipher handle with meta data
1226 * @dst: scatter/gather list that is filled by the cipher operation with the
1228 * @src: scatter/gather list that holds the plaintext
1229 * @nbytes: number of bytes of the plaintext to encrypt.
1231 * Encrypt plaintext data using the IV set by the caller with a preceding
1232 * call of crypto_blkcipher_set_iv.
1234 * The blkcipher_desc data structure must be filled by the caller and can
1235 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1236 * with the block cipher handle; desc.flags is filled with either
1237 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1239 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1241 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1242 struct scatterlist *dst,
1243 struct scatterlist *src,
1244 unsigned int nbytes)
1246 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1247 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1251 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1252 * @desc: reference to the block cipher handle with meta data
1253 * @dst: scatter/gather list that is filled by the cipher operation with the
1255 * @src: scatter/gather list that holds the plaintext
1256 * @nbytes: number of bytes of the plaintext to encrypt.
1258 * Encrypt plaintext data with the use of an IV that is solely used for this
1259 * cipher operation. Any previously set IV is not used.
1261 * The blkcipher_desc data structure must be filled by the caller and can
1262 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1263 * with the block cipher handle; desc.info is filled with the IV to be used for
1264 * the current operation; desc.flags is filled with either
1265 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1267 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1269 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1270 struct scatterlist *dst,
1271 struct scatterlist *src,
1272 unsigned int nbytes)
1274 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1278 * crypto_blkcipher_decrypt() - decrypt ciphertext
1279 * @desc: reference to the block cipher handle with meta data
1280 * @dst: scatter/gather list that is filled by the cipher operation with the
1282 * @src: scatter/gather list that holds the ciphertext
1283 * @nbytes: number of bytes of the ciphertext to decrypt.
1285 * Decrypt ciphertext data using the IV set by the caller with a preceding
1286 * call of crypto_blkcipher_set_iv.
1288 * The blkcipher_desc data structure must be filled by the caller as documented
1289 * for the crypto_blkcipher_encrypt call above.
1291 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1294 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1295 struct scatterlist *dst,
1296 struct scatterlist *src,
1297 unsigned int nbytes)
1299 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1300 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1304 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1305 * @desc: reference to the block cipher handle with meta data
1306 * @dst: scatter/gather list that is filled by the cipher operation with the
1308 * @src: scatter/gather list that holds the ciphertext
1309 * @nbytes: number of bytes of the ciphertext to decrypt.
1311 * Decrypt ciphertext data with the use of an IV that is solely used for this
1312 * cipher operation. Any previously set IV is not used.
1314 * The blkcipher_desc data structure must be filled by the caller as documented
1315 * for the crypto_blkcipher_encrypt_iv call above.
1317 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1319 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1320 struct scatterlist *dst,
1321 struct scatterlist *src,
1322 unsigned int nbytes)
1324 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1328 * crypto_blkcipher_set_iv() - set IV for cipher
1329 * @tfm: cipher handle
1330 * @src: buffer holding the IV
1331 * @len: length of the IV in bytes
1333 * The caller provided IV is set for the block cipher referenced by the cipher
1336 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1337 const u8 *src, unsigned int len)
1339 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1343 * crypto_blkcipher_get_iv() - obtain IV from cipher
1344 * @tfm: cipher handle
1345 * @dst: buffer filled with the IV
1346 * @len: length of the buffer dst
1348 * The caller can obtain the IV set for the block cipher referenced by the
1349 * cipher handle and store it into the user-provided buffer. If the buffer
1350 * has an insufficient space, the IV is truncated to fit the buffer.
1352 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1353 u8 *dst, unsigned int len)
1355 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1359 * DOC: Single Block Cipher API
1361 * The single block cipher API is used with the ciphers of type
1362 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1364 * Using the single block cipher API calls, operations with the basic cipher
1365 * primitive can be implemented. These cipher primitives exclude any block
1366 * chaining operations including IV handling.
1368 * The purpose of this single block cipher API is to support the implementation
1369 * of templates or other concepts that only need to perform the cipher operation
1370 * on one block at a time. Templates invoke the underlying cipher primitive
1371 * block-wise and process either the input or the output data of these cipher
1375 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1377 return (struct crypto_cipher *)tfm;
1380 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1382 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1383 return __crypto_cipher_cast(tfm);
1387 * crypto_alloc_cipher() - allocate single block cipher handle
1388 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1389 * single block cipher
1390 * @type: specifies the type of the cipher
1391 * @mask: specifies the mask for the cipher
1393 * Allocate a cipher handle for a single block cipher. The returned struct
1394 * crypto_cipher is the cipher handle that is required for any subsequent API
1395 * invocation for that single block cipher.
1397 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1398 * of an error, PTR_ERR() returns the error code.
1400 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1403 type &= ~CRYPTO_ALG_TYPE_MASK;
1404 type |= CRYPTO_ALG_TYPE_CIPHER;
1405 mask |= CRYPTO_ALG_TYPE_MASK;
1407 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1410 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1416 * crypto_free_cipher() - zeroize and free the single block cipher handle
1417 * @tfm: cipher handle to be freed
1419 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1421 crypto_free_tfm(crypto_cipher_tfm(tfm));
1425 * crypto_has_cipher() - Search for the availability of a single block cipher
1426 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1427 * single block cipher
1428 * @type: specifies the type of the cipher
1429 * @mask: specifies the mask for the cipher
1431 * Return: true when the single block cipher is known to the kernel crypto API;
1434 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1436 type &= ~CRYPTO_ALG_TYPE_MASK;
1437 type |= CRYPTO_ALG_TYPE_CIPHER;
1438 mask |= CRYPTO_ALG_TYPE_MASK;
1440 return crypto_has_alg(alg_name, type, mask);
1443 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1445 return &crypto_cipher_tfm(tfm)->crt_cipher;
1449 * crypto_cipher_blocksize() - obtain block size for cipher
1450 * @tfm: cipher handle
1452 * The block size for the single block cipher referenced with the cipher handle
1453 * tfm is returned. The caller may use that information to allocate appropriate
1454 * memory for the data returned by the encryption or decryption operation
1456 * Return: block size of cipher
1458 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1460 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1463 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1465 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1468 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1470 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1473 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1476 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1479 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1482 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1486 * crypto_cipher_setkey() - set key for cipher
1487 * @tfm: cipher handle
1488 * @key: buffer holding the key
1489 * @keylen: length of the key in bytes
1491 * The caller provided key is set for the single block cipher referenced by the
1494 * Note, the key length determines the cipher type. Many block ciphers implement
1495 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1496 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1499 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1501 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1502 const u8 *key, unsigned int keylen)
1504 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1509 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1510 * @tfm: cipher handle
1511 * @dst: points to the buffer that will be filled with the ciphertext
1512 * @src: buffer holding the plaintext to be encrypted
1514 * Invoke the encryption operation of one block. The caller must ensure that
1515 * the plaintext and ciphertext buffers are at least one block in size.
1517 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1518 u8 *dst, const u8 *src)
1520 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1525 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1526 * @tfm: cipher handle
1527 * @dst: points to the buffer that will be filled with the plaintext
1528 * @src: buffer holding the ciphertext to be decrypted
1530 * Invoke the decryption operation of one block. The caller must ensure that
1531 * the plaintext and ciphertext buffers are at least one block in size.
1533 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1534 u8 *dst, const u8 *src)
1536 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1540 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1542 return (struct crypto_comp *)tfm;
1545 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1547 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1548 CRYPTO_ALG_TYPE_MASK);
1549 return __crypto_comp_cast(tfm);
1552 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1555 type &= ~CRYPTO_ALG_TYPE_MASK;
1556 type |= CRYPTO_ALG_TYPE_COMPRESS;
1557 mask |= CRYPTO_ALG_TYPE_MASK;
1559 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1562 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1567 static inline void crypto_free_comp(struct crypto_comp *tfm)
1569 crypto_free_tfm(crypto_comp_tfm(tfm));
1572 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1574 type &= ~CRYPTO_ALG_TYPE_MASK;
1575 type |= CRYPTO_ALG_TYPE_COMPRESS;
1576 mask |= CRYPTO_ALG_TYPE_MASK;
1578 return crypto_has_alg(alg_name, type, mask);
1581 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1583 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1586 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1588 return &crypto_comp_tfm(tfm)->crt_compress;
1591 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1592 const u8 *src, unsigned int slen,
1593 u8 *dst, unsigned int *dlen)
1595 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1596 src, slen, dst, dlen);
1599 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1600 const u8 *src, unsigned int slen,
1601 u8 *dst, unsigned int *dlen)
1603 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1604 src, slen, dst, dlen);
1607 #endif /* _LINUX_CRYPTO_H */