binascii.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file binascii.c
  8. *
  9. * \brief Miscellaneous functions for encoding and decoding various things
  10. * in base{16,32,64}.
  11. */
  12. #include "orconfig.h"
  13. #include "lib/encoding/binascii.h"
  14. #include "lib/log/torlog.h"
  15. #include "lib/log/util_bug.h"
  16. #include "lib/cc/torint.h"
  17. #include "lib/string/compat_ctype.h"
  18. #include "lib/intmath/muldiv.h"
  19. #include "lib/malloc/util_malloc.h"
  20. #include <stddef.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. /* Return the base32 encoded size in bytes using the source length srclen.
  24. *
  25. * (WATCH OUT: This API counts the terminating NUL byte, but
  26. * base64_encode_size does not.)
  27. */
  28. size_t
  29. base32_encoded_size(size_t srclen)
  30. {
  31. size_t enclen;
  32. tor_assert(srclen < SIZE_T_CEILING / 8);
  33. enclen = BASE32_NOPAD_BUFSIZE(srclen);
  34. tor_assert(enclen < INT_MAX && enclen > srclen);
  35. return enclen;
  36. }
  37. /** Implements base32 encoding as in RFC 4648. */
  38. void
  39. base32_encode(char *dest, size_t destlen, const char *src, size_t srclen)
  40. {
  41. unsigned int i, v, u;
  42. size_t nbits = srclen * 8;
  43. size_t bit;
  44. /* We need enough space for the encoded data and the extra NUL byte. */
  45. tor_assert(base32_encoded_size(srclen) <= destlen);
  46. tor_assert(destlen < SIZE_T_CEILING);
  47. /* Make sure we leave no uninitialized data in the destination buffer. */
  48. memset(dest, 0, destlen);
  49. for (i=0,bit=0; bit < nbits; ++i, bit+=5) {
  50. /* set v to the 16-bit value starting at src[bits/8], 0-padded. */
  51. size_t idx = bit / 8;
  52. v = ((uint8_t)src[idx]) << 8;
  53. if (idx+1 < srclen)
  54. v += (uint8_t)src[idx+1];
  55. /* set u to the 5-bit value at the bit'th bit of buf. */
  56. u = (v >> (11-(bit%8))) & 0x1F;
  57. dest[i] = BASE32_CHARS[u];
  58. }
  59. dest[i] = '\0';
  60. }
  61. /** Implements base32 decoding as in RFC 4648.
  62. * Returns 0 if successful, -1 otherwise.
  63. */
  64. int
  65. base32_decode(char *dest, size_t destlen, const char *src, size_t srclen)
  66. {
  67. /* XXXX we might want to rewrite this along the lines of base64_decode, if
  68. * it ever shows up in the profile. */
  69. unsigned int i;
  70. size_t nbits, j, bit;
  71. char *tmp;
  72. nbits = ((srclen * 5) / 8) * 8;
  73. tor_assert(srclen < SIZE_T_CEILING / 5);
  74. tor_assert((nbits/8) <= destlen); /* We need enough space. */
  75. tor_assert(destlen < SIZE_T_CEILING);
  76. /* Make sure we leave no uninitialized data in the destination buffer. */
  77. memset(dest, 0, destlen);
  78. /* Convert base32 encoded chars to the 5-bit values that they represent. */
  79. tmp = tor_malloc_zero(srclen);
  80. for (j = 0; j < srclen; ++j) {
  81. if (src[j] > 0x60 && src[j] < 0x7B) tmp[j] = src[j] - 0x61;
  82. else if (src[j] > 0x31 && src[j] < 0x38) tmp[j] = src[j] - 0x18;
  83. else if (src[j] > 0x40 && src[j] < 0x5B) tmp[j] = src[j] - 0x41;
  84. else {
  85. log_warn(LD_GENERAL, "illegal character in base32 encoded string");
  86. tor_free(tmp);
  87. return -1;
  88. }
  89. }
  90. /* Assemble result byte-wise by applying five possible cases. */
  91. for (i = 0, bit = 0; bit < nbits; ++i, bit += 8) {
  92. switch (bit % 40) {
  93. case 0:
  94. dest[i] = (((uint8_t)tmp[(bit/5)]) << 3) +
  95. (((uint8_t)tmp[(bit/5)+1]) >> 2);
  96. break;
  97. case 8:
  98. dest[i] = (((uint8_t)tmp[(bit/5)]) << 6) +
  99. (((uint8_t)tmp[(bit/5)+1]) << 1) +
  100. (((uint8_t)tmp[(bit/5)+2]) >> 4);
  101. break;
  102. case 16:
  103. dest[i] = (((uint8_t)tmp[(bit/5)]) << 4) +
  104. (((uint8_t)tmp[(bit/5)+1]) >> 1);
  105. break;
  106. case 24:
  107. dest[i] = (((uint8_t)tmp[(bit/5)]) << 7) +
  108. (((uint8_t)tmp[(bit/5)+1]) << 2) +
  109. (((uint8_t)tmp[(bit/5)+2]) >> 3);
  110. break;
  111. case 32:
  112. dest[i] = (((uint8_t)tmp[(bit/5)]) << 5) +
  113. ((uint8_t)tmp[(bit/5)+1]);
  114. break;
  115. }
  116. }
  117. memset(tmp, 0, srclen); /* on the heap, this should be safe */
  118. tor_free(tmp);
  119. tmp = NULL;
  120. return 0;
  121. }
  122. #define BASE64_OPENSSL_LINELEN 64
  123. /** Return the Base64 encoded size of <b>srclen</b> bytes of data in
  124. * bytes.
  125. *
  126. * (WATCH OUT: This API <em>does not</em> count the terminating NUL byte,
  127. * but base32_encoded_size does.)
  128. *
  129. * If <b>flags</b>&amp;BASE64_ENCODE_MULTILINE is true, return the size
  130. * of the encoded output as multiline output (64 character, `\n' terminated
  131. * lines).
  132. */
  133. size_t
  134. base64_encode_size(size_t srclen, int flags)
  135. {
  136. size_t enclen;
  137. /* Use INT_MAX for overflow checking because base64_encode() returns int. */
  138. tor_assert(srclen < INT_MAX);
  139. tor_assert(CEIL_DIV(srclen, 3) < INT_MAX / 4);
  140. enclen = BASE64_LEN(srclen);
  141. if (flags & BASE64_ENCODE_MULTILINE)
  142. enclen += CEIL_DIV(enclen, BASE64_OPENSSL_LINELEN);
  143. tor_assert(enclen < INT_MAX && (enclen == 0 || enclen > srclen));
  144. return enclen;
  145. }
  146. /** Internal table mapping 6 bit values to the Base64 alphabet. */
  147. static const char base64_encode_table[64] = {
  148. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  149. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  150. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  151. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  152. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  153. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  154. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  155. '4', '5', '6', '7', '8', '9', '+', '/'
  156. };
  157. /** Base64 encode <b>srclen</b> bytes of data from <b>src</b>. Write
  158. * the result into <b>dest</b>, if it will fit within <b>destlen</b>
  159. * bytes. Return the number of bytes written on success; -1 if
  160. * destlen is too short, or other failure.
  161. *
  162. * If <b>flags</b>&amp;BASE64_ENCODE_MULTILINE is true, return encoded
  163. * output in multiline format (64 character, `\n' terminated lines).
  164. */
  165. int
  166. base64_encode(char *dest, size_t destlen, const char *src, size_t srclen,
  167. int flags)
  168. {
  169. const unsigned char *usrc = (unsigned char *)src;
  170. const unsigned char *eous = usrc + srclen;
  171. char *d = dest;
  172. uint32_t n = 0;
  173. size_t linelen = 0;
  174. size_t enclen;
  175. int n_idx = 0;
  176. if (!src || !dest)
  177. return -1;
  178. /* Ensure that there is sufficient space, including the NUL. */
  179. enclen = base64_encode_size(srclen, flags);
  180. if (destlen < enclen + 1)
  181. return -1;
  182. if (destlen > SIZE_T_CEILING)
  183. return -1;
  184. if (enclen > INT_MAX)
  185. return -1;
  186. /* Make sure we leave no uninitialized data in the destination buffer. */
  187. memset(dest, 0, destlen);
  188. /* XXX/Yawning: If this ends up being too slow, this can be sped up
  189. * by separating the multiline format case and the normal case, and
  190. * processing 48 bytes of input at a time when newlines are desired.
  191. */
  192. #define ENCODE_CHAR(ch) \
  193. STMT_BEGIN \
  194. *d++ = ch; \
  195. if (flags & BASE64_ENCODE_MULTILINE) { \
  196. if (++linelen % BASE64_OPENSSL_LINELEN == 0) { \
  197. linelen = 0; \
  198. *d++ = '\n'; \
  199. } \
  200. } \
  201. STMT_END
  202. #define ENCODE_N(idx) \
  203. ENCODE_CHAR(base64_encode_table[(n >> ((3 - idx) * 6)) & 0x3f])
  204. #define ENCODE_PAD() ENCODE_CHAR('=')
  205. /* Iterate over all the bytes in src. Each one will add 8 bits to the
  206. * value we're encoding. Accumulate bits in <b>n</b>, and whenever we
  207. * have 24 bits, batch them into 4 bytes and flush those bytes to dest.
  208. */
  209. for ( ; usrc < eous; ++usrc) {
  210. n = (n << 8) | *usrc;
  211. if ((++n_idx) == 3) {
  212. ENCODE_N(0);
  213. ENCODE_N(1);
  214. ENCODE_N(2);
  215. ENCODE_N(3);
  216. n_idx = 0;
  217. n = 0;
  218. }
  219. }
  220. switch (n_idx) {
  221. case 0:
  222. /* 0 leftover bits, no pading to add. */
  223. break;
  224. case 1:
  225. /* 8 leftover bits, pad to 12 bits, write the 2 6-bit values followed
  226. * by 2 padding characters.
  227. */
  228. n <<= 4;
  229. ENCODE_N(2);
  230. ENCODE_N(3);
  231. ENCODE_PAD();
  232. ENCODE_PAD();
  233. break;
  234. case 2:
  235. /* 16 leftover bits, pad to 18 bits, write the 3 6-bit values followed
  236. * by 1 padding character.
  237. */
  238. n <<= 2;
  239. ENCODE_N(1);
  240. ENCODE_N(2);
  241. ENCODE_N(3);
  242. ENCODE_PAD();
  243. break;
  244. // LCOV_EXCL_START -- we can't reach this point, because we enforce
  245. // 0 <= ncov_idx < 3 in the loop above.
  246. default:
  247. /* Something went catastrophically wrong. */
  248. tor_fragile_assert();
  249. return -1;
  250. // LCOV_EXCL_STOP
  251. }
  252. #undef ENCODE_N
  253. #undef ENCODE_PAD
  254. #undef ENCODE_CHAR
  255. /* Multiline output always includes at least one newline. */
  256. if (flags & BASE64_ENCODE_MULTILINE && linelen != 0)
  257. *d++ = '\n';
  258. tor_assert(d - dest == (ptrdiff_t)enclen);
  259. *d++ = '\0'; /* NUL terminate the output. */
  260. return (int) enclen;
  261. }
  262. /** As base64_encode, but do not add any internal spaces or external padding
  263. * to the output stream. */
  264. int
  265. base64_encode_nopad(char *dest, size_t destlen,
  266. const uint8_t *src, size_t srclen)
  267. {
  268. int n = base64_encode(dest, destlen, (const char*) src, srclen, 0);
  269. if (n <= 0)
  270. return n;
  271. tor_assert((size_t)n < destlen && dest[n] == 0);
  272. char *in, *out;
  273. in = out = dest;
  274. while (*in) {
  275. if (*in == '=' || *in == '\n') {
  276. ++in;
  277. } else {
  278. *out++ = *in++;
  279. }
  280. }
  281. *out = 0;
  282. tor_assert(out - dest <= INT_MAX);
  283. return (int)(out - dest);
  284. }
  285. #undef BASE64_OPENSSL_LINELEN
  286. /** @{ */
  287. /** Special values used for the base64_decode_table */
  288. #define X 255
  289. #define SP 64
  290. #define PAD 65
  291. /** @} */
  292. /** Internal table mapping byte values to what they represent in base64.
  293. * Numbers 0..63 are 6-bit integers. SPs are spaces, and should be
  294. * skipped. Xs are invalid and must not appear in base64. PAD indicates
  295. * end-of-string. */
  296. static const uint8_t base64_decode_table[256] = {
  297. X, X, X, X, X, X, X, X, X, SP, SP, SP, X, SP, X, X, /* */
  298. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  299. SP, X, X, X, X, X, X, X, X, X, X, 62, X, X, X, 63,
  300. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, X, X, X, PAD, X, X,
  301. X, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  302. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, X, X, X, X, X,
  303. X, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  304. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, X, X, X, X, X,
  305. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  306. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  307. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  308. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  309. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  310. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  311. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  312. X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
  313. };
  314. /** Base64 decode <b>srclen</b> bytes of data from <b>src</b>. Write
  315. * the result into <b>dest</b>, if it will fit within <b>destlen</b>
  316. * bytes. Return the number of bytes written on success; -1 if
  317. * destlen is too short, or other failure.
  318. *
  319. * NOTE 1: destlen is checked conservatively, as though srclen contained no
  320. * spaces or padding.
  321. *
  322. * NOTE 2: This implementation does not check for the correct number of
  323. * padding "=" characters at the end of the string, and does not check
  324. * for internal padding characters.
  325. */
  326. int
  327. base64_decode(char *dest, size_t destlen, const char *src, size_t srclen)
  328. {
  329. const char *eos = src+srclen;
  330. uint32_t n=0;
  331. int n_idx=0;
  332. size_t di = 0;
  333. if (destlen > INT_MAX)
  334. return -1;
  335. /* Make sure we leave no uninitialized data in the destination buffer. */
  336. memset(dest, 0, destlen);
  337. /* Iterate over all the bytes in src. Each one will add 0 or 6 bits to the
  338. * value we're decoding. Accumulate bits in <b>n</b>, and whenever we have
  339. * 24 bits, batch them into 3 bytes and flush those bytes to dest.
  340. */
  341. for ( ; src < eos; ++src) {
  342. unsigned char c = (unsigned char) *src;
  343. uint8_t v = base64_decode_table[c];
  344. switch (v) {
  345. case X:
  346. /* This character isn't allowed in base64. */
  347. return -1;
  348. case SP:
  349. /* This character is whitespace, and has no effect. */
  350. continue;
  351. case PAD:
  352. /* We've hit an = character: the data is over. */
  353. goto end_of_loop;
  354. default:
  355. /* We have an actual 6-bit value. Append it to the bits in n. */
  356. n = (n<<6) | v;
  357. if ((++n_idx) == 4) {
  358. /* We've accumulated 24 bits in n. Flush them. */
  359. if (destlen < 3 || di > destlen - 3)
  360. return -1;
  361. dest[di++] = (n>>16);
  362. dest[di++] = (n>>8) & 0xff;
  363. dest[di++] = (n) & 0xff;
  364. n_idx = 0;
  365. n = 0;
  366. }
  367. }
  368. }
  369. end_of_loop:
  370. /* If we have leftover bits, we need to cope. */
  371. switch (n_idx) {
  372. case 0:
  373. default:
  374. /* No leftover bits. We win. */
  375. break;
  376. case 1:
  377. /* 6 leftover bits. That's invalid; we can't form a byte out of that. */
  378. return -1;
  379. case 2:
  380. /* 12 leftover bits: The last 4 are padding and the first 8 are data. */
  381. if (destlen < 1 || di > destlen - 1)
  382. return -1;
  383. dest[di++] = n >> 4;
  384. break;
  385. case 3:
  386. /* 18 leftover bits: The last 2 are padding and the first 16 are data. */
  387. if (destlen < 2 || di > destlen - 2)
  388. return -1;
  389. dest[di++] = n >> 10;
  390. dest[di++] = n >> 2;
  391. }
  392. tor_assert(di <= destlen);
  393. return (int)di;
  394. }
  395. #undef X
  396. #undef SP
  397. #undef PAD
  398. /** Encode the <b>srclen</b> bytes at <b>src</b> in a NUL-terminated,
  399. * uppercase hexadecimal string; store it in the <b>destlen</b>-byte buffer
  400. * <b>dest</b>.
  401. */
  402. void
  403. base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
  404. {
  405. const char *end;
  406. char *cp;
  407. tor_assert(srclen < SIZE_T_CEILING / 2 - 1);
  408. tor_assert(destlen >= BASE16_BUFSIZE(srclen));
  409. tor_assert(destlen < SIZE_T_CEILING);
  410. /* Make sure we leave no uninitialized data in the destination buffer. */
  411. memset(dest, 0, destlen);
  412. cp = dest;
  413. end = src+srclen;
  414. while (src<end) {
  415. *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) >> 4 ];
  416. *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) & 0xf ];
  417. ++src;
  418. }
  419. *cp = '\0';
  420. }
  421. /** Given a hexadecimal string of <b>srclen</b> bytes in <b>src</b>, decode
  422. * it and store the result in the <b>destlen</b>-byte buffer at <b>dest</b>.
  423. * Return the number of bytes decoded on success, -1 on failure. If
  424. * <b>destlen</b> is greater than INT_MAX or less than half of
  425. * <b>srclen</b>, -1 is returned. */
  426. int
  427. base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
  428. {
  429. const char *end;
  430. char *dest_orig = dest;
  431. int v1,v2;
  432. if ((srclen % 2) != 0)
  433. return -1;
  434. if (destlen < srclen/2 || destlen > INT_MAX)
  435. return -1;
  436. /* Make sure we leave no uninitialized data in the destination buffer. */
  437. memset(dest, 0, destlen);
  438. end = src+srclen;
  439. while (src<end) {
  440. v1 = hex_decode_digit(*src);
  441. v2 = hex_decode_digit(*(src+1));
  442. if (v1<0||v2<0)
  443. return -1;
  444. *(uint8_t*)dest = (v1<<4)|v2;
  445. ++dest;
  446. src+=2;
  447. }
  448. tor_assert((dest-dest_orig) <= (ptrdiff_t) destlen);
  449. return (int) (dest-dest_orig);
  450. }