base64.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * RFC 1521 base64 encoding/decoding
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_BASE64_C)
  21. #include "mbedtls/base64.h"
  22. #include "base64_invasive.h"
  23. #include <stdint.h>
  24. #if defined(MBEDTLS_SELF_TEST)
  25. #include <string.h>
  26. #if defined(MBEDTLS_PLATFORM_C)
  27. #include "mbedtls/platform.h"
  28. #else
  29. #include <stdio.h>
  30. #define mbedtls_printf printf
  31. #endif /* MBEDTLS_PLATFORM_C */
  32. #endif /* MBEDTLS_SELF_TEST */
  33. #define BASE64_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
  34. /* Return 0xff if low <= c <= high, 0 otherwise.
  35. *
  36. * Constant flow with respect to c.
  37. */
  38. MBEDTLS_STATIC_TESTABLE
  39. unsigned char mbedtls_base64_mask_of_range( unsigned char low,
  40. unsigned char high,
  41. unsigned char c )
  42. {
  43. /* low_mask is: 0 if low <= c, 0x...ff if low > c */
  44. unsigned low_mask = ( (unsigned) c - low ) >> 8;
  45. /* high_mask is: 0 if c <= high, 0x...ff if c > high */
  46. unsigned high_mask = ( (unsigned) high - c ) >> 8;
  47. return( ~( low_mask | high_mask ) & 0xff );
  48. }
  49. /* Given a value in the range 0..63, return the corresponding Base64 digit.
  50. * The implementation assumes that letters are consecutive (e.g. ASCII
  51. * but not EBCDIC).
  52. */
  53. MBEDTLS_STATIC_TESTABLE
  54. unsigned char mbedtls_base64_enc_char( unsigned char val )
  55. {
  56. unsigned char digit = 0;
  57. /* For each range of values, if val is in that range, mask digit with
  58. * the corresponding value. Since val can only be in a single range,
  59. * only at most one masking will change digit. */
  60. digit |= mbedtls_base64_mask_of_range( 0, 25, val ) & ( 'A' + val );
  61. digit |= mbedtls_base64_mask_of_range( 26, 51, val ) & ( 'a' + val - 26 );
  62. digit |= mbedtls_base64_mask_of_range( 52, 61, val ) & ( '0' + val - 52 );
  63. digit |= mbedtls_base64_mask_of_range( 62, 62, val ) & '+';
  64. digit |= mbedtls_base64_mask_of_range( 63, 63, val ) & '/';
  65. return( digit );
  66. }
  67. /*
  68. * Encode a buffer into base64 format
  69. */
  70. int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  71. const unsigned char *src, size_t slen )
  72. {
  73. size_t i, n;
  74. int C1, C2, C3;
  75. unsigned char *p;
  76. if( slen == 0 )
  77. {
  78. *olen = 0;
  79. return( 0 );
  80. }
  81. n = slen / 3 + ( slen % 3 != 0 );
  82. if( n > ( BASE64_SIZE_T_MAX - 1 ) / 4 )
  83. {
  84. *olen = BASE64_SIZE_T_MAX;
  85. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  86. }
  87. n *= 4;
  88. if( ( dlen < n + 1 ) || ( NULL == dst ) )
  89. {
  90. *olen = n + 1;
  91. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  92. }
  93. n = ( slen / 3 ) * 3;
  94. for( i = 0, p = dst; i < n; i += 3 )
  95. {
  96. C1 = *src++;
  97. C2 = *src++;
  98. C3 = *src++;
  99. *p++ = mbedtls_base64_enc_char( ( C1 >> 2 ) & 0x3F );
  100. *p++ = mbedtls_base64_enc_char( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) )
  101. & 0x3F );
  102. *p++ = mbedtls_base64_enc_char( ( ( ( C2 & 15 ) << 2 ) + ( C3 >> 6 ) )
  103. & 0x3F );
  104. *p++ = mbedtls_base64_enc_char( C3 & 0x3F );
  105. }
  106. if( i < slen )
  107. {
  108. C1 = *src++;
  109. C2 = ( ( i + 1 ) < slen ) ? *src++ : 0;
  110. *p++ = mbedtls_base64_enc_char( ( C1 >> 2 ) & 0x3F );
  111. *p++ = mbedtls_base64_enc_char( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) )
  112. & 0x3F );
  113. if( ( i + 1 ) < slen )
  114. *p++ = mbedtls_base64_enc_char( ( ( C2 & 15 ) << 2 ) & 0x3F );
  115. else *p++ = '=';
  116. *p++ = '=';
  117. }
  118. *olen = p - dst;
  119. *p = 0;
  120. return( 0 );
  121. }
  122. /* Given a Base64 digit, return its value.
  123. * If c is not a Base64 digit ('A'..'Z', 'a'..'z', '0'..'9', '+' or '/'),
  124. * return -1.
  125. *
  126. * The implementation assumes that letters are consecutive (e.g. ASCII
  127. * but not EBCDIC).
  128. *
  129. * The implementation is constant-flow (no branch or memory access depending
  130. * on the value of c) unless the compiler inlines and optimizes a specific
  131. * access.
  132. */
  133. MBEDTLS_STATIC_TESTABLE
  134. signed char mbedtls_base64_dec_value( unsigned char c )
  135. {
  136. unsigned char val = 0;
  137. /* For each range of digits, if c is in that range, mask val with
  138. * the corresponding value. Since c can only be in a single range,
  139. * only at most one masking will change val. Set val to one plus
  140. * the desired value so that it stays 0 if c is in none of the ranges. */
  141. val |= mbedtls_base64_mask_of_range( 'A', 'Z', c ) & ( c - 'A' + 0 + 1 );
  142. val |= mbedtls_base64_mask_of_range( 'a', 'z', c ) & ( c - 'a' + 26 + 1 );
  143. val |= mbedtls_base64_mask_of_range( '0', '9', c ) & ( c - '0' + 52 + 1 );
  144. val |= mbedtls_base64_mask_of_range( '+', '+', c ) & ( c - '+' + 62 + 1 );
  145. val |= mbedtls_base64_mask_of_range( '/', '/', c ) & ( c - '/' + 63 + 1 );
  146. /* At this point, val is 0 if c is an invalid digit and v+1 if c is
  147. * a digit with the value v. */
  148. return( val - 1 );
  149. }
  150. /*
  151. * Decode a base64-formatted buffer
  152. */
  153. int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  154. const unsigned char *src, size_t slen )
  155. {
  156. size_t i; /* index in source */
  157. size_t n; /* number of digits or trailing = in source */
  158. uint32_t x; /* value accumulator */
  159. unsigned accumulated_digits = 0;
  160. unsigned equals = 0;
  161. int spaces_present = 0;
  162. unsigned char *p;
  163. /* First pass: check for validity and get output length */
  164. for( i = n = 0; i < slen; i++ )
  165. {
  166. /* Skip spaces before checking for EOL */
  167. spaces_present = 0;
  168. while( i < slen && src[i] == ' ' )
  169. {
  170. ++i;
  171. spaces_present = 1;
  172. }
  173. /* Spaces at end of buffer are OK */
  174. if( i == slen )
  175. break;
  176. if( ( slen - i ) >= 2 &&
  177. src[i] == '\r' && src[i + 1] == '\n' )
  178. continue;
  179. if( src[i] == '\n' )
  180. continue;
  181. /* Space inside a line is an error */
  182. if( spaces_present )
  183. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  184. if( src[i] > 127 )
  185. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  186. if( src[i] == '=' )
  187. {
  188. if( ++equals > 2 )
  189. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  190. }
  191. else
  192. {
  193. if( equals != 0 )
  194. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  195. if( mbedtls_base64_dec_value( src[i] ) < 0 )
  196. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  197. }
  198. n++;
  199. }
  200. if( n == 0 )
  201. {
  202. *olen = 0;
  203. return( 0 );
  204. }
  205. /* The following expression is to calculate the following formula without
  206. * risk of integer overflow in n:
  207. * n = ( ( n * 6 ) + 7 ) >> 3;
  208. */
  209. n = ( 6 * ( n >> 3 ) ) + ( ( 6 * ( n & 0x7 ) + 7 ) >> 3 );
  210. n -= equals;
  211. if( dst == NULL || dlen < n )
  212. {
  213. *olen = n;
  214. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  215. }
  216. equals = 0;
  217. for( x = 0, p = dst; i > 0; i--, src++ )
  218. {
  219. if( *src == '\r' || *src == '\n' || *src == ' ' )
  220. continue;
  221. x = x << 6;
  222. if( *src == '=' )
  223. ++equals;
  224. else
  225. x |= mbedtls_base64_dec_value( *src );
  226. if( ++accumulated_digits == 4 )
  227. {
  228. accumulated_digits = 0;
  229. *p++ = MBEDTLS_BYTE_2( x );
  230. if( equals <= 1 ) *p++ = MBEDTLS_BYTE_1( x );
  231. if( equals <= 0 ) *p++ = MBEDTLS_BYTE_0( x );
  232. }
  233. }
  234. *olen = p - dst;
  235. return( 0 );
  236. }
  237. #if defined(MBEDTLS_SELF_TEST)
  238. static const unsigned char base64_test_dec[64] =
  239. {
  240. 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
  241. 0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
  242. 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
  243. 0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
  244. 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
  245. 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
  246. 0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
  247. 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
  248. };
  249. static const unsigned char base64_test_enc[] =
  250. "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
  251. "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
  252. /*
  253. * Checkup routine
  254. */
  255. int mbedtls_base64_self_test( int verbose )
  256. {
  257. size_t len;
  258. const unsigned char *src;
  259. unsigned char buffer[128];
  260. if( verbose != 0 )
  261. mbedtls_printf( " Base64 encoding test: " );
  262. src = base64_test_dec;
  263. if( mbedtls_base64_encode( buffer, sizeof( buffer ), &len, src, 64 ) != 0 ||
  264. memcmp( base64_test_enc, buffer, 88 ) != 0 )
  265. {
  266. if( verbose != 0 )
  267. mbedtls_printf( "failed\n" );
  268. return( 1 );
  269. }
  270. if( verbose != 0 )
  271. mbedtls_printf( "passed\n Base64 decoding test: " );
  272. src = base64_test_enc;
  273. if( mbedtls_base64_decode( buffer, sizeof( buffer ), &len, src, 88 ) != 0 ||
  274. memcmp( base64_test_dec, buffer, 64 ) != 0 )
  275. {
  276. if( verbose != 0 )
  277. mbedtls_printf( "failed\n" );
  278. return( 1 );
  279. }
  280. if( verbose != 0 )
  281. mbedtls_printf( "passed\n\n" );
  282. return( 0 );
  283. }
  284. #endif /* MBEDTLS_SELF_TEST */
  285. #endif /* MBEDTLS_BASE64_C */