Skip to content

Commit 20cc01b

Browse files
committed
crypto: aead - Split out geniv into its own module
If aead is built as a module along with cryptomgr, it creates a dependency loop due to the dependency chain aead => crypto_null => cryptomgr => aead. This is due to the presence of the AEAD geniv code. This code is not really part of the AEAD API but simply support code for IV generators such as seqiv. This patch moves the geniv code into its own module thus breaking the dependency loop. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 8ab23d5 commit 20cc01b

3 files changed

Lines changed: 179 additions & 163 deletions

File tree

‎crypto/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ crypto_algapi-y := algapi.o scatterwalk.o $(crypto_algapi-y)
1414
obj-$(CONFIG_CRYPTO_ALGAPI2) += crypto_algapi.o
1515

1616
obj-$(CONFIG_CRYPTO_AEAD2) += aead.o
17+
obj-$(CONFIG_CRYPTO_AEAD2) += geniv.o
1718

1819
crypto_skcipher-y := ablkcipher.o
1920
crypto_skcipher-y += skcipher.o

‎crypto/aead.c‎

Lines changed: 2 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,14 @@
77
* Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
88
*/
99

10-
#include <crypto/internal/geniv.h>
11-
#include <crypto/internal/rng.h>
12-
#include <crypto/null.h>
13-
#include <crypto/scatterwalk.h>
14-
#include <linux/err.h>
10+
#include <crypto/internal/aead.h>
11+
#include <linux/errno.h>
1512
#include <linux/init.h>
1613
#include <linux/kernel.h>
1714
#include <linux/module.h>
18-
#include <linux/rtnetlink.h>
1915
#include <linux/slab.h>
2016
#include <linux/seq_file.h>
2117
#include <linux/cryptouser.h>
22-
#include <linux/compiler.h>
2318
#include <net/netlink.h>
2419

2520
#include "internal.h"
@@ -212,162 +207,6 @@ static const struct crypto_type crypto_aead_type = {
212207
.tfmsize = offsetof(struct crypto_aead, base),
213208
};
214209

215-
static int aead_geniv_setkey(struct crypto_aead *tfm,
216-
const u8 *key, unsigned int keylen)
217-
{
218-
struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
219-
220-
return crypto_aead_setkey(ctx->child, key, keylen);
221-
}
222-
223-
static int aead_geniv_setauthsize(struct crypto_aead *tfm,
224-
unsigned int authsize)
225-
{
226-
struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
227-
228-
return crypto_aead_setauthsize(ctx->child, authsize);
229-
}
230-
231-
struct aead_instance *aead_geniv_alloc(struct crypto_template *tmpl,
232-
struct rtattr **tb, u32 type, u32 mask)
233-
{
234-
const char *name;
235-
struct crypto_aead_spawn *spawn;
236-
struct crypto_attr_type *algt;
237-
struct aead_instance *inst;
238-
struct aead_alg *alg;
239-
unsigned int ivsize;
240-
unsigned int maxauthsize;
241-
int err;
242-
243-
algt = crypto_get_attr_type(tb);
244-
if (IS_ERR(algt))
245-
return ERR_CAST(algt);
246-
247-
if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
248-
return ERR_PTR(-EINVAL);
249-
250-
name = crypto_attr_alg_name(tb[1]);
251-
if (IS_ERR(name))
252-
return ERR_CAST(name);
253-
254-
inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
255-
if (!inst)
256-
return ERR_PTR(-ENOMEM);
257-
258-
spawn = aead_instance_ctx(inst);
259-
260-
/* Ignore async algorithms if necessary. */
261-
mask |= crypto_requires_sync(algt->type, algt->mask);
262-
263-
crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
264-
err = crypto_grab_aead(spawn, name, type, mask);
265-
if (err)
266-
goto err_free_inst;
267-
268-
alg = crypto_spawn_aead_alg(spawn);
269-
270-
ivsize = crypto_aead_alg_ivsize(alg);
271-
maxauthsize = crypto_aead_alg_maxauthsize(alg);
272-
273-
err = -EINVAL;
274-
if (ivsize < sizeof(u64))
275-
goto err_drop_alg;
276-
277-
err = -ENAMETOOLONG;
278-
if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
279-
"%s(%s)", tmpl->name, alg->base.cra_name) >=
280-
CRYPTO_MAX_ALG_NAME)
281-
goto err_drop_alg;
282-
if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
283-
"%s(%s)", tmpl->name, alg->base.cra_driver_name) >=
284-
CRYPTO_MAX_ALG_NAME)
285-
goto err_drop_alg;
286-
287-
inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
288-
inst->alg.base.cra_priority = alg->base.cra_priority;
289-
inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
290-
inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
291-
inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
292-
293-
inst->alg.setkey = aead_geniv_setkey;
294-
inst->alg.setauthsize = aead_geniv_setauthsize;
295-
296-
inst->alg.ivsize = ivsize;
297-
inst->alg.maxauthsize = maxauthsize;
298-
299-
out:
300-
return inst;
301-
302-
err_drop_alg:
303-
crypto_drop_aead(spawn);
304-
err_free_inst:
305-
kfree(inst);
306-
inst = ERR_PTR(err);
307-
goto out;
308-
}
309-
EXPORT_SYMBOL_GPL(aead_geniv_alloc);
310-
311-
void aead_geniv_free(struct aead_instance *inst)
312-
{
313-
crypto_drop_aead(aead_instance_ctx(inst));
314-
kfree(inst);
315-
}
316-
EXPORT_SYMBOL_GPL(aead_geniv_free);
317-
318-
int aead_init_geniv(struct crypto_aead *aead)
319-
{
320-
struct aead_geniv_ctx *ctx = crypto_aead_ctx(aead);
321-
struct aead_instance *inst = aead_alg_instance(aead);
322-
struct crypto_aead *child;
323-
int err;
324-
325-
spin_lock_init(&ctx->lock);
326-
327-
err = crypto_get_default_rng();
328-
if (err)
329-
goto out;
330-
331-
err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
332-
crypto_aead_ivsize(aead));
333-
crypto_put_default_rng();
334-
if (err)
335-
goto out;
336-
337-
ctx->sknull = crypto_get_default_null_skcipher();
338-
err = PTR_ERR(ctx->sknull);
339-
if (IS_ERR(ctx->sknull))
340-
goto out;
341-
342-
child = crypto_spawn_aead(aead_instance_ctx(inst));
343-
err = PTR_ERR(child);
344-
if (IS_ERR(child))
345-
goto drop_null;
346-
347-
ctx->child = child;
348-
crypto_aead_set_reqsize(aead, crypto_aead_reqsize(child) +
349-
sizeof(struct aead_request));
350-
351-
err = 0;
352-
353-
out:
354-
return err;
355-
356-
drop_null:
357-
crypto_put_default_null_skcipher();
358-
goto out;
359-
}
360-
EXPORT_SYMBOL_GPL(aead_init_geniv);
361-
362-
void aead_exit_geniv(struct crypto_aead *tfm)
363-
{
364-
struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
365-
366-
crypto_free_aead(ctx->child);
367-
crypto_put_default_null_skcipher();
368-
}
369-
EXPORT_SYMBOL_GPL(aead_exit_geniv);
370-
371210
int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
372211
u32 type, u32 mask)
373212
{

‎crypto/geniv.c‎

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* geniv: Shared IV generator code
4+
*
5+
* This file provides common code to IV generators such as seqiv.
6+
*
7+
* Copyright (c) 2007-2019 Herbert Xu <herbert@gondor.apana.org.au>
8+
*/
9+
10+
#include <crypto/internal/geniv.h>
11+
#include <crypto/internal/rng.h>
12+
#include <crypto/null.h>
13+
#include <linux/err.h>
14+
#include <linux/kernel.h>
15+
#include <linux/module.h>
16+
#include <linux/rtnetlink.h>
17+
#include <linux/slab.h>
18+
19+
static int aead_geniv_setkey(struct crypto_aead *tfm,
20+
const u8 *key, unsigned int keylen)
21+
{
22+
struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
23+
24+
return crypto_aead_setkey(ctx->child, key, keylen);
25+
}
26+
27+
static int aead_geniv_setauthsize(struct crypto_aead *tfm,
28+
unsigned int authsize)
29+
{
30+
struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
31+
32+
return crypto_aead_setauthsize(ctx->child, authsize);
33+
}
34+
35+
struct aead_instance *aead_geniv_alloc(struct crypto_template *tmpl,
36+
struct rtattr **tb, u32 type, u32 mask)
37+
{
38+
const char *name;
39+
struct crypto_aead_spawn *spawn;
40+
struct crypto_attr_type *algt;
41+
struct aead_instance *inst;
42+
struct aead_alg *alg;
43+
unsigned int ivsize;
44+
unsigned int maxauthsize;
45+
int err;
46+
47+
algt = crypto_get_attr_type(tb);
48+
if (IS_ERR(algt))
49+
return ERR_CAST(algt);
50+
51+
if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
52+
return ERR_PTR(-EINVAL);
53+
54+
name = crypto_attr_alg_name(tb[1]);
55+
if (IS_ERR(name))
56+
return ERR_CAST(name);
57+
58+
inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
59+
if (!inst)
60+
return ERR_PTR(-ENOMEM);
61+
62+
spawn = aead_instance_ctx(inst);
63+
64+
/* Ignore async algorithms if necessary. */
65+
mask |= crypto_requires_sync(algt->type, algt->mask);
66+
67+
crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
68+
err = crypto_grab_aead(spawn, name, type, mask);
69+
if (err)
70+
goto err_free_inst;
71+
72+
alg = crypto_spawn_aead_alg(spawn);
73+
74+
ivsize = crypto_aead_alg_ivsize(alg);
75+
maxauthsize = crypto_aead_alg_maxauthsize(alg);
76+
77+
err = -EINVAL;
78+
if (ivsize < sizeof(u64))
79+
goto err_drop_alg;
80+
81+
err = -ENAMETOOLONG;
82+
if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
83+
"%s(%s)", tmpl->name, alg->base.cra_name) >=
84+
CRYPTO_MAX_ALG_NAME)
85+
goto err_drop_alg;
86+
if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
87+
"%s(%s)", tmpl->name, alg->base.cra_driver_name) >=
88+
CRYPTO_MAX_ALG_NAME)
89+
goto err_drop_alg;
90+
91+
inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
92+
inst->alg.base.cra_priority = alg->base.cra_priority;
93+
inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
94+
inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
95+
inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
96+
97+
inst->alg.setkey = aead_geniv_setkey;
98+
inst->alg.setauthsize = aead_geniv_setauthsize;
99+
100+
inst->alg.ivsize = ivsize;
101+
inst->alg.maxauthsize = maxauthsize;
102+
103+
out:
104+
return inst;
105+
106+
err_drop_alg:
107+
crypto_drop_aead(spawn);
108+
err_free_inst:
109+
kfree(inst);
110+
inst = ERR_PTR(err);
111+
goto out;
112+
}
113+
EXPORT_SYMBOL_GPL(aead_geniv_alloc);
114+
115+
void aead_geniv_free(struct aead_instance *inst)
116+
{
117+
crypto_drop_aead(aead_instance_ctx(inst));
118+
kfree(inst);
119+
}
120+
EXPORT_SYMBOL_GPL(aead_geniv_free);
121+
122+
int aead_init_geniv(struct crypto_aead *aead)
123+
{
124+
struct aead_geniv_ctx *ctx = crypto_aead_ctx(aead);
125+
struct aead_instance *inst = aead_alg_instance(aead);
126+
struct crypto_aead *child;
127+
int err;
128+
129+
spin_lock_init(&ctx->lock);
130+
131+
err = crypto_get_default_rng();
132+
if (err)
133+
goto out;
134+
135+
err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
136+
crypto_aead_ivsize(aead));
137+
crypto_put_default_rng();
138+
if (err)
139+
goto out;
140+
141+
ctx->sknull = crypto_get_default_null_skcipher();
142+
err = PTR_ERR(ctx->sknull);
143+
if (IS_ERR(ctx->sknull))
144+
goto out;
145+
146+
child = crypto_spawn_aead(aead_instance_ctx(inst));
147+
err = PTR_ERR(child);
148+
if (IS_ERR(child))
149+
goto drop_null;
150+
151+
ctx->child = child;
152+
crypto_aead_set_reqsize(aead, crypto_aead_reqsize(child) +
153+
sizeof(struct aead_request));
154+
155+
err = 0;
156+
157+
out:
158+
return err;
159+
160+
drop_null:
161+
crypto_put_default_null_skcipher();
162+
goto out;
163+
}
164+
EXPORT_SYMBOL_GPL(aead_init_geniv);
165+
166+
void aead_exit_geniv(struct crypto_aead *tfm)
167+
{
168+
struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
169+
170+
crypto_free_aead(ctx->child);
171+
crypto_put_default_null_skcipher();
172+
}
173+
EXPORT_SYMBOL_GPL(aead_exit_geniv);
174+
175+
MODULE_LICENSE("GPL");
176+
MODULE_DESCRIPTION("Shared IV generator code");

0 commit comments

Comments
 (0)