blob: 89982047fe50ec5e537a904c0be64ca7bbf5b82b [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"
Gabor Mezei28d61152021-11-15 16:13:01 +01004#include "constant_time_internal.h"
5#include "constant_time_invasive.h"
Paul Elliott448d5462021-02-24 15:32:42 +00006#include <test/constant_flow.h>
Gilles Peskineba951f52021-08-06 14:55:55 +02007
8#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine93365a72021-08-06 16:54:22 +02009static const char base64_digits[] =
Gilles Peskineba951f52021-08-06 14:55:55 +020010 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
11#endif /* MBEDTLS_TEST_HOOKS */
12
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000014
Paul Bakker33b43f12013-08-20 11:48:36 +020015/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016 * depends_on:MBEDTLS_BASE64_C
Paul Bakker33b43f12013-08-20 11:48:36 +020017 * END_DEPENDENCIES
18 */
Paul Bakker5690efc2011-05-26 13:16:06 +000019
Gilles Peskinea64417a2021-08-03 12:38:55 +020020/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
21void mask_of_range( int low_arg, int high_arg )
22{
23 unsigned char low = low_arg, high = high_arg;
24 unsigned c;
25 for( c = 0; c <= 0xff; c++ )
26 {
27 mbedtls_test_set_step( c );
28 TEST_CF_SECRET( &c, sizeof( c ) );
Gabor Mezeib8d78922021-11-15 16:03:24 +010029 unsigned char m = mbedtls_ct_uchar_mask_of_range( low, high, c );
Gilles Peskinea64417a2021-08-03 12:38:55 +020030 TEST_CF_PUBLIC( &c, sizeof( c ) );
Gilles Peskine27298782021-08-03 17:41:49 +020031 TEST_CF_PUBLIC( &m, sizeof( m ) );
Gilles Peskinea64417a2021-08-03 12:38:55 +020032 if( low <= c && c <= high )
33 TEST_EQUAL( m, 0xff );
34 else
35 TEST_EQUAL( m, 0 );
36 }
37}
38/* END_CASE */
39
40/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
Gilles Peskineba951f52021-08-06 14:55:55 +020041void enc_chars( )
Gilles Peskinea64417a2021-08-03 12:38:55 +020042{
43 for( unsigned value = 0; value < 64; value++ )
44 {
45 mbedtls_test_set_step( value );
46 TEST_CF_SECRET( &value, sizeof( value ) );
Gabor Mezeib8d78922021-11-15 16:03:24 +010047 unsigned char digit = mbedtls_ct_base64_enc_char( value );
Gilles Peskinea64417a2021-08-03 12:38:55 +020048 TEST_CF_PUBLIC( &value, sizeof( value ) );
49 TEST_CF_PUBLIC( &digit, sizeof( digit ) );
Gilles Peskine93365a72021-08-06 16:54:22 +020050 TEST_EQUAL( digit, base64_digits[value] );
Gilles Peskinea64417a2021-08-03 12:38:55 +020051 }
52}
53/* END_CASE */
54
55/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
Gilles Peskineba951f52021-08-06 14:55:55 +020056void dec_chars( )
Gilles Peskinea64417a2021-08-03 12:38:55 +020057{
58 char *p;
Gilles Peskinea64417a2021-08-03 12:38:55 +020059 signed char expected;
60
61 for( unsigned c = 0; c <= 0xff; c++ )
62 {
63 mbedtls_test_set_step( c );
Gilles Peskine93365a72021-08-06 16:54:22 +020064 /* base64_digits is 0-terminated. sizeof()-1 excludes the trailing 0. */
65 p = memchr( base64_digits, c, sizeof( base64_digits ) - 1 );
Gilles Peskinea64417a2021-08-03 12:38:55 +020066 if( p == NULL )
67 expected = -1;
68 else
Gilles Peskine93365a72021-08-06 16:54:22 +020069 expected = p - base64_digits;
Gilles Peskinea64417a2021-08-03 12:38:55 +020070 TEST_CF_SECRET( &c, sizeof( c ) );
Gabor Mezeib8d78922021-11-15 16:03:24 +010071 signed char actual = mbedtls_ct_base64_dec_value( c );
Gilles Peskinea64417a2021-08-03 12:38:55 +020072 TEST_CF_PUBLIC( &c, sizeof( c ) );
73 TEST_CF_PUBLIC( &actual, sizeof( actual ) );
74 TEST_EQUAL( actual, expected );
75 }
76}
77/* END_CASE */
78
Paul Bakker33b43f12013-08-20 11:48:36 +020079/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010080void mbedtls_base64_encode( char * src_string, char * dst_string,
81 int dst_buf_size, int result )
Paul Bakker367dae42009-06-28 21:50:27 +000082{
83 unsigned char src_str[1000];
84 unsigned char dst_str[1000];
Paul Elliott448d5462021-02-24 15:32:42 +000085 size_t len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000086
87 memset(src_str, 0x00, 1000);
88 memset(dst_str, 0x00, 1000);
89
Paul Bakkerdd0aae92014-04-17 16:06:37 +020090 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Paul Elliott448d5462021-02-24 15:32:42 +000091 src_len = strlen( (char *) src_str );
92
93 TEST_CF_SECRET( src_str, sizeof( src_str ) );
94 TEST_ASSERT( mbedtls_base64_encode( dst_str, dst_buf_size, &len, src_str, src_len) == result );
95 TEST_CF_PUBLIC( src_str, sizeof( src_str ) );
96
Paul Elliottc48cb802021-03-02 22:48:40 +000097 /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
98 CF failures by unmarking it. */
99 TEST_CF_PUBLIC( dst_str, len );
100
Paul Bakker33b43f12013-08-20 11:48:36 +0200101 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +0000102 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200103 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +0000104 }
Paul Bakker367dae42009-06-28 21:50:27 +0000105}
Paul Bakker33b43f12013-08-20 11:48:36 +0200106/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000107
Paul Bakker33b43f12013-08-20 11:48:36 +0200108/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100109void mbedtls_base64_decode( char * src_string, char * dst_string, int result )
Paul Bakker367dae42009-06-28 21:50:27 +0000110{
111 unsigned char src_str[1000];
112 unsigned char dst_str[1000];
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100113 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200114 int res;
Paul Bakker367dae42009-06-28 21:50:27 +0000115
116 memset(src_str, 0x00, 1000);
117 memset(dst_str, 0x00, 1000);
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200118
119 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100120 res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
Paul Bakker94b916c2014-04-17 16:07:20 +0200121 TEST_ASSERT( res == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200122 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +0000123 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200124 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +0000125 }
Paul Bakker367dae42009-06-28 21:50:27 +0000126}
Paul Bakker33b43f12013-08-20 11:48:36 +0200127/* END_CASE */
Paul Bakker3d360822009-07-05 11:29:38 +0000128
Paul Bakkerd5983182014-07-04 13:50:31 +0200129/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100130void base64_encode_hex( data_t * src, char * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +0200131 int result )
132{
Azim Khand30ca132017-06-09 04:32:58 +0100133 unsigned char *res = NULL;
134 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200135
Ronald Cron690f3eb2020-06-10 10:42:18 +0200136 res = mbedtls_test_zero_alloc( dst_buf_size );
Paul Bakkerd5983182014-07-04 13:50:31 +0200137
Paul Elliott448d5462021-02-24 15:32:42 +0000138 TEST_CF_SECRET( src->x, src->len );
Azim Khand30ca132017-06-09 04:32:58 +0100139 TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result );
Paul Elliott448d5462021-02-24 15:32:42 +0000140 TEST_CF_PUBLIC( src->x, src->len );
141
Paul Elliottc48cb802021-03-02 22:48:40 +0000142 /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
143 CF failures by unmarking it. */
144 TEST_CF_PUBLIC( res, len );
145
Paul Bakkerd5983182014-07-04 13:50:31 +0200146 if( result == 0 )
147 {
148 TEST_ASSERT( len == strlen( dst ) );
149 TEST_ASSERT( memcmp( dst, res, len ) == 0 );
150 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200151
Paul Bakkerbd51b262014-07-10 15:26:12 +0200152exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +0200154}
155/* END_CASE */
156
157/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100158void base64_decode_hex( char * src, data_t * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +0200159 int result )
160{
Azim Khand30ca132017-06-09 04:32:58 +0100161 unsigned char *res = NULL;
162 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +0200163
Ronald Cron690f3eb2020-06-10 10:42:18 +0200164 res = mbedtls_test_zero_alloc( dst_buf_size );
Paul Bakkerd5983182014-07-04 13:50:31 +0200165
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100166 TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
Paul Bakkerd5983182014-07-04 13:50:31 +0200167 strlen( src ) ) == result );
168 if( result == 0 )
169 {
Azim Khand30ca132017-06-09 04:32:58 +0100170 TEST_ASSERT( len == dst->len );
171 TEST_ASSERT( memcmp( dst->x, res, len ) == 0 );
Paul Bakkerd5983182014-07-04 13:50:31 +0200172 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200173
Paul Bakkerbd51b262014-07-10 15:26:12 +0200174exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +0200176}
177/* END_CASE */
178
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200179/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100180void base64_decode_hex_src( data_t * src, char * dst_ref, int result )
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200181{
182 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +0100183 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200184
Azim Khand30ca132017-06-09 04:32:58 +0100185 TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result );
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200186 if( result == 0 )
187 {
188 TEST_ASSERT( len == strlen( dst_ref ) );
189 TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
190 }
191
192exit:
Azim Khand30ca132017-06-09 04:32:58 +0100193 ;;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200194}
195/* END_CASE */
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100198void base64_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000199{
Andres AG93012e82016-09-09 09:10:28 +0100200 TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000201}
Paul Bakker33b43f12013-08-20 11:48:36 +0200202/* END_CASE */