base64.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * RFC 1521 base64 encoding/decoding
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #include <limits.h>
  8. #include "common.h"
  9. #if defined(MBEDTLS_BASE64_C)
  10. #include "mbedtls/base64.h"
  11. #include "base64_internal.h"
  12. #include "constant_time_internal.h"
  13. #include "mbedtls/error.h"
  14. #include <stdint.h>
  15. #if defined(MBEDTLS_SELF_TEST)
  16. #include <string.h>
  17. #include "mbedtls/platform.h"
  18. #endif /* MBEDTLS_SELF_TEST */
  19. MBEDTLS_STATIC_TESTABLE
  20. unsigned char mbedtls_ct_base64_enc_char(unsigned char value)
  21. {
  22. unsigned char digit = 0;
  23. /* For each range of values, if value is in that range, mask digit with
  24. * the corresponding value. Since value can only be in a single range,
  25. * only at most one masking will change digit. */
  26. digit |= mbedtls_ct_uchar_in_range_if(0, 25, value, 'A' + value);
  27. digit |= mbedtls_ct_uchar_in_range_if(26, 51, value, 'a' + value - 26);
  28. digit |= mbedtls_ct_uchar_in_range_if(52, 61, value, '0' + value - 52);
  29. digit |= mbedtls_ct_uchar_in_range_if(62, 62, value, '+');
  30. digit |= mbedtls_ct_uchar_in_range_if(63, 63, value, '/');
  31. return digit;
  32. }
  33. MBEDTLS_STATIC_TESTABLE
  34. signed char mbedtls_ct_base64_dec_value(unsigned char c)
  35. {
  36. unsigned char val = 0;
  37. /* For each range of digits, if c is in that range, mask val with
  38. * the corresponding value. Since c can only be in a single range,
  39. * only at most one masking will change val. Set val to one plus
  40. * the desired value so that it stays 0 if c is in none of the ranges. */
  41. val |= mbedtls_ct_uchar_in_range_if('A', 'Z', c, c - 'A' + 0 + 1);
  42. val |= mbedtls_ct_uchar_in_range_if('a', 'z', c, c - 'a' + 26 + 1);
  43. val |= mbedtls_ct_uchar_in_range_if('0', '9', c, c - '0' + 52 + 1);
  44. val |= mbedtls_ct_uchar_in_range_if('+', '+', c, c - '+' + 62 + 1);
  45. val |= mbedtls_ct_uchar_in_range_if('/', '/', c, c - '/' + 63 + 1);
  46. /* At this point, val is 0 if c is an invalid digit and v+1 if c is
  47. * a digit with the value v. */
  48. return val - 1;
  49. }
  50. /*
  51. * Encode a buffer into base64 format
  52. */
  53. int mbedtls_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,
  54. const unsigned char *src, size_t slen)
  55. {
  56. size_t i, n;
  57. int C1, C2, C3;
  58. unsigned char *p;
  59. if (slen == 0) {
  60. *olen = 0;
  61. return 0;
  62. }
  63. n = slen / 3 + (slen % 3 != 0);
  64. if (n > (SIZE_MAX - 1) / 4) {
  65. *olen = SIZE_MAX;
  66. return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
  67. }
  68. n *= 4;
  69. if ((dlen < n + 1) || (NULL == dst)) {
  70. *olen = n + 1;
  71. return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
  72. }
  73. n = (slen / 3) * 3;
  74. for (i = 0, p = dst; i < n; i += 3) {
  75. C1 = *src++;
  76. C2 = *src++;
  77. C3 = *src++;
  78. *p++ = mbedtls_ct_base64_enc_char((C1 >> 2) & 0x3F);
  79. *p++ = mbedtls_ct_base64_enc_char((((C1 & 3) << 4) + (C2 >> 4))
  80. & 0x3F);
  81. *p++ = mbedtls_ct_base64_enc_char((((C2 & 15) << 2) + (C3 >> 6))
  82. & 0x3F);
  83. *p++ = mbedtls_ct_base64_enc_char(C3 & 0x3F);
  84. }
  85. if (i < slen) {
  86. C1 = *src++;
  87. C2 = ((i + 1) < slen) ? *src++ : 0;
  88. *p++ = mbedtls_ct_base64_enc_char((C1 >> 2) & 0x3F);
  89. *p++ = mbedtls_ct_base64_enc_char((((C1 & 3) << 4) + (C2 >> 4))
  90. & 0x3F);
  91. if ((i + 1) < slen) {
  92. *p++ = mbedtls_ct_base64_enc_char(((C2 & 15) << 2) & 0x3F);
  93. } else {
  94. *p++ = '=';
  95. }
  96. *p++ = '=';
  97. }
  98. *olen = (size_t) (p - dst);
  99. *p = 0;
  100. return 0;
  101. }
  102. /*
  103. * Decode a base64-formatted buffer
  104. */
  105. int mbedtls_base64_decode(unsigned char *dst, size_t dlen, size_t *olen,
  106. const unsigned char *src, size_t slen)
  107. {
  108. size_t i; /* index in source */
  109. size_t n; /* number of digits or trailing = in source */
  110. uint32_t x; /* value accumulator */
  111. unsigned accumulated_digits = 0;
  112. unsigned equals = 0;
  113. int spaces_present = 0;
  114. unsigned char *p;
  115. /* First pass: check for validity and get output length */
  116. for (i = n = 0; i < slen; i++) {
  117. /* Skip spaces before checking for EOL */
  118. spaces_present = 0;
  119. while (i < slen && src[i] == ' ') {
  120. ++i;
  121. spaces_present = 1;
  122. }
  123. /* Spaces at end of buffer are OK */
  124. if (i == slen) {
  125. break;
  126. }
  127. if ((slen - i) >= 2 &&
  128. src[i] == '\r' && src[i + 1] == '\n') {
  129. continue;
  130. }
  131. if (src[i] == '\n') {
  132. continue;
  133. }
  134. /* Space inside a line is an error */
  135. if (spaces_present) {
  136. return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
  137. }
  138. if (src[i] > 127) {
  139. return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
  140. }
  141. if (src[i] == '=') {
  142. if (++equals > 2) {
  143. return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
  144. }
  145. } else {
  146. if (equals != 0) {
  147. return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
  148. }
  149. if (mbedtls_ct_base64_dec_value(src[i]) < 0) {
  150. return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
  151. }
  152. }
  153. n++;
  154. }
  155. /* In valid base64, the number of digits (n-equals) is always of the form
  156. * 4*k, 4*k+2 or *4k+3. Also, the number n of digits plus the number of
  157. * equal signs at the end is always a multiple of 4. */
  158. if ((n - equals) % 4 == 1) {
  159. return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
  160. }
  161. if (n % 4 != 0) {
  162. return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
  163. }
  164. /* We've determined that the input is valid, and that it contains
  165. * exactly k blocks of digits-or-equals, with n = 4 * k,
  166. * and equals only present at the end of the last block if at all.
  167. * Now we can calculate the length of the output.
  168. *
  169. * Each block of 4 digits in the input map to 3 bytes of output.
  170. * For the last block:
  171. * - abcd (where abcd are digits) is a full 3-byte block;
  172. * - abc= means 1 byte less than a full 3-byte block of output;
  173. * - ab== means 2 bytes less than a full 3-byte block of output;
  174. * - a==== and ==== is rejected above.
  175. */
  176. *olen = (n / 4) * 3 - equals;
  177. /* If the output buffer is too small, signal this and stop here.
  178. * Also, as documented, stop here if `dst` is null, independently of
  179. * `dlen`.
  180. *
  181. * There is an edge case when the output is empty: in this case,
  182. * `dlen == 0` with `dst == NULL` is valid (on some platforms,
  183. * `malloc(0)` returns `NULL`). Since the call is valid, we return
  184. * 0 in this case.
  185. */
  186. if ((*olen != 0 && dst == NULL) || dlen < *olen) {
  187. return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
  188. }
  189. for (x = 0, p = dst; i > 0; i--, src++) {
  190. if (*src == '\r' || *src == '\n' || *src == ' ') {
  191. continue;
  192. }
  193. if (*src == '=') {
  194. /* We already know from the first loop that equal signs are
  195. * only at the end. */
  196. break;
  197. }
  198. x = x << 6;
  199. x |= mbedtls_ct_base64_dec_value(*src);
  200. if (++accumulated_digits == 4) {
  201. accumulated_digits = 0;
  202. *p++ = MBEDTLS_BYTE_2(x);
  203. *p++ = MBEDTLS_BYTE_1(x);
  204. *p++ = MBEDTLS_BYTE_0(x);
  205. }
  206. }
  207. if (accumulated_digits == 3) {
  208. *p++ = MBEDTLS_BYTE_2(x << 6);
  209. *p++ = MBEDTLS_BYTE_1(x << 6);
  210. } else if (accumulated_digits == 2) {
  211. *p++ = MBEDTLS_BYTE_2(x << 12);
  212. }
  213. if (*olen != (size_t) (p - dst)) {
  214. return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  215. }
  216. return 0;
  217. }
  218. #if defined(MBEDTLS_SELF_TEST)
  219. static const unsigned char base64_test_dec[64] =
  220. {
  221. 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
  222. 0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
  223. 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
  224. 0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
  225. 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
  226. 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
  227. 0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
  228. 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
  229. };
  230. static const unsigned char base64_test_enc[] =
  231. "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
  232. "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
  233. /*
  234. * Checkup routine
  235. */
  236. int mbedtls_base64_self_test(int verbose)
  237. {
  238. size_t len;
  239. const unsigned char *src;
  240. unsigned char buffer[128];
  241. if (verbose != 0) {
  242. mbedtls_printf(" Base64 encoding test: ");
  243. }
  244. src = base64_test_dec;
  245. if (mbedtls_base64_encode(buffer, sizeof(buffer), &len, src, 64) != 0 ||
  246. memcmp(base64_test_enc, buffer, 88) != 0) {
  247. if (verbose != 0) {
  248. mbedtls_printf("failed\n");
  249. }
  250. return 1;
  251. }
  252. if (verbose != 0) {
  253. mbedtls_printf("passed\n Base64 decoding test: ");
  254. }
  255. src = base64_test_enc;
  256. if (mbedtls_base64_decode(buffer, sizeof(buffer), &len, src, 88) != 0 ||
  257. memcmp(base64_test_dec, buffer, 64) != 0) {
  258. if (verbose != 0) {
  259. mbedtls_printf("failed\n");
  260. }
  261. return 1;
  262. }
  263. if (verbose != 0) {
  264. mbedtls_printf("passed\n\n");
  265. }
  266. return 0;
  267. }
  268. #endif /* MBEDTLS_SELF_TEST */
  269. #endif /* MBEDTLS_BASE64_C */