10c16c056SDaniel P. Berrange /*
20c16c056SDaniel P. Berrange * QEMU Crypto hash algorithms
30c16c056SDaniel P. Berrange *
44fd0a730SAlejandro Zeise * Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates
50c16c056SDaniel P. Berrange * Copyright (c) 2016 Red Hat, Inc.
60c16c056SDaniel P. Berrange *
70c16c056SDaniel P. Berrange * This library is free software; you can redistribute it and/or
80c16c056SDaniel P. Berrange * modify it under the terms of the GNU Lesser General Public
90c16c056SDaniel P. Berrange * License as published by the Free Software Foundation; either
10b7cbb874SThomas Huth * version 2.1 of the License, or (at your option) any later version.
110c16c056SDaniel P. Berrange *
120c16c056SDaniel P. Berrange * This library is distributed in the hope that it will be useful,
130c16c056SDaniel P. Berrange * but WITHOUT ANY WARRANTY; without even the implied warranty of
140c16c056SDaniel P. Berrange * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
150c16c056SDaniel P. Berrange * Lesser General Public License for more details.
160c16c056SDaniel P. Berrange *
170c16c056SDaniel P. Berrange * You should have received a copy of the GNU Lesser General Public
180c16c056SDaniel P. Berrange * License along with this library; if not, see <http://www.gnu.org/licenses/>.
190c16c056SDaniel P. Berrange *
200c16c056SDaniel P. Berrange */
210c16c056SDaniel P. Berrange
220c16c056SDaniel P. Berrange #include "qemu/osdep.h"
23a9c94277SMarkus Armbruster #include <gcrypt.h>
240c16c056SDaniel P. Berrange #include "qapi/error.h"
250c16c056SDaniel P. Berrange #include "crypto/hash.h"
26aa8efad9SLongpeng(Mike) #include "hashpriv.h"
270c16c056SDaniel P. Berrange
280c16c056SDaniel P. Berrange
29ef834aa2SMarkus Armbruster static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = {
30ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_MD5] = GCRY_MD_MD5,
31ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA1] = GCRY_MD_SHA1,
32ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA224] = GCRY_MD_SHA224,
33ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA256] = GCRY_MD_SHA256,
34ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA384] = GCRY_MD_SHA384,
35ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA512] = GCRY_MD_SHA512,
36ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_RIPEMD160] = GCRY_MD_RMD160,
37d078da86Sliequan che #ifdef CONFIG_CRYPTO_SM3
38d078da86Sliequan che [QCRYPTO_HASH_ALGO_SM3] = GCRY_MD_SM3,
39d078da86Sliequan che #endif
400c16c056SDaniel P. Berrange };
410c16c056SDaniel P. Berrange
qcrypto_hash_supports(QCryptoHashAlgo alg)42ef834aa2SMarkus Armbruster gboolean qcrypto_hash_supports(QCryptoHashAlgo alg)
430c16c056SDaniel P. Berrange {
440c16c056SDaniel P. Berrange if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map) &&
450c16c056SDaniel P. Berrange qcrypto_hash_alg_map[alg] != GCRY_MD_NONE) {
46*a7e42752SDaniel P. Berrangé return gcry_md_test_algo(qcrypto_hash_alg_map[alg]) == 0;
470c16c056SDaniel P. Berrange }
480c16c056SDaniel P. Berrange return false;
490c16c056SDaniel P. Berrange }
500c16c056SDaniel P. Berrange
514fd0a730SAlejandro Zeise static
qcrypto_gcrypt_hash_new(QCryptoHashAlgo alg,Error ** errp)524fd0a730SAlejandro Zeise QCryptoHash *qcrypto_gcrypt_hash_new(QCryptoHashAlgo alg, Error **errp)
534fd0a730SAlejandro Zeise {
544fd0a730SAlejandro Zeise QCryptoHash *hash;
55bbd40a0eSDaniel P. Berrangé gcry_error_t ret;
564fd0a730SAlejandro Zeise
574fd0a730SAlejandro Zeise hash = g_new(QCryptoHash, 1);
584fd0a730SAlejandro Zeise hash->alg = alg;
594fd0a730SAlejandro Zeise hash->opaque = g_new(gcry_md_hd_t, 1);
604fd0a730SAlejandro Zeise
614fd0a730SAlejandro Zeise ret = gcry_md_open((gcry_md_hd_t *) hash->opaque,
624fd0a730SAlejandro Zeise qcrypto_hash_alg_map[alg], 0);
63bbd40a0eSDaniel P. Berrangé if (ret != 0) {
644fd0a730SAlejandro Zeise error_setg(errp,
654fd0a730SAlejandro Zeise "Unable to initialize hash algorithm: %s",
664fd0a730SAlejandro Zeise gcry_strerror(ret));
674fd0a730SAlejandro Zeise g_free(hash->opaque);
684fd0a730SAlejandro Zeise g_free(hash);
694fd0a730SAlejandro Zeise return NULL;
704fd0a730SAlejandro Zeise }
714fd0a730SAlejandro Zeise return hash;
724fd0a730SAlejandro Zeise }
734fd0a730SAlejandro Zeise
744fd0a730SAlejandro Zeise static
qcrypto_gcrypt_hash_free(QCryptoHash * hash)754fd0a730SAlejandro Zeise void qcrypto_gcrypt_hash_free(QCryptoHash *hash)
764fd0a730SAlejandro Zeise {
774fd0a730SAlejandro Zeise gcry_md_hd_t *ctx = hash->opaque;
784fd0a730SAlejandro Zeise
794fd0a730SAlejandro Zeise if (ctx) {
804fd0a730SAlejandro Zeise gcry_md_close(*ctx);
814fd0a730SAlejandro Zeise g_free(ctx);
824fd0a730SAlejandro Zeise }
834fd0a730SAlejandro Zeise
844fd0a730SAlejandro Zeise g_free(hash);
854fd0a730SAlejandro Zeise }
864fd0a730SAlejandro Zeise
874fd0a730SAlejandro Zeise
884fd0a730SAlejandro Zeise static
qcrypto_gcrypt_hash_update(QCryptoHash * hash,const struct iovec * iov,size_t niov,Error ** errp)894fd0a730SAlejandro Zeise int qcrypto_gcrypt_hash_update(QCryptoHash *hash,
904fd0a730SAlejandro Zeise const struct iovec *iov,
914fd0a730SAlejandro Zeise size_t niov,
924fd0a730SAlejandro Zeise Error **errp)
934fd0a730SAlejandro Zeise {
944fd0a730SAlejandro Zeise gcry_md_hd_t *ctx = hash->opaque;
954fd0a730SAlejandro Zeise
964fd0a730SAlejandro Zeise for (int i = 0; i < niov; i++) {
974fd0a730SAlejandro Zeise gcry_md_write(*ctx, iov[i].iov_base, iov[i].iov_len);
984fd0a730SAlejandro Zeise }
994fd0a730SAlejandro Zeise
1004fd0a730SAlejandro Zeise return 0;
1014fd0a730SAlejandro Zeise }
1024fd0a730SAlejandro Zeise
1034fd0a730SAlejandro Zeise static
qcrypto_gcrypt_hash_finalize(QCryptoHash * hash,uint8_t ** result,size_t * result_len,Error ** errp)1044fd0a730SAlejandro Zeise int qcrypto_gcrypt_hash_finalize(QCryptoHash *hash,
1054fd0a730SAlejandro Zeise uint8_t **result,
1064fd0a730SAlejandro Zeise size_t *result_len,
1074fd0a730SAlejandro Zeise Error **errp)
1084fd0a730SAlejandro Zeise {
109dde538c9SDaniel P. Berrangé int ret;
1104fd0a730SAlejandro Zeise unsigned char *digest;
1114fd0a730SAlejandro Zeise gcry_md_hd_t *ctx = hash->opaque;
1124fd0a730SAlejandro Zeise
113dde538c9SDaniel P. Berrangé ret = gcry_md_get_algo_dlen(qcrypto_hash_alg_map[hash->alg]);
114dde538c9SDaniel P. Berrangé if (ret == 0) {
1154fd0a730SAlejandro Zeise error_setg(errp, "Unable to get hash length");
1164fd0a730SAlejandro Zeise return -1;
1174fd0a730SAlejandro Zeise }
1184fd0a730SAlejandro Zeise
119dde538c9SDaniel P. Berrangé if (*result_len == 0) {
120dde538c9SDaniel P. Berrangé *result_len = ret;
1214fd0a730SAlejandro Zeise *result = g_new(uint8_t, *result_len);
122dde538c9SDaniel P. Berrangé } else if (*result_len != ret) {
123dde538c9SDaniel P. Berrangé error_setg(errp,
124dde538c9SDaniel P. Berrangé "Result buffer size %zu is smaller than hash %d",
125dde538c9SDaniel P. Berrangé *result_len, ret);
126dde538c9SDaniel P. Berrangé return -1;
127dde538c9SDaniel P. Berrangé }
1284fd0a730SAlejandro Zeise
1294fd0a730SAlejandro Zeise /* Digest is freed by gcry_md_close(), copy it */
1304fd0a730SAlejandro Zeise digest = gcry_md_read(*ctx, 0);
1314fd0a730SAlejandro Zeise memcpy(*result, digest, *result_len);
1324fd0a730SAlejandro Zeise return 0;
1334fd0a730SAlejandro Zeise }
134aa8efad9SLongpeng(Mike)
135aa8efad9SLongpeng(Mike) QCryptoHashDriver qcrypto_hash_lib_driver = {
1364fd0a730SAlejandro Zeise .hash_new = qcrypto_gcrypt_hash_new,
1374fd0a730SAlejandro Zeise .hash_update = qcrypto_gcrypt_hash_update,
1384fd0a730SAlejandro Zeise .hash_finalize = qcrypto_gcrypt_hash_finalize,
1394fd0a730SAlejandro Zeise .hash_free = qcrypto_gcrypt_hash_free,
140aa8efad9SLongpeng(Mike) };
141