base64.c 7.7 KB

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