Skip to content

Commit 4e6c3df

Browse files
committed
crypto: skcipher - Add low-level skcipher interface
This patch allows skcipher algorithms and instances to be created and registered with the crypto API. They are accessible through the top-level skcipher interface, along with ablkcipher/blkcipher algorithms and instances. This patch also introduces a new parameter called chunk size which is meant for ciphers such as CTR and CTS which ostensibly can handle arbitrary lengths, but still behave like block ciphers in that you can only process a partial block at the very end. For these ciphers the block size will continue to be set to 1 as it is now while the chunk size will be set to the underlying block size. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent eb9bc8e commit 4e6c3df

4 files changed

Lines changed: 407 additions & 7 deletions

File tree

‎crypto/skcipher.c‎

Lines changed: 189 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
#include <crypto/internal/skcipher.h>
1818
#include <linux/bug.h>
19+
#include <linux/cryptouser.h>
1920
#include <linux/module.h>
21+
#include <linux/rtnetlink.h>
22+
#include <linux/seq_file.h>
23+
#include <net/netlink.h>
2024

2125
#include "internal.h"
2226

@@ -25,10 +29,11 @@ static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
2529
if (alg->cra_type == &crypto_blkcipher_type)
2630
return sizeof(struct crypto_blkcipher *);
2731

28-
BUG_ON(alg->cra_type != &crypto_ablkcipher_type &&
29-
alg->cra_type != &crypto_givcipher_type);
32+
if (alg->cra_type == &crypto_ablkcipher_type ||
33+
alg->cra_type == &crypto_givcipher_type)
34+
return sizeof(struct crypto_ablkcipher *);
3035

31-
return sizeof(struct crypto_ablkcipher *);
36+
return crypto_alg_extsize(alg);
3237
}
3338

3439
static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm,
@@ -216,32 +221,209 @@ static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
216221
return 0;
217222
}
218223

224+
static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
225+
{
226+
struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
227+
struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
228+
229+
alg->exit(skcipher);
230+
}
231+
219232
static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
220233
{
234+
struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
235+
struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
236+
221237
if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type)
222238
return crypto_init_skcipher_ops_blkcipher(tfm);
223239

224-
BUG_ON(tfm->__crt_alg->cra_type != &crypto_ablkcipher_type &&
225-
tfm->__crt_alg->cra_type != &crypto_givcipher_type);
240+
if (tfm->__crt_alg->cra_type == &crypto_ablkcipher_type ||
241+
tfm->__crt_alg->cra_type == &crypto_givcipher_type)
242+
return crypto_init_skcipher_ops_ablkcipher(tfm);
243+
244+
skcipher->setkey = alg->setkey;
245+
skcipher->encrypt = alg->encrypt;
246+
skcipher->decrypt = alg->decrypt;
247+
skcipher->ivsize = alg->ivsize;
248+
skcipher->keysize = alg->max_keysize;
249+
250+
if (alg->exit)
251+
skcipher->base.exit = crypto_skcipher_exit_tfm;
226252

227-
return crypto_init_skcipher_ops_ablkcipher(tfm);
253+
if (alg->init)
254+
return alg->init(skcipher);
255+
256+
return 0;
257+
}
258+
259+
static void crypto_skcipher_free_instance(struct crypto_instance *inst)
260+
{
261+
struct skcipher_instance *skcipher =
262+
container_of(inst, struct skcipher_instance, s.base);
263+
264+
skcipher->free(skcipher);
265+
}
266+
267+
static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
268+
__attribute__ ((unused));
269+
static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
270+
{
271+
struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
272+
base);
273+
274+
seq_printf(m, "type : skcipher\n");
275+
seq_printf(m, "async : %s\n",
276+
alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no");
277+
seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
278+
seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
279+
seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
280+
seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
281+
seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
228282
}
229283

284+
#ifdef CONFIG_NET
285+
static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
286+
{
287+
struct crypto_report_blkcipher rblkcipher;
288+
struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
289+
base);
290+
291+
strncpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
292+
strncpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
293+
294+
rblkcipher.blocksize = alg->cra_blocksize;
295+
rblkcipher.min_keysize = skcipher->min_keysize;
296+
rblkcipher.max_keysize = skcipher->max_keysize;
297+
rblkcipher.ivsize = skcipher->ivsize;
298+
299+
if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
300+
sizeof(struct crypto_report_blkcipher), &rblkcipher))
301+
goto nla_put_failure;
302+
return 0;
303+
304+
nla_put_failure:
305+
return -EMSGSIZE;
306+
}
307+
#else
308+
static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
309+
{
310+
return -ENOSYS;
311+
}
312+
#endif
313+
230314
static const struct crypto_type crypto_skcipher_type2 = {
231315
.extsize = crypto_skcipher_extsize,
232316
.init_tfm = crypto_skcipher_init_tfm,
317+
.free = crypto_skcipher_free_instance,
318+
#ifdef CONFIG_PROC_FS
319+
.show = crypto_skcipher_show,
320+
#endif
321+
.report = crypto_skcipher_report,
233322
.maskclear = ~CRYPTO_ALG_TYPE_MASK,
234323
.maskset = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
235-
.type = CRYPTO_ALG_TYPE_BLKCIPHER,
324+
.type = CRYPTO_ALG_TYPE_SKCIPHER,
236325
.tfmsize = offsetof(struct crypto_skcipher, base),
237326
};
238327

328+
int crypto_grab_skcipher2(struct crypto_skcipher_spawn *spawn,
329+
const char *name, u32 type, u32 mask)
330+
{
331+
spawn->base.frontend = &crypto_skcipher_type2;
332+
return crypto_grab_spawn(&spawn->base, name, type, mask);
333+
}
334+
EXPORT_SYMBOL_GPL(crypto_grab_skcipher2);
335+
239336
struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
240337
u32 type, u32 mask)
241338
{
242339
return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
243340
}
244341
EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
245342

343+
int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
344+
{
345+
return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,
346+
type, mask);
347+
}
348+
EXPORT_SYMBOL_GPL(crypto_has_skcipher2);
349+
350+
static int skcipher_prepare_alg(struct skcipher_alg *alg)
351+
{
352+
struct crypto_alg *base = &alg->base;
353+
354+
if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8)
355+
return -EINVAL;
356+
357+
if (!alg->chunksize)
358+
alg->chunksize = base->cra_blocksize;
359+
360+
base->cra_type = &crypto_skcipher_type2;
361+
base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
362+
base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
363+
364+
return 0;
365+
}
366+
367+
int crypto_register_skcipher(struct skcipher_alg *alg)
368+
{
369+
struct crypto_alg *base = &alg->base;
370+
int err;
371+
372+
err = skcipher_prepare_alg(alg);
373+
if (err)
374+
return err;
375+
376+
return crypto_register_alg(base);
377+
}
378+
EXPORT_SYMBOL_GPL(crypto_register_skcipher);
379+
380+
void crypto_unregister_skcipher(struct skcipher_alg *alg)
381+
{
382+
crypto_unregister_alg(&alg->base);
383+
}
384+
EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
385+
386+
int crypto_register_skciphers(struct skcipher_alg *algs, int count)
387+
{
388+
int i, ret;
389+
390+
for (i = 0; i < count; i++) {
391+
ret = crypto_register_skcipher(&algs[i]);
392+
if (ret)
393+
goto err;
394+
}
395+
396+
return 0;
397+
398+
err:
399+
for (--i; i >= 0; --i)
400+
crypto_unregister_skcipher(&algs[i]);
401+
402+
return ret;
403+
}
404+
EXPORT_SYMBOL_GPL(crypto_register_skciphers);
405+
406+
void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
407+
{
408+
int i;
409+
410+
for (i = count - 1; i >= 0; --i)
411+
crypto_unregister_skcipher(&algs[i]);
412+
}
413+
EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
414+
415+
int skcipher_register_instance(struct crypto_template *tmpl,
416+
struct skcipher_instance *inst)
417+
{
418+
int err;
419+
420+
err = skcipher_prepare_alg(&inst->alg);
421+
if (err)
422+
return err;
423+
424+
return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
425+
}
426+
EXPORT_SYMBOL_GPL(skcipher_register_instance);
427+
246428
MODULE_LICENSE("GPL");
247429
MODULE_DESCRIPTION("Symmetric key cipher type");

‎include/crypto/internal/skcipher.h‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,46 @@
1919

2020
struct rtattr;
2121

22+
struct skcipher_instance {
23+
void (*free)(struct skcipher_instance *inst);
24+
union {
25+
struct {
26+
char head[offsetof(struct skcipher_alg, base)];
27+
struct crypto_instance base;
28+
} s;
29+
struct skcipher_alg alg;
30+
};
31+
};
32+
2233
struct crypto_skcipher_spawn {
2334
struct crypto_spawn base;
2435
};
2536

2637
extern const struct crypto_type crypto_givcipher_type;
2738

39+
static inline struct crypto_instance *skcipher_crypto_instance(
40+
struct skcipher_instance *inst)
41+
{
42+
return &inst->s.base;
43+
}
44+
45+
static inline struct skcipher_instance *skcipher_alg_instance(
46+
struct crypto_skcipher *skcipher)
47+
{
48+
return container_of(crypto_skcipher_alg(skcipher),
49+
struct skcipher_instance, alg);
50+
}
51+
52+
static inline void *skcipher_instance_ctx(struct skcipher_instance *inst)
53+
{
54+
return crypto_instance_ctx(skcipher_crypto_instance(inst));
55+
}
56+
57+
static inline void skcipher_request_complete(struct skcipher_request *req, int err)
58+
{
59+
req->base.complete(&req->base, err);
60+
}
61+
2862
static inline void crypto_set_skcipher_spawn(
2963
struct crypto_skcipher_spawn *spawn, struct crypto_instance *inst)
3064
{
@@ -33,6 +67,8 @@ static inline void crypto_set_skcipher_spawn(
3367

3468
int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
3569
u32 type, u32 mask);
70+
int crypto_grab_skcipher2(struct crypto_skcipher_spawn *spawn,
71+
const char *name, u32 type, u32 mask);
3672

3773
struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type, u32 mask);
3874

@@ -47,6 +83,12 @@ static inline struct crypto_alg *crypto_skcipher_spawn_alg(
4783
return spawn->base.alg;
4884
}
4985

86+
static inline struct skcipher_alg *crypto_spawn_skcipher_alg(
87+
struct crypto_skcipher_spawn *spawn)
88+
{
89+
return container_of(spawn->base.alg, struct skcipher_alg, base);
90+
}
91+
5092
static inline struct crypto_ablkcipher *crypto_spawn_skcipher(
5193
struct crypto_skcipher_spawn *spawn)
5294
{
@@ -55,6 +97,25 @@ static inline struct crypto_ablkcipher *crypto_spawn_skcipher(
5597
crypto_skcipher_mask(0)));
5698
}
5799

100+
static inline struct crypto_skcipher *crypto_spawn_skcipher2(
101+
struct crypto_skcipher_spawn *spawn)
102+
{
103+
return crypto_spawn_tfm2(&spawn->base);
104+
}
105+
106+
static inline void crypto_skcipher_set_reqsize(
107+
struct crypto_skcipher *skcipher, unsigned int reqsize)
108+
{
109+
skcipher->reqsize = reqsize;
110+
}
111+
112+
int crypto_register_skcipher(struct skcipher_alg *alg);
113+
void crypto_unregister_skcipher(struct skcipher_alg *alg);
114+
int crypto_register_skciphers(struct skcipher_alg *algs, int count);
115+
void crypto_unregister_skciphers(struct skcipher_alg *algs, int count);
116+
int skcipher_register_instance(struct crypto_template *tmpl,
117+
struct skcipher_instance *inst);
118+
58119
int skcipher_null_givencrypt(struct skcipher_givcrypt_request *req);
59120
int skcipher_null_givdecrypt(struct skcipher_givcrypt_request *req);
60121
const char *crypto_default_geniv(const struct crypto_alg *alg);
@@ -122,5 +183,31 @@ static inline u32 skcipher_request_flags(struct skcipher_request *req)
122183
return req->base.flags;
123184
}
124185

186+
static inline unsigned int crypto_skcipher_alg_min_keysize(
187+
struct skcipher_alg *alg)
188+
{
189+
if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
190+
CRYPTO_ALG_TYPE_BLKCIPHER)
191+
return alg->base.cra_blkcipher.min_keysize;
192+
193+
if (alg->base.cra_ablkcipher.encrypt)
194+
return alg->base.cra_ablkcipher.min_keysize;
195+
196+
return alg->min_keysize;
197+
}
198+
199+
static inline unsigned int crypto_skcipher_alg_max_keysize(
200+
struct skcipher_alg *alg)
201+
{
202+
if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
203+
CRYPTO_ALG_TYPE_BLKCIPHER)
204+
return alg->base.cra_blkcipher.max_keysize;
205+
206+
if (alg->base.cra_ablkcipher.encrypt)
207+
return alg->base.cra_ablkcipher.max_keysize;
208+
209+
return alg->max_keysize;
210+
}
211+
125212
#endif /* _CRYPTO_INTERNAL_SKCIPHER_H */
126213

0 commit comments

Comments
 (0)