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