1 /* SPDX-License-Identifier: GPL-2.0-or-later */
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)
7 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
8 *
9 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
10 * and Nettle, by Niels Möller.
11 */
12 #ifndef _LINUX_CRYPTO_H
13 #define _LINUX_CRYPTO_H
14
15 #include <linux/completion.h>
16 #include <linux/errno.h>
17 #include <linux/list.h>
18 #include <linux/refcount.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21
22 /*
23 * Algorithm masks and types.
24 */
25 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
26 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
27 #define CRYPTO_ALG_TYPE_AEAD 0x00000003
28 #define CRYPTO_ALG_TYPE_LSKCIPHER 0x00000004
29 #define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005
30 #define CRYPTO_ALG_TYPE_AKCIPHER 0x00000006
31 #define CRYPTO_ALG_TYPE_SIG 0x00000007
32 #define CRYPTO_ALG_TYPE_KPP 0x00000008
33 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a
34 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b
35 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
36 #define CRYPTO_ALG_TYPE_HASH 0x0000000e
37 #define CRYPTO_ALG_TYPE_SHASH 0x0000000e
38 #define CRYPTO_ALG_TYPE_AHASH 0x0000000f
39
40 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e
41
42 #define CRYPTO_ALG_LARVAL 0x00000010
43 #define CRYPTO_ALG_DEAD 0x00000020
44 #define CRYPTO_ALG_DYING 0x00000040
45 #define CRYPTO_ALG_ASYNC 0x00000080
46
47 /*
48 * Set if the algorithm (or an algorithm which it uses) requires another
49 * algorithm of the same type to handle corner cases.
50 */
51 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
52
53 /*
54 * Set if the algorithm has passed automated run-time testing. Note that
55 * if there is no run-time testing for a given algorithm it is considered
56 * to have passed.
57 */
58
59 #define CRYPTO_ALG_TESTED 0x00000400
60
61 /*
62 * Set if the algorithm is an instance that is built from templates.
63 */
64 #define CRYPTO_ALG_INSTANCE 0x00000800
65
66 /* Set this bit if the algorithm provided is hardware accelerated but
67 * not available to userspace via instruction set or so.
68 */
69 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
70
71 /*
72 * Mark a cipher as a service implementation only usable by another
73 * cipher and never by a normal user of the kernel crypto API
74 */
75 #define CRYPTO_ALG_INTERNAL 0x00002000
76
77 /*
78 * Set if the algorithm has a ->setkey() method but can be used without
79 * calling it first, i.e. there is a default key.
80 */
81 #define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
82
83 /*
84 * Don't trigger module loading
85 */
86 #define CRYPTO_NOLOAD 0x00008000
87
88 /*
89 * The algorithm may allocate memory during request processing, i.e. during
90 * encryption, decryption, or hashing. Users can request an algorithm with this
91 * flag unset if they can't handle memory allocation failures.
92 *
93 * This flag is currently only implemented for algorithms of type "skcipher",
94 * "aead", "ahash", "shash", and "cipher". Algorithms of other types might not
95 * have this flag set even if they allocate memory.
96 *
97 * In some edge cases, algorithms can allocate memory regardless of this flag.
98 * To avoid these cases, users must obey the following usage constraints:
99 * skcipher:
100 * - The IV buffer and all scatterlist elements must be aligned to the
101 * algorithm's alignmask.
102 * - If the data were to be divided into chunks of size
103 * crypto_skcipher_walksize() (with any remainder going at the end), no
104 * chunk can cross a page boundary or a scatterlist element boundary.
105 * aead:
106 * - The IV buffer and all scatterlist elements must be aligned to the
107 * algorithm's alignmask.
108 * - The first scatterlist element must contain all the associated data,
109 * and its pages must be !PageHighMem.
110 * - If the plaintext/ciphertext were to be divided into chunks of size
111 * crypto_aead_walksize() (with the remainder going at the end), no chunk
112 * can cross a page boundary or a scatterlist element boundary.
113 * ahash:
114 * - crypto_ahash_finup() must not be used unless the algorithm implements
115 * ->finup() natively.
116 */
117 #define CRYPTO_ALG_ALLOCATES_MEMORY 0x00010000
118
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
128 /* Set if the algorithm supports request chains and virtual addresses. */
129 #define CRYPTO_ALG_REQ_CHAIN 0x00040000
130
131 /*
132 * Transform masks and values (for crt_flags).
133 */
134 #define CRYPTO_TFM_NEED_KEY 0x00000001
135
136 #define CRYPTO_TFM_REQ_MASK 0x000fff00
137 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x00000100
138 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
139 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
140 #define CRYPTO_TFM_REQ_ON_STACK 0x00000800
141
142 /*
143 * Miscellaneous stuff.
144 */
145 #define CRYPTO_MAX_ALG_NAME 128
146
147 /*
148 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
149 * declaration) is used to ensure that the crypto_tfm context structure is
150 * aligned correctly for the given architecture so that there are no alignment
151 * faults for C data types. On architectures that support non-cache coherent
152 * DMA, such as ARM or arm64, it also takes into account the minimal alignment
153 * that is required to ensure that the context struct member does not share any
154 * cachelines with the rest of the struct. This is needed to ensure that cache
155 * maintenance for non-coherent DMA (cache invalidation in particular) does not
156 * affect data that may be accessed by the CPU concurrently.
157 */
158 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
159
160 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
161
162 struct crypto_tfm;
163 struct crypto_type;
164 struct module;
165
166 typedef void (*crypto_completion_t)(void *req, int err);
167
168 /**
169 * DOC: Block Cipher Context Data Structures
170 *
171 * These data structures define the operating context for each block cipher
172 * type.
173 */
174
175 struct crypto_async_request {
176 struct list_head list;
177 crypto_completion_t complete;
178 void *data;
179 struct crypto_tfm *tfm;
180
181 u32 flags;
182 int err;
183 };
184
185 /**
186 * DOC: Block Cipher Algorithm Definitions
187 *
188 * These data structures define modular crypto algorithm implementations,
189 * managed via crypto_register_alg() and crypto_unregister_alg().
190 */
191
192 /**
193 * struct cipher_alg - single-block symmetric ciphers definition
194 * @cia_min_keysize: Minimum key size supported by the transformation. This is
195 * the smallest key length supported by this transformation
196 * algorithm. This must be set to one of the pre-defined
197 * values as this is not hardware specific. Possible values
198 * for this field can be found via git grep "_MIN_KEY_SIZE"
199 * include/crypto/
200 * @cia_max_keysize: Maximum key size supported by the transformation. This is
201 * the largest key length supported by this transformation
202 * algorithm. This must be set to one of the pre-defined values
203 * as this is not hardware specific. Possible values for this
204 * field can be found via git grep "_MAX_KEY_SIZE"
205 * include/crypto/
206 * @cia_setkey: Set key for the transformation. This function is used to either
207 * program a supplied key into the hardware or store the key in the
208 * transformation context for programming it later. Note that this
209 * function does modify the transformation context. This function
210 * can be called multiple times during the existence of the
211 * transformation object, so one must make sure the key is properly
212 * reprogrammed into the hardware. This function is also
213 * responsible for checking the key length for validity.
214 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
215 * single block of data, which must be @cra_blocksize big. This
216 * always operates on a full @cra_blocksize and it is not possible
217 * to encrypt a block of smaller size. The supplied buffers must
218 * therefore also be at least of @cra_blocksize size. Both the
219 * input and output buffers are always aligned to @cra_alignmask.
220 * In case either of the input or output buffer supplied by user
221 * of the crypto API is not aligned to @cra_alignmask, the crypto
222 * API will re-align the buffers. The re-alignment means that a
223 * new buffer will be allocated, the data will be copied into the
224 * new buffer, then the processing will happen on the new buffer,
225 * then the data will be copied back into the original buffer and
226 * finally the new buffer will be freed. In case a software
227 * fallback was put in place in the @cra_init call, this function
228 * might need to use the fallback if the algorithm doesn't support
229 * all of the key sizes. In case the key was stored in
230 * transformation context, the key might need to be re-programmed
231 * into the hardware in this function. This function shall not
232 * modify the transformation context, as this function may be
233 * called in parallel with the same transformation object.
234 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
235 * @cia_encrypt, and the conditions are exactly the same.
236 *
237 * All fields are mandatory and must be filled.
238 */
239 struct cipher_alg {
240 unsigned int cia_min_keysize;
241 unsigned int cia_max_keysize;
242 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
243 unsigned int keylen);
244 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
245 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
246 };
247
248 #define cra_cipher cra_u.cipher
249
250 /**
251 * struct crypto_alg - definition of a cryptograpic cipher algorithm
252 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
253 * CRYPTO_ALG_* flags for the flags which go in here. Those are
254 * used for fine-tuning the description of the transformation
255 * algorithm.
256 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
257 * of the smallest possible unit which can be transformed with
258 * this algorithm. The users must respect this value.
259 * In case of HASH transformation, it is possible for a smaller
260 * block than @cra_blocksize to be passed to the crypto API for
261 * transformation, in case of any other transformation type, an
262 * error will be returned upon any attempt to transform smaller
263 * than @cra_blocksize chunks.
264 * @cra_ctxsize: Size of the operational context of the transformation. This
265 * value informs the kernel crypto API about the memory size
266 * needed to be allocated for the transformation context.
267 * @cra_alignmask: For cipher, skcipher, lskcipher, and aead algorithms this is
268 * 1 less than the alignment, in bytes, that the algorithm
269 * implementation requires for input and output buffers. When
270 * the crypto API is invoked with buffers that are not aligned
271 * to this alignment, the crypto API automatically utilizes
272 * appropriately aligned temporary buffers to comply with what
273 * the algorithm needs. (For scatterlists this happens only if
274 * the algorithm uses the skcipher_walk helper functions.) This
275 * misalignment handling carries a performance penalty, so it is
276 * preferred that algorithms do not set a nonzero alignmask.
277 * Also, crypto API users may wish to allocate buffers aligned
278 * to the alignmask of the algorithm being used, in order to
279 * avoid the API having to realign them. Note: the alignmask is
280 * not supported for hash algorithms and is always 0 for them.
281 * @cra_priority: Priority of this transformation implementation. In case
282 * multiple transformations with same @cra_name are available to
283 * the Crypto API, the kernel will use the one with highest
284 * @cra_priority.
285 * @cra_name: Generic name (usable by multiple implementations) of the
286 * transformation algorithm. This is the name of the transformation
287 * itself. This field is used by the kernel when looking up the
288 * providers of particular transformation.
289 * @cra_driver_name: Unique name of the transformation provider. This is the
290 * name of the provider of the transformation. This can be any
291 * arbitrary value, but in the usual case, this contains the
292 * name of the chip or provider and the name of the
293 * transformation algorithm.
294 * @cra_type: Type of the cryptographic transformation. This is a pointer to
295 * struct crypto_type, which implements callbacks common for all
296 * transformation types. There are multiple options, such as
297 * &crypto_skcipher_type, &crypto_ahash_type, &crypto_rng_type.
298 * This field might be empty. In that case, there are no common
299 * callbacks. This is the case for: cipher.
300 * @cra_u: Callbacks implementing the transformation. This is a union of
301 * multiple structures. Depending on the type of transformation selected
302 * by @cra_type and @cra_flags above, the associated structure must be
303 * filled with callbacks. This field might be empty. This is the case
304 * for ahash, shash.
305 * @cra_init: Initialize the cryptographic transformation object. This function
306 * is used to initialize the cryptographic transformation object.
307 * This function is called only once at the instantiation time, right
308 * after the transformation context was allocated. In case the
309 * cryptographic hardware has some special requirements which need to
310 * be handled by software, this function shall check for the precise
311 * requirement of the transformation and put any software fallbacks
312 * in place.
313 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
314 * counterpart to @cra_init, used to remove various changes set in
315 * @cra_init.
316 * @cra_u.cipher: Union member which contains a single-block symmetric cipher
317 * definition. See @struct @cipher_alg.
318 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
319 * @cra_list: internally used
320 * @cra_users: internally used
321 * @cra_refcnt: internally used
322 * @cra_destroy: internally used
323 *
324 * The struct crypto_alg describes a generic Crypto API algorithm and is common
325 * for all of the transformations. Any variable not documented here shall not
326 * be used by a cipher implementation as it is internal to the Crypto API.
327 */
328 struct crypto_alg {
329 struct list_head cra_list;
330 struct list_head cra_users;
331
332 u32 cra_flags;
333 unsigned int cra_blocksize;
334 unsigned int cra_ctxsize;
335 unsigned int cra_alignmask;
336
337 int cra_priority;
338 refcount_t cra_refcnt;
339
340 char cra_name[CRYPTO_MAX_ALG_NAME];
341 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
342
343 const struct crypto_type *cra_type;
344
345 union {
346 struct cipher_alg cipher;
347 } cra_u;
348
349 int (*cra_init)(struct crypto_tfm *tfm);
350 void (*cra_exit)(struct crypto_tfm *tfm);
351 void (*cra_destroy)(struct crypto_alg *alg);
352
353 struct module *cra_module;
354 } CRYPTO_MINALIGN_ATTR;
355
356 /*
357 * A helper struct for waiting for completion of async crypto ops
358 */
359 struct crypto_wait {
360 struct completion completion;
361 int err;
362 };
363
364 /*
365 * Macro for declaring a crypto op async wait object on stack
366 */
367 #define DECLARE_CRYPTO_WAIT(_wait) \
368 struct crypto_wait _wait = { \
369 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
370
371 /*
372 * Async ops completion helper functioons
373 */
374 void crypto_req_done(void *req, int err);
375
crypto_wait_req(int err,struct crypto_wait * wait)376 static inline int crypto_wait_req(int err, struct crypto_wait *wait)
377 {
378 switch (err) {
379 case -EINPROGRESS:
380 case -EBUSY:
381 wait_for_completion(&wait->completion);
382 reinit_completion(&wait->completion);
383 err = wait->err;
384 break;
385 }
386
387 return err;
388 }
389
crypto_init_wait(struct crypto_wait * wait)390 static inline void crypto_init_wait(struct crypto_wait *wait)
391 {
392 init_completion(&wait->completion);
393 }
394
395 /*
396 * Algorithm query interface.
397 */
398 int crypto_has_alg(const char *name, u32 type, u32 mask);
399
400 /*
401 * Transforms: user-instantiated objects which encapsulate algorithms
402 * and core processing logic. Managed via crypto_alloc_*() and
403 * crypto_free_*(), as well as the various helpers below.
404 */
405
406 struct crypto_tfm {
407 refcount_t refcnt;
408
409 u32 crt_flags;
410
411 int node;
412
413 void (*exit)(struct crypto_tfm *tfm);
414
415 struct crypto_alg *__crt_alg;
416
417 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
418 };
419
420 /*
421 * Transform user interface.
422 */
423
424 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
425 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
426
crypto_free_tfm(struct crypto_tfm * tfm)427 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
428 {
429 return crypto_destroy_tfm(tfm, tfm);
430 }
431
432 /*
433 * Transform helpers which query the underlying algorithm.
434 */
crypto_tfm_alg_name(struct crypto_tfm * tfm)435 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
436 {
437 return tfm->__crt_alg->cra_name;
438 }
439
crypto_tfm_alg_driver_name(struct crypto_tfm * tfm)440 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
441 {
442 return tfm->__crt_alg->cra_driver_name;
443 }
444
crypto_tfm_alg_blocksize(struct crypto_tfm * tfm)445 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
446 {
447 return tfm->__crt_alg->cra_blocksize;
448 }
449
crypto_tfm_alg_alignmask(struct crypto_tfm * tfm)450 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
451 {
452 return tfm->__crt_alg->cra_alignmask;
453 }
454
crypto_tfm_get_flags(struct crypto_tfm * tfm)455 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
456 {
457 return tfm->crt_flags;
458 }
459
crypto_tfm_set_flags(struct crypto_tfm * tfm,u32 flags)460 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
461 {
462 tfm->crt_flags |= flags;
463 }
464
crypto_tfm_clear_flags(struct crypto_tfm * tfm,u32 flags)465 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
466 {
467 tfm->crt_flags &= ~flags;
468 }
469
crypto_tfm_ctx_alignment(void)470 static inline unsigned int crypto_tfm_ctx_alignment(void)
471 {
472 struct crypto_tfm *tfm;
473 return __alignof__(tfm->__crt_ctx);
474 }
475
crypto_reqchain_init(struct crypto_async_request * req)476 static inline void crypto_reqchain_init(struct crypto_async_request *req)
477 {
478 req->err = -EINPROGRESS;
479 INIT_LIST_HEAD(&req->list);
480 }
481
crypto_request_chain(struct crypto_async_request * req,struct crypto_async_request * head)482 static inline void crypto_request_chain(struct crypto_async_request *req,
483 struct crypto_async_request *head)
484 {
485 req->err = -EINPROGRESS;
486 list_add_tail(&req->list, &head->list);
487 }
488
crypto_tfm_is_async(struct crypto_tfm * tfm)489 static inline bool crypto_tfm_is_async(struct crypto_tfm *tfm)
490 {
491 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC;
492 }
493
494 #endif /* _LINUX_CRYPTO_H */
495
496