2
0

cipher-afalg.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * QEMU Crypto af_alg-backend cipher 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/sockets.h"
  15. #include "qemu-common.h"
  16. #include "qapi/error.h"
  17. #include "crypto/cipher.h"
  18. #include "cipherpriv.h"
  19. static char *
  20. qcrypto_afalg_cipher_format_name(QCryptoCipherAlgorithm alg,
  21. QCryptoCipherMode mode,
  22. Error **errp)
  23. {
  24. char *name;
  25. const char *alg_name;
  26. const char *mode_name;
  27. switch (alg) {
  28. case QCRYPTO_CIPHER_ALG_AES_128:
  29. case QCRYPTO_CIPHER_ALG_AES_192:
  30. case QCRYPTO_CIPHER_ALG_AES_256:
  31. alg_name = "aes";
  32. break;
  33. case QCRYPTO_CIPHER_ALG_CAST5_128:
  34. alg_name = "cast5";
  35. break;
  36. case QCRYPTO_CIPHER_ALG_SERPENT_128:
  37. case QCRYPTO_CIPHER_ALG_SERPENT_192:
  38. case QCRYPTO_CIPHER_ALG_SERPENT_256:
  39. alg_name = "serpent";
  40. break;
  41. case QCRYPTO_CIPHER_ALG_TWOFISH_128:
  42. case QCRYPTO_CIPHER_ALG_TWOFISH_192:
  43. case QCRYPTO_CIPHER_ALG_TWOFISH_256:
  44. alg_name = "twofish";
  45. break;
  46. default:
  47. error_setg(errp, "Unsupported cipher algorithm %d", alg);
  48. return NULL;
  49. }
  50. mode_name = QCryptoCipherMode_str(mode);
  51. name = g_strdup_printf("%s(%s)", mode_name, alg_name);
  52. return name;
  53. }
  54. QCryptoAFAlg *
  55. qcrypto_afalg_cipher_ctx_new(QCryptoCipherAlgorithm alg,
  56. QCryptoCipherMode mode,
  57. const uint8_t *key,
  58. size_t nkey, Error **errp)
  59. {
  60. QCryptoAFAlg *afalg;
  61. size_t expect_niv;
  62. char *name;
  63. name = qcrypto_afalg_cipher_format_name(alg, mode, errp);
  64. if (!name) {
  65. return NULL;
  66. }
  67. afalg = qcrypto_afalg_comm_alloc(AFALG_TYPE_CIPHER, name, errp);
  68. if (!afalg) {
  69. g_free(name);
  70. return NULL;
  71. }
  72. g_free(name);
  73. /* setkey */
  74. if (qemu_setsockopt(afalg->tfmfd, SOL_ALG, ALG_SET_KEY, key,
  75. nkey) != 0) {
  76. error_setg_errno(errp, errno, "Set key failed");
  77. qcrypto_afalg_comm_free(afalg);
  78. return NULL;
  79. }
  80. /* prepare msg header */
  81. afalg->msg = g_new0(struct msghdr, 1);
  82. afalg->msg->msg_controllen += CMSG_SPACE(ALG_OPTYPE_LEN);
  83. expect_niv = qcrypto_cipher_get_iv_len(alg, mode);
  84. if (expect_niv) {
  85. afalg->msg->msg_controllen += CMSG_SPACE(ALG_MSGIV_LEN(expect_niv));
  86. }
  87. afalg->msg->msg_control = g_new0(uint8_t, afalg->msg->msg_controllen);
  88. /* We use 1st msghdr for crypto-info and 2nd msghdr for IV-info */
  89. afalg->cmsg = CMSG_FIRSTHDR(afalg->msg);
  90. afalg->cmsg->cmsg_type = ALG_SET_OP;
  91. afalg->cmsg->cmsg_len = CMSG_SPACE(ALG_OPTYPE_LEN);
  92. if (expect_niv) {
  93. afalg->cmsg = CMSG_NXTHDR(afalg->msg, afalg->cmsg);
  94. afalg->cmsg->cmsg_type = ALG_SET_IV;
  95. afalg->cmsg->cmsg_len = CMSG_SPACE(ALG_MSGIV_LEN(expect_niv));
  96. }
  97. afalg->cmsg = CMSG_FIRSTHDR(afalg->msg);
  98. return afalg;
  99. }
  100. static int
  101. qcrypto_afalg_cipher_setiv(QCryptoCipher *cipher,
  102. const uint8_t *iv,
  103. size_t niv, Error **errp)
  104. {
  105. struct af_alg_iv *alg_iv;
  106. size_t expect_niv;
  107. QCryptoAFAlg *afalg = cipher->opaque;
  108. expect_niv = qcrypto_cipher_get_iv_len(cipher->alg, cipher->mode);
  109. if (niv != expect_niv) {
  110. error_setg(errp, "Set IV len(%zu) not match expected(%zu)",
  111. niv, expect_niv);
  112. return -1;
  113. }
  114. /* move ->cmsg to next msghdr, for IV-info */
  115. afalg->cmsg = CMSG_NXTHDR(afalg->msg, afalg->cmsg);
  116. /* build setiv msg */
  117. afalg->cmsg->cmsg_level = SOL_ALG;
  118. alg_iv = (struct af_alg_iv *)CMSG_DATA(afalg->cmsg);
  119. alg_iv->ivlen = niv;
  120. memcpy(alg_iv->iv, iv, niv);
  121. return 0;
  122. }
  123. static int
  124. qcrypto_afalg_cipher_op(QCryptoAFAlg *afalg,
  125. const void *in, void *out,
  126. size_t len, bool do_encrypt,
  127. Error **errp)
  128. {
  129. uint32_t *type = NULL;
  130. struct iovec iov;
  131. size_t ret, rlen, done = 0;
  132. uint32_t origin_controllen;
  133. origin_controllen = afalg->msg->msg_controllen;
  134. /* movev ->cmsg to first header, for crypto-info */
  135. afalg->cmsg = CMSG_FIRSTHDR(afalg->msg);
  136. /* build encrypt msg */
  137. afalg->cmsg->cmsg_level = SOL_ALG;
  138. afalg->msg->msg_iov = &iov;
  139. afalg->msg->msg_iovlen = 1;
  140. type = (uint32_t *)CMSG_DATA(afalg->cmsg);
  141. if (do_encrypt) {
  142. *type = ALG_OP_ENCRYPT;
  143. } else {
  144. *type = ALG_OP_DECRYPT;
  145. }
  146. do {
  147. iov.iov_base = (void *)in + done;
  148. iov.iov_len = len - done;
  149. /* send info to AF_ALG core */
  150. ret = sendmsg(afalg->opfd, afalg->msg, 0);
  151. if (ret == -1) {
  152. error_setg_errno(errp, errno, "Send data to AF_ALG core failed");
  153. return -1;
  154. }
  155. /* encrypto && get result */
  156. rlen = read(afalg->opfd, out, ret);
  157. if (rlen == -1) {
  158. error_setg_errno(errp, errno, "Get result from AF_ALG core failed");
  159. return -1;
  160. }
  161. assert(rlen == ret);
  162. /* do not update IV for following chunks */
  163. afalg->msg->msg_controllen = 0;
  164. done += ret;
  165. } while (done < len);
  166. afalg->msg->msg_controllen = origin_controllen;
  167. return 0;
  168. }
  169. static int
  170. qcrypto_afalg_cipher_encrypt(QCryptoCipher *cipher,
  171. const void *in, void *out,
  172. size_t len, Error **errp)
  173. {
  174. return qcrypto_afalg_cipher_op(cipher->opaque, in, out,
  175. len, true, errp);
  176. }
  177. static int
  178. qcrypto_afalg_cipher_decrypt(QCryptoCipher *cipher,
  179. const void *in, void *out,
  180. size_t len, Error **errp)
  181. {
  182. return qcrypto_afalg_cipher_op(cipher->opaque, in, out,
  183. len, false, errp);
  184. }
  185. static void qcrypto_afalg_comm_ctx_free(QCryptoCipher *cipher)
  186. {
  187. qcrypto_afalg_comm_free(cipher->opaque);
  188. }
  189. struct QCryptoCipherDriver qcrypto_cipher_afalg_driver = {
  190. .cipher_encrypt = qcrypto_afalg_cipher_encrypt,
  191. .cipher_decrypt = qcrypto_afalg_cipher_decrypt,
  192. .cipher_setiv = qcrypto_afalg_cipher_setiv,
  193. .cipher_free = qcrypto_afalg_comm_ctx_free,
  194. };