checksum.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2011-2014 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #define _GNU_SOURCE
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <stdio.h>
  31. #include "useful.h"
  32. #include "checksum.h"
  33. #include "private.h"
  34. #ifndef HAVE_LINUX_IF_ALG_H
  35. #ifndef HAVE_LINUX_TYPES_H
  36. typedef uint8_t __u8;
  37. typedef uint16_t __u16;
  38. typedef uint32_t __u32;
  39. #else
  40. #include <linux/types.h>
  41. #endif
  42. #ifndef AF_ALG
  43. #define AF_ALG 38
  44. #define PF_ALG AF_ALG
  45. #endif
  46. struct sockaddr_alg {
  47. __u16 salg_family;
  48. __u8 salg_type[14];
  49. __u32 salg_feat;
  50. __u32 salg_mask;
  51. __u8 salg_name[64];
  52. };
  53. /* Socket options */
  54. #define ALG_SET_KEY 1
  55. #else
  56. #include <linux/if_alg.h>
  57. #endif
  58. #ifndef SOL_ALG
  59. #define SOL_ALG 279
  60. #endif
  61. struct checksum_info {
  62. const char *name;
  63. uint8_t digest_len;
  64. bool supported;
  65. };
  66. static struct checksum_info checksum_algs[] = {
  67. [L_CHECKSUM_MD4] = { .name = "md4", .digest_len = 16 },
  68. [L_CHECKSUM_MD5] = { .name = "md5", .digest_len = 16 },
  69. [L_CHECKSUM_SHA1] = { .name = "sha1", .digest_len = 20 },
  70. [L_CHECKSUM_SHA256] = { .name = "sha256", .digest_len = 32 },
  71. [L_CHECKSUM_SHA384] = { .name = "sha384", .digest_len = 48 },
  72. [L_CHECKSUM_SHA512] = { .name = "sha512", .digest_len = 64 },
  73. };
  74. static struct checksum_info checksum_cmac_aes_alg =
  75. { .name = "cmac(aes)", .digest_len = 16 };
  76. static struct checksum_info checksum_hmac_algs[] = {
  77. [L_CHECKSUM_MD4] = { .name = "hmac(md4)", .digest_len = 16 },
  78. [L_CHECKSUM_MD5] = { .name = "hmac(md5)", .digest_len = 16 },
  79. [L_CHECKSUM_SHA1] = { .name = "hmac(sha1)", .digest_len = 20 },
  80. [L_CHECKSUM_SHA256] = { .name = "hmac(sha256)", .digest_len = 32 },
  81. [L_CHECKSUM_SHA384] = { .name = "hmac(sha384)", .digest_len = 48 },
  82. [L_CHECKSUM_SHA512] = { .name = "hmac(sha512)", .digest_len = 64 },
  83. };
  84. static const struct {
  85. struct checksum_info *list;
  86. size_t n;
  87. } checksum_info_table[] = {
  88. { checksum_algs, L_ARRAY_SIZE(checksum_algs) },
  89. { &checksum_cmac_aes_alg, 1 },
  90. { checksum_hmac_algs, L_ARRAY_SIZE(checksum_hmac_algs) },
  91. {}
  92. };
  93. /**
  94. * SECTION:checksum
  95. * @short_description: Checksum handling
  96. *
  97. * Checksum handling
  98. */
  99. #define is_valid_index(array, i) ((i) >= 0 && (i) < L_ARRAY_SIZE(array))
  100. /**
  101. * l_checksum:
  102. *
  103. * Opaque object representing the checksum.
  104. */
  105. struct l_checksum {
  106. int sk;
  107. const struct checksum_info *alg_info;
  108. };
  109. static int create_alg(const char *alg)
  110. {
  111. struct sockaddr_alg salg;
  112. int sk;
  113. sk = socket(PF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
  114. if (sk < 0)
  115. return -1;
  116. memset(&salg, 0, sizeof(salg));
  117. salg.salg_family = AF_ALG;
  118. strcpy((char *) salg.salg_type, "hash");
  119. strcpy((char *) salg.salg_name, alg);
  120. if (bind(sk, (struct sockaddr *) &salg, sizeof(salg)) < 0) {
  121. close(sk);
  122. return -1;
  123. }
  124. return sk;
  125. }
  126. /**
  127. * l_checksum_new:
  128. * @type: checksum type
  129. *
  130. * Creates new #l_checksum, using the checksum algorithm @type.
  131. *
  132. * Returns: a newly allocated #l_checksum object.
  133. **/
  134. LIB_EXPORT struct l_checksum *l_checksum_new(enum l_checksum_type type)
  135. {
  136. struct l_checksum *checksum;
  137. int fd;
  138. if (!is_valid_index(checksum_algs, type) || !checksum_algs[type].name)
  139. return NULL;
  140. checksum = l_new(struct l_checksum, 1);
  141. checksum->alg_info = &checksum_algs[type];
  142. fd = create_alg(checksum->alg_info->name);
  143. if (fd < 0)
  144. goto error;
  145. checksum->sk = accept4(fd, NULL, 0, SOCK_CLOEXEC);
  146. close(fd);
  147. if (checksum->sk < 0)
  148. goto error;
  149. return checksum;
  150. error:
  151. l_free(checksum);
  152. return NULL;
  153. }
  154. LIB_EXPORT struct l_checksum *l_checksum_new_cmac_aes(const void *key,
  155. size_t key_len)
  156. {
  157. struct l_checksum *checksum;
  158. int fd;
  159. fd = create_alg("cmac(aes)");
  160. if (fd < 0)
  161. return NULL;
  162. if (setsockopt(fd, SOL_ALG, ALG_SET_KEY, key, key_len) < 0) {
  163. close(fd);
  164. return NULL;
  165. }
  166. checksum = l_new(struct l_checksum, 1);
  167. checksum->sk = accept4(fd, NULL, 0, SOCK_CLOEXEC);
  168. close(fd);
  169. if (checksum->sk < 0) {
  170. l_free(checksum);
  171. return NULL;
  172. }
  173. checksum->alg_info = &checksum_cmac_aes_alg;
  174. return checksum;
  175. }
  176. LIB_EXPORT struct l_checksum *l_checksum_new_hmac(enum l_checksum_type type,
  177. const void *key, size_t key_len)
  178. {
  179. struct l_checksum *checksum;
  180. int fd;
  181. if (!is_valid_index(checksum_hmac_algs, type) ||
  182. !checksum_hmac_algs[type].name)
  183. return NULL;
  184. fd = create_alg(checksum_hmac_algs[type].name);
  185. if (fd < 0)
  186. return NULL;
  187. if (setsockopt(fd, SOL_ALG, ALG_SET_KEY, key, key_len) < 0) {
  188. close(fd);
  189. return NULL;
  190. }
  191. checksum = l_new(struct l_checksum, 1);
  192. checksum->sk = accept4(fd, NULL, 0, SOCK_CLOEXEC);
  193. close(fd);
  194. if (checksum->sk < 0) {
  195. l_free(checksum);
  196. return NULL;
  197. }
  198. checksum->alg_info = &checksum_hmac_algs[type];
  199. return checksum;
  200. }
  201. /**
  202. * l_checksum_clone:
  203. * @checksum: parent checksum object
  204. *
  205. * Creates a new checksum with an independent copy of parent @checksum's
  206. * state. l_checksum_get_digest can then be called on the parent or the
  207. * clone without affecting the state of the other object.
  208. **/
  209. LIB_EXPORT struct l_checksum *l_checksum_clone(struct l_checksum *checksum)
  210. {
  211. struct l_checksum *clone;
  212. if (unlikely(!checksum))
  213. return NULL;
  214. clone = l_new(struct l_checksum, 1);
  215. clone->sk = accept4(checksum->sk, NULL, 0, SOCK_CLOEXEC);
  216. if (clone->sk < 0) {
  217. l_free(clone);
  218. return NULL;
  219. }
  220. clone->alg_info = checksum->alg_info;
  221. return clone;
  222. }
  223. /**
  224. * l_checksum_free:
  225. * @checksum: checksum object
  226. *
  227. * Frees the memory allocated for @checksum.
  228. **/
  229. LIB_EXPORT void l_checksum_free(struct l_checksum *checksum)
  230. {
  231. if (unlikely(!checksum))
  232. return;
  233. close(checksum->sk);
  234. l_free(checksum);
  235. }
  236. /**
  237. * l_checksum_reset:
  238. * @checksum: checksum object
  239. *
  240. * Resets the internal state of @checksum.
  241. **/
  242. LIB_EXPORT void l_checksum_reset(struct l_checksum *checksum)
  243. {
  244. if (unlikely(!checksum))
  245. return;
  246. send(checksum->sk, NULL, 0, 0);
  247. }
  248. /**
  249. * l_checksum_update:
  250. * @checksum: checksum object
  251. * @data: data pointer
  252. * @len: length of data
  253. *
  254. * Updates checksum from @data pointer with @len bytes.
  255. *
  256. * Returns: true if the operation succeeded, false otherwise.
  257. **/
  258. LIB_EXPORT bool l_checksum_update(struct l_checksum *checksum,
  259. const void *data, size_t len)
  260. {
  261. ssize_t written;
  262. if (unlikely(!checksum))
  263. return false;
  264. written = send(checksum->sk, data, len, MSG_MORE);
  265. if (written < 0)
  266. return false;
  267. return true;
  268. }
  269. /**
  270. * l_checksum_updatev:
  271. * @checksum: checksum object
  272. * @iov: iovec pointer
  273. * @iov_len: Number of iovec entries
  274. *
  275. * This is a iovec based version of l_checksum_update; it updates the checksum
  276. * based on contents of @iov and @iov_len.
  277. *
  278. * Returns: true if the operation succeeded, false otherwise.
  279. **/
  280. LIB_EXPORT bool l_checksum_updatev(struct l_checksum *checksum,
  281. const struct iovec *iov, size_t iov_len)
  282. {
  283. struct msghdr msg;
  284. ssize_t written;
  285. if (unlikely(!checksum))
  286. return false;
  287. if (unlikely(!iov) || unlikely(!iov_len))
  288. return false;
  289. memset(&msg, 0, sizeof(msg));
  290. msg.msg_iov = (struct iovec *) iov;
  291. msg.msg_iovlen = iov_len;
  292. written = sendmsg(checksum->sk, &msg, MSG_MORE);
  293. if (written < 0)
  294. return false;
  295. return true;
  296. }
  297. /**
  298. * l_checksum_get_digest:
  299. * @checksum: checksum object
  300. * @digest: output data buffer
  301. * @len: length of output buffer
  302. *
  303. * Writes the digest from @checksum as raw binary data into the provided
  304. * buffer or, if the buffer is shorter, the initial @len bytes of the digest
  305. * data.
  306. *
  307. * Returns: Number of bytes written, or negative value if an error occurred.
  308. **/
  309. LIB_EXPORT ssize_t l_checksum_get_digest(struct l_checksum *checksum,
  310. void *digest, size_t len)
  311. {
  312. ssize_t result;
  313. if (unlikely(!checksum))
  314. return -EINVAL;
  315. if (unlikely(!digest))
  316. return -EFAULT;
  317. if (unlikely(!len))
  318. return -EINVAL;
  319. result = recv(checksum->sk, digest, len, 0);
  320. if (result < 0)
  321. return -errno;
  322. if ((size_t) result < len && result < checksum->alg_info->digest_len)
  323. return -EIO;
  324. return result;
  325. }
  326. /**
  327. * l_checksum_get_string:
  328. * @checksum: checksum object
  329. *
  330. * Gets the digest from @checksum as hex encoded string.
  331. *
  332. * Returns: a newly allocated hex string
  333. **/
  334. LIB_EXPORT char *l_checksum_get_string(struct l_checksum *checksum)
  335. {
  336. unsigned char digest[64];
  337. if (unlikely(!checksum))
  338. return NULL;
  339. l_checksum_get_digest(checksum, digest, sizeof(digest));
  340. return l_util_hexstring(digest, checksum->alg_info->digest_len);
  341. }
  342. static void init_supported()
  343. {
  344. static bool initialized = false;
  345. struct sockaddr_alg salg;
  346. int sk;
  347. unsigned int i, j;
  348. if (likely(initialized))
  349. return;
  350. initialized = true;
  351. sk = socket(PF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
  352. if (sk < 0)
  353. return;
  354. memset(&salg, 0, sizeof(salg));
  355. salg.salg_family = AF_ALG;
  356. strcpy((char *) salg.salg_type, "hash");
  357. for (i = 0; checksum_info_table[i].list; i++)
  358. for (j = 0; j < checksum_info_table[i].n; j++) {
  359. struct checksum_info *info;
  360. info = &checksum_info_table[i].list[j];
  361. if (!info->name)
  362. continue;
  363. strcpy((char *) salg.salg_name, info->name);
  364. if (bind(sk, (struct sockaddr *) &salg,
  365. sizeof(salg)) < 0)
  366. continue;
  367. info->supported = true;
  368. }
  369. close(sk);
  370. }
  371. LIB_EXPORT bool l_checksum_is_supported(enum l_checksum_type type,
  372. bool check_hmac)
  373. {
  374. const struct checksum_info *list;
  375. init_supported();
  376. if (!check_hmac) {
  377. if (!is_valid_index(checksum_algs, type))
  378. return false;
  379. list = checksum_algs;
  380. } else {
  381. if (!is_valid_index(checksum_hmac_algs, type))
  382. return false;
  383. list = checksum_hmac_algs;
  384. }
  385. return list[type].supported;
  386. }
  387. LIB_EXPORT bool l_checksum_cmac_aes_supported()
  388. {
  389. init_supported();
  390. return checksum_cmac_aes_alg.supported;
  391. }
  392. LIB_EXPORT ssize_t l_checksum_digest_length(enum l_checksum_type type)
  393. {
  394. return is_valid_index(checksum_algs, type) ?
  395. checksum_algs[type].digest_len : 0;
  396. }