2
0

hash-nettle.c 5.2 KB

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