blob: 1d3aa252cabafba10df19f4128d3718c9a106cc2 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Tadeusz Struk3c339ab2015-06-16 10:30:55 -07002/*
3 * Public Key Encryption
4 *
5 * Copyright (c) 2015, Intel Corporation
6 * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
Tadeusz Struk3c339ab2015-06-16 10:30:55 -07007 */
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
Tadeusz Struk22287b02015-10-08 09:26:55 -070016 * @src: Source data
Vitaly Chikunovc7381b02019-04-11 18:51:15 +030017 * 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)
Tadeusz Struk22287b02015-10-08 09:26:55 -070020 * @src_len: Size of the input buffer
Vitaly Chikunovc7381b02019-04-11 18:51:15 +030021 * 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.
LABBE Corentine14a1f12015-12-08 09:00:23 +010026 * After operation it will be updated with the actual size of the
Tadeusz Struk22287b02015-10-08 09:26:55 -070027 * result.
28 * In case of error where the dst sgl size was insufficient,
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070029 * it will be updated to the size required for the operation.
Vitaly Chikunovc7381b02019-04-11 18:51:15 +030030 * For verify op this is size of digest part in @src.
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070031 * @__ctx: Start of private context data
32 */
33struct akcipher_request {
34 struct crypto_async_request base;
Tadeusz Struk22287b02015-10-08 09:26:55 -070035 struct scatterlist *src;
36 struct scatterlist *dst;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070037 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 * @base: Common crypto API algorithm data structure
47 */
48struct crypto_akcipher {
49 struct crypto_tfm base;
50};
51
52/**
53 * struct akcipher_alg - generic public key algorithm
54 *
55 * @sign: Function performs a sign operation as defined by public key
56 * algorithm. In case of error, where the dst_len was insufficient,
57 * the req->dst_len will be updated to the size required for the
58 * operation
Vitaly Chikunovc7381b02019-04-11 18:51:15 +030059 * @verify: Function performs a complete verify operation as defined by
60 * public key algorithm, returning verification status. Requires
61 * digest value as input parameter.
LABBE Corentine14a1f12015-12-08 09:00:23 +010062 * @encrypt: Function performs an encrypt operation as defined by public key
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070063 * 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
Tadeusz Struk22287b02015-10-08 09:26:55 -070070 * @set_pub_key: Function invokes the algorithm specific set public key
71 * function, which knows how to decode and interpret
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +030072 * the BER encoded public key and parameters
Tadeusz Struk22287b02015-10-08 09:26:55 -070073 * @set_priv_key: Function invokes the algorithm specific set private key
74 * function, which knows how to decode and interpret
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +030075 * the BER encoded private key and parameters
LABBE Corentine14a1f12015-12-08 09:00:23 +010076 * @max_size: Function returns dest buffer size required for a given key.
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070077 * @init: Initialize the cryptographic transformation object.
78 * This function is used to initialize the cryptographic
79 * transformation object. This function is called only once at
80 * the instantiation time, right after the transformation context
81 * was allocated. In case the cryptographic hardware has some
82 * special requirements which need to be handled by software, this
83 * function shall check for the precise requirement of the
84 * transformation and put any software fallbacks in place.
85 * @exit: Deinitialize the cryptographic transformation object. This is a
86 * counterpart to @init, used to remove various changes set in
87 * @init.
88 *
89 * @reqsize: Request context size required by algorithm implementation
90 * @base: Common crypto API algorithm data structure
91 */
92struct akcipher_alg {
93 int (*sign)(struct akcipher_request *req);
94 int (*verify)(struct akcipher_request *req);
95 int (*encrypt)(struct akcipher_request *req);
96 int (*decrypt)(struct akcipher_request *req);
Tadeusz Struk22287b02015-10-08 09:26:55 -070097 int (*set_pub_key)(struct crypto_akcipher *tfm, const void *key,
98 unsigned int keylen);
99 int (*set_priv_key)(struct crypto_akcipher *tfm, const void *key,
100 unsigned int keylen);
Tudor-Dan Ambarus561f8e22017-05-25 10:18:12 +0300101 unsigned int (*max_size)(struct crypto_akcipher *tfm);
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700102 int (*init)(struct crypto_akcipher *tfm);
103 void (*exit)(struct crypto_akcipher *tfm);
104
105 unsigned int reqsize;
106 struct crypto_alg base;
107};
108
109/**
110 * DOC: Generic Public Key API
111 *
112 * The Public Key API is used with the algorithms of type
113 * CRYPTO_ALG_TYPE_AKCIPHER (listed as type "akcipher" in /proc/crypto)
114 */
115
116/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100117 * crypto_alloc_akcipher() - allocate AKCIPHER tfm handle
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700118 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
119 * public key algorithm e.g. "rsa"
120 * @type: specifies the type of the algorithm
121 * @mask: specifies the mask for the algorithm
122 *
123 * Allocate a handle for public key algorithm. The returned struct
124 * crypto_akcipher is the handle that is required for any subsequent
125 * API invocation for the public key operations.
126 *
127 * Return: allocated handle in case of success; IS_ERR() is true in case
128 * of an error, PTR_ERR() returns the error code.
129 */
130struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
131 u32 mask);
132
133static inline struct crypto_tfm *crypto_akcipher_tfm(
134 struct crypto_akcipher *tfm)
135{
136 return &tfm->base;
137}
138
139static inline struct akcipher_alg *__crypto_akcipher_alg(struct crypto_alg *alg)
140{
141 return container_of(alg, struct akcipher_alg, base);
142}
143
144static inline struct crypto_akcipher *__crypto_akcipher_tfm(
145 struct crypto_tfm *tfm)
146{
147 return container_of(tfm, struct crypto_akcipher, base);
148}
149
150static inline struct akcipher_alg *crypto_akcipher_alg(
151 struct crypto_akcipher *tfm)
152{
153 return __crypto_akcipher_alg(crypto_akcipher_tfm(tfm)->__crt_alg);
154}
155
156static inline unsigned int crypto_akcipher_reqsize(struct crypto_akcipher *tfm)
157{
158 return crypto_akcipher_alg(tfm)->reqsize;
159}
160
161static inline void akcipher_request_set_tfm(struct akcipher_request *req,
162 struct crypto_akcipher *tfm)
163{
164 req->base.tfm = crypto_akcipher_tfm(tfm);
165}
166
167static inline struct crypto_akcipher *crypto_akcipher_reqtfm(
168 struct akcipher_request *req)
169{
170 return __crypto_akcipher_tfm(req->base.tfm);
171}
172
173/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100174 * crypto_free_akcipher() - free AKCIPHER tfm handle
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700175 *
176 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
177 */
178static inline void crypto_free_akcipher(struct crypto_akcipher *tfm)
179{
180 crypto_destroy_tfm(tfm, crypto_akcipher_tfm(tfm));
181}
182
183/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100184 * akcipher_request_alloc() - allocates public key request
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700185 *
186 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
187 * @gfp: allocation flags
188 *
189 * Return: allocated handle in case of success or NULL in case of an error.
190 */
191static inline struct akcipher_request *akcipher_request_alloc(
192 struct crypto_akcipher *tfm, gfp_t gfp)
193{
194 struct akcipher_request *req;
195
196 req = kmalloc(sizeof(*req) + crypto_akcipher_reqsize(tfm), gfp);
197 if (likely(req))
198 akcipher_request_set_tfm(req, tfm);
199
200 return req;
201}
202
203/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100204 * akcipher_request_free() - zeroize and free public key request
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700205 *
206 * @req: request to free
207 */
208static inline void akcipher_request_free(struct akcipher_request *req)
209{
Waiman Long453431a2020-08-06 23:18:13 -0700210 kfree_sensitive(req);
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700211}
212
213/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100214 * akcipher_request_set_callback() - Sets an asynchronous callback.
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700215 *
216 * Callback will be called when an asynchronous operation on a given
217 * request is finished.
218 *
219 * @req: request that the callback will be set for
220 * @flgs: specify for instance if the operation may backlog
Stephan Muellere1eabc02016-02-16 11:32:06 +0100221 * @cmpl: callback which will be called
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700222 * @data: private data used by the caller
223 */
224static inline void akcipher_request_set_callback(struct akcipher_request *req,
225 u32 flgs,
226 crypto_completion_t cmpl,
227 void *data)
228{
229 req->base.complete = cmpl;
230 req->base.data = data;
231 req->base.flags = flgs;
232}
233
234/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100235 * akcipher_request_set_crypt() - Sets request parameters
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700236 *
237 * Sets parameters required by crypto operation
238 *
239 * @req: public key request
Tadeusz Struk22287b02015-10-08 09:26:55 -0700240 * @src: ptr to input scatter list
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300241 * @dst: ptr to output scatter list or NULL for verify op
Tadeusz Struk22287b02015-10-08 09:26:55 -0700242 * @src_len: size of the src input scatter list to be processed
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300243 * @dst_len: size of the dst output scatter list or size of signature
244 * portion in @src for verify op
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700245 */
246static inline void akcipher_request_set_crypt(struct akcipher_request *req,
Tadeusz Struk22287b02015-10-08 09:26:55 -0700247 struct scatterlist *src,
248 struct scatterlist *dst,
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700249 unsigned int src_len,
250 unsigned int dst_len)
251{
252 req->src = src;
253 req->dst = dst;
254 req->src_len = src_len;
255 req->dst_len = dst_len;
256}
257
258/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100259 * crypto_akcipher_maxsize() - Get len for output buffer
Tadeusz Struk22287b02015-10-08 09:26:55 -0700260 *
Tudor-Dan Ambarus561f8e22017-05-25 10:18:12 +0300261 * Function returns the dest buffer size required for a given key.
262 * Function assumes that the key is already set in the transformation. If this
263 * function is called without a setkey or with a failed setkey, you will end up
264 * in a NULL dereference.
Tadeusz Struk22287b02015-10-08 09:26:55 -0700265 *
266 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
Tadeusz Struk22287b02015-10-08 09:26:55 -0700267 */
Tudor-Dan Ambarus561f8e22017-05-25 10:18:12 +0300268static inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher *tfm)
Tadeusz Struk22287b02015-10-08 09:26:55 -0700269{
270 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
271
272 return alg->max_size(tfm);
273}
274
275/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100276 * crypto_akcipher_encrypt() - Invoke public key encrypt operation
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700277 *
278 * Function invokes the specific public key encrypt operation for a given
279 * public key algorithm
280 *
281 * @req: asymmetric key request
282 *
283 * Return: zero on success; error code in case of error
284 */
285static inline int crypto_akcipher_encrypt(struct akcipher_request *req)
286{
287 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
288 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000289 struct crypto_alg *calg = tfm->base.__crt_alg;
290 unsigned int src_len = req->src_len;
Corentin Labbecac58182018-09-19 10:10:54 +0000291 int ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700292
Corentin Labbef7d76e02018-11-29 14:42:21 +0000293 crypto_stats_get(calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000294 ret = alg->encrypt(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000295 crypto_stats_akcipher_encrypt(src_len, ret, calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000296 return ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700297}
298
299/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100300 * crypto_akcipher_decrypt() - Invoke public key decrypt operation
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700301 *
302 * Function invokes the specific public key decrypt operation for a given
303 * public key algorithm
304 *
305 * @req: asymmetric key request
306 *
307 * Return: zero on success; error code in case of error
308 */
309static inline int crypto_akcipher_decrypt(struct akcipher_request *req)
310{
311 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
312 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000313 struct crypto_alg *calg = tfm->base.__crt_alg;
314 unsigned int src_len = req->src_len;
Corentin Labbecac58182018-09-19 10:10:54 +0000315 int ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700316
Corentin Labbef7d76e02018-11-29 14:42:21 +0000317 crypto_stats_get(calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000318 ret = alg->decrypt(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000319 crypto_stats_akcipher_decrypt(src_len, ret, calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000320 return ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700321}
322
323/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100324 * crypto_akcipher_sign() - Invoke public key sign operation
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700325 *
326 * Function invokes the specific public key sign operation for a given
327 * public key algorithm
328 *
329 * @req: asymmetric key request
330 *
331 * Return: zero on success; error code in case of error
332 */
333static inline int crypto_akcipher_sign(struct akcipher_request *req)
334{
335 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
336 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000337 struct crypto_alg *calg = tfm->base.__crt_alg;
Corentin Labbecac58182018-09-19 10:10:54 +0000338 int ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700339
Corentin Labbef7d76e02018-11-29 14:42:21 +0000340 crypto_stats_get(calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000341 ret = alg->sign(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000342 crypto_stats_akcipher_sign(ret, calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000343 return ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700344}
345
346/**
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300347 * crypto_akcipher_verify() - Invoke public key signature verification
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700348 *
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300349 * Function invokes the specific public key signature verification operation
350 * for a given public key algorithm.
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700351 *
352 * @req: asymmetric key request
353 *
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300354 * Note: req->dst should be NULL, req->src should point to SG of size
355 * (req->src_size + req->dst_size), containing signature (of req->src_size
356 * length) with appended digest (of req->dst_size length).
357 *
358 * Return: zero on verification success; error code in case of error.
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700359 */
360static inline int crypto_akcipher_verify(struct akcipher_request *req)
361{
362 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
363 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000364 struct crypto_alg *calg = tfm->base.__crt_alg;
Corentin Labbecac58182018-09-19 10:10:54 +0000365 int ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700366
Corentin Labbef7d76e02018-11-29 14:42:21 +0000367 crypto_stats_get(calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000368 ret = alg->verify(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000369 crypto_stats_akcipher_verify(ret, calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000370 return ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700371}
372
373/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100374 * crypto_akcipher_set_pub_key() - Invoke set public key operation
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700375 *
376 * Function invokes the algorithm specific set key function, which knows
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300377 * how to decode and interpret the encoded key and parameters
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700378 *
379 * @tfm: tfm handle
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300380 * @key: BER encoded public key, algo OID, paramlen, BER encoded
381 * parameters
382 * @keylen: length of the key (not including other data)
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700383 *
384 * Return: zero on success; error code in case of error
385 */
Tadeusz Struk22287b02015-10-08 09:26:55 -0700386static inline int crypto_akcipher_set_pub_key(struct crypto_akcipher *tfm,
387 const void *key,
388 unsigned int keylen)
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700389{
390 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
391
Tadeusz Struk22287b02015-10-08 09:26:55 -0700392 return alg->set_pub_key(tfm, key, keylen);
393}
394
395/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100396 * crypto_akcipher_set_priv_key() - Invoke set private key operation
Tadeusz Struk22287b02015-10-08 09:26:55 -0700397 *
398 * Function invokes the algorithm specific set key function, which knows
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300399 * how to decode and interpret the encoded key and parameters
Tadeusz Struk22287b02015-10-08 09:26:55 -0700400 *
401 * @tfm: tfm handle
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300402 * @key: BER encoded private key, algo OID, paramlen, BER encoded
403 * parameters
404 * @keylen: length of the key (not including other data)
Tadeusz Struk22287b02015-10-08 09:26:55 -0700405 *
406 * Return: zero on success; error code in case of error
407 */
408static inline int crypto_akcipher_set_priv_key(struct crypto_akcipher *tfm,
409 const void *key,
410 unsigned int keylen)
411{
412 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
413
414 return alg->set_priv_key(tfm, key, keylen);
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700415}
416#endif