1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Scatterlist Cryptographic API. 4 * 5 * Copyright (c) 2002 James Morris <[email protected]> 6 * Copyright (c) 2002 David S. Miller ([email protected]) 7 * Copyright (c) 2005 Herbert Xu <[email protected]> 8 * 9 * Portions derived from Cryptoapi, by Alexander Kjeldaas <[email protected]> 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_COMPRESS 0x00000002 28 #define CRYPTO_ALG_TYPE_AEAD 0x00000003 29 #define CRYPTO_ALG_TYPE_LSKCIPHER 0x00000004 30 #define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005 31 #define CRYPTO_ALG_TYPE_AKCIPHER 0x00000006 32 #define CRYPTO_ALG_TYPE_SIG 0x00000007 33 #define CRYPTO_ALG_TYPE_KPP 0x00000008 34 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a 35 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b 36 #define CRYPTO_ALG_TYPE_RNG 0x0000000c 37 #define CRYPTO_ALG_TYPE_HASH 0x0000000e 38 #define CRYPTO_ALG_TYPE_SHASH 0x0000000e 39 #define CRYPTO_ALG_TYPE_AHASH 0x0000000f 40 41 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e 42 43 #define CRYPTO_ALG_LARVAL 0x00000010 44 #define CRYPTO_ALG_DEAD 0x00000020 45 #define CRYPTO_ALG_DYING 0x00000040 46 #define CRYPTO_ALG_ASYNC 0x00000080 47 48 /* 49 * Set if the algorithm (or an algorithm which it uses) requires another 50 * algorithm of the same type to handle corner cases. 51 */ 52 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100 53 54 /* 55 * Set if the algorithm has passed automated run-time testing. Note that 56 * if there is no run-time testing for a given algorithm it is considered 57 * to have passed. 58 */ 59 60 #define CRYPTO_ALG_TESTED 0x00000400 61 62 /* 63 * Set if the algorithm is an instance that is built from templates. 64 */ 65 #define CRYPTO_ALG_INSTANCE 0x00000800 66 67 /* Set this bit if the algorithm provided is hardware accelerated but 68 * not available to userspace via instruction set or so. 69 */ 70 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000 71 72 /* 73 * Mark a cipher as a service implementation only usable by another 74 * cipher and never by a normal user of the kernel crypto API 75 */ 76 #define CRYPTO_ALG_INTERNAL 0x00002000 77 78 /* 79 * Set if the algorithm has a ->setkey() method but can be used without 80 * calling it first, i.e. there is a default key. 81 */ 82 #define CRYPTO_ALG_OPTIONAL_KEY 0x00004000 83 84 /* 85 * Don't trigger module loading 86 */ 87 #define CRYPTO_NOLOAD 0x00008000 88 89 /* 90 * The algorithm may allocate memory during request processing, i.e. during 91 * encryption, decryption, or hashing. Users can request an algorithm with this 92 * flag unset if they can't handle memory allocation failures. 93 * 94 * This flag is currently only implemented for algorithms of type "skcipher", 95 * "aead", "ahash", "shash", and "cipher". Algorithms of other types might not 96 * have this flag set even if they allocate memory. 97 * 98 * In some edge cases, algorithms can allocate memory regardless of this flag. 99 * To avoid these cases, users must obey the following usage constraints: 100 * skcipher: 101 * - The IV buffer and all scatterlist elements must be aligned to the 102 * algorithm's alignmask. 103 * - If the data were to be divided into chunks of size 104 * crypto_skcipher_walksize() (with any remainder going at the end), no 105 * chunk can cross a page boundary or a scatterlist element boundary. 106 * aead: 107 * - The IV buffer and all scatterlist elements must be aligned to the 108 * algorithm's alignmask. 109 * - The first scatterlist element must contain all the associated data, 110 * and its pages must be !PageHighMem. 111 * - If the plaintext/ciphertext were to be divided into chunks of size 112 * crypto_aead_walksize() (with the remainder going at the end), no chunk 113 * can cross a page boundary or a scatterlist element boundary. 114 * ahash: 115 * - crypto_ahash_finup() must not be used unless the algorithm implements 116 * ->finup() natively. 117 */ 118 #define CRYPTO_ALG_ALLOCATES_MEMORY 0x00010000 119 120 /* 121 * Mark an algorithm as a service implementation only usable by a 122 * template and never by a normal user of the kernel crypto API. 123 * This is intended to be used by algorithms that are themselves 124 * not FIPS-approved but may instead be used to implement parts of 125 * a FIPS-approved algorithm (e.g., dh vs. ffdhe2048(dh)). 126 */ 127 #define CRYPTO_ALG_FIPS_INTERNAL 0x00020000 128 129 /* Set if the algorithm supports request chains and virtual addresses. */ 130 #define CRYPTO_ALG_REQ_CHAIN 0x00040000 131 132 /* 133 * Transform masks and values (for crt_flags). 134 */ 135 #define CRYPTO_TFM_NEED_KEY 0x00000001 136 137 #define CRYPTO_TFM_REQ_MASK 0x000fff00 138 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x00000100 139 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 140 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400 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 /** 249 * struct compress_alg - compression/decompression algorithm 250 * @coa_compress: Compress a buffer of specified length, storing the resulting 251 * data in the specified buffer. Return the length of the 252 * compressed data in dlen. 253 * @coa_decompress: Decompress the source buffer, storing the uncompressed 254 * data in the specified buffer. The length of the data is 255 * returned in dlen. 256 * 257 * All fields are mandatory. 258 */ 259 struct compress_alg { 260 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src, 261 unsigned int slen, u8 *dst, unsigned int *dlen); 262 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src, 263 unsigned int slen, u8 *dst, unsigned int *dlen); 264 }; 265 266 #define cra_cipher cra_u.cipher 267 #define cra_compress cra_u.compress 268 269 /** 270 * struct crypto_alg - definition of a cryptograpic cipher algorithm 271 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h 272 * CRYPTO_ALG_* flags for the flags which go in here. Those are 273 * used for fine-tuning the description of the transformation 274 * algorithm. 275 * @cra_blocksize: Minimum block size of this transformation. The size in bytes 276 * of the smallest possible unit which can be transformed with 277 * this algorithm. The users must respect this value. 278 * In case of HASH transformation, it is possible for a smaller 279 * block than @cra_blocksize to be passed to the crypto API for 280 * transformation, in case of any other transformation type, an 281 * error will be returned upon any attempt to transform smaller 282 * than @cra_blocksize chunks. 283 * @cra_ctxsize: Size of the operational context of the transformation. This 284 * value informs the kernel crypto API about the memory size 285 * needed to be allocated for the transformation context. 286 * @cra_alignmask: For cipher, skcipher, lskcipher, and aead algorithms this is 287 * 1 less than the alignment, in bytes, that the algorithm 288 * implementation requires for input and output buffers. When 289 * the crypto API is invoked with buffers that are not aligned 290 * to this alignment, the crypto API automatically utilizes 291 * appropriately aligned temporary buffers to comply with what 292 * the algorithm needs. (For scatterlists this happens only if 293 * the algorithm uses the skcipher_walk helper functions.) This 294 * misalignment handling carries a performance penalty, so it is 295 * preferred that algorithms do not set a nonzero alignmask. 296 * Also, crypto API users may wish to allocate buffers aligned 297 * to the alignmask of the algorithm being used, in order to 298 * avoid the API having to realign them. Note: the alignmask is 299 * not supported for hash algorithms and is always 0 for them. 300 * @cra_priority: Priority of this transformation implementation. In case 301 * multiple transformations with same @cra_name are available to 302 * the Crypto API, the kernel will use the one with highest 303 * @cra_priority. 304 * @cra_name: Generic name (usable by multiple implementations) of the 305 * transformation algorithm. This is the name of the transformation 306 * itself. This field is used by the kernel when looking up the 307 * providers of particular transformation. 308 * @cra_driver_name: Unique name of the transformation provider. This is the 309 * name of the provider of the transformation. This can be any 310 * arbitrary value, but in the usual case, this contains the 311 * name of the chip or provider and the name of the 312 * transformation algorithm. 313 * @cra_type: Type of the cryptographic transformation. This is a pointer to 314 * struct crypto_type, which implements callbacks common for all 315 * transformation types. There are multiple options, such as 316 * &crypto_skcipher_type, &crypto_ahash_type, &crypto_rng_type. 317 * This field might be empty. In that case, there are no common 318 * callbacks. This is the case for: cipher, compress, shash. 319 * @cra_u: Callbacks implementing the transformation. This is a union of 320 * multiple structures. Depending on the type of transformation selected 321 * by @cra_type and @cra_flags above, the associated structure must be 322 * filled with callbacks. This field might be empty. This is the case 323 * for ahash, shash. 324 * @cra_init: Initialize the cryptographic transformation object. This function 325 * is used to initialize the cryptographic transformation object. 326 * This function is called only once at the instantiation time, right 327 * after the transformation context was allocated. In case the 328 * cryptographic hardware has some special requirements which need to 329 * be handled by software, this function shall check for the precise 330 * requirement of the transformation and put any software fallbacks 331 * in place. 332 * @cra_exit: Deinitialize the cryptographic transformation object. This is a 333 * counterpart to @cra_init, used to remove various changes set in 334 * @cra_init. 335 * @cra_u.cipher: Union member which contains a single-block symmetric cipher 336 * definition. See @struct @cipher_alg. 337 * @cra_u.compress: Union member which contains a (de)compression algorithm. 338 * See @struct @compress_alg. 339 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE 340 * @cra_list: internally used 341 * @cra_users: internally used 342 * @cra_refcnt: internally used 343 * @cra_destroy: internally used 344 * 345 * The struct crypto_alg describes a generic Crypto API algorithm and is common 346 * for all of the transformations. Any variable not documented here shall not 347 * be used by a cipher implementation as it is internal to the Crypto API. 348 */ 349 struct crypto_alg { 350 struct list_head cra_list; 351 struct list_head cra_users; 352 353 u32 cra_flags; 354 unsigned int cra_blocksize; 355 unsigned int cra_ctxsize; 356 unsigned int cra_alignmask; 357 358 int cra_priority; 359 refcount_t cra_refcnt; 360 361 char cra_name[CRYPTO_MAX_ALG_NAME]; 362 char cra_driver_name[CRYPTO_MAX_ALG_NAME]; 363 364 const struct crypto_type *cra_type; 365 366 union { 367 struct cipher_alg cipher; 368 struct compress_alg compress; 369 } cra_u; 370 371 int (*cra_init)(struct crypto_tfm *tfm); 372 void (*cra_exit)(struct crypto_tfm *tfm); 373 void (*cra_destroy)(struct crypto_alg *alg); 374 375 struct module *cra_module; 376 } CRYPTO_MINALIGN_ATTR; 377 378 /* 379 * A helper struct for waiting for completion of async crypto ops 380 */ 381 struct crypto_wait { 382 struct completion completion; 383 int err; 384 }; 385 386 /* 387 * Macro for declaring a crypto op async wait object on stack 388 */ 389 #define DECLARE_CRYPTO_WAIT(_wait) \ 390 struct crypto_wait _wait = { \ 391 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 } 392 393 /* 394 * Async ops completion helper functioons 395 */ 396 void crypto_req_done(void *req, int err); 397 398 static inline int crypto_wait_req(int err, struct crypto_wait *wait) 399 { 400 switch (err) { 401 case -EINPROGRESS: 402 case -EBUSY: 403 wait_for_completion(&wait->completion); 404 reinit_completion(&wait->completion); 405 err = wait->err; 406 break; 407 } 408 409 return err; 410 } 411 412 static inline void crypto_init_wait(struct crypto_wait *wait) 413 { 414 init_completion(&wait->completion); 415 } 416 417 /* 418 * Algorithm query interface. 419 */ 420 int crypto_has_alg(const char *name, u32 type, u32 mask); 421 422 /* 423 * Transforms: user-instantiated objects which encapsulate algorithms 424 * and core processing logic. Managed via crypto_alloc_*() and 425 * crypto_free_*(), as well as the various helpers below. 426 */ 427 428 struct crypto_tfm { 429 refcount_t refcnt; 430 431 u32 crt_flags; 432 433 int node; 434 435 void (*exit)(struct crypto_tfm *tfm); 436 437 struct crypto_alg *__crt_alg; 438 439 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; 440 }; 441 442 struct crypto_comp { 443 struct crypto_tfm base; 444 }; 445 446 /* 447 * Transform user interface. 448 */ 449 450 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask); 451 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm); 452 453 static inline void crypto_free_tfm(struct crypto_tfm *tfm) 454 { 455 return crypto_destroy_tfm(tfm, tfm); 456 } 457 458 /* 459 * Transform helpers which query the underlying algorithm. 460 */ 461 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm) 462 { 463 return tfm->__crt_alg->cra_name; 464 } 465 466 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm) 467 { 468 return tfm->__crt_alg->cra_driver_name; 469 } 470 471 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm) 472 { 473 return tfm->__crt_alg->cra_blocksize; 474 } 475 476 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm) 477 { 478 return tfm->__crt_alg->cra_alignmask; 479 } 480 481 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm) 482 { 483 return tfm->crt_flags; 484 } 485 486 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags) 487 { 488 tfm->crt_flags |= flags; 489 } 490 491 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags) 492 { 493 tfm->crt_flags &= ~flags; 494 } 495 496 static inline unsigned int crypto_tfm_ctx_alignment(void) 497 { 498 struct crypto_tfm *tfm; 499 return __alignof__(tfm->__crt_ctx); 500 } 501 502 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm) 503 { 504 return (struct crypto_comp *)tfm; 505 } 506 507 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name, 508 u32 type, u32 mask) 509 { 510 type &= ~CRYPTO_ALG_TYPE_MASK; 511 type |= CRYPTO_ALG_TYPE_COMPRESS; 512 mask |= CRYPTO_ALG_TYPE_MASK; 513 514 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask)); 515 } 516 517 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm) 518 { 519 return &tfm->base; 520 } 521 522 static inline void crypto_free_comp(struct crypto_comp *tfm) 523 { 524 crypto_free_tfm(crypto_comp_tfm(tfm)); 525 } 526 527 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask) 528 { 529 type &= ~CRYPTO_ALG_TYPE_MASK; 530 type |= CRYPTO_ALG_TYPE_COMPRESS; 531 mask |= CRYPTO_ALG_TYPE_MASK; 532 533 return crypto_has_alg(alg_name, type, mask); 534 } 535 536 static inline const char *crypto_comp_name(struct crypto_comp *tfm) 537 { 538 return crypto_tfm_alg_name(crypto_comp_tfm(tfm)); 539 } 540 541 int crypto_comp_compress(struct crypto_comp *tfm, 542 const u8 *src, unsigned int slen, 543 u8 *dst, unsigned int *dlen); 544 545 int crypto_comp_decompress(struct crypto_comp *tfm, 546 const u8 *src, unsigned int slen, 547 u8 *dst, unsigned int *dlen); 548 549 static inline void crypto_reqchain_init(struct crypto_async_request *req) 550 { 551 req->err = -EINPROGRESS; 552 INIT_LIST_HEAD(&req->list); 553 } 554 555 static inline void crypto_request_chain(struct crypto_async_request *req, 556 struct crypto_async_request *head) 557 { 558 req->err = -EINPROGRESS; 559 list_add_tail(&req->list, &head->list); 560 } 561 562 static inline bool crypto_tfm_is_async(struct crypto_tfm *tfm) 563 { 564 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC; 565 } 566 567 #endif /* _LINUX_CRYPTO_H */ 568 569