Skip to content

Commit 492e2b6

Browse files
committed
[CRYPTO] api: Allow algorithm lookup by type
This patch also adds the infrastructure to pick an algorithm based on their type. For example, this allows you to select the encryption algorithm "aes", instead of any algorithm registered under the name "aes". For now this is only accessible internally. Eventually it will be made available through crypto_alloc_tfm. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 2b8c19d commit 492e2b6

4 files changed

Lines changed: 40 additions & 18 deletions

File tree

‎crypto/algapi.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121

2222
static LIST_HEAD(crypto_template_list);
2323

24-
void crypto_larval_error(const char *name)
24+
void crypto_larval_error(const char *name, u32 type, u32 mask)
2525
{
2626
struct crypto_alg *alg;
2727

2828
down_read(&crypto_alg_sem);
29-
alg = __crypto_alg_lookup(name);
29+
alg = __crypto_alg_lookup(name, type, mask);
3030
up_read(&crypto_alg_sem);
3131

3232
if (alg) {
@@ -87,6 +87,8 @@ static int __crypto_register_alg(struct crypto_alg *alg)
8787
!strcmp(alg->cra_driver_name, q->cra_name))) {
8888
struct crypto_larval *larval = (void *)q;
8989

90+
if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
91+
continue;
9092
if (!crypto_mod_get(alg))
9193
continue;
9294
larval->adult = alg;

‎crypto/api.c‎

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,21 @@ void crypto_mod_put(struct crypto_alg *alg)
5757
}
5858
EXPORT_SYMBOL_GPL(crypto_mod_put);
5959

60-
struct crypto_alg *__crypto_alg_lookup(const char *name)
60+
struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type, u32 mask)
6161
{
6262
struct crypto_alg *q, *alg = NULL;
6363
int best = -2;
6464

6565
list_for_each_entry(q, &crypto_alg_list, cra_list) {
6666
int exact, fuzzy;
6767

68+
if ((q->cra_flags ^ type) & mask)
69+
continue;
70+
71+
if (crypto_is_larval(q) &&
72+
((struct crypto_larval *)q)->mask != mask)
73+
continue;
74+
6875
exact = !strcmp(q->cra_driver_name, name);
6976
fuzzy = !strcmp(q->cra_name, name);
7077
if (!exact && !(fuzzy && q->cra_priority > best))
@@ -96,7 +103,8 @@ static void crypto_larval_destroy(struct crypto_alg *alg)
96103
kfree(larval);
97104
}
98105

99-
static struct crypto_alg *crypto_larval_alloc(const char *name)
106+
static struct crypto_alg *crypto_larval_alloc(const char *name, u32 type,
107+
u32 mask)
100108
{
101109
struct crypto_alg *alg;
102110
struct crypto_larval *larval;
@@ -105,7 +113,8 @@ static struct crypto_alg *crypto_larval_alloc(const char *name)
105113
if (!larval)
106114
return NULL;
107115

108-
larval->alg.cra_flags = CRYPTO_ALG_LARVAL;
116+
larval->mask = mask;
117+
larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type;
109118
larval->alg.cra_priority = -1;
110119
larval->alg.cra_destroy = crypto_larval_destroy;
111120

@@ -114,7 +123,7 @@ static struct crypto_alg *crypto_larval_alloc(const char *name)
114123
init_completion(&larval->completion);
115124

116125
down_write(&crypto_alg_sem);
117-
alg = __crypto_alg_lookup(name);
126+
alg = __crypto_alg_lookup(name, type, mask);
118127
if (!alg) {
119128
alg = &larval->alg;
120129
list_add(&alg->cra_list, &crypto_alg_list);
@@ -151,33 +160,36 @@ static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
151160
return alg;
152161
}
153162

154-
static struct crypto_alg *crypto_alg_lookup(const char *name)
163+
static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
164+
u32 mask)
155165
{
156166
struct crypto_alg *alg;
157167

158168
if (!name)
159169
return NULL;
160170

161171
down_read(&crypto_alg_sem);
162-
alg = __crypto_alg_lookup(name);
172+
alg = __crypto_alg_lookup(name, type, mask);
163173
up_read(&crypto_alg_sem);
164174

165175
return alg;
166176
}
167177

168-
/* A far more intelligent version of this is planned. For now, just
169-
* try an exact match on the name of the algorithm. */
170-
static struct crypto_alg *crypto_alg_mod_lookup(const char *name)
178+
struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
171179
{
172180
struct crypto_alg *alg;
173181
struct crypto_alg *larval;
174182
int ok;
175183

176-
alg = try_then_request_module(crypto_alg_lookup(name), name);
184+
mask &= ~CRYPTO_ALG_LARVAL;
185+
type &= mask;
186+
187+
alg = try_then_request_module(crypto_alg_lookup(name, type, mask),
188+
name);
177189
if (alg)
178190
return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
179191

180-
larval = crypto_larval_alloc(name);
192+
larval = crypto_larval_alloc(name, type, mask);
181193
if (!larval || !crypto_is_larval(larval))
182194
return larval;
183195

@@ -196,6 +208,7 @@ static struct crypto_alg *crypto_alg_mod_lookup(const char *name)
196208
crypto_larval_kill(larval);
197209
return alg;
198210
}
211+
EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
199212

200213
static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
201214
{
@@ -291,7 +304,7 @@ struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
291304
struct crypto_alg *alg;
292305
unsigned int tfm_size;
293306

294-
alg = crypto_alg_mod_lookup(name);
307+
alg = crypto_alg_mod_lookup(name, 0, 0);
295308
if (alg == NULL)
296309
goto out;
297310

@@ -346,7 +359,7 @@ void crypto_free_tfm(struct crypto_tfm *tfm)
346359
int crypto_alg_available(const char *name, u32 flags)
347360
{
348361
int ret = 0;
349-
struct crypto_alg *alg = crypto_alg_mod_lookup(name);
362+
struct crypto_alg *alg = crypto_alg_mod_lookup(name, 0, 0);
350363

351364
if (alg) {
352365
crypto_mod_put(alg);

‎crypto/cryptomgr.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct cryptomgr_param {
3131
} alg;
3232

3333
struct {
34+
u32 type;
35+
u32 mask;
3436
char name[CRYPTO_MAX_ALG_NAME];
3537
} larval;
3638

@@ -62,7 +64,8 @@ static void cryptomgr_probe(void *data)
6264
return;
6365

6466
err:
65-
crypto_larval_error(param->larval.name);
67+
crypto_larval_error(param->larval.name, param->larval.type,
68+
param->larval.mask);
6669
goto out;
6770
}
6871

@@ -101,6 +104,8 @@ static int cryptomgr_schedule_probe(struct crypto_larval *larval)
101104
param->alg.data.name[len] = 0;
102105

103106
memcpy(param->larval.name, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
107+
param->larval.type = larval->alg.cra_flags;
108+
param->larval.mask = larval->mask;
104109

105110
INIT_WORK(&param->work, cryptomgr_probe, param);
106111
schedule_work(&param->work);

‎crypto/internal.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct crypto_larval {
4343
struct crypto_alg alg;
4444
struct crypto_alg *adult;
4545
struct completion completion;
46+
u32 mask;
4647
};
4748

4849
extern struct list_head crypto_alg_list;
@@ -124,7 +125,8 @@ static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg,
124125

125126
struct crypto_alg *crypto_mod_get(struct crypto_alg *alg);
126127
void crypto_mod_put(struct crypto_alg *alg);
127-
struct crypto_alg *__crypto_alg_lookup(const char *name);
128+
struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type, u32 mask);
129+
struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);
128130

129131
int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags);
130132
int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags);
@@ -138,7 +140,7 @@ void crypto_exit_digest_ops(struct crypto_tfm *tfm);
138140
void crypto_exit_cipher_ops(struct crypto_tfm *tfm);
139141
void crypto_exit_compress_ops(struct crypto_tfm *tfm);
140142

141-
void crypto_larval_error(const char *name);
143+
void crypto_larval_error(const char *name, u32 type, u32 mask);
142144

143145
int crypto_register_instance(struct crypto_template *tmpl,
144146
struct crypto_instance *inst);

0 commit comments

Comments
 (0)