xref: /freebsd/contrib/wpa/src/utils/base64.c (revision 44d4804d1945435745518cd09eb8ae6ab22ecef4)
1 /*
2  * Base64 encoding/decoding (RFC1341)
3  * Copyright (c) 2005-2019, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 #include <stdint.h>
11 
12 #include "os.h"
13 #include "base64.h"
14 
15 static const unsigned char base64_table[65] =
16 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
17 static const unsigned char base64_url_table[65] =
18 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
19 
20 
21 static unsigned char * base64_gen_encode(const unsigned char *src, size_t len,
22 					 size_t *out_len,
23 					 const unsigned char *table,
24 					 int add_pad)
25 {
26 	unsigned char *out, *pos;
27 	const unsigned char *end, *in;
28 	size_t olen;
29 	int line_len;
30 
31 	if (len >= SIZE_MAX / 4)
32 		return NULL;
33 	olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
34 	if (add_pad)
35 		olen += olen / 72; /* line feeds */
36 	olen++; /* nul termination */
37 	if (olen < len)
38 		return NULL; /* integer overflow */
39 	out = os_malloc(olen);
40 	if (out == NULL)
41 		return NULL;
42 
43 	end = src + len;
44 	in = src;
45 	pos = out;
46 	line_len = 0;
47 	while (end - in >= 3) {
48 		*pos++ = table[(in[0] >> 2) & 0x3f];
49 		*pos++ = table[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3f];
50 		*pos++ = table[(((in[1] & 0x0f) << 2) | (in[2] >> 6)) & 0x3f];
51 		*pos++ = table[in[2] & 0x3f];
52 		in += 3;
53 		line_len += 4;
54 		if (add_pad && line_len >= 72) {
55 			*pos++ = '\n';
56 			line_len = 0;
57 		}
58 	}
59 
60 	if (end - in) {
61 		*pos++ = table[(in[0] >> 2) & 0x3f];
62 		if (end - in == 1) {
63 			*pos++ = table[((in[0] & 0x03) << 4) & 0x3f];
64 			if (add_pad)
65 				*pos++ = '=';
66 		} else {
67 			*pos++ = table[(((in[0] & 0x03) << 4) |
68 					(in[1] >> 4)) & 0x3f];
69 			*pos++ = table[((in[1] & 0x0f) << 2) & 0x3f];
70 		}
71 		if (add_pad)
72 			*pos++ = '=';
73 		line_len += 4;
74 	}
75 
76 	if (add_pad && line_len)
77 		*pos++ = '\n';
78 
79 	*pos = '\0';
80 	if (out_len)
81 		*out_len = pos - out;
82 	return out;
83 }
84 
85 
86 static unsigned char * base64_gen_decode(const unsigned char *src, size_t len,
87 					 size_t *out_len,
88 					 const unsigned char *table)
89 {
90 	unsigned char dtable[256], *out, *pos, block[4], tmp;
91 	size_t i, count, olen;
92 	int pad = 0;
93 	size_t extra_pad;
94 
95 	os_memset(dtable, 0x80, 256);
96 	for (i = 0; i < sizeof(base64_table) - 1; i++)
97 		dtable[table[i]] = (unsigned char) i;
98 	dtable['='] = 0;
99 
100 	count = 0;
101 	for (i = 0; i < len; i++) {
102 		if (dtable[src[i]] != 0x80)
103 			count++;
104 	}
105 
106 	if (count == 0)
107 		return NULL;
108 	extra_pad = (4 - count % 4) % 4;
109 
110 	olen = (count + extra_pad) / 4 * 3;
111 	pos = out = os_malloc(olen);
112 	if (out == NULL)
113 		return NULL;
114 
115 	count = 0;
116 	for (i = 0; i < len + extra_pad; i++) {
117 		unsigned char val;
118 
119 		if (i >= len)
120 			val = '=';
121 		else
122 			val = src[i];
123 		tmp = dtable[val];
124 		if (tmp == 0x80)
125 			continue;
126 
127 		if (val == '=')
128 			pad++;
129 		block[count] = tmp;
130 		count++;
131 		if (count == 4) {
132 			*pos++ = (block[0] << 2) | (block[1] >> 4);
133 			*pos++ = (block[1] << 4) | (block[2] >> 2);
134 			*pos++ = (block[2] << 6) | block[3];
135 			count = 0;
136 			if (pad) {
137 				if (pad == 1)
138 					pos--;
139 				else if (pad == 2)
140 					pos -= 2;
141 				else {
142 					/* Invalid padding */
143 					os_free(out);
144 					return NULL;
145 				}
146 				break;
147 			}
148 		}
149 	}
150 
151 	*out_len = pos - out;
152 	return out;
153 }
154 
155 
156 /**
157  * base64_encode - Base64 encode
158  * @src: Data to be encoded
159  * @len: Length of the data to be encoded
160  * @out_len: Pointer to output length variable, or %NULL if not used
161  * Returns: Allocated buffer of out_len bytes of encoded data,
162  * or %NULL on failure
163  *
164  * Caller is responsible for freeing the returned buffer. Returned buffer is
165  * nul terminated to make it easier to use as a C string. The nul terminator is
166  * not included in out_len.
167  */
168 unsigned char * base64_encode(const unsigned char *src, size_t len,
169 			      size_t *out_len)
170 {
171 	return base64_gen_encode(src, len, out_len, base64_table, 1);
172 }
173 
174 
175 unsigned char * base64_url_encode(const unsigned char *src, size_t len,
176 				  size_t *out_len, int add_pad)
177 {
178 	return base64_gen_encode(src, len, out_len, base64_url_table, add_pad);
179 }
180 
181 
182 /**
183  * base64_decode - Base64 decode
184  * @src: Data to be decoded
185  * @len: Length of the data to be decoded
186  * @out_len: Pointer to output length variable
187  * Returns: Allocated buffer of out_len bytes of decoded data,
188  * or %NULL on failure
189  *
190  * Caller is responsible for freeing the returned buffer.
191  */
192 unsigned char * base64_decode(const unsigned char *src, size_t len,
193 			      size_t *out_len)
194 {
195 	return base64_gen_decode(src, len, out_len, base64_table);
196 }
197 
198 
199 unsigned char * base64_url_decode(const unsigned char *src, size_t len,
200 				  size_t *out_len)
201 {
202 	return base64_gen_decode(src, len, out_len, base64_url_table);
203 }
204