]> git.proxmox.com Git - mirror_qemu.git/blob - crypto/akcipher.c
Merge tag 'migration-20231020-pull-request' of https://gitlab.com/juan.quintela/qemu...
[mirror_qemu.git] / crypto / akcipher.c
1 /*
2 * QEMU Crypto akcipher algorithms
3 *
4 * Copyright (c) 2022 Bytedance
5 * Author: zhenwei pi <pizhenwei@bytedance.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 #include "qemu/osdep.h"
23 #include "crypto/akcipher.h"
24 #include "akcipherpriv.h"
25 #include "der.h"
26 #include "rsakey.h"
27
28 #if defined(CONFIG_GCRYPT)
29 #include "akcipher-gcrypt.c.inc"
30 #elif defined(CONFIG_NETTLE) && defined(CONFIG_HOGWEED)
31 #include "akcipher-nettle.c.inc"
32 #else
33 QCryptoAkCipher *qcrypto_akcipher_new(const QCryptoAkCipherOptions *opts,
34 QCryptoAkCipherKeyType type,
35 const uint8_t *key, size_t keylen,
36 Error **errp)
37 {
38 QCryptoAkCipher *akcipher = NULL;
39
40 return akcipher;
41 }
42
43 bool qcrypto_akcipher_supports(QCryptoAkCipherOptions *opts)
44 {
45 return false;
46 }
47 #endif
48
49 int qcrypto_akcipher_encrypt(QCryptoAkCipher *akcipher,
50 const void *in, size_t in_len,
51 void *out, size_t out_len, Error **errp)
52 {
53 const QCryptoAkCipherDriver *drv = akcipher->driver;
54
55 return drv->encrypt(akcipher, in, in_len, out, out_len, errp);
56 }
57
58 int qcrypto_akcipher_decrypt(QCryptoAkCipher *akcipher,
59 const void *in, size_t in_len,
60 void *out, size_t out_len, Error **errp)
61 {
62 const QCryptoAkCipherDriver *drv = akcipher->driver;
63
64 return drv->decrypt(akcipher, in, in_len, out, out_len, errp);
65 }
66
67 int qcrypto_akcipher_sign(QCryptoAkCipher *akcipher,
68 const void *in, size_t in_len,
69 void *out, size_t out_len, Error **errp)
70 {
71 const QCryptoAkCipherDriver *drv = akcipher->driver;
72
73 return drv->sign(akcipher, in, in_len, out, out_len, errp);
74 }
75
76 int qcrypto_akcipher_verify(QCryptoAkCipher *akcipher,
77 const void *in, size_t in_len,
78 const void *in2, size_t in2_len, Error **errp)
79 {
80 const QCryptoAkCipherDriver *drv = akcipher->driver;
81
82 return drv->verify(akcipher, in, in_len, in2, in2_len, errp);
83 }
84
85 int qcrypto_akcipher_max_plaintext_len(QCryptoAkCipher *akcipher)
86 {
87 return akcipher->max_plaintext_len;
88 }
89
90 int qcrypto_akcipher_max_ciphertext_len(QCryptoAkCipher *akcipher)
91 {
92 return akcipher->max_ciphertext_len;
93 }
94
95 int qcrypto_akcipher_max_signature_len(QCryptoAkCipher *akcipher)
96 {
97 return akcipher->max_signature_len;
98 }
99
100 int qcrypto_akcipher_max_dgst_len(QCryptoAkCipher *akcipher)
101 {
102 return akcipher->max_dgst_len;
103 }
104
105 void qcrypto_akcipher_free(QCryptoAkCipher *akcipher)
106 {
107 const QCryptoAkCipherDriver *drv = akcipher->driver;
108
109 drv->free(akcipher);
110 }
111
112 int qcrypto_akcipher_export_p8info(const QCryptoAkCipherOptions *opts,
113 uint8_t *key, size_t keylen,
114 uint8_t **dst, size_t *dst_len,
115 Error **errp)
116 {
117 switch (opts->alg) {
118 case QCRYPTO_AKCIPHER_ALG_RSA:
119 qcrypto_akcipher_rsakey_export_p8info(key, keylen, dst, dst_len);
120 return 0;
121
122 default:
123 error_setg(errp, "Unsupported algorithm: %u", opts->alg);
124 return -1;
125 }
126 }