/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* Key-agreement Protocol Primitives (KPP)3*4* Copyright (c) 2016, Intel Corporation5* Authors: Salvatore Benedetto <[email protected]>6*/7#ifndef _CRYPTO_KPP_INT_H8#define _CRYPTO_KPP_INT_H9#include <crypto/kpp.h>10#include <crypto/algapi.h>1112/**13* struct kpp_instance - KPP template instance14* @free: Callback getting invoked upon instance destruction. Must be set.15* @s: Internal. Generic crypto core instance state properly layout16* to alias with @alg as needed.17* @alg: The &struct kpp_alg implementation provided by the instance.18*/19struct kpp_instance {20void (*free)(struct kpp_instance *inst);21union {22struct {23char head[offsetof(struct kpp_alg, base)];24struct crypto_instance base;25} s;26struct kpp_alg alg;27};28};2930/**31* struct crypto_kpp_spawn - KPP algorithm spawn32* @base: Internal. Generic crypto core spawn state.33*34* Template instances can get a hold on some inner KPP algorithm by35* binding a &struct crypto_kpp_spawn via36* crypto_grab_kpp(). Transforms may subsequently get instantiated37* from the referenced inner &struct kpp_alg by means of38* crypto_spawn_kpp().39*/40struct crypto_kpp_spawn {41struct crypto_spawn base;42};4344/*45* Transform internal helpers.46*/47static inline void *kpp_request_ctx(struct kpp_request *req)48{49return req->__ctx;50}5152static inline void *kpp_request_ctx_dma(struct kpp_request *req)53{54unsigned int align = crypto_dma_align();5556if (align <= crypto_tfm_ctx_alignment())57align = 1;5859return PTR_ALIGN(kpp_request_ctx(req), align);60}6162static inline void kpp_set_reqsize(struct crypto_kpp *kpp,63unsigned int reqsize)64{65kpp->reqsize = reqsize;66}6768static inline void kpp_set_reqsize_dma(struct crypto_kpp *kpp,69unsigned int reqsize)70{71reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);72kpp->reqsize = reqsize;73}7475static inline void *kpp_tfm_ctx(struct crypto_kpp *tfm)76{77return crypto_tfm_ctx(&tfm->base);78}7980static inline void *kpp_tfm_ctx_dma(struct crypto_kpp *tfm)81{82return crypto_tfm_ctx_dma(&tfm->base);83}8485static inline void kpp_request_complete(struct kpp_request *req, int err)86{87crypto_request_complete(&req->base, err);88}8990static inline const char *kpp_alg_name(struct crypto_kpp *tfm)91{92return crypto_kpp_tfm(tfm)->__crt_alg->cra_name;93}9495/*96* Template instance internal helpers.97*/98/**99* kpp_crypto_instance() - Cast a &struct kpp_instance to the corresponding100* generic &struct crypto_instance.101* @inst: Pointer to the &struct kpp_instance to be cast.102* Return: A pointer to the &struct crypto_instance embedded in @inst.103*/104static inline struct crypto_instance *kpp_crypto_instance(105struct kpp_instance *inst)106{107return &inst->s.base;108}109110/**111* kpp_instance() - Cast a generic &struct crypto_instance to the corresponding112* &struct kpp_instance.113* @inst: Pointer to the &struct crypto_instance to be cast.114* Return: A pointer to the &struct kpp_instance @inst is embedded in.115*/116static inline struct kpp_instance *kpp_instance(struct crypto_instance *inst)117{118return container_of(inst, struct kpp_instance, s.base);119}120121/**122* kpp_alg_instance() - Get the &struct kpp_instance a given KPP transform has123* been instantiated from.124* @kpp: The KPP transform instantiated from some &struct kpp_instance.125* Return: The &struct kpp_instance associated with @kpp.126*/127static inline struct kpp_instance *kpp_alg_instance(struct crypto_kpp *kpp)128{129return kpp_instance(crypto_tfm_alg_instance(&kpp->base));130}131132/**133* kpp_instance_ctx() - Get a pointer to a &struct kpp_instance's implementation134* specific context data.135* @inst: The &struct kpp_instance whose context data to access.136*137* A KPP template implementation may allocate extra memory beyond the138* end of a &struct kpp_instance instantiated from &crypto_template.create().139* This function provides a means to obtain a pointer to this area.140*141* Return: A pointer to the implementation specific context data.142*/143static inline void *kpp_instance_ctx(struct kpp_instance *inst)144{145return crypto_instance_ctx(kpp_crypto_instance(inst));146}147148/*149* KPP algorithm (un)registration functions.150*/151/**152* crypto_register_kpp() -- Register key-agreement protocol primitives algorithm153*154* Function registers an implementation of a key-agreement protocol primitive155* algorithm156*157* @alg: algorithm definition158*159* Return: zero on success; error code in case of error160*/161int crypto_register_kpp(struct kpp_alg *alg);162163/**164* crypto_unregister_kpp() -- Unregister key-agreement protocol primitive165* algorithm166*167* Function unregisters an implementation of a key-agreement protocol primitive168* algorithm169*170* @alg: algorithm definition171*/172void crypto_unregister_kpp(struct kpp_alg *alg);173174/**175* kpp_register_instance() - Register a KPP template instance.176* @tmpl: The instantiating template.177* @inst: The KPP template instance to be registered.178* Return: %0 on success, negative error code otherwise.179*/180int kpp_register_instance(struct crypto_template *tmpl,181struct kpp_instance *inst);182183/*184* KPP spawn related functions.185*/186/**187* crypto_grab_kpp() - Look up a KPP algorithm and bind a spawn to it.188* @spawn: The KPP spawn to bind.189* @inst: The template instance owning @spawn.190* @name: The KPP algorithm name to look up.191* @type: The type bitset to pass on to the lookup.192* @mask: The mask bismask to pass on to the lookup.193* Return: %0 on success, a negative error code otherwise.194*/195int crypto_grab_kpp(struct crypto_kpp_spawn *spawn,196struct crypto_instance *inst,197const char *name, u32 type, u32 mask);198199/**200* crypto_drop_kpp() - Release a spawn previously bound via crypto_grab_kpp().201* @spawn: The spawn to release.202*/203static inline void crypto_drop_kpp(struct crypto_kpp_spawn *spawn)204{205crypto_drop_spawn(&spawn->base);206}207208/**209* crypto_spawn_kpp_alg() - Get the algorithm a KPP spawn has been bound to.210* @spawn: The spawn to get the referenced &struct kpp_alg for.211*212* This function as well as the returned result are safe to use only213* after @spawn has been successfully bound via crypto_grab_kpp() and214* up to until the template instance owning @spawn has either been215* registered successfully or the spawn has been released again via216* crypto_drop_spawn().217*218* Return: A pointer to the &struct kpp_alg referenced from the spawn.219*/220static inline struct kpp_alg *crypto_spawn_kpp_alg(221struct crypto_kpp_spawn *spawn)222{223return container_of(spawn->base.alg, struct kpp_alg, base);224}225226/**227* crypto_spawn_kpp() - Create a transform from a KPP spawn.228* @spawn: The spawn previously bound to some &struct kpp_alg via229* crypto_grab_kpp().230*231* Once a &struct crypto_kpp_spawn has been successfully bound to a232* &struct kpp_alg via crypto_grab_kpp(), transforms for the latter233* may get instantiated from the former by means of this function.234*235* Return: A pointer to the freshly created KPP transform on success236* or an ``ERR_PTR()`` otherwise.237*/238static inline struct crypto_kpp *crypto_spawn_kpp(239struct crypto_kpp_spawn *spawn)240{241return crypto_spawn_tfm2(&spawn->base);242}243244#endif245246247