]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/sparse_array.c
Update copyright year
[thirdparty/openssl.git] / crypto / sparse_array.c
CommitLineData
a40f0f64 1/*
3c2bdd7d 2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
a40f0f64
P
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>
54b5fb2d 12#include <openssl/bn.h>
25f2138b 13#include "crypto/sparse_array.h"
a40f0f64
P
14
15/*
c2969ff6 16 * How many bits are used to index each level in the tree structure?
a40f0f64
P
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
8ab53b19 32 * interval [2, sizeof(ossl_uintmax_t) * 8].
a40f0f64
P
33 */
34#ifndef OPENSSL_SA_BLOCK_BITS
35# ifdef OPENSSL_SMALL_FOOTPRINT
36# define OPENSSL_SA_BLOCK_BITS 4
37# else
38# define OPENSSL_SA_BLOCK_BITS 12
39# endif
e0ae0585 40#elif OPENSSL_SA_BLOCK_BITS < 2 || OPENSSL_SA_BLOCK_BITS > (BN_BITS2 - 1)
a40f0f64
P
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;
e5fee28f 47 * a bit mask to quickly extract an index and
a40f0f64
P
48 * the maximum depth of the tree structure.
49 */
50#define SA_BLOCK_MAX (1 << OPENSSL_SA_BLOCK_BITS)
51#define SA_BLOCK_MASK (SA_BLOCK_MAX - 1)
8ab53b19 52#define SA_BLOCK_MAX_LEVELS (((int)sizeof(ossl_uintmax_t) * 8 \
a40f0f64
P
53 + OPENSSL_SA_BLOCK_BITS - 1) \
54 / OPENSSL_SA_BLOCK_BITS)
55
56struct sparse_array_st {
57 int levels;
8ab53b19 58 ossl_uintmax_t top;
a40f0f64
P
59 size_t nelem;
60 void **nodes;
61};
62
ff0266ed 63OPENSSL_SA *ossl_sa_new(void)
a40f0f64
P
64{
65 OPENSSL_SA *res = OPENSSL_zalloc(sizeof(*res));
66
67 return res;
68}
69
70static void sa_doall(const OPENSSL_SA *sa, void (*node)(void **),
8ab53b19 71 void (*leaf)(ossl_uintmax_t, void *, void *), void *arg)
a40f0f64
P
72{
73 int i[SA_BLOCK_MAX_LEVELS];
74 void *nodes[SA_BLOCK_MAX_LEVELS];
8ab53b19 75 ossl_uintmax_t idx = 0;
a40f0f64
P
76 int l = 0;
77
78 i[0] = 0;
79 nodes[0] = sa->nodes;
80 while (l >= 0) {
81 const int n = i[l];
82 void ** const p = nodes[l];
83
84 if (n >= SA_BLOCK_MAX) {
85 if (p != NULL && node != NULL)
86 (*node)(p);
87 l--;
008b4ff9 88 idx >>= OPENSSL_SA_BLOCK_BITS;
a40f0f64
P
89 } else {
90 i[l] = n + 1;
91 if (p != NULL && p[n] != NULL) {
008b4ff9 92 idx = (idx & ~SA_BLOCK_MASK) | n;
a40f0f64
P
93 if (l < sa->levels - 1) {
94 i[++l] = 0;
95 nodes[l] = p[n];
008b4ff9 96 idx <<= OPENSSL_SA_BLOCK_BITS;
a40f0f64 97 } else if (leaf != NULL) {
008b4ff9 98 (*leaf)(idx, p[n], arg);
a40f0f64
P
99 }
100 }
101 }
102 }
103}
104
105static void sa_free_node(void **p)
106{
107 OPENSSL_free(p);
108}
109
8ab53b19 110static void sa_free_leaf(ossl_uintmax_t n, void *p, void *arg)
a40f0f64
P
111{
112 OPENSSL_free(p);
113}
114
ff0266ed 115void ossl_sa_free(OPENSSL_SA *sa)
a40f0f64
P
116{
117 sa_doall(sa, &sa_free_node, NULL, NULL);
118 OPENSSL_free(sa);
119}
120
ff0266ed 121void ossl_sa_free_leaves(OPENSSL_SA *sa)
a40f0f64
P
122{
123 sa_doall(sa, &sa_free_node, &sa_free_leaf, NULL);
124 OPENSSL_free(sa);
125}
126
127/* Wrap this in a structure to avoid compiler warnings */
128struct trampoline_st {
8ab53b19 129 void (*func)(ossl_uintmax_t, void *);
a40f0f64
P
130};
131
8ab53b19 132static void trampoline(ossl_uintmax_t n, void *l, void *arg)
a40f0f64 133{
008b4ff9 134 ((const struct trampoline_st *)arg)->func(n, l);
a40f0f64
P
135}
136
ff0266ed 137void ossl_sa_doall(const OPENSSL_SA *sa, void (*leaf)(ossl_uintmax_t, void *))
a40f0f64
P
138{
139 struct trampoline_st tramp;
140
141 tramp.func = leaf;
142 if (sa != NULL)
143 sa_doall(sa, NULL, &trampoline, &tramp);
144}
145
ff0266ed 146void ossl_sa_doall_arg(const OPENSSL_SA *sa,
8ab53b19
P
147 void (*leaf)(ossl_uintmax_t, void *, void *),
148 void *arg)
a40f0f64
P
149{
150 if (sa != NULL)
151 sa_doall(sa, NULL, leaf, arg);
152}
153
ff0266ed 154size_t ossl_sa_num(const OPENSSL_SA *sa)
a40f0f64
P
155{
156 return sa == NULL ? 0 : sa->nelem;
157}
158
ff0266ed 159void *ossl_sa_get(const OPENSSL_SA *sa, ossl_uintmax_t n)
a40f0f64
P
160{
161 int level;
162 void **p, *r = NULL;
163
04cb5ec0 164 if (sa == NULL || sa->nelem == 0)
a40f0f64
P
165 return NULL;
166
167 if (n <= sa->top) {
168 p = sa->nodes;
169 for (level = sa->levels - 1; p != NULL && level > 0; level--)
170 p = (void **)p[(n >> (OPENSSL_SA_BLOCK_BITS * level))
171 & SA_BLOCK_MASK];
172 r = p == NULL ? NULL : p[n & SA_BLOCK_MASK];
173 }
174 return r;
175}
176
177static ossl_inline void **alloc_node(void)
178{
179 return OPENSSL_zalloc(SA_BLOCK_MAX * sizeof(void *));
180}
181
ff0266ed 182int ossl_sa_set(OPENSSL_SA *sa, ossl_uintmax_t posn, void *val)
a40f0f64
P
183{
184 int i, level = 1;
8ab53b19 185 ossl_uintmax_t n = posn;
a40f0f64
P
186 void **p;
187
188 if (sa == NULL)
189 return 0;
190
1bdbdaff 191 for (level = 1; level < SA_BLOCK_MAX_LEVELS; level++)
a40f0f64
P
192 if ((n >>= OPENSSL_SA_BLOCK_BITS) == 0)
193 break;
194
195 for (;sa->levels < level; sa->levels++) {
196 p = alloc_node();
197 if (p == NULL)
198 return 0;
199 p[0] = sa->nodes;
200 sa->nodes = p;
201 }
202 if (sa->top < posn)
203 sa->top = posn;
204
205 p = sa->nodes;
206 for (level = sa->levels - 1; level > 0; level--) {
207 i = (posn >> (OPENSSL_SA_BLOCK_BITS * level)) & SA_BLOCK_MASK;
208 if (p[i] == NULL && (p[i] = alloc_node()) == NULL)
209 return 0;
210 p = p[i];
211 }
212 p += posn & SA_BLOCK_MASK;
213 if (val == NULL && *p != NULL)
214 sa->nelem--;
215 else if (val != NULL && *p == NULL)
216 sa->nelem++;
217 *p = val;
218 return 1;
219}