Commit 3f683d6175748ef9daf4698d9ef5a488dd037063
1 parent
ff753308d2
crypto: api - Fix crypto_alloc_tfm/create_create_tfm return convention
This is based on a report and patch by Geert Uytterhoeven. The functions crypto_alloc_tfm and create_create_tfm return a pointer that needs to be adjusted by the caller when successful and otherwise an error value. This means that the caller has to check for the error and only perform the adjustment if the pointer returned is valid. Since all callers want to make the adjustment and we know how to adjust it ourselves, it's much easier to just return adjusted pointer directly. The only caveat is that we have to return a void * instead of struct crypto_tfm *. However, this isn't that bad because both of these functions are for internal use only (by types code like shash.c, not even algorithms code). This patch also moves crypto_alloc_tfm into crypto/internal.h (crypto_create_tfm is already there) to reflect this. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Showing 4 changed files with 16 additions and 26 deletions Side-by-side Diff
crypto/api.c
... | ... | @@ -453,8 +453,8 @@ |
453 | 453 | } |
454 | 454 | EXPORT_SYMBOL_GPL(crypto_alloc_base); |
455 | 455 | |
456 | -struct crypto_tfm *crypto_create_tfm(struct crypto_alg *alg, | |
457 | - const struct crypto_type *frontend) | |
456 | +void *crypto_create_tfm(struct crypto_alg *alg, | |
457 | + const struct crypto_type *frontend) | |
458 | 458 | { |
459 | 459 | char *mem; |
460 | 460 | struct crypto_tfm *tfm = NULL; |
461 | 461 | |
... | ... | @@ -488,9 +488,9 @@ |
488 | 488 | crypto_shoot_alg(alg); |
489 | 489 | kfree(mem); |
490 | 490 | out_err: |
491 | - tfm = ERR_PTR(err); | |
491 | + mem = ERR_PTR(err); | |
492 | 492 | out: |
493 | - return tfm; | |
493 | + return mem; | |
494 | 494 | } |
495 | 495 | EXPORT_SYMBOL_GPL(crypto_create_tfm); |
496 | 496 | |
497 | 497 | |
... | ... | @@ -514,12 +514,11 @@ |
514 | 514 | * |
515 | 515 | * In case of error the return value is an error pointer. |
516 | 516 | */ |
517 | -struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, | |
518 | - const struct crypto_type *frontend, | |
519 | - u32 type, u32 mask) | |
517 | +void *crypto_alloc_tfm(const char *alg_name, | |
518 | + const struct crypto_type *frontend, u32 type, u32 mask) | |
520 | 519 | { |
521 | 520 | struct crypto_alg *(*lookup)(const char *name, u32 type, u32 mask); |
522 | - struct crypto_tfm *tfm; | |
521 | + void *tfm; | |
523 | 522 | int err; |
524 | 523 | |
525 | 524 | type &= frontend->maskclear; |
crypto/internal.h
... | ... | @@ -109,8 +109,10 @@ |
109 | 109 | void crypto_shoot_alg(struct crypto_alg *alg); |
110 | 110 | struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type, |
111 | 111 | u32 mask); |
112 | -struct crypto_tfm *crypto_create_tfm(struct crypto_alg *alg, | |
113 | - const struct crypto_type *frontend); | |
112 | +void *crypto_create_tfm(struct crypto_alg *alg, | |
113 | + const struct crypto_type *frontend); | |
114 | +void *crypto_alloc_tfm(const char *alg_name, | |
115 | + const struct crypto_type *frontend, u32 type, u32 mask); | |
114 | 116 | |
115 | 117 | int crypto_register_instance(struct crypto_template *tmpl, |
116 | 118 | struct crypto_instance *inst); |
crypto/shash.c
... | ... | @@ -18,15 +18,10 @@ |
18 | 18 | #include <linux/slab.h> |
19 | 19 | #include <linux/seq_file.h> |
20 | 20 | |
21 | -static const struct crypto_type crypto_shash_type; | |
22 | - | |
23 | -static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm) | |
24 | -{ | |
25 | - return container_of(tfm, struct crypto_shash, base); | |
26 | -} | |
27 | - | |
28 | 21 | #include "internal.h" |
29 | 22 | |
23 | +static const struct crypto_type crypto_shash_type; | |
24 | + | |
30 | 25 | static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key, |
31 | 26 | unsigned int keylen) |
32 | 27 | { |
... | ... | @@ -282,8 +277,7 @@ |
282 | 277 | if (!crypto_mod_get(calg)) |
283 | 278 | return -EAGAIN; |
284 | 279 | |
285 | - shash = __crypto_shash_cast(crypto_create_tfm( | |
286 | - calg, &crypto_shash_type)); | |
280 | + shash = crypto_create_tfm(calg, &crypto_shash_type); | |
287 | 281 | if (IS_ERR(shash)) { |
288 | 282 | crypto_mod_put(calg); |
289 | 283 | return PTR_ERR(shash); |
... | ... | @@ -391,8 +385,7 @@ |
391 | 385 | if (!crypto_mod_get(calg)) |
392 | 386 | return -EAGAIN; |
393 | 387 | |
394 | - shash = __crypto_shash_cast(crypto_create_tfm( | |
395 | - calg, &crypto_shash_type)); | |
388 | + shash = crypto_create_tfm(calg, &crypto_shash_type); | |
396 | 389 | if (IS_ERR(shash)) { |
397 | 390 | crypto_mod_put(calg); |
398 | 391 | return PTR_ERR(shash); |
... | ... | @@ -480,8 +473,7 @@ |
480 | 473 | struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, |
481 | 474 | u32 mask) |
482 | 475 | { |
483 | - return __crypto_shash_cast( | |
484 | - crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask)); | |
476 | + return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask); | |
485 | 477 | } |
486 | 478 | EXPORT_SYMBOL_GPL(crypto_alloc_shash); |
487 | 479 |
include/linux/crypto.h
... | ... | @@ -548,9 +548,6 @@ |
548 | 548 | * Transform user interface. |
549 | 549 | */ |
550 | 550 | |
551 | -struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, | |
552 | - const struct crypto_type *frontend, | |
553 | - u32 type, u32 mask); | |
554 | 551 | struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask); |
555 | 552 | void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm); |
556 | 553 |