Coverage Report

Created: 2024-01-21 07:01

/src/mbedtls/library/base64.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  RFC 1521 base64 encoding/decoding
3
 *
4
 *  Copyright The Mbed TLS Contributors
5
 *  SPDX-License-Identifier: Apache-2.0
6
 *
7
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8
 *  not use this file except in compliance with the License.
9
 *  You may obtain a copy of the License at
10
 *
11
 *  http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *  Unless required by applicable law or agreed to in writing, software
14
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *  See the License for the specific language governing permissions and
17
 *  limitations under the License.
18
 */
19
20
#include "common.h"
21
22
#if defined(MBEDTLS_BASE64_C)
23
24
#include "mbedtls/base64.h"
25
#include "constant_time_internal.h"
26
27
#include <stdint.h>
28
29
#if defined(MBEDTLS_SELF_TEST)
30
#include <string.h>
31
#include "mbedtls/platform.h"
32
#endif /* MBEDTLS_SELF_TEST */
33
34
0
#define BASE64_SIZE_T_MAX   ((size_t) -1)   /* SIZE_T_MAX is not standard */
35
36
/*
37
 * Encode a buffer into base64 format
38
 */
39
int mbedtls_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,
40
                          const unsigned char *src, size_t slen)
41
0
{
42
0
    size_t i, n;
43
0
    int C1, C2, C3;
44
0
    unsigned char *p;
45
46
0
    if (slen == 0) {
47
0
        *olen = 0;
48
0
        return 0;
49
0
    }
50
51
0
    n = slen / 3 + (slen % 3 != 0);
52
53
0
    if (n > (BASE64_SIZE_T_MAX - 1) / 4) {
54
0
        *olen = BASE64_SIZE_T_MAX;
55
0
        return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
56
0
    }
57
58
0
    n *= 4;
59
60
0
    if ((dlen < n + 1) || (NULL == dst)) {
61
0
        *olen = n + 1;
62
0
        return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
63
0
    }
64
65
0
    n = (slen / 3) * 3;
66
67
0
    for (i = 0, p = dst; i < n; i += 3) {
68
0
        C1 = *src++;
69
0
        C2 = *src++;
70
0
        C3 = *src++;
71
72
0
        *p++ = mbedtls_ct_base64_enc_char((C1 >> 2) & 0x3F);
73
0
        *p++ = mbedtls_ct_base64_enc_char((((C1 &  3) << 4) + (C2 >> 4))
74
0
                                          & 0x3F);
75
0
        *p++ = mbedtls_ct_base64_enc_char((((C2 & 15) << 2) + (C3 >> 6))
76
0
                                          & 0x3F);
77
0
        *p++ = mbedtls_ct_base64_enc_char(C3 & 0x3F);
78
0
    }
79
80
0
    if (i < slen) {
81
0
        C1 = *src++;
82
0
        C2 = ((i + 1) < slen) ? *src++ : 0;
83
84
0
        *p++ = mbedtls_ct_base64_enc_char((C1 >> 2) & 0x3F);
85
0
        *p++ = mbedtls_ct_base64_enc_char((((C1 & 3) << 4) + (C2 >> 4))
86
0
                                          & 0x3F);
87
88
0
        if ((i + 1) < slen) {
89
0
            *p++ = mbedtls_ct_base64_enc_char(((C2 & 15) << 2) & 0x3F);
90
0
        } else {
91
0
            *p++ = '=';
92
0
        }
93
94
0
        *p++ = '=';
95
0
    }
96
97
0
    *olen = p - dst;
98
0
    *p = 0;
99
100
0
    return 0;
101
0
}
102
103
/*
104
 * Decode a base64-formatted buffer
105
 */
106
int mbedtls_base64_decode(unsigned char *dst, size_t dlen, size_t *olen,
107
                          const unsigned char *src, size_t slen)
108
4.47k
{
109
4.47k
    size_t i; /* index in source */
110
4.47k
    size_t n; /* number of digits or trailing = in source */
111
4.47k
    uint32_t x; /* value accumulator */
112
4.47k
    unsigned accumulated_digits = 0;
113
4.47k
    unsigned equals = 0;
114
4.47k
    int spaces_present = 0;
115
4.47k
    unsigned char *p;
116
117
    /* First pass: check for validity and get output length */
118
4.50M
    for (i = n = 0; i < slen; i++) {
119
        /* Skip spaces before checking for EOL */
120
4.50M
        spaces_present = 0;
121
4.50M
        while (i < slen && src[i] == ' ') {
122
0
            ++i;
123
0
            spaces_present = 1;
124
0
        }
125
126
        /* Spaces at end of buffer are OK */
127
4.50M
        if (i == slen) {
128
0
            break;
129
0
        }
130
131
4.50M
        if ((slen - i) >= 2 &&
132
4.50M
            src[i] == '\r' && src[i + 1] == '\n') {
133
70.0k
            continue;
134
70.0k
        }
135
136
4.43M
        if (src[i] == '\n') {
137
70.0k
            continue;
138
70.0k
        }
139
140
        /* Space inside a line is an error */
141
4.36M
        if (spaces_present) {
142
0
            return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
143
0
        }
144
145
4.36M
        if (src[i] > 127) {
146
0
            return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
147
0
        }
148
149
4.36M
        if (src[i] == '=') {
150
2.98k
            if (++equals > 2) {
151
0
                return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
152
0
            }
153
4.35M
        } else {
154
4.35M
            if (equals != 0) {
155
0
                return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
156
0
            }
157
4.35M
            if (mbedtls_ct_base64_dec_value(src[i]) < 0) {
158
0
                return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
159
0
            }
160
4.35M
        }
161
4.36M
        n++;
162
4.36M
    }
163
164
4.47k
    if (n == 0) {
165
0
        *olen = 0;
166
0
        return 0;
167
0
    }
168
169
    /* The following expression is to calculate the following formula without
170
     * risk of integer overflow in n:
171
     *     n = ( ( n * 6 ) + 7 ) >> 3;
172
     */
173
4.47k
    n = (6 * (n >> 3)) + ((6 * (n & 0x7) + 7) >> 3);
174
4.47k
    n -= equals;
175
176
4.47k
    if (dst == NULL || dlen < n) {
177
2.23k
        *olen = n;
178
2.23k
        return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
179
2.23k
    }
180
181
2.23k
    equals = 0;
182
2.25M
    for (x = 0, p = dst; i > 0; i--, src++) {
183
2.25M
        if (*src == '\r' || *src == '\n' || *src == ' ') {
184
70.0k
            continue;
185
70.0k
        }
186
187
2.18M
        x = x << 6;
188
2.18M
        if (*src == '=') {
189
1.49k
            ++equals;
190
2.17M
        } else {
191
2.17M
            x |= mbedtls_ct_base64_dec_value(*src);
192
2.17M
        }
193
194
2.18M
        if (++accumulated_digits == 4) {
195
545k
            accumulated_digits = 0;
196
545k
            *p++ = MBEDTLS_BYTE_2(x);
197
545k
            if (equals <= 1) {
198
544k
                *p++ = MBEDTLS_BYTE_1(x);
199
544k
            }
200
545k
            if (equals <= 0) {
201
544k
                *p++ = MBEDTLS_BYTE_0(x);
202
544k
            }
203
545k
        }
204
2.18M
    }
205
206
2.23k
    *olen = p - dst;
207
208
2.23k
    return 0;
209
4.47k
}
210
211
#if defined(MBEDTLS_SELF_TEST)
212
213
static const unsigned char base64_test_dec[64] =
214
{
215
    0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
216
    0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
217
    0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
218
    0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
219
    0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
220
    0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
221
    0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
222
    0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
223
};
224
225
static const unsigned char base64_test_enc[] =
226
    "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
227
    "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
228
229
/*
230
 * Checkup routine
231
 */
232
int mbedtls_base64_self_test(int verbose)
233
0
{
234
0
    size_t len;
235
0
    const unsigned char *src;
236
0
    unsigned char buffer[128];
237
238
0
    if (verbose != 0) {
239
0
        mbedtls_printf("  Base64 encoding test: ");
240
0
    }
241
242
0
    src = base64_test_dec;
243
244
0
    if (mbedtls_base64_encode(buffer, sizeof(buffer), &len, src, 64) != 0 ||
245
0
        memcmp(base64_test_enc, buffer, 88) != 0) {
246
0
        if (verbose != 0) {
247
0
            mbedtls_printf("failed\n");
248
0
        }
249
250
0
        return 1;
251
0
    }
252
253
0
    if (verbose != 0) {
254
0
        mbedtls_printf("passed\n  Base64 decoding test: ");
255
0
    }
256
257
0
    src = base64_test_enc;
258
259
0
    if (mbedtls_base64_decode(buffer, sizeof(buffer), &len, src, 88) != 0 ||
260
0
        memcmp(base64_test_dec, buffer, 64) != 0) {
261
0
        if (verbose != 0) {
262
0
            mbedtls_printf("failed\n");
263
0
        }
264
265
0
        return 1;
266
0
    }
267
268
0
    if (verbose != 0) {
269
0
        mbedtls_printf("passed\n\n");
270
0
    }
271
272
0
    return 0;
273
0
}
274
275
#endif /* MBEDTLS_SELF_TEST */
276
277
#endif /* MBEDTLS_BASE64_C */