hash-afalg.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * QEMU Crypto af_alg-backend hash/hmac support
  3. *
  4. * Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates
  5. * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD.
  6. *
  7. * Authors:
  8. * Longpeng(Mike) <longpeng2@huawei.com>
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2 or
  11. * (at your option) any later version. See the COPYING file in the
  12. * top-level directory.
  13. */
  14. #include "qemu/osdep.h"
  15. #include "qemu/iov.h"
  16. #include "qemu/sockets.h"
  17. #include "qapi/error.h"
  18. #include "crypto/hash.h"
  19. #include "crypto/hmac.h"
  20. #include "hashpriv.h"
  21. #include "hmacpriv.h"
  22. static char *
  23. qcrypto_afalg_hash_format_name(QCryptoHashAlgo alg,
  24. bool is_hmac,
  25. Error **errp)
  26. {
  27. char *name;
  28. const char *alg_name;
  29. switch (alg) {
  30. case QCRYPTO_HASH_ALGO_MD5:
  31. alg_name = "md5";
  32. break;
  33. case QCRYPTO_HASH_ALGO_SHA1:
  34. alg_name = "sha1";
  35. break;
  36. case QCRYPTO_HASH_ALGO_SHA224:
  37. alg_name = "sha224";
  38. break;
  39. case QCRYPTO_HASH_ALGO_SHA256:
  40. alg_name = "sha256";
  41. break;
  42. case QCRYPTO_HASH_ALGO_SHA384:
  43. alg_name = "sha384";
  44. break;
  45. case QCRYPTO_HASH_ALGO_SHA512:
  46. alg_name = "sha512";
  47. break;
  48. case QCRYPTO_HASH_ALGO_RIPEMD160:
  49. alg_name = "rmd160";
  50. break;
  51. default:
  52. error_setg(errp, "Unsupported hash algorithm %d", alg);
  53. return NULL;
  54. }
  55. if (is_hmac) {
  56. name = g_strdup_printf("hmac(%s)", alg_name);
  57. } else {
  58. name = g_strdup_printf("%s", alg_name);
  59. }
  60. return name;
  61. }
  62. static QCryptoAFAlgo *
  63. qcrypto_afalg_hash_hmac_ctx_new(QCryptoHashAlgo alg,
  64. const uint8_t *key, size_t nkey,
  65. bool is_hmac, Error **errp)
  66. {
  67. QCryptoAFAlgo *afalg;
  68. char *name;
  69. name = qcrypto_afalg_hash_format_name(alg, is_hmac, errp);
  70. if (!name) {
  71. return NULL;
  72. }
  73. afalg = qcrypto_afalg_comm_alloc(AFALG_TYPE_HASH, name, errp);
  74. if (!afalg) {
  75. g_free(name);
  76. return NULL;
  77. }
  78. g_free(name);
  79. /* HMAC needs setkey */
  80. if (is_hmac) {
  81. if (setsockopt(afalg->tfmfd, SOL_ALG, ALG_SET_KEY,
  82. key, nkey) != 0) {
  83. error_setg_errno(errp, errno, "Set hmac key failed");
  84. qcrypto_afalg_comm_free(afalg);
  85. return NULL;
  86. }
  87. }
  88. return afalg;
  89. }
  90. static QCryptoAFAlgo *
  91. qcrypto_afalg_hash_ctx_new(QCryptoHashAlgo alg,
  92. Error **errp)
  93. {
  94. return qcrypto_afalg_hash_hmac_ctx_new(alg, NULL, 0, false, errp);
  95. }
  96. QCryptoAFAlgo *
  97. qcrypto_afalg_hmac_ctx_new(QCryptoHashAlgo alg,
  98. const uint8_t *key, size_t nkey,
  99. Error **errp)
  100. {
  101. return qcrypto_afalg_hash_hmac_ctx_new(alg, key, nkey, true, errp);
  102. }
  103. static
  104. QCryptoHash *qcrypto_afalg_hash_new(QCryptoHashAlgo alg, Error **errp)
  105. {
  106. /* Check if hash algorithm is supported */
  107. char *alg_name = qcrypto_afalg_hash_format_name(alg, false, NULL);
  108. QCryptoHash *hash;
  109. if (alg_name == NULL) {
  110. error_setg(errp, "Unknown hash algorithm %d", alg);
  111. return NULL;
  112. }
  113. g_free(alg_name);
  114. hash = g_new(QCryptoHash, 1);
  115. hash->alg = alg;
  116. hash->opaque = qcrypto_afalg_hash_ctx_new(alg, errp);
  117. if (!hash->opaque) {
  118. free(hash);
  119. return NULL;
  120. }
  121. return hash;
  122. }
  123. static
  124. void qcrypto_afalg_hash_free(QCryptoHash *hash)
  125. {
  126. QCryptoAFAlgo *ctx = hash->opaque;
  127. if (ctx) {
  128. qcrypto_afalg_comm_free(ctx);
  129. }
  130. g_free(hash);
  131. }
  132. /**
  133. * Send data to the kernel's crypto core.
  134. *
  135. * The more_data parameter is used to notify the crypto engine
  136. * that this is an "update" operation, and that more data will
  137. * be provided to calculate the final hash.
  138. */
  139. static
  140. int qcrypto_afalg_send_to_kernel(QCryptoAFAlgo *afalg,
  141. const struct iovec *iov,
  142. size_t niov,
  143. bool more_data,
  144. Error **errp)
  145. {
  146. int ret = 0;
  147. int flags = (more_data ? MSG_MORE : 0);
  148. /* send data to kernel's crypto core */
  149. ret = iov_send_recv_with_flags(afalg->opfd, flags, iov, niov,
  150. 0, iov_size(iov, niov), true);
  151. if (ret < 0) {
  152. error_setg_errno(errp, errno, "Send data to afalg-core failed");
  153. ret = -1;
  154. } else {
  155. /* No error, so return 0 */
  156. ret = 0;
  157. }
  158. return ret;
  159. }
  160. static
  161. int qcrypto_afalg_recv_from_kernel(QCryptoAFAlgo *afalg,
  162. QCryptoHashAlgo alg,
  163. uint8_t **result,
  164. size_t *result_len,
  165. Error **errp)
  166. {
  167. struct iovec outv;
  168. int ret;
  169. const int expected_len = qcrypto_hash_digest_len(alg);
  170. if (*result_len == 0) {
  171. *result_len = expected_len;
  172. *result = g_new0(uint8_t, *result_len);
  173. } else if (*result_len != expected_len) {
  174. error_setg(errp,
  175. "Result buffer size %zu is not match hash %d",
  176. *result_len, expected_len);
  177. return -1;
  178. }
  179. /* hash && get result */
  180. outv.iov_base = *result;
  181. outv.iov_len = *result_len;
  182. ret = iov_send_recv(afalg->opfd, &outv, 1,
  183. 0, iov_size(&outv, 1), false);
  184. if (ret < 0) {
  185. error_setg_errno(errp, errno, "Recv result from afalg-core failed");
  186. return -1;
  187. }
  188. return 0;
  189. }
  190. static
  191. int qcrypto_afalg_hash_update(QCryptoHash *hash,
  192. const struct iovec *iov,
  193. size_t niov,
  194. Error **errp)
  195. {
  196. return qcrypto_afalg_send_to_kernel((QCryptoAFAlgo *) hash->opaque,
  197. iov, niov, true, errp);
  198. }
  199. static
  200. int qcrypto_afalg_hash_finalize(QCryptoHash *hash,
  201. uint8_t **result,
  202. size_t *result_len,
  203. Error **errp)
  204. {
  205. return qcrypto_afalg_recv_from_kernel((QCryptoAFAlgo *) hash->opaque,
  206. hash->alg, result, result_len, errp);
  207. }
  208. static int
  209. qcrypto_afalg_hash_hmac_bytesv(QCryptoAFAlgo *hmac,
  210. QCryptoHashAlgo alg,
  211. const struct iovec *iov,
  212. size_t niov, uint8_t **result,
  213. size_t *resultlen,
  214. Error **errp)
  215. {
  216. int ret = 0;
  217. ret = qcrypto_afalg_send_to_kernel(hmac, iov, niov, false, errp);
  218. if (ret == 0) {
  219. ret = qcrypto_afalg_recv_from_kernel(hmac, alg, result,
  220. resultlen, errp);
  221. }
  222. return ret;
  223. }
  224. static int
  225. qcrypto_afalg_hmac_bytesv(QCryptoHmac *hmac,
  226. const struct iovec *iov,
  227. size_t niov, uint8_t **result,
  228. size_t *resultlen,
  229. Error **errp)
  230. {
  231. return qcrypto_afalg_hash_hmac_bytesv(hmac->opaque, hmac->alg,
  232. iov, niov, result, resultlen,
  233. errp);
  234. }
  235. static void qcrypto_afalg_hmac_ctx_free(QCryptoHmac *hmac)
  236. {
  237. QCryptoAFAlgo *afalg;
  238. afalg = hmac->opaque;
  239. qcrypto_afalg_comm_free(afalg);
  240. }
  241. QCryptoHashDriver qcrypto_hash_afalg_driver = {
  242. .hash_new = qcrypto_afalg_hash_new,
  243. .hash_free = qcrypto_afalg_hash_free,
  244. .hash_update = qcrypto_afalg_hash_update,
  245. .hash_finalize = qcrypto_afalg_hash_finalize
  246. };
  247. QCryptoHmacDriver qcrypto_hmac_afalg_driver = {
  248. .hmac_bytesv = qcrypto_afalg_hmac_bytesv,
  249. .hmac_free = qcrypto_afalg_hmac_ctx_free,
  250. };