1 /* SPDX-License-Identifier: GPL-2.0-or-later * 1 2 /* 3 * Scatterlist Cryptographic API. 4 * 5 * Copyright (c) 2002 James Morris <jmorris@in 6 * Copyright (c) 2002 David S. Miller (davem@r 7 * Copyright (c) 2005 Herbert Xu <herbert@gond 8 * 9 * Portions derived from Cryptoapi, by Alexand 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/refcount.h> 17 #include <linux/slab.h> 18 #include <linux/types.h> 19 20 /* 21 * Algorithm masks and types. 22 */ 23 #define CRYPTO_ALG_TYPE_MASK 0x0000 24 #define CRYPTO_ALG_TYPE_CIPHER 0x0000 25 #define CRYPTO_ALG_TYPE_COMPRESS 0x0000 26 #define CRYPTO_ALG_TYPE_AEAD 0x0000 27 #define CRYPTO_ALG_TYPE_LSKCIPHER 0x0000 28 #define CRYPTO_ALG_TYPE_SKCIPHER 0x0000 29 #define CRYPTO_ALG_TYPE_AKCIPHER 0x0000 30 #define CRYPTO_ALG_TYPE_SIG 0x0000 31 #define CRYPTO_ALG_TYPE_KPP 0x0000 32 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000 33 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000 34 #define CRYPTO_ALG_TYPE_RNG 0x0000 35 #define CRYPTO_ALG_TYPE_HASH 0x0000 36 #define CRYPTO_ALG_TYPE_SHASH 0x0000 37 #define CRYPTO_ALG_TYPE_AHASH 0x0000 38 39 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000 40 41 #define CRYPTO_ALG_LARVAL 0x0000 42 #define CRYPTO_ALG_DEAD 0x0000 43 #define CRYPTO_ALG_DYING 0x0000 44 #define CRYPTO_ALG_ASYNC 0x0000 45 46 /* 47 * Set if the algorithm (or an algorithm which 48 * algorithm of the same type to handle corner 49 */ 50 #define CRYPTO_ALG_NEED_FALLBACK 0x0000 51 52 /* 53 * Set if the algorithm has passed automated r 54 * if there is no run-time testing for a given 55 * to have passed. 56 */ 57 58 #define CRYPTO_ALG_TESTED 0x0000 59 60 /* 61 * Set if the algorithm is an instance that is 62 */ 63 #define CRYPTO_ALG_INSTANCE 0x0000 64 65 /* Set this bit if the algorithm provided is h 66 * not available to userspace via instruction 67 */ 68 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x0000 69 70 /* 71 * Mark a cipher as a service implementation o 72 * cipher and never by a normal user of the ke 73 */ 74 #define CRYPTO_ALG_INTERNAL 0x0000 75 76 /* 77 * Set if the algorithm has a ->setkey() metho 78 * calling it first, i.e. there is a default k 79 */ 80 #define CRYPTO_ALG_OPTIONAL_KEY 0x0000 81 82 /* 83 * Don't trigger module loading 84 */ 85 #define CRYPTO_NOLOAD 0x0000 86 87 /* 88 * The algorithm may allocate memory during re 89 * encryption, decryption, or hashing. Users 90 * flag unset if they can't handle memory allo 91 * 92 * This flag is currently only implemented for 93 * "aead", "ahash", "shash", and "cipher". Al 94 * have this flag set even if they allocate me 95 * 96 * In some edge cases, algorithms can allocate 97 * To avoid these cases, users must obey the f 98 * skcipher: 99 * - The IV buffer and all scatterlist el 100 * algorithm's alignmask. 101 * - If the data were to be divided into 102 * crypto_skcipher_walksize() (with any 103 * chunk can cross a page boundary or a 104 * aead: 105 * - The IV buffer and all scatterlist el 106 * algorithm's alignmask. 107 * - The first scatterlist element must c 108 * and its pages must be !PageHighMem. 109 * - If the plaintext/ciphertext were to 110 * crypto_aead_walksize() (with the rem 111 * can cross a page boundary or a scatt 112 * ahash: 113 * - crypto_ahash_finup() must not be use 114 * ->finup() natively. 115 */ 116 #define CRYPTO_ALG_ALLOCATES_MEMORY 0x0001 117 118 /* 119 * Mark an algorithm as a service implementati 120 * template and never by a normal user of the 121 * This is intended to be used by algorithms t 122 * not FIPS-approved but may instead be used t 123 * a FIPS-approved algorithm (e.g., dh vs. ffd 124 */ 125 #define CRYPTO_ALG_FIPS_INTERNAL 0x0002 126 127 /* 128 * Transform masks and values (for crt_flags). 129 */ 130 #define CRYPTO_TFM_NEED_KEY 0x0000 131 132 #define CRYPTO_TFM_REQ_MASK 0x000f 133 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x0000 134 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x0000 135 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x0000 136 137 /* 138 * Miscellaneous stuff. 139 */ 140 #define CRYPTO_MAX_ALG_NAME 128 141 142 /* 143 * The macro CRYPTO_MINALIGN_ATTR (along with 144 * declaration) is used to ensure that the cry 145 * aligned correctly for the given architectur 146 * faults for C data types. On architectures 147 * DMA, such as ARM or arm64, it also takes in 148 * that is required to ensure that the context 149 * cachelines with the rest of the struct. Thi 150 * maintenance for non-coherent DMA (cache inv 151 * affect data that may be accessed by the CPU 152 */ 153 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN 154 155 #define CRYPTO_MINALIGN_ATTR __attribute__ ((_ 156 157 struct crypto_tfm; 158 struct crypto_type; 159 struct module; 160 161 typedef void (*crypto_completion_t)(void *req, 162 163 /** 164 * DOC: Block Cipher Context Data Structures 165 * 166 * These data structures define the operating 167 * type. 168 */ 169 170 struct crypto_async_request { 171 struct list_head list; 172 crypto_completion_t complete; 173 void *data; 174 struct crypto_tfm *tfm; 175 176 u32 flags; 177 }; 178 179 /** 180 * DOC: Block Cipher Algorithm Definitions 181 * 182 * These data structures define modular crypto 183 * managed via crypto_register_alg() and crypt 184 */ 185 186 /** 187 * struct cipher_alg - single-block symmetric 188 * @cia_min_keysize: Minimum key size supporte 189 * the smallest key length s 190 * algorithm. This must be s 191 * values as this is not har 192 * for this field can be fou 193 * include/crypto/ 194 * @cia_max_keysize: Maximum key size supporte 195 * the largest key length sup 196 * algorithm. This must be se 197 * as this is not hardware sp 198 * field can be found via git 199 * include/crypto/ 200 * @cia_setkey: Set key for the transformation 201 * program a supplied key into th 202 * transformation context for pro 203 * function does modify the trans 204 * can be called multiple times d 205 * transformation object, so one 206 * reprogrammed into the hardware 207 * responsible for checking the k 208 * @cia_encrypt: Encrypt a single block. This 209 * single block of data, which m 210 * always operates on a full @cr 211 * to encrypt a block of smaller 212 * therefore also be at least of 213 * input and output buffers are 214 * In case either of the input o 215 * of the crypto API is not alig 216 * API will re-align the buffers 217 * new buffer will be allocated, 218 * new buffer, then the processi 219 * then the data will be copied 220 * finally the new buffer will b 221 * fallback was put in place in 222 * might need to use the fallbac 223 * all of the key sizes. In case 224 * transformation context, the k 225 * into the hardware in this fun 226 * modify the transformation con 227 * called in parallel with the s 228 * @cia_decrypt: Decrypt a single block. This 229 * @cia_encrypt, and the conditi 230 * 231 * All fields are mandatory and must be filled 232 */ 233 struct cipher_alg { 234 unsigned int cia_min_keysize; 235 unsigned int cia_max_keysize; 236 int (*cia_setkey)(struct crypto_tfm *t 237 unsigned int keylen) 238 void (*cia_encrypt)(struct crypto_tfm 239 void (*cia_decrypt)(struct crypto_tfm 240 }; 241 242 /** 243 * struct compress_alg - compression/decompres 244 * @coa_compress: Compress a buffer of specifi 245 * data in the specified buffer 246 * compressed data in dlen. 247 * @coa_decompress: Decompress the source buff 248 * data in the specified buff 249 * returned in dlen. 250 * 251 * All fields are mandatory. 252 */ 253 struct compress_alg { 254 int (*coa_compress)(struct crypto_tfm 255 unsigned int slen, 256 int (*coa_decompress)(struct crypto_tf 257 unsigned int sle 258 }; 259 260 #define cra_cipher cra_u.cipher 261 #define cra_compress cra_u.compress 262 263 /** 264 * struct crypto_alg - definition of a cryptog 265 * @cra_flags: Flags describing this transform 266 * CRYPTO_ALG_* flags for the flag 267 * used for fine-tuning the descri 268 * algorithm. 269 * @cra_blocksize: Minimum block size of this 270 * of the smallest possible un 271 * this algorithm. The users m 272 * In case of HASH transformat 273 * block than @cra_blocksize t 274 * transformation, in case of 275 * error will be returned upon 276 * than @cra_blocksize chunks. 277 * @cra_ctxsize: Size of the operational conte 278 * value informs the kernel cryp 279 * needed to be allocated for th 280 * @cra_alignmask: For cipher, skcipher, lskci 281 * 1 less than the alignment, 282 * implementation requires for 283 * the crypto API is invoked w 284 * to this alignment, the cryp 285 * appropriately aligned tempo 286 * the algorithm needs. (For 287 * the algorithm uses the skci 288 * misalignment handling carri 289 * preferred that algorithms d 290 * Also, crypto API users may 291 * to the alignmask of the alg 292 * avoid the API having to rea 293 * not supported for hash algo 294 * @cra_priority: Priority of this transformat 295 * multiple transformations wit 296 * the Crypto API, the kernel w 297 * @cra_priority. 298 * @cra_name: Generic name (usable by multiple 299 * transformation algorithm. This i 300 * itself. This field is used by th 301 * providers of particular transfor 302 * @cra_driver_name: Unique name of the transf 303 * name of the provider of t 304 * arbitrary value, but in t 305 * name of the chip or provi 306 * transformation algorithm. 307 * @cra_type: Type of the cryptographic transf 308 * struct crypto_type, which implem 309 * transformation types. There are 310 * &crypto_skcipher_type, &crypto_a 311 * This field might be empty. In th 312 * callbacks. This is the case for: 313 * @cra_u: Callbacks implementing the transfor 314 * multiple structures. Depending on t 315 * by @cra_type and @cra_flags above, 316 * filled with callbacks. This field m 317 * for ahash, shash. 318 * @cra_init: Initialize the cryptographic tra 319 * is used to initialize the crypto 320 * This function is called only onc 321 * after the transformation context 322 * cryptographic hardware has some 323 * be handled by software, this fun 324 * requirement of the transformatio 325 * in place. 326 * @cra_exit: Deinitialize the cryptographic t 327 * counterpart to @cra_init, used t 328 * @cra_init. 329 * @cra_u.cipher: Union member which contains 330 * definition. See @struct @cip 331 * @cra_u.compress: Union member which contain 332 * See @struct @compress_alg. 333 * @cra_module: Owner of this transformation i 334 * @cra_list: internally used 335 * @cra_users: internally used 336 * @cra_refcnt: internally used 337 * @cra_destroy: internally used 338 * 339 * The struct crypto_alg describes a generic C 340 * for all of the transformations. Any variabl 341 * be used by a cipher implementation as it is 342 */ 343 struct crypto_alg { 344 struct list_head cra_list; 345 struct list_head cra_users; 346 347 u32 cra_flags; 348 unsigned int cra_blocksize; 349 unsigned int cra_ctxsize; 350 unsigned int cra_alignmask; 351 352 int cra_priority; 353 refcount_t cra_refcnt; 354 355 char cra_name[CRYPTO_MAX_ALG_NAME]; 356 char cra_driver_name[CRYPTO_MAX_ALG_NA 357 358 const struct crypto_type *cra_type; 359 360 union { 361 struct cipher_alg cipher; 362 struct compress_alg compress; 363 } cra_u; 364 365 int (*cra_init)(struct crypto_tfm *tfm 366 void (*cra_exit)(struct crypto_tfm *tf 367 void (*cra_destroy)(struct crypto_alg 368 369 struct module *cra_module; 370 } CRYPTO_MINALIGN_ATTR; 371 372 /* 373 * A helper struct for waiting for completion 374 */ 375 struct crypto_wait { 376 struct completion completion; 377 int err; 378 }; 379 380 /* 381 * Macro for declaring a crypto op async wait 382 */ 383 #define DECLARE_CRYPTO_WAIT(_wait) \ 384 struct crypto_wait _wait = { \ 385 COMPLETION_INITIALIZER_ONSTACK 386 387 /* 388 * Async ops completion helper functioons 389 */ 390 void crypto_req_done(void *req, int err); 391 392 static inline int crypto_wait_req(int err, str 393 { 394 switch (err) { 395 case -EINPROGRESS: 396 case -EBUSY: 397 wait_for_completion(&wait->com 398 reinit_completion(&wait->compl 399 err = wait->err; 400 break; 401 } 402 403 return err; 404 } 405 406 static inline void crypto_init_wait(struct cry 407 { 408 init_completion(&wait->completion); 409 } 410 411 /* 412 * Algorithm query interface. 413 */ 414 int crypto_has_alg(const char *name, u32 type, 415 416 /* 417 * Transforms: user-instantiated objects which 418 * and core processing logic. Managed via cry 419 * crypto_free_*(), as well as the various hel 420 */ 421 422 struct crypto_tfm { 423 refcount_t refcnt; 424 425 u32 crt_flags; 426 427 int node; 428 429 void (*exit)(struct crypto_tfm *tfm); 430 431 struct crypto_alg *__crt_alg; 432 433 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR 434 }; 435 436 struct crypto_comp { 437 struct crypto_tfm base; 438 }; 439 440 /* 441 * Transform user interface. 442 */ 443 444 struct crypto_tfm *crypto_alloc_base(const cha 445 void crypto_destroy_tfm(void *mem, struct cryp 446 447 static inline void crypto_free_tfm(struct cryp 448 { 449 return crypto_destroy_tfm(tfm, tfm); 450 } 451 452 /* 453 * Transform helpers which query the underlyin 454 */ 455 static inline const char *crypto_tfm_alg_name( 456 { 457 return tfm->__crt_alg->cra_name; 458 } 459 460 static inline const char *crypto_tfm_alg_drive 461 { 462 return tfm->__crt_alg->cra_driver_name 463 } 464 465 static inline unsigned int crypto_tfm_alg_bloc 466 { 467 return tfm->__crt_alg->cra_blocksize; 468 } 469 470 static inline unsigned int crypto_tfm_alg_alig 471 { 472 return tfm->__crt_alg->cra_alignmask; 473 } 474 475 static inline u32 crypto_tfm_get_flags(struct 476 { 477 return tfm->crt_flags; 478 } 479 480 static inline void crypto_tfm_set_flags(struct 481 { 482 tfm->crt_flags |= flags; 483 } 484 485 static inline void crypto_tfm_clear_flags(stru 486 { 487 tfm->crt_flags &= ~flags; 488 } 489 490 static inline unsigned int crypto_tfm_ctx_alig 491 { 492 struct crypto_tfm *tfm; 493 return __alignof__(tfm->__crt_ctx); 494 } 495 496 static inline struct crypto_comp *__crypto_com 497 { 498 return (struct crypto_comp *)tfm; 499 } 500 501 static inline struct crypto_comp *crypto_alloc 502 503 { 504 type &= ~CRYPTO_ALG_TYPE_MASK; 505 type |= CRYPTO_ALG_TYPE_COMPRESS; 506 mask |= CRYPTO_ALG_TYPE_MASK; 507 508 return __crypto_comp_cast(crypto_alloc 509 } 510 511 static inline struct crypto_tfm *crypto_comp_t 512 { 513 return &tfm->base; 514 } 515 516 static inline void crypto_free_comp(struct cry 517 { 518 crypto_free_tfm(crypto_comp_tfm(tfm)); 519 } 520 521 static inline int crypto_has_comp(const char * 522 { 523 type &= ~CRYPTO_ALG_TYPE_MASK; 524 type |= CRYPTO_ALG_TYPE_COMPRESS; 525 mask |= CRYPTO_ALG_TYPE_MASK; 526 527 return crypto_has_alg(alg_name, type, 528 } 529 530 static inline const char *crypto_comp_name(str 531 { 532 return crypto_tfm_alg_name(crypto_comp 533 } 534 535 int crypto_comp_compress(struct crypto_comp *t 536 const u8 *src, unsign 537 u8 *dst, unsigned int 538 539 int crypto_comp_decompress(struct crypto_comp 540 const u8 *src, unsi 541 u8 *dst, unsigned i 542 543 #endif /* _LINUX_CRYPTO_H */ 544 545
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.