xref: /linux/include/crypto/internal/kpp.h (revision 24168c5e6dfbdd5b414f048f47f75d64533296ca)
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_request_ctx_dma(struct kpp_request *req)
54 {
55 	unsigned int align = crypto_dma_align();
56 
57 	if (align <= crypto_tfm_ctx_alignment())
58 		align = 1;
59 
60 	return PTR_ALIGN(kpp_request_ctx(req), align);
61 }
62 
63 static inline void kpp_set_reqsize(struct crypto_kpp *kpp,
64 				   unsigned int reqsize)
65 {
66 	kpp->reqsize = reqsize;
67 }
68 
69 static inline void kpp_set_reqsize_dma(struct crypto_kpp *kpp,
70 				       unsigned int reqsize)
71 {
72 	reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
73 	kpp->reqsize = reqsize;
74 }
75 
76 static inline void *kpp_tfm_ctx(struct crypto_kpp *tfm)
77 {
78 	return crypto_tfm_ctx(&tfm->base);
79 }
80 
81 static inline void *kpp_tfm_ctx_dma(struct crypto_kpp *tfm)
82 {
83 	return crypto_tfm_ctx_dma(&tfm->base);
84 }
85 
86 static inline void kpp_request_complete(struct kpp_request *req, int err)
87 {
88 	crypto_request_complete(&req->base, err);
89 }
90 
91 static inline const char *kpp_alg_name(struct crypto_kpp *tfm)
92 {
93 	return crypto_kpp_tfm(tfm)->__crt_alg->cra_name;
94 }
95 
96 /*
97  * Template instance internal helpers.
98  */
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.
104  */
105 static inline struct crypto_instance *kpp_crypto_instance(
106 	struct kpp_instance *inst)
107 {
108 	return &inst->s.base;
109 }
110 
111 /**
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.
116  */
117 static inline struct kpp_instance *kpp_instance(struct crypto_instance *inst)
118 {
119 	return container_of(inst, struct kpp_instance, s.base);
120 }
121 
122 /**
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.
127  */
128 static inline struct kpp_instance *kpp_alg_instance(struct crypto_kpp *kpp)
129 {
130 	return kpp_instance(crypto_tfm_alg_instance(&kpp->base));
131 }
132 
133 /**
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.
137  *
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.
141  *
142  * Return: A pointer to the implementation specific context data.
143  */
144 static inline void *kpp_instance_ctx(struct kpp_instance *inst)
145 {
146 	return crypto_instance_ctx(kpp_crypto_instance(inst));
147 }
148 
149 /*
150  * KPP algorithm (un)registration functions.
151  */
152 /**
153  * crypto_register_kpp() -- Register key-agreement protocol primitives algorithm
154  *
155  * Function registers an implementation of a key-agreement protocol primitive
156  * algorithm
157  *
158  * @alg:	algorithm definition
159  *
160  * Return: zero on success; error code in case of error
161  */
162 int crypto_register_kpp(struct kpp_alg *alg);
163 
164 /**
165  * crypto_unregister_kpp() -- Unregister key-agreement protocol primitive
166  * algorithm
167  *
168  * Function unregisters an implementation of a key-agreement protocol primitive
169  * algorithm
170  *
171  * @alg:	algorithm definition
172  */
173 void crypto_unregister_kpp(struct kpp_alg *alg);
174 
175 /**
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.
180  */
181 int kpp_register_instance(struct crypto_template *tmpl,
182 			  struct kpp_instance *inst);
183 
184 /*
185  * KPP spawn related functions.
186  */
187 /**
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.
195  */
196 int crypto_grab_kpp(struct crypto_kpp_spawn *spawn,
197 		    struct crypto_instance *inst,
198 		    const char *name, u32 type, u32 mask);
199 
200 /**
201  * crypto_drop_kpp() - Release a spawn previously bound via crypto_grab_kpp().
202  * @spawn: The spawn to release.
203  */
204 static inline void crypto_drop_kpp(struct crypto_kpp_spawn *spawn)
205 {
206 	crypto_drop_spawn(&spawn->base);
207 }
208 
209 /**
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.
212  *
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().
218  *
219  * Return: A pointer to the &struct kpp_alg referenced from the spawn.
220  */
221 static inline struct kpp_alg *crypto_spawn_kpp_alg(
222 	struct crypto_kpp_spawn *spawn)
223 {
224 	return container_of(spawn->base.alg, struct kpp_alg, base);
225 }
226 
227 /**
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().
231  *
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.
235  *
236  * Return: A pointer to the freshly created KPP transform on success
237  * or an ``ERR_PTR()`` otherwise.
238  */
239 static inline struct crypto_kpp *crypto_spawn_kpp(
240 	struct crypto_kpp_spawn *spawn)
241 {
242 	return crypto_spawn_tfm2(&spawn->base);
243 }
244 
245 #endif
246