blob: 89d7995116ec24416792c93d52a9c7b16d7997be [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/base64.h"
Paul Elliott448d5462021-02-24 15:32:42 +00003#include <test/constant_flow.h>
Paul Bakker33b43f12013-08-20 11:48:36 +02004/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00005
Paul Bakker33b43f12013-08-20 11:48:36 +02006/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007 * depends_on:MBEDTLS_BASE64_C
Paul Bakker33b43f12013-08-20 11:48:36 +02008 * END_DEPENDENCIES
9 */
Paul Bakker5690efc2011-05-26 13:16:06 +000010
Gilles Peskinea64417a2021-08-03 12:38:55 +020011/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
12void mask_of_range( int low_arg, int high_arg )
13{
14 unsigned char low = low_arg, high = high_arg;
15 unsigned c;
16 for( c = 0; c <= 0xff; c++ )
17 {
18 mbedtls_test_set_step( c );
19 TEST_CF_SECRET( &c, sizeof( c ) );
20 unsigned char m = mbedtls_base64_mask_of_range( low, high, c );
21 TEST_CF_PUBLIC( &c, sizeof( c ) );
22 if( low <= c && c <= high )
23 TEST_EQUAL( m, 0xff );
24 else
25 TEST_EQUAL( m, 0 );
26 }
27}
28/* END_CASE */
29
30/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
31void enc_chars( char *chars )
32{
33 for( unsigned value = 0; value < 64; value++ )
34 {
35 mbedtls_test_set_step( value );
36 TEST_CF_SECRET( &value, sizeof( value ) );
37 unsigned char digit = mbedtls_base64_enc_char( value );
38 TEST_CF_PUBLIC( &value, sizeof( value ) );
39 TEST_CF_PUBLIC( &digit, sizeof( digit ) );
40 TEST_EQUAL( digit, chars[value] );
41 }
42}
43/* END_CASE */
44
45/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
46void dec_chars( char *chars )
47{
48 char *p;
49 const size_t chars_len = strlen( chars );
50 signed char expected;
51
52 for( unsigned c = 0; c <= 0xff; c++ )
53 {
54 mbedtls_test_set_step( c );
55 p = memchr( chars, c, chars_len );
56 if( p == NULL )
57 expected = -1;
58 else
59 expected = p - chars;
60 TEST_CF_SECRET( &c, sizeof( c ) );
61 signed char actual = mbedtls_base64_dec_value( c );
62 TEST_CF_PUBLIC( &c, sizeof( c ) );
63 TEST_CF_PUBLIC( &actual, sizeof( actual ) );
64 TEST_EQUAL( actual, expected );
65 }
66}
67/* END_CASE */
68
Paul Bakker33b43f12013-08-20 11:48:36 +020069/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010070void mbedtls_base64_encode( char * src_string, char * dst_string,
71 int dst_buf_size, int result )
Paul Bakker367dae42009-06-28 21:50:27 +000072{
73 unsigned char src_str[1000];
74 unsigned char dst_str[1000];
Paul Elliott448d5462021-02-24 15:32:42 +000075 size_t len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000076
77 memset(src_str, 0x00, 1000);
78 memset(dst_str, 0x00, 1000);
79
Paul Bakkerdd0aae92014-04-17 16:06:37 +020080 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Paul Elliott448d5462021-02-24 15:32:42 +000081 src_len = strlen( (char *) src_str );
82
83 TEST_CF_SECRET( src_str, sizeof( src_str ) );
84 TEST_ASSERT( mbedtls_base64_encode( dst_str, dst_buf_size, &len, src_str, src_len) == result );
85 TEST_CF_PUBLIC( src_str, sizeof( src_str ) );
86
Paul Elliottc48cb802021-03-02 22:48:40 +000087 /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
88 CF failures by unmarking it. */
89 TEST_CF_PUBLIC( dst_str, len );
90
Paul Bakker33b43f12013-08-20 11:48:36 +020091 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +000092 {
Paul Bakker33b43f12013-08-20 11:48:36 +020093 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +000094 }
Paul Bakker367dae42009-06-28 21:50:27 +000095}
Paul Bakker33b43f12013-08-20 11:48:36 +020096/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000097
Paul Bakker33b43f12013-08-20 11:48:36 +020098/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010099void mbedtls_base64_decode( char * src_string, char * dst_string, int result )
Paul Bakker367dae42009-06-28 21:50:27 +0000100{
101 unsigned char src_str[1000];
102 unsigned char dst_str[1000];
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100103 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200104 int res;
Paul Bakker367dae42009-06-28 21:50:27 +0000105
106 memset(src_str, 0x00, 1000);
107 memset(dst_str, 0x00, 1000);
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200108
109 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100110 res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
Paul Bakker94b916c2014-04-17 16:07:20 +0200111 TEST_ASSERT( res == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200112 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +0000113 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200114 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +0000115 }
Paul Bakker367dae42009-06-28 21:50:27 +0000116}
Paul Bakker33b43f12013-08-20 11:48:36 +0200117/* END_CASE */
Paul Bakker3d360822009-07-05 11:29:38 +0000118
Paul Bakkerd5983182014-07-04 13:50:31 +0200119/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100120void base64_encode_hex( data_t * src, char * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +0200121 int result )
122{
Azim Khand30ca132017-06-09 04:32:58 +0100123 unsigned char *res = NULL;
124 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200125
Ronald Cron690f3eb2020-06-10 10:42:18 +0200126 res = mbedtls_test_zero_alloc( dst_buf_size );
Paul Bakkerd5983182014-07-04 13:50:31 +0200127
Paul Elliott448d5462021-02-24 15:32:42 +0000128 TEST_CF_SECRET( src->x, src->len );
Azim Khand30ca132017-06-09 04:32:58 +0100129 TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result );
Paul Elliott448d5462021-02-24 15:32:42 +0000130 TEST_CF_PUBLIC( src->x, src->len );
131
Paul Elliottc48cb802021-03-02 22:48:40 +0000132 /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
133 CF failures by unmarking it. */
134 TEST_CF_PUBLIC( res, len );
135
Paul Bakkerd5983182014-07-04 13:50:31 +0200136 if( result == 0 )
137 {
138 TEST_ASSERT( len == strlen( dst ) );
139 TEST_ASSERT( memcmp( dst, res, len ) == 0 );
140 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200141
Paul Bakkerbd51b262014-07-10 15:26:12 +0200142exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +0200144}
145/* END_CASE */
146
147/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100148void base64_decode_hex( char * src, data_t * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +0200149 int result )
150{
Azim Khand30ca132017-06-09 04:32:58 +0100151 unsigned char *res = NULL;
152 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200153
Ronald Cron690f3eb2020-06-10 10:42:18 +0200154 res = mbedtls_test_zero_alloc( dst_buf_size );
Paul Bakkerd5983182014-07-04 13:50:31 +0200155
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100156 TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
Paul Bakkerd5983182014-07-04 13:50:31 +0200157 strlen( src ) ) == result );
158 if( result == 0 )
159 {
Azim Khand30ca132017-06-09 04:32:58 +0100160 TEST_ASSERT( len == dst->len );
161 TEST_ASSERT( memcmp( dst->x, res, len ) == 0 );
Paul Bakkerd5983182014-07-04 13:50:31 +0200162 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200163
Paul Bakkerbd51b262014-07-10 15:26:12 +0200164exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +0200166}
167/* END_CASE */
168
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200169/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100170void base64_decode_hex_src( data_t * src, char * dst_ref, int result )
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200171{
172 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +0100173 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200174
Azim Khand30ca132017-06-09 04:32:58 +0100175 TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result );
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200176 if( result == 0 )
177 {
178 TEST_ASSERT( len == strlen( dst_ref ) );
179 TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
180 }
181
182exit:
Azim Khand30ca132017-06-09 04:32:58 +0100183 ;;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200184}
185/* END_CASE */
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100188void base64_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000189{
Andres AG93012e82016-09-09 09:10:28 +0100190 TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000191}
Paul Bakker33b43f12013-08-20 11:48:36 +0200192/* END_CASE */