base64.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_BASE64_C)
  52. #include "mbedtls/base64.h"
  53. #include <stdint.h>
  54. #if defined(MBEDTLS_SELF_TEST)
  55. #include <string.h>
  56. #if defined(MBEDTLS_PLATFORM_C)
  57. #include "mbedtls/platform.h"
  58. #else
  59. #include <stdio.h>
  60. #define mbedtls_printf printf
  61. #endif /* MBEDTLS_PLATFORM_C */
  62. #endif /* MBEDTLS_SELF_TEST */
  63. static const unsigned char base64_enc_map[64] =
  64. {
  65. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  66. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  67. 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
  68. 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  69. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
  70. 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
  71. '8', '9', '+', '/'
  72. };
  73. static const unsigned char base64_dec_map[128] =
  74. {
  75. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  76. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  77. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  78. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  79. 127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
  80. 54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
  81. 127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
  82. 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  83. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  84. 25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
  85. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  86. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  87. 49, 50, 51, 127, 127, 127, 127, 127
  88. };
  89. #define BASE64_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
  90. /*
  91. * Constant flow conditional assignment to unsigned char
  92. */
  93. static void mbedtls_base64_cond_assign_uchar( unsigned char * dest, const unsigned char * const src,
  94. unsigned char condition )
  95. {
  96. /* MSVC has a warning about unary minus on unsigned integer types,
  97. * but this is well-defined and precisely what we want to do here. */
  98. #if defined(_MSC_VER)
  99. #pragma warning( push )
  100. #pragma warning( disable : 4146 )
  101. #endif
  102. /* Generate bitmask from condition, mask will either be 0xFF or 0 */
  103. unsigned char mask = ( condition | -condition );
  104. mask >>= 7;
  105. mask = -mask;
  106. #if defined(_MSC_VER)
  107. #pragma warning( pop )
  108. #endif
  109. *dest = ( ( *src ) & mask ) | ( ( *dest ) & ~mask );
  110. }
  111. /*
  112. * Constant flow conditional assignment to uint_32
  113. */
  114. static void mbedtls_base64_cond_assign_uint32( uint32_t * dest, const uint32_t src,
  115. uint32_t condition )
  116. {
  117. /* MSVC has a warning about unary minus on unsigned integer types,
  118. * but this is well-defined and precisely what we want to do here. */
  119. #if defined(_MSC_VER)
  120. #pragma warning( push )
  121. #pragma warning( disable : 4146 )
  122. #endif
  123. /* Generate bitmask from condition, mask will either be 0xFFFFFFFF or 0 */
  124. uint32_t mask = ( condition | -condition );
  125. mask >>= 31;
  126. mask = -mask;
  127. #if defined(_MSC_VER)
  128. #pragma warning( pop )
  129. #endif
  130. *dest = ( src & mask ) | ( ( *dest ) & ~mask );
  131. }
  132. /*
  133. * Constant flow check for equality
  134. */
  135. static unsigned char mbedtls_base64_eq( size_t in_a, size_t in_b )
  136. {
  137. size_t difference = in_a ^ in_b;
  138. /* MSVC has a warning about unary minus on unsigned integer types,
  139. * but this is well-defined and precisely what we want to do here. */
  140. #if defined(_MSC_VER)
  141. #pragma warning( push )
  142. #pragma warning( disable : 4146 )
  143. #endif
  144. difference |= -difference;
  145. #if defined(_MSC_VER)
  146. #pragma warning( pop )
  147. #endif
  148. /* cope with the varying size of size_t per platform */
  149. difference >>= ( sizeof( difference ) * 8 - 1 );
  150. return (unsigned char) ( 1 ^ difference );
  151. }
  152. /*
  153. * Constant flow lookup into table.
  154. */
  155. static unsigned char mbedtls_base64_table_lookup( const unsigned char * const table,
  156. const size_t table_size, const size_t table_index )
  157. {
  158. size_t i;
  159. unsigned char result = 0;
  160. for( i = 0; i < table_size; ++i )
  161. {
  162. mbedtls_base64_cond_assign_uchar( &result, &table[i], mbedtls_base64_eq( i, table_index ) );
  163. }
  164. return result;
  165. }
  166. /*
  167. * Encode a buffer into base64 format
  168. */
  169. int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  170. const unsigned char *src, size_t slen )
  171. {
  172. size_t i, n;
  173. int C1, C2, C3;
  174. unsigned char *p;
  175. if( slen == 0 )
  176. {
  177. *olen = 0;
  178. return( 0 );
  179. }
  180. n = slen / 3 + ( slen % 3 != 0 );
  181. if( n > ( BASE64_SIZE_T_MAX - 1 ) / 4 )
  182. {
  183. *olen = BASE64_SIZE_T_MAX;
  184. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  185. }
  186. n *= 4;
  187. if( ( dlen < n + 1 ) || ( NULL == dst ) )
  188. {
  189. *olen = n + 1;
  190. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  191. }
  192. n = ( slen / 3 ) * 3;
  193. for( i = 0, p = dst; i < n; i += 3 )
  194. {
  195. C1 = *src++;
  196. C2 = *src++;
  197. C3 = *src++;
  198. *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  199. ( ( C1 >> 2 ) & 0x3F ) );
  200. *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  201. ( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) ) & 0x3F ) );
  202. *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  203. ( ( ( ( C2 & 15 ) << 2 ) + ( C3 >> 6 ) ) & 0x3F ) );
  204. *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  205. ( C3 & 0x3F ) );
  206. }
  207. if( i < slen )
  208. {
  209. C1 = *src++;
  210. C2 = ( ( i + 1 ) < slen ) ? *src++ : 0;
  211. *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  212. ( ( C1 >> 2 ) & 0x3F ) );
  213. *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  214. ( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) ) & 0x3F ) );
  215. if( ( i + 1 ) < slen )
  216. *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  217. ( ( ( C2 & 15 ) << 2 ) & 0x3F ) );
  218. else *p++ = '=';
  219. *p++ = '=';
  220. }
  221. *olen = p - dst;
  222. *p = 0;
  223. return( 0 );
  224. }
  225. /*
  226. * Decode a base64-formatted buffer
  227. */
  228. int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  229. const unsigned char *src, size_t slen )
  230. {
  231. size_t i, n;
  232. uint32_t j, x;
  233. unsigned char *p;
  234. unsigned char dec_map_lookup;
  235. /* First pass: check for validity and get output length */
  236. for( i = n = j = 0; i < slen; i++ )
  237. {
  238. /* Skip spaces before checking for EOL */
  239. x = 0;
  240. while( i < slen && src[i] == ' ' )
  241. {
  242. ++i;
  243. ++x;
  244. }
  245. /* Spaces at end of buffer are OK */
  246. if( i == slen )
  247. break;
  248. if( ( slen - i ) >= 2 &&
  249. src[i] == '\r' && src[i + 1] == '\n' )
  250. continue;
  251. if( src[i] == '\n' )
  252. continue;
  253. /* Space inside a line is an error */
  254. if( x != 0 )
  255. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  256. if( src[i] == '=' && ++j > 2 )
  257. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  258. dec_map_lookup = mbedtls_base64_table_lookup( base64_dec_map, sizeof( base64_dec_map ), src[i] );
  259. if( src[i] > 127 || dec_map_lookup == 127 )
  260. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  261. if( dec_map_lookup < 64 && j != 0 )
  262. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  263. n++;
  264. }
  265. if( n == 0 )
  266. {
  267. *olen = 0;
  268. return( 0 );
  269. }
  270. /* The following expression is to calculate the following formula without
  271. * risk of integer overflow in n:
  272. * n = ( ( n * 6 ) + 7 ) >> 3;
  273. */
  274. n = ( 6 * ( n >> 3 ) ) + ( ( 6 * ( n & 0x7 ) + 7 ) >> 3 );
  275. n -= j;
  276. if( dst == NULL || dlen < n )
  277. {
  278. *olen = n;
  279. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  280. }
  281. for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
  282. {
  283. if( *src == '\r' || *src == '\n' || *src == ' ' )
  284. continue;
  285. dec_map_lookup = mbedtls_base64_table_lookup( base64_dec_map, sizeof( base64_dec_map ), *src );
  286. mbedtls_base64_cond_assign_uint32( &j, j - 1, mbedtls_base64_eq( dec_map_lookup, 64 ) );
  287. x = ( x << 6 ) | ( dec_map_lookup & 0x3F );
  288. if( ++n == 4 )
  289. {
  290. n = 0;
  291. if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
  292. if( j > 1 ) *p++ = (unsigned char)( x >> 8 );
  293. if( j > 2 ) *p++ = (unsigned char)( x );
  294. }
  295. }
  296. *olen = p - dst;
  297. return( 0 );
  298. }
  299. #if defined(MBEDTLS_SELF_TEST)
  300. static const unsigned char base64_test_dec[64] =
  301. {
  302. 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
  303. 0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
  304. 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
  305. 0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
  306. 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
  307. 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
  308. 0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
  309. 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
  310. };
  311. static const unsigned char base64_test_enc[] =
  312. "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
  313. "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
  314. /*
  315. * Checkup routine
  316. */
  317. int mbedtls_base64_self_test( int verbose )
  318. {
  319. size_t len;
  320. const unsigned char *src;
  321. unsigned char buffer[128];
  322. if( verbose != 0 )
  323. mbedtls_printf( " Base64 encoding test: " );
  324. src = base64_test_dec;
  325. if( mbedtls_base64_encode( buffer, sizeof( buffer ), &len, src, 64 ) != 0 ||
  326. memcmp( base64_test_enc, buffer, 88 ) != 0 )
  327. {
  328. if( verbose != 0 )
  329. mbedtls_printf( "failed\n" );
  330. return( 1 );
  331. }
  332. if( verbose != 0 )
  333. mbedtls_printf( "passed\n Base64 decoding test: " );
  334. src = base64_test_enc;
  335. if( mbedtls_base64_decode( buffer, sizeof( buffer ), &len, src, 88 ) != 0 ||
  336. memcmp( base64_test_dec, buffer, 64 ) != 0 )
  337. {
  338. if( verbose != 0 )
  339. mbedtls_printf( "failed\n" );
  340. return( 1 );
  341. }
  342. if( verbose != 0 )
  343. mbedtls_printf( "passed\n\n" );
  344. return( 0 );
  345. }
  346. #endif /* MBEDTLS_SELF_TEST */
  347. #endif /* MBEDTLS_BASE64_C */