Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / include / crypto / internal / kpp.h
blob0a6db8c4a9a0f9c5e5323a90f6072fba5c127537
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Key-agreement Protocol Primitives (KPP)
5 * Copyright (c) 2016, Intel Corporation
6 * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
7 */
8 #ifndef _CRYPTO_KPP_INT_H
9 #define _CRYPTO_KPP_INT_H
10 #include <crypto/kpp.h>
11 #include <crypto/algapi.h>
13 /**
14 * struct kpp_instance - KPP template instance
15 * @free: Callback getting invoked upon instance destruction. Must be set.
16 * @s: Internal. Generic crypto core instance state properly layout
17 * to alias with @alg as needed.
18 * @alg: The &struct kpp_alg implementation provided by the instance.
20 struct kpp_instance {
21 void (*free)(struct kpp_instance *inst);
22 union {
23 struct {
24 char head[offsetof(struct kpp_alg, base)];
25 struct crypto_instance base;
26 } s;
27 struct kpp_alg alg;
31 /**
32 * struct crypto_kpp_spawn - KPP algorithm spawn
33 * @base: Internal. Generic crypto core spawn state.
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().
41 struct crypto_kpp_spawn {
42 struct crypto_spawn base;
46 * Transform internal helpers.
48 static inline void *kpp_request_ctx(struct kpp_request *req)
50 return req->__ctx;
53 static inline void *kpp_request_ctx_dma(struct kpp_request *req)
55 unsigned int align = crypto_dma_align();
57 if (align <= crypto_tfm_ctx_alignment())
58 align = 1;
60 return PTR_ALIGN(kpp_request_ctx(req), align);
63 static inline void kpp_set_reqsize(struct crypto_kpp *kpp,
64 unsigned int reqsize)
66 kpp->reqsize = reqsize;
69 static inline void kpp_set_reqsize_dma(struct crypto_kpp *kpp,
70 unsigned int reqsize)
72 reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
73 kpp->reqsize = reqsize;
76 static inline void *kpp_tfm_ctx(struct crypto_kpp *tfm)
78 return crypto_tfm_ctx(&tfm->base);
81 static inline void *kpp_tfm_ctx_dma(struct crypto_kpp *tfm)
83 return crypto_tfm_ctx_dma(&tfm->base);
86 static inline void kpp_request_complete(struct kpp_request *req, int err)
88 crypto_request_complete(&req->base, err);
91 static inline const char *kpp_alg_name(struct crypto_kpp *tfm)
93 return crypto_kpp_tfm(tfm)->__crt_alg->cra_name;
97 * Template instance internal helpers.
99 /**
100 * kpp_crypto_instance() - Cast a &struct kpp_instance to the corresponding
101 * generic &struct crypto_instance.
102 * @inst: Pointer to the &struct kpp_instance to be cast.
103 * Return: A pointer to the &struct crypto_instance embedded in @inst.
105 static inline struct crypto_instance *kpp_crypto_instance(
106 struct kpp_instance *inst)
108 return &inst->s.base;
112 * kpp_instance() - Cast a generic &struct crypto_instance to the corresponding
113 * &struct kpp_instance.
114 * @inst: Pointer to the &struct crypto_instance to be cast.
115 * Return: A pointer to the &struct kpp_instance @inst is embedded in.
117 static inline struct kpp_instance *kpp_instance(struct crypto_instance *inst)
119 return container_of(inst, struct kpp_instance, s.base);
123 * kpp_alg_instance() - Get the &struct kpp_instance a given KPP transform has
124 * been instantiated from.
125 * @kpp: The KPP transform instantiated from some &struct kpp_instance.
126 * Return: The &struct kpp_instance associated with @kpp.
128 static inline struct kpp_instance *kpp_alg_instance(struct crypto_kpp *kpp)
130 return kpp_instance(crypto_tfm_alg_instance(&kpp->base));
134 * kpp_instance_ctx() - Get a pointer to a &struct kpp_instance's implementation
135 * specific context data.
136 * @inst: The &struct kpp_instance whose context data to access.
138 * A KPP template implementation may allocate extra memory beyond the
139 * end of a &struct kpp_instance instantiated from &crypto_template.create().
140 * This function provides a means to obtain a pointer to this area.
142 * Return: A pointer to the implementation specific context data.
144 static inline void *kpp_instance_ctx(struct kpp_instance *inst)
146 return crypto_instance_ctx(kpp_crypto_instance(inst));
150 * KPP algorithm (un)registration functions.
153 * crypto_register_kpp() -- Register key-agreement protocol primitives algorithm
155 * Function registers an implementation of a key-agreement protocol primitive
156 * algorithm
158 * @alg: algorithm definition
160 * Return: zero on success; error code in case of error
162 int crypto_register_kpp(struct kpp_alg *alg);
165 * crypto_unregister_kpp() -- Unregister key-agreement protocol primitive
166 * algorithm
168 * Function unregisters an implementation of a key-agreement protocol primitive
169 * algorithm
171 * @alg: algorithm definition
173 void crypto_unregister_kpp(struct kpp_alg *alg);
176 * kpp_register_instance() - Register a KPP template instance.
177 * @tmpl: The instantiating template.
178 * @inst: The KPP template instance to be registered.
179 * Return: %0 on success, negative error code otherwise.
181 int kpp_register_instance(struct crypto_template *tmpl,
182 struct kpp_instance *inst);
185 * KPP spawn related functions.
188 * crypto_grab_kpp() - Look up a KPP algorithm and bind a spawn to it.
189 * @spawn: The KPP spawn to bind.
190 * @inst: The template instance owning @spawn.
191 * @name: The KPP algorithm name to look up.
192 * @type: The type bitset to pass on to the lookup.
193 * @mask: The mask bismask to pass on to the lookup.
194 * Return: %0 on success, a negative error code otherwise.
196 int crypto_grab_kpp(struct crypto_kpp_spawn *spawn,
197 struct crypto_instance *inst,
198 const char *name, u32 type, u32 mask);
201 * crypto_drop_kpp() - Release a spawn previously bound via crypto_grab_kpp().
202 * @spawn: The spawn to release.
204 static inline void crypto_drop_kpp(struct crypto_kpp_spawn *spawn)
206 crypto_drop_spawn(&spawn->base);
210 * crypto_spawn_kpp_alg() - Get the algorithm a KPP spawn has been bound to.
211 * @spawn: The spawn to get the referenced &struct kpp_alg for.
213 * This function as well as the returned result are safe to use only
214 * after @spawn has been successfully bound via crypto_grab_kpp() and
215 * up to until the template instance owning @spawn has either been
216 * registered successfully or the spawn has been released again via
217 * crypto_drop_spawn().
219 * Return: A pointer to the &struct kpp_alg referenced from the spawn.
221 static inline struct kpp_alg *crypto_spawn_kpp_alg(
222 struct crypto_kpp_spawn *spawn)
224 return container_of(spawn->base.alg, struct kpp_alg, base);
228 * crypto_spawn_kpp() - Create a transform from a KPP spawn.
229 * @spawn: The spawn previously bound to some &struct kpp_alg via
230 * crypto_grab_kpp().
232 * Once a &struct crypto_kpp_spawn has been successfully bound to a
233 * &struct kpp_alg via crypto_grab_kpp(), transforms for the latter
234 * may get instantiated from the former by means of this function.
236 * Return: A pointer to the freshly created KPP transform on success
237 * or an ``ERR_PTR()`` otherwise.
239 static inline struct crypto_kpp *crypto_spawn_kpp(
240 struct crypto_kpp_spawn *spawn)
242 return crypto_spawn_tfm2(&spawn->base);
245 #endif