| .. | .. |
|---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-or-later */ |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Symmetric key ciphers. |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * 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 | 6 | */ |
|---|
| 12 | 7 | |
|---|
| 13 | 8 | #ifndef _CRYPTO_SKCIPHER_H |
|---|
| .. | .. |
|---|
| 23 | 18 | * @iv: Initialisation Vector |
|---|
| 24 | 19 | * @src: Source SG list |
|---|
| 25 | 20 | * @dst: Destination SG list |
|---|
| 26 | | - * @base: Underlying async request request |
|---|
| 21 | + * @base: Underlying async request |
|---|
| 27 | 22 | * @__ctx: Start of private context data |
|---|
| 28 | 23 | */ |
|---|
| 29 | 24 | struct skcipher_request { |
|---|
| .. | .. |
|---|
| 39 | 34 | void *__ctx[] CRYPTO_MINALIGN_ATTR; |
|---|
| 40 | 35 | }; |
|---|
| 41 | 36 | |
|---|
| 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 | | - */ |
|---|
| 48 | | -struct skcipher_givcrypt_request { |
|---|
| 49 | | - u64 seq; |
|---|
| 50 | | - u8 *giv; |
|---|
| 51 | | - |
|---|
| 52 | | - struct ablkcipher_request creq; |
|---|
| 53 | | -}; |
|---|
| 54 | | - |
|---|
| 55 | 37 | struct crypto_skcipher { |
|---|
| 56 | | - int (*setkey)(struct crypto_skcipher *tfm, const u8 *key, |
|---|
| 57 | | - unsigned int keylen); |
|---|
| 58 | | - int (*encrypt)(struct skcipher_request *req); |
|---|
| 59 | | - int (*decrypt)(struct skcipher_request *req); |
|---|
| 60 | | - |
|---|
| 61 | | - unsigned int ivsize; |
|---|
| 62 | 38 | unsigned int reqsize; |
|---|
| 63 | | - unsigned int keysize; |
|---|
| 64 | 39 | |
|---|
| 65 | 40 | struct crypto_tfm base; |
|---|
| 66 | 41 | }; |
|---|
| .. | .. |
|---|
| 156 | 131 | ] CRYPTO_MINALIGN_ATTR; \ |
|---|
| 157 | 132 | struct skcipher_request *name = (void *)__##name##_desc |
|---|
| 158 | 133 | |
|---|
| 159 | | -#define SKCIPHER_REQUEST_ON_STACK(name, tfm) \ |
|---|
| 160 | | - char __##name##_desc[sizeof(struct skcipher_request) + \ |
|---|
| 161 | | - crypto_skcipher_reqsize(tfm)] CRYPTO_MINALIGN_ATTR; \ |
|---|
| 162 | | - struct skcipher_request *name = (void *)__##name##_desc |
|---|
| 163 | | - |
|---|
| 164 | 134 | /** |
|---|
| 165 | 135 | * DOC: Symmetric Key Cipher API |
|---|
| 166 | 136 | * |
|---|
| .. | .. |
|---|
| 243 | 213 | * crypto_has_skcipher() - Search for the availability of an skcipher. |
|---|
| 244 | 214 | * @alg_name: is the cra_name / name or cra_driver_name / driver name of the |
|---|
| 245 | 215 | * skcipher |
|---|
| 246 | | - * @type: specifies the type of the cipher |
|---|
| 247 | | - * @mask: specifies the mask for the cipher |
|---|
| 248 | | - * |
|---|
| 249 | | - * Return: true when the skcipher is known to the kernel crypto API; false |
|---|
| 250 | | - * otherwise |
|---|
| 251 | | - */ |
|---|
| 252 | | -static inline int crypto_has_skcipher(const char *alg_name, u32 type, |
|---|
| 253 | | - u32 mask) |
|---|
| 254 | | -{ |
|---|
| 255 | | - return crypto_has_alg(alg_name, crypto_skcipher_type(type), |
|---|
| 256 | | - crypto_skcipher_mask(mask)); |
|---|
| 257 | | -} |
|---|
| 258 | | - |
|---|
| 259 | | -/** |
|---|
| 260 | | - * crypto_has_skcipher2() - Search for the availability of an skcipher. |
|---|
| 261 | | - * @alg_name: is the cra_name / name or cra_driver_name / driver name of the |
|---|
| 262 | | - * skcipher |
|---|
| 263 | 216 | * @type: specifies the type of the skcipher |
|---|
| 264 | 217 | * @mask: specifies the mask for the skcipher |
|---|
| 265 | 218 | * |
|---|
| 266 | 219 | * Return: true when the skcipher is known to the kernel crypto API; false |
|---|
| 267 | 220 | * otherwise |
|---|
| 268 | 221 | */ |
|---|
| 269 | | -int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask); |
|---|
| 222 | +int crypto_has_skcipher(const char *alg_name, u32 type, u32 mask); |
|---|
| 270 | 223 | |
|---|
| 271 | 224 | static inline const char *crypto_skcipher_driver_name( |
|---|
| 272 | 225 | struct crypto_skcipher *tfm) |
|---|
| .. | .. |
|---|
| 283 | 236 | |
|---|
| 284 | 237 | static inline unsigned int crypto_skcipher_alg_ivsize(struct skcipher_alg *alg) |
|---|
| 285 | 238 | { |
|---|
| 286 | | - if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) == |
|---|
| 287 | | - CRYPTO_ALG_TYPE_BLKCIPHER) |
|---|
| 288 | | - return alg->base.cra_blkcipher.ivsize; |
|---|
| 289 | | - |
|---|
| 290 | | - if (alg->base.cra_ablkcipher.encrypt) |
|---|
| 291 | | - return alg->base.cra_ablkcipher.ivsize; |
|---|
| 292 | | - |
|---|
| 293 | 239 | return alg->ivsize; |
|---|
| 294 | 240 | } |
|---|
| 295 | 241 | |
|---|
| .. | .. |
|---|
| 304 | 250 | */ |
|---|
| 305 | 251 | static inline unsigned int crypto_skcipher_ivsize(struct crypto_skcipher *tfm) |
|---|
| 306 | 252 | { |
|---|
| 307 | | - return tfm->ivsize; |
|---|
| 253 | + return crypto_skcipher_alg(tfm)->ivsize; |
|---|
| 308 | 254 | } |
|---|
| 309 | 255 | |
|---|
| 310 | 256 | static inline unsigned int crypto_sync_skcipher_ivsize( |
|---|
| .. | .. |
|---|
| 313 | 259 | return crypto_skcipher_ivsize(&tfm->base); |
|---|
| 314 | 260 | } |
|---|
| 315 | 261 | |
|---|
| 262 | +/** |
|---|
| 263 | + * crypto_skcipher_blocksize() - obtain block size of cipher |
|---|
| 264 | + * @tfm: cipher handle |
|---|
| 265 | + * |
|---|
| 266 | + * The block size for the skcipher referenced with the cipher handle is |
|---|
| 267 | + * returned. The caller may use that information to allocate appropriate |
|---|
| 268 | + * memory for the data returned by the encryption or decryption operation |
|---|
| 269 | + * |
|---|
| 270 | + * Return: block size of cipher |
|---|
| 271 | + */ |
|---|
| 272 | +static inline unsigned int crypto_skcipher_blocksize( |
|---|
| 273 | + struct crypto_skcipher *tfm) |
|---|
| 274 | +{ |
|---|
| 275 | + return crypto_tfm_alg_blocksize(crypto_skcipher_tfm(tfm)); |
|---|
| 276 | +} |
|---|
| 277 | + |
|---|
| 316 | 278 | static inline unsigned int crypto_skcipher_alg_chunksize( |
|---|
| 317 | 279 | struct skcipher_alg *alg) |
|---|
| 318 | 280 | { |
|---|
| 319 | | - if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) == |
|---|
| 320 | | - CRYPTO_ALG_TYPE_BLKCIPHER) |
|---|
| 321 | | - return alg->base.cra_blocksize; |
|---|
| 322 | | - |
|---|
| 323 | | - if (alg->base.cra_ablkcipher.encrypt) |
|---|
| 324 | | - return alg->base.cra_blocksize; |
|---|
| 325 | | - |
|---|
| 326 | 281 | return alg->chunksize; |
|---|
| 327 | | -} |
|---|
| 328 | | - |
|---|
| 329 | | -static inline unsigned int crypto_skcipher_alg_walksize( |
|---|
| 330 | | - struct skcipher_alg *alg) |
|---|
| 331 | | -{ |
|---|
| 332 | | - if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) == |
|---|
| 333 | | - CRYPTO_ALG_TYPE_BLKCIPHER) |
|---|
| 334 | | - return alg->base.cra_blocksize; |
|---|
| 335 | | - |
|---|
| 336 | | - if (alg->base.cra_ablkcipher.encrypt) |
|---|
| 337 | | - return alg->base.cra_blocksize; |
|---|
| 338 | | - |
|---|
| 339 | | - return alg->walksize; |
|---|
| 340 | 282 | } |
|---|
| 341 | 283 | |
|---|
| 342 | 284 | /** |
|---|
| .. | .. |
|---|
| 354 | 296 | struct crypto_skcipher *tfm) |
|---|
| 355 | 297 | { |
|---|
| 356 | 298 | return crypto_skcipher_alg_chunksize(crypto_skcipher_alg(tfm)); |
|---|
| 357 | | -} |
|---|
| 358 | | - |
|---|
| 359 | | -/** |
|---|
| 360 | | - * crypto_skcipher_walksize() - obtain walk size |
|---|
| 361 | | - * @tfm: cipher handle |
|---|
| 362 | | - * |
|---|
| 363 | | - * In some cases, algorithms can only perform optimally when operating on |
|---|
| 364 | | - * multiple blocks in parallel. This is reflected by the walksize, which |
|---|
| 365 | | - * must be a multiple of the chunksize (or equal if the concern does not |
|---|
| 366 | | - * apply) |
|---|
| 367 | | - * |
|---|
| 368 | | - * Return: walk size in bytes |
|---|
| 369 | | - */ |
|---|
| 370 | | -static inline unsigned int crypto_skcipher_walksize( |
|---|
| 371 | | - struct crypto_skcipher *tfm) |
|---|
| 372 | | -{ |
|---|
| 373 | | - return crypto_skcipher_alg_walksize(crypto_skcipher_alg(tfm)); |
|---|
| 374 | | -} |
|---|
| 375 | | - |
|---|
| 376 | | -/** |
|---|
| 377 | | - * crypto_skcipher_blocksize() - obtain block size of cipher |
|---|
| 378 | | - * @tfm: cipher handle |
|---|
| 379 | | - * |
|---|
| 380 | | - * The block size for the skcipher referenced with the cipher handle is |
|---|
| 381 | | - * returned. The caller may use that information to allocate appropriate |
|---|
| 382 | | - * memory for the data returned by the encryption or decryption operation |
|---|
| 383 | | - * |
|---|
| 384 | | - * Return: block size of cipher |
|---|
| 385 | | - */ |
|---|
| 386 | | -static inline unsigned int crypto_skcipher_blocksize( |
|---|
| 387 | | - struct crypto_skcipher *tfm) |
|---|
| 388 | | -{ |
|---|
| 389 | | - return crypto_tfm_alg_blocksize(crypto_skcipher_tfm(tfm)); |
|---|
| 390 | 299 | } |
|---|
| 391 | 300 | |
|---|
| 392 | 301 | static inline unsigned int crypto_sync_skcipher_blocksize( |
|---|
| .. | .. |
|---|
| 452 | 361 | * |
|---|
| 453 | 362 | * Return: 0 if the setting of the key was successful; < 0 if an error occurred |
|---|
| 454 | 363 | */ |
|---|
| 455 | | -static inline int crypto_skcipher_setkey(struct crypto_skcipher *tfm, |
|---|
| 456 | | - const u8 *key, unsigned int keylen) |
|---|
| 457 | | -{ |
|---|
| 458 | | - return tfm->setkey(tfm, key, keylen); |
|---|
| 459 | | -} |
|---|
| 364 | +int crypto_skcipher_setkey(struct crypto_skcipher *tfm, |
|---|
| 365 | + const u8 *key, unsigned int keylen); |
|---|
| 460 | 366 | |
|---|
| 461 | 367 | static inline int crypto_sync_skcipher_setkey(struct crypto_sync_skcipher *tfm, |
|---|
| 462 | 368 | const u8 *key, unsigned int keylen) |
|---|
| .. | .. |
|---|
| 464 | 370 | return crypto_skcipher_setkey(&tfm->base, key, keylen); |
|---|
| 465 | 371 | } |
|---|
| 466 | 372 | |
|---|
| 467 | | -static inline unsigned int crypto_skcipher_default_keysize( |
|---|
| 373 | +static inline unsigned int crypto_skcipher_min_keysize( |
|---|
| 468 | 374 | struct crypto_skcipher *tfm) |
|---|
| 469 | 375 | { |
|---|
| 470 | | - return tfm->keysize; |
|---|
| 376 | + return crypto_skcipher_alg(tfm)->min_keysize; |
|---|
| 377 | +} |
|---|
| 378 | + |
|---|
| 379 | +static inline unsigned int crypto_skcipher_max_keysize( |
|---|
| 380 | + struct crypto_skcipher *tfm) |
|---|
| 381 | +{ |
|---|
| 382 | + return crypto_skcipher_alg(tfm)->max_keysize; |
|---|
| 471 | 383 | } |
|---|
| 472 | 384 | |
|---|
| 473 | 385 | /** |
|---|
| .. | .. |
|---|
| 504 | 416 | * |
|---|
| 505 | 417 | * Return: 0 if the cipher operation was successful; < 0 if an error occurred |
|---|
| 506 | 418 | */ |
|---|
| 507 | | -static inline int crypto_skcipher_encrypt(struct skcipher_request *req) |
|---|
| 508 | | -{ |
|---|
| 509 | | - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
|---|
| 510 | | - |
|---|
| 511 | | - if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) |
|---|
| 512 | | - return -ENOKEY; |
|---|
| 513 | | - |
|---|
| 514 | | - return tfm->encrypt(req); |
|---|
| 515 | | -} |
|---|
| 419 | +int crypto_skcipher_encrypt(struct skcipher_request *req); |
|---|
| 516 | 420 | |
|---|
| 517 | 421 | /** |
|---|
| 518 | 422 | * crypto_skcipher_decrypt() - decrypt ciphertext |
|---|
| .. | .. |
|---|
| 525 | 429 | * |
|---|
| 526 | 430 | * Return: 0 if the cipher operation was successful; < 0 if an error occurred |
|---|
| 527 | 431 | */ |
|---|
| 528 | | -static inline int crypto_skcipher_decrypt(struct skcipher_request *req) |
|---|
| 529 | | -{ |
|---|
| 530 | | - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
|---|
| 531 | | - |
|---|
| 532 | | - if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) |
|---|
| 533 | | - return -ENOKEY; |
|---|
| 534 | | - |
|---|
| 535 | | - return tfm->decrypt(req); |
|---|
| 536 | | -} |
|---|
| 432 | +int crypto_skcipher_decrypt(struct skcipher_request *req); |
|---|
| 537 | 433 | |
|---|
| 538 | 434 | /** |
|---|
| 539 | 435 | * DOC: Symmetric Key Cipher Request Handle |
|---|
| .. | .. |
|---|
| 614 | 510 | */ |
|---|
| 615 | 511 | static inline void skcipher_request_free(struct skcipher_request *req) |
|---|
| 616 | 512 | { |
|---|
| 617 | | - kzfree(req); |
|---|
| 513 | + kfree_sensitive(req); |
|---|
| 618 | 514 | } |
|---|
| 619 | 515 | |
|---|
| 620 | 516 | static inline void skcipher_request_zero(struct skcipher_request *req) |
|---|