blob: 7baa3d501c802ebdaa10d6b1e9ad063a275e5b8a [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"
Gabor Mezei28d61152021-11-15 16:13:01 +01003#include "constant_time_internal.h"
4#include "constant_time_invasive.h"
Paul Elliott448d5462021-02-24 15:32:42 +00005#include <test/constant_flow.h>
Gilles Peskineba951f52021-08-06 14:55:55 +02006
7#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine93365a72021-08-06 16:54:22 +02008static const char base64_digits[] =
Gilles Peskineba951f52021-08-06 14:55:55 +02009 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
10#endif /* MBEDTLS_TEST_HOOKS */
11
Paul Bakker33b43f12013-08-20 11:48:36 +020012/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000013
Paul Bakker33b43f12013-08-20 11:48:36 +020014/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015 * depends_on:MBEDTLS_BASE64_C
Paul Bakker33b43f12013-08-20 11:48:36 +020016 * END_DEPENDENCIES
17 */
Paul Bakker5690efc2011-05-26 13:16:06 +000018
Gilles Peskinea64417a2021-08-03 12:38:55 +020019/* 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 ) );
Gabor Mezeib8d78922021-11-15 16:03:24 +010028 unsigned char m = mbedtls_ct_uchar_mask_of_range( low, high, c );
Gilles Peskinea64417a2021-08-03 12:38:55 +020029 TEST_CF_PUBLIC( &c, sizeof( c ) );
Gilles Peskine27298782021-08-03 17:41:49 +020030 TEST_CF_PUBLIC( &m, sizeof( m ) );
Gilles Peskinea64417a2021-08-03 12:38:55 +020031 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 */
Gilles Peskineba951f52021-08-06 14:55:55 +020040void enc_chars( )
Gilles Peskinea64417a2021-08-03 12:38:55 +020041{
42 for( unsigned value = 0; value < 64; value++ )
43 {
44 mbedtls_test_set_step( value );
45 TEST_CF_SECRET( &value, sizeof( value ) );
Gabor Mezeib8d78922021-11-15 16:03:24 +010046 unsigned char digit = mbedtls_ct_base64_enc_char( value );
Gilles Peskinea64417a2021-08-03 12:38:55 +020047 TEST_CF_PUBLIC( &value, sizeof( value ) );
48 TEST_CF_PUBLIC( &digit, sizeof( digit ) );
Gilles Peskine93365a72021-08-06 16:54:22 +020049 TEST_EQUAL( digit, base64_digits[value] );
Gilles Peskinea64417a2021-08-03 12:38:55 +020050 }
51}
52/* END_CASE */
53
54/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
Gilles Peskineba951f52021-08-06 14:55:55 +020055void dec_chars( )
Gilles Peskinea64417a2021-08-03 12:38:55 +020056{
57 char *p;
Gilles Peskinea64417a2021-08-03 12:38:55 +020058 signed char expected;
59
60 for( unsigned c = 0; c <= 0xff; c++ )
61 {
62 mbedtls_test_set_step( c );
Gilles Peskine93365a72021-08-06 16:54:22 +020063 /* base64_digits is 0-terminated. sizeof()-1 excludes the trailing 0. */
64 p = memchr( base64_digits, c, sizeof( base64_digits ) - 1 );
Gilles Peskinea64417a2021-08-03 12:38:55 +020065 if( p == NULL )
66 expected = -1;
67 else
Gilles Peskine93365a72021-08-06 16:54:22 +020068 expected = p - base64_digits;
Gilles Peskinea64417a2021-08-03 12:38:55 +020069 TEST_CF_SECRET( &c, sizeof( c ) );
Gabor Mezeib8d78922021-11-15 16:03:24 +010070 signed char actual = mbedtls_ct_base64_dec_value( c );
Gilles Peskinea64417a2021-08-03 12:38:55 +020071 TEST_CF_PUBLIC( &c, sizeof( c ) );
72 TEST_CF_PUBLIC( &actual, sizeof( actual ) );
73 TEST_EQUAL( actual, expected );
74 }
75}
76/* END_CASE */
77
Paul Bakker33b43f12013-08-20 11:48:36 +020078/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010079void mbedtls_base64_encode( char * src_string, char * dst_string,
80 int dst_buf_size, int result )
Paul Bakker367dae42009-06-28 21:50:27 +000081{
82 unsigned char src_str[1000];
83 unsigned char dst_str[1000];
Paul Elliott448d5462021-02-24 15:32:42 +000084 size_t len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000085
86 memset(src_str, 0x00, 1000);
87 memset(dst_str, 0x00, 1000);
88
Paul Bakkerdd0aae92014-04-17 16:06:37 +020089 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Paul Elliott448d5462021-02-24 15:32:42 +000090 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
Paul Elliottc48cb802021-03-02 22:48:40 +000096 /* 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
Paul Bakker33b43f12013-08-20 11:48:36 +0200100 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +0000101 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200102 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +0000103 }
Paul Bakker367dae42009-06-28 21:50:27 +0000104}
Paul Bakker33b43f12013-08-20 11:48:36 +0200105/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000106
Paul Bakker33b43f12013-08-20 11:48:36 +0200107/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100108void mbedtls_base64_decode( char * src_string, char * dst_string, int result )
Paul Bakker367dae42009-06-28 21:50:27 +0000109{
110 unsigned char src_str[1000];
111 unsigned char dst_str[1000];
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100112 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200113 int res;
Paul Bakker367dae42009-06-28 21:50:27 +0000114
115 memset(src_str, 0x00, 1000);
116 memset(dst_str, 0x00, 1000);
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200117
118 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100119 res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
Paul Bakker94b916c2014-04-17 16:07:20 +0200120 TEST_ASSERT( res == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200121 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +0000122 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200123 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +0000124 }
Paul Bakker367dae42009-06-28 21:50:27 +0000125}
Paul Bakker33b43f12013-08-20 11:48:36 +0200126/* END_CASE */
Paul Bakker3d360822009-07-05 11:29:38 +0000127
Paul Bakkerd5983182014-07-04 13:50:31 +0200128/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100129void base64_encode_hex( data_t * src, char * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +0200130 int result )
131{
Azim Khand30ca132017-06-09 04:32:58 +0100132 unsigned char *res = NULL;
133 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200134
Ronald Cron690f3eb2020-06-10 10:42:18 +0200135 res = mbedtls_test_zero_alloc( dst_buf_size );
Paul Bakkerd5983182014-07-04 13:50:31 +0200136
Paul Elliott448d5462021-02-24 15:32:42 +0000137 TEST_CF_SECRET( src->x, src->len );
Azim Khand30ca132017-06-09 04:32:58 +0100138 TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result );
Paul Elliott448d5462021-02-24 15:32:42 +0000139 TEST_CF_PUBLIC( src->x, src->len );
140
Paul Elliottc48cb802021-03-02 22:48:40 +0000141 /* 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
Paul Bakkerd5983182014-07-04 13:50:31 +0200145 if( result == 0 )
146 {
147 TEST_ASSERT( len == strlen( dst ) );
148 TEST_ASSERT( memcmp( dst, res, len ) == 0 );
149 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200150
Paul Bakkerbd51b262014-07-10 15:26:12 +0200151exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +0200153}
154/* END_CASE */
155
156/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100157void base64_decode_hex( char * src, data_t * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +0200158 int result )
159{
Azim Khand30ca132017-06-09 04:32:58 +0100160 unsigned char *res = NULL;
161 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200162
Ronald Cron690f3eb2020-06-10 10:42:18 +0200163 res = mbedtls_test_zero_alloc( dst_buf_size );
Paul Bakkerd5983182014-07-04 13:50:31 +0200164
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100165 TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
Paul Bakkerd5983182014-07-04 13:50:31 +0200166 strlen( src ) ) == result );
167 if( result == 0 )
168 {
Azim Khand30ca132017-06-09 04:32:58 +0100169 TEST_ASSERT( len == dst->len );
170 TEST_ASSERT( memcmp( dst->x, res, len ) == 0 );
Paul Bakkerd5983182014-07-04 13:50:31 +0200171 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200172
Paul Bakkerbd51b262014-07-10 15:26:12 +0200173exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +0200175}
176/* END_CASE */
177
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200178/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100179void base64_decode_hex_src( data_t * src, char * dst_ref, int result )
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200180{
181 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +0100182 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200183
Azim Khand30ca132017-06-09 04:32:58 +0100184 TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result );
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200185 if( result == 0 )
186 {
187 TEST_ASSERT( len == strlen( dst_ref ) );
188 TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
189 }
190
191exit:
Azim Khand30ca132017-06-09 04:32:58 +0100192 ;;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200193}
194/* END_CASE */
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100197void base64_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000198{
Andres AG93012e82016-09-09 09:10:28 +0100199 TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000200}
Paul Bakker33b43f12013-08-20 11:48:36 +0200201/* END_CASE */