Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / lib / crypto_backend / base64.c
1 /*
2  * Base64 "Not encryption" helpers, copied and adapted from systemd project.
3  *
4  * Copyright (C) 2010 Lennart Poettering
5  *
6  * cryptsetup related changes
7  * Copyright (C) 2021-2023 Milan Broz
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <limits.h>
27
28 #include "crypto_backend.h"
29
30 #define WHITESPACE " \t\n\r"
31
32 /* https://tools.ietf.org/html/rfc4648#section-4 */
33 static char base64char(int x)
34 {
35         static const char table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
36                                       "abcdefghijklmnopqrstuvwxyz"
37                                       "0123456789+/";
38         return table[x & 63];
39 }
40
41 static int unbase64char(char c)
42 {
43         unsigned offset;
44
45         if (c >= 'A' && c <= 'Z')
46                 return c - 'A';
47
48         offset = 'Z' - 'A' + 1;
49
50         if (c >= 'a' && c <= 'z')
51                 return c - 'a' + offset;
52
53         offset += 'z' - 'a' + 1;
54
55         if (c >= '0' && c <= '9')
56                 return c - '0' + offset;
57
58         offset += '9' - '0' + 1;
59
60         if (c == '+')
61                 return offset;
62
63         offset++;
64
65         if (c == '/')
66                 return offset;
67
68         return -EINVAL;
69 }
70
71 int crypt_base64_encode(char **out, size_t *out_length, const char *in, size_t in_length)
72 {
73         char *r, *z;
74         const uint8_t *x;
75
76         assert(in || in_length == 0);
77         assert(out);
78
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);
81         if (!r)
82                 return -ENOMEM;
83
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 */
90         }
91
92         switch (in_length % 3) {
93         case 2:
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 */
97                 *(z++) = '=';
98
99                 break;
100         case 1:
101                 *(z++) = base64char(x[0] >> 2);        /* 00XXXXXX */
102                 *(z++) = base64char((x[0] & 3) << 4);  /* 00XX0000 */
103                 *(z++) = '=';
104                 *(z++) = '=';
105
106                 break;
107         }
108
109         *z = 0;
110         *out = r;
111         if (out_length)
112                 *out_length = z - r;
113         return 0;
114 }
115
116 static int unbase64_next(const char **p, size_t *l)
117 {
118         int ret;
119
120         assert(p);
121         assert(l);
122
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. */
125
126         for (;;) {
127                 if (*l == 0)
128                         return -EPIPE;
129
130                 if (!strchr(WHITESPACE, **p))
131                         break;
132
133                 /* Skip leading whitespace */
134                 (*p)++, (*l)--;
135         }
136
137         if (**p == '=')
138                 ret = INT_MAX; /* return padding as INT_MAX */
139         else {
140                 ret = unbase64char(**p);
141                 if (ret < 0)
142                         return ret;
143         }
144
145         for (;;) {
146                 (*p)++, (*l)--;
147
148                 if (*l == 0)
149                         break;
150                 if (!strchr(WHITESPACE, **p))
151                         break;
152
153                 /* Skip following whitespace */
154         }
155
156         return ret;
157 }
158
159 int crypt_base64_decode(char **out, size_t *out_length, const char *in, size_t in_length)
160 {
161         uint8_t *buf = NULL;
162         const char *x;
163         uint8_t *z;
164         size_t len;
165         int r;
166
167         assert(in || in_length == 0);
168         assert(out);
169         assert(out_length);
170
171         if (in_length == (size_t) -1)
172                 in_length = strlen(in);
173
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);
177
178         buf = malloc(len + 1);
179         if (!buf)
180                 return -ENOMEM;
181
182         for (x = in, z = buf;;) {
183                 int a, b, c, d; /* a == 00XXXXXX; b == 00YYYYYY; c == 00ZZZZZZ; d == 00WWWWWW */
184
185                 a = unbase64_next(&x, &in_length);
186                 if (a == -EPIPE) /* End of string */
187                         break;
188                 if (a < 0) {
189                         r = a;
190                         goto err;
191                 }
192                 if (a == INT_MAX) { /* Padding is not allowed at the beginning of a 4ch block */
193                         r = -EINVAL;
194                         goto err;
195                 }
196
197                 b = unbase64_next(&x, &in_length);
198                 if (b < 0) {
199                         r = b;
200                         goto err;
201                 }
202                 if (b == INT_MAX) { /* Padding is not allowed at the second character of a 4ch block either */
203                         r = -EINVAL;
204                         goto err;
205                 }
206
207                 c = unbase64_next(&x, &in_length);
208                 if (c < 0) {
209                         r = c;
210                         goto err;
211                 }
212
213                 d = unbase64_next(&x, &in_length);
214                 if (d < 0) {
215                         r = d;
216                         goto err;
217                 }
218
219                 if (c == INT_MAX) { /* Padding at the third character */
220
221                         if (d != INT_MAX) { /* If the third character is padding, the fourth must be too */
222                                 r = -EINVAL;
223                                 goto err;
224                         }
225
226                         /* b == 00YY0000 */
227                         if (b & 15) {
228                                 r = -EINVAL;
229                                 goto err;
230                         }
231
232                         if (in_length > 0) { /* Trailing rubbish? */
233                                 r = -ENAMETOOLONG;
234                                 goto err;
235                         }
236
237                         *(z++) = (uint8_t) a << 2 | (uint8_t) (b >> 4); /* XXXXXXYY */
238                         break;
239                 }
240
241                 if (d == INT_MAX) {
242                         /* c == 00ZZZZ00 */
243                         if (c & 3) {
244                                 r = -EINVAL;
245                                 goto err;
246                         }
247
248                         if (in_length > 0) { /* Trailing rubbish? */
249                                 r = -ENAMETOOLONG;
250                                 goto err;
251                         }
252
253                         *(z++) = (uint8_t) a << 2 | (uint8_t) b >> 4; /* XXXXXXYY */
254                         *(z++) = (uint8_t) b << 4 | (uint8_t) c >> 2; /* YYYYZZZZ */
255                         break;
256                 }
257
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 */
261         }
262
263         *z = 0;
264
265         *out_length = (size_t) (z - buf);
266         *out = (char *)buf;
267         return 0;
268 err:
269         free(buf);
270
271         /* Ignore other errors in crypt_backend */
272         if (r != -ENOMEM)
273                 r = -EINVAL;
274
275         return r;
276 }