base64.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 <stdint.h>
  23. #if defined(MBEDTLS_SELF_TEST)
  24. #include <string.h>
  25. #if defined(MBEDTLS_PLATFORM_C)
  26. #include "mbedtls/platform.h"
  27. #else
  28. #include <stdio.h>
  29. #define mbedtls_printf printf
  30. #endif /* MBEDTLS_PLATFORM_C */
  31. #endif /* MBEDTLS_SELF_TEST */
  32. static const unsigned char base64_enc_map[64] =
  33. {
  34. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  35. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  36. 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
  37. 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  38. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
  39. 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
  40. '8', '9', '+', '/'
  41. };
  42. static const unsigned char base64_dec_map[128] =
  43. {
  44. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  45. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  46. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  47. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  48. 127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
  49. 54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
  50. 127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
  51. 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  52. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  53. 25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
  54. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  55. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  56. 49, 50, 51, 127, 127, 127, 127, 127
  57. };
  58. #define BASE64_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
  59. /*
  60. * Encode a buffer into base64 format
  61. */
  62. int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  63. const unsigned char *src, size_t slen )
  64. {
  65. size_t i, n;
  66. int C1, C2, C3;
  67. unsigned char *p;
  68. if( slen == 0 )
  69. {
  70. *olen = 0;
  71. return( 0 );
  72. }
  73. n = slen / 3 + ( slen % 3 != 0 );
  74. if( n > ( BASE64_SIZE_T_MAX - 1 ) / 4 )
  75. {
  76. *olen = BASE64_SIZE_T_MAX;
  77. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  78. }
  79. n *= 4;
  80. if( ( dlen < n + 1 ) || ( NULL == dst ) )
  81. {
  82. *olen = n + 1;
  83. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  84. }
  85. n = ( slen / 3 ) * 3;
  86. for( i = 0, p = dst; i < n; i += 3 )
  87. {
  88. C1 = *src++;
  89. C2 = *src++;
  90. C3 = *src++;
  91. *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
  92. *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
  93. *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
  94. *p++ = base64_enc_map[C3 & 0x3F];
  95. }
  96. if( i < slen )
  97. {
  98. C1 = *src++;
  99. C2 = ( ( i + 1 ) < slen ) ? *src++ : 0;
  100. *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
  101. *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
  102. if( ( i + 1 ) < slen )
  103. *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
  104. else *p++ = '=';
  105. *p++ = '=';
  106. }
  107. *olen = p - dst;
  108. *p = 0;
  109. return( 0 );
  110. }
  111. /*
  112. * Decode a base64-formatted buffer
  113. */
  114. int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  115. const unsigned char *src, size_t slen )
  116. {
  117. size_t i, n;
  118. uint32_t j, x;
  119. unsigned char *p;
  120. /* First pass: check for validity and get output length */
  121. for( i = n = j = 0; i < slen; i++ )
  122. {
  123. /* Skip spaces before checking for EOL */
  124. x = 0;
  125. while( i < slen && src[i] == ' ' )
  126. {
  127. ++i;
  128. ++x;
  129. }
  130. /* Spaces at end of buffer are OK */
  131. if( i == slen )
  132. break;
  133. if( ( slen - i ) >= 2 &&
  134. src[i] == '\r' && src[i + 1] == '\n' )
  135. continue;
  136. if( src[i] == '\n' )
  137. continue;
  138. /* Space inside a line is an error */
  139. if( x != 0 )
  140. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  141. if( src[i] == '=' && ++j > 2 )
  142. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  143. if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
  144. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  145. if( base64_dec_map[src[i]] < 64 && j != 0 )
  146. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  147. n++;
  148. }
  149. if( n == 0 )
  150. {
  151. *olen = 0;
  152. return( 0 );
  153. }
  154. /* The following expression is to calculate the following formula without
  155. * risk of integer overflow in n:
  156. * n = ( ( n * 6 ) + 7 ) >> 3;
  157. */
  158. n = ( 6 * ( n >> 3 ) ) + ( ( 6 * ( n & 0x7 ) + 7 ) >> 3 );
  159. n -= j;
  160. if( dst == NULL || dlen < n )
  161. {
  162. *olen = n;
  163. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  164. }
  165. for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
  166. {
  167. if( *src == '\r' || *src == '\n' || *src == ' ' )
  168. continue;
  169. j -= ( base64_dec_map[*src] == 64 );
  170. x = ( x << 6 ) | ( base64_dec_map[*src] & 0x3F );
  171. if( ++n == 4 )
  172. {
  173. n = 0;
  174. if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
  175. if( j > 1 ) *p++ = (unsigned char)( x >> 8 );
  176. if( j > 2 ) *p++ = (unsigned char)( x );
  177. }
  178. }
  179. *olen = p - dst;
  180. return( 0 );
  181. }
  182. #if defined(MBEDTLS_SELF_TEST)
  183. static const unsigned char base64_test_dec[64] =
  184. {
  185. 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
  186. 0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
  187. 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
  188. 0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
  189. 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
  190. 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
  191. 0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
  192. 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
  193. };
  194. static const unsigned char base64_test_enc[] =
  195. "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
  196. "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
  197. /*
  198. * Checkup routine
  199. */
  200. int mbedtls_base64_self_test( int verbose )
  201. {
  202. size_t len;
  203. const unsigned char *src;
  204. unsigned char buffer[128];
  205. if( verbose != 0 )
  206. mbedtls_printf( " Base64 encoding test: " );
  207. src = base64_test_dec;
  208. if( mbedtls_base64_encode( buffer, sizeof( buffer ), &len, src, 64 ) != 0 ||
  209. memcmp( base64_test_enc, buffer, 88 ) != 0 )
  210. {
  211. if( verbose != 0 )
  212. mbedtls_printf( "failed\n" );
  213. return( 1 );
  214. }
  215. if( verbose != 0 )
  216. mbedtls_printf( "passed\n Base64 decoding test: " );
  217. src = base64_test_enc;
  218. if( mbedtls_base64_decode( buffer, sizeof( buffer ), &len, src, 88 ) != 0 ||
  219. memcmp( base64_test_dec, buffer, 64 ) != 0 )
  220. {
  221. if( verbose != 0 )
  222. mbedtls_printf( "failed\n" );
  223. return( 1 );
  224. }
  225. if( verbose != 0 )
  226. mbedtls_printf( "passed\n\n" );
  227. return( 0 );
  228. }
  229. #endif /* MBEDTLS_SELF_TEST */
  230. #endif /* MBEDTLS_BASE64_C */