xref: /openbmc/linux/include/crypto/internal/kpp.h (revision 47010c04)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Key-agreement Protocol Primitives (KPP)
4  *
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>
12 
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.
19  */
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;
28 	};
29 };
30 
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 
45 /*
46  * Transform internal helpers.
47  */
48 static inline void *kpp_request_ctx(struct kpp_request *req)
49 {
50 	return req->__ctx;
51 }
52 
53 static inline void *kpp_tfm_ctx(struct crypto_kpp *tfm)
54 {
55 	return tfm->base.__crt_ctx;
56 }
57 
58 static inline void kpp_request_complete(struct kpp_request *req, int err)
59 {
60 	req->base.complete(&req->base, err);
61 }
62 
63 static inline const char *kpp_alg_name(struct crypto_kpp *tfm)
64 {
65 	return crypto_kpp_tfm(tfm)->__crt_alg->cra_name;
66 }
67 
68 /*
69  * Template instance internal helpers.
70  */
71 /**
72  * kpp_crypto_instance() - Cast a &struct kpp_instance to the corresponding
73  *                         generic &struct crypto_instance.
74  * @inst: Pointer to the &struct kpp_instance to be cast.
75  * Return: A pointer to the &struct crypto_instance embedded in @inst.
76  */
77 static inline struct crypto_instance *kpp_crypto_instance(
78 	struct kpp_instance *inst)
79 {
80 	return &inst->s.base;
81 }
82 
83 /**
84  * kpp_instance() - Cast a generic &struct crypto_instance to the corresponding
85  *                  &struct kpp_instance.
86  * @inst: Pointer to the &struct crypto_instance to be cast.
87  * Return: A pointer to the &struct kpp_instance @inst is embedded in.
88  */
89 static inline struct kpp_instance *kpp_instance(struct crypto_instance *inst)
90 {
91 	return container_of(inst, struct kpp_instance, s.base);
92 }
93 
94 /**
95  * kpp_alg_instance() - Get the &struct kpp_instance a given KPP transform has
96  *                      been instantiated from.
97  * @kpp: The KPP transform instantiated from some &struct kpp_instance.
98  * Return: The &struct kpp_instance associated with @kpp.
99  */
100 static inline struct kpp_instance *kpp_alg_instance(struct crypto_kpp *kpp)
101 {
102 	return kpp_instance(crypto_tfm_alg_instance(&kpp->base));
103 }
104 
105 /**
106  * kpp_instance_ctx() - Get a pointer to a &struct kpp_instance's implementation
107  *                      specific context data.
108  * @inst: The &struct kpp_instance whose context data to access.
109  *
110  * A KPP template implementation may allocate extra memory beyond the
111  * end of a &struct kpp_instance instantiated from &crypto_template.create().
112  * This function provides a means to obtain a pointer to this area.
113  *
114  * Return: A pointer to the implementation specific context data.
115  */
116 static inline void *kpp_instance_ctx(struct kpp_instance *inst)
117 {
118 	return crypto_instance_ctx(kpp_crypto_instance(inst));
119 }
120 
121 /*
122  * KPP algorithm (un)registration functions.
123  */
124 /**
125  * crypto_register_kpp() -- Register key-agreement protocol primitives algorithm
126  *
127  * Function registers an implementation of a key-agreement protocol primitive
128  * algorithm
129  *
130  * @alg:	algorithm definition
131  *
132  * Return: zero on success; error code in case of error
133  */
134 int crypto_register_kpp(struct kpp_alg *alg);
135 
136 /**
137  * crypto_unregister_kpp() -- Unregister key-agreement protocol primitive
138  * algorithm
139  *
140  * Function unregisters an implementation of a key-agreement protocol primitive
141  * algorithm
142  *
143  * @alg:	algorithm definition
144  */
145 void crypto_unregister_kpp(struct kpp_alg *alg);
146 
147 /**
148  * kpp_register_instance() - Register a KPP template instance.
149  * @tmpl: The instantiating template.
150  * @inst: The KPP template instance to be registered.
151  * Return: %0 on success, negative error code otherwise.
152  */
153 int kpp_register_instance(struct crypto_template *tmpl,
154 			  struct kpp_instance *inst);
155 
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 
217 #endif
218