2
0

hmac-glib.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * QEMU Crypto hmac algorithms (based on glib)
  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. static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = {
  19. [QCRYPTO_HASH_ALG_MD5] = G_CHECKSUM_MD5,
  20. [QCRYPTO_HASH_ALG_SHA1] = G_CHECKSUM_SHA1,
  21. [QCRYPTO_HASH_ALG_SHA256] = G_CHECKSUM_SHA256,
  22. [QCRYPTO_HASH_ALG_SHA512] = G_CHECKSUM_SHA512,
  23. [QCRYPTO_HASH_ALG_SHA224] = -1,
  24. [QCRYPTO_HASH_ALG_SHA384] = -1,
  25. [QCRYPTO_HASH_ALG_RIPEMD160] = -1,
  26. };
  27. typedef struct QCryptoHmacGlib QCryptoHmacGlib;
  28. struct QCryptoHmacGlib {
  29. GHmac *ghmac;
  30. };
  31. bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)
  32. {
  33. if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) &&
  34. qcrypto_hmac_alg_map[alg] != -1) {
  35. return true;
  36. }
  37. return false;
  38. }
  39. void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
  40. const uint8_t *key, size_t nkey,
  41. Error **errp)
  42. {
  43. QCryptoHmacGlib *ctx;
  44. if (!qcrypto_hmac_supports(alg)) {
  45. error_setg(errp, "Unsupported hmac algorithm %s",
  46. QCryptoHashAlgorithm_str(alg));
  47. return NULL;
  48. }
  49. ctx = g_new0(QCryptoHmacGlib, 1);
  50. ctx->ghmac = g_hmac_new(qcrypto_hmac_alg_map[alg],
  51. (const uint8_t *)key, nkey);
  52. if (!ctx->ghmac) {
  53. error_setg(errp, "Cannot initialize hmac and set key");
  54. goto error;
  55. }
  56. return ctx;
  57. error:
  58. g_free(ctx);
  59. return NULL;
  60. }
  61. static void
  62. qcrypto_glib_hmac_ctx_free(QCryptoHmac *hmac)
  63. {
  64. QCryptoHmacGlib *ctx;
  65. ctx = hmac->opaque;
  66. g_hmac_unref(ctx->ghmac);
  67. g_free(ctx);
  68. }
  69. static int
  70. qcrypto_glib_hmac_bytesv(QCryptoHmac *hmac,
  71. const struct iovec *iov,
  72. size_t niov,
  73. uint8_t **result,
  74. size_t *resultlen,
  75. Error **errp)
  76. {
  77. QCryptoHmacGlib *ctx;
  78. int i, ret;
  79. ctx = hmac->opaque;
  80. for (i = 0; i < niov; i++) {
  81. g_hmac_update(ctx->ghmac, iov[i].iov_base, iov[i].iov_len);
  82. }
  83. ret = g_checksum_type_get_length(qcrypto_hmac_alg_map[hmac->alg]);
  84. if (ret < 0) {
  85. error_setg(errp, "Unable to get hmac length");
  86. return -1;
  87. }
  88. if (*resultlen == 0) {
  89. *resultlen = ret;
  90. *result = g_new0(uint8_t, *resultlen);
  91. } else if (*resultlen != ret) {
  92. error_setg(errp, "Result buffer size %zu is smaller than hmac %d",
  93. *resultlen, ret);
  94. return -1;
  95. }
  96. g_hmac_get_digest(ctx->ghmac, *result, resultlen);
  97. return 0;
  98. }
  99. QCryptoHmacDriver qcrypto_hmac_lib_driver = {
  100. .hmac_bytesv = qcrypto_glib_hmac_bytesv,
  101. .hmac_free = qcrypto_glib_hmac_ctx_free,
  102. };