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