xref: /openbmc/linux/include/crypto/akcipher.h (revision 0030d7d6)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Public Key Encryption
4  *
5  * Copyright (c) 2015, Intel Corporation
6  * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
7  */
8 #ifndef _CRYPTO_AKCIPHER_H
9 #define _CRYPTO_AKCIPHER_H
10 #include <linux/crypto.h>
11 
12 /**
13  * struct akcipher_request - public key request
14  *
15  * @base:	Common attributes for async crypto requests
16  * @src:	Source data
17  *		For verify op this is signature + digest, in that case
18  *		total size of @src is @src_len + @dst_len.
19  * @dst:	Destination data (Should be NULL for verify op)
20  * @src_len:	Size of the input buffer
21  *		For verify op it's size of signature part of @src, this part
22  *		is supposed to be operated by cipher.
23  * @dst_len:	Size of @dst buffer (for all ops except verify).
24  *		It needs to be at least	as big as the expected result
25  *		depending on the operation.
26  *		After operation it will be updated with the actual size of the
27  *		result.
28  *		In case of error where the dst sgl size was insufficient,
29  *		it will be updated to the size required for the operation.
30  *		For verify op this is size of digest part in @src.
31  * @__ctx:	Start of private context data
32  */
33 struct akcipher_request {
34 	struct crypto_async_request base;
35 	struct scatterlist *src;
36 	struct scatterlist *dst;
37 	unsigned int src_len;
38 	unsigned int dst_len;
39 	void *__ctx[] CRYPTO_MINALIGN_ATTR;
40 };
41 
42 /**
43  * struct crypto_akcipher - user-instantiated objects which encapsulate
44  * algorithms and core processing logic
45  *
46  * @reqsize:	Request context size required by algorithm implementation
47  * @base:	Common crypto API algorithm data structure
48  */
49 struct crypto_akcipher {
50 	unsigned int reqsize;
51 
52 	struct crypto_tfm base;
53 };
54 
55 /**
56  * struct akcipher_alg - generic public key algorithm
57  *
58  * @sign:	Function performs a sign operation as defined by public key
59  *		algorithm. In case of error, where the dst_len was insufficient,
60  *		the req->dst_len will be updated to the size required for the
61  *		operation
62  * @verify:	Function performs a complete verify operation as defined by
63  *		public key algorithm, returning verification status. Requires
64  *		digest value as input parameter.
65  * @encrypt:	Function performs an encrypt operation as defined by public key
66  *		algorithm. In case of error, where the dst_len was insufficient,
67  *		the req->dst_len will be updated to the size required for the
68  *		operation
69  * @decrypt:	Function performs a decrypt operation as defined by public key
70  *		algorithm. In case of error, where the dst_len was insufficient,
71  *		the req->dst_len will be updated to the size required for the
72  *		operation
73  * @set_pub_key: Function invokes the algorithm specific set public key
74  *		function, which knows how to decode and interpret
75  *		the BER encoded public key and parameters
76  * @set_priv_key: Function invokes the algorithm specific set private key
77  *		function, which knows how to decode and interpret
78  *		the BER encoded private key and parameters
79  * @max_size:	Function returns dest buffer size required for a given key.
80  * @init:	Initialize the cryptographic transformation object.
81  *		This function is used to initialize the cryptographic
82  *		transformation object. This function is called only once at
83  *		the instantiation time, right after the transformation context
84  *		was allocated. In case the cryptographic hardware has some
85  *		special requirements which need to be handled by software, this
86  *		function shall check for the precise requirement of the
87  *		transformation and put any software fallbacks in place.
88  * @exit:	Deinitialize the cryptographic transformation object. This is a
89  *		counterpart to @init, used to remove various changes set in
90  *		@init.
91  *
92  * @base:	Common crypto API algorithm data structure
93  */
94 struct akcipher_alg {
95 	int (*sign)(struct akcipher_request *req);
96 	int (*verify)(struct akcipher_request *req);
97 	int (*encrypt)(struct akcipher_request *req);
98 	int (*decrypt)(struct akcipher_request *req);
99 	int (*set_pub_key)(struct crypto_akcipher *tfm, const void *key,
100 			   unsigned int keylen);
101 	int (*set_priv_key)(struct crypto_akcipher *tfm, const void *key,
102 			    unsigned int keylen);
103 	unsigned int (*max_size)(struct crypto_akcipher *tfm);
104 	int (*init)(struct crypto_akcipher *tfm);
105 	void (*exit)(struct crypto_akcipher *tfm);
106 
107 	struct crypto_alg base;
108 };
109 
110 /**
111  * DOC: Generic Public Key API
112  *
113  * The Public Key API is used with the algorithms of type
114  * CRYPTO_ALG_TYPE_AKCIPHER (listed as type "akcipher" in /proc/crypto)
115  */
116 
117 /**
118  * crypto_alloc_akcipher() - allocate AKCIPHER tfm handle
119  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
120  *	      public key algorithm e.g. "rsa"
121  * @type: specifies the type of the algorithm
122  * @mask: specifies the mask for the algorithm
123  *
124  * Allocate a handle for public key algorithm. The returned struct
125  * crypto_akcipher is the handle that is required for any subsequent
126  * API invocation for the public key operations.
127  *
128  * Return: allocated handle in case of success; IS_ERR() is true in case
129  *	   of an error, PTR_ERR() returns the error code.
130  */
131 struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
132 					      u32 mask);
133 
134 static inline struct crypto_tfm *crypto_akcipher_tfm(
135 	struct crypto_akcipher *tfm)
136 {
137 	return &tfm->base;
138 }
139 
140 static inline struct akcipher_alg *__crypto_akcipher_alg(struct crypto_alg *alg)
141 {
142 	return container_of(alg, struct akcipher_alg, base);
143 }
144 
145 static inline struct crypto_akcipher *__crypto_akcipher_tfm(
146 	struct crypto_tfm *tfm)
147 {
148 	return container_of(tfm, struct crypto_akcipher, base);
149 }
150 
151 static inline struct akcipher_alg *crypto_akcipher_alg(
152 	struct crypto_akcipher *tfm)
153 {
154 	return __crypto_akcipher_alg(crypto_akcipher_tfm(tfm)->__crt_alg);
155 }
156 
157 static inline unsigned int crypto_akcipher_reqsize(struct crypto_akcipher *tfm)
158 {
159 	return tfm->reqsize;
160 }
161 
162 static inline void akcipher_request_set_tfm(struct akcipher_request *req,
163 					    struct crypto_akcipher *tfm)
164 {
165 	req->base.tfm = crypto_akcipher_tfm(tfm);
166 }
167 
168 static inline struct crypto_akcipher *crypto_akcipher_reqtfm(
169 	struct akcipher_request *req)
170 {
171 	return __crypto_akcipher_tfm(req->base.tfm);
172 }
173 
174 /**
175  * crypto_free_akcipher() - free AKCIPHER tfm handle
176  *
177  * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
178  *
179  * If @tfm is a NULL or error pointer, this function does nothing.
180  */
181 static inline void crypto_free_akcipher(struct crypto_akcipher *tfm)
182 {
183 	crypto_destroy_tfm(tfm, crypto_akcipher_tfm(tfm));
184 }
185 
186 /**
187  * akcipher_request_alloc() - allocates public key request
188  *
189  * @tfm:	AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
190  * @gfp:	allocation flags
191  *
192  * Return: allocated handle in case of success or NULL in case of an error.
193  */
194 static inline struct akcipher_request *akcipher_request_alloc(
195 	struct crypto_akcipher *tfm, gfp_t gfp)
196 {
197 	struct akcipher_request *req;
198 
199 	req = kmalloc(sizeof(*req) + crypto_akcipher_reqsize(tfm), gfp);
200 	if (likely(req))
201 		akcipher_request_set_tfm(req, tfm);
202 
203 	return req;
204 }
205 
206 /**
207  * akcipher_request_free() - zeroize and free public key request
208  *
209  * @req:	request to free
210  */
211 static inline void akcipher_request_free(struct akcipher_request *req)
212 {
213 	kfree_sensitive(req);
214 }
215 
216 /**
217  * akcipher_request_set_callback() - Sets an asynchronous callback.
218  *
219  * Callback will be called when an asynchronous operation on a given
220  * request is finished.
221  *
222  * @req:	request that the callback will be set for
223  * @flgs:	specify for instance if the operation may backlog
224  * @cmpl:	callback which will be called
225  * @data:	private data used by the caller
226  */
227 static inline void akcipher_request_set_callback(struct akcipher_request *req,
228 						 u32 flgs,
229 						 crypto_completion_t cmpl,
230 						 void *data)
231 {
232 	req->base.complete = cmpl;
233 	req->base.data = data;
234 	req->base.flags = flgs;
235 }
236 
237 /**
238  * akcipher_request_set_crypt() - Sets request parameters
239  *
240  * Sets parameters required by crypto operation
241  *
242  * @req:	public key request
243  * @src:	ptr to input scatter list
244  * @dst:	ptr to output scatter list or NULL for verify op
245  * @src_len:	size of the src input scatter list to be processed
246  * @dst_len:	size of the dst output scatter list or size of signature
247  *		portion in @src for verify op
248  */
249 static inline void akcipher_request_set_crypt(struct akcipher_request *req,
250 					      struct scatterlist *src,
251 					      struct scatterlist *dst,
252 					      unsigned int src_len,
253 					      unsigned int dst_len)
254 {
255 	req->src = src;
256 	req->dst = dst;
257 	req->src_len = src_len;
258 	req->dst_len = dst_len;
259 }
260 
261 /**
262  * crypto_akcipher_maxsize() - Get len for output buffer
263  *
264  * Function returns the dest buffer size required for a given key.
265  * Function assumes that the key is already set in the transformation. If this
266  * function is called without a setkey or with a failed setkey, you will end up
267  * in a NULL dereference.
268  *
269  * @tfm:	AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
270  */
271 static inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher *tfm)
272 {
273 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
274 
275 	return alg->max_size(tfm);
276 }
277 
278 /**
279  * crypto_akcipher_encrypt() - Invoke public key encrypt operation
280  *
281  * Function invokes the specific public key encrypt operation for a given
282  * public key algorithm
283  *
284  * @req:	asymmetric key request
285  *
286  * Return: zero on success; error code in case of error
287  */
288 static inline int crypto_akcipher_encrypt(struct akcipher_request *req)
289 {
290 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
291 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
292 	struct crypto_alg *calg = tfm->base.__crt_alg;
293 	unsigned int src_len = req->src_len;
294 	int ret;
295 
296 	crypto_stats_get(calg);
297 	ret = alg->encrypt(req);
298 	crypto_stats_akcipher_encrypt(src_len, ret, calg);
299 	return ret;
300 }
301 
302 /**
303  * crypto_akcipher_decrypt() - Invoke public key decrypt operation
304  *
305  * Function invokes the specific public key decrypt operation for a given
306  * public key algorithm
307  *
308  * @req:	asymmetric key request
309  *
310  * Return: zero on success; error code in case of error
311  */
312 static inline int crypto_akcipher_decrypt(struct akcipher_request *req)
313 {
314 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
315 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
316 	struct crypto_alg *calg = tfm->base.__crt_alg;
317 	unsigned int src_len = req->src_len;
318 	int ret;
319 
320 	crypto_stats_get(calg);
321 	ret = alg->decrypt(req);
322 	crypto_stats_akcipher_decrypt(src_len, ret, calg);
323 	return ret;
324 }
325 
326 /**
327  * crypto_akcipher_sign() - Invoke public key sign operation
328  *
329  * Function invokes the specific public key sign operation for a given
330  * public key algorithm
331  *
332  * @req:	asymmetric key request
333  *
334  * Return: zero on success; error code in case of error
335  */
336 static inline int crypto_akcipher_sign(struct akcipher_request *req)
337 {
338 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
339 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
340 	struct crypto_alg *calg = tfm->base.__crt_alg;
341 	int ret;
342 
343 	crypto_stats_get(calg);
344 	ret = alg->sign(req);
345 	crypto_stats_akcipher_sign(ret, calg);
346 	return ret;
347 }
348 
349 /**
350  * crypto_akcipher_verify() - Invoke public key signature verification
351  *
352  * Function invokes the specific public key signature verification operation
353  * for a given public key algorithm.
354  *
355  * @req:	asymmetric key request
356  *
357  * Note: req->dst should be NULL, req->src should point to SG of size
358  * (req->src_size + req->dst_size), containing signature (of req->src_size
359  * length) with appended digest (of req->dst_size length).
360  *
361  * Return: zero on verification success; error code in case of error.
362  */
363 static inline int crypto_akcipher_verify(struct akcipher_request *req)
364 {
365 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
366 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
367 	struct crypto_alg *calg = tfm->base.__crt_alg;
368 	int ret;
369 
370 	crypto_stats_get(calg);
371 	ret = alg->verify(req);
372 	crypto_stats_akcipher_verify(ret, calg);
373 	return ret;
374 }
375 
376 /**
377  * crypto_akcipher_set_pub_key() - Invoke set public key operation
378  *
379  * Function invokes the algorithm specific set key function, which knows
380  * how to decode and interpret the encoded key and parameters
381  *
382  * @tfm:	tfm handle
383  * @key:	BER encoded public key, algo OID, paramlen, BER encoded
384  *		parameters
385  * @keylen:	length of the key (not including other data)
386  *
387  * Return: zero on success; error code in case of error
388  */
389 static inline int crypto_akcipher_set_pub_key(struct crypto_akcipher *tfm,
390 					      const void *key,
391 					      unsigned int keylen)
392 {
393 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
394 
395 	return alg->set_pub_key(tfm, key, keylen);
396 }
397 
398 /**
399  * crypto_akcipher_set_priv_key() - Invoke set private key operation
400  *
401  * Function invokes the algorithm specific set key function, which knows
402  * how to decode and interpret the encoded key and parameters
403  *
404  * @tfm:	tfm handle
405  * @key:	BER encoded private key, algo OID, paramlen, BER encoded
406  *		parameters
407  * @keylen:	length of the key (not including other data)
408  *
409  * Return: zero on success; error code in case of error
410  */
411 static inline int crypto_akcipher_set_priv_key(struct crypto_akcipher *tfm,
412 					       const void *key,
413 					       unsigned int keylen)
414 {
415 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
416 
417 	return alg->set_priv_key(tfm, key, keylen);
418 }
419 #endif
420