2
0

hash.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * QEMU Crypto hash algorithms
  3. *
  4. * Copyright (c) 2015 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 "crypto/hash.h"
  22. #include "hashpriv.h"
  23. static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALG__MAX] = {
  24. [QCRYPTO_HASH_ALG_MD5] = 16,
  25. [QCRYPTO_HASH_ALG_SHA1] = 20,
  26. [QCRYPTO_HASH_ALG_SHA224] = 28,
  27. [QCRYPTO_HASH_ALG_SHA256] = 32,
  28. [QCRYPTO_HASH_ALG_SHA384] = 48,
  29. [QCRYPTO_HASH_ALG_SHA512] = 64,
  30. [QCRYPTO_HASH_ALG_RIPEMD160] = 20,
  31. };
  32. size_t qcrypto_hash_digest_len(QCryptoHashAlgorithm alg)
  33. {
  34. assert(alg < G_N_ELEMENTS(qcrypto_hash_alg_size));
  35. return qcrypto_hash_alg_size[alg];
  36. }
  37. int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
  38. const struct iovec *iov,
  39. size_t niov,
  40. uint8_t **result,
  41. size_t *resultlen,
  42. Error **errp)
  43. {
  44. #ifdef CONFIG_AF_ALG
  45. int ret;
  46. /*
  47. * TODO:
  48. * Maybe we should treat some afalg errors as fatal
  49. */
  50. ret = qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov,
  51. result, resultlen,
  52. NULL);
  53. if (ret == 0) {
  54. return ret;
  55. }
  56. #endif
  57. return qcrypto_hash_lib_driver.hash_bytesv(alg, iov, niov,
  58. result, resultlen,
  59. errp);
  60. }
  61. int qcrypto_hash_bytes(QCryptoHashAlgorithm alg,
  62. const char *buf,
  63. size_t len,
  64. uint8_t **result,
  65. size_t *resultlen,
  66. Error **errp)
  67. {
  68. struct iovec iov = { .iov_base = (char *)buf,
  69. .iov_len = len };
  70. return qcrypto_hash_bytesv(alg, &iov, 1, result, resultlen, errp);
  71. }
  72. static const char hex[] = "0123456789abcdef";
  73. int qcrypto_hash_digestv(QCryptoHashAlgorithm alg,
  74. const struct iovec *iov,
  75. size_t niov,
  76. char **digest,
  77. Error **errp)
  78. {
  79. uint8_t *result = NULL;
  80. size_t resultlen = 0;
  81. size_t i;
  82. if (qcrypto_hash_bytesv(alg, iov, niov, &result, &resultlen, errp) < 0) {
  83. return -1;
  84. }
  85. *digest = g_new0(char, (resultlen * 2) + 1);
  86. for (i = 0 ; i < resultlen ; i++) {
  87. (*digest)[(i * 2)] = hex[(result[i] >> 4) & 0xf];
  88. (*digest)[(i * 2) + 1] = hex[result[i] & 0xf];
  89. }
  90. (*digest)[resultlen * 2] = '\0';
  91. g_free(result);
  92. return 0;
  93. }
  94. int qcrypto_hash_digest(QCryptoHashAlgorithm alg,
  95. const char *buf,
  96. size_t len,
  97. char **digest,
  98. Error **errp)
  99. {
  100. struct iovec iov = { .iov_base = (char *)buf, .iov_len = len };
  101. return qcrypto_hash_digestv(alg, &iov, 1, digest, errp);
  102. }
  103. int qcrypto_hash_base64v(QCryptoHashAlgorithm alg,
  104. const struct iovec *iov,
  105. size_t niov,
  106. char **base64,
  107. Error **errp)
  108. {
  109. uint8_t *result = NULL;
  110. size_t resultlen = 0;
  111. if (qcrypto_hash_bytesv(alg, iov, niov, &result, &resultlen, errp) < 0) {
  112. return -1;
  113. }
  114. *base64 = g_base64_encode(result, resultlen);
  115. g_free(result);
  116. return 0;
  117. }
  118. int qcrypto_hash_base64(QCryptoHashAlgorithm alg,
  119. const char *buf,
  120. size_t len,
  121. char **base64,
  122. Error **errp)
  123. {
  124. struct iovec iov = { .iov_base = (char *)buf, .iov_len = len };
  125. return qcrypto_hash_base64v(alg, &iov, 1, base64, errp);
  126. }