Skip to content

Commit ebc610e

Browse files
committed
[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: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 6158efc commit ebc610e

10 files changed

Lines changed: 117 additions & 37 deletions

File tree

‎crypto/algapi.c‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,15 +425,45 @@ int crypto_unregister_notifier(struct notifier_block *nb)
425425
}
426426
EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
427427

428-
struct crypto_alg *crypto_get_attr_alg(void *param, unsigned int len,
429-
u32 type, u32 mask)
428+
struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
430429
{
431-
struct rtattr *rta = param;
430+
struct rtattr *rta = tb[CRYPTOA_TYPE - 1];
431+
struct crypto_attr_type *algt;
432+
433+
if (!rta)
434+
return ERR_PTR(-ENOENT);
435+
if (RTA_PAYLOAD(rta) < sizeof(*algt))
436+
return ERR_PTR(-EINVAL);
437+
438+
algt = RTA_DATA(rta);
439+
440+
return algt;
441+
}
442+
EXPORT_SYMBOL_GPL(crypto_get_attr_type);
443+
444+
int crypto_check_attr_type(struct rtattr **tb, u32 type)
445+
{
446+
struct crypto_attr_type *algt;
447+
448+
algt = crypto_get_attr_type(tb);
449+
if (IS_ERR(algt))
450+
return PTR_ERR(algt);
451+
452+
if ((algt->type ^ type) & algt->mask)
453+
return -EINVAL;
454+
455+
return 0;
456+
}
457+
EXPORT_SYMBOL_GPL(crypto_check_attr_type);
458+
459+
struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb, u32 type, u32 mask)
460+
{
461+
struct rtattr *rta = tb[CRYPTOA_ALG - 1];
432462
struct crypto_attr_alg *alga;
433463

434-
if (!RTA_OK(rta, len))
435-
return ERR_PTR(-EBADR);
436-
if (rta->rta_type != CRYPTOA_ALG || RTA_PAYLOAD(rta) < sizeof(*alga))
464+
if (!rta)
465+
return ERR_PTR(-ENOENT);
466+
if (RTA_PAYLOAD(rta) < sizeof(*alga))
437467
return ERR_PTR(-EINVAL);
438468

439469
alga = RTA_DATA(rta);

‎crypto/cbc.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,18 @@ static void crypto_cbc_exit_tfm(struct crypto_tfm *tfm)
275275
crypto_free_cipher(ctx->child);
276276
}
277277

278-
static struct crypto_instance *crypto_cbc_alloc(void *param, unsigned int len)
278+
static struct crypto_instance *crypto_cbc_alloc(struct rtattr **tb)
279279
{
280280
struct crypto_instance *inst;
281281
struct crypto_alg *alg;
282+
int err;
283+
284+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
285+
if (err)
286+
return ERR_PTR(err);
282287

283-
alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER,
284-
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
288+
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
289+
CRYPTO_ALG_TYPE_MASK);
285290
if (IS_ERR(alg))
286291
return ERR_PTR(PTR_ERR(alg));
287292

‎crypto/cryptomgr.c‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@
2626
struct cryptomgr_param {
2727
struct work_struct work;
2828

29+
struct rtattr *tb[CRYPTOA_MAX];
30+
31+
struct {
32+
struct rtattr attr;
33+
struct crypto_attr_type data;
34+
} type;
35+
2936
struct {
3037
struct rtattr attr;
3138
struct crypto_attr_alg data;
3239
} alg;
3340

3441
struct {
35-
u32 type;
36-
u32 mask;
3742
char name[CRYPTO_MAX_ALG_NAME];
3843
} larval;
3944

@@ -53,7 +58,7 @@ static void cryptomgr_probe(struct work_struct *work)
5358
goto err;
5459

5560
do {
56-
inst = tmpl->alloc(&param->alg, sizeof(param->alg));
61+
inst = tmpl->alloc(param->tb);
5762
if (IS_ERR(inst))
5863
err = PTR_ERR(inst);
5964
else if ((err = crypto_register_instance(tmpl, inst)))
@@ -70,8 +75,8 @@ static void cryptomgr_probe(struct work_struct *work)
7075
return;
7176

7277
err:
73-
crypto_larval_error(param->larval.name, param->larval.type,
74-
param->larval.mask);
78+
crypto_larval_error(param->larval.name, param->type.data.type,
79+
param->type.data.mask);
7580
goto out;
7681
}
7782

@@ -82,7 +87,7 @@ static int cryptomgr_schedule_probe(struct crypto_larval *larval)
8287
const char *p;
8388
unsigned int len;
8489

85-
param = kmalloc(sizeof(*param), GFP_KERNEL);
90+
param = kzalloc(sizeof(*param), GFP_KERNEL);
8691
if (!param)
8792
goto err;
8893

@@ -94,7 +99,6 @@ static int cryptomgr_schedule_probe(struct crypto_larval *larval)
9499
goto err_free_param;
95100

96101
memcpy(param->template, name, len);
97-
param->template[len] = 0;
98102

99103
name = p + 1;
100104
for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
@@ -104,14 +108,18 @@ static int cryptomgr_schedule_probe(struct crypto_larval *larval)
104108
if (!len || *p != ')' || p[1])
105109
goto err_free_param;
106110

111+
param->type.attr.rta_len = sizeof(param->type);
112+
param->type.attr.rta_type = CRYPTOA_TYPE;
113+
param->type.data.type = larval->alg.cra_flags;
114+
param->type.data.mask = larval->mask;
115+
param->tb[CRYPTOA_TYPE - 1] = &param->type.attr;
116+
107117
param->alg.attr.rta_len = sizeof(param->alg);
108118
param->alg.attr.rta_type = CRYPTOA_ALG;
109119
memcpy(param->alg.data.name, name, len);
110-
param->alg.data.name[len] = 0;
120+
param->tb[CRYPTOA_ALG - 1] = &param->alg.attr;
111121

112122
memcpy(param->larval.name, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
113-
param->larval.type = larval->alg.cra_flags;
114-
param->larval.mask = larval->mask;
115123

116124
INIT_WORK(&param->work, cryptomgr_probe);
117125
schedule_work(&param->work);

‎crypto/ecb.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,18 @@ static void crypto_ecb_exit_tfm(struct crypto_tfm *tfm)
115115
crypto_free_cipher(ctx->child);
116116
}
117117

118-
static struct crypto_instance *crypto_ecb_alloc(void *param, unsigned int len)
118+
static struct crypto_instance *crypto_ecb_alloc(struct rtattr **tb)
119119
{
120120
struct crypto_instance *inst;
121121
struct crypto_alg *alg;
122+
int err;
123+
124+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
125+
if (err)
126+
return ERR_PTR(err);
122127

123-
alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER,
124-
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
128+
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
129+
CRYPTO_ALG_TYPE_MASK);
125130
if (IS_ERR(alg))
126131
return ERR_PTR(PTR_ERR(alg));
127132

‎crypto/hmac.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,18 @@ static void hmac_free(struct crypto_instance *inst)
197197
kfree(inst);
198198
}
199199

200-
static struct crypto_instance *hmac_alloc(void *param, unsigned int len)
200+
static struct crypto_instance *hmac_alloc(struct rtattr **tb)
201201
{
202202
struct crypto_instance *inst;
203203
struct crypto_alg *alg;
204+
int err;
205+
206+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_HASH);
207+
if (err)
208+
return ERR_PTR(err);
204209

205-
alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_HASH,
206-
CRYPTO_ALG_TYPE_HASH_MASK | CRYPTO_ALG_ASYNC);
210+
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_HASH,
211+
CRYPTO_ALG_TYPE_HASH_MASK);
207212
if (IS_ERR(alg))
208213
return ERR_PTR(PTR_ERR(alg));
209214

‎crypto/lrw.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,18 @@ static void exit_tfm(struct crypto_tfm *tfm)
228228
crypto_free_cipher(ctx->child);
229229
}
230230

231-
static struct crypto_instance *alloc(void *param, unsigned int len)
231+
static struct crypto_instance *alloc(struct rtattr **tb)
232232
{
233233
struct crypto_instance *inst;
234234
struct crypto_alg *alg;
235+
int err;
236+
237+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
238+
if (err)
239+
return ERR_PTR(err);
235240

236-
alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER,
237-
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
241+
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
242+
CRYPTO_ALG_TYPE_MASK);
238243
if (IS_ERR(alg))
239244
return ERR_PTR(PTR_ERR(alg));
240245

‎crypto/pcbc.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,18 @@ static void crypto_pcbc_exit_tfm(struct crypto_tfm *tfm)
279279
crypto_free_cipher(ctx->child);
280280
}
281281

282-
static struct crypto_instance *crypto_pcbc_alloc(void *param, unsigned int len)
282+
static struct crypto_instance *crypto_pcbc_alloc(struct rtattr **tb)
283283
{
284284
struct crypto_instance *inst;
285285
struct crypto_alg *alg;
286+
int err;
287+
288+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
289+
if (err)
290+
return ERR_PTR(err);
286291

287-
alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER,
288-
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
292+
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
293+
CRYPTO_ALG_TYPE_MASK);
289294
if (IS_ERR(alg))
290295
return ERR_PTR(PTR_ERR(alg));
291296

‎crypto/xcbc.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,18 @@ static void xcbc_exit_tfm(struct crypto_tfm *tfm)
288288
crypto_free_cipher(ctx->child);
289289
}
290290

291-
static struct crypto_instance *xcbc_alloc(void *param, unsigned int len)
291+
static struct crypto_instance *xcbc_alloc(struct rtattr **tb)
292292
{
293293
struct crypto_instance *inst;
294294
struct crypto_alg *alg;
295-
alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER,
296-
CRYPTO_ALG_TYPE_HASH_MASK | CRYPTO_ALG_ASYNC);
295+
int err;
296+
297+
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_HASH);
298+
if (err)
299+
return ERR_PTR(err);
300+
301+
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
302+
CRYPTO_ALG_TYPE_MASK);
297303
if (IS_ERR(alg))
298304
return ERR_PTR(PTR_ERR(alg));
299305

‎include/crypto/algapi.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/crypto.h>
1616

1717
struct module;
18+
struct rtattr;
1819
struct seq_file;
1920

2021
struct crypto_type {
@@ -38,7 +39,7 @@ struct crypto_template {
3839
struct hlist_head instances;
3940
struct module *module;
4041

41-
struct crypto_instance *(*alloc)(void *param, unsigned int len);
42+
struct crypto_instance *(*alloc)(struct rtattr **tb);
4243
void (*free)(struct crypto_instance *inst);
4344

4445
char name[CRYPTO_MAX_ALG_NAME];
@@ -96,8 +97,9 @@ void crypto_drop_spawn(struct crypto_spawn *spawn);
9697
struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
9798
u32 mask);
9899

99-
struct crypto_alg *crypto_get_attr_alg(void *param, unsigned int len,
100-
u32 type, u32 mask);
100+
struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb);
101+
int crypto_check_attr_type(struct rtattr **tb, u32 type);
102+
struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb, u32 type, u32 mask);
101103
struct crypto_instance *crypto_alloc_instance(const char *name,
102104
struct crypto_alg *alg);
103105

‎include/linux/crypto.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,21 @@ struct crypto_hash {
372372
enum {
373373
CRYPTOA_UNSPEC,
374374
CRYPTOA_ALG,
375+
CRYPTOA_TYPE,
376+
__CRYPTOA_MAX,
375377
};
376378

379+
#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
380+
377381
struct crypto_attr_alg {
378382
char name[CRYPTO_MAX_ALG_NAME];
379383
};
380384

385+
struct crypto_attr_type {
386+
u32 type;
387+
u32 mask;
388+
};
389+
381390
/*
382391
* Transform user interface.
383392
*/

0 commit comments

Comments
 (0)