Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/buffer/buffer.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2020 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 "internal/cryptlib.h"
12
#include <openssl/buffer.h>
13
14
/*
15
 * LIMIT_BEFORE_EXPANSION is the maximum n such that (n+3)/3*4 < 2**31. That
16
 * function is applied in several functions in this file and this limit
17
 * ensures that the result fits in an int.
18
 */
19
37.8M
#define LIMIT_BEFORE_EXPANSION 0x5ffffffc
20
21
BUF_MEM *BUF_MEM_new_ex(unsigned long flags)
22
6.77M
{
23
6.77M
    BUF_MEM *ret;
24
25
6.77M
    ret = BUF_MEM_new();
26
6.77M
    if (ret != NULL)
27
6.77M
        ret->flags = flags;
28
6.77M
    return ret;
29
6.77M
}
30
31
BUF_MEM *BUF_MEM_new(void)
32
18.8M
{
33
18.8M
    BUF_MEM *ret;
34
35
18.8M
    ret = OPENSSL_zalloc(sizeof(*ret));
36
18.8M
    if (ret == NULL) {
37
0
        ERR_raise(ERR_LIB_BUF, ERR_R_MALLOC_FAILURE);
38
0
        return NULL;
39
0
    }
40
18.8M
    return ret;
41
18.8M
}
42
43
void BUF_MEM_free(BUF_MEM *a)
44
76.1M
{
45
76.1M
    if (a == NULL)
46
63.2M
        return;
47
12.8M
    if (a->data != NULL) {
48
4.95M
        if (a->flags & BUF_MEM_FLAG_SECURE)
49
1.41k
            OPENSSL_secure_clear_free(a->data, a->max);
50
4.95M
        else
51
4.95M
            OPENSSL_clear_free(a->data, a->max);
52
4.95M
    }
53
12.8M
    OPENSSL_free(a);
54
12.8M
}
55
56
/* Allocate a block of secure memory; copy over old data if there
57
 * was any, and then free it. */
58
static char *sec_alloc_realloc(BUF_MEM *str, size_t len)
59
6.84k
{
60
6.84k
    char *ret;
61
62
6.84k
    ret = OPENSSL_secure_malloc(len);
63
6.84k
    if (str->data != NULL) {
64
5.43k
        if (ret != NULL) {
65
5.43k
            memcpy(ret, str->data, str->length);
66
5.43k
            OPENSSL_secure_clear_free(str->data, str->length);
67
5.43k
            str->data = NULL;
68
5.43k
        }
69
5.43k
    }
70
6.84k
    return ret;
71
6.84k
}
72
73
size_t BUF_MEM_grow(BUF_MEM *str, size_t len)
74
45.1M
{
75
45.1M
    char *ret;
76
45.1M
    size_t n;
77
78
45.1M
    if (str->length >= len) {
79
37.8M
        str->length = len;
80
37.8M
        return len;
81
37.8M
    }
82
7.31M
    if (str->max >= len) {
83
4.03M
        if (str->data != NULL)
84
4.03M
            memset(&str->data[str->length], 0, len - str->length);
85
4.03M
        str->length = len;
86
4.03M
        return len;
87
4.03M
    }
88
    /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
89
3.27M
    if (len > LIMIT_BEFORE_EXPANSION) {
90
0
        ERR_raise(ERR_LIB_BUF, ERR_R_MALLOC_FAILURE);
91
0
        return 0;
92
0
    }
93
3.27M
    n = (len + 3) / 3 * 4;
94
3.27M
    if ((str->flags & BUF_MEM_FLAG_SECURE))
95
0
        ret = sec_alloc_realloc(str, n);
96
3.27M
    else
97
3.27M
        ret = OPENSSL_realloc(str->data, n);
98
3.27M
    if (ret == NULL) {
99
0
        ERR_raise(ERR_LIB_BUF, ERR_R_MALLOC_FAILURE);
100
0
        len = 0;
101
3.27M
    } else {
102
3.27M
        str->data = ret;
103
3.27M
        str->max = n;
104
3.27M
        memset(&str->data[str->length], 0, len - str->length);
105
3.27M
        str->length = len;
106
3.27M
    }
107
3.27M
    return len;
108
3.27M
}
109
110
size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len)
111
2.27G
{
112
2.27G
    char *ret;
113
2.27G
    size_t n;
114
115
2.27G
    if (str->length >= len) {
116
1.95M
        if (str->data != NULL)
117
1.95M
            memset(&str->data[len], 0, str->length - len);
118
1.95M
        str->length = len;
119
1.95M
        return len;
120
1.95M
    }
121
2.27G
    if (str->max >= len) {
122
2.24G
        memset(&str->data[str->length], 0, len - str->length);
123
2.24G
        str->length = len;
124
2.24G
        return len;
125
2.24G
    }
126
    /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
127
34.5M
    if (len > LIMIT_BEFORE_EXPANSION) {
128
0
        ERR_raise(ERR_LIB_BUF, ERR_R_MALLOC_FAILURE);
129
0
        return 0;
130
0
    }
131
34.5M
    n = (len + 3) / 3 * 4;
132
34.5M
    if ((str->flags & BUF_MEM_FLAG_SECURE))
133
6.84k
        ret = sec_alloc_realloc(str, n);
134
34.5M
    else
135
34.5M
        ret = OPENSSL_clear_realloc(str->data, str->max, n);
136
34.5M
    if (ret == NULL) {
137
0
        ERR_raise(ERR_LIB_BUF, ERR_R_MALLOC_FAILURE);
138
0
        len = 0;
139
34.5M
    } else {
140
34.5M
        str->data = ret;
141
34.5M
        str->max = n;
142
34.5M
        memset(&str->data[str->length], 0, len - str->length);
143
34.5M
        str->length = len;
144
34.5M
    }
145
34.5M
    return len;
146
34.5M
}
147
148
void BUF_reverse(unsigned char *out, const unsigned char *in, size_t size)
149
0
{
150
0
    size_t i;
151
0
    if (in) {
152
0
        out += size - 1;
153
0
        for (i = 0; i < size; i++)
154
0
            *out-- = *in++;
155
0
    } else {
156
0
        unsigned char *q;
157
0
        char c;
158
0
        q = out + size - 1;
159
0
        for (i = 0; i < size / 2; i++) {
160
0
            c = *q;
161
0
            *q-- = *out;
162
0
            *out++ = c;
163
0
        }
164
0
    }
165
0
}