blob: d0e116770552e3f80a84d4566d555899b4d83811 [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>
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00006
Paul Bakker33b43f12013-08-20 11:48:36 +02007/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008 * depends_on:MBEDTLS_BASE64_C
Paul Bakker33b43f12013-08-20 11:48:36 +02009 * END_DEPENDENCIES
10 */
Paul Bakker5690efc2011-05-26 13:16:06 +000011
Gilles Peskinea64417a2021-08-03 12:38:55 +020012/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
13void mask_of_range( int low_arg, int high_arg )
14{
15 unsigned char low = low_arg, high = high_arg;
16 unsigned c;
17 for( c = 0; c <= 0xff; c++ )
18 {
19 mbedtls_test_set_step( c );
20 TEST_CF_SECRET( &c, sizeof( c ) );
21 unsigned char m = mbedtls_base64_mask_of_range( low, high, c );
22 TEST_CF_PUBLIC( &c, sizeof( c ) );
Gilles Peskine27298782021-08-03 17:41:49 +020023 TEST_CF_PUBLIC( &m, sizeof( m ) );
Gilles Peskinea64417a2021-08-03 12:38:55 +020024 if( low <= c && c <= high )
25 TEST_EQUAL( m, 0xff );
26 else
27 TEST_EQUAL( m, 0 );
28 }
29}
30/* END_CASE */
31
32/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
33void enc_chars( char *chars )
34{
35 for( unsigned value = 0; value < 64; value++ )
36 {
37 mbedtls_test_set_step( value );
38 TEST_CF_SECRET( &value, sizeof( value ) );
39 unsigned char digit = mbedtls_base64_enc_char( value );
40 TEST_CF_PUBLIC( &value, sizeof( value ) );
41 TEST_CF_PUBLIC( &digit, sizeof( digit ) );
42 TEST_EQUAL( digit, chars[value] );
43 }
44}
45/* END_CASE */
46
47/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
48void dec_chars( char *chars )
49{
50 char *p;
51 const size_t chars_len = strlen( chars );
52 signed char expected;
53
54 for( unsigned c = 0; c <= 0xff; c++ )
55 {
56 mbedtls_test_set_step( c );
57 p = memchr( chars, c, chars_len );
58 if( p == NULL )
59 expected = -1;
60 else
61 expected = p - chars;
62 TEST_CF_SECRET( &c, sizeof( c ) );
63 signed char actual = mbedtls_base64_dec_value( c );
64 TEST_CF_PUBLIC( &c, sizeof( c ) );
65 TEST_CF_PUBLIC( &actual, sizeof( actual ) );
66 TEST_EQUAL( actual, expected );
67 }
68}
69/* END_CASE */
70
Paul Bakker33b43f12013-08-20 11:48:36 +020071/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010072void mbedtls_base64_encode( char * src_string, char * dst_string,
73 int dst_buf_size, int result )
Paul Bakker367dae42009-06-28 21:50:27 +000074{
75 unsigned char src_str[1000];
76 unsigned char dst_str[1000];
Paul Elliott448d5462021-02-24 15:32:42 +000077 size_t len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000078
79 memset(src_str, 0x00, 1000);
80 memset(dst_str, 0x00, 1000);
81
Paul Bakkerdd0aae92014-04-17 16:06:37 +020082 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Paul Elliott448d5462021-02-24 15:32:42 +000083 src_len = strlen( (char *) src_str );
84
85 TEST_CF_SECRET( src_str, sizeof( src_str ) );
86 TEST_ASSERT( mbedtls_base64_encode( dst_str, dst_buf_size, &len, src_str, src_len) == result );
87 TEST_CF_PUBLIC( src_str, sizeof( src_str ) );
88
Paul Elliottc48cb802021-03-02 22:48:40 +000089 /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
90 CF failures by unmarking it. */
91 TEST_CF_PUBLIC( dst_str, len );
92
Paul Bakker33b43f12013-08-20 11:48:36 +020093 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +000094 {
Paul Bakker33b43f12013-08-20 11:48:36 +020095 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +000096 }
Paul Bakker367dae42009-06-28 21:50:27 +000097}
Paul Bakker33b43f12013-08-20 11:48:36 +020098/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000099
Paul Bakker33b43f12013-08-20 11:48:36 +0200100/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100101void mbedtls_base64_decode( char * src_string, char * dst_string, int result )
Paul Bakker367dae42009-06-28 21:50:27 +0000102{
103 unsigned char src_str[1000];
104 unsigned char dst_str[1000];
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100105 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200106 int res;
Paul Bakker367dae42009-06-28 21:50:27 +0000107
108 memset(src_str, 0x00, 1000);
109 memset(dst_str, 0x00, 1000);
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200110
111 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100112 res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
Paul Bakker94b916c2014-04-17 16:07:20 +0200113 TEST_ASSERT( res == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200114 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +0000115 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200116 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +0000117 }
Paul Bakker367dae42009-06-28 21:50:27 +0000118}
Paul Bakker33b43f12013-08-20 11:48:36 +0200119/* END_CASE */
Paul Bakker3d360822009-07-05 11:29:38 +0000120
Paul Bakkerd5983182014-07-04 13:50:31 +0200121/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100122void base64_encode_hex( data_t * src, char * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +0200123 int result )
124{
Azim Khand30ca132017-06-09 04:32:58 +0100125 unsigned char *res = NULL;
126 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200127
Ronald Cron690f3eb2020-06-10 10:42:18 +0200128 res = mbedtls_test_zero_alloc( dst_buf_size );
Paul Bakkerd5983182014-07-04 13:50:31 +0200129
Paul Elliott448d5462021-02-24 15:32:42 +0000130 TEST_CF_SECRET( src->x, src->len );
Azim Khand30ca132017-06-09 04:32:58 +0100131 TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result );
Paul Elliott448d5462021-02-24 15:32:42 +0000132 TEST_CF_PUBLIC( src->x, src->len );
133
Paul Elliottc48cb802021-03-02 22:48:40 +0000134 /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
135 CF failures by unmarking it. */
136 TEST_CF_PUBLIC( res, len );
137
Paul Bakkerd5983182014-07-04 13:50:31 +0200138 if( result == 0 )
139 {
140 TEST_ASSERT( len == strlen( dst ) );
141 TEST_ASSERT( memcmp( dst, res, len ) == 0 );
142 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200143
Paul Bakkerbd51b262014-07-10 15:26:12 +0200144exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +0200146}
147/* END_CASE */
148
149/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100150void base64_decode_hex( char * src, data_t * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +0200151 int result )
152{
Azim Khand30ca132017-06-09 04:32:58 +0100153 unsigned char *res = NULL;
154 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200155
Ronald Cron690f3eb2020-06-10 10:42:18 +0200156 res = mbedtls_test_zero_alloc( dst_buf_size );
Paul Bakkerd5983182014-07-04 13:50:31 +0200157
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100158 TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
Paul Bakkerd5983182014-07-04 13:50:31 +0200159 strlen( src ) ) == result );
160 if( result == 0 )
161 {
Azim Khand30ca132017-06-09 04:32:58 +0100162 TEST_ASSERT( len == dst->len );
163 TEST_ASSERT( memcmp( dst->x, res, len ) == 0 );
Paul Bakkerd5983182014-07-04 13:50:31 +0200164 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200165
Paul Bakkerbd51b262014-07-10 15:26:12 +0200166exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +0200168}
169/* END_CASE */
170
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200171/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100172void base64_decode_hex_src( data_t * src, char * dst_ref, int result )
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200173{
174 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +0100175 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200176
Azim Khand30ca132017-06-09 04:32:58 +0100177 TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result );
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200178 if( result == 0 )
179 {
180 TEST_ASSERT( len == strlen( dst_ref ) );
181 TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
182 }
183
184exit:
Azim Khand30ca132017-06-09 04:32:58 +0100185 ;;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200186}
187/* END_CASE */
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100190void base64_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000191{
Andres AG93012e82016-09-09 09:10:28 +0100192 TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000193}
Paul Bakker33b43f12013-08-20 11:48:36 +0200194/* END_CASE */