Skip to content

Commit 378f4f5

Browse files
committed
[CRYPTO] skcipher: Add crypto_grab_skcipher interface
Note: From now on the collective of ablkcipher/blkcipher/givcipher will be known as skcipher, i.e., symmetric key cipher. The name blkcipher has always been much of a misnomer since it supports stream ciphers too. This patch adds the function crypto_grab_skcipher as a new way of getting an ablkcipher spawn. The problem is that previously we did this in two steps, first getting the algorithm and then calling crypto_init_spawn. This meant that each spawn user had to be aware of what type and mask to use for these two steps. This is difficult and also presents a problem when the type/mask changes as they're about to be for IV generators. The new interface does both steps together just like crypto_alloc_ablkcipher. As a side-effect this also allows us to be stronger on type enforcement for spawns. For now this is only done for ablkcipher but it's trivial to extend for other types. This patch also moves the type/mask logic for skcipher into the helpers crypto_skcipher_type and crypto_skcipher_mask. Finally this patch introduces the function crypto_require_sync to determine whether the user is specifically requesting a sync algorithm. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 84c9115 commit 378f4f5

4 files changed

Lines changed: 108 additions & 16 deletions

File tree

‎crypto/ablkcipher.c‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
*
1414
*/
1515

16-
#include <crypto/algapi.h>
17-
#include <linux/errno.h>
16+
#include <crypto/internal/skcipher.h>
17+
#include <linux/err.h>
1818
#include <linux/init.h>
1919
#include <linux/kernel.h>
2020
#include <linux/module.h>
2121
#include <linux/slab.h>
2222
#include <linux/seq_file.h>
2323

24+
#include "internal.h"
25+
2426
static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
2527
unsigned int keylen)
2628
{
@@ -105,5 +107,24 @@ const struct crypto_type crypto_ablkcipher_type = {
105107
};
106108
EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
107109

110+
int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
111+
u32 type, u32 mask)
112+
{
113+
struct crypto_alg *alg;
114+
int err;
115+
116+
type = crypto_skcipher_type(type);
117+
mask = crypto_skcipher_mask(mask);
118+
119+
alg = crypto_alg_mod_lookup(name, type, mask);
120+
if (IS_ERR(alg))
121+
return PTR_ERR(alg);
122+
123+
err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
124+
crypto_mod_put(alg);
125+
return err;
126+
}
127+
EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
128+
108129
MODULE_LICENSE("GPL");
109130
MODULE_DESCRIPTION("Asynchronous block chaining cipher type");

‎include/crypto/algapi.h‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ void crypto_drop_spawn(struct crypto_spawn *spawn);
111111
struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
112112
u32 mask);
113113

114+
static inline void crypto_set_spawn(struct crypto_spawn *spawn,
115+
struct crypto_instance *inst)
116+
{
117+
spawn->inst = inst;
118+
}
119+
114120
struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb);
115121
int crypto_check_attr_type(struct rtattr **tb, u32 type);
116122
const char *crypto_attr_alg_name(struct rtattr *rta);
@@ -195,10 +201,9 @@ static inline struct crypto_instance *crypto_aead_alg_instance(
195201
static inline struct crypto_ablkcipher *crypto_spawn_ablkcipher(
196202
struct crypto_spawn *spawn)
197203
{
198-
u32 type = CRYPTO_ALG_TYPE_BLKCIPHER;
199-
u32 mask = CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
200-
201-
return __crypto_ablkcipher_cast(crypto_spawn_tfm(spawn, type, mask));
204+
return __crypto_ablkcipher_cast(
205+
crypto_spawn_tfm(spawn, crypto_skcipher_type(0),
206+
crypto_skcipher_mask(0)));
202207
}
203208

204209
static inline struct crypto_blkcipher *crypto_spawn_blkcipher(
@@ -308,5 +313,14 @@ static inline struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb,
308313
return crypto_attr_alg(tb[1], type, mask);
309314
}
310315

316+
/*
317+
* Returns CRYPTO_ALG_ASYNC if type/mask requires the use of sync algorithms.
318+
* Otherwise returns zero.
319+
*/
320+
static inline int crypto_requires_sync(u32 type, u32 mask)
321+
{
322+
return (type ^ CRYPTO_ALG_ASYNC) & mask & CRYPTO_ALG_ASYNC;
323+
}
324+
311325
#endif /* _CRYPTO_ALGAPI_H */
312326

‎include/crypto/internal/skcipher.h‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Symmetric key ciphers.
3+
*
4+
* Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
5+
*
6+
* This program is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License as published by the Free
8+
* Software Foundation; either version 2 of the License, or (at your option)
9+
* any later version.
10+
*
11+
*/
12+
13+
#ifndef _CRYPTO_INTERNAL_SKCIPHER_H
14+
#define _CRYPTO_INTERNAL_SKCIPHER_H
15+
16+
#include <crypto/algapi.h>
17+
18+
struct crypto_skcipher_spawn {
19+
struct crypto_spawn base;
20+
};
21+
22+
static inline void crypto_set_skcipher_spawn(
23+
struct crypto_skcipher_spawn *spawn, struct crypto_instance *inst)
24+
{
25+
crypto_set_spawn(&spawn->base, inst);
26+
}
27+
28+
int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
29+
u32 type, u32 mask);
30+
31+
static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn)
32+
{
33+
crypto_drop_spawn(&spawn->base);
34+
}
35+
36+
static inline struct crypto_alg *crypto_skcipher_spawn_alg(
37+
struct crypto_skcipher_spawn *spawn)
38+
{
39+
return spawn->base.alg;
40+
}
41+
42+
static inline struct crypto_ablkcipher *crypto_spawn_skcipher(
43+
struct crypto_skcipher_spawn *spawn)
44+
{
45+
return __crypto_ablkcipher_cast(
46+
crypto_spawn_tfm(&spawn->base, crypto_skcipher_type(0),
47+
crypto_skcipher_mask(0)));
48+
}
49+
50+
#endif /* _CRYPTO_INTERNAL_SKCIPHER_H */
51+

‎include/linux/crypto.h‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -528,16 +528,26 @@ static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
528528
return (struct crypto_ablkcipher *)tfm;
529529
}
530530

531-
static inline struct crypto_ablkcipher *crypto_alloc_ablkcipher(
532-
const char *alg_name, u32 type, u32 mask)
531+
static inline u32 crypto_skcipher_type(u32 type)
533532
{
534533
type &= ~CRYPTO_ALG_TYPE_MASK;
535-
mask &= ~CRYPTO_ALG_TYPE_MASK;
536534
type |= CRYPTO_ALG_TYPE_BLKCIPHER;
535+
return type;
536+
}
537+
538+
static inline u32 crypto_skcipher_mask(u32 mask)
539+
{
540+
mask &= ~CRYPTO_ALG_TYPE_MASK;
537541
mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
542+
return mask;
543+
}
538544

545+
static inline struct crypto_ablkcipher *crypto_alloc_ablkcipher(
546+
const char *alg_name, u32 type, u32 mask)
547+
{
539548
return __crypto_ablkcipher_cast(
540-
crypto_alloc_base(alg_name, type, mask));
549+
crypto_alloc_base(alg_name, crypto_skcipher_type(type),
550+
crypto_skcipher_mask(mask)));
541551
}
542552

543553
static inline struct crypto_tfm *crypto_ablkcipher_tfm(
@@ -554,12 +564,8 @@ static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
554564
static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
555565
u32 mask)
556566
{
557-
type &= ~CRYPTO_ALG_TYPE_MASK;
558-
mask &= ~CRYPTO_ALG_TYPE_MASK;
559-
type |= CRYPTO_ALG_TYPE_BLKCIPHER;
560-
mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
561-
562-
return crypto_has_alg(alg_name, type, mask);
567+
return crypto_has_alg(alg_name, crypto_skcipher_type(type),
568+
crypto_skcipher_mask(mask));
563569
}
564570

565571
static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(

0 commit comments

Comments
 (0)