hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/include/crypto/akcipher.h
....@@ -1,14 +1,9 @@
1
+/* SPDX-License-Identifier: GPL-2.0-or-later */
12 /*
23 * Public Key Encryption
34 *
45 * Copyright (c) 2015, Intel Corporation
56 * 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
- *
127 */
138 #ifndef _CRYPTO_AKCIPHER_H
149 #define _CRYPTO_AKCIPHER_H
....@@ -19,14 +14,20 @@
1914 *
2015 * @base: Common attributes for async crypto requests
2116 * @src: Source data
22
- * @dst: Destination data
17
+ * 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)
2320 * @src_len: Size of the input buffer
24
- * @dst_len: Size of the output buffer. It needs to be at least
25
- * as big as the expected result depending on the operation
21
+ * 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.
2626 * After operation it will be updated with the actual size of the
2727 * result.
2828 * In case of error where the dst sgl size was insufficient,
2929 * it will be updated to the size required for the operation.
30
+ * For verify op this is size of digest part in @src.
3031 * @__ctx: Start of private context data
3132 */
3233 struct akcipher_request {
....@@ -55,10 +56,9 @@
5556 * algorithm. In case of error, where the dst_len was insufficient,
5657 * the req->dst_len will be updated to the size required for the
5758 * 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
59
+ * @verify: Function performs a complete verify operation as defined by
60
+ * public key algorithm, returning verification status. Requires
61
+ * digest value as input parameter.
6262 * @encrypt: Function performs an encrypt operation as defined by public key
6363 * algorithm. In case of error, where the dst_len was insufficient,
6464 * the req->dst_len will be updated to the size required for the
....@@ -69,10 +69,10 @@
6969 * operation
7070 * @set_pub_key: Function invokes the algorithm specific set public key
7171 * function, which knows how to decode and interpret
72
- * the BER encoded public key
72
+ * the BER encoded public key and parameters
7373 * @set_priv_key: Function invokes the algorithm specific set private key
7474 * function, which knows how to decode and interpret
75
- * the BER encoded private key
75
+ * the BER encoded private key and parameters
7676 * @max_size: Function returns dest buffer size required for a given key.
7777 * @init: Initialize the cryptographic transformation object.
7878 * This function is used to initialize the cryptographic
....@@ -209,7 +209,7 @@
209209 */
210210 static inline void akcipher_request_free(struct akcipher_request *req)
211211 {
212
- kzfree(req);
212
+ kfree_sensitive(req);
213213 }
214214
215215 /**
....@@ -240,9 +240,10 @@
240240 *
241241 * @req: public key request
242242 * @src: ptr to input scatter list
243
- * @dst: ptr to output scatter list
243
+ * @dst: ptr to output scatter list or NULL for verify op
244244 * @src_len: size of the src input scatter list to be processed
245
- * @dst_len: size of the dst output scatter list
245
+ * @dst_len: size of the dst output scatter list or size of signature
246
+ * portion in @src for verify op
246247 */
247248 static inline void akcipher_request_set_crypt(struct akcipher_request *req,
248249 struct scatterlist *src,
....@@ -287,8 +288,14 @@
287288 {
288289 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
289290 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
291
+ struct crypto_alg *calg = tfm->base.__crt_alg;
292
+ unsigned int src_len = req->src_len;
293
+ int ret;
290294
291
- return alg->encrypt(req);
295
+ crypto_stats_get(calg);
296
+ ret = alg->encrypt(req);
297
+ crypto_stats_akcipher_encrypt(src_len, ret, calg);
298
+ return ret;
292299 }
293300
294301 /**
....@@ -305,8 +312,14 @@
305312 {
306313 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
307314 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
315
+ struct crypto_alg *calg = tfm->base.__crt_alg;
316
+ unsigned int src_len = req->src_len;
317
+ int ret;
308318
309
- return alg->decrypt(req);
319
+ crypto_stats_get(calg);
320
+ ret = alg->decrypt(req);
321
+ crypto_stats_akcipher_decrypt(src_len, ret, calg);
322
+ return ret;
310323 }
311324
312325 /**
....@@ -323,37 +336,52 @@
323336 {
324337 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
325338 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
339
+ struct crypto_alg *calg = tfm->base.__crt_alg;
340
+ int ret;
326341
327
- return alg->sign(req);
342
+ crypto_stats_get(calg);
343
+ ret = alg->sign(req);
344
+ crypto_stats_akcipher_sign(ret, calg);
345
+ return ret;
328346 }
329347
330348 /**
331
- * crypto_akcipher_verify() - Invoke public key verify operation
349
+ * crypto_akcipher_verify() - Invoke public key signature verification
332350 *
333
- * Function invokes the specific public key verify operation for a given
334
- * public key algorithm
351
+ * Function invokes the specific public key signature verification operation
352
+ * for a given public key algorithm.
335353 *
336354 * @req: asymmetric key request
337355 *
338
- * Return: zero on success; error code in case of error
356
+ * Note: req->dst should be NULL, req->src should point to SG of size
357
+ * (req->src_size + req->dst_size), containing signature (of req->src_size
358
+ * length) with appended digest (of req->dst_size length).
359
+ *
360
+ * Return: zero on verification success; error code in case of error.
339361 */
340362 static inline int crypto_akcipher_verify(struct akcipher_request *req)
341363 {
342364 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
343365 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
366
+ struct crypto_alg *calg = tfm->base.__crt_alg;
367
+ int ret;
344368
345
- return alg->verify(req);
369
+ crypto_stats_get(calg);
370
+ ret = alg->verify(req);
371
+ crypto_stats_akcipher_verify(ret, calg);
372
+ return ret;
346373 }
347374
348375 /**
349376 * crypto_akcipher_set_pub_key() - Invoke set public key operation
350377 *
351378 * Function invokes the algorithm specific set key function, which knows
352
- * how to decode and interpret the encoded key
379
+ * how to decode and interpret the encoded key and parameters
353380 *
354381 * @tfm: tfm handle
355
- * @key: BER encoded public key
356
- * @keylen: length of the key
382
+ * @key: BER encoded public key, algo OID, paramlen, BER encoded
383
+ * parameters
384
+ * @keylen: length of the key (not including other data)
357385 *
358386 * Return: zero on success; error code in case of error
359387 */
....@@ -370,11 +398,12 @@
370398 * crypto_akcipher_set_priv_key() - Invoke set private key operation
371399 *
372400 * Function invokes the algorithm specific set key function, which knows
373
- * how to decode and interpret the encoded key
401
+ * how to decode and interpret the encoded key and parameters
374402 *
375403 * @tfm: tfm handle
376
- * @key: BER encoded private key
377
- * @keylen: length of the key
404
+ * @key: BER encoded private key, algo OID, paramlen, BER encoded
405
+ * parameters
406
+ * @keylen: length of the key (not including other data)
378407 *
379408 * Return: zero on success; error code in case of error
380409 */