blob: badaf6e1d939c92f3d85ec529f1065bbf5096ead [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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020012void mbedtls_base64_encode(char *src_string,
13 char *dst_string,
14 int dst_buf_size,
15 int result)
Paul Bakker367dae42009-06-28 21:50:27 +000016{
17 unsigned char src_str[1000];
18 unsigned char dst_str[1000];
Paul Elliott448d5462021-02-24 15:32:42 +000019 size_t len, src_len;
Paul Bakker367dae42009-06-28 21:50:27 +000020
21 memset(src_str, 0x00, 1000);
22 memset(dst_str, 0x00, 1000);
23
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020024 strncpy((char *)src_str, src_string, sizeof(src_str) - 1);
25 src_len = strlen((char *)src_str);
Paul Elliott448d5462021-02-24 15:32:42 +000026
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020027 TEST_CF_SECRET(src_str, sizeof(src_str));
28 TEST_ASSERT(mbedtls_base64_encode(dst_str, dst_buf_size, &len, src_str,
29 src_len) == result);
30 TEST_CF_PUBLIC(src_str, sizeof(src_str));
Paul Elliott448d5462021-02-24 15:32:42 +000031
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020032 /* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT
33 below from triggering CF failures by unmarking it. */
34 TEST_CF_PUBLIC(dst_str, len);
Paul Elliottc48cb802021-03-02 22:48:40 +000035
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020036 if (result == 0) {
37 TEST_ASSERT(strcmp((char *)dst_str, dst_string) == 0);
Paul Bakker5946fd92009-07-11 15:29:30 +000038 }
Paul Bakker367dae42009-06-28 21:50:27 +000039}
Paul Bakker33b43f12013-08-20 11:48:36 +020040/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000041
Paul Bakker33b43f12013-08-20 11:48:36 +020042/* BEGIN_CASE */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020043void mbedtls_base64_decode(char *src_string, char *dst_string, int result)
Paul Bakker367dae42009-06-28 21:50:27 +000044{
45 unsigned char src_str[1000];
46 unsigned char dst_str[1000];
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010047 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +020048 int res;
Paul Bakker367dae42009-06-28 21:50:27 +000049
50 memset(src_str, 0x00, 1000);
51 memset(dst_str, 0x00, 1000);
Paul Bakkerdd0aae92014-04-17 16:06:37 +020052
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053 strncpy((char *)src_str, src_string, sizeof(src_str) - 1);
54 res = mbedtls_base64_decode(dst_str, sizeof(dst_str), &len, src_str,
55 strlen((char *)src_str));
56 TEST_ASSERT(res == result);
57 if (result == 0) {
58 TEST_ASSERT(strcmp((char *)dst_str, dst_string) == 0);
Paul Bakker5946fd92009-07-11 15:29:30 +000059 }
Paul Bakker367dae42009-06-28 21:50:27 +000060}
Paul Bakker33b43f12013-08-20 11:48:36 +020061/* END_CASE */
Paul Bakker3d360822009-07-05 11:29:38 +000062
Paul Bakkerd5983182014-07-04 13:50:31 +020063/* BEGIN_CASE */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020064void base64_encode_hex(data_t *src, char *dst, int dst_buf_size, int result)
Paul Bakkerd5983182014-07-04 13:50:31 +020065{
Azim Khand30ca132017-06-09 04:32:58 +010066 unsigned char *res = NULL;
67 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +020068
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020069 res = mbedtls_test_zero_alloc(dst_buf_size);
Paul Bakkerd5983182014-07-04 13:50:31 +020070
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020071 TEST_CF_SECRET(src->x, src->len);
72 TEST_ASSERT(mbedtls_base64_encode(res, dst_buf_size, &len, src->x,
73 src->len) == result);
74 TEST_CF_PUBLIC(src->x, src->len);
Paul Elliott448d5462021-02-24 15:32:42 +000075
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020076 /* res will have had tainted data copied to it, prevent the TEST_ASSERT
77 below from triggering CF failures by unmarking it. */
78 TEST_CF_PUBLIC(res, len);
Paul Elliottc48cb802021-03-02 22:48:40 +000079
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020080 if (result == 0) {
81 TEST_ASSERT(len == strlen(dst));
82 TEST_ASSERT(memcmp(dst, res, len) == 0);
Paul Bakkerd5983182014-07-04 13:50:31 +020083 }
Paul Bakker6697b6c2014-07-04 18:35:50 +020084
Paul Bakkerbd51b262014-07-10 15:26:12 +020085exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020086 mbedtls_free(res);
Paul Bakkerd5983182014-07-04 13:50:31 +020087}
88/* END_CASE */
89
90/* BEGIN_CASE */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020091void base64_decode_hex(char *src, data_t *dst, int dst_buf_size, int result)
Paul Bakkerd5983182014-07-04 13:50:31 +020092{
Azim Khand30ca132017-06-09 04:32:58 +010093 unsigned char *res = NULL;
94 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +020095
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020096 res = mbedtls_test_zero_alloc(dst_buf_size);
Paul Bakkerd5983182014-07-04 13:50:31 +020097
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020098 TEST_ASSERT(mbedtls_base64_decode(res, dst_buf_size, &len,
99 (unsigned char *)src,
100 strlen(src)) == result);
101 if (result == 0) {
102 TEST_ASSERT(len == dst->len);
103 TEST_ASSERT(memcmp(dst->x, res, len) == 0);
Paul Bakkerd5983182014-07-04 13:50:31 +0200104 }
Paul Bakker6697b6c2014-07-04 18:35:50 +0200105
Paul Bakkerbd51b262014-07-10 15:26:12 +0200106exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200107 mbedtls_free(res);
Paul Bakkerd5983182014-07-04 13:50:31 +0200108}
109/* END_CASE */
110
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200111/* BEGIN_CASE */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200112void base64_decode_hex_src(data_t *src, char *dst_ref, int result)
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200113{
114 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +0100115 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200116
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200117 TEST_ASSERT(mbedtls_base64_decode(dst, sizeof(dst), &len, src->x,
118 src->len) == result);
119 if (result == 0) {
120 TEST_ASSERT(len == strlen(dst_ref));
121 TEST_ASSERT(memcmp(dst, dst_ref, len) == 0);
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200122 }
123
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200124exit:;
125 ;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200126}
127/* END_CASE */
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200130void base64_selftest()
Paul Bakker3d360822009-07-05 11:29:38 +0000131{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200132 TEST_ASSERT(mbedtls_base64_self_test(1) == 0);
Paul Bakker3d360822009-07-05 11:29:38 +0000133}
Paul Bakker33b43f12013-08-20 11:48:36 +0200134/* END_CASE */