1/* BEGIN_HEADER */
2#include "mbedtls/base64.h"
3#include "constant_time_internal.h"
4#include "constant_time_invasive.h"
5#include <test/constant_flow.h>
6
7#if defined(MBEDTLS_TEST_HOOKS)
8static const char base64_digits[] =
9    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
10#endif /* MBEDTLS_TEST_HOOKS */
11
12/* END_HEADER */
13
14/* BEGIN_DEPENDENCIES
15 * depends_on:MBEDTLS_BASE64_C
16 * END_DEPENDENCIES
17 */
18
19/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
20void mask_of_range( int low_arg, int high_arg )
21{
22    unsigned char low = low_arg, high = high_arg;
23    unsigned c;
24    for( c = 0; c <= 0xff; c++ )
25    {
26        mbedtls_test_set_step( c );
27        TEST_CF_SECRET( &c, sizeof( c ) );
28        unsigned char m = mbedtls_ct_uchar_mask_of_range( low, high, c );
29        TEST_CF_PUBLIC( &c, sizeof( c ) );
30        TEST_CF_PUBLIC( &m, sizeof( m ) );
31        if( low <= c && c <= high )
32            TEST_EQUAL( m, 0xff );
33        else
34            TEST_EQUAL( m, 0 );
35    }
36}
37/* END_CASE */
38
39/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
40void enc_chars( )
41{
42    for( unsigned value = 0; value < 64; value++ )
43    {
44        mbedtls_test_set_step( value );
45        TEST_CF_SECRET( &value, sizeof( value ) );
46        unsigned char digit = mbedtls_ct_base64_enc_char( value );
47        TEST_CF_PUBLIC( &value, sizeof( value ) );
48        TEST_CF_PUBLIC( &digit, sizeof( digit ) );
49        TEST_EQUAL( digit, base64_digits[value] );
50    }
51}
52/* END_CASE */
53
54/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
55void dec_chars( )
56{
57    char *p;
58    signed char expected;
59
60    for( unsigned c = 0; c <= 0xff; c++ )
61    {
62        mbedtls_test_set_step( c );
63        /* base64_digits is 0-terminated. sizeof()-1 excludes the trailing 0. */
64        p = memchr( base64_digits, c, sizeof( base64_digits ) - 1 );
65        if( p == NULL )
66            expected = -1;
67        else
68            expected = p - base64_digits;
69        TEST_CF_SECRET( &c, sizeof( c ) );
70        signed char actual = mbedtls_ct_base64_dec_value( c );
71        TEST_CF_PUBLIC( &c, sizeof( c ) );
72        TEST_CF_PUBLIC( &actual, sizeof( actual ) );
73        TEST_EQUAL( actual, expected );
74    }
75}
76/* END_CASE */
77
78/* BEGIN_CASE */
79void mbedtls_base64_encode( char * src_string, char * dst_string,
80                            int dst_buf_size, int result )
81{
82    unsigned char src_str[1000];
83    unsigned char dst_str[1000];
84    size_t len, src_len;
85
86    memset(src_str, 0x00, 1000);
87    memset(dst_str, 0x00, 1000);
88
89    strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
90    src_len = strlen( (char *) src_str );
91
92    TEST_CF_SECRET( src_str, sizeof( src_str ) );
93    TEST_ASSERT( mbedtls_base64_encode( dst_str, dst_buf_size, &len, src_str, src_len) == result );
94    TEST_CF_PUBLIC( src_str, sizeof( src_str ) );
95
96    /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
97       CF failures by unmarking it. */
98    TEST_CF_PUBLIC( dst_str, len );
99
100    if( result == 0 )
101    {
102        TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
103    }
104}
105/* END_CASE */
106
107/* BEGIN_CASE */
108void mbedtls_base64_decode( char * src_string, char * dst_string, int result )
109{
110    unsigned char src_str[1000];
111    unsigned char dst_str[1000];
112    size_t len;
113    int res;
114
115    memset(src_str, 0x00, 1000);
116    memset(dst_str, 0x00, 1000);
117
118    strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
119    res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
120    TEST_ASSERT( res == result );
121    if( result == 0 )
122    {
123        TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
124    }
125}
126/* END_CASE */
127
128/* BEGIN_CASE */
129void base64_encode_hex( data_t * src, char * dst, int dst_buf_size,
130                        int result )
131{
132    unsigned char *res = NULL;
133    size_t len;
134
135    res = mbedtls_test_zero_alloc( dst_buf_size );
136
137    TEST_CF_SECRET( src->x, src->len );
138    TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result );
139    TEST_CF_PUBLIC( src->x, src->len );
140
141    /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
142       CF failures by unmarking it. */
143    TEST_CF_PUBLIC( res, len );
144
145    if( result == 0 )
146    {
147        TEST_ASSERT( len == strlen( dst ) );
148        TEST_ASSERT( memcmp( dst, res, len ) == 0 );
149    }
150
151exit:
152    mbedtls_free( res );
153}
154/* END_CASE */
155
156/* BEGIN_CASE */
157void base64_decode_hex( char * src, data_t * dst, int dst_buf_size,
158                        int result )
159{
160    unsigned char *res = NULL;
161    size_t len;
162
163    res = mbedtls_test_zero_alloc( dst_buf_size );
164
165    TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
166                                strlen( src ) ) == result );
167    if( result == 0 )
168    {
169        TEST_ASSERT( len == dst->len );
170        TEST_ASSERT( memcmp( dst->x, res, len ) == 0 );
171    }
172
173exit:
174    mbedtls_free( res );
175}
176/* END_CASE */
177
178/* BEGIN_CASE */
179void base64_decode_hex_src( data_t * src, char * dst_ref, int result )
180{
181    unsigned char dst[1000] = { 0 };
182    size_t len;
183
184    TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result );
185    if( result == 0 )
186    {
187        TEST_ASSERT( len == strlen( dst_ref ) );
188        TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
189    }
190
191exit:
192    ;;
193}
194/* END_CASE */
195
196/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
197void base64_selftest(  )
198{
199    TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
200}
201/* END_CASE */
202