blob: 8775c8dfbc705b04aec96bb6148bd2d269248f16 [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"
Gilles Peskinec1776a02021-08-06 14:47:10 +02003#include "base64_invasive.h"
Paul Elliott448d5462021-02-24 15:32:42 +00004#include <test/constant_flow.h>
Gilles Peskineba951f52021-08-06 14:55:55 +02005
6#if defined(MBEDTLS_TEST_HOOKS)
7static const char digits[] =
8 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
9#endif /* MBEDTLS_TEST_HOOKS */
10
Paul Bakker33b43f12013-08-20 11:48:36 +020011/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014 * depends_on:MBEDTLS_BASE64_C
Paul Bakker33b43f12013-08-20 11:48:36 +020015 * END_DEPENDENCIES
16 */
Paul Bakker5690efc2011-05-26 13:16:06 +000017
Gilles Peskinea64417a2021-08-03 12:38:55 +020018/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
19void mask_of_range( int low_arg, int high_arg )
20{
21 unsigned char low = low_arg, high = high_arg;
22 unsigned c;
23 for( c = 0; c <= 0xff; c++ )
24 {
25 mbedtls_test_set_step( c );
26 TEST_CF_SECRET( &c, sizeof( c ) );
27 unsigned char m = mbedtls_base64_mask_of_range( low, high, c );
28 TEST_CF_PUBLIC( &c, sizeof( c ) );
Gilles Peskine27298782021-08-03 17:41:49 +020029 TEST_CF_PUBLIC( &m, sizeof( m ) );
Gilles Peskinea64417a2021-08-03 12:38:55 +020030 if( low <= c && c <= high )
31 TEST_EQUAL( m, 0xff );
32 else
33 TEST_EQUAL( m, 0 );
34 }
35}
36/* END_CASE */
37
38/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
Gilles Peskineba951f52021-08-06 14:55:55 +020039void enc_chars( )
Gilles Peskinea64417a2021-08-03 12:38:55 +020040{
41 for( unsigned value = 0; value < 64; value++ )
42 {
43 mbedtls_test_set_step( value );
44 TEST_CF_SECRET( &value, sizeof( value ) );
45 unsigned char digit = mbedtls_base64_enc_char( value );
46 TEST_CF_PUBLIC( &value, sizeof( value ) );
47 TEST_CF_PUBLIC( &digit, sizeof( digit ) );
Gilles Peskineba951f52021-08-06 14:55:55 +020048 TEST_EQUAL( digit, digits[value] );
Gilles Peskinea64417a2021-08-03 12:38:55 +020049 }
50}
51/* END_CASE */
52
53/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
Gilles Peskineba951f52021-08-06 14:55:55 +020054void dec_chars( )
Gilles Peskinea64417a2021-08-03 12:38:55 +020055{
56 char *p;
Gilles Peskinea64417a2021-08-03 12:38:55 +020057 signed char expected;
58
59 for( unsigned c = 0; c <= 0xff; c++ )
60 {
61 mbedtls_test_set_step( c );
Gilles Peskineba951f52021-08-06 14:55:55 +020062 /* digits is 0-terminated. sizeof()-1 excludes the trailing 0. */
63 p = memchr( digits, c, sizeof( digits ) - 1 );
Gilles Peskinea64417a2021-08-03 12:38:55 +020064 if( p == NULL )
65 expected = -1;
66 else
Gilles Peskineba951f52021-08-06 14:55:55 +020067 expected = p - digits;
Gilles Peskinea64417a2021-08-03 12:38:55 +020068 TEST_CF_SECRET( &c, sizeof( c ) );
69 signed char actual = mbedtls_base64_dec_value( c );
70 TEST_CF_PUBLIC( &c, sizeof( c ) );
71 TEST_CF_PUBLIC( &actual, sizeof( actual ) );
72 TEST_EQUAL( actual, expected );
73 }
74}
75/* END_CASE */
76
Paul Bakker33b43f12013-08-20 11:48:36 +020077/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010078void mbedtls_base64_encode( char * src_string, char * dst_string,
79 int dst_buf_size, int result )
Paul Bakker367dae42009-06-28 21:50:27 +000080{
81 unsigned char src_str[1000];
82 unsigned char dst_str[1000];
Paul Elliott448d5462021-02-24 15:32:42 +000083 size_t len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000084
85 memset(src_str, 0x00, 1000);
86 memset(dst_str, 0x00, 1000);
87
Paul Bakkerdd0aae92014-04-17 16:06:37 +020088 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Paul Elliott448d5462021-02-24 15:32:42 +000089 src_len = strlen( (char *) src_str );
90
91 TEST_CF_SECRET( src_str, sizeof( src_str ) );
92 TEST_ASSERT( mbedtls_base64_encode( dst_str, dst_buf_size, &len, src_str, src_len) == result );
93 TEST_CF_PUBLIC( src_str, sizeof( src_str ) );
94
Paul Elliottc48cb802021-03-02 22:48:40 +000095 /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
96 CF failures by unmarking it. */
97 TEST_CF_PUBLIC( dst_str, len );
98
Paul Bakker33b43f12013-08-20 11:48:36 +020099 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +0000100 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200101 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +0000102 }
Paul Bakker367dae42009-06-28 21:50:27 +0000103}
Paul Bakker33b43f12013-08-20 11:48:36 +0200104/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000105
Paul Bakker33b43f12013-08-20 11:48:36 +0200106/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100107void mbedtls_base64_decode( char * src_string, char * dst_string, int result )
Paul Bakker367dae42009-06-28 21:50:27 +0000108{
109 unsigned char src_str[1000];
110 unsigned char dst_str[1000];
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100111 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200112 int res;
Paul Bakker367dae42009-06-28 21:50:27 +0000113
114 memset(src_str, 0x00, 1000);
115 memset(dst_str, 0x00, 1000);
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200116
117 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100118 res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
Paul Bakker94b916c2014-04-17 16:07:20 +0200119 TEST_ASSERT( res == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200120 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +0000121 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200122 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +0000123 }
Paul Bakker367dae42009-06-28 21:50:27 +0000124}
Paul Bakker33b43f12013-08-20 11:48:36 +0200125/* END_CASE */
Paul Bakker3d360822009-07-05 11:29:38 +0000126
Paul Bakkerd5983182014-07-04 13:50:31 +0200127/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100128void base64_encode_hex( data_t * src, char * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +0200129 int result )
130{
Azim Khand30ca132017-06-09 04:32:58 +0100131 unsigned char *res = NULL;
132 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200133
Ronald Cron690f3eb2020-06-10 10:42:18 +0200134 res = mbedtls_test_zero_alloc( dst_buf_size );
Paul Bakkerd5983182014-07-04 13:50:31 +0200135
Paul Elliott448d5462021-02-24 15:32:42 +0000136 TEST_CF_SECRET( src->x, src->len );
Azim Khand30ca132017-06-09 04:32:58 +0100137 TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result );
Paul Elliott448d5462021-02-24 15:32:42 +0000138 TEST_CF_PUBLIC( src->x, src->len );
139
Paul Elliottc48cb802021-03-02 22:48:40 +0000140 /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
141 CF failures by unmarking it. */
142 TEST_CF_PUBLIC( res, len );
143
Paul Bakkerd5983182014-07-04 13:50:31 +0200144 if( result == 0 )
145 {
146 TEST_ASSERT( len == strlen( dst ) );
147 TEST_ASSERT( memcmp( dst, res, len ) == 0 );
148 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200149
Paul Bakkerbd51b262014-07-10 15:26:12 +0200150exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +0200152}
153/* END_CASE */
154
155/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100156void base64_decode_hex( char * src, data_t * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +0200157 int result )
158{
Azim Khand30ca132017-06-09 04:32:58 +0100159 unsigned char *res = NULL;
160 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200161
Ronald Cron690f3eb2020-06-10 10:42:18 +0200162 res = mbedtls_test_zero_alloc( dst_buf_size );
Paul Bakkerd5983182014-07-04 13:50:31 +0200163
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100164 TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
Paul Bakkerd5983182014-07-04 13:50:31 +0200165 strlen( src ) ) == result );
166 if( result == 0 )
167 {
Azim Khand30ca132017-06-09 04:32:58 +0100168 TEST_ASSERT( len == dst->len );
169 TEST_ASSERT( memcmp( dst->x, res, len ) == 0 );
Paul Bakkerd5983182014-07-04 13:50:31 +0200170 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200171
Paul Bakkerbd51b262014-07-10 15:26:12 +0200172exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +0200174}
175/* END_CASE */
176
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200177/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100178void base64_decode_hex_src( data_t * src, char * dst_ref, int result )
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200179{
180 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +0100181 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200182
Azim Khand30ca132017-06-09 04:32:58 +0100183 TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result );
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200184 if( result == 0 )
185 {
186 TEST_ASSERT( len == strlen( dst_ref ) );
187 TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
188 }
189
190exit:
Azim Khand30ca132017-06-09 04:32:58 +0100191 ;;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200192}
193/* END_CASE */
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100196void base64_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000197{
Andres AG93012e82016-09-09 09:10:28 +0100198 TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000199}
Paul Bakker33b43f12013-08-20 11:48:36 +0200200/* END_CASE */