summaryrefslogtreecommitdiffstats
path: root/lib/crypto_backend/base64.c
blob: 42f70cb1d4574545524fde1c3abf567c467133a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Base64 "Not encryption" helpers, copied and adapted from systemd project.
 *
 * Copyright (C) 2010 Lennart Poettering
 *
 * cryptsetup related changes
 * Copyright (C) 2021-2023 Milan Broz
 *
 * This file is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this file; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <errno.h>
#include <stdlib.h>
#include <limits.h>

#include "crypto_backend.h"

#define WHITESPACE " \t\n\r"

/* https://tools.ietf.org/html/rfc4648#section-4 */
static char base64char(int x)
{
	static const char table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
				      "abcdefghijklmnopqrstuvwxyz"
				      "0123456789+/";
	return table[x & 63];
}

static int unbase64char(char c)
{
	unsigned offset;

	if (c >= 'A' && c <= 'Z')
		return c - 'A';

	offset = 'Z' - 'A' + 1;

	if (c >= 'a' && c <= 'z')
		return c - 'a' + offset;

	offset += 'z' - 'a' + 1;

	if (c >= '0' && c <= '9')
		return c - '0' + offset;

	offset += '9' - '0' + 1;

	if (c == '+')
		return offset;

	offset++;

	if (c == '/')
		return offset;

	return -EINVAL;
}

int crypt_base64_encode(char **out, size_t *out_length, const char *in, size_t in_length)
{
	char *r, *z;
	const uint8_t *x;

	assert(in || in_length == 0);
	assert(out);

	/* three input bytes makes four output bytes, padding is added so we must round up */
	z = r = malloc(4 * (in_length + 2) / 3 + 1);
	if (!r)
		return -ENOMEM;

	for (x = (const uint8_t *)in; x < (const uint8_t*)in + (in_length / 3) * 3; x += 3) {
		/* x[0] == XXXXXXXX; x[1] == YYYYYYYY; x[2] == ZZZZZZZZ */
		*(z++) = base64char(x[0] >> 2);                    /* 00XXXXXX */
		*(z++) = base64char((x[0] & 3) << 4 | x[1] >> 4);  /* 00XXYYYY */
		*(z++) = base64char((x[1] & 15) << 2 | x[2] >> 6); /* 00YYYYZZ */
		*(z++) = base64char(x[2] & 63);                    /* 00ZZZZZZ */
	}

	switch (in_length % 3) {
	case 2:
		*(z++) = base64char(x[0] >> 2);                   /* 00XXXXXX */
		*(z++) = base64char((x[0] & 3) << 4 | x[1] >> 4); /* 00XXYYYY */
		*(z++) = base64char((x[1] & 15) << 2);            /* 00YYYY00 */
		*(z++) = '=';

		break;
	case 1:
		*(z++) = base64char(x[0] >> 2);        /* 00XXXXXX */
		*(z++) = base64char((x[0] & 3) << 4);  /* 00XX0000 */
		*(z++) = '=';
		*(z++) = '=';

		break;
	}

	*z = 0;
	*out = r;
	if (out_length)
		*out_length = z - r;
	return 0;
}

static int unbase64_next(const char **p, size_t *l)
{
	int ret;

	assert(p);
	assert(l);

	/* Find the next non-whitespace character, and decode it. If we find padding, we return it as INT_MAX. We
	 * greedily skip all preceding and all following whitespace. */

	for (;;) {
		if (*l == 0)
			return -EPIPE;

		if (!strchr(WHITESPACE, **p))
			break;

		/* Skip leading whitespace */
		(*p)++, (*l)--;
	}

	if (**p == '=')
		ret = INT_MAX; /* return padding as INT_MAX */
	else {
		ret = unbase64char(**p);
		if (ret < 0)
			return ret;
	}

	for (;;) {
		(*p)++, (*l)--;

		if (*l == 0)
			break;
		if (!strchr(WHITESPACE, **p))
			break;

		/* Skip following whitespace */
	}

	return ret;
}

int crypt_base64_decode(char **out, size_t *out_length, const char *in, size_t in_length)
{
	uint8_t *buf = NULL;
	const char *x;
	uint8_t *z;
	size_t len;
	int r;

	assert(in || in_length == 0);
	assert(out);
	assert(out_length);

	if (in_length == (size_t) -1)
		in_length = strlen(in);

	/* A group of four input bytes needs three output bytes, in case of padding we need to add two or three extra
	 * bytes. Note that this calculation is an upper boundary, as we ignore whitespace while decoding */
	len = (in_length / 4) * 3 + (in_length % 4 != 0 ? (in_length % 4) - 1 : 0);

	buf = malloc(len + 1);
	if (!buf)
		return -ENOMEM;

	for (x = in, z = buf;;) {
		int a, b, c, d; /* a == 00XXXXXX; b == 00YYYYYY; c == 00ZZZZZZ; d == 00WWWWWW */

		a = unbase64_next(&x, &in_length);
		if (a == -EPIPE) /* End of string */
			break;
		if (a < 0) {
			r = a;
			goto err;
		}
		if (a == INT_MAX) { /* Padding is not allowed at the beginning of a 4ch block */
			r = -EINVAL;
			goto err;
		}

		b = unbase64_next(&x, &in_length);
		if (b < 0) {
			r = b;
			goto err;
		}
		if (b == INT_MAX) { /* Padding is not allowed at the second character of a 4ch block either */
			r = -EINVAL;
			goto err;
		}

		c = unbase64_next(&x, &in_length);
		if (c < 0) {
			r = c;
			goto err;
		}

		d = unbase64_next(&x, &in_length);
		if (d < 0) {
			r = d;
			goto err;
		}

		if (c == INT_MAX) { /* Padding at the third character */

			if (d != INT_MAX) { /* If the third character is padding, the fourth must be too */
				r = -EINVAL;
				goto err;
			}

			/* b == 00YY0000 */
			if (b & 15) {
				r = -EINVAL;
				goto err;
			}

			if (in_length > 0) { /* Trailing rubbish? */
				r = -ENAMETOOLONG;
				goto err;
			}

			*(z++) = (uint8_t) a << 2 | (uint8_t) (b >> 4); /* XXXXXXYY */
			break;
		}

		if (d == INT_MAX) {
			/* c == 00ZZZZ00 */
			if (c & 3) {
				r = -EINVAL;
				goto err;
			}

			if (in_length > 0) { /* Trailing rubbish? */
				r = -ENAMETOOLONG;
				goto err;
			}

			*(z++) = (uint8_t) a << 2 | (uint8_t) b >> 4; /* XXXXXXYY */
			*(z++) = (uint8_t) b << 4 | (uint8_t) c >> 2; /* YYYYZZZZ */
			break;
		}

		*(z++) = (uint8_t) a << 2 | (uint8_t) b >> 4; /* XXXXXXYY */
		*(z++) = (uint8_t) b << 4 | (uint8_t) c >> 2; /* YYYYZZZZ */
		*(z++) = (uint8_t) c << 6 | (uint8_t) d;      /* ZZWWWWWW */
	}

	*z = 0;

	*out_length = (size_t) (z - buf);
	*out = (char *)buf;
	return 0;
err:
	free(buf);

	/* Ignore other errors in crypt_backend */
	if (r != -ENOMEM)
		r = -EINVAL;

	return r;
}