hash-glib.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "qapi/error.h"
  22. #include "crypto/hash.h"
  23. #include "hashpriv.h"
  24. static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG__MAX] = {
  25. [QCRYPTO_HASH_ALG_MD5] = G_CHECKSUM_MD5,
  26. [QCRYPTO_HASH_ALG_SHA1] = G_CHECKSUM_SHA1,
  27. [QCRYPTO_HASH_ALG_SHA224] = -1,
  28. [QCRYPTO_HASH_ALG_SHA256] = G_CHECKSUM_SHA256,
  29. [QCRYPTO_HASH_ALG_SHA384] = -1,
  30. [QCRYPTO_HASH_ALG_SHA512] = G_CHECKSUM_SHA512,
  31. [QCRYPTO_HASH_ALG_RIPEMD160] = -1,
  32. };
  33. gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg)
  34. {
  35. if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map) &&
  36. qcrypto_hash_alg_map[alg] != -1) {
  37. return true;
  38. }
  39. return false;
  40. }
  41. static int
  42. qcrypto_glib_hash_bytesv(QCryptoHashAlgorithm alg,
  43. const struct iovec *iov,
  44. size_t niov,
  45. uint8_t **result,
  46. size_t *resultlen,
  47. Error **errp)
  48. {
  49. int i, ret;
  50. GChecksum *cs;
  51. if (!qcrypto_hash_supports(alg)) {
  52. error_setg(errp,
  53. "Unknown hash algorithm %d",
  54. alg);
  55. return -1;
  56. }
  57. cs = g_checksum_new(qcrypto_hash_alg_map[alg]);
  58. for (i = 0; i < niov; i++) {
  59. g_checksum_update(cs, iov[i].iov_base, iov[i].iov_len);
  60. }
  61. ret = g_checksum_type_get_length(qcrypto_hash_alg_map[alg]);
  62. if (ret < 0) {
  63. error_setg(errp, "%s",
  64. "Unable to get hash length");
  65. goto error;
  66. }
  67. if (*resultlen == 0) {
  68. *resultlen = ret;
  69. *result = g_new0(uint8_t, *resultlen);
  70. } else if (*resultlen != ret) {
  71. error_setg(errp,
  72. "Result buffer size %zu is smaller than hash %d",
  73. *resultlen, ret);
  74. goto error;
  75. }
  76. g_checksum_get_digest(cs, *result, resultlen);
  77. g_checksum_free(cs);
  78. return 0;
  79. error:
  80. g_checksum_free(cs);
  81. return -1;
  82. }
  83. QCryptoHashDriver qcrypto_hash_lib_driver = {
  84. .hash_bytesv = qcrypto_glib_hash_bytesv,
  85. };