2 * QEMU Crypto hash algorithms
4 * Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates
5 * Copyright (c) 2016 Red Hat, Inc.
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 "qapi/error.h"
24 #include "crypto/hash.h"
28 static int qcrypto_hash_alg_map
[QCRYPTO_HASH_ALGO__MAX
] = {
29 [QCRYPTO_HASH_ALGO_MD5
] = G_CHECKSUM_MD5
,
30 [QCRYPTO_HASH_ALGO_SHA1
] = G_CHECKSUM_SHA1
,
31 [QCRYPTO_HASH_ALGO_SHA224
] = -1,
32 [QCRYPTO_HASH_ALGO_SHA256
] = G_CHECKSUM_SHA256
,
33 [QCRYPTO_HASH_ALGO_SHA384
] = G_CHECKSUM_SHA384
,
34 [QCRYPTO_HASH_ALGO_SHA512
] = G_CHECKSUM_SHA512
,
35 [QCRYPTO_HASH_ALGO_RIPEMD160
] = -1,
38 gboolean
qcrypto_hash_supports(QCryptoHashAlgo alg
)
40 if (alg
< G_N_ELEMENTS(qcrypto_hash_alg_map
) &&
41 qcrypto_hash_alg_map
[alg
] != -1) {
48 QCryptoHash
*qcrypto_glib_hash_new(QCryptoHashAlgo alg
,
53 hash
= g_new(QCryptoHash
, 1);
55 hash
->opaque
= g_checksum_new(qcrypto_hash_alg_map
[alg
]);
61 void qcrypto_glib_hash_free(QCryptoHash
*hash
)
64 g_checksum_free(hash
->opaque
);
72 int qcrypto_glib_hash_update(QCryptoHash
*hash
,
73 const struct iovec
*iov
,
77 GChecksum
*ctx
= hash
->opaque
;
79 for (int i
= 0; i
< niov
; i
++) {
80 g_checksum_update(ctx
, iov
[i
].iov_base
, iov
[i
].iov_len
);
87 int qcrypto_glib_hash_finalize(QCryptoHash
*hash
,
93 GChecksum
*ctx
= hash
->opaque
;
95 ret
= g_checksum_type_get_length(qcrypto_hash_alg_map
[hash
->alg
]);
97 error_setg(errp
, "Unable to get hash length");
103 *result
= g_new(uint8_t, *result_len
);
105 g_checksum_get_digest(ctx
, *result
, result_len
);
109 QCryptoHashDriver qcrypto_hash_lib_driver
= {
110 .hash_new
= qcrypto_glib_hash_new
,
111 .hash_update
= qcrypto_glib_hash_update
,
112 .hash_finalize
= qcrypto_glib_hash_finalize
,
113 .hash_free
= qcrypto_glib_hash_free
,