blob: ec10033462ad07ff540aea7c7ea52bb4e63882f0 [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 Elliott448d5462021-02-24 15:32:42 +00003#include <test/constant_flow.h>
Paul Bakker33b43f12013-08-20 11:48:36 +02004/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00005
Paul Bakker33b43f12013-08-20 11:48:36 +02006/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007 * depends_on:MBEDTLS_BASE64_C
Paul Bakker33b43f12013-08-20 11:48:36 +02008 * END_DEPENDENCIES
9 */
Paul Bakker5690efc2011-05-26 13:16:06 +000010
Paul Bakker33b43f12013-08-20 11:48:36 +020011/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010012void mbedtls_base64_encode( char * src_string, char * dst_string,
13 int dst_buf_size, int result )
Paul Bakker367dae42009-06-28 21:50:27 +000014{
15 unsigned char src_str[1000];
16 unsigned char dst_str[1000];
Paul Elliott448d5462021-02-24 15:32:42 +000017 size_t len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000018
19 memset(src_str, 0x00, 1000);
20 memset(dst_str, 0x00, 1000);
21
Paul Bakkerdd0aae92014-04-17 16:06:37 +020022 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Paul Elliott448d5462021-02-24 15:32:42 +000023 src_len = strlen( (char *) src_str );
24
25 TEST_CF_SECRET( src_str, sizeof( src_str ) );
26 TEST_ASSERT( mbedtls_base64_encode( dst_str, dst_buf_size, &len, src_str, src_len) == result );
27 TEST_CF_PUBLIC( src_str, sizeof( src_str ) );
28
Paul Bakker33b43f12013-08-20 11:48:36 +020029 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +000030 {
Paul Bakker33b43f12013-08-20 11:48:36 +020031 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +000032 }
Paul Bakker367dae42009-06-28 21:50:27 +000033}
Paul Bakker33b43f12013-08-20 11:48:36 +020034/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000035
Paul Bakker33b43f12013-08-20 11:48:36 +020036/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010037void mbedtls_base64_decode( char * src_string, char * dst_string, int result )
Paul Bakker367dae42009-06-28 21:50:27 +000038{
39 unsigned char src_str[1000];
40 unsigned char dst_str[1000];
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010041 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +020042 int res;
Paul Bakker367dae42009-06-28 21:50:27 +000043
44 memset(src_str, 0x00, 1000);
45 memset(dst_str, 0x00, 1000);
Paul Bakkerdd0aae92014-04-17 16:06:37 +020046
47 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010048 res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
Paul Bakker94b916c2014-04-17 16:07:20 +020049 TEST_ASSERT( res == result );
Paul Bakker33b43f12013-08-20 11:48:36 +020050 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +000051 {
Paul Bakker33b43f12013-08-20 11:48:36 +020052 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +000053 }
Paul Bakker367dae42009-06-28 21:50:27 +000054}
Paul Bakker33b43f12013-08-20 11:48:36 +020055/* END_CASE */
Paul Bakker3d360822009-07-05 11:29:38 +000056
Paul Bakkerd5983182014-07-04 13:50:31 +020057/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010058void base64_encode_hex( data_t * src, char * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +020059 int result )
60{
Azim Khand30ca132017-06-09 04:32:58 +010061 unsigned char *res = NULL;
62 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +020063
Ronald Cron690f3eb2020-06-10 10:42:18 +020064 res = mbedtls_test_zero_alloc( dst_buf_size );
Paul Bakkerd5983182014-07-04 13:50:31 +020065
Paul Elliott448d5462021-02-24 15:32:42 +000066 TEST_CF_SECRET( src->x, src->len );
Azim Khand30ca132017-06-09 04:32:58 +010067 TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result );
Paul Elliott448d5462021-02-24 15:32:42 +000068 TEST_CF_PUBLIC( src->x, src->len );
69
Paul Bakkerd5983182014-07-04 13:50:31 +020070 if( result == 0 )
71 {
72 TEST_ASSERT( len == strlen( dst ) );
73 TEST_ASSERT( memcmp( dst, res, len ) == 0 );
74 }
Paul Bakker6697b6c2014-07-04 18:35:50 +020075
Paul Bakkerbd51b262014-07-10 15:26:12 +020076exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +020078}
79/* END_CASE */
80
81/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010082void base64_decode_hex( char * src, data_t * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +020083 int result )
84{
Azim Khand30ca132017-06-09 04:32:58 +010085 unsigned char *res = NULL;
86 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +020087
Ronald Cron690f3eb2020-06-10 10:42:18 +020088 res = mbedtls_test_zero_alloc( dst_buf_size );
Paul Bakkerd5983182014-07-04 13:50:31 +020089
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010090 TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
Paul Bakkerd5983182014-07-04 13:50:31 +020091 strlen( src ) ) == result );
92 if( result == 0 )
93 {
Azim Khand30ca132017-06-09 04:32:58 +010094 TEST_ASSERT( len == dst->len );
95 TEST_ASSERT( memcmp( dst->x, res, len ) == 0 );
Paul Bakkerd5983182014-07-04 13:50:31 +020096 }
Paul Bakker6697b6c2014-07-04 18:35:50 +020097
Paul Bakkerbd51b262014-07-10 15:26:12 +020098exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +0200100}
101/* END_CASE */
102
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200103/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100104void base64_decode_hex_src( data_t * src, char * dst_ref, int result )
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200105{
106 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +0100107 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200108
Azim Khand30ca132017-06-09 04:32:58 +0100109 TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result );
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200110 if( result == 0 )
111 {
112 TEST_ASSERT( len == strlen( dst_ref ) );
113 TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
114 }
115
116exit:
Azim Khand30ca132017-06-09 04:32:58 +0100117 ;;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200118}
119/* END_CASE */
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100122void base64_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000123{
Andres AG93012e82016-09-09 09:10:28 +0100124 TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000125}
Paul Bakker33b43f12013-08-20 11:48:36 +0200126/* END_CASE */