base64.c 7.5 KB

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