2
0

hash-gcrypt.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * QEMU Crypto hash algorithms
  3. *
  4. * Copyright (c) 2016 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include <gcrypt.h>
  22. #include "qapi/error.h"
  23. #include "crypto/hash.h"
  24. #include "hashpriv.h"
  25. static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG__MAX] = {
  26. [QCRYPTO_HASH_ALG_MD5] = GCRY_MD_MD5,
  27. [QCRYPTO_HASH_ALG_SHA1] = GCRY_MD_SHA1,
  28. [QCRYPTO_HASH_ALG_SHA224] = GCRY_MD_SHA224,
  29. [QCRYPTO_HASH_ALG_SHA256] = GCRY_MD_SHA256,
  30. [QCRYPTO_HASH_ALG_SHA384] = GCRY_MD_SHA384,
  31. [QCRYPTO_HASH_ALG_SHA512] = GCRY_MD_SHA512,
  32. [QCRYPTO_HASH_ALG_RIPEMD160] = GCRY_MD_RMD160,
  33. };
  34. gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg)
  35. {
  36. if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map) &&
  37. qcrypto_hash_alg_map[alg] != GCRY_MD_NONE) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. static int
  43. qcrypto_gcrypt_hash_bytesv(QCryptoHashAlgorithm alg,
  44. const struct iovec *iov,
  45. size_t niov,
  46. uint8_t **result,
  47. size_t *resultlen,
  48. Error **errp)
  49. {
  50. int i, ret;
  51. gcry_md_hd_t md;
  52. unsigned char *digest;
  53. if (!qcrypto_hash_supports(alg)) {
  54. error_setg(errp,
  55. "Unknown hash algorithm %d",
  56. alg);
  57. return -1;
  58. }
  59. ret = gcry_md_open(&md, qcrypto_hash_alg_map[alg], 0);
  60. if (ret < 0) {
  61. error_setg(errp,
  62. "Unable to initialize hash algorithm: %s",
  63. gcry_strerror(ret));
  64. return -1;
  65. }
  66. for (i = 0; i < niov; i++) {
  67. gcry_md_write(md, iov[i].iov_base, iov[i].iov_len);
  68. }
  69. ret = gcry_md_get_algo_dlen(qcrypto_hash_alg_map[alg]);
  70. if (ret <= 0) {
  71. error_setg(errp,
  72. "Unable to get hash length: %s",
  73. gcry_strerror(ret));
  74. goto error;
  75. }
  76. if (*resultlen == 0) {
  77. *resultlen = ret;
  78. *result = g_new0(uint8_t, *resultlen);
  79. } else if (*resultlen != ret) {
  80. error_setg(errp,
  81. "Result buffer size %zu is smaller than hash %d",
  82. *resultlen, ret);
  83. goto error;
  84. }
  85. digest = gcry_md_read(md, 0);
  86. if (!digest) {
  87. error_setg(errp,
  88. "No digest produced");
  89. goto error;
  90. }
  91. memcpy(*result, digest, *resultlen);
  92. gcry_md_close(md);
  93. return 0;
  94. error:
  95. gcry_md_close(md);
  96. return -1;
  97. }
  98. QCryptoHashDriver qcrypto_hash_lib_driver = {
  99. .hash_bytesv = qcrypto_gcrypt_hash_bytesv,
  100. };