Coverage Report

Created: 2026-06-08 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
0
#define B64_EOF 0xF2
56
0
#define B64_WS 0xE0
57
0
#define B64_ERROR 0xFF
58
0
#define B64_NOT_BASE64(a) (((a) | 0x13) == 0xF3)
59
0
#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
0
{
96
0
    if (a & 0x80)
97
0
        return B64_ERROR;
98
0
    return table[a];
99
0
}
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
0
{
112
0
    return OPENSSL_zalloc(sizeof(EVP_ENCODE_CTX));
113
0
}
114
115
void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx)
116
0
{
117
0
    OPENSSL_free(ctx);
118
0
}
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
0
{
129
0
    return ctx->num;
130
0
}
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
0
{
139
0
    ctx->num = 0;
140
0
    ctx->line_num = 0;
141
0
    ctx->flags = 0;
142
0
}
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
        if ((OPENSSL_ia32cap_P[2] & (1u << 5)) != 0) {
187
            const int newlines = !(ctx->flags & EVP_ENCODE_CTX_NO_NEWLINES) ? EVP_ENCODE_B64_LENGTH : 0;
188
189
            j = encode_base64_avx2(ctx,
190
                (unsigned char *)out,
191
                (const unsigned char *)in,
192
                inl - (inl % EVP_ENCODE_B64_LENGTH), newlines, &wrap_cnt);
193
        } else {
194
            j = evp_encodeblock_int(ctx, out, in, inl - (inl % EVP_ENCODE_B64_LENGTH),
195
                &wrap_cnt);
196
        }
197
#else
198
0
        j = evp_encodeblock_int(ctx, out, in, inl - (inl % EVP_ENCODE_B64_LENGTH),
199
0
            &wrap_cnt);
200
0
#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
0
{
226
0
    size_t ret = 0;
227
0
    int wrap_cnt = 0;
228
229
0
    if (ctx->num != 0) {
230
0
        ret = evp_encodeblock_int(ctx, out, ctx->enc_data, ctx->num,
231
0
            &wrap_cnt);
232
0
        if (ret > 0) {
233
0
            if ((ctx->flags & EVP_ENCODE_CTX_NO_NEWLINES) == 0)
234
0
                out[ret++] = '\n';
235
0
            out[ret] = '\0';
236
0
            ctx->num = 0;
237
0
        }
238
0
    }
239
0
    *outl = (int)ret;
240
0
}
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
    if ((OPENSSL_ia32cap_P[2] & (1u << 5)) != 0)
250
        return (int)encode_base64_avx2(NULL, t, f, dlen, 0, &wrap_cnt);
251
    else
252
        return (int)evp_encodeblock_int(NULL, t, f, dlen, &wrap_cnt);
253
#else
254
0
    return (int)evp_encodeblock_int(NULL, t, f, dlen, &wrap_cnt);
255
0
#endif
256
0
}
257
258
void EVP_DecodeInit(EVP_ENCODE_CTX *ctx)
259
0
{
260
    /* Only ctx->num and ctx->flags are used during decoding. */
261
0
    ctx->num = 0;
262
0
    ctx->line_num = 0;
263
0
    ctx->flags = 0;
264
0
}
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
0
{
289
0
    int seof = 0, eof = 0, rv = -1, ret = 0, i, v, tmp, n, decoded_len;
290
0
    unsigned char *d;
291
0
    const unsigned char *table;
292
293
0
    n = ctx->num;
294
0
    d = ctx->enc_data;
295
296
0
    if (n > 0 && d[n - 1] == '=') {
297
0
        eof++;
298
0
        if (n > 1 && d[n - 2] == '=')
299
0
            eof++;
300
0
    }
301
302
    /* Legacy behaviour: an empty input chunk signals end of input. */
303
0
    if (inl == 0) {
304
0
        rv = 0;
305
0
        goto end;
306
0
    }
307
308
0
    if ((ctx->flags & EVP_ENCODE_CTX_USE_SRP_ALPHABET) != 0)
309
0
        table = srpdata_ascii2bin;
310
0
    else
311
0
        table = data_ascii2bin;
312
313
0
    for (i = 0; i < inl; i++) {
314
0
        tmp = *(in++);
315
0
        v = conv_ascii2bin(tmp, table);
316
0
        if (v == B64_ERROR) {
317
0
            rv = -1;
318
0
            goto end;
319
0
        }
320
321
0
        if (tmp == '=') {
322
0
            eof++;
323
0
        } else if (eof > 0 && B64_BASE64(v)) {
324
            /* More data after padding. */
325
0
            rv = -1;
326
0
            goto end;
327
0
        }
328
329
0
        if (eof > 2) {
330
0
            rv = -1;
331
0
            goto end;
332
0
        }
333
334
0
        if (v == B64_EOF) {
335
0
            seof = 1;
336
0
            goto tail;
337
0
        }
338
339
        /* Only save valid base64 characters. */
340
0
        if (B64_BASE64(v)) {
341
0
            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
0
            OPENSSL_assert(n < (int)sizeof(ctx->enc_data));
351
0
            d[n++] = tmp;
352
0
        }
353
354
0
        if (n == 64) {
355
0
            decoded_len = evp_decodeblock_int(ctx, out, d, n, eof);
356
0
            n = 0;
357
0
            if (decoded_len < 0 || (decoded_len == 0 && eof > 0)) {
358
0
                rv = -1;
359
0
                goto end;
360
0
            }
361
0
            ret += decoded_len;
362
0
            out += decoded_len;
363
0
        }
364
0
    }
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
0
tail:
372
0
    if (n > 0) {
373
0
        if ((n & 3) == 0) {
374
0
            decoded_len = evp_decodeblock_int(ctx, out, d, n, eof);
375
0
            n = 0;
376
0
            if (decoded_len < 0 || (decoded_len == 0 && eof > 0)) {
377
0
                rv = -1;
378
0
                goto end;
379
0
            }
380
0
            ret += decoded_len;
381
0
        } else if (seof) {
382
            /* EOF in the middle of a base64 block. */
383
0
            rv = -1;
384
0
            goto end;
385
0
        }
386
0
    }
387
388
0
    rv = seof || (n == 0 && eof) ? 0 : 1;
389
0
end:
390
    /* Legacy behaviour. This should probably rather be zeroed on error. */
391
0
    *outl = ret;
392
0
    ctx->num = n;
393
0
    return rv;
394
0
}
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
0
{
400
0
    int i, ret = 0, a, b, c, d;
401
0
    unsigned long l;
402
0
    const unsigned char *table;
403
404
0
    if (eof < -1 || eof > 2)
405
0
        return -1;
406
407
0
    if (ctx != NULL && (ctx->flags & EVP_ENCODE_CTX_USE_SRP_ALPHABET) != 0)
408
0
        table = srpdata_ascii2bin;
409
0
    else
410
0
        table = data_ascii2bin;
411
412
    /* trim whitespace from the start of the line. */
413
0
    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
0
    while ((n > 3) && (B64_NOT_BASE64(conv_ascii2bin(f[n - 1], table))))
423
0
        n--;
424
425
0
    if (n % 4 != 0)
426
0
        return -1;
427
0
    if (n == 0)
428
0
        return 0;
429
430
    /* all 4-byte blocks except the last one do not have padding. */
431
0
    for (i = 0; i < n - 4; i += 4) {
432
0
        a = conv_ascii2bin(*(f++), table);
433
0
        b = conv_ascii2bin(*(f++), table);
434
0
        c = conv_ascii2bin(*(f++), table);
435
0
        d = conv_ascii2bin(*(f++), table);
436
0
        if ((a | b | c | d) & 0x80)
437
0
            return -1;
438
0
        l = ((((unsigned long)a) << 18L) | (((unsigned long)b) << 12L) | (((unsigned long)c) << 6L) | (((unsigned long)d)));
439
0
        *(t++) = (unsigned char)(l >> 16L) & 0xff;
440
0
        *(t++) = (unsigned char)(l >> 8L) & 0xff;
441
0
        *(t++) = (unsigned char)(l) & 0xff;
442
0
        ret += 3;
443
0
    }
444
445
    /* process the last block that may have padding. */
446
0
    a = conv_ascii2bin(*(f++), table);
447
0
    b = conv_ascii2bin(*(f++), table);
448
0
    c = conv_ascii2bin(*(f++), table);
449
0
    d = conv_ascii2bin(*(f++), table);
450
0
    if ((a | b | c | d) & 0x80)
451
0
        return -1;
452
0
    l = ((((unsigned long)a) << 18L) | (((unsigned long)b) << 12L) | (((unsigned long)c) << 6L) | (((unsigned long)d)));
453
454
0
    if (eof == -1)
455
0
        eof = (c == '=') + (d == '=');
456
457
0
    switch (eof) {
458
0
    case 2:
459
0
        *t = (unsigned char)(l >> 16L) & 0xff;
460
0
        break;
461
0
    case 1:
462
0
        *(t++) = (unsigned char)(l >> 16L) & 0xff;
463
0
        *t = (unsigned char)(l >> 8L) & 0xff;
464
0
        break;
465
0
    case 0:
466
0
        *(t++) = (unsigned char)(l >> 16L) & 0xff;
467
0
        *(t++) = (unsigned char)(l >> 8L) & 0xff;
468
0
        *t = (unsigned char)(l) & 0xff;
469
0
        break;
470
0
    }
471
0
    ret += 3 - eof;
472
473
0
    return ret;
474
0
}
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
0
{
483
0
    int i;
484
485
0
    *outl = 0;
486
0
    if (ctx->num != 0) {
487
0
        i = evp_decodeblock_int(ctx, out, ctx->enc_data, ctx->num, -1);
488
0
        if (i < 0)
489
0
            return -1;
490
0
        ctx->num = 0;
491
0
        *outl = i;
492
0
        return 1;
493
0
    } else
494
0
        return 1;
495
0
}