af_alg_ops.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (C) 2010 Martin Willi
  3. * Copyright (C) 2010 revosec AG
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include "af_alg_ops.h"
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #include <linux/socket.h>
  19. #include <utils/debug.h>
  20. typedef struct private_af_alg_ops_t private_af_alg_ops_t;
  21. /**
  22. * Private data of an af_alg_ops_t object.
  23. */
  24. struct private_af_alg_ops_t {
  25. /**
  26. * Public af_alg_ops_t interface.
  27. */
  28. af_alg_ops_t public;
  29. /**
  30. * Transform FD
  31. */
  32. int tfm;
  33. /**
  34. * Operation FD
  35. */
  36. int op;
  37. };
  38. METHOD(af_alg_ops_t, reset, void,
  39. private_af_alg_ops_t *this)
  40. {
  41. if (this->op != -1)
  42. {
  43. close(this->op);
  44. this->op = -1;
  45. }
  46. }
  47. METHOD(af_alg_ops_t, hash, bool,
  48. private_af_alg_ops_t *this, chunk_t data, char *out, size_t outlen)
  49. {
  50. ssize_t len;
  51. while (this->op == -1)
  52. {
  53. this->op = accept(this->tfm, NULL, 0);
  54. if (this->op == -1 && errno != EINTR)
  55. {
  56. DBG1(DBG_LIB, "opening AF_ALG hasher failed: %s", strerror(errno));
  57. return FALSE;
  58. }
  59. }
  60. do
  61. {
  62. len = send(this->op, data.ptr, data.len, out ? 0 : MSG_MORE);
  63. if (len == -1)
  64. {
  65. if (errno == EINTR)
  66. {
  67. continue;
  68. }
  69. DBG1(DBG_LIB, "writing to AF_ALG hasher failed: %s", strerror(errno));
  70. return FALSE;
  71. }
  72. data = chunk_skip(data, len);
  73. }
  74. while (data.len);
  75. if (out)
  76. {
  77. while (outlen)
  78. {
  79. len = read(this->op, out, outlen);
  80. if (len == -1)
  81. {
  82. if (errno == EINTR)
  83. {
  84. continue;
  85. }
  86. DBG1(DBG_LIB, "reading AF_ALG hasher failed: %s", strerror(errno));
  87. return FALSE;
  88. }
  89. outlen -= len;
  90. out += len;
  91. }
  92. reset(this);
  93. }
  94. return TRUE;
  95. }
  96. METHOD(af_alg_ops_t, crypt_, bool,
  97. private_af_alg_ops_t *this, uint32_t type, chunk_t iv, chunk_t data,
  98. char *out)
  99. {
  100. struct msghdr msg = {};
  101. struct cmsghdr *cmsg;
  102. struct af_alg_iv *ivm;
  103. struct iovec iov;
  104. char buf[CMSG_SPACE(sizeof(type)) +
  105. CMSG_SPACE(offsetof(struct af_alg_iv, iv) + iv.len)];
  106. ssize_t len;
  107. int op;
  108. do
  109. {
  110. op = accept(this->tfm, NULL, 0);
  111. if (op == -1 && errno != EINTR)
  112. {
  113. DBG1(DBG_LIB, "accepting AF_ALG crypter failed: %s", strerror(errno));
  114. return FALSE;
  115. }
  116. }
  117. while (op == -1);
  118. memset(buf, 0, sizeof(buf));
  119. msg.msg_control = buf;
  120. msg.msg_controllen = sizeof(buf);
  121. cmsg = CMSG_FIRSTHDR(&msg);
  122. cmsg->cmsg_level = SOL_ALG;
  123. cmsg->cmsg_type = ALG_SET_OP;
  124. cmsg->cmsg_len = CMSG_LEN(sizeof(type));
  125. memcpy(CMSG_DATA(cmsg), &type, sizeof(type));
  126. cmsg = CMSG_NXTHDR(&msg, cmsg);
  127. cmsg->cmsg_level = SOL_ALG;
  128. cmsg->cmsg_type = ALG_SET_IV;
  129. cmsg->cmsg_len = CMSG_LEN(offsetof(struct af_alg_iv, iv) + iv.len);
  130. ivm = (void*)CMSG_DATA(cmsg);
  131. ivm->ivlen = iv.len;
  132. memcpy(ivm->iv, iv.ptr, iv.len);
  133. msg.msg_iov = &iov;
  134. msg.msg_iovlen = 1;
  135. while (data.len)
  136. {
  137. iov.iov_base = data.ptr;
  138. iov.iov_len = data.len;
  139. len = sendmsg(op, &msg, 0);
  140. if (len == -1)
  141. {
  142. if (errno == EINTR)
  143. {
  144. continue;
  145. }
  146. DBG1(DBG_LIB, "writing to AF_ALG crypter failed: %s", strerror(errno));
  147. return FALSE;
  148. }
  149. while (read(op, out, len) != len)
  150. {
  151. if (errno != EINTR)
  152. {
  153. DBG1(DBG_LIB, "reading from AF_ALG crypter failed: %s",
  154. strerror(errno));
  155. return FALSE;
  156. }
  157. }
  158. data = chunk_skip(data, len);
  159. /* no IV for subsequent data chunks */
  160. msg.msg_controllen = 0;
  161. }
  162. close(op);
  163. return TRUE;
  164. }
  165. METHOD(af_alg_ops_t, set_key, bool,
  166. private_af_alg_ops_t *this, chunk_t key)
  167. {
  168. if (setsockopt(this->tfm, SOL_ALG, ALG_SET_KEY, key.ptr, key.len) == -1)
  169. {
  170. DBG1(DBG_LIB, "setting AF_ALG key failed: %s", strerror(errno));
  171. return FALSE;
  172. }
  173. return TRUE;
  174. }
  175. METHOD(af_alg_ops_t, destroy, void,
  176. private_af_alg_ops_t *this)
  177. {
  178. close(this->tfm);
  179. if (this->op != -1)
  180. {
  181. close(this->op);
  182. }
  183. free(this);
  184. }
  185. /**
  186. * See header
  187. */
  188. af_alg_ops_t *af_alg_ops_create(char *type, char *alg)
  189. {
  190. private_af_alg_ops_t *this;
  191. struct sockaddr_alg sa = {
  192. .salg_family = AF_ALG,
  193. };
  194. strncpy(sa.salg_type, type, sizeof(sa.salg_type));
  195. strncpy(sa.salg_name, alg, sizeof(sa.salg_name));
  196. INIT(this,
  197. .public = {
  198. .hash = _hash,
  199. .reset = _reset,
  200. .crypt = _crypt_,
  201. .set_key = _set_key,
  202. .destroy = _destroy,
  203. },
  204. .tfm = socket(AF_ALG, SOCK_SEQPACKET, 0),
  205. .op = -1,
  206. );
  207. if (this->tfm == -1)
  208. {
  209. DBG1(DBG_LIB, "opening AF_ALG socket failed: %s", strerror(errno));
  210. free(this);
  211. return NULL;
  212. }
  213. if (bind(this->tfm, (struct sockaddr*)&sa, sizeof(sa)) == -1)
  214. {
  215. if (errno != ENOENT)
  216. { /* fail silently if algorithm not supported */
  217. DBG1(DBG_LIB, "binding AF_ALG socket for '%s' failed: %s",
  218. sa.salg_name, strerror(errno));
  219. }
  220. destroy(this);
  221. return NULL;
  222. }
  223. return &this->public;
  224. }