hash-nettle.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #include <nettle/md5.h>
  25. #include <nettle/sha.h>
  26. #include <nettle/ripemd160.h>
  27. typedef void (*qcrypto_nettle_init)(void *ctx);
  28. typedef void (*qcrypto_nettle_write)(void *ctx,
  29. size_t len,
  30. const uint8_t *buf);
  31. typedef void (*qcrypto_nettle_result)(void *ctx,
  32. size_t len,
  33. uint8_t *buf);
  34. union qcrypto_hash_ctx {
  35. struct md5_ctx md5;
  36. struct sha1_ctx sha1;
  37. struct sha224_ctx sha224;
  38. struct sha256_ctx sha256;
  39. struct sha384_ctx sha384;
  40. struct sha512_ctx sha512;
  41. struct ripemd160_ctx ripemd160;
  42. };
  43. struct qcrypto_hash_alg {
  44. qcrypto_nettle_init init;
  45. qcrypto_nettle_write write;
  46. qcrypto_nettle_result result;
  47. size_t len;
  48. } qcrypto_hash_alg_map[] = {
  49. [QCRYPTO_HASH_ALG_MD5] = {
  50. .init = (qcrypto_nettle_init)md5_init,
  51. .write = (qcrypto_nettle_write)md5_update,
  52. .result = (qcrypto_nettle_result)md5_digest,
  53. .len = MD5_DIGEST_SIZE,
  54. },
  55. [QCRYPTO_HASH_ALG_SHA1] = {
  56. .init = (qcrypto_nettle_init)sha1_init,
  57. .write = (qcrypto_nettle_write)sha1_update,
  58. .result = (qcrypto_nettle_result)sha1_digest,
  59. .len = SHA1_DIGEST_SIZE,
  60. },
  61. [QCRYPTO_HASH_ALG_SHA224] = {
  62. .init = (qcrypto_nettle_init)sha224_init,
  63. .write = (qcrypto_nettle_write)sha224_update,
  64. .result = (qcrypto_nettle_result)sha224_digest,
  65. .len = SHA224_DIGEST_SIZE,
  66. },
  67. [QCRYPTO_HASH_ALG_SHA256] = {
  68. .init = (qcrypto_nettle_init)sha256_init,
  69. .write = (qcrypto_nettle_write)sha256_update,
  70. .result = (qcrypto_nettle_result)sha256_digest,
  71. .len = SHA256_DIGEST_SIZE,
  72. },
  73. [QCRYPTO_HASH_ALG_SHA384] = {
  74. .init = (qcrypto_nettle_init)sha384_init,
  75. .write = (qcrypto_nettle_write)sha384_update,
  76. .result = (qcrypto_nettle_result)sha384_digest,
  77. .len = SHA384_DIGEST_SIZE,
  78. },
  79. [QCRYPTO_HASH_ALG_SHA512] = {
  80. .init = (qcrypto_nettle_init)sha512_init,
  81. .write = (qcrypto_nettle_write)sha512_update,
  82. .result = (qcrypto_nettle_result)sha512_digest,
  83. .len = SHA512_DIGEST_SIZE,
  84. },
  85. [QCRYPTO_HASH_ALG_RIPEMD160] = {
  86. .init = (qcrypto_nettle_init)ripemd160_init,
  87. .write = (qcrypto_nettle_write)ripemd160_update,
  88. .result = (qcrypto_nettle_result)ripemd160_digest,
  89. .len = RIPEMD160_DIGEST_SIZE,
  90. },
  91. };
  92. gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg)
  93. {
  94. if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map) &&
  95. qcrypto_hash_alg_map[alg].init != NULL) {
  96. return true;
  97. }
  98. return false;
  99. }
  100. static int
  101. qcrypto_nettle_hash_bytesv(QCryptoHashAlgorithm alg,
  102. const struct iovec *iov,
  103. size_t niov,
  104. uint8_t **result,
  105. size_t *resultlen,
  106. Error **errp)
  107. {
  108. size_t i;
  109. union qcrypto_hash_ctx ctx;
  110. if (!qcrypto_hash_supports(alg)) {
  111. error_setg(errp,
  112. "Unknown hash algorithm %d",
  113. alg);
  114. return -1;
  115. }
  116. qcrypto_hash_alg_map[alg].init(&ctx);
  117. for (i = 0; i < niov; i++) {
  118. /* Some versions of nettle have functions
  119. * declared with 'int' instead of 'size_t'
  120. * so to be safe avoid writing more than
  121. * UINT_MAX bytes at a time
  122. */
  123. size_t len = iov[i].iov_len;
  124. uint8_t *base = iov[i].iov_base;
  125. while (len) {
  126. size_t shortlen = MIN(len, UINT_MAX);
  127. qcrypto_hash_alg_map[alg].write(&ctx, len, base);
  128. len -= shortlen;
  129. base += len;
  130. }
  131. }
  132. if (*resultlen == 0) {
  133. *resultlen = qcrypto_hash_alg_map[alg].len;
  134. *result = g_new0(uint8_t, *resultlen);
  135. } else if (*resultlen != qcrypto_hash_alg_map[alg].len) {
  136. error_setg(errp,
  137. "Result buffer size %zu is smaller than hash %zu",
  138. *resultlen, qcrypto_hash_alg_map[alg].len);
  139. return -1;
  140. }
  141. qcrypto_hash_alg_map[alg].result(&ctx, *resultlen, *result);
  142. return 0;
  143. }
  144. QCryptoHashDriver qcrypto_hash_lib_driver = {
  145. .hash_bytesv = qcrypto_nettle_hash_bytesv,
  146. };