hash-gcrypt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * QEMU Crypto hash algorithms
  3. *
  4. * Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates
  5. * Copyright (c) 2016 Red Hat, Inc.
  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. #include "qemu/osdep.h"
  22. #include <gcrypt.h>
  23. #include "qapi/error.h"
  24. #include "crypto/hash.h"
  25. #include "hashpriv.h"
  26. static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = {
  27. [QCRYPTO_HASH_ALGO_MD5] = GCRY_MD_MD5,
  28. [QCRYPTO_HASH_ALGO_SHA1] = GCRY_MD_SHA1,
  29. [QCRYPTO_HASH_ALGO_SHA224] = GCRY_MD_SHA224,
  30. [QCRYPTO_HASH_ALGO_SHA256] = GCRY_MD_SHA256,
  31. [QCRYPTO_HASH_ALGO_SHA384] = GCRY_MD_SHA384,
  32. [QCRYPTO_HASH_ALGO_SHA512] = GCRY_MD_SHA512,
  33. [QCRYPTO_HASH_ALGO_RIPEMD160] = GCRY_MD_RMD160,
  34. #ifdef CONFIG_CRYPTO_SM3
  35. [QCRYPTO_HASH_ALGO_SM3] = GCRY_MD_SM3,
  36. #endif
  37. };
  38. gboolean qcrypto_hash_supports(QCryptoHashAlgo alg)
  39. {
  40. if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map) &&
  41. qcrypto_hash_alg_map[alg] != GCRY_MD_NONE) {
  42. return gcry_md_test_algo(qcrypto_hash_alg_map[alg]) == 0;
  43. }
  44. return false;
  45. }
  46. static
  47. QCryptoHash *qcrypto_gcrypt_hash_new(QCryptoHashAlgo alg, Error **errp)
  48. {
  49. QCryptoHash *hash;
  50. gcry_error_t ret;
  51. hash = g_new(QCryptoHash, 1);
  52. hash->alg = alg;
  53. hash->opaque = g_new(gcry_md_hd_t, 1);
  54. ret = gcry_md_open((gcry_md_hd_t *) hash->opaque,
  55. qcrypto_hash_alg_map[alg], 0);
  56. if (ret != 0) {
  57. error_setg(errp,
  58. "Unable to initialize hash algorithm: %s",
  59. gcry_strerror(ret));
  60. g_free(hash->opaque);
  61. g_free(hash);
  62. return NULL;
  63. }
  64. return hash;
  65. }
  66. static
  67. void qcrypto_gcrypt_hash_free(QCryptoHash *hash)
  68. {
  69. gcry_md_hd_t *ctx = hash->opaque;
  70. if (ctx) {
  71. gcry_md_close(*ctx);
  72. g_free(ctx);
  73. }
  74. g_free(hash);
  75. }
  76. static
  77. int qcrypto_gcrypt_hash_update(QCryptoHash *hash,
  78. const struct iovec *iov,
  79. size_t niov,
  80. Error **errp)
  81. {
  82. gcry_md_hd_t *ctx = hash->opaque;
  83. for (int i = 0; i < niov; i++) {
  84. gcry_md_write(*ctx, iov[i].iov_base, iov[i].iov_len);
  85. }
  86. return 0;
  87. }
  88. static
  89. int qcrypto_gcrypt_hash_finalize(QCryptoHash *hash,
  90. uint8_t **result,
  91. size_t *result_len,
  92. Error **errp)
  93. {
  94. int ret;
  95. unsigned char *digest;
  96. gcry_md_hd_t *ctx = hash->opaque;
  97. ret = gcry_md_get_algo_dlen(qcrypto_hash_alg_map[hash->alg]);
  98. if (ret == 0) {
  99. error_setg(errp, "Unable to get hash length");
  100. return -1;
  101. }
  102. if (*result_len == 0) {
  103. *result_len = ret;
  104. *result = g_new(uint8_t, *result_len);
  105. } else if (*result_len != ret) {
  106. error_setg(errp,
  107. "Result buffer size %zu is smaller than hash %d",
  108. *result_len, ret);
  109. return -1;
  110. }
  111. /* Digest is freed by gcry_md_close(), copy it */
  112. digest = gcry_md_read(*ctx, 0);
  113. memcpy(*result, digest, *result_len);
  114. return 0;
  115. }
  116. QCryptoHashDriver qcrypto_hash_lib_driver = {
  117. .hash_new = qcrypto_gcrypt_hash_new,
  118. .hash_update = qcrypto_gcrypt_hash_update,
  119. .hash_finalize = qcrypto_gcrypt_hash_finalize,
  120. .hash_free = qcrypto_gcrypt_hash_free,
  121. };