Skip to content

Commit b9c55aa

Browse files
committed
[CRYPTO] skcipher: Create default givcipher instances
This patch makes crypto_alloc_ablkcipher/crypto_grab_skcipher always return algorithms that are capable of generating their own IVs through givencrypt and givdecrypt. Each algorithm may specify its default IV generator through the geniv field. For algorithms that do not set the geniv field, the blkcipher layer will pick a default. Currently it's chainiv for synchronous algorithms and eseqiv for asynchronous algorithms. Note that if these wrappers do not work on an algorithm then that algorithm must specify its own geniv or it can't be used at all. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 806d183 commit b9c55aa

6 files changed

Lines changed: 181 additions & 13 deletions

File tree

‎crypto/ablkcipher.c‎

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <linux/init.h>
1919
#include <linux/kernel.h>
2020
#include <linux/module.h>
21+
#include <linux/rtnetlink.h>
22+
#include <linux/sched.h>
2123
#include <linux/slab.h>
2224
#include <linux/seq_file.h>
2325

@@ -68,6 +70,16 @@ static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
6870
return alg->cra_ctxsize;
6971
}
7072

73+
int skcipher_null_givencrypt(struct skcipher_givcrypt_request *req)
74+
{
75+
return crypto_ablkcipher_encrypt(&req->creq);
76+
}
77+
78+
int skcipher_null_givdecrypt(struct skcipher_givcrypt_request *req)
79+
{
80+
return crypto_ablkcipher_decrypt(&req->creq);
81+
}
82+
7183
static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
7284
u32 mask)
7385
{
@@ -80,6 +92,10 @@ static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
8092
crt->setkey = setkey;
8193
crt->encrypt = alg->encrypt;
8294
crt->decrypt = alg->decrypt;
95+
if (!alg->ivsize) {
96+
crt->givencrypt = skcipher_null_givencrypt;
97+
crt->givdecrypt = skcipher_null_givdecrypt;
98+
}
8399
crt->base = __crypto_ablkcipher_cast(tfm);
84100
crt->ivsize = alg->ivsize;
85101

@@ -163,6 +179,108 @@ const char *crypto_default_geniv(const struct crypto_alg *alg)
163179
return alg->cra_flags & CRYPTO_ALG_ASYNC ? "eseqiv" : "chainiv";
164180
}
165181

182+
static int crypto_givcipher_default(struct crypto_alg *alg, u32 type, u32 mask)
183+
{
184+
struct rtattr *tb[3];
185+
struct {
186+
struct rtattr attr;
187+
struct crypto_attr_type data;
188+
} ptype;
189+
struct {
190+
struct rtattr attr;
191+
struct crypto_attr_alg data;
192+
} palg;
193+
struct crypto_template *tmpl;
194+
struct crypto_instance *inst;
195+
struct crypto_alg *larval;
196+
const char *geniv;
197+
int err;
198+
199+
larval = crypto_larval_lookup(alg->cra_driver_name,
200+
CRYPTO_ALG_TYPE_GIVCIPHER,
201+
CRYPTO_ALG_TYPE_MASK);
202+
err = PTR_ERR(larval);
203+
if (IS_ERR(larval))
204+
goto out;
205+
206+
err = -EAGAIN;
207+
if (!crypto_is_larval(larval))
208+
goto drop_larval;
209+
210+
ptype.attr.rta_len = sizeof(ptype);
211+
ptype.attr.rta_type = CRYPTOA_TYPE;
212+
ptype.data.type = type | CRYPTO_ALG_GENIV;
213+
/* GENIV tells the template that we're making a default geniv. */
214+
ptype.data.mask = mask | CRYPTO_ALG_GENIV;
215+
tb[0] = &ptype.attr;
216+
217+
palg.attr.rta_len = sizeof(palg);
218+
palg.attr.rta_type = CRYPTOA_ALG;
219+
/* Must use the exact name to locate ourselves. */
220+
memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
221+
tb[1] = &palg.attr;
222+
223+
tb[2] = NULL;
224+
225+
if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
226+
CRYPTO_ALG_TYPE_BLKCIPHER)
227+
geniv = alg->cra_blkcipher.geniv;
228+
else
229+
geniv = alg->cra_ablkcipher.geniv;
230+
231+
if (!geniv)
232+
geniv = crypto_default_geniv(alg);
233+
234+
tmpl = crypto_lookup_template(geniv);
235+
err = -ENOENT;
236+
if (!tmpl)
237+
goto kill_larval;
238+
239+
inst = tmpl->alloc(tb);
240+
err = PTR_ERR(inst);
241+
if (IS_ERR(inst))
242+
goto put_tmpl;
243+
244+
if ((err = crypto_register_instance(tmpl, inst))) {
245+
tmpl->free(inst);
246+
goto put_tmpl;
247+
}
248+
249+
/* Redo the lookup to use the instance we just registered. */
250+
err = -EAGAIN;
251+
252+
put_tmpl:
253+
crypto_tmpl_put(tmpl);
254+
kill_larval:
255+
crypto_larval_kill(larval);
256+
drop_larval:
257+
crypto_mod_put(larval);
258+
out:
259+
crypto_mod_put(alg);
260+
return err;
261+
}
262+
263+
static struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type,
264+
u32 mask)
265+
{
266+
struct crypto_alg *alg;
267+
268+
alg = crypto_alg_mod_lookup(name, type, mask);
269+
if (IS_ERR(alg))
270+
return alg;
271+
272+
if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
273+
CRYPTO_ALG_TYPE_GIVCIPHER)
274+
return alg;
275+
276+
if (!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
277+
CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
278+
alg->cra_ablkcipher.ivsize))
279+
return alg;
280+
281+
return ERR_PTR(crypto_givcipher_default(alg, type, mask));
282+
}
283+
166284
int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
167285
u32 type, u32 mask)
168286
{
@@ -172,7 +290,7 @@ int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
172290
type = crypto_skcipher_type(type);
173291
mask = crypto_skcipher_mask(mask);
174292

175-
alg = crypto_alg_mod_lookup(name, type, mask);
293+
alg = crypto_lookup_skcipher(name, type, mask);
176294
if (IS_ERR(alg))
177295
return PTR_ERR(alg);
178296

@@ -182,5 +300,43 @@ int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
182300
}
183301
EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
184302

303+
struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
304+
u32 type, u32 mask)
305+
{
306+
struct crypto_tfm *tfm;
307+
int err;
308+
309+
type = crypto_skcipher_type(type);
310+
mask = crypto_skcipher_mask(mask);
311+
312+
for (;;) {
313+
struct crypto_alg *alg;
314+
315+
alg = crypto_lookup_skcipher(alg_name, type, mask);
316+
if (IS_ERR(alg)) {
317+
err = PTR_ERR(alg);
318+
goto err;
319+
}
320+
321+
tfm = __crypto_alloc_tfm(alg, type, mask);
322+
if (!IS_ERR(tfm))
323+
return __crypto_ablkcipher_cast(tfm);
324+
325+
crypto_mod_put(alg);
326+
err = PTR_ERR(tfm);
327+
328+
err:
329+
if (err != -EAGAIN)
330+
break;
331+
if (signal_pending(current)) {
332+
err = -EINTR;
333+
break;
334+
}
335+
}
336+
337+
return ERR_PTR(err);
338+
}
339+
EXPORT_SYMBOL_GPL(crypto_alloc_ablkcipher);
340+
185341
MODULE_LICENSE("GPL");
186342
MODULE_DESCRIPTION("Asynchronous block chaining cipher type");

‎crypto/api.c‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static struct crypto_alg *crypto_larval_alloc(const char *name, u32 type,
137137
return alg;
138138
}
139139

140-
static void crypto_larval_kill(struct crypto_alg *alg)
140+
void crypto_larval_kill(struct crypto_alg *alg)
141141
{
142142
struct crypto_larval *larval = (void *)alg;
143143

@@ -147,6 +147,7 @@ static void crypto_larval_kill(struct crypto_alg *alg)
147147
complete_all(&larval->completion);
148148
crypto_alg_put(alg);
149149
}
150+
EXPORT_SYMBOL_GPL(crypto_larval_kill);
150151

151152
static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
152153
{
@@ -176,11 +177,9 @@ static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
176177
return alg;
177178
}
178179

179-
struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
180+
struct crypto_alg *crypto_larval_lookup(const char *name, u32 type, u32 mask)
180181
{
181182
struct crypto_alg *alg;
182-
struct crypto_alg *larval;
183-
int ok;
184183

185184
if (!name)
186185
return ERR_PTR(-ENOENT);
@@ -193,7 +192,17 @@ struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
193192
if (alg)
194193
return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
195194

196-
larval = crypto_larval_alloc(name, type, mask);
195+
return crypto_larval_alloc(name, type, mask);
196+
}
197+
EXPORT_SYMBOL_GPL(crypto_larval_lookup);
198+
199+
struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
200+
{
201+
struct crypto_alg *alg;
202+
struct crypto_alg *larval;
203+
int ok;
204+
205+
larval = crypto_larval_lookup(name, type, mask);
197206
if (IS_ERR(larval) || !crypto_is_larval(larval))
198207
return larval;
199208

‎crypto/blkcipher.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,10 @@ static int crypto_init_blkcipher_ops_async(struct crypto_tfm *tfm)
450450
crt->setkey = async_setkey;
451451
crt->encrypt = async_encrypt;
452452
crt->decrypt = async_decrypt;
453+
if (!alg->ivsize) {
454+
crt->givencrypt = skcipher_null_givencrypt;
455+
crt->givdecrypt = skcipher_null_givdecrypt;
456+
}
453457
crt->base = __crypto_ablkcipher_cast(tfm);
454458
crt->ivsize = alg->ivsize;
455459

‎crypto/internal.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ void crypto_exit_digest_ops(struct crypto_tfm *tfm);
9393
void crypto_exit_cipher_ops(struct crypto_tfm *tfm);
9494
void crypto_exit_compress_ops(struct crypto_tfm *tfm);
9595

96+
void crypto_larval_kill(struct crypto_alg *alg);
97+
struct crypto_alg *crypto_larval_lookup(const char *name, u32 type, u32 mask);
9698
void crypto_larval_error(const char *name, u32 type, u32 mask);
9799

98100
void crypto_shoot_alg(struct crypto_alg *alg);

‎include/crypto/internal/skcipher.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ static inline struct crypto_ablkcipher *crypto_spawn_skcipher(
5353
crypto_skcipher_mask(0)));
5454
}
5555

56+
int skcipher_null_givencrypt(struct skcipher_givcrypt_request *req);
57+
int skcipher_null_givdecrypt(struct skcipher_givcrypt_request *req);
5658
const char *crypto_default_geniv(const struct crypto_alg *alg);
5759

5860
struct crypto_instance *skcipher_geniv_alloc(struct crypto_template *tmpl,

‎include/linux/crypto.h‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -561,13 +561,8 @@ static inline u32 crypto_skcipher_mask(u32 mask)
561561
return mask;
562562
}
563563

564-
static inline struct crypto_ablkcipher *crypto_alloc_ablkcipher(
565-
const char *alg_name, u32 type, u32 mask)
566-
{
567-
return __crypto_ablkcipher_cast(
568-
crypto_alloc_base(alg_name, crypto_skcipher_type(type),
569-
crypto_skcipher_mask(mask)));
570-
}
564+
struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
565+
u32 type, u32 mask);
571566

572567
static inline struct crypto_tfm *crypto_ablkcipher_tfm(
573568
struct crypto_ablkcipher *tfm)

0 commit comments

Comments
 (0)