2 * Base64 "Not encryption" helpers, copied and adapted from systemd project.
4 * Copyright (C) 2010 Lennart Poettering
6 * cryptsetup related changes
7 * Copyright (C) 2021-2023 Milan Broz
9 * This file is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This file is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this file; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "crypto_backend.h"
30 #define WHITESPACE " \t\n\r"
32 /* https://tools.ietf.org/html/rfc4648#section-4 */
33 static char base64char(int x)
35 static const char table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
36 "abcdefghijklmnopqrstuvwxyz"
41 static int unbase64char(char c)
45 if (c >= 'A' && c <= 'Z')
48 offset = 'Z' - 'A' + 1;
50 if (c >= 'a' && c <= 'z')
51 return c - 'a' + offset;
53 offset += 'z' - 'a' + 1;
55 if (c >= '0' && c <= '9')
56 return c - '0' + offset;
58 offset += '9' - '0' + 1;
71 int crypt_base64_encode(char **out, size_t *out_length, const char *in, size_t in_length)
76 assert(in || in_length == 0);
79 /* three input bytes makes four output bytes, padding is added so we must round up */
80 z = r = malloc(4 * (in_length + 2) / 3 + 1);
84 for (x = (const uint8_t *)in; x < (const uint8_t*)in + (in_length / 3) * 3; x += 3) {
85 /* x[0] == XXXXXXXX; x[1] == YYYYYYYY; x[2] == ZZZZZZZZ */
86 *(z++) = base64char(x[0] >> 2); /* 00XXXXXX */
87 *(z++) = base64char((x[0] & 3) << 4 | x[1] >> 4); /* 00XXYYYY */
88 *(z++) = base64char((x[1] & 15) << 2 | x[2] >> 6); /* 00YYYYZZ */
89 *(z++) = base64char(x[2] & 63); /* 00ZZZZZZ */
92 switch (in_length % 3) {
94 *(z++) = base64char(x[0] >> 2); /* 00XXXXXX */
95 *(z++) = base64char((x[0] & 3) << 4 | x[1] >> 4); /* 00XXYYYY */
96 *(z++) = base64char((x[1] & 15) << 2); /* 00YYYY00 */
101 *(z++) = base64char(x[0] >> 2); /* 00XXXXXX */
102 *(z++) = base64char((x[0] & 3) << 4); /* 00XX0000 */
116 static int unbase64_next(const char **p, size_t *l)
123 /* Find the next non-whitespace character, and decode it. If we find padding, we return it as INT_MAX. We
124 * greedily skip all preceding and all following whitespace. */
130 if (!strchr(WHITESPACE, **p))
133 /* Skip leading whitespace */
138 ret = INT_MAX; /* return padding as INT_MAX */
140 ret = unbase64char(**p);
150 if (!strchr(WHITESPACE, **p))
153 /* Skip following whitespace */
159 int crypt_base64_decode(char **out, size_t *out_length, const char *in, size_t in_length)
167 assert(in || in_length == 0);
171 if (in_length == (size_t) -1)
172 in_length = strlen(in);
174 /* A group of four input bytes needs three output bytes, in case of padding we need to add two or three extra
175 * bytes. Note that this calculation is an upper boundary, as we ignore whitespace while decoding */
176 len = (in_length / 4) * 3 + (in_length % 4 != 0 ? (in_length % 4) - 1 : 0);
178 buf = malloc(len + 1);
182 for (x = in, z = buf;;) {
183 int a, b, c, d; /* a == 00XXXXXX; b == 00YYYYYY; c == 00ZZZZZZ; d == 00WWWWWW */
185 a = unbase64_next(&x, &in_length);
186 if (a == -EPIPE) /* End of string */
192 if (a == INT_MAX) { /* Padding is not allowed at the beginning of a 4ch block */
197 b = unbase64_next(&x, &in_length);
202 if (b == INT_MAX) { /* Padding is not allowed at the second character of a 4ch block either */
207 c = unbase64_next(&x, &in_length);
213 d = unbase64_next(&x, &in_length);
219 if (c == INT_MAX) { /* Padding at the third character */
221 if (d != INT_MAX) { /* If the third character is padding, the fourth must be too */
232 if (in_length > 0) { /* Trailing rubbish? */
237 *(z++) = (uint8_t) a << 2 | (uint8_t) (b >> 4); /* XXXXXXYY */
248 if (in_length > 0) { /* Trailing rubbish? */
253 *(z++) = (uint8_t) a << 2 | (uint8_t) b >> 4; /* XXXXXXYY */
254 *(z++) = (uint8_t) b << 4 | (uint8_t) c >> 2; /* YYYYZZZZ */
258 *(z++) = (uint8_t) a << 2 | (uint8_t) b >> 4; /* XXXXXXYY */
259 *(z++) = (uint8_t) b << 4 | (uint8_t) c >> 2; /* YYYYZZZZ */
260 *(z++) = (uint8_t) c << 6 | (uint8_t) d; /* ZZWWWWWW */
265 *out_length = (size_t) (z - buf);
271 /* Ignore other errors in crypt_backend */