Merge pull request #4835 from gilles-peskine-arm/base64-no-table-2.2x

Backport 2.2x: range-based constant-flow base64
This commit is contained in:
Manuel Pégourié-Gonnard
2021-10-27 12:18:29 +02:00
committed by GitHub
14 changed files with 674 additions and 164 deletions

View File

@@ -1,3 +1,33 @@
mask_of_range empty (1..0)
mask_of_range:1:0
mask_of_range empty (255..0)
mask_of_range:255:0
mask_of_range empty (42..7)
mask_of_range:42:7
mask_of_range 0..0
mask_of_range:0:0
mask_of_range 42..42
mask_of_range:42:42
mask_of_range 255..255
mask_of_range:255:255
mask_of_range 0..255
mask_of_range:0:255
mask_of_range 'A'..'Z'
mask_of_range:65:90
enc_char (all digits)
enc_chars:
dec_value (all characters)
dec_chars:
Test case mbedtls_base64_encode #1 buffer just right
mbedtls_base64_encode:"":"":0:0

View File

@@ -1,6 +1,13 @@
/* BEGIN_HEADER */
#include "mbedtls/base64.h"
#include "base64_invasive.h"
#include <test/constant_flow.h>
#if defined(MBEDTLS_TEST_HOOKS)
static const char base64_digits[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#endif /* MBEDTLS_TEST_HOOKS */
/* END_HEADER */
/* BEGIN_DEPENDENCIES
@@ -8,6 +15,65 @@
* END_DEPENDENCIES
*/
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
void mask_of_range( int low_arg, int high_arg )
{
unsigned char low = low_arg, high = high_arg;
unsigned c;
for( c = 0; c <= 0xff; c++ )
{
mbedtls_test_set_step( c );
TEST_CF_SECRET( &c, sizeof( c ) );
unsigned char m = mbedtls_base64_mask_of_range( low, high, c );
TEST_CF_PUBLIC( &c, sizeof( c ) );
TEST_CF_PUBLIC( &m, sizeof( m ) );
if( low <= c && c <= high )
TEST_EQUAL( m, 0xff );
else
TEST_EQUAL( m, 0 );
}
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
void enc_chars( )
{
for( unsigned value = 0; value < 64; value++ )
{
mbedtls_test_set_step( value );
TEST_CF_SECRET( &value, sizeof( value ) );
unsigned char digit = mbedtls_base64_enc_char( value );
TEST_CF_PUBLIC( &value, sizeof( value ) );
TEST_CF_PUBLIC( &digit, sizeof( digit ) );
TEST_EQUAL( digit, base64_digits[value] );
}
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
void dec_chars( )
{
char *p;
signed char expected;
for( unsigned c = 0; c <= 0xff; c++ )
{
mbedtls_test_set_step( c );
/* base64_digits is 0-terminated. sizeof()-1 excludes the trailing 0. */
p = memchr( base64_digits, c, sizeof( base64_digits ) - 1 );
if( p == NULL )
expected = -1;
else
expected = p - base64_digits;
TEST_CF_SECRET( &c, sizeof( c ) );
signed char actual = mbedtls_base64_dec_value( c );
TEST_CF_PUBLIC( &c, sizeof( c ) );
TEST_CF_PUBLIC( &actual, sizeof( actual ) );
TEST_EQUAL( actual, expected );
}
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_base64_encode( char * src_string, char * dst_string,
int dst_buf_size, int result )