base64.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "constant_time_internal.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. /*
  35. * Encode a buffer into base64 format
  36. */
  37. int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  38. const unsigned char *src, size_t slen )
  39. {
  40. size_t i, n;
  41. int C1, C2, C3;
  42. unsigned char *p;
  43. if( slen == 0 )
  44. {
  45. *olen = 0;
  46. return( 0 );
  47. }
  48. n = slen / 3 + ( slen % 3 != 0 );
  49. if( n > ( BASE64_SIZE_T_MAX - 1 ) / 4 )
  50. {
  51. *olen = BASE64_SIZE_T_MAX;
  52. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  53. }
  54. n *= 4;
  55. if( ( dlen < n + 1 ) || ( NULL == dst ) )
  56. {
  57. *olen = n + 1;
  58. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  59. }
  60. n = ( slen / 3 ) * 3;
  61. for( i = 0, p = dst; i < n; i += 3 )
  62. {
  63. C1 = *src++;
  64. C2 = *src++;
  65. C3 = *src++;
  66. *p++ = mbedtls_ct_base64_enc_char( ( C1 >> 2 ) & 0x3F );
  67. *p++ = mbedtls_ct_base64_enc_char( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) )
  68. & 0x3F );
  69. *p++ = mbedtls_ct_base64_enc_char( ( ( ( C2 & 15 ) << 2 ) + ( C3 >> 6 ) )
  70. & 0x3F );
  71. *p++ = mbedtls_ct_base64_enc_char( C3 & 0x3F );
  72. }
  73. if( i < slen )
  74. {
  75. C1 = *src++;
  76. C2 = ( ( i + 1 ) < slen ) ? *src++ : 0;
  77. *p++ = mbedtls_ct_base64_enc_char( ( C1 >> 2 ) & 0x3F );
  78. *p++ = mbedtls_ct_base64_enc_char( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) )
  79. & 0x3F );
  80. if( ( i + 1 ) < slen )
  81. *p++ = mbedtls_ct_base64_enc_char( ( ( C2 & 15 ) << 2 ) & 0x3F );
  82. else *p++ = '=';
  83. *p++ = '=';
  84. }
  85. *olen = p - dst;
  86. *p = 0;
  87. return( 0 );
  88. }
  89. /*
  90. * Decode a base64-formatted buffer
  91. */
  92. int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  93. const unsigned char *src, size_t slen )
  94. {
  95. size_t i; /* index in source */
  96. size_t n; /* number of digits or trailing = in source */
  97. uint32_t x; /* value accumulator */
  98. unsigned accumulated_digits = 0;
  99. unsigned equals = 0;
  100. int spaces_present = 0;
  101. unsigned char *p;
  102. /* First pass: check for validity and get output length */
  103. for( i = n = 0; i < slen; i++ )
  104. {
  105. /* Skip spaces before checking for EOL */
  106. spaces_present = 0;
  107. while( i < slen && src[i] == ' ' )
  108. {
  109. ++i;
  110. spaces_present = 1;
  111. }
  112. /* Spaces at end of buffer are OK */
  113. if( i == slen )
  114. break;
  115. if( ( slen - i ) >= 2 &&
  116. src[i] == '\r' && src[i + 1] == '\n' )
  117. continue;
  118. if( src[i] == '\n' )
  119. continue;
  120. /* Space inside a line is an error */
  121. if( spaces_present )
  122. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  123. if( src[i] > 127 )
  124. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  125. if( src[i] == '=' )
  126. {
  127. if( ++equals > 2 )
  128. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  129. }
  130. else
  131. {
  132. if( equals != 0 )
  133. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  134. if( mbedtls_ct_base64_dec_value( src[i] ) < 0 )
  135. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  136. }
  137. n++;
  138. }
  139. if( n == 0 )
  140. {
  141. *olen = 0;
  142. return( 0 );
  143. }
  144. /* The following expression is to calculate the following formula without
  145. * risk of integer overflow in n:
  146. * n = ( ( n * 6 ) + 7 ) >> 3;
  147. */
  148. n = ( 6 * ( n >> 3 ) ) + ( ( 6 * ( n & 0x7 ) + 7 ) >> 3 );
  149. n -= equals;
  150. if( dst == NULL || dlen < n )
  151. {
  152. *olen = n;
  153. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  154. }
  155. equals = 0;
  156. for( x = 0, p = dst; i > 0; i--, src++ )
  157. {
  158. if( *src == '\r' || *src == '\n' || *src == ' ' )
  159. continue;
  160. x = x << 6;
  161. if( *src == '=' )
  162. ++equals;
  163. else
  164. x |= mbedtls_ct_base64_dec_value( *src );
  165. if( ++accumulated_digits == 4 )
  166. {
  167. accumulated_digits = 0;
  168. *p++ = MBEDTLS_BYTE_2( x );
  169. if( equals <= 1 ) *p++ = MBEDTLS_BYTE_1( x );
  170. if( equals <= 0 ) *p++ = MBEDTLS_BYTE_0( x );
  171. }
  172. }
  173. *olen = p - dst;
  174. return( 0 );
  175. }
  176. #if defined(MBEDTLS_SELF_TEST)
  177. static const unsigned char base64_test_dec[64] =
  178. {
  179. 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
  180. 0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
  181. 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
  182. 0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
  183. 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
  184. 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
  185. 0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
  186. 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
  187. };
  188. static const unsigned char base64_test_enc[] =
  189. "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
  190. "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
  191. /*
  192. * Checkup routine
  193. */
  194. int mbedtls_base64_self_test( int verbose )
  195. {
  196. size_t len;
  197. const unsigned char *src;
  198. unsigned char buffer[128];
  199. if( verbose != 0 )
  200. mbedtls_printf( " Base64 encoding test: " );
  201. src = base64_test_dec;
  202. if( mbedtls_base64_encode( buffer, sizeof( buffer ), &len, src, 64 ) != 0 ||
  203. memcmp( base64_test_enc, buffer, 88 ) != 0 )
  204. {
  205. if( verbose != 0 )
  206. mbedtls_printf( "failed\n" );
  207. return( 1 );
  208. }
  209. if( verbose != 0 )
  210. mbedtls_printf( "passed\n Base64 decoding test: " );
  211. src = base64_test_enc;
  212. if( mbedtls_base64_decode( buffer, sizeof( buffer ), &len, src, 88 ) != 0 ||
  213. memcmp( base64_test_dec, buffer, 64 ) != 0 )
  214. {
  215. if( verbose != 0 )
  216. mbedtls_printf( "failed\n" );
  217. return( 1 );
  218. }
  219. if( verbose != 0 )
  220. mbedtls_printf( "passed\n\n" );
  221. return( 0 );
  222. }
  223. #endif /* MBEDTLS_SELF_TEST */
  224. #endif /* MBEDTLS_BASE64_C */