Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/evp/encode.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include <limits.h>
12
#include <assert.h>
13
#include "internal/cryptlib.h"
14
#include <openssl/evp.h>
15
#include "crypto/evp.h"
16
#include "evp_local.h"
17
18
#if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && (defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64))
19
#if !defined(_M_ARM64EC)
20
#define HAS_IA32CAP_IS_64
21
#endif /* !defined(_M_ARM64EC) */
22
#endif
23
24
#include "enc_b64_avx2.h"
25
#include "enc_b64_scalar.h"
26
27
static unsigned char conv_ascii2bin(unsigned char a,
28
    const unsigned char *table);
29
size_t evp_encodeblock_int(EVP_ENCODE_CTX *ctx, unsigned char *t,
30
    const unsigned char *f, int dlen, int *wrap_cnt);
31
static int evp_decodeblock_int(EVP_ENCODE_CTX *ctx, unsigned char *t,
32
    const unsigned char *f, int n, int eof);
33
/*-
34
 * 64 char lines
35
 * pad input with 0
36
 * left over chars are set to =
37
 * 1 byte  => xx==
38
 * 2 bytes => xxx=
39
 * 3 bytes => xxxx
40
 */
41
#define BIN_PER_LINE (64 / 4 * 3)
42
#define CHUNKS_PER_LINE (64 / 4)
43
#define CHAR_PER_LINE (64 + 1)
44
45
/*-
46
 * 0xF0 is a EOLN
47
 * 0xF1 is ignore but next needs to be 0xF0 (for \r\n processing).
48
 * 0xF2 is EOF
49
 * 0xE0 is ignore at start of line.
50
 * 0xFF is error
51
 */
52
53
#define B64_EOLN 0xF0
54
#define B64_CR 0xF1
55
220M
#define B64_EOF 0xF2
56
3.35M
#define B64_WS 0xE0
57
220M
#define B64_ERROR 0xFF
58
229M
#define B64_NOT_BASE64(a) (((a) | 0x13) == 0xF3)
59
225M
#define B64_BASE64(a) (!B64_NOT_BASE64(a))
60
61
static const unsigned char data_ascii2bin[128] = {
62
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE0,
63
    0xF0, 0xFF, 0xFF, 0xF1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
64
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
65
    0xFF, 0xFF, 0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
66
    0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xF2, 0xFF, 0x3F, 0x34, 0x35,
67
    0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF,
68
    0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04,
69
    0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
70
    0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
71
    0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1A, 0x1B, 0x1C,
72
    0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
73
    0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
74
    0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
75
};
76
77
static const unsigned char srpdata_ascii2bin[128] = {
78
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE0,
79
    0xF0, 0xFF, 0xFF, 0xF1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
80
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
81
    0xFF, 0xFF, 0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
82
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF2, 0x3E, 0x3F, 0x00, 0x01,
83
    0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF,
84
    0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
85
    0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
86
    0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22,
87
    0x23, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x24, 0x25, 0x26,
88
    0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
89
    0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
90
    0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
91
};
92
93
#ifndef CHARSET_EBCDIC
94
static unsigned char conv_ascii2bin(unsigned char a, const unsigned char *table)
95
578M
{
96
578M
    if (a & 0x80)
97
46.6k
        return B64_ERROR;
98
578M
    return table[a];
99
578M
}
100
#else
101
static unsigned char conv_ascii2bin(unsigned char a, const unsigned char *table)
102
{
103
    a = os_toascii[a];
104
    if (a & 0x80)
105
        return B64_ERROR;
106
    return table[a];
107
}
108
#endif
109
110
EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void)
111
218k
{
112
218k
    return OPENSSL_zalloc(sizeof(EVP_ENCODE_CTX));
113
218k
}
114
115
void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx)
116
252k
{
117
252k
    OPENSSL_free(ctx);
118
252k
}
119
120
int EVP_ENCODE_CTX_copy(EVP_ENCODE_CTX *dctx, const EVP_ENCODE_CTX *sctx)
121
0
{
122
0
    memcpy(dctx, sctx, sizeof(EVP_ENCODE_CTX));
123
124
0
    return 1;
125
0
}
126
127
int EVP_ENCODE_CTX_num(EVP_ENCODE_CTX *ctx)
128
8.08k
{
129
8.08k
    return ctx->num;
130
8.08k
}
131
132
void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags)
133
0
{
134
0
    ctx->flags = flags;
135
0
}
136
137
void EVP_EncodeInit(EVP_ENCODE_CTX *ctx)
138
3.93k
{
139
3.93k
    ctx->num = 0;
140
3.93k
    ctx->line_num = 0;
141
3.93k
    ctx->flags = 0;
142
3.93k
}
143
144
int EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
145
    const unsigned char *in, int inl)
146
0
{
147
0
    int i;
148
0
    size_t j;
149
0
    size_t total = 0;
150
151
0
    *outl = 0;
152
0
    if (inl <= 0)
153
0
        return 0;
154
0
    assert(EVP_ENCODE_B64_LENGTH <= (int)sizeof(ctx->enc_data));
155
0
    if (EVP_ENCODE_B64_LENGTH - ctx->num > inl) {
156
0
        memcpy(&(ctx->enc_data[ctx->num]), in, inl);
157
0
        ctx->num += inl;
158
0
        return 1;
159
0
    }
160
0
    if (ctx->num != 0) {
161
0
        i = EVP_ENCODE_B64_LENGTH - ctx->num;
162
0
        memcpy(&(ctx->enc_data[ctx->num]), in, i);
163
0
        in += i;
164
0
        inl -= i;
165
0
        int wrap_cnt = 0;
166
0
        j = evp_encodeblock_int(ctx, out, ctx->enc_data, EVP_ENCODE_B64_LENGTH,
167
0
            &wrap_cnt);
168
0
        ctx->num = 0;
169
0
        out += j;
170
0
        total = j;
171
0
        *out = '\0';
172
0
    }
173
0
    int wrap_cnt = 0;
174
0
    if (EVP_ENCODE_B64_LENGTH % 3 != 0) {
175
0
        j = evp_encodeblock_int(ctx, out, in, inl - (inl % EVP_ENCODE_B64_LENGTH),
176
0
            &wrap_cnt);
177
0
    } else {
178
#if defined(__AVX2__) && defined(HAVE_AVX2_INTRINSICS)
179
        const int newlines = !(ctx->flags & EVP_ENCODE_CTX_NO_NEWLINES) ? EVP_ENCODE_B64_LENGTH : 0;
180
181
        j = encode_base64_avx2(ctx,
182
            (unsigned char *)out,
183
            (const unsigned char *)in,
184
            inl - (inl % EVP_ENCODE_B64_LENGTH), newlines, &wrap_cnt);
185
#elif defined(HAS_IA32CAP_IS_64) && defined(HAVE_AVX2_INTRINSICS)
186
0
        if ((OPENSSL_ia32cap_P[2] & (1u << 5)) != 0) {
187
0
            const int newlines = !(ctx->flags & EVP_ENCODE_CTX_NO_NEWLINES) ? EVP_ENCODE_B64_LENGTH : 0;
188
189
0
            j = encode_base64_avx2(ctx,
190
0
                (unsigned char *)out,
191
0
                (const unsigned char *)in,
192
0
                inl - (inl % EVP_ENCODE_B64_LENGTH), newlines, &wrap_cnt);
193
0
        } else {
194
0
            j = evp_encodeblock_int(ctx, out, in, inl - (inl % EVP_ENCODE_B64_LENGTH),
195
0
                &wrap_cnt);
196
0
        }
197
#else
198
        j = evp_encodeblock_int(ctx, out, in, inl - (inl % EVP_ENCODE_B64_LENGTH),
199
            &wrap_cnt);
200
#endif
201
0
    }
202
0
    in += inl - (inl % EVP_ENCODE_B64_LENGTH);
203
0
    inl -= inl - (inl % EVP_ENCODE_B64_LENGTH);
204
0
    out += j;
205
0
    total += j;
206
0
    if ((ctx->flags & EVP_ENCODE_CTX_NO_NEWLINES) == 0 && EVP_ENCODE_B64_LENGTH % 3 != 0) {
207
0
        *(out++) = '\n';
208
0
        total++;
209
0
    }
210
0
    *out = '\0';
211
0
    if (total > INT_MAX) {
212
        /* Too much output data! */
213
0
        *outl = 0;
214
0
        return 0;
215
0
    }
216
0
    if (inl != 0)
217
0
        memcpy(&(ctx->enc_data[0]), in, inl);
218
0
    ctx->num = inl;
219
0
    *outl = (int)total;
220
221
0
    return 1;
222
0
}
223
224
void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl)
225
{
226
    size_t ret = 0;
227
    int wrap_cnt = 0;
228
229
    if (ctx->num != 0) {
230
        ret = evp_encodeblock_int(ctx, out, ctx->enc_data, ctx->num,
231
            &wrap_cnt);
232
        if (ret > 0) {
233
            if ((ctx->flags & EVP_ENCODE_CTX_NO_NEWLINES) == 0)
234
                out[ret++] = '\n';
235
            out[ret] = '\0';
236
            ctx->num = 0;
237
        }
238
    }
239
    *outl = (int)ret;
240
}
241
242
int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int dlen)
243
0
{
244
0
    int wrap_cnt = 0;
245
246
#if defined(__AVX2__) && defined(HAVE_AVX2_INTRINSICS)
247
    return (int)encode_base64_avx2(NULL, t, f, dlen, 0, &wrap_cnt);
248
#elif defined(HAS_IA32CAP_IS_64) && defined(HAVE_AVX2_INTRINSICS)
249
0
    if ((OPENSSL_ia32cap_P[2] & (1u << 5)) != 0)
250
0
        return (int)encode_base64_avx2(NULL, t, f, dlen, 0, &wrap_cnt);
251
0
    else
252
0
        return (int)evp_encodeblock_int(NULL, t, f, dlen, &wrap_cnt);
253
#else
254
    return (int)evp_encodeblock_int(NULL, t, f, dlen, &wrap_cnt);
255
#endif
256
0
}
257
258
void EVP_DecodeInit(EVP_ENCODE_CTX *ctx)
259
732k
{
260
    /* Only ctx->num and ctx->flags are used during decoding. */
261
732k
    ctx->num = 0;
262
732k
    ctx->line_num = 0;
263
732k
    ctx->flags = 0;
264
732k
}
265
266
/*-
267
 * -1 for error
268
 *  0 for last line
269
 *  1 for full line
270
 *
271
 * Note: even though EVP_DecodeUpdate attempts to detect and report end of
272
 * content, the context doesn't currently remember it and will accept more data
273
 * in the next call. Therefore, the caller is responsible for checking and
274
 * rejecting a 0 return value in the middle of content.
275
 *
276
 * Note: even though EVP_DecodeUpdate has historically tried to detect end of
277
 * content based on line length, this has never worked properly. Therefore,
278
 * we now return 0 when one of the following is true:
279
 *   - Padding or B64_EOF was detected and the last block is complete.
280
 *   - Input has zero-length.
281
 * -1 is returned if:
282
 *   - Invalid characters are detected.
283
 *   - There is extra trailing padding, or data after padding.
284
 *   - B64_EOF is detected after an incomplete base64 block.
285
 */
286
int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
287
    const unsigned char *in, int inl)
288
697k
{
289
697k
    int seof = 0, eof = 0, rv = -1, ret = 0, i, v, tmp, n, decoded_len;
290
697k
    unsigned char *d;
291
697k
    const unsigned char *table;
292
293
697k
    n = ctx->num;
294
697k
    d = ctx->enc_data;
295
296
697k
    if (n > 0 && d[n - 1] == '=') {
297
5.35k
        eof++;
298
5.35k
        if (n > 1 && d[n - 2] == '=')
299
1.90k
            eof++;
300
5.35k
    }
301
302
    /* Legacy behaviour: an empty input chunk signals end of input. */
303
697k
    if (inl == 0) {
304
0
        rv = 0;
305
0
        goto end;
306
0
    }
307
308
697k
    if ((ctx->flags & EVP_ENCODE_CTX_USE_SRP_ALPHABET) != 0)
309
0
        table = srpdata_ascii2bin;
310
697k
    else
311
697k
        table = data_ascii2bin;
312
313
220M
    for (i = 0; i < inl; i++) {
314
220M
        tmp = *(in++);
315
220M
        v = conv_ascii2bin(tmp, table);
316
220M
        if (v == B64_ERROR) {
317
413k
            rv = -1;
318
413k
            goto end;
319
413k
        }
320
321
220M
        if (tmp == '=') {
322
218k
            eof++;
323
219M
        } else if (eof > 0 && B64_BASE64(v)) {
324
            /* More data after padding. */
325
914
            rv = -1;
326
914
            goto end;
327
914
        }
328
329
220M
        if (eof > 2) {
330
1.09k
            rv = -1;
331
1.09k
            goto end;
332
1.09k
        }
333
334
220M
        if (v == B64_EOF) {
335
3.66k
            seof = 1;
336
3.66k
            goto tail;
337
3.66k
        }
338
339
        /* Only save valid base64 characters. */
340
220M
        if (B64_BASE64(v)) {
341
208M
            if (n >= 64) {
342
                /*
343
                 * We increment n once per loop, and empty the buffer as soon as
344
                 * we reach 64 characters, so this can only happen if someone's
345
                 * manually messed with the ctx. Refuse to write any more data.
346
                 */
347
0
                rv = -1;
348
0
                goto end;
349
0
            }
350
208M
            OPENSSL_assert(n < (int)sizeof(ctx->enc_data));
351
208M
            d[n++] = tmp;
352
208M
        }
353
354
220M
        if (n == 64) {
355
3.17M
            decoded_len = evp_decodeblock_int(ctx, out, d, n, eof);
356
3.17M
            n = 0;
357
3.17M
            if (decoded_len < 0 || (decoded_len == 0 && eof > 0)) {
358
0
                rv = -1;
359
0
                goto end;
360
0
            }
361
3.17M
            ret += decoded_len;
362
3.17M
            out += decoded_len;
363
3.17M
        }
364
220M
    }
365
366
    /*
367
     * Legacy behaviour: if the current line is a full base64-block (i.e., has
368
     * 0 mod 4 base64 characters), it is processed immediately. We keep this
369
     * behaviour as applications may not be calling EVP_DecodeFinal properly.
370
     */
371
281k
tail:
372
281k
    if (n > 0) {
373
255k
        if ((n & 3) == 0) {
374
179k
            decoded_len = evp_decodeblock_int(ctx, out, d, n, eof);
375
179k
            n = 0;
376
179k
            if (decoded_len < 0 || (decoded_len == 0 && eof > 0)) {
377
0
                rv = -1;
378
0
                goto end;
379
0
            }
380
179k
            ret += decoded_len;
381
179k
        } else if (seof) {
382
            /* EOF in the middle of a base64 block. */
383
1.19k
            rv = -1;
384
1.19k
            goto end;
385
1.19k
        }
386
255k
    }
387
388
280k
    rv = seof || (n == 0 && eof) ? 0 : 1;
389
697k
end:
390
    /* Legacy behaviour. This should probably rather be zeroed on error. */
391
697k
    *outl = ret;
392
697k
    ctx->num = n;
393
697k
    return rv;
394
280k
}
395
396
static int evp_decodeblock_int(EVP_ENCODE_CTX *ctx, unsigned char *t,
397
    const unsigned char *f, int n,
398
    int eof)
399
3.35M
{
400
3.35M
    int i, ret = 0, a, b, c, d;
401
3.35M
    unsigned long l;
402
3.35M
    const unsigned char *table;
403
404
3.35M
    if (eof < -1 || eof > 2)
405
0
        return -1;
406
407
3.35M
    if (ctx != NULL && (ctx->flags & EVP_ENCODE_CTX_USE_SRP_ALPHABET) != 0)
408
0
        table = srpdata_ascii2bin;
409
3.35M
    else
410
3.35M
        table = data_ascii2bin;
411
412
    /* trim whitespace from the start of the line. */
413
3.35M
    while ((n > 0) && (conv_ascii2bin(*f, table) == B64_WS)) {
414
0
        f++;
415
0
        n--;
416
0
    }
417
418
    /*
419
     * strip off stuff at the end of the line ascii2bin values B64_WS,
420
     * B64_EOLN, B64_EOLN and B64_EOF
421
     */
422
3.35M
    while ((n > 3) && (B64_NOT_BASE64(conv_ascii2bin(f[n - 1], table))))
423
0
        n--;
424
425
3.35M
    if (n % 4 != 0)
426
681
        return -1;
427
3.35M
    if (n == 0)
428
0
        return 0;
429
430
    /* all 4-byte blocks except the last one do not have padding. */
431
52.0M
    for (i = 0; i < n - 4; i += 4) {
432
48.6M
        a = conv_ascii2bin(*(f++), table);
433
48.6M
        b = conv_ascii2bin(*(f++), table);
434
48.6M
        c = conv_ascii2bin(*(f++), table);
435
48.6M
        d = conv_ascii2bin(*(f++), table);
436
48.6M
        if ((a | b | c | d) & 0x80)
437
0
            return -1;
438
48.6M
        l = ((((unsigned long)a) << 18L) | (((unsigned long)b) << 12L) | (((unsigned long)c) << 6L) | (((unsigned long)d)));
439
48.6M
        *(t++) = (unsigned char)(l >> 16L) & 0xff;
440
48.6M
        *(t++) = (unsigned char)(l >> 8L) & 0xff;
441
48.6M
        *(t++) = (unsigned char)(l) & 0xff;
442
48.6M
        ret += 3;
443
48.6M
    }
444
445
    /* process the last block that may have padding. */
446
3.35M
    a = conv_ascii2bin(*(f++), table);
447
3.35M
    b = conv_ascii2bin(*(f++), table);
448
3.35M
    c = conv_ascii2bin(*(f++), table);
449
3.35M
    d = conv_ascii2bin(*(f++), table);
450
3.35M
    if ((a | b | c | d) & 0x80)
451
0
        return -1;
452
3.35M
    l = ((((unsigned long)a) << 18L) | (((unsigned long)b) << 12L) | (((unsigned long)c) << 6L) | (((unsigned long)d)));
453
454
3.35M
    if (eof == -1)
455
0
        eof = (c == '=') + (d == '=');
456
457
3.35M
    switch (eof) {
458
98.6k
    case 2:
459
98.6k
        *t = (unsigned char)(l >> 16L) & 0xff;
460
98.6k
        break;
461
15.6k
    case 1:
462
15.6k
        *(t++) = (unsigned char)(l >> 16L) & 0xff;
463
15.6k
        *t = (unsigned char)(l >> 8L) & 0xff;
464
15.6k
        break;
465
3.24M
    case 0:
466
3.24M
        *(t++) = (unsigned char)(l >> 16L) & 0xff;
467
3.24M
        *(t++) = (unsigned char)(l >> 8L) & 0xff;
468
3.24M
        *t = (unsigned char)(l) & 0xff;
469
3.24M
        break;
470
3.35M
    }
471
3.35M
    ret += 3 - eof;
472
473
3.35M
    return ret;
474
3.35M
}
475
476
int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n)
477
0
{
478
0
    return evp_decodeblock_int(NULL, t, f, n, 0);
479
0
}
480
481
int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl)
482
207k
{
483
207k
    int i;
484
485
207k
    *outl = 0;
486
207k
    if (ctx->num != 0) {
487
856
        i = evp_decodeblock_int(ctx, out, ctx->enc_data, ctx->num, -1);
488
856
        if (i < 0)
489
856
            return -1;
490
0
        ctx->num = 0;
491
0
        *outl = i;
492
0
        return 1;
493
856
    } else
494
207k
        return 1;
495
207k
}