Merge tag 'mm-hotfixes-stable-2023-05-03-16-27' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / include / linux / crypto.h
CommitLineData
2874c5fd 1/* SPDX-License-Identifier: GPL-2.0-or-later */
1da177e4
LT
2/*
3 * Scatterlist Cryptographic API.
4 *
5 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
6 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
5cb1454b 7 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
1da177e4
LT
8 *
9 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
18735dd8 10 * and Nettle, by Niels Möller.
1da177e4
LT
11 */
12#ifndef _LINUX_CRYPTO_H
13#define _LINUX_CRYPTO_H
14
01f727cd 15#include <linux/completion.h>
d037cb4a 16#include <linux/refcount.h>
79911102 17#include <linux/slab.h>
01f727cd 18#include <linux/types.h>
1da177e4
LT
19
20/*
21 * Algorithm masks and types.
22 */
2825982d 23#define CRYPTO_ALG_TYPE_MASK 0x0000000f
1da177e4 24#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
004a403c
LH
25#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
26#define CRYPTO_ALG_TYPE_AEAD 0x00000003
4e6c3df4 27#define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005
4e5f2c40 28#define CRYPTO_ALG_TYPE_KPP 0x00000008
2ebda74f 29#define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a
1ab53a77 30#define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b
17f0f4a4 31#define CRYPTO_ALG_TYPE_RNG 0x0000000c
3c339ab8 32#define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
63044c4f
GC
33#define CRYPTO_ALG_TYPE_HASH 0x0000000e
34#define CRYPTO_ALG_TYPE_SHASH 0x0000000e
35#define CRYPTO_ALG_TYPE_AHASH 0x0000000f
055bcee3
HX
36
37#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
63044c4f 38#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
1ab53a77 39#define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e
1da177e4 40
2825982d 41#define CRYPTO_ALG_LARVAL 0x00000010
6bfd4809
HX
42#define CRYPTO_ALG_DEAD 0x00000020
43#define CRYPTO_ALG_DYING 0x00000040
f3f632d6 44#define CRYPTO_ALG_ASYNC 0x00000080
2825982d 45
6010439f 46/*
2eb27c11
EB
47 * Set if the algorithm (or an algorithm which it uses) requires another
48 * algorithm of the same type to handle corner cases.
6010439f
HX
49 */
50#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
51
73d3864a
HX
52/*
53 * Set if the algorithm has passed automated run-time testing. Note that
54 * if there is no run-time testing for a given algorithm it is considered
55 * to have passed.
56 */
57
58#define CRYPTO_ALG_TESTED 0x00000400
59
64a947b1 60/*
864e0981 61 * Set if the algorithm is an instance that is built from templates.
64a947b1
SK
62 */
63#define CRYPTO_ALG_INSTANCE 0x00000800
64
d912bb76
NM
65/* Set this bit if the algorithm provided is hardware accelerated but
66 * not available to userspace via instruction set or so.
67 */
68#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
69
06ca7f68
SM
70/*
71 * Mark a cipher as a service implementation only usable by another
72 * cipher and never by a normal user of the kernel crypto API
73 */
74#define CRYPTO_ALG_INTERNAL 0x00002000
75
a208fa8f
EB
76/*
77 * Set if the algorithm has a ->setkey() method but can be used without
78 * calling it first, i.e. there is a default key.
79 */
80#define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
81
e2861fa7
MG
82/*
83 * Don't trigger module loading
84 */
85#define CRYPTO_NOLOAD 0x00008000
86
fbb6cda4
EB
87/*
88 * The algorithm may allocate memory during request processing, i.e. during
89 * encryption, decryption, or hashing. Users can request an algorithm with this
90 * flag unset if they can't handle memory allocation failures.
91 *
92 * This flag is currently only implemented for algorithms of type "skcipher",
93 * "aead", "ahash", "shash", and "cipher". Algorithms of other types might not
94 * have this flag set even if they allocate memory.
95 *
96 * In some edge cases, algorithms can allocate memory regardless of this flag.
97 * To avoid these cases, users must obey the following usage constraints:
98 * skcipher:
99 * - The IV buffer and all scatterlist elements must be aligned to the
100 * algorithm's alignmask.
101 * - If the data were to be divided into chunks of size
102 * crypto_skcipher_walksize() (with any remainder going at the end), no
103 * chunk can cross a page boundary or a scatterlist element boundary.
104 * aead:
105 * - The IV buffer and all scatterlist elements must be aligned to the
106 * algorithm's alignmask.
107 * - The first scatterlist element must contain all the associated data,
108 * and its pages must be !PageHighMem.
109 * - If the plaintext/ciphertext were to be divided into chunks of size
110 * crypto_aead_walksize() (with the remainder going at the end), no chunk
111 * can cross a page boundary or a scatterlist element boundary.
112 * ahash:
113 * - The result buffer must be aligned to the algorithm's alignmask.
114 * - crypto_ahash_finup() must not be used unless the algorithm implements
115 * ->finup() natively.
116 */
117#define CRYPTO_ALG_ALLOCATES_MEMORY 0x00010000
118
d6097b8d
NS
119/*
120 * Mark an algorithm as a service implementation only usable by a
121 * template and never by a normal user of the kernel crypto API.
122 * This is intended to be used by algorithms that are themselves
123 * not FIPS-approved but may instead be used to implement parts of
124 * a FIPS-approved algorithm (e.g., dh vs. ffdhe2048(dh)).
125 */
126#define CRYPTO_ALG_FIPS_INTERNAL 0x00020000
127
1da177e4
LT
128/*
129 * Transform masks and values (for crt_flags).
130 */
9fa68f62
EB
131#define CRYPTO_TFM_NEED_KEY 0x00000001
132
1da177e4 133#define CRYPTO_TFM_REQ_MASK 0x000fff00
231baecd 134#define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x00000100
64baf3cf 135#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
32e3983f 136#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
1da177e4
LT
137
138/*
139 * Miscellaneous stuff.
140 */
f437a3f4 141#define CRYPTO_MAX_ALG_NAME 128
1da177e4 142
79911102
HX
143/*
144 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
145 * declaration) is used to ensure that the crypto_tfm context structure is
146 * aligned correctly for the given architecture so that there are no alignment
660d2062
AB
147 * faults for C data types. On architectures that support non-cache coherent
148 * DMA, such as ARM or arm64, it also takes into account the minimal alignment
149 * that is required to ensure that the context struct member does not share any
150 * cachelines with the rest of the struct. This is needed to ensure that cache
151 * maintenance for non-coherent DMA (cache invalidation in particular) does not
152 * affect data that may be accessed by the CPU concurrently.
79911102 153 */
79911102 154#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
79911102 155
79911102 156#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
79911102 157
40725181 158struct crypto_tfm;
e853c3cf 159struct crypto_type;
01f727cd 160struct module;
40725181 161
255e48eb 162typedef void (*crypto_completion_t)(void *req, int err);
32e3983f 163
0d7f488f
SM
164/**
165 * DOC: Block Cipher Context Data Structures
166 *
167 * These data structures define the operating context for each block cipher
168 * type.
169 */
170
32e3983f
HX
171struct crypto_async_request {
172 struct list_head list;
173 crypto_completion_t complete;
174 void *data;
175 struct crypto_tfm *tfm;
176
177 u32 flags;
178};
179
0d7f488f
SM
180/**
181 * DOC: Block Cipher Algorithm Definitions
182 *
183 * These data structures define modular crypto algorithm implementations,
184 * managed via crypto_register_alg() and crypto_unregister_alg().
185 */
186
0d7f488f
SM
187/**
188 * struct cipher_alg - single-block symmetric ciphers definition
189 * @cia_min_keysize: Minimum key size supported by the transformation. This is
190 * the smallest key length supported by this transformation
191 * algorithm. This must be set to one of the pre-defined
192 * values as this is not hardware specific. Possible values
193 * for this field can be found via git grep "_MIN_KEY_SIZE"
194 * include/crypto/
195 * @cia_max_keysize: Maximum key size supported by the transformation. This is
196 * the largest key length supported by this transformation
197 * algorithm. This must be set to one of the pre-defined values
198 * as this is not hardware specific. Possible values for this
199 * field can be found via git grep "_MAX_KEY_SIZE"
200 * include/crypto/
201 * @cia_setkey: Set key for the transformation. This function is used to either
202 * program a supplied key into the hardware or store the key in the
203 * transformation context for programming it later. Note that this
204 * function does modify the transformation context. This function
205 * can be called multiple times during the existence of the
206 * transformation object, so one must make sure the key is properly
207 * reprogrammed into the hardware. This function is also
208 * responsible for checking the key length for validity.
209 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
210 * single block of data, which must be @cra_blocksize big. This
211 * always operates on a full @cra_blocksize and it is not possible
212 * to encrypt a block of smaller size. The supplied buffers must
213 * therefore also be at least of @cra_blocksize size. Both the
214 * input and output buffers are always aligned to @cra_alignmask.
215 * In case either of the input or output buffer supplied by user
216 * of the crypto API is not aligned to @cra_alignmask, the crypto
217 * API will re-align the buffers. The re-alignment means that a
218 * new buffer will be allocated, the data will be copied into the
219 * new buffer, then the processing will happen on the new buffer,
220 * then the data will be copied back into the original buffer and
221 * finally the new buffer will be freed. In case a software
222 * fallback was put in place in the @cra_init call, this function
223 * might need to use the fallback if the algorithm doesn't support
224 * all of the key sizes. In case the key was stored in
225 * transformation context, the key might need to be re-programmed
226 * into the hardware in this function. This function shall not
227 * modify the transformation context, as this function may be
228 * called in parallel with the same transformation object.
229 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
230 * @cia_encrypt, and the conditions are exactly the same.
231 *
232 * All fields are mandatory and must be filled.
233 */
1da177e4
LT
234struct cipher_alg {
235 unsigned int cia_min_keysize;
236 unsigned int cia_max_keysize;
6c2bb98b 237 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
560c06ae 238 unsigned int keylen);
6c2bb98b
HX
239 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
240 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
1da177e4
LT
241};
242
5a35316d
HG
243/**
244 * struct compress_alg - compression/decompression algorithm
245 * @coa_compress: Compress a buffer of specified length, storing the resulting
246 * data in the specified buffer. Return the length of the
247 * compressed data in dlen.
248 * @coa_decompress: Decompress the source buffer, storing the uncompressed
249 * data in the specified buffer. The length of the data is
250 * returned in dlen.
251 *
252 * All fields are mandatory.
253 */
1da177e4 254struct compress_alg {
6c2bb98b
HX
255 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
256 unsigned int slen, u8 *dst, unsigned int *dlen);
257 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
258 unsigned int slen, u8 *dst, unsigned int *dlen);
1da177e4
LT
259};
260
261#define cra_cipher cra_u.cipher
1da177e4
LT
262#define cra_compress cra_u.compress
263
0d7f488f
SM
264/**
265 * struct crypto_alg - definition of a cryptograpic cipher algorithm
266 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
267 * CRYPTO_ALG_* flags for the flags which go in here. Those are
268 * used for fine-tuning the description of the transformation
269 * algorithm.
270 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
271 * of the smallest possible unit which can be transformed with
272 * this algorithm. The users must respect this value.
273 * In case of HASH transformation, it is possible for a smaller
274 * block than @cra_blocksize to be passed to the crypto API for
275 * transformation, in case of any other transformation type, an
276 * error will be returned upon any attempt to transform smaller
277 * than @cra_blocksize chunks.
278 * @cra_ctxsize: Size of the operational context of the transformation. This
279 * value informs the kernel crypto API about the memory size
280 * needed to be allocated for the transformation context.
281 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
282 * buffer containing the input data for the algorithm must be
283 * aligned to this alignment mask. The data buffer for the
284 * output data must be aligned to this alignment mask. Note that
285 * the Crypto API will do the re-alignment in software, but
286 * only under special conditions and there is a performance hit.
287 * The re-alignment happens at these occasions for different
288 * @cra_u types: cipher -- For both input data and output data
289 * buffer; ahash -- For output hash destination buf; shash --
290 * For output hash destination buf.
291 * This is needed on hardware which is flawed by design and
292 * cannot pick data from arbitrary addresses.
293 * @cra_priority: Priority of this transformation implementation. In case
294 * multiple transformations with same @cra_name are available to
295 * the Crypto API, the kernel will use the one with highest
296 * @cra_priority.
297 * @cra_name: Generic name (usable by multiple implementations) of the
298 * transformation algorithm. This is the name of the transformation
299 * itself. This field is used by the kernel when looking up the
300 * providers of particular transformation.
301 * @cra_driver_name: Unique name of the transformation provider. This is the
302 * name of the provider of the transformation. This can be any
303 * arbitrary value, but in the usual case, this contains the
304 * name of the chip or provider and the name of the
305 * transformation algorithm.
306 * @cra_type: Type of the cryptographic transformation. This is a pointer to
307 * struct crypto_type, which implements callbacks common for all
c65058b7
EB
308 * transformation types. There are multiple options, such as
309 * &crypto_skcipher_type, &crypto_ahash_type, &crypto_rng_type.
0d7f488f
SM
310 * This field might be empty. In that case, there are no common
311 * callbacks. This is the case for: cipher, compress, shash.
312 * @cra_u: Callbacks implementing the transformation. This is a union of
313 * multiple structures. Depending on the type of transformation selected
314 * by @cra_type and @cra_flags above, the associated structure must be
315 * filled with callbacks. This field might be empty. This is the case
316 * for ahash, shash.
317 * @cra_init: Initialize the cryptographic transformation object. This function
318 * is used to initialize the cryptographic transformation object.
319 * This function is called only once at the instantiation time, right
320 * after the transformation context was allocated. In case the
321 * cryptographic hardware has some special requirements which need to
322 * be handled by software, this function shall check for the precise
323 * requirement of the transformation and put any software fallbacks
324 * in place.
325 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
326 * counterpart to @cra_init, used to remove various changes set in
327 * @cra_init.
0063ec44
GH
328 * @cra_u.cipher: Union member which contains a single-block symmetric cipher
329 * definition. See @struct @cipher_alg.
330 * @cra_u.compress: Union member which contains a (de)compression algorithm.
331 * See @struct @compress_alg.
0d7f488f
SM
332 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
333 * @cra_list: internally used
334 * @cra_users: internally used
335 * @cra_refcnt: internally used
336 * @cra_destroy: internally used
337 *
338 * The struct crypto_alg describes a generic Crypto API algorithm and is common
339 * for all of the transformations. Any variable not documented here shall not
340 * be used by a cipher implementation as it is internal to the Crypto API.
341 */
1da177e4
LT
342struct crypto_alg {
343 struct list_head cra_list;
6bfd4809
HX
344 struct list_head cra_users;
345
1da177e4
LT
346 u32 cra_flags;
347 unsigned int cra_blocksize;
348 unsigned int cra_ctxsize;
95477377 349 unsigned int cra_alignmask;
5cb1454b
HX
350
351 int cra_priority;
ce8614a3 352 refcount_t cra_refcnt;
5cb1454b 353
d913ea0d
HX
354 char cra_name[CRYPTO_MAX_ALG_NAME];
355 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
1da177e4 356
e853c3cf
HX
357 const struct crypto_type *cra_type;
358
1da177e4
LT
359 union {
360 struct cipher_alg cipher;
1da177e4
LT
361 struct compress_alg compress;
362 } cra_u;
c7fc0599
HX
363
364 int (*cra_init)(struct crypto_tfm *tfm);
365 void (*cra_exit)(struct crypto_tfm *tfm);
6521f302 366 void (*cra_destroy)(struct crypto_alg *alg);
1da177e4
LT
367
368 struct module *cra_module;
edf18b91 369} CRYPTO_MINALIGN_ATTR;
1da177e4 370
ada69a16
GBY
371/*
372 * A helper struct for waiting for completion of async crypto ops
373 */
374struct crypto_wait {
375 struct completion completion;
376 int err;
377};
378
379/*
380 * Macro for declaring a crypto op async wait object on stack
381 */
382#define DECLARE_CRYPTO_WAIT(_wait) \
383 struct crypto_wait _wait = { \
384 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
385
386/*
387 * Async ops completion helper functioons
388 */
255e48eb 389void crypto_req_done(void *req, int err);
ada69a16
GBY
390
391static inline int crypto_wait_req(int err, struct crypto_wait *wait)
392{
393 switch (err) {
394 case -EINPROGRESS:
395 case -EBUSY:
396 wait_for_completion(&wait->completion);
397 reinit_completion(&wait->completion);
398 err = wait->err;
399 break;
c782937e 400 }
ada69a16
GBY
401
402 return err;
403}
404
405static inline void crypto_init_wait(struct crypto_wait *wait)
406{
407 init_completion(&wait->completion);
408}
409
1da177e4
LT
410/*
411 * Algorithm query interface.
412 */
fce32d70 413int crypto_has_alg(const char *name, u32 type, u32 mask);
1da177e4
LT
414
415/*
416 * Transforms: user-instantiated objects which encapsulate algorithms
6d7d684d
HX
417 * and core processing logic. Managed via crypto_alloc_*() and
418 * crypto_free_*(), as well as the various helpers below.
1da177e4 419 */
1da177e4 420
1da177e4 421struct crypto_tfm {
ae131f49 422 refcount_t refcnt;
1da177e4
LT
423
424 u32 crt_flags;
7bc13b5b
BS
425
426 int node;
1da177e4 427
4a779486 428 void (*exit)(struct crypto_tfm *tfm);
1da177e4
LT
429
430 struct crypto_alg *__crt_alg;
f10b7897 431
79911102 432 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
1da177e4
LT
433};
434
78a1fe4f
HX
435struct crypto_comp {
436 struct crypto_tfm base;
437};
438
1da177e4
LT
439/*
440 * Transform user interface.
441 */
442
6d7d684d 443struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
7b2cd92a
HX
444void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
445
446static inline void crypto_free_tfm(struct crypto_tfm *tfm)
447{
448 return crypto_destroy_tfm(tfm, tfm);
449}
1da177e4
LT
450
451/*
452 * Transform helpers which query the underlying algorithm.
453 */
454static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
455{
456 return tfm->__crt_alg->cra_name;
457}
458
b14cdd67
ML
459static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
460{
461 return tfm->__crt_alg->cra_driver_name;
462}
463
1da177e4
LT
464static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
465{
466 return tfm->__crt_alg->cra_blocksize;
467}
468
fbdae9f3
HX
469static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
470{
471 return tfm->__crt_alg->cra_alignmask;
472}
473
f28776a3
HX
474static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
475{
476 return tfm->crt_flags;
477}
478
479static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
480{
481 tfm->crt_flags |= flags;
482}
483
484static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
485{
486 tfm->crt_flags &= ~flags;
487}
488
f10b7897
HX
489static inline unsigned int crypto_tfm_ctx_alignment(void)
490{
491 struct crypto_tfm *tfm;
492 return __alignof__(tfm->__crt_ctx);
40725181
HX
493}
494
fce32d70
HX
495static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
496{
497 return (struct crypto_comp *)tfm;
498}
499
fce32d70
HX
500static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
501 u32 type, u32 mask)
502{
503 type &= ~CRYPTO_ALG_TYPE_MASK;
504 type |= CRYPTO_ALG_TYPE_COMPRESS;
505 mask |= CRYPTO_ALG_TYPE_MASK;
506
507 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
508}
509
510static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
511{
78a1fe4f 512 return &tfm->base;
fce32d70
HX
513}
514
515static inline void crypto_free_comp(struct crypto_comp *tfm)
516{
517 crypto_free_tfm(crypto_comp_tfm(tfm));
518}
519
520static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
521{
522 type &= ~CRYPTO_ALG_TYPE_MASK;
523 type |= CRYPTO_ALG_TYPE_COMPRESS;
524 mask |= CRYPTO_ALG_TYPE_MASK;
525
526 return crypto_has_alg(alg_name, type, mask);
527}
528
e4d5b79c
HX
529static inline const char *crypto_comp_name(struct crypto_comp *tfm)
530{
531 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
532}
533
c441a909
EB
534int crypto_comp_compress(struct crypto_comp *tfm,
535 const u8 *src, unsigned int slen,
536 u8 *dst, unsigned int *dlen);
1da177e4 537
c441a909
EB
538int crypto_comp_decompress(struct crypto_comp *tfm,
539 const u8 *src, unsigned int slen,
540 u8 *dst, unsigned int *dlen);
1da177e4 541
1da177e4
LT
542#endif /* _LINUX_CRYPTO_H */
543