Skip to content

Commit 7fed0bf

Browse files
committed
[CRYPTO] api: Add common instance initialisation code
This patch adds the helpers crypto_get_attr_alg and crypto_alloc_instance which can be used by simple one-argument templates like hmac to process input parameters and allocate instances. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent df89820 commit 7fed0bf

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎crypto/algapi.c‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/kernel.h>
1717
#include <linux/list.h>
1818
#include <linux/module.h>
19+
#include <linux/rtnetlink.h>
1920
#include <linux/string.h>
2021

2122
#include "internal.h"
@@ -415,6 +416,58 @@ int crypto_unregister_notifier(struct notifier_block *nb)
415416
}
416417
EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
417418

419+
struct crypto_alg *crypto_get_attr_alg(void *param, unsigned int len,
420+
u32 type, u32 mask)
421+
{
422+
struct rtattr *rta = param;
423+
struct crypto_attr_alg *alga;
424+
425+
if (!RTA_OK(rta, len))
426+
return ERR_PTR(-EBADR);
427+
if (rta->rta_type != CRYPTOA_ALG || RTA_PAYLOAD(rta) < sizeof(*alga))
428+
return ERR_PTR(-EINVAL);
429+
430+
alga = RTA_DATA(rta);
431+
alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
432+
433+
return crypto_alg_mod_lookup(alga->name, type, mask);
434+
}
435+
EXPORT_SYMBOL_GPL(crypto_get_attr_alg);
436+
437+
struct crypto_instance *crypto_alloc_instance(const char *name,
438+
struct crypto_alg *alg)
439+
{
440+
struct crypto_instance *inst;
441+
struct crypto_spawn *spawn;
442+
int err;
443+
444+
inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
445+
if (!inst)
446+
return ERR_PTR(-ENOMEM);
447+
448+
err = -ENAMETOOLONG;
449+
if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
450+
alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
451+
goto err_free_inst;
452+
453+
if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
454+
name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
455+
goto err_free_inst;
456+
457+
spawn = crypto_instance_ctx(inst);
458+
err = crypto_init_spawn(spawn, alg, inst);
459+
460+
if (err)
461+
goto err_free_inst;
462+
463+
return inst;
464+
465+
err_free_inst:
466+
kfree(inst);
467+
return ERR_PTR(err);
468+
}
469+
EXPORT_SYMBOL_GPL(crypto_alloc_instance);
470+
418471
static int __init crypto_algapi_init(void)
419472
{
420473
crypto_init_proc();

‎include/crypto/algapi.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
5151
void crypto_drop_spawn(struct crypto_spawn *spawn);
5252
struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn);
5353

54+
struct crypto_alg *crypto_get_attr_alg(void *param, unsigned int len,
55+
u32 type, u32 mask);
56+
struct crypto_instance *crypto_alloc_instance(const char *name,
57+
struct crypto_alg *alg);
58+
5459
static inline void *crypto_instance_ctx(struct crypto_instance *inst)
5560
{
5661
return inst->__ctx;

0 commit comments

Comments
 (0)