Merge tag 'qemu-macppc-20230206' of https://github.com/mcayland/qemu into staging
[qemu.git] / crypto / akcipher.c
blobe4bbc6e5f1a69a2d10b2218fd57beecf0c69f8cc
1 /*
2 * QEMU Crypto akcipher algorithms
4 * Copyright (c) 2022 Bytedance
5 * Author: zhenwei pi <pizhenwei@bytedance.com>
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.
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.
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/>.
22 #include "qemu/osdep.h"
23 #include "crypto/akcipher.h"
24 #include "akcipherpriv.h"
25 #include "der.h"
26 #include "rsakey.h"
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)
38 QCryptoAkCipher *akcipher = NULL;
40 return akcipher;
43 bool qcrypto_akcipher_supports(QCryptoAkCipherOptions *opts)
45 return false;
47 #endif
49 int qcrypto_akcipher_encrypt(QCryptoAkCipher *akcipher,
50 const void *in, size_t in_len,
51 void *out, size_t out_len, Error **errp)
53 const QCryptoAkCipherDriver *drv = akcipher->driver;
55 return drv->encrypt(akcipher, in, in_len, out, out_len, errp);
58 int qcrypto_akcipher_decrypt(QCryptoAkCipher *akcipher,
59 const void *in, size_t in_len,
60 void *out, size_t out_len, Error **errp)
62 const QCryptoAkCipherDriver *drv = akcipher->driver;
64 return drv->decrypt(akcipher, in, in_len, out, out_len, errp);
67 int qcrypto_akcipher_sign(QCryptoAkCipher *akcipher,
68 const void *in, size_t in_len,
69 void *out, size_t out_len, Error **errp)
71 const QCryptoAkCipherDriver *drv = akcipher->driver;
73 return drv->sign(akcipher, in, in_len, out, out_len, errp);
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)
80 const QCryptoAkCipherDriver *drv = akcipher->driver;
82 return drv->verify(akcipher, in, in_len, in2, in2_len, errp);
85 int qcrypto_akcipher_max_plaintext_len(QCryptoAkCipher *akcipher)
87 return akcipher->max_plaintext_len;
90 int qcrypto_akcipher_max_ciphertext_len(QCryptoAkCipher *akcipher)
92 return akcipher->max_ciphertext_len;
95 int qcrypto_akcipher_max_signature_len(QCryptoAkCipher *akcipher)
97 return akcipher->max_signature_len;
100 int qcrypto_akcipher_max_dgst_len(QCryptoAkCipher *akcipher)
102 return akcipher->max_dgst_len;
105 void qcrypto_akcipher_free(QCryptoAkCipher *akcipher)
107 const QCryptoAkCipherDriver *drv = akcipher->driver;
109 drv->free(akcipher);
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)
117 switch (opts->alg) {
118 case QCRYPTO_AKCIPHER_ALG_RSA:
119 qcrypto_akcipher_rsakey_export_p8info(key, keylen, dst, dst_len);
120 return 0;
122 default:
123 error_setg(errp, "Unsupported algorithm: %u", opts->alg);
124 return -1;