hash-afalg.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * QEMU Crypto af_alg-backend hash/hmac support
  3. *
  4. * Copyright (c) 2017 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. #include "qemu/osdep.h"
  14. #include "qemu/iov.h"
  15. #include "qemu/sockets.h"
  16. #include "qapi/error.h"
  17. #include "crypto/hash.h"
  18. #include "crypto/hmac.h"
  19. #include "hashpriv.h"
  20. #include "hmacpriv.h"
  21. static char *
  22. qcrypto_afalg_hash_format_name(QCryptoHashAlgorithm alg,
  23. bool is_hmac,
  24. Error **errp)
  25. {
  26. char *name;
  27. const char *alg_name;
  28. switch (alg) {
  29. case QCRYPTO_HASH_ALG_MD5:
  30. alg_name = "md5";
  31. break;
  32. case QCRYPTO_HASH_ALG_SHA1:
  33. alg_name = "sha1";
  34. break;
  35. case QCRYPTO_HASH_ALG_SHA224:
  36. alg_name = "sha224";
  37. break;
  38. case QCRYPTO_HASH_ALG_SHA256:
  39. alg_name = "sha256";
  40. break;
  41. case QCRYPTO_HASH_ALG_SHA384:
  42. alg_name = "sha384";
  43. break;
  44. case QCRYPTO_HASH_ALG_SHA512:
  45. alg_name = "sha512";
  46. break;
  47. case QCRYPTO_HASH_ALG_RIPEMD160:
  48. alg_name = "rmd160";
  49. break;
  50. default:
  51. error_setg(errp, "Unsupported hash algorithm %d", alg);
  52. return NULL;
  53. }
  54. if (is_hmac) {
  55. name = g_strdup_printf("hmac(%s)", alg_name);
  56. } else {
  57. name = g_strdup_printf("%s", alg_name);
  58. }
  59. return name;
  60. }
  61. static QCryptoAFAlg *
  62. qcrypto_afalg_hash_hmac_ctx_new(QCryptoHashAlgorithm alg,
  63. const uint8_t *key, size_t nkey,
  64. bool is_hmac, Error **errp)
  65. {
  66. QCryptoAFAlg *afalg;
  67. char *name;
  68. name = qcrypto_afalg_hash_format_name(alg, is_hmac, errp);
  69. if (!name) {
  70. return NULL;
  71. }
  72. afalg = qcrypto_afalg_comm_alloc(AFALG_TYPE_HASH, name, errp);
  73. if (!afalg) {
  74. g_free(name);
  75. return NULL;
  76. }
  77. g_free(name);
  78. /* HMAC needs setkey */
  79. if (is_hmac) {
  80. if (setsockopt(afalg->tfmfd, SOL_ALG, ALG_SET_KEY,
  81. key, nkey) != 0) {
  82. error_setg_errno(errp, errno, "Set hmac key failed");
  83. qcrypto_afalg_comm_free(afalg);
  84. return NULL;
  85. }
  86. }
  87. return afalg;
  88. }
  89. static QCryptoAFAlg *
  90. qcrypto_afalg_hash_ctx_new(QCryptoHashAlgorithm alg,
  91. Error **errp)
  92. {
  93. return qcrypto_afalg_hash_hmac_ctx_new(alg, NULL, 0, false, errp);
  94. }
  95. QCryptoAFAlg *
  96. qcrypto_afalg_hmac_ctx_new(QCryptoHashAlgorithm alg,
  97. const uint8_t *key, size_t nkey,
  98. Error **errp)
  99. {
  100. return qcrypto_afalg_hash_hmac_ctx_new(alg, key, nkey, true, errp);
  101. }
  102. static int
  103. qcrypto_afalg_hash_hmac_bytesv(QCryptoAFAlg *hmac,
  104. QCryptoHashAlgorithm alg,
  105. const struct iovec *iov,
  106. size_t niov, uint8_t **result,
  107. size_t *resultlen,
  108. Error **errp)
  109. {
  110. QCryptoAFAlg *afalg;
  111. struct iovec outv;
  112. int ret = 0;
  113. bool is_hmac = (hmac != NULL) ? true : false;
  114. const int expect_len = qcrypto_hash_digest_len(alg);
  115. if (*resultlen == 0) {
  116. *resultlen = expect_len;
  117. *result = g_new0(uint8_t, *resultlen);
  118. } else if (*resultlen != expect_len) {
  119. error_setg(errp,
  120. "Result buffer size %zu is not match hash %d",
  121. *resultlen, expect_len);
  122. return -1;
  123. }
  124. if (is_hmac) {
  125. afalg = hmac;
  126. } else {
  127. afalg = qcrypto_afalg_hash_ctx_new(alg, errp);
  128. if (!afalg) {
  129. return -1;
  130. }
  131. }
  132. /* send data to kernel's crypto core */
  133. ret = iov_send_recv(afalg->opfd, iov, niov,
  134. 0, iov_size(iov, niov), true);
  135. if (ret < 0) {
  136. error_setg_errno(errp, errno, "Send data to afalg-core failed");
  137. goto out;
  138. }
  139. /* hash && get result */
  140. outv.iov_base = *result;
  141. outv.iov_len = *resultlen;
  142. ret = iov_send_recv(afalg->opfd, &outv, 1,
  143. 0, iov_size(&outv, 1), false);
  144. if (ret < 0) {
  145. error_setg_errno(errp, errno, "Recv result from afalg-core failed");
  146. } else {
  147. ret = 0;
  148. }
  149. out:
  150. if (!is_hmac) {
  151. qcrypto_afalg_comm_free(afalg);
  152. }
  153. return ret;
  154. }
  155. static int
  156. qcrypto_afalg_hash_bytesv(QCryptoHashAlgorithm alg,
  157. const struct iovec *iov,
  158. size_t niov, uint8_t **result,
  159. size_t *resultlen,
  160. Error **errp)
  161. {
  162. return qcrypto_afalg_hash_hmac_bytesv(NULL, alg, iov, niov, result,
  163. resultlen, errp);
  164. }
  165. static int
  166. qcrypto_afalg_hmac_bytesv(QCryptoHmac *hmac,
  167. const struct iovec *iov,
  168. size_t niov, uint8_t **result,
  169. size_t *resultlen,
  170. Error **errp)
  171. {
  172. return qcrypto_afalg_hash_hmac_bytesv(hmac->opaque, hmac->alg,
  173. iov, niov, result, resultlen,
  174. errp);
  175. }
  176. static void qcrypto_afalg_hmac_ctx_free(QCryptoHmac *hmac)
  177. {
  178. QCryptoAFAlg *afalg;
  179. afalg = hmac->opaque;
  180. qcrypto_afalg_comm_free(afalg);
  181. }
  182. QCryptoHashDriver qcrypto_hash_afalg_driver = {
  183. .hash_bytesv = qcrypto_afalg_hash_bytesv,
  184. };
  185. QCryptoHmacDriver qcrypto_hmac_afalg_driver = {
  186. .hmac_bytesv = qcrypto_afalg_hmac_bytesv,
  187. .hmac_free = qcrypto_afalg_hmac_ctx_free,
  188. };