cipher-afalg.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "qapi/error.h"
  16. #include "crypto/cipher.h"
  17. #include "cipherpriv.h"
  18. static char *
  19. qcrypto_afalg_cipher_format_name(QCryptoCipherAlgorithm alg,
  20. QCryptoCipherMode mode,
  21. Error **errp)
  22. {
  23. char *name;
  24. const char *alg_name;
  25. const char *mode_name;
  26. switch (alg) {
  27. case QCRYPTO_CIPHER_ALG_AES_128:
  28. case QCRYPTO_CIPHER_ALG_AES_192:
  29. case QCRYPTO_CIPHER_ALG_AES_256:
  30. alg_name = "aes";
  31. break;
  32. case QCRYPTO_CIPHER_ALG_CAST5_128:
  33. alg_name = "cast5";
  34. break;
  35. case QCRYPTO_CIPHER_ALG_SERPENT_128:
  36. case QCRYPTO_CIPHER_ALG_SERPENT_192:
  37. case QCRYPTO_CIPHER_ALG_SERPENT_256:
  38. alg_name = "serpent";
  39. break;
  40. case QCRYPTO_CIPHER_ALG_TWOFISH_128:
  41. case QCRYPTO_CIPHER_ALG_TWOFISH_192:
  42. case QCRYPTO_CIPHER_ALG_TWOFISH_256:
  43. alg_name = "twofish";
  44. break;
  45. default:
  46. error_setg(errp, "Unsupported cipher algorithm %d", alg);
  47. return NULL;
  48. }
  49. mode_name = QCryptoCipherMode_str(mode);
  50. name = g_strdup_printf("%s(%s)", mode_name, alg_name);
  51. return name;
  52. }
  53. static const struct QCryptoCipherDriver qcrypto_cipher_afalg_driver;
  54. QCryptoCipher *
  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 (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. afalg->base.driver = &qcrypto_cipher_afalg_driver;
  99. return &afalg->base;
  100. }
  101. static int
  102. qcrypto_afalg_cipher_setiv(QCryptoCipher *cipher,
  103. const uint8_t *iv,
  104. size_t niv, Error **errp)
  105. {
  106. QCryptoAFAlg *afalg = container_of(cipher, QCryptoAFAlg, base);
  107. struct af_alg_iv *alg_iv;
  108. size_t expect_niv;
  109. expect_niv = qcrypto_cipher_get_iv_len(cipher->alg, cipher->mode);
  110. if (niv != expect_niv) {
  111. error_setg(errp, "Set IV len(%zu) not match expected(%zu)",
  112. niv, expect_niv);
  113. return -1;
  114. }
  115. /* move ->cmsg to next msghdr, for IV-info */
  116. afalg->cmsg = CMSG_NXTHDR(afalg->msg, afalg->cmsg);
  117. /* build setiv msg */
  118. afalg->cmsg->cmsg_level = SOL_ALG;
  119. alg_iv = (struct af_alg_iv *)CMSG_DATA(afalg->cmsg);
  120. alg_iv->ivlen = niv;
  121. memcpy(alg_iv->iv, iv, niv);
  122. return 0;
  123. }
  124. static int
  125. qcrypto_afalg_cipher_op(QCryptoAFAlg *afalg,
  126. const void *in, void *out,
  127. size_t len, bool do_encrypt,
  128. Error **errp)
  129. {
  130. uint32_t *type = NULL;
  131. struct iovec iov;
  132. size_t ret, rlen, done = 0;
  133. uint32_t origin_controllen;
  134. origin_controllen = afalg->msg->msg_controllen;
  135. /* movev ->cmsg to first header, for crypto-info */
  136. afalg->cmsg = CMSG_FIRSTHDR(afalg->msg);
  137. /* build encrypt msg */
  138. afalg->cmsg->cmsg_level = SOL_ALG;
  139. afalg->msg->msg_iov = &iov;
  140. afalg->msg->msg_iovlen = 1;
  141. type = (uint32_t *)CMSG_DATA(afalg->cmsg);
  142. if (do_encrypt) {
  143. *type = ALG_OP_ENCRYPT;
  144. } else {
  145. *type = ALG_OP_DECRYPT;
  146. }
  147. do {
  148. iov.iov_base = (void *)in + done;
  149. iov.iov_len = len - done;
  150. /* send info to AF_ALG core */
  151. ret = sendmsg(afalg->opfd, afalg->msg, 0);
  152. if (ret == -1) {
  153. error_setg_errno(errp, errno, "Send data to AF_ALG core failed");
  154. return -1;
  155. }
  156. /* encrypto && get result */
  157. rlen = read(afalg->opfd, out, ret);
  158. if (rlen == -1) {
  159. error_setg_errno(errp, errno, "Get result from AF_ALG core failed");
  160. return -1;
  161. }
  162. assert(rlen == ret);
  163. /* do not update IV for following chunks */
  164. afalg->msg->msg_controllen = 0;
  165. done += ret;
  166. } while (done < len);
  167. afalg->msg->msg_controllen = origin_controllen;
  168. return 0;
  169. }
  170. static int
  171. qcrypto_afalg_cipher_encrypt(QCryptoCipher *cipher,
  172. const void *in, void *out,
  173. size_t len, Error **errp)
  174. {
  175. QCryptoAFAlg *afalg = container_of(cipher, QCryptoAFAlg, base);
  176. return qcrypto_afalg_cipher_op(afalg, in, out, len, true, errp);
  177. }
  178. static int
  179. qcrypto_afalg_cipher_decrypt(QCryptoCipher *cipher,
  180. const void *in, void *out,
  181. size_t len, Error **errp)
  182. {
  183. QCryptoAFAlg *afalg = container_of(cipher, QCryptoAFAlg, base);
  184. return qcrypto_afalg_cipher_op(afalg, in, out, len, false, errp);
  185. }
  186. static void qcrypto_afalg_comm_ctx_free(QCryptoCipher *cipher)
  187. {
  188. QCryptoAFAlg *afalg = container_of(cipher, QCryptoAFAlg, base);
  189. qcrypto_afalg_comm_free(afalg);
  190. }
  191. static const struct QCryptoCipherDriver qcrypto_cipher_afalg_driver = {
  192. .cipher_encrypt = qcrypto_afalg_cipher_encrypt,
  193. .cipher_decrypt = qcrypto_afalg_cipher_decrypt,
  194. .cipher_setiv = qcrypto_afalg_cipher_setiv,
  195. .cipher_free = qcrypto_afalg_comm_ctx_free,
  196. };