hash-nettle.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * QEMU Crypto hash algorithms
  3. *
  4. * Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates
  5. * Copyright (c) 2016 Red Hat, Inc.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include "qemu/osdep.h"
  22. #include "qapi/error.h"
  23. #include "crypto/hash.h"
  24. #include "hashpriv.h"
  25. #include <nettle/md5.h>
  26. #include <nettle/sha.h>
  27. #include <nettle/ripemd160.h>
  28. #ifdef CONFIG_CRYPTO_SM3
  29. #include <nettle/sm3.h>
  30. #endif
  31. typedef void (*qcrypto_nettle_init)(void *ctx);
  32. typedef void (*qcrypto_nettle_write)(void *ctx,
  33. size_t len,
  34. const uint8_t *buf);
  35. typedef void (*qcrypto_nettle_result)(void *ctx,
  36. size_t len,
  37. uint8_t *buf);
  38. union qcrypto_hash_ctx {
  39. struct md5_ctx md5;
  40. struct sha1_ctx sha1;
  41. struct sha224_ctx sha224;
  42. struct sha256_ctx sha256;
  43. struct sha384_ctx sha384;
  44. struct sha512_ctx sha512;
  45. struct ripemd160_ctx ripemd160;
  46. #ifdef CONFIG_CRYPTO_SM3
  47. struct sm3_ctx sm3;
  48. #endif
  49. };
  50. struct qcrypto_hash_alg {
  51. qcrypto_nettle_init init;
  52. qcrypto_nettle_write write;
  53. qcrypto_nettle_result result;
  54. size_t len;
  55. } qcrypto_hash_alg_map[] = {
  56. [QCRYPTO_HASH_ALGO_MD5] = {
  57. .init = (qcrypto_nettle_init)md5_init,
  58. .write = (qcrypto_nettle_write)md5_update,
  59. .result = (qcrypto_nettle_result)md5_digest,
  60. .len = MD5_DIGEST_SIZE,
  61. },
  62. [QCRYPTO_HASH_ALGO_SHA1] = {
  63. .init = (qcrypto_nettle_init)sha1_init,
  64. .write = (qcrypto_nettle_write)sha1_update,
  65. .result = (qcrypto_nettle_result)sha1_digest,
  66. .len = SHA1_DIGEST_SIZE,
  67. },
  68. [QCRYPTO_HASH_ALGO_SHA224] = {
  69. .init = (qcrypto_nettle_init)sha224_init,
  70. .write = (qcrypto_nettle_write)sha224_update,
  71. .result = (qcrypto_nettle_result)sha224_digest,
  72. .len = SHA224_DIGEST_SIZE,
  73. },
  74. [QCRYPTO_HASH_ALGO_SHA256] = {
  75. .init = (qcrypto_nettle_init)sha256_init,
  76. .write = (qcrypto_nettle_write)sha256_update,
  77. .result = (qcrypto_nettle_result)sha256_digest,
  78. .len = SHA256_DIGEST_SIZE,
  79. },
  80. [QCRYPTO_HASH_ALGO_SHA384] = {
  81. .init = (qcrypto_nettle_init)sha384_init,
  82. .write = (qcrypto_nettle_write)sha384_update,
  83. .result = (qcrypto_nettle_result)sha384_digest,
  84. .len = SHA384_DIGEST_SIZE,
  85. },
  86. [QCRYPTO_HASH_ALGO_SHA512] = {
  87. .init = (qcrypto_nettle_init)sha512_init,
  88. .write = (qcrypto_nettle_write)sha512_update,
  89. .result = (qcrypto_nettle_result)sha512_digest,
  90. .len = SHA512_DIGEST_SIZE,
  91. },
  92. [QCRYPTO_HASH_ALGO_RIPEMD160] = {
  93. .init = (qcrypto_nettle_init)ripemd160_init,
  94. .write = (qcrypto_nettle_write)ripemd160_update,
  95. .result = (qcrypto_nettle_result)ripemd160_digest,
  96. .len = RIPEMD160_DIGEST_SIZE,
  97. },
  98. #ifdef CONFIG_CRYPTO_SM3
  99. [QCRYPTO_HASH_ALGO_SM3] = {
  100. .init = (qcrypto_nettle_init)sm3_init,
  101. .write = (qcrypto_nettle_write)sm3_update,
  102. .result = (qcrypto_nettle_result)sm3_digest,
  103. .len = SM3_DIGEST_SIZE,
  104. },
  105. #endif
  106. };
  107. gboolean qcrypto_hash_supports(QCryptoHashAlgo alg)
  108. {
  109. if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map) &&
  110. qcrypto_hash_alg_map[alg].init != NULL) {
  111. return true;
  112. }
  113. return false;
  114. }
  115. static
  116. QCryptoHash *qcrypto_nettle_hash_new(QCryptoHashAlgo alg, Error **errp)
  117. {
  118. QCryptoHash *hash;
  119. hash = g_new(QCryptoHash, 1);
  120. hash->alg = alg;
  121. hash->opaque = g_new(union qcrypto_hash_ctx, 1);
  122. qcrypto_hash_alg_map[alg].init(hash->opaque);
  123. return hash;
  124. }
  125. static
  126. void qcrypto_nettle_hash_free(QCryptoHash *hash)
  127. {
  128. union qcrypto_hash_ctx *ctx = hash->opaque;
  129. g_free(ctx);
  130. g_free(hash);
  131. }
  132. static
  133. int qcrypto_nettle_hash_update(QCryptoHash *hash,
  134. const struct iovec *iov,
  135. size_t niov,
  136. Error **errp)
  137. {
  138. union qcrypto_hash_ctx *ctx = hash->opaque;
  139. for (int i = 0; i < niov; i++) {
  140. qcrypto_hash_alg_map[hash->alg].write(ctx,
  141. iov[i].iov_len,
  142. iov[i].iov_base);
  143. }
  144. return 0;
  145. }
  146. static
  147. int qcrypto_nettle_hash_finalize(QCryptoHash *hash,
  148. uint8_t **result,
  149. size_t *result_len,
  150. Error **errp)
  151. {
  152. union qcrypto_hash_ctx *ctx = hash->opaque;
  153. int ret = qcrypto_hash_alg_map[hash->alg].len;
  154. if (*result_len == 0) {
  155. *result_len = ret;
  156. *result = g_new(uint8_t, *result_len);
  157. } else if (*result_len != ret) {
  158. error_setg(errp,
  159. "Result buffer size %zu is smaller than hash %d",
  160. *result_len, ret);
  161. return -1;
  162. }
  163. qcrypto_hash_alg_map[hash->alg].result(ctx, *result_len, *result);
  164. return 0;
  165. }
  166. QCryptoHashDriver qcrypto_hash_lib_driver = {
  167. .hash_new = qcrypto_nettle_hash_new,
  168. .hash_update = qcrypto_nettle_hash_update,
  169. .hash_finalize = qcrypto_nettle_hash_finalize,
  170. .hash_free = qcrypto_nettle_hash_free,
  171. };