Commit ebc610e5 authored by Herbert Xu's avatar Herbert Xu

[CRYPTO] templates: Pass type/mask when creating instances

This patch passes the type/mask along when constructing instances of
templates.  This is in preparation for templates that may support
multiple types of instances depending on what is requested.  For example,
the planned software async crypto driver will use this construct.

For the moment this allows us to check whether the instance constructed
is of the correct type and avoid returning success if the type does not
match.
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 6158efc0
...@@ -425,15 +425,45 @@ int crypto_unregister_notifier(struct notifier_block *nb) ...@@ -425,15 +425,45 @@ int crypto_unregister_notifier(struct notifier_block *nb)
} }
EXPORT_SYMBOL_GPL(crypto_unregister_notifier); EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
struct crypto_alg *crypto_get_attr_alg(void *param, unsigned int len, struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
u32 type, u32 mask)
{ {
struct rtattr *rta = param; struct rtattr *rta = tb[CRYPTOA_TYPE - 1];
struct crypto_attr_type *algt;
if (!rta)
return ERR_PTR(-ENOENT);
if (RTA_PAYLOAD(rta) < sizeof(*algt))
return ERR_PTR(-EINVAL);
algt = RTA_DATA(rta);
return algt;
}
EXPORT_SYMBOL_GPL(crypto_get_attr_type);
int crypto_check_attr_type(struct rtattr **tb, u32 type)
{
struct crypto_attr_type *algt;
algt = crypto_get_attr_type(tb);
if (IS_ERR(algt))
return PTR_ERR(algt);
if ((algt->type ^ type) & algt->mask)
return -EINVAL;
return 0;
}
EXPORT_SYMBOL_GPL(crypto_check_attr_type);
struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb, u32 type, u32 mask)
{
struct rtattr *rta = tb[CRYPTOA_ALG - 1];
struct crypto_attr_alg *alga; struct crypto_attr_alg *alga;
if (!RTA_OK(rta, len)) if (!rta)
return ERR_PTR(-EBADR); return ERR_PTR(-ENOENT);
if (rta->rta_type != CRYPTOA_ALG || RTA_PAYLOAD(rta) < sizeof(*alga)) if (RTA_PAYLOAD(rta) < sizeof(*alga))
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
alga = RTA_DATA(rta); alga = RTA_DATA(rta);
......
...@@ -275,13 +275,18 @@ static void crypto_cbc_exit_tfm(struct crypto_tfm *tfm) ...@@ -275,13 +275,18 @@ static void crypto_cbc_exit_tfm(struct crypto_tfm *tfm)
crypto_free_cipher(ctx->child); crypto_free_cipher(ctx->child);
} }
static struct crypto_instance *crypto_cbc_alloc(void *param, unsigned int len) static struct crypto_instance *crypto_cbc_alloc(struct rtattr **tb)
{ {
struct crypto_instance *inst; struct crypto_instance *inst;
struct crypto_alg *alg; struct crypto_alg *alg;
int err;
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
if (err)
return ERR_PTR(err);
alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER, alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); CRYPTO_ALG_TYPE_MASK);
if (IS_ERR(alg)) if (IS_ERR(alg))
return ERR_PTR(PTR_ERR(alg)); return ERR_PTR(PTR_ERR(alg));
......
...@@ -26,14 +26,19 @@ ...@@ -26,14 +26,19 @@
struct cryptomgr_param { struct cryptomgr_param {
struct work_struct work; struct work_struct work;
struct rtattr *tb[CRYPTOA_MAX];
struct {
struct rtattr attr;
struct crypto_attr_type data;
} type;
struct { struct {
struct rtattr attr; struct rtattr attr;
struct crypto_attr_alg data; struct crypto_attr_alg data;
} alg; } alg;
struct { struct {
u32 type;
u32 mask;
char name[CRYPTO_MAX_ALG_NAME]; char name[CRYPTO_MAX_ALG_NAME];
} larval; } larval;
...@@ -53,7 +58,7 @@ static void cryptomgr_probe(struct work_struct *work) ...@@ -53,7 +58,7 @@ static void cryptomgr_probe(struct work_struct *work)
goto err; goto err;
do { do {
inst = tmpl->alloc(&param->alg, sizeof(param->alg)); inst = tmpl->alloc(param->tb);
if (IS_ERR(inst)) if (IS_ERR(inst))
err = PTR_ERR(inst); err = PTR_ERR(inst);
else if ((err = crypto_register_instance(tmpl, inst))) else if ((err = crypto_register_instance(tmpl, inst)))
...@@ -70,8 +75,8 @@ static void cryptomgr_probe(struct work_struct *work) ...@@ -70,8 +75,8 @@ static void cryptomgr_probe(struct work_struct *work)
return; return;
err: err:
crypto_larval_error(param->larval.name, param->larval.type, crypto_larval_error(param->larval.name, param->type.data.type,
param->larval.mask); param->type.data.mask);
goto out; goto out;
} }
...@@ -82,7 +87,7 @@ static int cryptomgr_schedule_probe(struct crypto_larval *larval) ...@@ -82,7 +87,7 @@ static int cryptomgr_schedule_probe(struct crypto_larval *larval)
const char *p; const char *p;
unsigned int len; unsigned int len;
param = kmalloc(sizeof(*param), GFP_KERNEL); param = kzalloc(sizeof(*param), GFP_KERNEL);
if (!param) if (!param)
goto err; goto err;
...@@ -94,7 +99,6 @@ static int cryptomgr_schedule_probe(struct crypto_larval *larval) ...@@ -94,7 +99,6 @@ static int cryptomgr_schedule_probe(struct crypto_larval *larval)
goto err_free_param; goto err_free_param;
memcpy(param->template, name, len); memcpy(param->template, name, len);
param->template[len] = 0;
name = p + 1; name = p + 1;
for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++) for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
...@@ -104,14 +108,18 @@ static int cryptomgr_schedule_probe(struct crypto_larval *larval) ...@@ -104,14 +108,18 @@ static int cryptomgr_schedule_probe(struct crypto_larval *larval)
if (!len || *p != ')' || p[1]) if (!len || *p != ')' || p[1])
goto err_free_param; goto err_free_param;
param->type.attr.rta_len = sizeof(param->type);
param->type.attr.rta_type = CRYPTOA_TYPE;
param->type.data.type = larval->alg.cra_flags;
param->type.data.mask = larval->mask;
param->tb[CRYPTOA_TYPE - 1] = &param->type.attr;
param->alg.attr.rta_len = sizeof(param->alg); param->alg.attr.rta_len = sizeof(param->alg);
param->alg.attr.rta_type = CRYPTOA_ALG; param->alg.attr.rta_type = CRYPTOA_ALG;
memcpy(param->alg.data.name, name, len); memcpy(param->alg.data.name, name, len);
param->alg.data.name[len] = 0; param->tb[CRYPTOA_ALG - 1] = &param->alg.attr;
memcpy(param->larval.name, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME); memcpy(param->larval.name, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
param->larval.type = larval->alg.cra_flags;
param->larval.mask = larval->mask;
INIT_WORK(&param->work, cryptomgr_probe); INIT_WORK(&param->work, cryptomgr_probe);
schedule_work(&param->work); schedule_work(&param->work);
......
...@@ -115,13 +115,18 @@ static void crypto_ecb_exit_tfm(struct crypto_tfm *tfm) ...@@ -115,13 +115,18 @@ static void crypto_ecb_exit_tfm(struct crypto_tfm *tfm)
crypto_free_cipher(ctx->child); crypto_free_cipher(ctx->child);
} }
static struct crypto_instance *crypto_ecb_alloc(void *param, unsigned int len) static struct crypto_instance *crypto_ecb_alloc(struct rtattr **tb)
{ {
struct crypto_instance *inst; struct crypto_instance *inst;
struct crypto_alg *alg; struct crypto_alg *alg;
int err;
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
if (err)
return ERR_PTR(err);
alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER, alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); CRYPTO_ALG_TYPE_MASK);
if (IS_ERR(alg)) if (IS_ERR(alg))
return ERR_PTR(PTR_ERR(alg)); return ERR_PTR(PTR_ERR(alg));
......
...@@ -197,13 +197,18 @@ static void hmac_free(struct crypto_instance *inst) ...@@ -197,13 +197,18 @@ static void hmac_free(struct crypto_instance *inst)
kfree(inst); kfree(inst);
} }
static struct crypto_instance *hmac_alloc(void *param, unsigned int len) static struct crypto_instance *hmac_alloc(struct rtattr **tb)
{ {
struct crypto_instance *inst; struct crypto_instance *inst;
struct crypto_alg *alg; struct crypto_alg *alg;
int err;
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_HASH);
if (err)
return ERR_PTR(err);
alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_HASH, alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_HASH,
CRYPTO_ALG_TYPE_HASH_MASK | CRYPTO_ALG_ASYNC); CRYPTO_ALG_TYPE_HASH_MASK);
if (IS_ERR(alg)) if (IS_ERR(alg))
return ERR_PTR(PTR_ERR(alg)); return ERR_PTR(PTR_ERR(alg));
......
...@@ -228,13 +228,18 @@ static void exit_tfm(struct crypto_tfm *tfm) ...@@ -228,13 +228,18 @@ static void exit_tfm(struct crypto_tfm *tfm)
crypto_free_cipher(ctx->child); crypto_free_cipher(ctx->child);
} }
static struct crypto_instance *alloc(void *param, unsigned int len) static struct crypto_instance *alloc(struct rtattr **tb)
{ {
struct crypto_instance *inst; struct crypto_instance *inst;
struct crypto_alg *alg; struct crypto_alg *alg;
int err;
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
if (err)
return ERR_PTR(err);
alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER, alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); CRYPTO_ALG_TYPE_MASK);
if (IS_ERR(alg)) if (IS_ERR(alg))
return ERR_PTR(PTR_ERR(alg)); return ERR_PTR(PTR_ERR(alg));
......
...@@ -279,13 +279,18 @@ static void crypto_pcbc_exit_tfm(struct crypto_tfm *tfm) ...@@ -279,13 +279,18 @@ static void crypto_pcbc_exit_tfm(struct crypto_tfm *tfm)
crypto_free_cipher(ctx->child); crypto_free_cipher(ctx->child);
} }
static struct crypto_instance *crypto_pcbc_alloc(void *param, unsigned int len) static struct crypto_instance *crypto_pcbc_alloc(struct rtattr **tb)
{ {
struct crypto_instance *inst; struct crypto_instance *inst;
struct crypto_alg *alg; struct crypto_alg *alg;
int err;
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
if (err)
return ERR_PTR(err);
alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER, alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); CRYPTO_ALG_TYPE_MASK);
if (IS_ERR(alg)) if (IS_ERR(alg))
return ERR_PTR(PTR_ERR(alg)); return ERR_PTR(PTR_ERR(alg));
......
...@@ -288,12 +288,18 @@ static void xcbc_exit_tfm(struct crypto_tfm *tfm) ...@@ -288,12 +288,18 @@ static void xcbc_exit_tfm(struct crypto_tfm *tfm)
crypto_free_cipher(ctx->child); crypto_free_cipher(ctx->child);
} }
static struct crypto_instance *xcbc_alloc(void *param, unsigned int len) static struct crypto_instance *xcbc_alloc(struct rtattr **tb)
{ {
struct crypto_instance *inst; struct crypto_instance *inst;
struct crypto_alg *alg; struct crypto_alg *alg;
alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER, int err;
CRYPTO_ALG_TYPE_HASH_MASK | CRYPTO_ALG_ASYNC);
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_HASH);
if (err)
return ERR_PTR(err);
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
CRYPTO_ALG_TYPE_MASK);
if (IS_ERR(alg)) if (IS_ERR(alg))
return ERR_PTR(PTR_ERR(alg)); return ERR_PTR(PTR_ERR(alg));
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <linux/crypto.h> #include <linux/crypto.h>
struct module; struct module;
struct rtattr;
struct seq_file; struct seq_file;
struct crypto_type { struct crypto_type {
...@@ -38,7 +39,7 @@ struct crypto_template { ...@@ -38,7 +39,7 @@ struct crypto_template {
struct hlist_head instances; struct hlist_head instances;
struct module *module; struct module *module;
struct crypto_instance *(*alloc)(void *param, unsigned int len); struct crypto_instance *(*alloc)(struct rtattr **tb);
void (*free)(struct crypto_instance *inst); void (*free)(struct crypto_instance *inst);
char name[CRYPTO_MAX_ALG_NAME]; char name[CRYPTO_MAX_ALG_NAME];
...@@ -96,8 +97,9 @@ void crypto_drop_spawn(struct crypto_spawn *spawn); ...@@ -96,8 +97,9 @@ void crypto_drop_spawn(struct crypto_spawn *spawn);
struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type, struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
u32 mask); u32 mask);
struct crypto_alg *crypto_get_attr_alg(void *param, unsigned int len, struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb);
u32 type, u32 mask); int crypto_check_attr_type(struct rtattr **tb, u32 type);
struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb, u32 type, u32 mask);
struct crypto_instance *crypto_alloc_instance(const char *name, struct crypto_instance *crypto_alloc_instance(const char *name,
struct crypto_alg *alg); struct crypto_alg *alg);
......
...@@ -372,12 +372,21 @@ struct crypto_hash { ...@@ -372,12 +372,21 @@ struct crypto_hash {
enum { enum {
CRYPTOA_UNSPEC, CRYPTOA_UNSPEC,
CRYPTOA_ALG, CRYPTOA_ALG,
CRYPTOA_TYPE,
__CRYPTOA_MAX,
}; };
#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
struct crypto_attr_alg { struct crypto_attr_alg {
char name[CRYPTO_MAX_ALG_NAME]; char name[CRYPTO_MAX_ALG_NAME];
}; };
struct crypto_attr_type {
u32 type;
u32 mask;
};
/* /*
* Transform user interface. * Transform user interface.
*/ */
......
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