Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/base64.h" |
Paul Elliott | 448d546 | 2021-02-24 15:32:42 +0000 | [diff] [blame] | 3 | #include <test/constant_flow.h> |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 4 | /* END_HEADER */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 5 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 6 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7 | * depends_on:MBEDTLS_BASE64_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 8 | * END_DEPENDENCIES |
| 9 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 10 | |
Gilles Peskine | a64417a | 2021-08-03 12:38:55 +0200 | [diff] [blame] | 11 | /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */ |
| 12 | void 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 ) ); |
Gilles Peskine | 2729878 | 2021-08-03 17:41:49 +0200 | [diff] [blame] | 22 | TEST_CF_PUBLIC( &m, sizeof( m ) ); |
Gilles Peskine | a64417a | 2021-08-03 12:38:55 +0200 | [diff] [blame] | 23 | if( low <= c && c <= high ) |
| 24 | TEST_EQUAL( m, 0xff ); |
| 25 | else |
| 26 | TEST_EQUAL( m, 0 ); |
| 27 | } |
| 28 | } |
| 29 | /* END_CASE */ |
| 30 | |
| 31 | /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */ |
| 32 | void enc_chars( char *chars ) |
| 33 | { |
| 34 | for( unsigned value = 0; value < 64; value++ ) |
| 35 | { |
| 36 | mbedtls_test_set_step( value ); |
| 37 | TEST_CF_SECRET( &value, sizeof( value ) ); |
| 38 | unsigned char digit = mbedtls_base64_enc_char( value ); |
| 39 | TEST_CF_PUBLIC( &value, sizeof( value ) ); |
| 40 | TEST_CF_PUBLIC( &digit, sizeof( digit ) ); |
| 41 | TEST_EQUAL( digit, chars[value] ); |
| 42 | } |
| 43 | } |
| 44 | /* END_CASE */ |
| 45 | |
| 46 | /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */ |
| 47 | void dec_chars( char *chars ) |
| 48 | { |
| 49 | char *p; |
| 50 | const size_t chars_len = strlen( chars ); |
| 51 | signed char expected; |
| 52 | |
| 53 | for( unsigned c = 0; c <= 0xff; c++ ) |
| 54 | { |
| 55 | mbedtls_test_set_step( c ); |
| 56 | p = memchr( chars, c, chars_len ); |
| 57 | if( p == NULL ) |
| 58 | expected = -1; |
| 59 | else |
| 60 | expected = p - chars; |
| 61 | TEST_CF_SECRET( &c, sizeof( c ) ); |
| 62 | signed char actual = mbedtls_base64_dec_value( c ); |
| 63 | TEST_CF_PUBLIC( &c, sizeof( c ) ); |
| 64 | TEST_CF_PUBLIC( &actual, sizeof( actual ) ); |
| 65 | TEST_EQUAL( actual, expected ); |
| 66 | } |
| 67 | } |
| 68 | /* END_CASE */ |
| 69 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 70 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 71 | void mbedtls_base64_encode( char * src_string, char * dst_string, |
| 72 | int dst_buf_size, int result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 73 | { |
| 74 | unsigned char src_str[1000]; |
| 75 | unsigned char dst_str[1000]; |
Paul Elliott | 448d546 | 2021-02-24 15:32:42 +0000 | [diff] [blame] | 76 | size_t len, src_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 77 | |
| 78 | memset(src_str, 0x00, 1000); |
| 79 | memset(dst_str, 0x00, 1000); |
| 80 | |
Paul Bakker | dd0aae9 | 2014-04-17 16:06:37 +0200 | [diff] [blame] | 81 | strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 ); |
Paul Elliott | 448d546 | 2021-02-24 15:32:42 +0000 | [diff] [blame] | 82 | src_len = strlen( (char *) src_str ); |
| 83 | |
| 84 | TEST_CF_SECRET( src_str, sizeof( src_str ) ); |
| 85 | TEST_ASSERT( mbedtls_base64_encode( dst_str, dst_buf_size, &len, src_str, src_len) == result ); |
| 86 | TEST_CF_PUBLIC( src_str, sizeof( src_str ) ); |
| 87 | |
Paul Elliott | c48cb80 | 2021-03-02 22:48:40 +0000 | [diff] [blame] | 88 | /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering |
| 89 | CF failures by unmarking it. */ |
| 90 | TEST_CF_PUBLIC( dst_str, len ); |
| 91 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 92 | if( result == 0 ) |
Paul Bakker | 5946fd9 | 2009-07-11 15:29:30 +0000 | [diff] [blame] | 93 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 94 | TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 ); |
Paul Bakker | 5946fd9 | 2009-07-11 15:29:30 +0000 | [diff] [blame] | 95 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 96 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 97 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 98 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 99 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 100 | void mbedtls_base64_decode( char * src_string, char * dst_string, int result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 101 | { |
| 102 | unsigned char src_str[1000]; |
| 103 | unsigned char dst_str[1000]; |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 104 | size_t len; |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 105 | int res; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 106 | |
| 107 | memset(src_str, 0x00, 1000); |
| 108 | memset(dst_str, 0x00, 1000); |
Paul Bakker | dd0aae9 | 2014-04-17 16:06:37 +0200 | [diff] [blame] | 109 | |
| 110 | strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 ); |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 111 | res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) ); |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 112 | TEST_ASSERT( res == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 113 | if( result == 0 ) |
Paul Bakker | 5946fd9 | 2009-07-11 15:29:30 +0000 | [diff] [blame] | 114 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 115 | TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 ); |
Paul Bakker | 5946fd9 | 2009-07-11 15:29:30 +0000 | [diff] [blame] | 116 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 117 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 118 | /* END_CASE */ |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 119 | |
Paul Bakker | d598318 | 2014-07-04 13:50:31 +0200 | [diff] [blame] | 120 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 121 | void base64_encode_hex( data_t * src, char * dst, int dst_buf_size, |
Paul Bakker | d598318 | 2014-07-04 13:50:31 +0200 | [diff] [blame] | 122 | int result ) |
| 123 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 124 | unsigned char *res = NULL; |
| 125 | size_t len; |
Paul Bakker | d598318 | 2014-07-04 13:50:31 +0200 | [diff] [blame] | 126 | |
Ronald Cron | 690f3eb | 2020-06-10 10:42:18 +0200 | [diff] [blame] | 127 | res = mbedtls_test_zero_alloc( dst_buf_size ); |
Paul Bakker | d598318 | 2014-07-04 13:50:31 +0200 | [diff] [blame] | 128 | |
Paul Elliott | 448d546 | 2021-02-24 15:32:42 +0000 | [diff] [blame] | 129 | TEST_CF_SECRET( src->x, src->len ); |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 130 | TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result ); |
Paul Elliott | 448d546 | 2021-02-24 15:32:42 +0000 | [diff] [blame] | 131 | TEST_CF_PUBLIC( src->x, src->len ); |
| 132 | |
Paul Elliott | c48cb80 | 2021-03-02 22:48:40 +0000 | [diff] [blame] | 133 | /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering |
| 134 | CF failures by unmarking it. */ |
| 135 | TEST_CF_PUBLIC( res, len ); |
| 136 | |
Paul Bakker | d598318 | 2014-07-04 13:50:31 +0200 | [diff] [blame] | 137 | if( result == 0 ) |
| 138 | { |
| 139 | TEST_ASSERT( len == strlen( dst ) ); |
| 140 | TEST_ASSERT( memcmp( dst, res, len ) == 0 ); |
| 141 | } |
Paul Bakker | 6697b6c | 2014-07-04 18:35:50 +0200 | [diff] [blame] | 142 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 143 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | mbedtls_free( res ); |
Paul Bakker | d598318 | 2014-07-04 13:50:31 +0200 | [diff] [blame] | 145 | } |
| 146 | /* END_CASE */ |
| 147 | |
| 148 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 149 | void base64_decode_hex( char * src, data_t * dst, int dst_buf_size, |
Paul Bakker | d598318 | 2014-07-04 13:50:31 +0200 | [diff] [blame] | 150 | int result ) |
| 151 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 152 | unsigned char *res = NULL; |
| 153 | size_t len; |
Paul Bakker | d598318 | 2014-07-04 13:50:31 +0200 | [diff] [blame] | 154 | |
Ronald Cron | 690f3eb | 2020-06-10 10:42:18 +0200 | [diff] [blame] | 155 | res = mbedtls_test_zero_alloc( dst_buf_size ); |
Paul Bakker | d598318 | 2014-07-04 13:50:31 +0200 | [diff] [blame] | 156 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 157 | TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src, |
Paul Bakker | d598318 | 2014-07-04 13:50:31 +0200 | [diff] [blame] | 158 | strlen( src ) ) == result ); |
| 159 | if( result == 0 ) |
| 160 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 161 | TEST_ASSERT( len == dst->len ); |
| 162 | TEST_ASSERT( memcmp( dst->x, res, len ) == 0 ); |
Paul Bakker | d598318 | 2014-07-04 13:50:31 +0200 | [diff] [blame] | 163 | } |
Paul Bakker | 6697b6c | 2014-07-04 18:35:50 +0200 | [diff] [blame] | 164 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 165 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | mbedtls_free( res ); |
Paul Bakker | d598318 | 2014-07-04 13:50:31 +0200 | [diff] [blame] | 167 | } |
| 168 | /* END_CASE */ |
| 169 | |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 170 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 171 | void base64_decode_hex_src( data_t * src, char * dst_ref, int result ) |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 172 | { |
| 173 | unsigned char dst[1000] = { 0 }; |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 174 | size_t len; |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 175 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 176 | TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result ); |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 177 | if( result == 0 ) |
| 178 | { |
| 179 | TEST_ASSERT( len == strlen( dst_ref ) ); |
| 180 | TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 ); |
| 181 | } |
| 182 | |
| 183 | exit: |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 184 | ;; |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 185 | } |
| 186 | /* END_CASE */ |
| 187 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 189 | void base64_selftest( ) |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 190 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 191 | TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 ); |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 192 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 193 | /* END_CASE */ |