2
0

hmac.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * QEMU Crypto hmac algorithms
  3. *
  4. * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or
  7. * (at your option) any later version. See the COPYING file in the
  8. * top-level directory.
  9. *
  10. */
  11. #include "qemu/osdep.h"
  12. #include "crypto/hmac.h"
  13. #include "hmacpriv.h"
  14. static const char hex[] = "0123456789abcdef";
  15. int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
  16. const struct iovec *iov,
  17. size_t niov,
  18. uint8_t **result,
  19. size_t *resultlen,
  20. Error **errp)
  21. {
  22. QCryptoHmacDriver *drv = hmac->driver;
  23. return drv->hmac_bytesv(hmac, iov, niov, result, resultlen, errp);
  24. }
  25. int qcrypto_hmac_bytes(QCryptoHmac *hmac,
  26. const char *buf,
  27. size_t len,
  28. uint8_t **result,
  29. size_t *resultlen,
  30. Error **errp)
  31. {
  32. struct iovec iov = {
  33. .iov_base = (char *)buf,
  34. .iov_len = len
  35. };
  36. return qcrypto_hmac_bytesv(hmac, &iov, 1, result, resultlen, errp);
  37. }
  38. int qcrypto_hmac_digestv(QCryptoHmac *hmac,
  39. const struct iovec *iov,
  40. size_t niov,
  41. char **digest,
  42. Error **errp)
  43. {
  44. uint8_t *result = NULL;
  45. size_t resultlen = 0;
  46. size_t i;
  47. if (qcrypto_hmac_bytesv(hmac, iov, niov, &result, &resultlen, errp) < 0) {
  48. return -1;
  49. }
  50. *digest = g_new0(char, (resultlen * 2) + 1);
  51. for (i = 0 ; i < resultlen ; i++) {
  52. (*digest)[(i * 2)] = hex[(result[i] >> 4) & 0xf];
  53. (*digest)[(i * 2) + 1] = hex[result[i] & 0xf];
  54. }
  55. (*digest)[resultlen * 2] = '\0';
  56. g_free(result);
  57. return 0;
  58. }
  59. int qcrypto_hmac_digest(QCryptoHmac *hmac,
  60. const char *buf,
  61. size_t len,
  62. char **digest,
  63. Error **errp)
  64. {
  65. struct iovec iov = {
  66. .iov_base = (char *)buf,
  67. .iov_len = len
  68. };
  69. return qcrypto_hmac_digestv(hmac, &iov, 1, digest, errp);
  70. }
  71. QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg,
  72. const uint8_t *key, size_t nkey,
  73. Error **errp)
  74. {
  75. QCryptoHmac *hmac;
  76. void *ctx = NULL;
  77. QCryptoHmacDriver *drv = NULL;
  78. #ifdef CONFIG_AF_ALG
  79. ctx = qcrypto_afalg_hmac_ctx_new(alg, key, nkey, NULL);
  80. if (ctx) {
  81. drv = &qcrypto_hmac_afalg_driver;
  82. }
  83. #endif
  84. if (!ctx) {
  85. ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp);
  86. if (!ctx) {
  87. return NULL;
  88. }
  89. drv = &qcrypto_hmac_lib_driver;
  90. }
  91. hmac = g_new0(QCryptoHmac, 1);
  92. hmac->alg = alg;
  93. hmac->opaque = ctx;
  94. hmac->driver = (void *)drv;
  95. return hmac;
  96. }
  97. void qcrypto_hmac_free(QCryptoHmac *hmac)
  98. {
  99. QCryptoHmacDriver *drv;
  100. if (hmac) {
  101. drv = hmac->driver;
  102. drv->hmac_free(hmac);
  103. g_free(hmac);
  104. }
  105. }