1 /*
2  * Key-agreement Protocol Primitives (KPP)
3  *
4  * Copyright (c) 2016, Intel Corporation
5  * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  */
13 
14 #ifndef _CRYPTO_KPP_
15 #define _CRYPTO_KPP_
16 #include <linux/crypto.h>
17 
18 /**
19  * struct kpp_request
20  *
21  * @base:	Common attributes for async crypto requests
22  * @src:	Source data
23  * @dst:	Destination data
24  * @src_len:	Size of the input buffer
25  * @dst_len:	Size of the output buffer. It needs to be at least
26  *		as big as the expected result depending	on the operation
27  *		After operation it will be updated with the actual size of the
28  *		result. In case of error where the dst sgl size was insufficient,
29  *		it will be updated to the size required for the operation.
30  * @__ctx:	Start of private context data
31  */
32 struct kpp_request {
33 	struct crypto_async_request base;
34 	struct scatterlist *src;
35 	struct scatterlist *dst;
36 	unsigned int src_len;
37 	unsigned int dst_len;
38 	void *__ctx[] CRYPTO_MINALIGN_ATTR;
39 };
40 
41 /**
42  * struct crypto_kpp - user-instantiated object which encapsulate
43  * algorithms and core processing logic
44  *
45  * @base:	Common crypto API algorithm data structure
46  */
47 struct crypto_kpp {
48 	struct crypto_tfm base;
49 };
50 
51 /**
52  * struct kpp_alg - generic key-agreement protocol primitives
53  *
54  * @set_secret:		Function invokes the protocol specific function to
55  *			store the secret private key along with parameters.
56  *			The implementation knows how to decode the buffer
57  * @generate_public_key: Function generate the public key to be sent to the
58  *			counterpart. In case of error, where output is not big
59  *			enough req->dst_len will be updated to the size
60  *			required
61  * @compute_shared_secret: Function compute the shared secret as defined by
62  *			the algorithm. The result is given back to the user.
63  *			In case of error, where output is not big enough,
64  *			req->dst_len will be updated to the size required
65  * @max_size:		Function returns the size of the output buffer
66  * @init:		Initialize the object. This is called only once at
67  *			instantiation time. In case the cryptographic hardware
68  *			needs to be initialized. Software fallback should be
69  *			put in place here.
70  * @exit:		Undo everything @init did.
71  *
72  * @reqsize:		Request context size required by algorithm
73  *			implementation
74  * @base:		Common crypto API algorithm data structure
75  */
76 struct kpp_alg {
77 	int (*set_secret)(struct crypto_kpp *tfm, const void *buffer,
78 			  unsigned int len);
79 	int (*generate_public_key)(struct kpp_request *req);
80 	int (*compute_shared_secret)(struct kpp_request *req);
81 
82 	unsigned int (*max_size)(struct crypto_kpp *tfm);
83 
84 	int (*init)(struct crypto_kpp *tfm);
85 	void (*exit)(struct crypto_kpp *tfm);
86 
87 	unsigned int reqsize;
88 	struct crypto_alg base;
89 };
90 
91 /**
92  * DOC: Generic Key-agreement Protocol Primitives API
93  *
94  * The KPP API is used with the algorithm type
95  * CRYPTO_ALG_TYPE_KPP (listed as type "kpp" in /proc/crypto)
96  */
97 
98 /**
99  * crypto_alloc_kpp() - allocate KPP tfm handle
100  * @alg_name: is the name of the kpp algorithm (e.g. "dh", "ecdh")
101  * @type: specifies the type of the algorithm
102  * @mask: specifies the mask for the algorithm
103  *
104  * Allocate a handle for kpp algorithm. The returned struct crypto_kpp
105  * is required for any following API invocation
106  *
107  * Return: allocated handle in case of success; IS_ERR() is true in case of
108  *	   an error, PTR_ERR() returns the error code.
109  */
110 struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask);
111 
crypto_kpp_tfm(struct crypto_kpp * tfm)112 static inline struct crypto_tfm *crypto_kpp_tfm(struct crypto_kpp *tfm)
113 {
114 	return &tfm->base;
115 }
116 
__crypto_kpp_alg(struct crypto_alg * alg)117 static inline struct kpp_alg *__crypto_kpp_alg(struct crypto_alg *alg)
118 {
119 	return container_of(alg, struct kpp_alg, base);
120 }
121 
__crypto_kpp_tfm(struct crypto_tfm * tfm)122 static inline struct crypto_kpp *__crypto_kpp_tfm(struct crypto_tfm *tfm)
123 {
124 	return container_of(tfm, struct crypto_kpp, base);
125 }
126 
crypto_kpp_alg(struct crypto_kpp * tfm)127 static inline struct kpp_alg *crypto_kpp_alg(struct crypto_kpp *tfm)
128 {
129 	return __crypto_kpp_alg(crypto_kpp_tfm(tfm)->__crt_alg);
130 }
131 
crypto_kpp_reqsize(struct crypto_kpp * tfm)132 static inline unsigned int crypto_kpp_reqsize(struct crypto_kpp *tfm)
133 {
134 	return crypto_kpp_alg(tfm)->reqsize;
135 }
136 
kpp_request_set_tfm(struct kpp_request * req,struct crypto_kpp * tfm)137 static inline void kpp_request_set_tfm(struct kpp_request *req,
138 				       struct crypto_kpp *tfm)
139 {
140 	req->base.tfm = crypto_kpp_tfm(tfm);
141 }
142 
crypto_kpp_reqtfm(struct kpp_request * req)143 static inline struct crypto_kpp *crypto_kpp_reqtfm(struct kpp_request *req)
144 {
145 	return __crypto_kpp_tfm(req->base.tfm);
146 }
147 
crypto_kpp_get_flags(struct crypto_kpp * tfm)148 static inline u32 crypto_kpp_get_flags(struct crypto_kpp *tfm)
149 {
150 	return crypto_tfm_get_flags(crypto_kpp_tfm(tfm));
151 }
152 
crypto_kpp_set_flags(struct crypto_kpp * tfm,u32 flags)153 static inline void crypto_kpp_set_flags(struct crypto_kpp *tfm, u32 flags)
154 {
155 	crypto_tfm_set_flags(crypto_kpp_tfm(tfm), flags);
156 }
157 
158 /**
159  * crypto_free_kpp() - free KPP tfm handle
160  *
161  * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
162  *
163  * If @tfm is a NULL or error pointer, this function does nothing.
164  */
crypto_free_kpp(struct crypto_kpp * tfm)165 static inline void crypto_free_kpp(struct crypto_kpp *tfm)
166 {
167 	crypto_destroy_tfm(tfm, crypto_kpp_tfm(tfm));
168 }
169 
170 /**
171  * kpp_request_alloc() - allocates kpp request
172  *
173  * @tfm:	KPP tfm handle allocated with crypto_alloc_kpp()
174  * @gfp:	allocation flags
175  *
176  * Return: allocated handle in case of success or NULL in case of an error.
177  */
kpp_request_alloc(struct crypto_kpp * tfm,gfp_t gfp)178 static inline struct kpp_request *kpp_request_alloc(struct crypto_kpp *tfm,
179 						    gfp_t gfp)
180 {
181 	struct kpp_request *req;
182 
183 	req = kmalloc(sizeof(*req) + crypto_kpp_reqsize(tfm), gfp);
184 	if (likely(req))
185 		kpp_request_set_tfm(req, tfm);
186 
187 	return req;
188 }
189 
190 /**
191  * kpp_request_free() - zeroize and free kpp request
192  *
193  * @req:	request to free
194  */
kpp_request_free(struct kpp_request * req)195 static inline void kpp_request_free(struct kpp_request *req)
196 {
197 	kzfree(req);
198 }
199 
200 /**
201  * kpp_request_set_callback() - Sets an asynchronous callback.
202  *
203  * Callback will be called when an asynchronous operation on a given
204  * request is finished.
205  *
206  * @req:	request that the callback will be set for
207  * @flgs:	specify for instance if the operation may backlog
208  * @cmpl:	callback which will be called
209  * @data:	private data used by the caller
210  */
kpp_request_set_callback(struct kpp_request * req,u32 flgs,crypto_completion_t cmpl,void * data)211 static inline void kpp_request_set_callback(struct kpp_request *req,
212 					    u32 flgs,
213 					    crypto_completion_t cmpl,
214 					    void *data)
215 {
216 	req->base.complete = cmpl;
217 	req->base.data = data;
218 	req->base.flags = flgs;
219 }
220 
221 /**
222  * kpp_request_set_input() - Sets input buffer
223  *
224  * Sets parameters required by generate_public_key
225  *
226  * @req:	kpp request
227  * @input:	ptr to input scatter list
228  * @input_len:	size of the input scatter list
229  */
kpp_request_set_input(struct kpp_request * req,struct scatterlist * input,unsigned int input_len)230 static inline void kpp_request_set_input(struct kpp_request *req,
231 					 struct scatterlist *input,
232 					 unsigned int input_len)
233 {
234 	req->src = input;
235 	req->src_len = input_len;
236 }
237 
238 /**
239  * kpp_request_set_output() - Sets output buffer
240  *
241  * Sets parameters required by kpp operation
242  *
243  * @req:	kpp request
244  * @output:	ptr to output scatter list
245  * @output_len:	size of the output scatter list
246  */
kpp_request_set_output(struct kpp_request * req,struct scatterlist * output,unsigned int output_len)247 static inline void kpp_request_set_output(struct kpp_request *req,
248 					  struct scatterlist *output,
249 					  unsigned int output_len)
250 {
251 	req->dst = output;
252 	req->dst_len = output_len;
253 }
254 
255 enum {
256 	CRYPTO_KPP_SECRET_TYPE_UNKNOWN,
257 	CRYPTO_KPP_SECRET_TYPE_DH,
258 	CRYPTO_KPP_SECRET_TYPE_ECDH,
259 };
260 
261 /**
262  * struct kpp_secret - small header for packing secret buffer
263  *
264  * @type:	define type of secret. Each kpp type will define its own
265  * @len:	specify the len of the secret, include the header, that
266  *		follows the struct
267  */
268 struct kpp_secret {
269 	unsigned short type;
270 	unsigned short len;
271 };
272 
273 /**
274  * crypto_kpp_set_secret() - Invoke kpp operation
275  *
276  * Function invokes the specific kpp operation for a given alg.
277  *
278  * @tfm:	tfm handle
279  * @buffer:	Buffer holding the packet representation of the private
280  *		key. The structure of the packet key depends on the particular
281  *		KPP implementation. Packing and unpacking helpers are provided
282  *		for ECDH and DH (see the respective header files for those
283  *		implementations).
284  * @len:	Length of the packet private key buffer.
285  *
286  * Return: zero on success; error code in case of error
287  */
crypto_kpp_set_secret(struct crypto_kpp * tfm,const void * buffer,unsigned int len)288 static inline int crypto_kpp_set_secret(struct crypto_kpp *tfm,
289 					const void *buffer, unsigned int len)
290 {
291 	struct kpp_alg *alg = crypto_kpp_alg(tfm);
292 
293 	return alg->set_secret(tfm, buffer, len);
294 }
295 
296 /**
297  * crypto_kpp_generate_public_key() - Invoke kpp operation
298  *
299  * Function invokes the specific kpp operation for generating the public part
300  * for a given kpp algorithm.
301  *
302  * To generate a private key, the caller should use a random number generator.
303  * The output of the requested length serves as the private key.
304  *
305  * @req:	kpp key request
306  *
307  * Return: zero on success; error code in case of error
308  */
crypto_kpp_generate_public_key(struct kpp_request * req)309 static inline int crypto_kpp_generate_public_key(struct kpp_request *req)
310 {
311 	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
312 	struct kpp_alg *alg = crypto_kpp_alg(tfm);
313 
314 	return alg->generate_public_key(req);
315 }
316 
317 /**
318  * crypto_kpp_compute_shared_secret() - Invoke kpp operation
319  *
320  * Function invokes the specific kpp operation for computing the shared secret
321  * for a given kpp algorithm.
322  *
323  * @req:	kpp key request
324  *
325  * Return: zero on success; error code in case of error
326  */
crypto_kpp_compute_shared_secret(struct kpp_request * req)327 static inline int crypto_kpp_compute_shared_secret(struct kpp_request *req)
328 {
329 	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
330 	struct kpp_alg *alg = crypto_kpp_alg(tfm);
331 
332 	return alg->compute_shared_secret(req);
333 }
334 
335 /**
336  * crypto_kpp_maxsize() - Get len for output buffer
337  *
338  * Function returns the output buffer size required for a given key.
339  * Function assumes that the key is already set in the transformation. If this
340  * function is called without a setkey or with a failed setkey, you will end up
341  * in a NULL dereference.
342  *
343  * @tfm:	KPP tfm handle allocated with crypto_alloc_kpp()
344  */
crypto_kpp_maxsize(struct crypto_kpp * tfm)345 static inline unsigned int crypto_kpp_maxsize(struct crypto_kpp *tfm)
346 {
347 	struct kpp_alg *alg = crypto_kpp_alg(tfm);
348 
349 	return alg->max_size(tfm);
350 }
351 
352 #endif
353