Skip to content

Commit 46ed526

Browse files
nicstangeherbertx
authored andcommitted
crypto: kpp - provide support for KPP spawns
The upcoming support for the RFC 7919 ffdhe group parameters will be made available in the form of templates like "ffdhe2048(dh)", "ffdhe3072(dh)" and so on. Template instantiations thereof would wrap the inner "dh" kpp_alg and also provide kpp_alg services to the outside again. The primitves needed for providing kpp_alg services from template instances have been introduced with the previous patch. Continue this work now and implement everything needed for enabling template instances to make use of inner KPP algorithms like "dh". More specifically, define a struct crypto_kpp_spawn in close analogy to crypto_skcipher_spawn, crypto_shash_spawn and alike. Implement a crypto_grab_kpp() and crypto_drop_kpp() pair for binding such a spawn to some inner kpp_alg and for releasing it respectively. Template implementations can instantiate transforms from the underlying kpp_alg by means of the new crypto_spawn_kpp(). Finally, provide the crypto_spawn_kpp_alg() helper for accessing a spawn's underlying kpp_alg during template instantiation. Annotate everything with proper kernel-doc comments, even though include/crypto/internal/kpp.h is not considered for the generated docs. Signed-off-by: Nicolai Stange <[email protected]> Reviewed-by: Hannes Reinecke <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 1038fd7 commit 46ed526

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

crypto/kpp.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask)
9595
}
9696
EXPORT_SYMBOL_GPL(crypto_alloc_kpp);
9797

98+
int crypto_grab_kpp(struct crypto_kpp_spawn *spawn,
99+
struct crypto_instance *inst,
100+
const char *name, u32 type, u32 mask)
101+
{
102+
spawn->base.frontend = &crypto_kpp_type;
103+
return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
104+
}
105+
EXPORT_SYMBOL_GPL(crypto_grab_kpp);
106+
98107
static void kpp_prepare_alg(struct kpp_alg *alg)
99108
{
100109
struct crypto_alg *base = &alg->base;

include/crypto/internal/kpp.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ struct kpp_instance {
2828
};
2929
};
3030

31+
/**
32+
* struct crypto_kpp_spawn - KPP algorithm spawn
33+
* @base: Internal. Generic crypto core spawn state.
34+
*
35+
* Template instances can get a hold on some inner KPP algorithm by
36+
* binding a &struct crypto_kpp_spawn via
37+
* crypto_grab_kpp(). Transforms may subsequently get instantiated
38+
* from the referenced inner &struct kpp_alg by means of
39+
* crypto_spawn_kpp().
40+
*/
41+
struct crypto_kpp_spawn {
42+
struct crypto_spawn base;
43+
};
44+
3145
/*
3246
* Transform internal helpers.
3347
*/
@@ -139,4 +153,65 @@ void crypto_unregister_kpp(struct kpp_alg *alg);
139153
int kpp_register_instance(struct crypto_template *tmpl,
140154
struct kpp_instance *inst);
141155

156+
/*
157+
* KPP spawn related functions.
158+
*/
159+
/**
160+
* crypto_grab_kpp() - Look up a KPP algorithm and bind a spawn to it.
161+
* @spawn: The KPP spawn to bind.
162+
* @inst: The template instance owning @spawn.
163+
* @name: The KPP algorithm name to look up.
164+
* @type: The type bitset to pass on to the lookup.
165+
* @mask: The mask bismask to pass on to the lookup.
166+
* Return: %0 on success, a negative error code otherwise.
167+
*/
168+
int crypto_grab_kpp(struct crypto_kpp_spawn *spawn,
169+
struct crypto_instance *inst,
170+
const char *name, u32 type, u32 mask);
171+
172+
/**
173+
* crypto_drop_kpp() - Release a spawn previously bound via crypto_grab_kpp().
174+
* @spawn: The spawn to release.
175+
*/
176+
static inline void crypto_drop_kpp(struct crypto_kpp_spawn *spawn)
177+
{
178+
crypto_drop_spawn(&spawn->base);
179+
}
180+
181+
/**
182+
* crypto_spawn_kpp_alg() - Get the algorithm a KPP spawn has been bound to.
183+
* @spawn: The spawn to get the referenced &struct kpp_alg for.
184+
*
185+
* This function as well as the returned result are safe to use only
186+
* after @spawn has been successfully bound via crypto_grab_kpp() and
187+
* up to until the template instance owning @spawn has either been
188+
* registered successfully or the spawn has been released again via
189+
* crypto_drop_spawn().
190+
*
191+
* Return: A pointer to the &struct kpp_alg referenced from the spawn.
192+
*/
193+
static inline struct kpp_alg *crypto_spawn_kpp_alg(
194+
struct crypto_kpp_spawn *spawn)
195+
{
196+
return container_of(spawn->base.alg, struct kpp_alg, base);
197+
}
198+
199+
/**
200+
* crypto_spawn_kpp() - Create a transform from a KPP spawn.
201+
* @spawn: The spawn previously bound to some &struct kpp_alg via
202+
* crypto_grab_kpp().
203+
*
204+
* Once a &struct crypto_kpp_spawn has been successfully bound to a
205+
* &struct kpp_alg via crypto_grab_kpp(), transforms for the latter
206+
* may get instantiated from the former by means of this function.
207+
*
208+
* Return: A pointer to the freshly created KPP transform on success
209+
* or an ``ERR_PTR()`` otherwise.
210+
*/
211+
static inline struct crypto_kpp *crypto_spawn_kpp(
212+
struct crypto_kpp_spawn *spawn)
213+
{
214+
return crypto_spawn_tfm2(&spawn->base);
215+
}
216+
142217
#endif

0 commit comments

Comments
 (0)