xref: /linux/include/crypto/akcipher.h (revision 93df8a1ed6231727c5db94a80b1a6bd5ee67cec3)
1 /*
2  * Public Key Encryption
3  *
4  * Copyright (c) 2015, Intel Corporation
5  * Authors: Tadeusz Struk <tadeusz.struk@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 #ifndef _CRYPTO_AKCIPHER_H
14 #define _CRYPTO_AKCIPHER_H
15 #include <linux/crypto.h>
16 
17 /**
18  * struct akcipher_request - public key request
19  *
20  * @base:	Common attributes for async crypto requests
21  * @src:	Pointer to memory containing the input parameters
22  *		The format of the parameter(s) is expeted to be Octet String
23  * @dst:	Pointer to memory whare the result will be stored
24  * @src_len:	Size of the input parameter
25  * @dst_len:	Size of the output buffer. It needs to be at leaset
26  *		as big as the expected result depending	on the operation
27  *		After operation it will be updated with the acctual size of the
28  *		result. In case of error, where the dst_len was insufficient,
29  *		it will be updated to the size required for the operation.
30  * @__ctx:	Start of private context data
31  */
32 struct akcipher_request {
33 	struct crypto_async_request base;
34 	void *src;
35 	void *dst;
36 	unsigned int src_len;
37 	unsigned int dst_len;
38 	void *__ctx[] CRYPTO_MINALIGN_ATTR;
39 };
40 
41 /**
42  * struct crypto_akcipher - user-instantiated objects which encapsulate
43  * algorithms and core processing logic
44  *
45  * @base:	Common crypto API algorithm data structure
46  */
47 struct crypto_akcipher {
48 	struct crypto_tfm base;
49 };
50 
51 /**
52  * struct akcipher_alg - generic public key algorithm
53  *
54  * @sign:	Function performs a sign operation as defined by public key
55  *		algorithm. In case of error, where the dst_len was insufficient,
56  *		the req->dst_len will be updated to the size required for the
57  *		operation
58  * @verify:	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  * @encrypt:	Function performs an encrytp operation as defined by public key
63  *		algorithm. In case of error, where the dst_len was insufficient,
64  *		the req->dst_len will be updated to the size required for the
65  *		operation
66  * @decrypt:	Function performs a decrypt operation as defined by public key
67  *		algorithm. In case of error, where the dst_len was insufficient,
68  *		the req->dst_len will be updated to the size required for the
69  *		operation
70  * @setkey:	Function invokes the algorithm specific set key function, which
71  *		knows how to decode and interpret the BER encoded key
72  * @init:	Initialize the cryptographic transformation object.
73  *		This function is used to initialize the cryptographic
74  *		transformation object. This function is called only once at
75  *		the instantiation time, right after the transformation context
76  *		was allocated. In case the cryptographic hardware has some
77  *		special requirements which need to be handled by software, this
78  *		function shall check for the precise requirement of the
79  *		transformation and put any software fallbacks in place.
80  * @exit:	Deinitialize the cryptographic transformation object. This is a
81  *		counterpart to @init, used to remove various changes set in
82  *		@init.
83  *
84  * @reqsize:	Request context size required by algorithm implementation
85  * @base:	Common crypto API algorithm data structure
86  */
87 struct akcipher_alg {
88 	int (*sign)(struct akcipher_request *req);
89 	int (*verify)(struct akcipher_request *req);
90 	int (*encrypt)(struct akcipher_request *req);
91 	int (*decrypt)(struct akcipher_request *req);
92 	int (*setkey)(struct crypto_akcipher *tfm, const void *key,
93 		      unsigned int keylen);
94 	int (*init)(struct crypto_akcipher *tfm);
95 	void (*exit)(struct crypto_akcipher *tfm);
96 
97 	unsigned int reqsize;
98 	struct crypto_alg base;
99 };
100 
101 /**
102  * DOC: Generic Public Key API
103  *
104  * The Public Key API is used with the algorithms of type
105  * CRYPTO_ALG_TYPE_AKCIPHER (listed as type "akcipher" in /proc/crypto)
106  */
107 
108 /**
109  * crypto_alloc_akcipher() -- allocate AKCIPHER tfm handle
110  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
111  *	      public key algorithm e.g. "rsa"
112  * @type: specifies the type of the algorithm
113  * @mask: specifies the mask for the algorithm
114  *
115  * Allocate a handle for public key algorithm. The returned struct
116  * crypto_akcipher is the handle that is required for any subsequent
117  * API invocation for the public key operations.
118  *
119  * Return: allocated handle in case of success; IS_ERR() is true in case
120  *	   of an error, PTR_ERR() returns the error code.
121  */
122 struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
123 					      u32 mask);
124 
125 static inline struct crypto_tfm *crypto_akcipher_tfm(
126 	struct crypto_akcipher *tfm)
127 {
128 	return &tfm->base;
129 }
130 
131 static inline struct akcipher_alg *__crypto_akcipher_alg(struct crypto_alg *alg)
132 {
133 	return container_of(alg, struct akcipher_alg, base);
134 }
135 
136 static inline struct crypto_akcipher *__crypto_akcipher_tfm(
137 	struct crypto_tfm *tfm)
138 {
139 	return container_of(tfm, struct crypto_akcipher, base);
140 }
141 
142 static inline struct akcipher_alg *crypto_akcipher_alg(
143 	struct crypto_akcipher *tfm)
144 {
145 	return __crypto_akcipher_alg(crypto_akcipher_tfm(tfm)->__crt_alg);
146 }
147 
148 static inline unsigned int crypto_akcipher_reqsize(struct crypto_akcipher *tfm)
149 {
150 	return crypto_akcipher_alg(tfm)->reqsize;
151 }
152 
153 static inline void akcipher_request_set_tfm(struct akcipher_request *req,
154 					    struct crypto_akcipher *tfm)
155 {
156 	req->base.tfm = crypto_akcipher_tfm(tfm);
157 }
158 
159 static inline struct crypto_akcipher *crypto_akcipher_reqtfm(
160 	struct akcipher_request *req)
161 {
162 	return __crypto_akcipher_tfm(req->base.tfm);
163 }
164 
165 /**
166  * crypto_free_akcipher() -- free AKCIPHER tfm handle
167  *
168  * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
169  */
170 static inline void crypto_free_akcipher(struct crypto_akcipher *tfm)
171 {
172 	crypto_destroy_tfm(tfm, crypto_akcipher_tfm(tfm));
173 }
174 
175 /**
176  * akcipher_request_alloc() -- allocates public key request
177  *
178  * @tfm:	AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
179  * @gfp:	allocation flags
180  *
181  * Return: allocated handle in case of success or NULL in case of an error.
182  */
183 static inline struct akcipher_request *akcipher_request_alloc(
184 	struct crypto_akcipher *tfm, gfp_t gfp)
185 {
186 	struct akcipher_request *req;
187 
188 	req = kmalloc(sizeof(*req) + crypto_akcipher_reqsize(tfm), gfp);
189 	if (likely(req))
190 		akcipher_request_set_tfm(req, tfm);
191 
192 	return req;
193 }
194 
195 /**
196  * akcipher_request_free() -- zeroize and free public key request
197  *
198  * @req:	request to free
199  */
200 static inline void akcipher_request_free(struct akcipher_request *req)
201 {
202 	kzfree(req);
203 }
204 
205 /**
206  * akcipher_request_set_callback() -- Sets an asynchronous callback.
207  *
208  * Callback will be called when an asynchronous operation on a given
209  * request is finished.
210  *
211  * @req:	request that the callback will be set for
212  * @flgs:	specify for instance if the operation may backlog
213  * @cmlp:	callback which will be called
214  * @data:	private data used by the caller
215  */
216 static inline void akcipher_request_set_callback(struct akcipher_request *req,
217 						 u32 flgs,
218 						 crypto_completion_t cmpl,
219 						 void *data)
220 {
221 	req->base.complete = cmpl;
222 	req->base.data = data;
223 	req->base.flags = flgs;
224 }
225 
226 /**
227  * akcipher_request_set_crypt() -- Sets reqest parameters
228  *
229  * Sets parameters required by crypto operation
230  *
231  * @req:	public key request
232  * @src:	ptr to input parameter
233  * @dst:	ptr of output parameter
234  * @src_len:	size of the input buffer
235  * @dst_len:	size of the output buffer. It will be updated by the
236  *		implementation to reflect the acctual size of the result
237  */
238 static inline void akcipher_request_set_crypt(struct akcipher_request *req,
239 					      void *src, void *dst,
240 					      unsigned int src_len,
241 					      unsigned int dst_len)
242 {
243 	req->src = src;
244 	req->dst = dst;
245 	req->src_len = src_len;
246 	req->dst_len = dst_len;
247 }
248 
249 /**
250  * crypto_akcipher_encrypt() -- Invoke public key encrypt operation
251  *
252  * Function invokes the specific public key encrypt operation for a given
253  * public key algorithm
254  *
255  * @req:	asymmetric key request
256  *
257  * Return: zero on success; error code in case of error
258  */
259 static inline int crypto_akcipher_encrypt(struct akcipher_request *req)
260 {
261 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
262 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
263 
264 	return alg->encrypt(req);
265 }
266 
267 /**
268  * crypto_akcipher_decrypt() -- Invoke public key decrypt operation
269  *
270  * Function invokes the specific public key decrypt operation for a given
271  * public key algorithm
272  *
273  * @req:	asymmetric key request
274  *
275  * Return: zero on success; error code in case of error
276  */
277 static inline int crypto_akcipher_decrypt(struct akcipher_request *req)
278 {
279 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
280 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
281 
282 	return alg->decrypt(req);
283 }
284 
285 /**
286  * crypto_akcipher_sign() -- Invoke public key sign operation
287  *
288  * Function invokes the specific public key sign operation for a given
289  * public key algorithm
290  *
291  * @req:	asymmetric key request
292  *
293  * Return: zero on success; error code in case of error
294  */
295 static inline int crypto_akcipher_sign(struct akcipher_request *req)
296 {
297 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
298 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
299 
300 	return alg->sign(req);
301 }
302 
303 /**
304  * crypto_akcipher_verify() -- Invoke public key verify operation
305  *
306  * Function invokes the specific public key verify operation for a given
307  * public key algorithm
308  *
309  * @req:	asymmetric key request
310  *
311  * Return: zero on success; error code in case of error
312  */
313 static inline int crypto_akcipher_verify(struct akcipher_request *req)
314 {
315 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
316 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
317 
318 	return alg->verify(req);
319 }
320 
321 /**
322  * crypto_akcipher_setkey() -- Invoke public key setkey operation
323  *
324  * Function invokes the algorithm specific set key function, which knows
325  * how to decode and interpret the encoded key
326  *
327  * @tfm:	tfm handle
328  * @key:	BER encoded private or public key
329  * @keylen:	length of the key
330  *
331  * Return: zero on success; error code in case of error
332  */
333 static inline int crypto_akcipher_setkey(struct crypto_akcipher *tfm, void *key,
334 					 unsigned int keylen)
335 {
336 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
337 
338 	return alg->setkey(tfm, key, keylen);
339 }
340 #endif
341