pwbox.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /* pwbox.c -- generated by Trunnel v1.4.4.
  2. * https://gitweb.torproject.org/trunnel.git
  3. * You probably shouldn't edit this file.
  4. */
  5. #include <stdlib.h>
  6. #include "trunnel-impl.h"
  7. #include "pwbox.h"
  8. #define TRUNNEL_SET_ERROR_CODE(obj) \
  9. do { \
  10. (obj)->trunnel_error_code_ = 1; \
  11. } while (0)
  12. #if defined(__COVERITY__) || defined(__clang_analyzer__)
  13. /* If we're runnning a static analysis tool, we don't want it to complain
  14. * that some of our remaining-bytes checks are dead-code. */
  15. int pwbox_deadcode_dummy__ = 0;
  16. #define OR_DEADCODE_DUMMY || pwbox_deadcode_dummy__
  17. #else
  18. #define OR_DEADCODE_DUMMY
  19. #endif
  20. #define CHECK_REMAINING(nbytes, label) \
  21. do { \
  22. if (remaining < (nbytes) OR_DEADCODE_DUMMY) { \
  23. goto label; \
  24. } \
  25. } while (0)
  26. pwbox_encoded_t *
  27. pwbox_encoded_new(void)
  28. {
  29. pwbox_encoded_t *val = trunnel_calloc(1, sizeof(pwbox_encoded_t));
  30. if (NULL == val)
  31. return NULL;
  32. val->fixedbytes0 = PWBOX0_CONST0;
  33. val->fixedbytes1 = PWBOX0_CONST1;
  34. return val;
  35. }
  36. /** Release all storage held inside 'obj', but do not free 'obj'.
  37. */
  38. static void
  39. pwbox_encoded_clear(pwbox_encoded_t *obj)
  40. {
  41. (void) obj;
  42. TRUNNEL_DYNARRAY_WIPE(&obj->skey_header);
  43. TRUNNEL_DYNARRAY_CLEAR(&obj->skey_header);
  44. TRUNNEL_DYNARRAY_WIPE(&obj->data);
  45. TRUNNEL_DYNARRAY_CLEAR(&obj->data);
  46. }
  47. void
  48. pwbox_encoded_free(pwbox_encoded_t *obj)
  49. {
  50. if (obj == NULL)
  51. return;
  52. pwbox_encoded_clear(obj);
  53. trunnel_memwipe(obj, sizeof(pwbox_encoded_t));
  54. trunnel_free_(obj);
  55. }
  56. uint32_t
  57. pwbox_encoded_get_fixedbytes0(pwbox_encoded_t *inp)
  58. {
  59. return inp->fixedbytes0;
  60. }
  61. int
  62. pwbox_encoded_set_fixedbytes0(pwbox_encoded_t *inp, uint32_t val)
  63. {
  64. if (! ((val == PWBOX0_CONST0))) {
  65. TRUNNEL_SET_ERROR_CODE(inp);
  66. return -1;
  67. }
  68. inp->fixedbytes0 = val;
  69. return 0;
  70. }
  71. uint32_t
  72. pwbox_encoded_get_fixedbytes1(pwbox_encoded_t *inp)
  73. {
  74. return inp->fixedbytes1;
  75. }
  76. int
  77. pwbox_encoded_set_fixedbytes1(pwbox_encoded_t *inp, uint32_t val)
  78. {
  79. if (! ((val == PWBOX0_CONST1))) {
  80. TRUNNEL_SET_ERROR_CODE(inp);
  81. return -1;
  82. }
  83. inp->fixedbytes1 = val;
  84. return 0;
  85. }
  86. uint8_t
  87. pwbox_encoded_get_header_len(pwbox_encoded_t *inp)
  88. {
  89. return inp->header_len;
  90. }
  91. int
  92. pwbox_encoded_set_header_len(pwbox_encoded_t *inp, uint8_t val)
  93. {
  94. inp->header_len = val;
  95. return 0;
  96. }
  97. size_t
  98. pwbox_encoded_getlen_skey_header(const pwbox_encoded_t *inp)
  99. {
  100. return TRUNNEL_DYNARRAY_LEN(&inp->skey_header);
  101. }
  102. uint8_t
  103. pwbox_encoded_get_skey_header(pwbox_encoded_t *inp, size_t idx)
  104. {
  105. return TRUNNEL_DYNARRAY_GET(&inp->skey_header, idx);
  106. }
  107. int
  108. pwbox_encoded_set_skey_header(pwbox_encoded_t *inp, size_t idx, uint8_t elt)
  109. {
  110. TRUNNEL_DYNARRAY_SET(&inp->skey_header, idx, elt);
  111. return 0;
  112. }
  113. int
  114. pwbox_encoded_add_skey_header(pwbox_encoded_t *inp, uint8_t elt)
  115. {
  116. #if SIZE_MAX >= UINT8_MAX
  117. if (inp->skey_header.n_ == UINT8_MAX)
  118. goto trunnel_alloc_failed;
  119. #endif
  120. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->skey_header, elt, {});
  121. return 0;
  122. trunnel_alloc_failed:
  123. TRUNNEL_SET_ERROR_CODE(inp);
  124. return -1;
  125. }
  126. uint8_t *
  127. pwbox_encoded_getarray_skey_header(pwbox_encoded_t *inp)
  128. {
  129. return inp->skey_header.elts_;
  130. }
  131. int
  132. pwbox_encoded_setlen_skey_header(pwbox_encoded_t *inp, size_t newlen)
  133. {
  134. uint8_t *newptr;
  135. #if UINT8_MAX < SIZE_MAX
  136. if (newlen > UINT8_MAX)
  137. goto trunnel_alloc_failed;
  138. #endif
  139. newptr = trunnel_dynarray_setlen(&inp->skey_header.allocated_,
  140. &inp->skey_header.n_, inp->skey_header.elts_, newlen,
  141. sizeof(inp->skey_header.elts_[0]), (trunnel_free_fn_t) NULL,
  142. &inp->trunnel_error_code_);
  143. if (newptr == NULL)
  144. goto trunnel_alloc_failed;
  145. inp->skey_header.elts_ = newptr;
  146. return 0;
  147. trunnel_alloc_failed:
  148. TRUNNEL_SET_ERROR_CODE(inp);
  149. return -1;
  150. }
  151. size_t
  152. pwbox_encoded_getlen_iv(const pwbox_encoded_t *inp)
  153. {
  154. (void)inp; return 16;
  155. }
  156. uint8_t
  157. pwbox_encoded_get_iv(const pwbox_encoded_t *inp, size_t idx)
  158. {
  159. trunnel_assert(idx < 16);
  160. return inp->iv[idx];
  161. }
  162. int
  163. pwbox_encoded_set_iv(pwbox_encoded_t *inp, size_t idx, uint8_t elt)
  164. {
  165. trunnel_assert(idx < 16);
  166. inp->iv[idx] = elt;
  167. return 0;
  168. }
  169. uint8_t *
  170. pwbox_encoded_getarray_iv(pwbox_encoded_t *inp)
  171. {
  172. return inp->iv;
  173. }
  174. size_t
  175. pwbox_encoded_getlen_data(const pwbox_encoded_t *inp)
  176. {
  177. return TRUNNEL_DYNARRAY_LEN(&inp->data);
  178. }
  179. uint8_t
  180. pwbox_encoded_get_data(pwbox_encoded_t *inp, size_t idx)
  181. {
  182. return TRUNNEL_DYNARRAY_GET(&inp->data, idx);
  183. }
  184. int
  185. pwbox_encoded_set_data(pwbox_encoded_t *inp, size_t idx, uint8_t elt)
  186. {
  187. TRUNNEL_DYNARRAY_SET(&inp->data, idx, elt);
  188. return 0;
  189. }
  190. int
  191. pwbox_encoded_add_data(pwbox_encoded_t *inp, uint8_t elt)
  192. {
  193. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->data, elt, {});
  194. return 0;
  195. trunnel_alloc_failed:
  196. TRUNNEL_SET_ERROR_CODE(inp);
  197. return -1;
  198. }
  199. uint8_t *
  200. pwbox_encoded_getarray_data(pwbox_encoded_t *inp)
  201. {
  202. return inp->data.elts_;
  203. }
  204. int
  205. pwbox_encoded_setlen_data(pwbox_encoded_t *inp, size_t newlen)
  206. {
  207. uint8_t *newptr;
  208. newptr = trunnel_dynarray_setlen(&inp->data.allocated_,
  209. &inp->data.n_, inp->data.elts_, newlen,
  210. sizeof(inp->data.elts_[0]), (trunnel_free_fn_t) NULL,
  211. &inp->trunnel_error_code_);
  212. if (newptr == NULL)
  213. goto trunnel_alloc_failed;
  214. inp->data.elts_ = newptr;
  215. return 0;
  216. trunnel_alloc_failed:
  217. TRUNNEL_SET_ERROR_CODE(inp);
  218. return -1;
  219. }
  220. size_t
  221. pwbox_encoded_getlen_hmac(const pwbox_encoded_t *inp)
  222. {
  223. (void)inp; return 32;
  224. }
  225. uint8_t
  226. pwbox_encoded_get_hmac(const pwbox_encoded_t *inp, size_t idx)
  227. {
  228. trunnel_assert(idx < 32);
  229. return inp->hmac[idx];
  230. }
  231. int
  232. pwbox_encoded_set_hmac(pwbox_encoded_t *inp, size_t idx, uint8_t elt)
  233. {
  234. trunnel_assert(idx < 32);
  235. inp->hmac[idx] = elt;
  236. return 0;
  237. }
  238. uint8_t *
  239. pwbox_encoded_getarray_hmac(pwbox_encoded_t *inp)
  240. {
  241. return inp->hmac;
  242. }
  243. const char *
  244. pwbox_encoded_check(const pwbox_encoded_t *obj)
  245. {
  246. if (obj == NULL)
  247. return "Object was NULL";
  248. if (obj->trunnel_error_code_)
  249. return "A set function failed on this object";
  250. if (! (obj->fixedbytes0 == PWBOX0_CONST0))
  251. return "Integer out of bounds";
  252. if (! (obj->fixedbytes1 == PWBOX0_CONST1))
  253. return "Integer out of bounds";
  254. if (TRUNNEL_DYNARRAY_LEN(&obj->skey_header) != obj->header_len)
  255. return "Length mismatch for skey_header";
  256. return NULL;
  257. }
  258. ssize_t
  259. pwbox_encoded_encoded_len(const pwbox_encoded_t *obj)
  260. {
  261. ssize_t result = 0;
  262. if (NULL != pwbox_encoded_check(obj))
  263. return -1;
  264. /* Length of u32 fixedbytes0 IN [PWBOX0_CONST0] */
  265. result += 4;
  266. /* Length of u32 fixedbytes1 IN [PWBOX0_CONST1] */
  267. result += 4;
  268. /* Length of u8 header_len */
  269. result += 1;
  270. /* Length of u8 skey_header[header_len] */
  271. result += TRUNNEL_DYNARRAY_LEN(&obj->skey_header);
  272. /* Length of u8 iv[16] */
  273. result += 16;
  274. /* Length of u8 data[] */
  275. result += TRUNNEL_DYNARRAY_LEN(&obj->data);
  276. /* Length of u8 hmac[32] */
  277. result += 32;
  278. return result;
  279. }
  280. int
  281. pwbox_encoded_clear_errors(pwbox_encoded_t *obj)
  282. {
  283. int r = obj->trunnel_error_code_;
  284. obj->trunnel_error_code_ = 0;
  285. return r;
  286. }
  287. ssize_t
  288. pwbox_encoded_encode(uint8_t *output, size_t avail, const pwbox_encoded_t *obj)
  289. {
  290. ssize_t result = 0;
  291. size_t written = 0;
  292. uint8_t *ptr = output;
  293. const char *msg;
  294. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  295. const ssize_t encoded_len = pwbox_encoded_encoded_len(obj);
  296. #endif
  297. int enforce_avail = 0;
  298. const size_t avail_orig = avail;
  299. if (NULL != (msg = pwbox_encoded_check(obj)))
  300. goto check_failed;
  301. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  302. trunnel_assert(encoded_len >= 0);
  303. #endif
  304. /* Encode u32 fixedbytes0 IN [PWBOX0_CONST0] */
  305. trunnel_assert(written <= avail);
  306. if (avail - written < 4)
  307. goto truncated;
  308. trunnel_set_uint32(ptr, trunnel_htonl(obj->fixedbytes0));
  309. written += 4; ptr += 4;
  310. /* Encode u32 fixedbytes1 IN [PWBOX0_CONST1] */
  311. trunnel_assert(written <= avail);
  312. if (avail - written < 4)
  313. goto truncated;
  314. trunnel_set_uint32(ptr, trunnel_htonl(obj->fixedbytes1));
  315. written += 4; ptr += 4;
  316. /* Encode u8 header_len */
  317. trunnel_assert(written <= avail);
  318. if (avail - written < 1)
  319. goto truncated;
  320. trunnel_set_uint8(ptr, (obj->header_len));
  321. written += 1; ptr += 1;
  322. /* Encode u8 skey_header[header_len] */
  323. {
  324. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->skey_header);
  325. trunnel_assert(obj->header_len == elt_len);
  326. trunnel_assert(written <= avail);
  327. if (avail - written < elt_len)
  328. goto truncated;
  329. if (elt_len)
  330. memcpy(ptr, obj->skey_header.elts_, elt_len);
  331. written += elt_len; ptr += elt_len;
  332. }
  333. /* Encode u8 iv[16] */
  334. trunnel_assert(written <= avail);
  335. if (avail - written < 16)
  336. goto truncated;
  337. memcpy(ptr, obj->iv, 16);
  338. written += 16; ptr += 16;
  339. {
  340. /* Encode u8 data[] */
  341. {
  342. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->data);
  343. trunnel_assert(written <= avail);
  344. if (avail - written < elt_len)
  345. goto truncated;
  346. if (elt_len)
  347. memcpy(ptr, obj->data.elts_, elt_len);
  348. written += elt_len; ptr += elt_len;
  349. }
  350. trunnel_assert(written <= avail);
  351. if (avail - written < 32)
  352. goto truncated;
  353. avail = written + 32;
  354. enforce_avail = 1;
  355. }
  356. /* Encode u8 hmac[32] */
  357. trunnel_assert(written <= avail);
  358. if (avail - written < 32) {
  359. if (avail_orig - written < 32)
  360. goto truncated;
  361. else
  362. goto check_failed;
  363. }
  364. memcpy(ptr, obj->hmac, 32);
  365. written += 32; ptr += 32;
  366. trunnel_assert(ptr == output + written);
  367. if (enforce_avail && avail != written)
  368. goto check_failed;
  369. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  370. {
  371. trunnel_assert(encoded_len >= 0);
  372. trunnel_assert((size_t)encoded_len == written);
  373. }
  374. #endif
  375. return written;
  376. truncated:
  377. result = -2;
  378. goto fail;
  379. check_failed:
  380. (void)msg;
  381. result = -1;
  382. goto fail;
  383. fail:
  384. trunnel_assert(result < 0);
  385. return result;
  386. }
  387. /** As pwbox_encoded_parse(), but do not allocate the output object.
  388. */
  389. static ssize_t
  390. pwbox_encoded_parse_into(pwbox_encoded_t *obj, const uint8_t *input, const size_t len_in)
  391. {
  392. const uint8_t *ptr = input;
  393. size_t remaining = len_in;
  394. ssize_t result = 0;
  395. (void)result;
  396. /* Parse u32 fixedbytes0 IN [PWBOX0_CONST0] */
  397. CHECK_REMAINING(4, truncated);
  398. obj->fixedbytes0 = trunnel_ntohl(trunnel_get_uint32(ptr));
  399. remaining -= 4; ptr += 4;
  400. if (! (obj->fixedbytes0 == PWBOX0_CONST0))
  401. goto fail;
  402. /* Parse u32 fixedbytes1 IN [PWBOX0_CONST1] */
  403. CHECK_REMAINING(4, truncated);
  404. obj->fixedbytes1 = trunnel_ntohl(trunnel_get_uint32(ptr));
  405. remaining -= 4; ptr += 4;
  406. if (! (obj->fixedbytes1 == PWBOX0_CONST1))
  407. goto fail;
  408. /* Parse u8 header_len */
  409. CHECK_REMAINING(1, truncated);
  410. obj->header_len = (trunnel_get_uint8(ptr));
  411. remaining -= 1; ptr += 1;
  412. /* Parse u8 skey_header[header_len] */
  413. CHECK_REMAINING(obj->header_len, truncated);
  414. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->skey_header, obj->header_len, {});
  415. obj->skey_header.n_ = obj->header_len;
  416. if (obj->header_len)
  417. memcpy(obj->skey_header.elts_, ptr, obj->header_len);
  418. ptr += obj->header_len; remaining -= obj->header_len;
  419. /* Parse u8 iv[16] */
  420. CHECK_REMAINING(16, truncated);
  421. memcpy(obj->iv, ptr, 16);
  422. remaining -= 16; ptr += 16;
  423. {
  424. size_t remaining_after;
  425. CHECK_REMAINING(32, truncated);
  426. remaining_after = 32;
  427. remaining = remaining - 32;
  428. /* Parse u8 data[] */
  429. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->data, remaining, {});
  430. obj->data.n_ = remaining;
  431. if (remaining)
  432. memcpy(obj->data.elts_, ptr, remaining);
  433. ptr += remaining; remaining -= remaining;
  434. if (remaining != 0)
  435. goto fail;
  436. remaining = remaining_after;
  437. }
  438. /* Parse u8 hmac[32] */
  439. CHECK_REMAINING(32, truncated);
  440. memcpy(obj->hmac, ptr, 32);
  441. remaining -= 32; ptr += 32;
  442. trunnel_assert(ptr + remaining == input + len_in);
  443. return len_in - remaining;
  444. truncated:
  445. return -2;
  446. trunnel_alloc_failed:
  447. return -1;
  448. fail:
  449. result = -1;
  450. return result;
  451. }
  452. ssize_t
  453. pwbox_encoded_parse(pwbox_encoded_t **output, const uint8_t *input, const size_t len_in)
  454. {
  455. ssize_t result;
  456. *output = pwbox_encoded_new();
  457. if (NULL == *output)
  458. return -1;
  459. result = pwbox_encoded_parse_into(*output, input, len_in);
  460. if (result < 0) {
  461. pwbox_encoded_free(*output);
  462. *output = NULL;
  463. }
  464. return result;
  465. }