blob: 8884046659a05d37a26ac854ef0ecf555592f40c [file] [log] [blame]
Tadeusz Struk3c339ab2015-06-16 10:30:55 -07001/*
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
Tadeusz Struk22287b02015-10-08 09:26:55 -070021 * @src: Source data
Vitaly Chikunovc7381b02019-04-11 18:51:15 +030022 * For verify op this is signature + digest, in that case
23 * total size of @src is @src_len + @dst_len.
24 * @dst: Destination data (Should be NULL for verify op)
Tadeusz Struk22287b02015-10-08 09:26:55 -070025 * @src_len: Size of the input buffer
Vitaly Chikunovc7381b02019-04-11 18:51:15 +030026 * For verify op it's size of signature part of @src, this part
27 * is supposed to be operated by cipher.
28 * @dst_len: Size of @dst buffer (for all ops except verify).
29 * It needs to be at least as big as the expected result
30 * depending on the operation.
LABBE Corentine14a1f12015-12-08 09:00:23 +010031 * After operation it will be updated with the actual size of the
Tadeusz Struk22287b02015-10-08 09:26:55 -070032 * result.
33 * In case of error where the dst sgl size was insufficient,
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070034 * it will be updated to the size required for the operation.
Vitaly Chikunovc7381b02019-04-11 18:51:15 +030035 * For verify op this is size of digest part in @src.
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070036 * @__ctx: Start of private context data
37 */
38struct akcipher_request {
39 struct crypto_async_request base;
Tadeusz Struk22287b02015-10-08 09:26:55 -070040 struct scatterlist *src;
41 struct scatterlist *dst;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070042 unsigned int src_len;
43 unsigned int dst_len;
44 void *__ctx[] CRYPTO_MINALIGN_ATTR;
45};
46
47/**
48 * struct crypto_akcipher - user-instantiated objects which encapsulate
49 * algorithms and core processing logic
50 *
51 * @base: Common crypto API algorithm data structure
52 */
53struct crypto_akcipher {
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
Vitaly Chikunovc7381b02019-04-11 18:51:15 +030064 * @verify: Function performs a complete verify operation as defined by
65 * public key algorithm, returning verification status. Requires
66 * digest value as input parameter.
LABBE Corentine14a1f12015-12-08 09:00:23 +010067 * @encrypt: Function performs an encrypt operation as defined by public key
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070068 * 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
Tadeusz Struk22287b02015-10-08 09:26:55 -070075 * @set_pub_key: Function invokes the algorithm specific set public key
76 * function, which knows how to decode and interpret
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +030077 * the BER encoded public key and parameters
Tadeusz Struk22287b02015-10-08 09:26:55 -070078 * @set_priv_key: Function invokes the algorithm specific set private key
79 * function, which knows how to decode and interpret
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +030080 * the BER encoded private key and parameters
LABBE Corentine14a1f12015-12-08 09:00:23 +010081 * @max_size: Function returns dest buffer size required for a given key.
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070082 * @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 *
94 * @reqsize: Request context size required by algorithm implementation
95 * @base: Common crypto API algorithm data structure
96 */
97struct akcipher_alg {
98 int (*sign)(struct akcipher_request *req);
99 int (*verify)(struct akcipher_request *req);
100 int (*encrypt)(struct akcipher_request *req);
101 int (*decrypt)(struct akcipher_request *req);
Tadeusz Struk22287b02015-10-08 09:26:55 -0700102 int (*set_pub_key)(struct crypto_akcipher *tfm, const void *key,
103 unsigned int keylen);
104 int (*set_priv_key)(struct crypto_akcipher *tfm, const void *key,
105 unsigned int keylen);
Tudor-Dan Ambarus561f8e22017-05-25 10:18:12 +0300106 unsigned int (*max_size)(struct crypto_akcipher *tfm);
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700107 int (*init)(struct crypto_akcipher *tfm);
108 void (*exit)(struct crypto_akcipher *tfm);
109
110 unsigned int reqsize;
111 struct crypto_alg base;
112};
113
114/**
115 * DOC: Generic Public Key API
116 *
117 * The Public Key API is used with the algorithms of type
118 * CRYPTO_ALG_TYPE_AKCIPHER (listed as type "akcipher" in /proc/crypto)
119 */
120
121/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100122 * crypto_alloc_akcipher() - allocate AKCIPHER tfm handle
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700123 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
124 * public key algorithm e.g. "rsa"
125 * @type: specifies the type of the algorithm
126 * @mask: specifies the mask for the algorithm
127 *
128 * Allocate a handle for public key algorithm. The returned struct
129 * crypto_akcipher is the handle that is required for any subsequent
130 * API invocation for the public key operations.
131 *
132 * Return: allocated handle in case of success; IS_ERR() is true in case
133 * of an error, PTR_ERR() returns the error code.
134 */
135struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
136 u32 mask);
137
138static inline struct crypto_tfm *crypto_akcipher_tfm(
139 struct crypto_akcipher *tfm)
140{
141 return &tfm->base;
142}
143
144static inline struct akcipher_alg *__crypto_akcipher_alg(struct crypto_alg *alg)
145{
146 return container_of(alg, struct akcipher_alg, base);
147}
148
149static inline struct crypto_akcipher *__crypto_akcipher_tfm(
150 struct crypto_tfm *tfm)
151{
152 return container_of(tfm, struct crypto_akcipher, base);
153}
154
155static inline struct akcipher_alg *crypto_akcipher_alg(
156 struct crypto_akcipher *tfm)
157{
158 return __crypto_akcipher_alg(crypto_akcipher_tfm(tfm)->__crt_alg);
159}
160
161static inline unsigned int crypto_akcipher_reqsize(struct crypto_akcipher *tfm)
162{
163 return crypto_akcipher_alg(tfm)->reqsize;
164}
165
166static inline void akcipher_request_set_tfm(struct akcipher_request *req,
167 struct crypto_akcipher *tfm)
168{
169 req->base.tfm = crypto_akcipher_tfm(tfm);
170}
171
172static inline struct crypto_akcipher *crypto_akcipher_reqtfm(
173 struct akcipher_request *req)
174{
175 return __crypto_akcipher_tfm(req->base.tfm);
176}
177
178/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100179 * crypto_free_akcipher() - free AKCIPHER tfm handle
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700180 *
181 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
182 */
183static inline void crypto_free_akcipher(struct crypto_akcipher *tfm)
184{
185 crypto_destroy_tfm(tfm, crypto_akcipher_tfm(tfm));
186}
187
188/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100189 * akcipher_request_alloc() - allocates public key request
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700190 *
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 */
196static 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/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100209 * akcipher_request_free() - zeroize and free public key request
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700210 *
211 * @req: request to free
212 */
213static inline void akcipher_request_free(struct akcipher_request *req)
214{
215 kzfree(req);
216}
217
218/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100219 * akcipher_request_set_callback() - Sets an asynchronous callback.
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700220 *
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
Stephan Muellere1eabc02016-02-16 11:32:06 +0100226 * @cmpl: callback which will be called
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700227 * @data: private data used by the caller
228 */
229static 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/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100240 * akcipher_request_set_crypt() - Sets request parameters
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700241 *
242 * Sets parameters required by crypto operation
243 *
244 * @req: public key request
Tadeusz Struk22287b02015-10-08 09:26:55 -0700245 * @src: ptr to input scatter list
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300246 * @dst: ptr to output scatter list or NULL for verify op
Tadeusz Struk22287b02015-10-08 09:26:55 -0700247 * @src_len: size of the src input scatter list to be processed
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300248 * @dst_len: size of the dst output scatter list or size of signature
249 * portion in @src for verify op
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700250 */
251static inline void akcipher_request_set_crypt(struct akcipher_request *req,
Tadeusz Struk22287b02015-10-08 09:26:55 -0700252 struct scatterlist *src,
253 struct scatterlist *dst,
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700254 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
263/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100264 * crypto_akcipher_maxsize() - Get len for output buffer
Tadeusz Struk22287b02015-10-08 09:26:55 -0700265 *
Tudor-Dan Ambarus561f8e22017-05-25 10:18:12 +0300266 * 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.
Tadeusz Struk22287b02015-10-08 09:26:55 -0700270 *
271 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
Tadeusz Struk22287b02015-10-08 09:26:55 -0700272 */
Tudor-Dan Ambarus561f8e22017-05-25 10:18:12 +0300273static inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher *tfm)
Tadeusz Struk22287b02015-10-08 09:26:55 -0700274{
275 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
276
277 return alg->max_size(tfm);
278}
279
280/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100281 * crypto_akcipher_encrypt() - Invoke public key encrypt operation
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700282 *
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 */
290static inline int crypto_akcipher_encrypt(struct akcipher_request *req)
291{
292 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
293 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000294 struct crypto_alg *calg = tfm->base.__crt_alg;
295 unsigned int src_len = req->src_len;
Corentin Labbecac58182018-09-19 10:10:54 +0000296 int ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700297
Corentin Labbef7d76e02018-11-29 14:42:21 +0000298 crypto_stats_get(calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000299 ret = alg->encrypt(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000300 crypto_stats_akcipher_encrypt(src_len, ret, calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000301 return ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700302}
303
304/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100305 * crypto_akcipher_decrypt() - Invoke public key decrypt operation
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700306 *
307 * Function invokes the specific public key decrypt operation for a given
308 * public key algorithm
309 *
310 * @req: asymmetric key request
311 *
312 * Return: zero on success; error code in case of error
313 */
314static inline int crypto_akcipher_decrypt(struct akcipher_request *req)
315{
316 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
317 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000318 struct crypto_alg *calg = tfm->base.__crt_alg;
319 unsigned int src_len = req->src_len;
Corentin Labbecac58182018-09-19 10:10:54 +0000320 int ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700321
Corentin Labbef7d76e02018-11-29 14:42:21 +0000322 crypto_stats_get(calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000323 ret = alg->decrypt(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000324 crypto_stats_akcipher_decrypt(src_len, ret, calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000325 return ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700326}
327
328/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100329 * crypto_akcipher_sign() - Invoke public key sign operation
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700330 *
331 * Function invokes the specific public key sign operation for a given
332 * public key algorithm
333 *
334 * @req: asymmetric key request
335 *
336 * Return: zero on success; error code in case of error
337 */
338static inline int crypto_akcipher_sign(struct akcipher_request *req)
339{
340 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
341 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000342 struct crypto_alg *calg = tfm->base.__crt_alg;
Corentin Labbecac58182018-09-19 10:10:54 +0000343 int ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700344
Corentin Labbef7d76e02018-11-29 14:42:21 +0000345 crypto_stats_get(calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000346 ret = alg->sign(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000347 crypto_stats_akcipher_sign(ret, calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000348 return ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700349}
350
351/**
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300352 * crypto_akcipher_verify() - Invoke public key signature verification
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700353 *
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300354 * Function invokes the specific public key signature verification operation
355 * for a given public key algorithm.
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700356 *
357 * @req: asymmetric key request
358 *
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300359 * Note: req->dst should be NULL, req->src should point to SG of size
360 * (req->src_size + req->dst_size), containing signature (of req->src_size
361 * length) with appended digest (of req->dst_size length).
362 *
363 * Return: zero on verification success; error code in case of error.
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700364 */
365static inline int crypto_akcipher_verify(struct akcipher_request *req)
366{
367 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
368 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000369 struct crypto_alg *calg = tfm->base.__crt_alg;
Corentin Labbecac58182018-09-19 10:10:54 +0000370 int ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700371
Corentin Labbef7d76e02018-11-29 14:42:21 +0000372 crypto_stats_get(calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000373 ret = alg->verify(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000374 crypto_stats_akcipher_verify(ret, calg);
Corentin Labbecac58182018-09-19 10:10:54 +0000375 return ret;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700376}
377
378/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100379 * crypto_akcipher_set_pub_key() - Invoke set public key operation
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700380 *
381 * Function invokes the algorithm specific set key function, which knows
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300382 * how to decode and interpret the encoded key and parameters
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700383 *
384 * @tfm: tfm handle
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300385 * @key: BER encoded public key, algo OID, paramlen, BER encoded
386 * parameters
387 * @keylen: length of the key (not including other data)
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700388 *
389 * Return: zero on success; error code in case of error
390 */
Tadeusz Struk22287b02015-10-08 09:26:55 -0700391static inline int crypto_akcipher_set_pub_key(struct crypto_akcipher *tfm,
392 const void *key,
393 unsigned int keylen)
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700394{
395 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
396
Tadeusz Struk22287b02015-10-08 09:26:55 -0700397 return alg->set_pub_key(tfm, key, keylen);
398}
399
400/**
Stephan Muellere1eabc02016-02-16 11:32:06 +0100401 * crypto_akcipher_set_priv_key() - Invoke set private key operation
Tadeusz Struk22287b02015-10-08 09:26:55 -0700402 *
403 * Function invokes the algorithm specific set key function, which knows
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300404 * how to decode and interpret the encoded key and parameters
Tadeusz Struk22287b02015-10-08 09:26:55 -0700405 *
406 * @tfm: tfm handle
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300407 * @key: BER encoded private key, algo OID, paramlen, BER encoded
408 * parameters
409 * @keylen: length of the key (not including other data)
Tadeusz Struk22287b02015-10-08 09:26:55 -0700410 *
411 * Return: zero on success; error code in case of error
412 */
413static inline int crypto_akcipher_set_priv_key(struct crypto_akcipher *tfm,
414 const void *key,
415 unsigned int keylen)
416{
417 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
418
419 return alg->set_priv_key(tfm, key, keylen);
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700420}
421#endif