hmac-gcrypt.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * QEMU Crypto hmac algorithms (based on libgcrypt)
  3. *
  4. * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
  5. *
  6. * Authors:
  7. * Longpeng(Mike) <longpeng2@huawei.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or
  10. * (at your option) any later version. See the COPYING file in the
  11. * top-level directory.
  12. *
  13. */
  14. #include "qemu/osdep.h"
  15. #include "qapi/error.h"
  16. #include "crypto/hmac.h"
  17. #include "hmacpriv.h"
  18. #include <gcrypt.h>
  19. static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = {
  20. [QCRYPTO_HASH_ALG_MD5] = GCRY_MAC_HMAC_MD5,
  21. [QCRYPTO_HASH_ALG_SHA1] = GCRY_MAC_HMAC_SHA1,
  22. [QCRYPTO_HASH_ALG_SHA224] = GCRY_MAC_HMAC_SHA224,
  23. [QCRYPTO_HASH_ALG_SHA256] = GCRY_MAC_HMAC_SHA256,
  24. [QCRYPTO_HASH_ALG_SHA384] = GCRY_MAC_HMAC_SHA384,
  25. [QCRYPTO_HASH_ALG_SHA512] = GCRY_MAC_HMAC_SHA512,
  26. [QCRYPTO_HASH_ALG_RIPEMD160] = GCRY_MAC_HMAC_RMD160,
  27. };
  28. typedef struct QCryptoHmacGcrypt QCryptoHmacGcrypt;
  29. struct QCryptoHmacGcrypt {
  30. gcry_mac_hd_t handle;
  31. };
  32. bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)
  33. {
  34. if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) &&
  35. qcrypto_hmac_alg_map[alg] != GCRY_MAC_NONE) {
  36. return true;
  37. }
  38. return false;
  39. }
  40. void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
  41. const uint8_t *key, size_t nkey,
  42. Error **errp)
  43. {
  44. QCryptoHmacGcrypt *ctx;
  45. gcry_error_t err;
  46. if (!qcrypto_hmac_supports(alg)) {
  47. error_setg(errp, "Unsupported hmac algorithm %s",
  48. QCryptoHashAlgorithm_str(alg));
  49. return NULL;
  50. }
  51. ctx = g_new0(QCryptoHmacGcrypt, 1);
  52. err = gcry_mac_open(&ctx->handle, qcrypto_hmac_alg_map[alg],
  53. GCRY_MAC_FLAG_SECURE, NULL);
  54. if (err != 0) {
  55. error_setg(errp, "Cannot initialize hmac: %s",
  56. gcry_strerror(err));
  57. goto error;
  58. }
  59. err = gcry_mac_setkey(ctx->handle, (const void *)key, nkey);
  60. if (err != 0) {
  61. error_setg(errp, "Cannot set key: %s",
  62. gcry_strerror(err));
  63. gcry_mac_close(ctx->handle);
  64. goto error;
  65. }
  66. return ctx;
  67. error:
  68. g_free(ctx);
  69. return NULL;
  70. }
  71. static void
  72. qcrypto_gcrypt_hmac_ctx_free(QCryptoHmac *hmac)
  73. {
  74. QCryptoHmacGcrypt *ctx;
  75. ctx = hmac->opaque;
  76. gcry_mac_close(ctx->handle);
  77. g_free(ctx);
  78. }
  79. static int
  80. qcrypto_gcrypt_hmac_bytesv(QCryptoHmac *hmac,
  81. const struct iovec *iov,
  82. size_t niov,
  83. uint8_t **result,
  84. size_t *resultlen,
  85. Error **errp)
  86. {
  87. QCryptoHmacGcrypt *ctx;
  88. gcry_error_t err;
  89. uint32_t ret;
  90. int i;
  91. ctx = hmac->opaque;
  92. for (i = 0; i < niov; i++) {
  93. gcry_mac_write(ctx->handle, iov[i].iov_base, iov[i].iov_len);
  94. }
  95. ret = gcry_mac_get_algo_maclen(qcrypto_hmac_alg_map[hmac->alg]);
  96. if (ret <= 0) {
  97. error_setg(errp, "Unable to get hmac length: %s",
  98. gcry_strerror(ret));
  99. return -1;
  100. }
  101. if (*resultlen == 0) {
  102. *resultlen = ret;
  103. *result = g_new0(uint8_t, *resultlen);
  104. } else if (*resultlen != ret) {
  105. error_setg(errp, "Result buffer size %zu is smaller than hmac %d",
  106. *resultlen, ret);
  107. return -1;
  108. }
  109. err = gcry_mac_read(ctx->handle, *result, resultlen);
  110. if (err != 0) {
  111. error_setg(errp, "Cannot get result: %s",
  112. gcry_strerror(err));
  113. return -1;
  114. }
  115. err = gcry_mac_reset(ctx->handle);
  116. if (err != 0) {
  117. error_setg(errp, "Cannot reset hmac context: %s",
  118. gcry_strerror(err));
  119. return -1;
  120. }
  121. return 0;
  122. }
  123. QCryptoHmacDriver qcrypto_hmac_lib_driver = {
  124. .hmac_bytesv = qcrypto_gcrypt_hmac_bytesv,
  125. .hmac_free = qcrypto_gcrypt_hmac_ctx_free,
  126. };