1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Public Key Encryption
5 * Copyright (c) 2015, Intel Corporation
6 * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
8 #ifndef _CRYPTO_AKCIPHER_H
9 #define _CRYPTO_AKCIPHER_H
11 #include <linux/atomic.h>
12 #include <linux/crypto.h>
15 * struct akcipher_request - public key request
17 * @base: Common attributes for async crypto requests
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
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
35 struct akcipher_request {
36 struct crypto_async_request base;
37 struct scatterlist *src;
38 struct scatterlist *dst;
41 void *__ctx[] CRYPTO_MINALIGN_ATTR;
45 * struct crypto_akcipher - user-instantiated objects which encapsulate
46 * algorithms and core processing logic
48 * @reqsize: Request context size required by algorithm implementation
49 * @base: Common crypto API algorithm data structure
51 struct crypto_akcipher {
54 struct crypto_tfm base;
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
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;
78 * struct akcipher_alg - generic public key algorithm
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
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
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
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
113 * @stat: Statistics for akcipher algorithm
115 * @base: Common crypto API algorithm data structure
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);
130 #ifdef CONFIG_CRYPTO_STATS
131 struct crypto_istat_akcipher stat;
134 struct crypto_alg base;
138 * DOC: Generic Public Key API
140 * The Public Key API is used with the algorithms of type
141 * CRYPTO_ALG_TYPE_AKCIPHER (listed as type "akcipher" in /proc/crypto)
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
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.
155 * Return: allocated handle in case of success; IS_ERR() is true in case
156 * of an error, PTR_ERR() returns the error code.
158 struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
161 static inline struct crypto_tfm *crypto_akcipher_tfm(
162 struct crypto_akcipher *tfm)
167 static inline struct akcipher_alg *__crypto_akcipher_alg(struct crypto_alg *alg)
169 return container_of(alg, struct akcipher_alg, base);
172 static inline struct crypto_akcipher *__crypto_akcipher_tfm(
173 struct crypto_tfm *tfm)
175 return container_of(tfm, struct crypto_akcipher, base);
178 static inline struct akcipher_alg *crypto_akcipher_alg(
179 struct crypto_akcipher *tfm)
181 return __crypto_akcipher_alg(crypto_akcipher_tfm(tfm)->__crt_alg);
184 static inline unsigned int crypto_akcipher_reqsize(struct crypto_akcipher *tfm)
189 static inline void akcipher_request_set_tfm(struct akcipher_request *req,
190 struct crypto_akcipher *tfm)
192 req->base.tfm = crypto_akcipher_tfm(tfm);
195 static inline struct crypto_akcipher *crypto_akcipher_reqtfm(
196 struct akcipher_request *req)
198 return __crypto_akcipher_tfm(req->base.tfm);
202 * crypto_free_akcipher() - free AKCIPHER tfm handle
204 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
206 * If @tfm is a NULL or error pointer, this function does nothing.
208 static inline void crypto_free_akcipher(struct crypto_akcipher *tfm)
210 crypto_destroy_tfm(tfm, crypto_akcipher_tfm(tfm));
214 * akcipher_request_alloc() - allocates public key request
216 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
217 * @gfp: allocation flags
219 * Return: allocated handle in case of success or NULL in case of an error.
221 static inline struct akcipher_request *akcipher_request_alloc(
222 struct crypto_akcipher *tfm, gfp_t gfp)
224 struct akcipher_request *req;
226 req = kmalloc(sizeof(*req) + crypto_akcipher_reqsize(tfm), gfp);
228 akcipher_request_set_tfm(req, tfm);
234 * akcipher_request_free() - zeroize and free public key request
236 * @req: request to free
238 static inline void akcipher_request_free(struct akcipher_request *req)
240 kfree_sensitive(req);
244 * akcipher_request_set_callback() - Sets an asynchronous callback.
246 * Callback will be called when an asynchronous operation on a given
247 * request is finished.
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
254 static inline void akcipher_request_set_callback(struct akcipher_request *req,
256 crypto_completion_t cmpl,
259 req->base.complete = cmpl;
260 req->base.data = data;
261 req->base.flags = flgs;
265 * akcipher_request_set_crypt() - Sets request parameters
267 * Sets parameters required by crypto operation
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
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)
284 req->src_len = src_len;
285 req->dst_len = dst_len;
289 * crypto_akcipher_maxsize() - Get len for output buffer
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.
296 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
298 static inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher *tfm)
300 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
302 return alg->max_size(tfm);
305 static inline struct crypto_istat_akcipher *akcipher_get_stat(
306 struct akcipher_alg *alg)
308 #ifdef CONFIG_CRYPTO_STATS
315 static inline int crypto_akcipher_errstat(struct akcipher_alg *alg, int err)
317 if (!IS_ENABLED(CONFIG_CRYPTO_STATS))
320 if (err && err != -EINPROGRESS && err != -EBUSY)
321 atomic64_inc(&akcipher_get_stat(alg)->err_cnt);
327 * crypto_akcipher_encrypt() - Invoke public key encrypt operation
329 * Function invokes the specific public key encrypt operation for a given
330 * public key algorithm
332 * @req: asymmetric key request
334 * Return: zero on success; error code in case of error
336 static inline int crypto_akcipher_encrypt(struct akcipher_request *req)
338 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
339 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
341 if (IS_ENABLED(CONFIG_CRYPTO_STATS)) {
342 struct crypto_istat_akcipher *istat = akcipher_get_stat(alg);
344 atomic64_inc(&istat->encrypt_cnt);
345 atomic64_add(req->src_len, &istat->encrypt_tlen);
348 return crypto_akcipher_errstat(alg, alg->encrypt(req));
352 * crypto_akcipher_decrypt() - Invoke public key decrypt operation
354 * Function invokes the specific public key decrypt operation for a given
355 * public key algorithm
357 * @req: asymmetric key request
359 * Return: zero on success; error code in case of error
361 static inline int crypto_akcipher_decrypt(struct akcipher_request *req)
363 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
364 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
366 if (IS_ENABLED(CONFIG_CRYPTO_STATS)) {
367 struct crypto_istat_akcipher *istat = akcipher_get_stat(alg);
369 atomic64_inc(&istat->decrypt_cnt);
370 atomic64_add(req->src_len, &istat->decrypt_tlen);
373 return crypto_akcipher_errstat(alg, alg->decrypt(req));
377 * crypto_akcipher_sign() - Invoke public key sign operation
379 * Function invokes the specific public key sign operation for a given
380 * public key algorithm
382 * @req: asymmetric key request
384 * Return: zero on success; error code in case of error
386 static inline int crypto_akcipher_sign(struct akcipher_request *req)
388 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
389 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
391 if (IS_ENABLED(CONFIG_CRYPTO_STATS))
392 atomic64_inc(&akcipher_get_stat(alg)->sign_cnt);
394 return crypto_akcipher_errstat(alg, alg->sign(req));
398 * crypto_akcipher_verify() - Invoke public key signature verification
400 * Function invokes the specific public key signature verification operation
401 * for a given public key algorithm.
403 * @req: asymmetric key request
405 * Note: req->dst should be NULL, req->src should point to SG of size
406 * (req->src_size + req->dst_size), containing signature (of req->src_size
407 * length) with appended digest (of req->dst_size length).
409 * Return: zero on verification success; error code in case of error.
411 static inline int crypto_akcipher_verify(struct akcipher_request *req)
413 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
414 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
416 if (IS_ENABLED(CONFIG_CRYPTO_STATS))
417 atomic64_inc(&akcipher_get_stat(alg)->verify_cnt);
419 return crypto_akcipher_errstat(alg, alg->verify(req));
423 * crypto_akcipher_set_pub_key() - Invoke set public key operation
425 * Function invokes the algorithm specific set key function, which knows
426 * how to decode and interpret the encoded key and parameters
429 * @key: BER encoded public key, algo OID, paramlen, BER encoded
431 * @keylen: length of the key (not including other data)
433 * Return: zero on success; error code in case of error
435 static inline int crypto_akcipher_set_pub_key(struct crypto_akcipher *tfm,
439 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
441 return alg->set_pub_key(tfm, key, keylen);
445 * crypto_akcipher_set_priv_key() - Invoke set private key operation
447 * Function invokes the algorithm specific set key function, which knows
448 * how to decode and interpret the encoded key and parameters
451 * @key: BER encoded private key, algo OID, paramlen, BER encoded
453 * @keylen: length of the key (not including other data)
455 * Return: zero on success; error code in case of error
457 static inline int crypto_akcipher_set_priv_key(struct crypto_akcipher *tfm,
461 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
463 return alg->set_priv_key(tfm, key, keylen);