1 /*
2  * RFC 1521 base64 encoding/decoding
3  *
4  * Copyright (C) 2018, Nordic Semiconductor ASA
5  * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may
9  * not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Adapted for Zephyr by Carles Cufi (carles.cufi@nordicsemi.no)
21  *  - Removed mbedtls_ prefixes
22  *  - Reworked coding style
23  */
24 
25 #include <string.h>
26 #include <stdbool.h>
27 #include <zephyr/sys/base64.h>
28 #include <zephyr/ztest.h>
29 
30 #include "../../../lib/utils/base64.c"
31 
32 static const unsigned char base64_test_dec[64] = {
33 	0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
34 	0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
35 	0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
36 	0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
37 	0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
38 	0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
39 	0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
40 	0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
41 };
42 
43 static const unsigned char base64_test_enc[] =
44 	"JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
45 	"swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
46 
47 static const unsigned char base64_test_enc2[] =
48 	"Jkwo048//dw 0sf356efdaKFLowLKAfJdw410Lw3PdKl"
49 	"swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
50 
51 static const unsigned char base64_test_enc3[] =
52 	"PsdA1lf04JJ3nc00A30F8ker09i0ldkw36bv=SDW2\r\nv"
53 	"swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
54 
55 static const unsigned char base64_test_enc4[] =
56 	"===uVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
57 	"swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
58 
59 static const unsigned char base64_test_enc5[] =
60 	"JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i\n\r\ni"
61 	"swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw= ";
62 
ZTEST(lib_base64,test_base64_codec)63 ZTEST(lib_base64, test_base64_codec)
64 {
65 	size_t len;
66 	int rc;
67 	const unsigned char *src;
68 	unsigned char buffer[128];
69 	int slen;
70 	int n;
71 
72 	src = base64_test_dec;
73 
74 	/* test base64_encode */
75 	rc = base64_encode(buffer, sizeof(buffer), &len, src, 64);
76 	zassert_equal(rc, 0, "Encode test return value");
77 	rc = memcmp(base64_test_enc, buffer, 88);
78 	zassert_equal(rc, 0, "Encode test comparison");
79 
80 	src = base64_test_enc;
81 
82 	/* test base64_decode */
83 	rc = base64_decode(buffer, sizeof(buffer), &len, src, 88);
84 	zassert_equal(rc, 0, "Decode test return value");
85 	rc = memcmp(base64_test_dec, buffer, 64);
86 	zassert_equal(rc, 0, "Decode test comparison");
87 
88 	/* test error paths - encode */
89 	rc = base64_encode(buffer, sizeof(buffer), &len, src, 0);
90 	zassert_equal(rc, 0, "Error: slen: encode test return value");
91 	zassert_equal(len, 0, "Error: slen: length value");
92 
93 	slen = ((-1 - 1) / 4) * 3 - 1;
94 	rc = base64_encode(buffer, sizeof(buffer), &len, src, slen);
95 	zassert_equal(rc, -ENOMEM, "Error: n: encode test return value");
96 	zassert_equal(len, -1, "Error: n: length value");
97 
98 	slen = 100;
99 	n = slen / 3 + (slen % 3 != 0);
100 	n *= 4;
101 	rc = base64_encode(buffer, sizeof(buffer), &len, src, slen);
102 	zassert_equal(rc, -ENOMEM, "Error: dlen: encode test return value");
103 	zassert_equal(len, n + 1, "Error: dlen: length value");
104 
105 	/* test error paths - decode */
106 	src = base64_test_enc2;
107 	rc = base64_decode(buffer, sizeof(buffer), &len, src, 88);
108 	zassert_equal(rc, -EINVAL, "Error: space: decode test return value");
109 
110 	src = base64_test_enc3;
111 	rc = base64_decode(buffer, sizeof(buffer), &len, src, 88);
112 	zassert_equal(rc, -EINVAL, "Error: dec_map: decode test return value");
113 
114 	src = base64_test_enc4;
115 	rc = base64_decode(buffer, sizeof(buffer), &len, src, 88);
116 	zassert_equal(rc, -EINVAL, "Error: equal: decode test return value");
117 
118 	src = base64_test_enc5;
119 	rc = base64_decode(buffer, sizeof(buffer), &len, src, 88);
120 	zassert_equal(rc, 0, "return, newline: decode test return value");
121 
122 	src = base64_test_enc;
123 	rc = base64_decode(buffer, sizeof(buffer), &len, src, 0);
124 	zassert_equal(rc, 0, "Error: n: decode test return value");
125 	zassert_equal(len, 0, "Error: n: length value");
126 
127 	src = base64_test_enc;
128 	rc = base64_decode(NULL, -1, &len, src, 88);
129 	zassert_equal(rc, -ENOMEM, "Error: dst NULL: decode test return value");
130 }
131 
132 ZTEST_SUITE(lib_base64, NULL, NULL, NULL, NULL, NULL);
133