2 * QEMU Crypto asymmetric 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 #ifndef QCRYPTO_AKCIPHER_H
23 #define QCRYPTO_AKCIPHER_H
25 #include "qapi/qapi-types-crypto.h"
27 typedef struct QCryptoAkCipher QCryptoAkCipher
;
30 * qcrypto_akcipher_supports:
31 * @opts: the asymmetric key algorithm and related options
33 * Determine if asymmetric key cipher decribed with @opts is
34 * supported by the current configured build
36 * Returns: true if it is supported, false otherwise.
38 bool qcrypto_akcipher_supports(QCryptoAkCipherOptions
*opts
);
41 * qcrypto_akcipher_new:
42 * @opts: specify the algorithm and the related arguments
43 * @type: private or public key type
44 * @key: buffer to store the key
45 * @key_len: the length of key buffer
46 * @errp: error pointer
48 * Create akcipher context
50 * Returns: On success, a new QCryptoAkCipher initialized with @opt
51 * is created and returned, otherwise NULL is returned.
54 QCryptoAkCipher
*qcrypto_akcipher_new(const QCryptoAkCipherOptions
*opts
,
55 QCryptoAkCipherKeyType type
,
56 const uint8_t *key
, size_t key_len
,
60 * qcrypto_akcipher_encrypt:
61 * @akcipher: akcipher context
62 * @in: plaintext pending to be encrypted
63 * @in_len: length of plaintext, less or equal to the size reported
64 * by a call to qcrypto_akcipher_max_plaintext_len()
65 * @out: buffer to store the ciphertext
66 * @out_len: length of ciphertext, less or equal to the size reported
67 * by a call to qcrypto_akcipher_max_ciphertext_len()
68 * @errp: error pointer
70 * Encrypt @in and write ciphertext into @out
72 * Returns: length of ciphertext if encrypt succeed,
73 * otherwise -1 is returned
75 int qcrypto_akcipher_encrypt(QCryptoAkCipher
*akcipher
,
76 const void *in
, size_t in_len
,
77 void *out
, size_t out_len
, Error
**errp
);
80 * qcrypto_akcipher_decrypt:
81 * @akcipher: akcipher context
82 * @in: ciphertext to be decrypted
83 * @in_len: the length of ciphertext, less or equal to the size reported
84 * by a call to qcrypto_akcipher_max_ciphertext_len()
85 * @out: buffer to store the plaintext
86 * @out_len: length of the plaintext buffer, less or equal to the size
87 * reported by a call to qcrypto_akcipher_max_plaintext_len()
88 * @errp: error pointer
90 * Decrypt @in and write plaintext into @out
92 * Returns: length of plaintext if decrypt succeed,
93 * otherwise -1 is returned
95 int qcrypto_akcipher_decrypt(QCryptoAkCipher
*akcipher
,
96 const void *in
, size_t in_len
,
97 void *out
, size_t out_len
, Error
**errp
);
100 * qcrypto_akcipher_sign:
101 * @akcipher: akcipher context
102 * @in: data to be signed
103 * @in_len: the length of data, less or equal to the size reported
104 * by a call to qcrypto_akcipher_max_dgst_len()
105 * @out: buffer to store the signature
106 * @out_len: length of the signature buffer, less or equal to the size
107 * by a call to qcrypto_akcipher_max_signature_len()
108 * @errp: error pointer
110 * Generate signature for @in, write into @out
112 * Returns: length of signature if succeed,
113 * otherwise -1 is returned
115 int qcrypto_akcipher_sign(QCryptoAkCipher
*akcipher
,
116 const void *in
, size_t in_len
,
117 void *out
, size_t out_len
, Error
**errp
);
120 * qcrypto_akcipher_verify:
121 * @akcipher: akcipher context
122 * @in: pointer to the signature
123 * @in_len: length of signature, ess or equal to the size reported
124 * by a call to qcrypto_akcipher_max_signature_len()
125 * @in2: pointer to original data
126 * @in2_len: the length of original data, less or equal to the size
127 * by a call to qcrypto_akcipher_max_dgst_len()
128 * @errp: error pointer
130 * Verify @in and @in2 match or not
132 * Returns: 0 for succeed,
133 * otherwise -1 is returned
135 int qcrypto_akcipher_verify(QCryptoAkCipher
*akcipher
,
136 const void *in
, size_t in_len
,
137 const void *in2
, size_t in2_len
, Error
**errp
);
139 int qcrypto_akcipher_max_plaintext_len(QCryptoAkCipher
*akcipher
);
141 int qcrypto_akcipher_max_ciphertext_len(QCryptoAkCipher
*akcipher
);
143 int qcrypto_akcipher_max_signature_len(QCryptoAkCipher
*akcipher
);
145 int qcrypto_akcipher_max_dgst_len(QCryptoAkCipher
*akcipher
);
148 * qcrypto_akcipher_free:
149 * @akcipher: akcipher context
151 * Free the akcipher context
154 void qcrypto_akcipher_free(QCryptoAkCipher
*akcipher
);
156 G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoAkCipher
, qcrypto_akcipher_free
)
158 #endif /* QCRYPTO_AKCIPHER_H */