Coverage Report

Created: 2024-05-15 07:14

/src/openssl/crypto/sparse_array.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
#include <openssl/crypto.h>
12
#include <openssl/bn.h>
13
#include "crypto/sparse_array.h"
14
15
/*
16
 * How many bits are used to index each level in the tree structure?
17
 * This setting determines the number of pointers stored in each node of the
18
 * tree used to represent the sparse array.  Having more pointers reduces the
19
 * depth of the tree but potentially wastes more memory.  That is, this is a
20
 * direct space versus time tradeoff.
21
 *
22
 * The large memory model uses twelve bits which means that the are 4096
23
 * pointers in each tree node.  This is more than sufficient to hold the
24
 * largest defined NID (as of Feb 2019).  This means that using a NID to
25
 * index a sparse array becomes a constant time single array look up.
26
 *
27
 * The small memory model uses four bits which means the tree nodes contain
28
 * sixteen pointers.  This reduces the amount of unused space significantly
29
 * at a cost in time.
30
 *
31
 * The library builder is also permitted to define other sizes in the closed
32
 * interval [2, sizeof(ossl_uintmax_t) * 8].
33
 */
34
#ifndef OPENSSL_SA_BLOCK_BITS
35
# ifdef OPENSSL_SMALL_FOOTPRINT
36
#  define OPENSSL_SA_BLOCK_BITS           4
37
# else
38
0
#  define OPENSSL_SA_BLOCK_BITS           12
39
# endif
40
#elif OPENSSL_SA_BLOCK_BITS < 2 || OPENSSL_SA_BLOCK_BITS > (BN_BITS2 - 1)
41
# error OPENSSL_SA_BLOCK_BITS is out of range
42
#endif
43
44
/*
45
 * From the number of bits, work out:
46
 *    the number of pointers in a tree node;
47
 *    a bit mask to quickly extract an index and
48
 *    the maximum depth of the tree structure.
49
  */
50
0
#define SA_BLOCK_MAX            (1 << OPENSSL_SA_BLOCK_BITS)
51
0
#define SA_BLOCK_MASK           (SA_BLOCK_MAX - 1)
52
0
#define SA_BLOCK_MAX_LEVELS     (((int)sizeof(ossl_uintmax_t) * 8 \
53
0
                                  + OPENSSL_SA_BLOCK_BITS - 1) \
54
0
                                 / OPENSSL_SA_BLOCK_BITS)
55
56
struct sparse_array_st {
57
    int levels;
58
    ossl_uintmax_t top;
59
    size_t nelem;
60
    void **nodes;
61
};
62
63
OPENSSL_SA *OPENSSL_SA_new(void)
64
0
{
65
0
    OPENSSL_SA *res = OPENSSL_zalloc(sizeof(*res));
66
67
0
    return res;
68
0
}
69
70
static void sa_doall(const OPENSSL_SA *sa, void (*node)(void **),
71
                     void (*leaf)(ossl_uintmax_t, void *, void *), void *arg)
72
0
{
73
0
    int i[SA_BLOCK_MAX_LEVELS];
74
0
    void *nodes[SA_BLOCK_MAX_LEVELS];
75
0
    ossl_uintmax_t idx = 0;
76
0
    int l = 0;
77
78
0
    i[0] = 0;
79
0
    nodes[0] = sa->nodes;
80
0
    while (l >= 0) {
81
0
        const int n = i[l];
82
0
        void ** const p = nodes[l];
83
84
0
        if (n >= SA_BLOCK_MAX) {
85
0
            if (p != NULL && node != NULL)
86
0
                (*node)(p);
87
0
            l--;
88
0
            idx >>= OPENSSL_SA_BLOCK_BITS;
89
0
        } else {
90
0
            i[l] = n + 1;
91
0
            if (p != NULL && p[n] != NULL) {
92
0
                idx = (idx & ~SA_BLOCK_MASK) | n;
93
0
                if (l < sa->levels - 1) {
94
0
                    i[++l] = 0;
95
0
                    nodes[l] = p[n];
96
0
                    idx <<= OPENSSL_SA_BLOCK_BITS;
97
0
                } else if (leaf != NULL) {
98
0
                    (*leaf)(idx, p[n], arg);
99
0
                }
100
0
            }
101
0
        }
102
0
    }
103
0
}
104
105
static void sa_free_node(void **p)
106
0
{
107
0
    OPENSSL_free(p);
108
0
}
109
110
static void sa_free_leaf(ossl_uintmax_t n, void *p, void *arg)
111
0
{
112
0
    OPENSSL_free(p);
113
0
}
114
115
void OPENSSL_SA_free(OPENSSL_SA *sa)
116
0
{
117
0
    sa_doall(sa, &sa_free_node, NULL, NULL);
118
0
    OPENSSL_free(sa);
119
0
}
120
121
void OPENSSL_SA_free_leaves(OPENSSL_SA *sa)
122
0
{
123
0
    sa_doall(sa, &sa_free_node, &sa_free_leaf, NULL);
124
0
    OPENSSL_free(sa);
125
0
}
126
127
/* Wrap this in a structure to avoid compiler warnings */
128
struct trampoline_st {
129
    void (*func)(ossl_uintmax_t, void *);
130
};
131
132
static void trampoline(ossl_uintmax_t n, void *l, void *arg)
133
0
{
134
0
    ((const struct trampoline_st *)arg)->func(n, l);
135
0
}
136
137
void OPENSSL_SA_doall(const OPENSSL_SA *sa, void (*leaf)(ossl_uintmax_t,
138
                                                         void *))
139
0
{
140
0
    struct trampoline_st tramp;
141
142
0
    tramp.func = leaf;
143
0
    if (sa != NULL)
144
0
        sa_doall(sa, NULL, &trampoline, &tramp);
145
0
}
146
147
void OPENSSL_SA_doall_arg(const OPENSSL_SA *sa,
148
                          void (*leaf)(ossl_uintmax_t, void *, void *),
149
                          void *arg)
150
0
{
151
0
    if (sa != NULL)
152
0
        sa_doall(sa, NULL, leaf, arg);
153
0
}
154
155
size_t OPENSSL_SA_num(const OPENSSL_SA *sa)
156
0
{
157
0
    return sa == NULL ? 0 : sa->nelem;
158
0
}
159
160
void *OPENSSL_SA_get(const OPENSSL_SA *sa, ossl_uintmax_t n)
161
0
{
162
0
    int level;
163
0
    void **p, *r = NULL;
164
165
0
    if (sa == NULL)
166
0
        return NULL;
167
168
0
    if (n <= sa->top) {
169
0
        p = sa->nodes;
170
0
        for (level = sa->levels - 1; p != NULL && level > 0; level--)
171
0
            p = (void **)p[(n >> (OPENSSL_SA_BLOCK_BITS * level))
172
0
                           & SA_BLOCK_MASK];
173
0
        r = p == NULL ? NULL : p[n & SA_BLOCK_MASK];
174
0
    }
175
0
    return r;
176
0
}
177
178
static ossl_inline void **alloc_node(void)
179
0
{
180
0
    return OPENSSL_zalloc(SA_BLOCK_MAX * sizeof(void *));
181
0
}
182
183
int OPENSSL_SA_set(OPENSSL_SA *sa, ossl_uintmax_t posn, void *val)
184
0
{
185
0
    int i, level = 1;
186
0
    ossl_uintmax_t n = posn;
187
0
    void **p;
188
189
0
    if (sa == NULL)
190
0
        return 0;
191
192
0
    for (level = 1; level < SA_BLOCK_MAX_LEVELS; level++)
193
0
        if ((n >>= OPENSSL_SA_BLOCK_BITS) == 0)
194
0
            break;
195
196
0
    for (;sa->levels < level; sa->levels++) {
197
0
        p = alloc_node();
198
0
        if (p == NULL)
199
0
            return 0;
200
0
        p[0] = sa->nodes;
201
0
        sa->nodes = p;
202
0
    }
203
0
    if (sa->top < posn)
204
0
        sa->top = posn;
205
206
0
    p = sa->nodes;
207
0
    for (level = sa->levels - 1; level > 0; level--) {
208
0
        i = (posn >> (OPENSSL_SA_BLOCK_BITS * level)) & SA_BLOCK_MASK;
209
0
        if (p[i] == NULL && (p[i] = alloc_node()) == NULL)
210
0
            return 0;
211
0
        p = p[i];
212
0
    }
213
0
    p += posn & SA_BLOCK_MASK;
214
0
    if (val == NULL && *p != NULL)
215
0
        sa->nelem--;
216
0
    else if (val != NULL && *p == NULL)
217
0
        sa->nelem++;
218
0
    *p = val;
219
0
    return 1;
220
0
}