blob: 0185f3b2435224162a1b1410f427ad28749a997b [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"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_BASE64_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010011void mbedtls_base64_encode( char * src_string, char * dst_string,
12 int dst_buf_size, int result )
Paul Bakker367dae42009-06-28 21:50:27 +000013{
14 unsigned char src_str[1000];
15 unsigned char dst_str[1000];
Paul Elliott69b904b2021-02-24 15:32:42 +000016 size_t len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000017
18 memset(src_str, 0x00, 1000);
19 memset(dst_str, 0x00, 1000);
20
Paul Bakkerdd0aae92014-04-17 16:06:37 +020021 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Paul Elliott69b904b2021-02-24 15:32:42 +000022 src_len = strlen( (char *) src_str );
23
24 TEST_CF_SECRET( src_str, sizeof( src_str ) );
25 TEST_ASSERT( mbedtls_base64_encode( dst_str, dst_buf_size, &len, src_str, src_len) == result );
26 TEST_CF_PUBLIC( src_str, sizeof( src_str ) );
27
Paul Elliott4c717072021-03-02 22:48:40 +000028 /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
29 CF failures by unmarking it. */
30 TEST_CF_PUBLIC( dst_str, len );
31
Paul Bakker33b43f12013-08-20 11:48:36 +020032 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +000033 {
Paul Bakker33b43f12013-08-20 11:48:36 +020034 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +000035 }
Paul Bakker367dae42009-06-28 21:50:27 +000036}
Paul Bakker33b43f12013-08-20 11:48:36 +020037/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000038
Paul Bakker33b43f12013-08-20 11:48:36 +020039/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010040void mbedtls_base64_decode( char * src_string, char * dst_string, int result )
Paul Bakker367dae42009-06-28 21:50:27 +000041{
42 unsigned char src_str[1000];
43 unsigned char dst_str[1000];
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010044 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +020045 int res;
Paul Bakker367dae42009-06-28 21:50:27 +000046
47 memset(src_str, 0x00, 1000);
48 memset(dst_str, 0x00, 1000);
Paul Bakkerdd0aae92014-04-17 16:06:37 +020049
50 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010051 res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
Paul Bakker94b916c2014-04-17 16:07:20 +020052 TEST_ASSERT( res == result );
Paul Bakker33b43f12013-08-20 11:48:36 +020053 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +000054 {
Paul Bakker33b43f12013-08-20 11:48:36 +020055 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +000056 }
Paul Bakker367dae42009-06-28 21:50:27 +000057}
Paul Bakker33b43f12013-08-20 11:48:36 +020058/* END_CASE */
Paul Bakker3d360822009-07-05 11:29:38 +000059
Paul Bakkerd5983182014-07-04 13:50:31 +020060/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010061void base64_encode_hex( data_t * src, char * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +020062 int result )
63{
Azim Khand30ca132017-06-09 04:32:58 +010064 unsigned char *res = NULL;
65 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +020066
Paul Bakkerd5983182014-07-04 13:50:31 +020067 res = zero_alloc( dst_buf_size );
68
Paul Elliott69b904b2021-02-24 15:32:42 +000069 TEST_CF_SECRET( src->x, src->len );
Azim Khand30ca132017-06-09 04:32:58 +010070 TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result );
Paul Elliott69b904b2021-02-24 15:32:42 +000071 TEST_CF_PUBLIC( src->x, src->len );
72
Paul Elliott4c717072021-03-02 22:48:40 +000073 /* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
74 CF failures by unmarking it. */
75 TEST_CF_PUBLIC( res, len );
76
Paul Bakkerd5983182014-07-04 13:50:31 +020077 if( result == 0 )
78 {
79 TEST_ASSERT( len == strlen( dst ) );
80 TEST_ASSERT( memcmp( dst, res, len ) == 0 );
81 }
Paul Bakker6697b6c2014-07-04 18:35:50 +020082
Paul Bakkerbd51b262014-07-10 15:26:12 +020083exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +020085}
86/* END_CASE */
87
88/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010089void base64_decode_hex( char * src, data_t * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +020090 int result )
91{
Azim Khand30ca132017-06-09 04:32:58 +010092 unsigned char *res = NULL;
93 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +020094
Paul Bakkerd5983182014-07-04 13:50:31 +020095 res = zero_alloc( dst_buf_size );
96
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010097 TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
Paul Bakkerd5983182014-07-04 13:50:31 +020098 strlen( src ) ) == result );
99 if( result == 0 )
100 {
Azim Khand30ca132017-06-09 04:32:58 +0100101 TEST_ASSERT( len == dst->len );
102 TEST_ASSERT( memcmp( dst->x, res, len ) == 0 );
Paul Bakkerd5983182014-07-04 13:50:31 +0200103 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200104
Paul Bakkerbd51b262014-07-10 15:26:12 +0200105exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +0200107}
108/* END_CASE */
109
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200110/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100111void base64_decode_hex_src( data_t * src, char * dst_ref, int result )
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200112{
113 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +0100114 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200115
Azim Khand30ca132017-06-09 04:32:58 +0100116 TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result );
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200117 if( result == 0 )
118 {
119 TEST_ASSERT( len == strlen( dst_ref ) );
120 TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
121 }
122
123exit:
Azim Khand30ca132017-06-09 04:32:58 +0100124 ;;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200125}
126/* END_CASE */
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100129void base64_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000130{
Andres AG93012e82016-09-09 09:10:28 +0100131 TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000132}
Paul Bakker33b43f12013-08-20 11:48:36 +0200133/* END_CASE */