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 <stdint.h>
26 #include <errno.h>
27 #include <zephyr/sys/base64.h>
28 
29 static const uint8_t base64_enc_map[64] = {
30 	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
31 	'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
32 	'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
33 	'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
34 	'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
35 	'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
36 	'8', '9', '+', '/'
37 };
38 
39 static const uint8_t base64_dec_map[128] = {
40 	127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
41 	127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
42 	127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
43 	127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
44 	127, 127, 127,	62, 127, 127, 127,  63,  52,  53,
45 	 54,  55,  56,	57,  58,  59,  60,  61, 127, 127,
46 	127,  64, 127, 127, 127,   0,	1,   2,   3,   4,
47 	  5,   6,   7,	 8,   9,  10,  11,  12,  13,  14,
48 	 15,  16,  17,	18,  19,  20,  21,  22,  23,  24,
49 	 25, 127, 127, 127, 127, 127, 127,  26,  27,  28,
50 	 29,  30,  31,	32,  33,  34,  35,  36,  37,  38,
51 	 39,  40,  41,	42,  43,  44,  45,  46,  47,  48,
52 	 49,  50,  51, 127, 127, 127, 127, 127
53 };
54 
55 #define BASE64_SIZE_T_MAX	((size_t) -1) /* SIZE_T_MAX is not standard */
56 
57 /*
58  * Encode a buffer into base64 format
59  */
base64_encode(uint8_t * dst,size_t dlen,size_t * olen,const uint8_t * src,size_t slen)60 int base64_encode(uint8_t *dst, size_t dlen, size_t *olen, const uint8_t *src,
61 		  size_t slen)
62 {
63 	size_t i, n;
64 	int C1, C2, C3;
65 	uint8_t *p;
66 
67 	if (slen == 0) {
68 		*olen = 0;
69 		return 0;
70 	}
71 
72 	n = slen / 3 + (slen % 3 != 0);
73 
74 	if (n > (BASE64_SIZE_T_MAX - 1) / 4) {
75 		*olen = BASE64_SIZE_T_MAX;
76 		return -ENOMEM;
77 	}
78 
79 	n *= 4;
80 
81 	if ((dlen < n + 1) || (!dst)) {
82 		*olen = n + 1;
83 		return -ENOMEM;
84 	}
85 
86 	n = (slen / 3) * 3;
87 
88 	for (i = 0, p = dst; i < n; i += 3) {
89 		C1 = *src++;
90 		C2 = *src++;
91 		C3 = *src++;
92 
93 		*p++ = base64_enc_map[(C1 >> 2) & 0x3F];
94 		*p++ = base64_enc_map[(((C1 &  3) << 4) + (C2 >> 4)) & 0x3F];
95 		*p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
96 		*p++ = base64_enc_map[C3 & 0x3F];
97 	}
98 
99 	if (i < slen) {
100 		C1 = *src++;
101 		C2 = ((i + 1) < slen) ? *src++ : 0;
102 
103 		*p++ = base64_enc_map[(C1 >> 2) & 0x3F];
104 		*p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
105 
106 		if ((i + 1) < slen) {
107 			*p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
108 		} else {
109 			*p++ = '=';
110 		}
111 
112 		*p++ = '=';
113 	}
114 
115 	*olen = p - dst;
116 	*p = 0U;
117 
118 	return 0;
119 }
120 
121 /*
122  * Decode a base64-formatted buffer
123  */
base64_decode(uint8_t * dst,size_t dlen,size_t * olen,const uint8_t * src,size_t slen)124 int base64_decode(uint8_t *dst, size_t dlen, size_t *olen, const uint8_t *src,
125 		  size_t slen)
126 {
127 	size_t i, n;
128 	uint32_t j, x;
129 	uint8_t *p;
130 
131 	/* First pass: check for validity and get output length */
132 	for (i = n = j = 0U; i < slen; i++) {
133 		/* Skip spaces before checking for EOL */
134 		x = 0U;
135 		while (i < slen && src[i] == ' ') {
136 			++i;
137 			++x;
138 		}
139 
140 		/* Spaces at end of buffer are OK */
141 		if (i == slen) {
142 			break;
143 		}
144 
145 		if ((slen - i) >= 2 && src[i] == '\r' && src[i + 1] == '\n') {
146 			continue;
147 		}
148 
149 		if (src[i] == '\n') {
150 			continue;
151 		}
152 
153 		/* Space inside a line is an error */
154 		if (x != 0U) {
155 			return -EINVAL;
156 		}
157 
158 		if (src[i] == '=' && ++j > 2) {
159 			return -EINVAL;
160 		}
161 
162 		if (src[i] > 127 || base64_dec_map[src[i]] == 127U) {
163 			return -EINVAL;
164 		}
165 
166 		if (base64_dec_map[src[i]] < 64 && j != 0U) {
167 			return -EINVAL;
168 		}
169 
170 		n++;
171 	}
172 
173 	if (n == 0) {
174 		*olen = 0;
175 		return 0;
176 	}
177 
178 	/* The following expression is to calculate the following formula
179 	 * without risk of integer overflow in n:
180 	 *	   n = ( ( n * 6 ) + 7 ) >> 3;
181 	 */
182 	n = (6 * (n >> 3)) + ((6 * (n & 0x7) + 7) >> 3);
183 	n -= j;
184 
185 	if (dst == NULL || dlen < n) {
186 		*olen = n;
187 		return -ENOMEM;
188 	}
189 
190 	for (j = 3U, n = x = 0U, p = dst; i > 0; i--, src++) {
191 
192 		if (*src == '\r' || *src == '\n' || *src == ' ') {
193 			continue;
194 		}
195 
196 		j -= (base64_dec_map[*src] == 64U);
197 		x  = (x << 6) | (base64_dec_map[*src] & 0x3F);
198 
199 		if (++n == 4) {
200 			n = 0;
201 			if (j > 0) {
202 				*p++ = (unsigned char)(x >> 16);
203 			}
204 			if (j > 1) {
205 				*p++ = (unsigned char)(x >> 8);
206 			}
207 			if (j > 2) {
208 				*p++ = (unsigned char)(x);
209 			}
210 		}
211 	}
212 
213 	*olen = p - dst;
214 
215 	return 0;
216 }
217