2
0

pr4819-faster-base64.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. diff --git a/library/base64.c b/library/base64.c
  2. index 692e11e3fae..b89313062b6 100644
  3. --- a/library/base64.c
  4. +++ b/library/base64.c
  5. @@ -66,127 +66,38 @@
  6. #endif /* MBEDTLS_PLATFORM_C */
  7. #endif /* MBEDTLS_SELF_TEST */
  8. -static const unsigned char base64_enc_map[64] =
  9. -{
  10. - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  11. - 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  12. - 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
  13. - 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  14. - 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
  15. - 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
  16. - '8', '9', '+', '/'
  17. -};
  18. -
  19. -static const unsigned char base64_dec_map[128] =
  20. -{
  21. - 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  22. - 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  23. - 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  24. - 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  25. - 127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
  26. - 54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
  27. - 127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
  28. - 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  29. - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  30. - 25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
  31. - 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  32. - 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  33. - 49, 50, 51, 127, 127, 127, 127, 127
  34. -};
  35. -
  36. #define BASE64_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
  37. -/*
  38. - * Constant flow conditional assignment to unsigned char
  39. - */
  40. -static void mbedtls_base64_cond_assign_uchar( unsigned char * dest, const unsigned char * const src,
  41. - unsigned char condition )
  42. -{
  43. - /* MSVC has a warning about unary minus on unsigned integer types,
  44. - * but this is well-defined and precisely what we want to do here. */
  45. -#if defined(_MSC_VER)
  46. -#pragma warning( push )
  47. -#pragma warning( disable : 4146 )
  48. -#endif
  49. -
  50. - /* Generate bitmask from condition, mask will either be 0xFF or 0 */
  51. - unsigned char mask = ( condition | -condition );
  52. - mask >>= 7;
  53. - mask = -mask;
  54. -
  55. -#if defined(_MSC_VER)
  56. -#pragma warning( pop )
  57. -#endif
  58. -
  59. - *dest = ( ( *src ) & mask ) | ( ( *dest ) & ~mask );
  60. -}
  61. -
  62. -/*
  63. - * Constant flow conditional assignment to uint_32
  64. - */
  65. -static void mbedtls_base64_cond_assign_uint32( uint32_t * dest, const uint32_t src,
  66. - uint32_t condition )
  67. -{
  68. - /* MSVC has a warning about unary minus on unsigned integer types,
  69. - * but this is well-defined and precisely what we want to do here. */
  70. -#if defined(_MSC_VER)
  71. -#pragma warning( push )
  72. -#pragma warning( disable : 4146 )
  73. -#endif
  74. -
  75. - /* Generate bitmask from condition, mask will either be 0xFFFFFFFF or 0 */
  76. - uint32_t mask = ( condition | -condition );
  77. - mask >>= 31;
  78. - mask = -mask;
  79. -
  80. -#if defined(_MSC_VER)
  81. -#pragma warning( pop )
  82. -#endif
  83. -
  84. - *dest = ( src & mask ) | ( ( *dest ) & ~mask );
  85. -}
  86. -
  87. -/*
  88. - * Constant flow check for equality
  89. +/* Return 0xff if low <= c <= high, 0 otherwise.
  90. + *
  91. + * Constant flow with respect to c.
  92. */
  93. -static unsigned char mbedtls_base64_eq( size_t in_a, size_t in_b )
  94. +static unsigned char mask_of_range( unsigned char low, unsigned char high,
  95. + unsigned char c )
  96. {
  97. - size_t difference = in_a ^ in_b;
  98. -
  99. - /* MSVC has a warning about unary minus on unsigned integer types,
  100. - * but this is well-defined and precisely what we want to do here. */
  101. -#if defined(_MSC_VER)
  102. -#pragma warning( push )
  103. -#pragma warning( disable : 4146 )
  104. -#endif
  105. -
  106. - difference |= -difference;
  107. -
  108. -#if defined(_MSC_VER)
  109. -#pragma warning( pop )
  110. -#endif
  111. -
  112. - /* cope with the varying size of size_t per platform */
  113. - difference >>= ( sizeof( difference ) * 8 - 1 );
  114. -
  115. - return (unsigned char) ( 1 ^ difference );
  116. + /* low_mask is: 0 if low <= c, 0x...ff if low > c */
  117. + unsigned low_mask = ( (unsigned) c - low ) >> 8;
  118. + /* high_mask is: 0 if c <= high, 0x...ff if high > c */
  119. + unsigned high_mask = ( (unsigned) high - c ) >> 8;
  120. + return( ~( low_mask | high_mask ) & 0xff );
  121. }
  122. -/*
  123. - * Constant flow lookup into table.
  124. +/* Given a value in the range 0..63, return the corresponding Base64 digit.
  125. + * The implementation assumes that letters are consecutive (e.g. ASCII
  126. + * but not EBCDIC).
  127. */
  128. -static unsigned char mbedtls_base64_table_lookup( const unsigned char * const table,
  129. - const size_t table_size, const size_t table_index )
  130. +static unsigned char enc_char( unsigned char val )
  131. {
  132. - size_t i;
  133. - unsigned char result = 0;
  134. -
  135. - for( i = 0; i < table_size; ++i )
  136. - {
  137. - mbedtls_base64_cond_assign_uchar( &result, &table[i], mbedtls_base64_eq( i, table_index ) );
  138. - }
  139. -
  140. - return result;
  141. + unsigned char digit = 0;
  142. + /* For each range of values, if val is in that range, mask digit with
  143. + * the corresponding value. Since val can only be in a single range,
  144. + * only at most one masking will change digit. */
  145. + digit |= mask_of_range( 0, 25, val ) & ( 'A' + val );
  146. + digit |= mask_of_range( 26, 51, val ) & ( 'a' + val - 26 );
  147. + digit |= mask_of_range( 52, 61, val ) & ( '0' + val - 52 );
  148. + digit |= mask_of_range( 62, 62, val ) & '+';
  149. + digit |= mask_of_range( 63, 63, val ) & '/';
  150. + return( digit );
  151. }
  152. /*
  153. @@ -229,17 +140,10 @@ int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  154. C2 = *src++;
  155. C3 = *src++;
  156. - *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  157. - ( ( C1 >> 2 ) & 0x3F ) );
  158. -
  159. - *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  160. - ( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) ) & 0x3F ) );
  161. -
  162. - *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  163. - ( ( ( ( C2 & 15 ) << 2 ) + ( C3 >> 6 ) ) & 0x3F ) );
  164. -
  165. - *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  166. - ( C3 & 0x3F ) );
  167. + *p++ = enc_char( ( C1 >> 2 ) & 0x3F );
  168. + *p++ = enc_char( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) ) & 0x3F );
  169. + *p++ = enc_char( ( ( ( C2 & 15 ) << 2 ) + ( C3 >> 6 ) ) & 0x3F );
  170. + *p++ = enc_char( C3 & 0x3F );
  171. }
  172. if( i < slen )
  173. @@ -247,15 +151,11 @@ int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  174. C1 = *src++;
  175. C2 = ( ( i + 1 ) < slen ) ? *src++ : 0;
  176. - *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  177. - ( ( C1 >> 2 ) & 0x3F ) );
  178. -
  179. - *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  180. - ( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) ) & 0x3F ) );
  181. + *p++ = enc_char( ( C1 >> 2 ) & 0x3F );
  182. + *p++ = enc_char( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) ) & 0x3F );
  183. if( ( i + 1 ) < slen )
  184. - *p++ = mbedtls_base64_table_lookup( base64_enc_map, sizeof( base64_enc_map ),
  185. - ( ( ( C2 & 15 ) << 2 ) & 0x3F ) );
  186. + *p++ = enc_char( ( ( C2 & 15 ) << 2 ) & 0x3F );
  187. else *p++ = '=';
  188. *p++ = '=';
  189. @@ -267,26 +167,57 @@ int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  190. return( 0 );
  191. }
  192. +/* Given a Base64 digit, return its value.
  193. + * If c is not a Base64 digit ('A'..'Z', 'a'..'z', '0'..'9', '+' or '/'),
  194. + * return -1.
  195. + *
  196. + * The implementation assumes that letters are consecutive (e.g. ASCII
  197. + * but not EBCDIC).
  198. + *
  199. + * The implementation is constant-flow (no branch or memory access depending
  200. + * on the value of c) unless the compiler inlines and optimizes a specific
  201. + * access.
  202. + */
  203. +static signed char dec_value( unsigned char c )
  204. +{
  205. + unsigned char val = 0;
  206. + /* For each range of digits, if c is in that range, mask val with
  207. + * the corresponding value. Since c can only be in a single range,
  208. + * only at most one masking will change val. Set val to one plus
  209. + * the desired value so that it stays 0 if c is in none of the ranges. */
  210. + val |= mask_of_range( 'A', 'Z', c ) & ( c - 'A' + 0 + 1 );
  211. + val |= mask_of_range( 'a', 'z', c ) & ( c - 'a' + 26 + 1 );
  212. + val |= mask_of_range( '0', '9', c ) & ( c - '0' + 52 + 1 );
  213. + val |= mask_of_range( '+', '+', c ) & ( c - '+' + 62 + 1 );
  214. + val |= mask_of_range( '/', '/', c ) & ( c - '/' + 63 + 1 );
  215. + /* At this point, val is 0 if c is an invalid digit and v+1 if c is
  216. + * a digit with the value v. */
  217. + return( val - 1 );
  218. +}
  219. +
  220. /*
  221. * Decode a base64-formatted buffer
  222. */
  223. int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  224. const unsigned char *src, size_t slen )
  225. {
  226. - size_t i, n;
  227. - uint32_t j, x;
  228. + size_t i; /* index in source */
  229. + size_t n; /* number of digits or trailing = in source */
  230. + uint32_t x; /* value accumulator */
  231. + unsigned accumulated_digits = 0;
  232. + unsigned equals = 0;
  233. + int spaces_present = 0;
  234. unsigned char *p;
  235. - unsigned char dec_map_lookup;
  236. /* First pass: check for validity and get output length */
  237. - for( i = n = j = 0; i < slen; i++ )
  238. + for( i = n = 0; i < slen; i++ )
  239. {
  240. /* Skip spaces before checking for EOL */
  241. - x = 0;
  242. + spaces_present = 0;
  243. while( i < slen && src[i] == ' ' )
  244. {
  245. ++i;
  246. - ++x;
  247. + spaces_present = 1;
  248. }
  249. /* Spaces at end of buffer are OK */
  250. @@ -301,20 +232,24 @@ int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  251. continue;
  252. /* Space inside a line is an error */
  253. - if( x != 0 )
  254. + if( spaces_present )
  255. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  256. - if( src[i] == '=' && ++j > 2 )
  257. - return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  258. -
  259. - dec_map_lookup = mbedtls_base64_table_lookup( base64_dec_map, sizeof( base64_dec_map ), src[i] );
  260. -
  261. - if( src[i] > 127 || dec_map_lookup == 127 )
  262. - return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  263. -
  264. - if( dec_map_lookup < 64 && j != 0 )
  265. + if( src[i] > 127 )
  266. return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  267. + if( src[i] == '=' )
  268. + {
  269. + if( ++equals > 2 )
  270. + return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  271. + }
  272. + else
  273. + {
  274. + if( equals != 0 )
  275. + return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  276. + if( dec_value( src[i] ) < 0 )
  277. + return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
  278. + }
  279. n++;
  280. }
  281. @@ -329,7 +264,7 @@ int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  282. * n = ( ( n * 6 ) + 7 ) >> 3;
  283. */
  284. n = ( 6 * ( n >> 3 ) ) + ( ( 6 * ( n & 0x7 ) + 7 ) >> 3 );
  285. - n -= j;
  286. + n -= equals;
  287. if( dst == NULL || dlen < n )
  288. {
  289. @@ -337,22 +272,24 @@ int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  290. return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  291. }
  292. - for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
  293. - {
  294. + equals = 0;
  295. + for( x = 0, p = dst; i > 0; i--, src++ )
  296. + {
  297. if( *src == '\r' || *src == '\n' || *src == ' ' )
  298. continue;
  299. - dec_map_lookup = mbedtls_base64_table_lookup( base64_dec_map, sizeof( base64_dec_map ), *src );
  300. -
  301. - mbedtls_base64_cond_assign_uint32( &j, j - 1, mbedtls_base64_eq( dec_map_lookup, 64 ) );
  302. - x = ( x << 6 ) | ( dec_map_lookup & 0x3F );
  303. + x = x << 6;
  304. + if( *src == '=' )
  305. + ++equals;
  306. + else
  307. + x |= dec_value( *src );
  308. - if( ++n == 4 )
  309. + if( ++accumulated_digits == 4 )
  310. {
  311. - n = 0;
  312. - if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
  313. - if( j > 1 ) *p++ = (unsigned char)( x >> 8 );
  314. - if( j > 2 ) *p++ = (unsigned char)( x );
  315. + accumulated_digits = 0;
  316. + *p++ = (unsigned char)( x >> 16 );
  317. + if( equals <= 1 ) *p++ = (unsigned char)( x >> 8 );
  318. + if( equals <= 0 ) *p++ = (unsigned char)( x );
  319. }
  320. }