1/* 2 * Symmetric key ciphers. 3 * 4 * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au> 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License as published by the Free 8 * Software Foundation; either version 2 of the License, or (at your option) 9 * any later version. 10 * 11 */ 12 13#ifndef _CRYPTO_SKCIPHER_H 14#define _CRYPTO_SKCIPHER_H 15 16#include <linux/crypto.h> 17#include <linux/kernel.h> 18#include <linux/slab.h> 19 20/** 21 * struct skcipher_request - Symmetric key cipher request 22 * @cryptlen: Number of bytes to encrypt or decrypt 23 * @iv: Initialisation Vector 24 * @src: Source SG list 25 * @dst: Destination SG list 26 * @base: Underlying async request request 27 * @__ctx: Start of private context data 28 */ 29struct skcipher_request { 30 unsigned int cryptlen; 31 32 u8 *iv; 33 34 struct scatterlist *src; 35 struct scatterlist *dst; 36 37 struct crypto_async_request base; 38 39 void *__ctx[] CRYPTO_MINALIGN_ATTR; 40}; 41 42/** 43 * struct skcipher_givcrypt_request - Crypto request with IV generation 44 * @seq: Sequence number for IV generation 45 * @giv: Space for generated IV 46 * @creq: The crypto request itself 47 */ 48struct skcipher_givcrypt_request { 49 u64 seq; 50 u8 *giv; 51 52 struct ablkcipher_request creq; 53}; 54 55struct crypto_skcipher { 56 int (*setkey)(struct crypto_skcipher *tfm, const u8 *key, 57 unsigned int keylen); 58 int (*setkeytype)(struct crypto_skcipher *tfm, const u8 *key, 59 unsigned int keylen); 60 int (*encrypt)(struct skcipher_request *req); 61 int (*decrypt)(struct skcipher_request *req); 62 63 unsigned int ivsize; 64 unsigned int reqsize; 65 unsigned int keysize; 66 67 struct crypto_tfm base; 68}; 69 70/** 71 * struct skcipher_alg - symmetric key cipher definition 72 * @min_keysize: Minimum key size supported by the transformation. This is the 73 * smallest key length supported by this transformation algorithm. 74 * This must be set to one of the pre-defined values as this is 75 * not hardware specific. Possible values for this field can be 76 * found via git grep "_MIN_KEY_SIZE" include/crypto/ 77 * @max_keysize: Maximum key size supported by the transformation. This is the 78 * largest key length supported by this transformation algorithm. 79 * This must be set to one of the pre-defined values as this is 80 * not hardware specific. Possible values for this field can be 81 * found via git grep "_MAX_KEY_SIZE" include/crypto/ 82 * @setkey: Set key for the transformation. This function is used to either 83 * program a supplied key into the hardware or store the key in the 84 * transformation context for programming it later. Note that this 85 * function does modify the transformation context. This function can 86 * be called multiple times during the existence of the transformation 87 * object, so one must make sure the key is properly reprogrammed into 88 * the hardware. This function is also responsible for checking the key 89 * length for validity. In case a software fallback was put in place in 90 * the @cra_init call, this function might need to use the fallback if 91 * the algorithm doesn't support all of the key sizes. 92 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt 93 * the supplied scatterlist containing the blocks of data. The crypto 94 * API consumer is responsible for aligning the entries of the 95 * scatterlist properly and making sure the chunks are correctly 96 * sized. In case a software fallback was put in place in the 97 * @cra_init call, this function might need to use the fallback if 98 * the algorithm doesn't support all of the key sizes. In case the 99 * key was stored in transformation context, the key might need to be 100 * re-programmed into the hardware in this function. This function 101 * shall not modify the transformation context, as this function may 102 * be called in parallel with the same transformation object. 103 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt 104 * and the conditions are exactly the same. 105 * @init: Initialize the cryptographic transformation object. This function 106 * is used to initialize the cryptographic transformation object. 107 * This function is called only once at the instantiation time, right 108 * after the transformation context was allocated. In case the 109 * cryptographic hardware has some special requirements which need to 110 * be handled by software, this function shall check for the precise 111 * requirement of the transformation and put any software fallbacks 112 * in place. 113 * @exit: Deinitialize the cryptographic transformation object. This is a 114 * counterpart to @init, used to remove various changes set in 115 * @init. 116 * @ivsize: IV size applicable for transformation. The consumer must provide an 117 * IV of exactly that size to perform the encrypt or decrypt operation. 118 * @chunksize: Equal to the block size except for stream ciphers such as 119 * CTR where it is set to the underlying block size. 120 * @walksize: Equal to the chunk size except in cases where the algorithm is 121 * considerably more efficient if it can operate on multiple chunks 122 * in parallel. Should be a multiple of chunksize. 123 * @base: Definition of a generic crypto algorithm. 124 * 125 * All fields except @ivsize are mandatory and must be filled. 126 */ 127struct skcipher_alg { 128 int (*setkey)(struct crypto_skcipher *tfm, const u8 *key, 129 unsigned int keylen); 130 int (*setkeytype)(struct crypto_skcipher *tfm, const u8 *key, 131 unsigned int keylen); 132 int (*encrypt)(struct skcipher_request *req); 133 int (*decrypt)(struct skcipher_request *req); 134 int (*init)(struct crypto_skcipher *tfm); 135 void (*exit)(struct crypto_skcipher *tfm); 136 137 unsigned int min_keysize; 138 unsigned int max_keysize; 139 unsigned int ivsize; 140 unsigned int chunksize; 141 unsigned int walksize; 142 143 struct crypto_alg base; 144}; 145 146#define SKCIPHER_REQUEST_ON_STACK(name, tfm) \ 147 char __##name##_desc[sizeof(struct skcipher_request) + \ 148 crypto_skcipher_reqsize(tfm)] CRYPTO_MINALIGN_ATTR; \ 149 struct skcipher_request *name = (void *)__##name##_desc 150 151/** 152 * DOC: Symmetric Key Cipher API 153 * 154 * Symmetric key cipher API is used with the ciphers of type 155 * CRYPTO_ALG_TYPE_SKCIPHER (listed as type "skcipher" in /proc/crypto). 156 * 157 * Asynchronous cipher operations imply that the function invocation for a 158 * cipher request returns immediately before the completion of the operation. 159 * The cipher request is scheduled as a separate kernel thread and therefore 160 * load-balanced on the different CPUs via the process scheduler. To allow 161 * the kernel crypto API to inform the caller about the completion of a cipher 162 * request, the caller must provide a callback function. That function is 163 * invoked with the cipher handle when the request completes. 164 * 165 * To support the asynchronous operation, additional information than just the 166 * cipher handle must be supplied to the kernel crypto API. That additional 167 * information is given by filling in the skcipher_request data structure. 168 * 169 * For the symmetric key cipher API, the state is maintained with the tfm 170 * cipher handle. A single tfm can be used across multiple calls and in 171 * parallel. For asynchronous block cipher calls, context data supplied and 172 * only used by the caller can be referenced the request data structure in 173 * addition to the IV used for the cipher request. The maintenance of such 174 * state information would be important for a crypto driver implementer to 175 * have, because when calling the callback function upon completion of the 176 * cipher operation, that callback function may need some information about 177 * which operation just finished if it invoked multiple in parallel. This 178 * state information is unused by the kernel crypto API. 179 */ 180 181static inline struct crypto_skcipher *__crypto_skcipher_cast( 182 struct crypto_tfm *tfm) 183{ 184 return container_of(tfm, struct crypto_skcipher, base); 185} 186 187/** 188 * crypto_alloc_skcipher() - allocate symmetric key cipher handle 189 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 190 * skcipher cipher 191 * @type: specifies the type of the cipher 192 * @mask: specifies the mask for the cipher 193 * 194 * Allocate a cipher handle for an skcipher. The returned struct 195 * crypto_skcipher is the cipher handle that is required for any subsequent 196 * API invocation for that skcipher. 197 * 198 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 199 * of an error, PTR_ERR() returns the error code. 200 */ 201struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name, 202 u32 type, u32 mask); 203 204static inline struct crypto_tfm *crypto_skcipher_tfm( 205 struct crypto_skcipher *tfm) 206{ 207 return &tfm->base; 208} 209 210/** 211 * crypto_free_skcipher() - zeroize and free cipher handle 212 * @tfm: cipher handle to be freed 213 */ 214static inline void crypto_free_skcipher(struct crypto_skcipher *tfm) 215{ 216 crypto_destroy_tfm(tfm, crypto_skcipher_tfm(tfm)); 217} 218 219/** 220 * crypto_has_skcipher() - Search for the availability of an skcipher. 221 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 222 * skcipher 223 * @type: specifies the type of the cipher 224 * @mask: specifies the mask for the cipher 225 * 226 * Return: true when the skcipher is known to the kernel crypto API; false 227 * otherwise 228 */ 229static inline int crypto_has_skcipher(const char *alg_name, u32 type, 230 u32 mask) 231{ 232 return crypto_has_alg(alg_name, crypto_skcipher_type(type), 233 crypto_skcipher_mask(mask)); 234} 235 236/** 237 * crypto_has_skcipher2() - Search for the availability of an skcipher. 238 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 239 * skcipher 240 * @type: specifies the type of the skcipher 241 * @mask: specifies the mask for the skcipher 242 * 243 * Return: true when the skcipher is known to the kernel crypto API; false 244 * otherwise 245 */ 246int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask); 247 248static inline const char *crypto_skcipher_driver_name( 249 struct crypto_skcipher *tfm) 250{ 251 return crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)); 252} 253 254static inline struct skcipher_alg *crypto_skcipher_alg( 255 struct crypto_skcipher *tfm) 256{ 257 return container_of(crypto_skcipher_tfm(tfm)->__crt_alg, 258 struct skcipher_alg, base); 259} 260 261static inline unsigned int crypto_skcipher_alg_ivsize(struct skcipher_alg *alg) 262{ 263 if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) == 264 CRYPTO_ALG_TYPE_BLKCIPHER) 265 return alg->base.cra_blkcipher.ivsize; 266 267 if (alg->base.cra_ablkcipher.encrypt) 268 return alg->base.cra_ablkcipher.ivsize; 269 270 return alg->ivsize; 271} 272 273/** 274 * crypto_skcipher_ivsize() - obtain IV size 275 * @tfm: cipher handle 276 * 277 * The size of the IV for the skcipher referenced by the cipher handle is 278 * returned. This IV size may be zero if the cipher does not need an IV. 279 * 280 * Return: IV size in bytes 281 */ 282static inline unsigned int crypto_skcipher_ivsize(struct crypto_skcipher *tfm) 283{ 284 return tfm->ivsize; 285} 286 287static inline unsigned int crypto_skcipher_alg_chunksize( 288 struct skcipher_alg *alg) 289{ 290 if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) == 291 CRYPTO_ALG_TYPE_BLKCIPHER) 292 return alg->base.cra_blocksize; 293 294 if (alg->base.cra_ablkcipher.encrypt) 295 return alg->base.cra_blocksize; 296 297 return alg->chunksize; 298} 299 300static inline unsigned int crypto_skcipher_alg_walksize( 301 struct skcipher_alg *alg) 302{ 303 if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) == 304 CRYPTO_ALG_TYPE_BLKCIPHER) 305 return alg->base.cra_blocksize; 306 307 if (alg->base.cra_ablkcipher.encrypt) 308 return alg->base.cra_blocksize; 309 310 return alg->walksize; 311} 312 313/** 314 * crypto_skcipher_chunksize() - obtain chunk size 315 * @tfm: cipher handle 316 * 317 * The block size is set to one for ciphers such as CTR. However, 318 * you still need to provide incremental updates in multiples of 319 * the underlying block size as the IV does not have sub-block 320 * granularity. This is known in this API as the chunk size. 321 * 322 * Return: chunk size in bytes 323 */ 324static inline unsigned int crypto_skcipher_chunksize( 325 struct crypto_skcipher *tfm) 326{ 327 return crypto_skcipher_alg_chunksize(crypto_skcipher_alg(tfm)); 328} 329 330/** 331 * crypto_skcipher_walksize() - obtain walk size 332 * @tfm: cipher handle 333 * 334 * In some cases, algorithms can only perform optimally when operating on 335 * multiple blocks in parallel. This is reflected by the walksize, which 336 * must be a multiple of the chunksize (or equal if the concern does not 337 * apply) 338 * 339 * Return: walk size in bytes 340 */ 341static inline unsigned int crypto_skcipher_walksize( 342 struct crypto_skcipher *tfm) 343{ 344 return crypto_skcipher_alg_walksize(crypto_skcipher_alg(tfm)); 345} 346 347/** 348 * crypto_skcipher_blocksize() - obtain block size of cipher 349 * @tfm: cipher handle 350 * 351 * The block size for the skcipher referenced with the cipher handle is 352 * returned. The caller may use that information to allocate appropriate 353 * memory for the data returned by the encryption or decryption operation 354 * 355 * Return: block size of cipher 356 */ 357static inline unsigned int crypto_skcipher_blocksize( 358 struct crypto_skcipher *tfm) 359{ 360 return crypto_tfm_alg_blocksize(crypto_skcipher_tfm(tfm)); 361} 362 363static inline unsigned int crypto_skcipher_alignmask( 364 struct crypto_skcipher *tfm) 365{ 366 return crypto_tfm_alg_alignmask(crypto_skcipher_tfm(tfm)); 367} 368 369static inline u32 crypto_skcipher_get_flags(struct crypto_skcipher *tfm) 370{ 371 return crypto_tfm_get_flags(crypto_skcipher_tfm(tfm)); 372} 373 374static inline void crypto_skcipher_set_flags(struct crypto_skcipher *tfm, 375 u32 flags) 376{ 377 crypto_tfm_set_flags(crypto_skcipher_tfm(tfm), flags); 378} 379 380static inline void crypto_skcipher_clear_flags(struct crypto_skcipher *tfm, 381 u32 flags) 382{ 383 crypto_tfm_clear_flags(crypto_skcipher_tfm(tfm), flags); 384} 385 386/** 387 * crypto_skcipher_setkey() - set key for cipher 388 * @tfm: cipher handle 389 * @key: buffer holding the key 390 * @keylen: length of the key in bytes 391 * 392 * The caller provided key is set for the skcipher referenced by the cipher 393 * handle. 394 * 395 * Note, the key length determines the cipher type. Many block ciphers implement 396 * different cipher modes depending on the key size, such as AES-128 vs AES-192 397 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 398 * is performed. 399 * 400 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 401 */ 402static inline int crypto_skcipher_setkey(struct crypto_skcipher *tfm, 403 const u8 *key, unsigned int keylen) 404{ 405 return tfm->setkey(tfm, key, keylen); 406} 407 408static inline int crypto_skcipher_setkeytype(struct crypto_skcipher *tfm, 409 const u8 *key, unsigned int keylen) 410{ 411 return tfm->setkeytype(tfm, key, keylen); 412} 413 414static inline bool crypto_skcipher_has_setkey(struct crypto_skcipher *tfm) 415{ 416 return tfm->keysize; 417} 418 419static inline unsigned int crypto_skcipher_default_keysize( 420 struct crypto_skcipher *tfm) 421{ 422 return tfm->keysize; 423} 424 425/** 426 * crypto_skcipher_reqtfm() - obtain cipher handle from request 427 * @req: skcipher_request out of which the cipher handle is to be obtained 428 * 429 * Return the crypto_skcipher handle when furnishing an skcipher_request 430 * data structure. 431 * 432 * Return: crypto_skcipher handle 433 */ 434static inline struct crypto_skcipher *crypto_skcipher_reqtfm( 435 struct skcipher_request *req) 436{ 437 return __crypto_skcipher_cast(req->base.tfm); 438} 439 440/** 441 * crypto_skcipher_encrypt() - encrypt plaintext 442 * @req: reference to the skcipher_request handle that holds all information 443 * needed to perform the cipher operation 444 * 445 * Encrypt plaintext data using the skcipher_request handle. That data 446 * structure and how it is filled with data is discussed with the 447 * skcipher_request_* functions. 448 * 449 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 450 */ 451static inline int crypto_skcipher_encrypt(struct skcipher_request *req) 452{ 453 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 454 455 return tfm->encrypt(req); 456} 457 458/** 459 * crypto_skcipher_decrypt() - decrypt ciphertext 460 * @req: reference to the skcipher_request handle that holds all information 461 * needed to perform the cipher operation 462 * 463 * Decrypt ciphertext data using the skcipher_request handle. That data 464 * structure and how it is filled with data is discussed with the 465 * skcipher_request_* functions. 466 * 467 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 468 */ 469static inline int crypto_skcipher_decrypt(struct skcipher_request *req) 470{ 471 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 472 473 return tfm->decrypt(req); 474} 475 476/** 477 * DOC: Symmetric Key Cipher Request Handle 478 * 479 * The skcipher_request data structure contains all pointers to data 480 * required for the symmetric key cipher operation. This includes the cipher 481 * handle (which can be used by multiple skcipher_request instances), pointer 482 * to plaintext and ciphertext, asynchronous callback function, etc. It acts 483 * as a handle to the skcipher_request_* API calls in a similar way as 484 * skcipher handle to the crypto_skcipher_* API calls. 485 */ 486 487/** 488 * crypto_skcipher_reqsize() - obtain size of the request data structure 489 * @tfm: cipher handle 490 * 491 * Return: number of bytes 492 */ 493static inline unsigned int crypto_skcipher_reqsize(struct crypto_skcipher *tfm) 494{ 495 return tfm->reqsize; 496} 497 498/** 499 * skcipher_request_set_tfm() - update cipher handle reference in request 500 * @req: request handle to be modified 501 * @tfm: cipher handle that shall be added to the request handle 502 * 503 * Allow the caller to replace the existing skcipher handle in the request 504 * data structure with a different one. 505 */ 506static inline void skcipher_request_set_tfm(struct skcipher_request *req, 507 struct crypto_skcipher *tfm) 508{ 509 req->base.tfm = crypto_skcipher_tfm(tfm); 510} 511 512static inline struct skcipher_request *skcipher_request_cast( 513 struct crypto_async_request *req) 514{ 515 return container_of(req, struct skcipher_request, base); 516} 517 518/** 519 * skcipher_request_alloc() - allocate request data structure 520 * @tfm: cipher handle to be registered with the request 521 * @gfp: memory allocation flag that is handed to kmalloc by the API call. 522 * 523 * Allocate the request data structure that must be used with the skcipher 524 * encrypt and decrypt API calls. During the allocation, the provided skcipher 525 * handle is registered in the request data structure. 526 * 527 * Return: allocated request handle in case of success, or NULL if out of memory 528 */ 529static inline struct skcipher_request *skcipher_request_alloc( 530 struct crypto_skcipher *tfm, gfp_t gfp) 531{ 532 struct skcipher_request *req; 533 534 req = kmalloc(sizeof(struct skcipher_request) + 535 crypto_skcipher_reqsize(tfm), gfp); 536 537 if (likely(req)) 538 skcipher_request_set_tfm(req, tfm); 539 540 return req; 541} 542 543/** 544 * skcipher_request_free() - zeroize and free request data structure 545 * @req: request data structure cipher handle to be freed 546 */ 547static inline void skcipher_request_free(struct skcipher_request *req) 548{ 549 kzfree(req); 550} 551 552static inline void skcipher_request_zero(struct skcipher_request *req) 553{ 554 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 555 556 memzero_explicit(req, sizeof(*req) + crypto_skcipher_reqsize(tfm)); 557} 558 559/** 560 * skcipher_request_set_callback() - set asynchronous callback function 561 * @req: request handle 562 * @flags: specify zero or an ORing of the flags 563 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and 564 * increase the wait queue beyond the initial maximum size; 565 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep 566 * @compl: callback function pointer to be registered with the request handle 567 * @data: The data pointer refers to memory that is not used by the kernel 568 * crypto API, but provided to the callback function for it to use. Here, 569 * the caller can provide a reference to memory the callback function can 570 * operate on. As the callback function is invoked asynchronously to the 571 * related functionality, it may need to access data structures of the 572 * related functionality which can be referenced using this pointer. The 573 * callback function can access the memory via the "data" field in the 574 * crypto_async_request data structure provided to the callback function. 575 * 576 * This function allows setting the callback function that is triggered once the 577 * cipher operation completes. 578 * 579 * The callback function is registered with the skcipher_request handle and 580 * must comply with the following template:: 581 * 582 * void callback_function(struct crypto_async_request *req, int error) 583 */ 584static inline void skcipher_request_set_callback(struct skcipher_request *req, 585 u32 flags, 586 crypto_completion_t compl, 587 void *data) 588{ 589 req->base.complete = compl; 590 req->base.data = data; 591 req->base.flags = flags; 592} 593 594/** 595 * skcipher_request_set_crypt() - set data buffers 596 * @req: request handle 597 * @src: source scatter / gather list 598 * @dst: destination scatter / gather list 599 * @cryptlen: number of bytes to process from @src 600 * @iv: IV for the cipher operation which must comply with the IV size defined 601 * by crypto_skcipher_ivsize 602 * 603 * This function allows setting of the source data and destination data 604 * scatter / gather lists. 605 * 606 * For encryption, the source is treated as the plaintext and the 607 * destination is the ciphertext. For a decryption operation, the use is 608 * reversed - the source is the ciphertext and the destination is the plaintext. 609 */ 610static inline void skcipher_request_set_crypt( 611 struct skcipher_request *req, 612 struct scatterlist *src, struct scatterlist *dst, 613 unsigned int cryptlen, void *iv) 614{ 615 req->src = src; 616 req->dst = dst; 617 req->cryptlen = cryptlen; 618 req->iv = iv; 619} 620 621#endif /* _CRYPTO_SKCIPHER_H */ 622 623