xref: /linux/include/crypto/internal/kpp.h (revision 3d780c8a9850ad60dee47a8d971ba7888f3d1bd3)
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_set_reqsize(struct crypto_kpp *kpp,
54 				   unsigned int reqsize)
55 {
56 	kpp->reqsize = reqsize;
57 }
58 
59 static inline void *kpp_tfm_ctx(struct crypto_kpp *tfm)
60 {
61 	return tfm->base.__crt_ctx;
62 }
63 
64 static inline void kpp_request_complete(struct kpp_request *req, int err)
65 {
66 	req->base.complete(&req->base, err);
67 }
68 
69 static inline const char *kpp_alg_name(struct crypto_kpp *tfm)
70 {
71 	return crypto_kpp_tfm(tfm)->__crt_alg->cra_name;
72 }
73 
74 /*
75  * Template instance internal helpers.
76  */
77 /**
78  * kpp_crypto_instance() - Cast a &struct kpp_instance to the corresponding
79  *                         generic &struct crypto_instance.
80  * @inst: Pointer to the &struct kpp_instance to be cast.
81  * Return: A pointer to the &struct crypto_instance embedded in @inst.
82  */
83 static inline struct crypto_instance *kpp_crypto_instance(
84 	struct kpp_instance *inst)
85 {
86 	return &inst->s.base;
87 }
88 
89 /**
90  * kpp_instance() - Cast a generic &struct crypto_instance to the corresponding
91  *                  &struct kpp_instance.
92  * @inst: Pointer to the &struct crypto_instance to be cast.
93  * Return: A pointer to the &struct kpp_instance @inst is embedded in.
94  */
95 static inline struct kpp_instance *kpp_instance(struct crypto_instance *inst)
96 {
97 	return container_of(inst, struct kpp_instance, s.base);
98 }
99 
100 /**
101  * kpp_alg_instance() - Get the &struct kpp_instance a given KPP transform has
102  *                      been instantiated from.
103  * @kpp: The KPP transform instantiated from some &struct kpp_instance.
104  * Return: The &struct kpp_instance associated with @kpp.
105  */
106 static inline struct kpp_instance *kpp_alg_instance(struct crypto_kpp *kpp)
107 {
108 	return kpp_instance(crypto_tfm_alg_instance(&kpp->base));
109 }
110 
111 /**
112  * kpp_instance_ctx() - Get a pointer to a &struct kpp_instance's implementation
113  *                      specific context data.
114  * @inst: The &struct kpp_instance whose context data to access.
115  *
116  * A KPP template implementation may allocate extra memory beyond the
117  * end of a &struct kpp_instance instantiated from &crypto_template.create().
118  * This function provides a means to obtain a pointer to this area.
119  *
120  * Return: A pointer to the implementation specific context data.
121  */
122 static inline void *kpp_instance_ctx(struct kpp_instance *inst)
123 {
124 	return crypto_instance_ctx(kpp_crypto_instance(inst));
125 }
126 
127 /*
128  * KPP algorithm (un)registration functions.
129  */
130 /**
131  * crypto_register_kpp() -- Register key-agreement protocol primitives algorithm
132  *
133  * Function registers an implementation of a key-agreement protocol primitive
134  * algorithm
135  *
136  * @alg:	algorithm definition
137  *
138  * Return: zero on success; error code in case of error
139  */
140 int crypto_register_kpp(struct kpp_alg *alg);
141 
142 /**
143  * crypto_unregister_kpp() -- Unregister key-agreement protocol primitive
144  * algorithm
145  *
146  * Function unregisters an implementation of a key-agreement protocol primitive
147  * algorithm
148  *
149  * @alg:	algorithm definition
150  */
151 void crypto_unregister_kpp(struct kpp_alg *alg);
152 
153 /**
154  * kpp_register_instance() - Register a KPP template instance.
155  * @tmpl: The instantiating template.
156  * @inst: The KPP template instance to be registered.
157  * Return: %0 on success, negative error code otherwise.
158  */
159 int kpp_register_instance(struct crypto_template *tmpl,
160 			  struct kpp_instance *inst);
161 
162 /*
163  * KPP spawn related functions.
164  */
165 /**
166  * crypto_grab_kpp() - Look up a KPP algorithm and bind a spawn to it.
167  * @spawn: The KPP spawn to bind.
168  * @inst: The template instance owning @spawn.
169  * @name: The KPP algorithm name to look up.
170  * @type: The type bitset to pass on to the lookup.
171  * @mask: The mask bismask to pass on to the lookup.
172  * Return: %0 on success, a negative error code otherwise.
173  */
174 int crypto_grab_kpp(struct crypto_kpp_spawn *spawn,
175 		    struct crypto_instance *inst,
176 		    const char *name, u32 type, u32 mask);
177 
178 /**
179  * crypto_drop_kpp() - Release a spawn previously bound via crypto_grab_kpp().
180  * @spawn: The spawn to release.
181  */
182 static inline void crypto_drop_kpp(struct crypto_kpp_spawn *spawn)
183 {
184 	crypto_drop_spawn(&spawn->base);
185 }
186 
187 /**
188  * crypto_spawn_kpp_alg() - Get the algorithm a KPP spawn has been bound to.
189  * @spawn: The spawn to get the referenced &struct kpp_alg for.
190  *
191  * This function as well as the returned result are safe to use only
192  * after @spawn has been successfully bound via crypto_grab_kpp() and
193  * up to until the template instance owning @spawn has either been
194  * registered successfully or the spawn has been released again via
195  * crypto_drop_spawn().
196  *
197  * Return: A pointer to the &struct kpp_alg referenced from the spawn.
198  */
199 static inline struct kpp_alg *crypto_spawn_kpp_alg(
200 	struct crypto_kpp_spawn *spawn)
201 {
202 	return container_of(spawn->base.alg, struct kpp_alg, base);
203 }
204 
205 /**
206  * crypto_spawn_kpp() - Create a transform from a KPP spawn.
207  * @spawn: The spawn previously bound to some &struct kpp_alg via
208  *         crypto_grab_kpp().
209  *
210  * Once a &struct crypto_kpp_spawn has been successfully bound to a
211  * &struct kpp_alg via crypto_grab_kpp(), transforms for the latter
212  * may get instantiated from the former by means of this function.
213  *
214  * Return: A pointer to the freshly created KPP transform on success
215  * or an ``ERR_PTR()`` otherwise.
216  */
217 static inline struct crypto_kpp *crypto_spawn_kpp(
218 	struct crypto_kpp_spawn *spawn)
219 {
220 	return crypto_spawn_tfm2(&spawn->base);
221 }
222 
223 #endif
224