xref: /openbmc/qemu/crypto/akcipher.c (revision 92e87950)
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 
26 #if defined(CONFIG_GCRYPT)
27 #include "akcipher-gcrypt.c.inc"
28 #elif defined(CONFIG_NETTLE) && defined(CONFIG_HOGWEED)
29 #include "akcipher-nettle.c.inc"
30 #else
31 QCryptoAkCipher *qcrypto_akcipher_new(const QCryptoAkCipherOptions *opts,
32                                       QCryptoAkCipherKeyType type,
33                                       const uint8_t *key, size_t keylen,
34                                       Error **errp)
35 {
36     QCryptoAkCipher *akcipher = NULL;
37 
38     return akcipher;
39 }
40 
41 bool qcrypto_akcipher_supports(QCryptoAkCipherOptions *opts)
42 {
43     return false;
44 }
45 #endif
46 
47 int qcrypto_akcipher_encrypt(QCryptoAkCipher *akcipher,
48                              const void *in, size_t in_len,
49                              void *out, size_t out_len, Error **errp)
50 {
51     const QCryptoAkCipherDriver *drv = akcipher->driver;
52 
53     return drv->encrypt(akcipher, in, in_len, out, out_len, errp);
54 }
55 
56 int qcrypto_akcipher_decrypt(QCryptoAkCipher *akcipher,
57                              const void *in, size_t in_len,
58                              void *out, size_t out_len, Error **errp)
59 {
60     const QCryptoAkCipherDriver *drv = akcipher->driver;
61 
62     return drv->decrypt(akcipher, in, in_len, out, out_len, errp);
63 }
64 
65 int qcrypto_akcipher_sign(QCryptoAkCipher *akcipher,
66                           const void *in, size_t in_len,
67                           void *out, size_t out_len, Error **errp)
68 {
69     const QCryptoAkCipherDriver *drv = akcipher->driver;
70 
71     return drv->sign(akcipher, in, in_len, out, out_len, errp);
72 }
73 
74 int qcrypto_akcipher_verify(QCryptoAkCipher *akcipher,
75                             const void *in, size_t in_len,
76                             const void *in2, size_t in2_len, Error **errp)
77 {
78     const QCryptoAkCipherDriver *drv = akcipher->driver;
79 
80     return drv->verify(akcipher, in, in_len, in2, in2_len, errp);
81 }
82 
83 int qcrypto_akcipher_max_plaintext_len(QCryptoAkCipher *akcipher)
84 {
85     return akcipher->max_plaintext_len;
86 }
87 
88 int qcrypto_akcipher_max_ciphertext_len(QCryptoAkCipher *akcipher)
89 {
90     return akcipher->max_ciphertext_len;
91 }
92 
93 int qcrypto_akcipher_max_signature_len(QCryptoAkCipher *akcipher)
94 {
95     return akcipher->max_signature_len;
96 }
97 
98 int qcrypto_akcipher_max_dgst_len(QCryptoAkCipher *akcipher)
99 {
100     return akcipher->max_dgst_len;
101 }
102 
103 void qcrypto_akcipher_free(QCryptoAkCipher *akcipher)
104 {
105     const QCryptoAkCipherDriver *drv = akcipher->driver;
106 
107     drv->free(akcipher);
108 }
109