sss.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright (c) 2015 Red Hat, Inc.
  4. * Author: Nathaniel McCallum <npmccallum@redhat.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * Additional permission under GPLv3 section 7:
  20. *
  21. * In the following paragraph, "GPL" means the GNU General Public
  22. * License, version 3 or any later version, and "Non-GPL Code" means
  23. * code that is governed neither by the GPL nor a license
  24. * compatible with the GPL.
  25. *
  26. * You may link the code of this Program with Non-GPL Code and convey
  27. * linked combinations including the two, provided that such Non-GPL
  28. * Code only links to the code of this Program through those well
  29. * defined interfaces identified in the file named EXCEPTION found in
  30. * the source code files (the "Approved Interfaces"). The files of
  31. * Non-GPL Code may instantiate templates or use macros or inline
  32. * functions from the Approved Interfaces without causing the resulting
  33. * work to be covered by the GPL. Only the copyright holders of this
  34. * Program may make changes or additions to the list of Approved
  35. * Interfaces.
  36. */
  37. #define _GNU_SOURCE
  38. #include "sss.h"
  39. #include <jose/b64.h>
  40. #include <openssl/bn.h>
  41. #include <stdbool.h>
  42. #include <stddef.h>
  43. #include <stdint.h>
  44. #include <string.h>
  45. #include <sys/types.h>
  46. #include <sys/wait.h>
  47. #include <fcntl.h>
  48. #include <unistd.h>
  49. #include <signal.h>
  50. #define BIGNUM_auto __attribute__((cleanup(BN_cleanup))) BIGNUM
  51. #define BN_CTX_auto __attribute__((cleanup(BN_CTX_cleanup))) BN_CTX
  52. static BIGNUM *
  53. bn_decode(const uint8_t buf[], size_t len)
  54. {
  55. return BN_bin2bn(buf, len, NULL);
  56. }
  57. static BIGNUM *
  58. bn_decode_json(const json_t *json)
  59. {
  60. uint8_t *buf = NULL;
  61. BIGNUM *bn = NULL;
  62. size_t len;
  63. len = jose_b64_dec(json, NULL, 0);
  64. if (len == SIZE_MAX)
  65. return NULL;
  66. buf = malloc(len);
  67. if (!buf)
  68. return NULL;
  69. if (jose_b64_dec(json, buf, len) != len) {
  70. free(buf);
  71. return NULL;
  72. }
  73. bn = bn_decode(buf, len);
  74. free(buf);
  75. return bn;
  76. }
  77. static bool
  78. bn_encode(const BIGNUM *bn, uint8_t buf[], size_t len)
  79. {
  80. int bytes = 0;
  81. if (!bn)
  82. return false;
  83. if (len == 0)
  84. len = BN_num_bytes(bn);
  85. bytes = BN_num_bytes(bn);
  86. if (bytes < 0 || bytes > (int) len)
  87. return false;
  88. memset(buf, 0, len);
  89. return BN_bn2bin(bn, &buf[len - bytes]) > 0;
  90. }
  91. static json_t *
  92. bn_encode_json(const BIGNUM *bn, size_t len)
  93. {
  94. uint8_t *buf = NULL;
  95. json_t *out = NULL;
  96. if (!bn)
  97. return NULL;
  98. if (len == 0)
  99. len = BN_num_bytes(bn);
  100. if ((int) len < BN_num_bytes(bn))
  101. return NULL;
  102. buf = malloc(len);
  103. if (!buf)
  104. return NULL;
  105. if (!bn_encode(bn, buf, len)) {
  106. free(buf);
  107. return NULL;
  108. }
  109. out = jose_b64_enc(buf, len);
  110. free(buf);
  111. return out;
  112. }
  113. static void
  114. BN_CTX_cleanup(BN_CTX **ctx)
  115. {
  116. if (ctx)
  117. BN_CTX_free(*ctx);
  118. }
  119. static void
  120. BN_cleanup(BIGNUM **bnp)
  121. {
  122. if (bnp)
  123. BN_clear_free(*bnp);
  124. }
  125. json_t *
  126. sss_generate(size_t key_bytes, size_t threshold)
  127. {
  128. BIGNUM_auto *p = NULL;
  129. BIGNUM_auto *e = NULL;
  130. json_t *sss = NULL;
  131. if (key_bytes == 0 || threshold < 1)
  132. return NULL;
  133. p = BN_new();
  134. e = BN_new();
  135. if (!p || !e)
  136. goto error;
  137. if (!BN_generate_prime_ex(p, key_bytes * 8, 1, NULL, NULL, NULL))
  138. goto error;
  139. sss = json_pack("{s:i,s:[],s:o}", "t", threshold, "e", "p",
  140. bn_encode_json(p, key_bytes));
  141. if (!sss)
  142. goto error;
  143. for (size_t i = 0; i < threshold; i++) {
  144. if (BN_rand_range(e, p) <= 0)
  145. goto error;
  146. if (json_array_append_new(json_object_get(sss, "e"),
  147. bn_encode_json(e, key_bytes)))
  148. goto error;
  149. }
  150. return sss;
  151. error:
  152. json_decref(sss);
  153. return NULL;
  154. }
  155. uint8_t *
  156. sss_point(const json_t *sss, size_t *len)
  157. {
  158. BN_CTX_auto *ctx = NULL;
  159. BIGNUM_auto *tmp = NULL;
  160. BIGNUM_auto *xx = NULL;
  161. BIGNUM_auto *yy = NULL;
  162. BIGNUM_auto *pp = NULL;
  163. uint8_t *key = NULL;
  164. json_t *e = NULL;
  165. json_t *p = NULL;
  166. json_int_t t = 0;
  167. if (json_unpack((json_t *) sss, "{s:I,s:o,s:o}",
  168. "t", &t, "p", &p, "e", &e) != 0)
  169. return NULL;
  170. ctx = BN_CTX_new();
  171. pp = bn_decode_json(p);
  172. xx = BN_new();
  173. yy = BN_new();
  174. tmp = BN_new();
  175. if (!ctx || !pp || !xx || !yy || !tmp)
  176. return NULL;
  177. if (BN_rand_range(xx, pp) <= 0)
  178. return NULL;
  179. if (BN_zero(yy) <= 0)
  180. return NULL;
  181. for (size_t i = 0; i < json_array_size(e); i++) {
  182. BIGNUM_auto *ee = NULL;
  183. ee = bn_decode_json(json_array_get(e, i));
  184. if (!ee)
  185. return NULL;
  186. if (BN_cmp(pp, ee) <= 0)
  187. return NULL;
  188. /* y += e[i] * x^i */
  189. if (BN_set_word(tmp, i) <= 0)
  190. return NULL;
  191. if (BN_mod_exp(tmp, xx, tmp, pp, ctx) <= 0)
  192. return NULL;
  193. if (BN_mod_mul(tmp, ee, tmp, pp, ctx) <= 0)
  194. return NULL;
  195. if (BN_mod_add(yy, yy, tmp, pp, ctx) <= 0)
  196. return NULL;
  197. }
  198. *len = jose_b64_dec(p, NULL, 0);
  199. if (*len == SIZE_MAX)
  200. return NULL;
  201. key = malloc(*len * 2);
  202. if (!key)
  203. return NULL;
  204. if (!bn_encode(xx, key, *len) || !bn_encode(yy, &key[*len], *len)) {
  205. memset(key, 0, *len * 2);
  206. free(key);
  207. return NULL;
  208. }
  209. *len *= 2;
  210. return key;
  211. }
  212. json_t *
  213. sss_recover(const json_t *p, size_t npnts, const uint8_t *pnts[])
  214. {
  215. BN_CTX_auto *ctx = BN_CTX_new();
  216. BIGNUM_auto *pp = bn_decode_json(p);
  217. BIGNUM_auto *acc = BN_new();
  218. BIGNUM_auto *tmp = BN_new();
  219. BIGNUM_auto *k = BN_new();
  220. size_t len = 0;
  221. if (!ctx || !pp || !acc || !tmp || !k)
  222. return NULL;
  223. if (BN_zero(k) <= 0)
  224. return NULL;
  225. len = jose_b64_dec(p, NULL, 0);
  226. if (len == SIZE_MAX)
  227. return NULL;
  228. for (size_t i = 0; i < npnts; i++) {
  229. BIGNUM_auto *xo = NULL; /* Outer X */
  230. BIGNUM_auto *yo = NULL; /* Outer Y */
  231. xo = bn_decode(pnts[i], len);
  232. yo = bn_decode(&pnts[i][len], len);
  233. if (!xo || !yo)
  234. return NULL;
  235. if (BN_one(acc) <= 0)
  236. return NULL;
  237. for (size_t j = 0; j < npnts; j++) {
  238. BIGNUM_auto *xi = NULL; /* Inner X */
  239. if (i == j)
  240. continue;
  241. xi = bn_decode(pnts[j], len);
  242. if (!xi)
  243. return NULL;
  244. /* acc *= (0 - xi) / (xo - xi) */
  245. if (BN_zero(tmp) <= 0)
  246. return NULL;
  247. if (BN_mod_sub(tmp, tmp, xi, pp, ctx) <= 0)
  248. return NULL;
  249. if (BN_mod_mul(acc, acc, tmp, pp, ctx) <= 0)
  250. return NULL;
  251. if (BN_mod_sub(tmp, xo, xi, pp, ctx) <= 0)
  252. return NULL;
  253. if (BN_mod_inverse(tmp, tmp, pp, ctx) != tmp)
  254. return NULL;
  255. if (BN_mod_mul(acc, acc, tmp, pp, ctx) <= 0)
  256. return NULL;
  257. }
  258. /* k += acc * y[i] */
  259. if (BN_mod_mul(acc, acc, yo, pp, ctx) <= 0)
  260. return NULL;
  261. if (BN_mod_add(k, k, acc, pp, ctx) <= 0)
  262. return NULL;
  263. }
  264. return bn_encode_json(k, len);
  265. }
  266. enum {
  267. PIPE_RD = 0,
  268. PIPE_WR = 1
  269. };
  270. FILE *
  271. call(char *const argv[], const void *buf, size_t len, pid_t *pid)
  272. {
  273. int dump[2] = { -1, -1 };
  274. int load[2] = { -1, -1 };
  275. FILE *out = NULL;
  276. ssize_t wr = 0;
  277. *pid = 0;
  278. if (pipe2(dump, O_CLOEXEC) < 0)
  279. goto error;
  280. if (pipe2(load, O_CLOEXEC) < 0)
  281. goto error;
  282. *pid = fork();
  283. if (*pid < 0)
  284. goto error;
  285. if (*pid == 0) {
  286. if (dup2(dump[PIPE_RD], STDIN_FILENO) < 0 ||
  287. dup2(load[PIPE_WR], STDOUT_FILENO) < 0)
  288. exit(EXIT_FAILURE);
  289. execvp(argv[0], argv);
  290. exit(EXIT_FAILURE);
  291. }
  292. for (const uint8_t *tmp = buf; len > 0; tmp += wr, len -= wr) {
  293. wr = write(dump[PIPE_WR], tmp, len);
  294. if (wr < 0)
  295. goto error;
  296. }
  297. out = fdopen(load[PIPE_RD], "r");
  298. if (!out)
  299. goto error;
  300. close(dump[PIPE_RD]);
  301. close(dump[PIPE_WR]);
  302. close(load[PIPE_WR]);
  303. return out;
  304. error:
  305. close(dump[PIPE_RD]);
  306. close(dump[PIPE_WR]);
  307. close(load[PIPE_RD]);
  308. close(load[PIPE_WR]);
  309. if (*pid > 0) {
  310. kill(*pid, SIGTERM);
  311. waitpid(*pid, NULL, 0);
  312. *pid = 0;
  313. }
  314. return NULL;
  315. }