Commit ba448407 authored by Eric Biggers's avatar Eric Biggers Committed by Herbert Xu

crypto: adiantum - use crypto_grab_{cipher,shash} and simplify error paths

Make the adiantum template use the new functions crypto_grab_cipher()
and crypto_grab_shash() to initialize its cipher and shash spawns.

This is needed to make all spawns be initialized in a consistent way.

Also simplify the error handling by taking advantage of crypto_drop_*()
now accepting (as a no-op) spawns that haven't been initialized yet, and
by taking advantage of crypto_grab_*() now handling ERR_PTR() names.
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 0764ac28
...@@ -39,8 +39,6 @@ ...@@ -39,8 +39,6 @@
#include <crypto/scatterwalk.h> #include <crypto/scatterwalk.h>
#include <linux/module.h> #include <linux/module.h>
#include "internal.h"
/* /*
* Size of right-hand part of input data, in bytes; also the size of the block * Size of right-hand part of input data, in bytes; also the size of the block
* cipher's block size and the hash function's output. * cipher's block size and the hash function's output.
...@@ -64,7 +62,7 @@ ...@@ -64,7 +62,7 @@
struct adiantum_instance_ctx { struct adiantum_instance_ctx {
struct crypto_skcipher_spawn streamcipher_spawn; struct crypto_skcipher_spawn streamcipher_spawn;
struct crypto_spawn blockcipher_spawn; struct crypto_cipher_spawn blockcipher_spawn;
struct crypto_shash_spawn hash_spawn; struct crypto_shash_spawn hash_spawn;
}; };
...@@ -410,7 +408,7 @@ static int adiantum_init_tfm(struct crypto_skcipher *tfm) ...@@ -410,7 +408,7 @@ static int adiantum_init_tfm(struct crypto_skcipher *tfm)
if (IS_ERR(streamcipher)) if (IS_ERR(streamcipher))
return PTR_ERR(streamcipher); return PTR_ERR(streamcipher);
blockcipher = crypto_spawn_cipher(&ictx->blockcipher_spawn); blockcipher = crypto_spawn_cipher(&ictx->blockcipher_spawn.base);
if (IS_ERR(blockcipher)) { if (IS_ERR(blockcipher)) {
err = PTR_ERR(blockcipher); err = PTR_ERR(blockcipher);
goto err_free_streamcipher; goto err_free_streamcipher;
...@@ -461,7 +459,7 @@ static void adiantum_free_instance(struct skcipher_instance *inst) ...@@ -461,7 +459,7 @@ static void adiantum_free_instance(struct skcipher_instance *inst)
struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst); struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst);
crypto_drop_skcipher(&ictx->streamcipher_spawn); crypto_drop_skcipher(&ictx->streamcipher_spawn);
crypto_drop_spawn(&ictx->blockcipher_spawn); crypto_drop_cipher(&ictx->blockcipher_spawn);
crypto_drop_shash(&ictx->hash_spawn); crypto_drop_shash(&ictx->hash_spawn);
kfree(inst); kfree(inst);
} }
...@@ -494,14 +492,11 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) ...@@ -494,14 +492,11 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
{ {
struct crypto_attr_type *algt; struct crypto_attr_type *algt;
u32 mask; u32 mask;
const char *streamcipher_name;
const char *blockcipher_name;
const char *nhpoly1305_name; const char *nhpoly1305_name;
struct skcipher_instance *inst; struct skcipher_instance *inst;
struct adiantum_instance_ctx *ictx; struct adiantum_instance_ctx *ictx;
struct skcipher_alg *streamcipher_alg; struct skcipher_alg *streamcipher_alg;
struct crypto_alg *blockcipher_alg; struct crypto_alg *blockcipher_alg;
struct crypto_alg *_hash_alg;
struct shash_alg *hash_alg; struct shash_alg *hash_alg;
int err; int err;
...@@ -514,20 +509,6 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) ...@@ -514,20 +509,6 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
mask = crypto_requires_sync(algt->type, algt->mask); mask = crypto_requires_sync(algt->type, algt->mask);
streamcipher_name = crypto_attr_alg_name(tb[1]);
if (IS_ERR(streamcipher_name))
return PTR_ERR(streamcipher_name);
blockcipher_name = crypto_attr_alg_name(tb[2]);
if (IS_ERR(blockcipher_name))
return PTR_ERR(blockcipher_name);
nhpoly1305_name = crypto_attr_alg_name(tb[3]);
if (nhpoly1305_name == ERR_PTR(-ENOENT))
nhpoly1305_name = "nhpoly1305";
if (IS_ERR(nhpoly1305_name))
return PTR_ERR(nhpoly1305_name);
inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL); inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
if (!inst) if (!inst)
return -ENOMEM; return -ENOMEM;
...@@ -536,33 +517,29 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) ...@@ -536,33 +517,29 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
/* Stream cipher, e.g. "xchacha12" */ /* Stream cipher, e.g. "xchacha12" */
err = crypto_grab_skcipher(&ictx->streamcipher_spawn, err = crypto_grab_skcipher(&ictx->streamcipher_spawn,
skcipher_crypto_instance(inst), skcipher_crypto_instance(inst),
streamcipher_name, 0, mask); crypto_attr_alg_name(tb[1]), 0, mask);
if (err) if (err)
goto out_free_inst; goto err_free_inst;
streamcipher_alg = crypto_spawn_skcipher_alg(&ictx->streamcipher_spawn); streamcipher_alg = crypto_spawn_skcipher_alg(&ictx->streamcipher_spawn);
/* Block cipher, e.g. "aes" */ /* Block cipher, e.g. "aes" */
err = crypto_grab_spawn(&ictx->blockcipher_spawn, err = crypto_grab_cipher(&ictx->blockcipher_spawn,
skcipher_crypto_instance(inst), skcipher_crypto_instance(inst),
blockcipher_name, crypto_attr_alg_name(tb[2]), 0, mask);
CRYPTO_ALG_TYPE_CIPHER, CRYPTO_ALG_TYPE_MASK);
if (err) if (err)
goto out_drop_streamcipher; goto err_free_inst;
blockcipher_alg = ictx->blockcipher_spawn.alg; blockcipher_alg = crypto_spawn_cipher_alg(&ictx->blockcipher_spawn);
/* NHPoly1305 ε-∆U hash function */ /* NHPoly1305 ε-∆U hash function */
_hash_alg = crypto_alg_mod_lookup(nhpoly1305_name, nhpoly1305_name = crypto_attr_alg_name(tb[3]);
CRYPTO_ALG_TYPE_SHASH, if (nhpoly1305_name == ERR_PTR(-ENOENT))
CRYPTO_ALG_TYPE_MASK); nhpoly1305_name = "nhpoly1305";
if (IS_ERR(_hash_alg)) { err = crypto_grab_shash(&ictx->hash_spawn,
err = PTR_ERR(_hash_alg); skcipher_crypto_instance(inst),
goto out_drop_blockcipher; nhpoly1305_name, 0, mask);
}
hash_alg = __crypto_shash_alg(_hash_alg);
err = crypto_init_shash_spawn(&ictx->hash_spawn, hash_alg,
skcipher_crypto_instance(inst));
if (err) if (err)
goto out_put_hash; goto err_free_inst;
hash_alg = crypto_spawn_shash_alg(&ictx->hash_spawn);
/* Check the set of algorithms */ /* Check the set of algorithms */
if (!adiantum_supported_algorithms(streamcipher_alg, blockcipher_alg, if (!adiantum_supported_algorithms(streamcipher_alg, blockcipher_alg,
...@@ -571,7 +548,7 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) ...@@ -571,7 +548,7 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
streamcipher_alg->base.cra_name, streamcipher_alg->base.cra_name,
blockcipher_alg->cra_name, hash_alg->base.cra_name); blockcipher_alg->cra_name, hash_alg->base.cra_name);
err = -EINVAL; err = -EINVAL;
goto out_drop_hash; goto err_free_inst;
} }
/* Instance fields */ /* Instance fields */
...@@ -580,13 +557,13 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) ...@@ -580,13 +557,13 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
"adiantum(%s,%s)", streamcipher_alg->base.cra_name, "adiantum(%s,%s)", streamcipher_alg->base.cra_name,
blockcipher_alg->cra_name) >= CRYPTO_MAX_ALG_NAME) blockcipher_alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
goto out_drop_hash; goto err_free_inst;
if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
"adiantum(%s,%s,%s)", "adiantum(%s,%s,%s)",
streamcipher_alg->base.cra_driver_name, streamcipher_alg->base.cra_driver_name,
blockcipher_alg->cra_driver_name, blockcipher_alg->cra_driver_name,
hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
goto out_drop_hash; goto err_free_inst;
inst->alg.base.cra_flags = streamcipher_alg->base.cra_flags & inst->alg.base.cra_flags = streamcipher_alg->base.cra_flags &
CRYPTO_ALG_ASYNC; CRYPTO_ALG_ASYNC;
...@@ -616,22 +593,10 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) ...@@ -616,22 +593,10 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
inst->free = adiantum_free_instance; inst->free = adiantum_free_instance;
err = skcipher_register_instance(tmpl, inst); err = skcipher_register_instance(tmpl, inst);
if (err) if (err) {
goto out_drop_hash; err_free_inst:
adiantum_free_instance(inst);
crypto_mod_put(_hash_alg); }
return 0;
out_drop_hash:
crypto_drop_shash(&ictx->hash_spawn);
out_put_hash:
crypto_mod_put(_hash_alg);
out_drop_blockcipher:
crypto_drop_spawn(&ictx->blockcipher_spawn);
out_drop_streamcipher:
crypto_drop_skcipher(&ictx->streamcipher_spawn);
out_free_inst:
kfree(inst);
return err; return err;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment