Coverage Report

Created: 2024-11-21 07:03

/src/boringssl/crypto/fipsmodule/bn/ctx.c.inc
Line
Count
Source (jump to first uncovered line)
1
/* Written by Ulf Moeller for the OpenSSL project. */
2
/* ====================================================================
3
 * Copyright (c) 1998-2004 The OpenSSL Project.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer. 
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in
14
 *    the documentation and/or other materials provided with the
15
 *    distribution.
16
 *
17
 * 3. All advertising materials mentioning features or use of this
18
 *    software must display the following acknowledgment:
19
 *    "This product includes software developed by the OpenSSL Project
20
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21
 *
22
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23
 *    endorse or promote products derived from this software without
24
 *    prior written permission. For written permission, please contact
25
 *    openssl-core@openssl.org.
26
 *
27
 * 5. Products derived from this software may not be called "OpenSSL"
28
 *    nor may "OpenSSL" appear in their names without prior written
29
 *    permission of the OpenSSL Project.
30
 *
31
 * 6. Redistributions of any form whatsoever must retain the following
32
 *    acknowledgment:
33
 *    "This product includes software developed by the OpenSSL Project
34
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35
 *
36
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47
 * OF THE POSSIBILITY OF SUCH DAMAGE.
48
 * ====================================================================
49
 *
50
 * This product includes cryptographic software written by Eric Young
51
 * (eay@cryptsoft.com).  This product includes software written by Tim
52
 * Hudson (tjh@cryptsoft.com). */
53
54
55
#include <openssl/bn.h>
56
57
#include <assert.h>
58
#include <string.h>
59
60
#include <openssl/err.h>
61
#include <openssl/mem.h>
62
63
#include "../../internal.h"
64
65
66
// The stack frame info is resizing, set a first-time expansion size;
67
1.39k
#define BN_CTX_START_FRAMES 32
68
69
70
// BN_STACK
71
72
// A |BN_STACK| is a stack of |size_t| values.
73
typedef struct {
74
  // Array of indexes into |ctx->bignums|.
75
  size_t *indexes;
76
  // Number of stack frames, and the size of the allocated array
77
  size_t depth, size;
78
} BN_STACK;
79
80
static void BN_STACK_init(BN_STACK *);
81
static void BN_STACK_cleanup(BN_STACK *);
82
static int BN_STACK_push(BN_STACK *, size_t idx);
83
static size_t BN_STACK_pop(BN_STACK *);
84
85
86
// BN_CTX
87
88
DEFINE_STACK_OF(BIGNUM)
89
90
// The opaque BN_CTX type
91
struct bignum_ctx {
92
  // bignums is the stack of |BIGNUM|s managed by this |BN_CTX|.
93
  STACK_OF(BIGNUM) *bignums;
94
  // stack is the stack of |BN_CTX_start| frames. It is the value of |used| at
95
  // the time |BN_CTX_start| was called.
96
  BN_STACK stack;
97
  // used is the number of |BIGNUM|s from |bignums| that have been used.
98
  size_t used;
99
  // error is one if any operation on this |BN_CTX| failed. All subsequent
100
  // operations will fail.
101
  char error;
102
  // defer_error is one if an operation on this |BN_CTX| has failed, but no
103
  // error has been pushed to the queue yet. This is used to defer errors from
104
  // |BN_CTX_start| to |BN_CTX_get|.
105
  char defer_error;
106
};
107
108
3.12k
BN_CTX *BN_CTX_new(void) {
109
3.12k
  BN_CTX *ret = OPENSSL_malloc(sizeof(BN_CTX));
110
3.12k
  if (!ret) {
111
0
    return NULL;
112
0
  }
113
114
  // Initialise the structure
115
3.12k
  ret->bignums = NULL;
116
3.12k
  BN_STACK_init(&ret->stack);
117
3.12k
  ret->used = 0;
118
3.12k
  ret->error = 0;
119
3.12k
  ret->defer_error = 0;
120
3.12k
  return ret;
121
3.12k
}
122
123
3.53k
void BN_CTX_free(BN_CTX *ctx) {
124
3.53k
  if (ctx == NULL) {
125
413
    return;
126
413
  }
127
128
  // All |BN_CTX_start| calls must be matched with |BN_CTX_end|, otherwise the
129
  // function may use more memory than expected, potentially without bound if
130
  // done in a loop. Assert that all |BIGNUM|s have been released.
131
3.12k
  assert(ctx->used == 0 || ctx->error);
132
3.12k
  sk_BIGNUM_pop_free(ctx->bignums, BN_free);
133
3.12k
  BN_STACK_cleanup(&ctx->stack);
134
3.12k
  OPENSSL_free(ctx);
135
3.12k
}
136
137
6.30M
void BN_CTX_start(BN_CTX *ctx) {
138
6.30M
  if (ctx->error) {
139
    // Once an operation has failed, |ctx->stack| no longer matches the number
140
    // of |BN_CTX_end| calls to come. Do nothing.
141
0
    return;
142
0
  }
143
144
6.30M
  if (!BN_STACK_push(&ctx->stack, ctx->used)) {
145
0
    ctx->error = 1;
146
    // |BN_CTX_start| cannot fail, so defer the error to |BN_CTX_get|.
147
0
    ctx->defer_error = 1;
148
0
  }
149
6.30M
}
150
151
6.96M
BIGNUM *BN_CTX_get(BN_CTX *ctx) {
152
  // Once any operation has failed, they all do.
153
6.96M
  if (ctx->error) {
154
0
    if (ctx->defer_error) {
155
0
      OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
156
0
      ctx->defer_error = 0;
157
0
    }
158
0
    return NULL;
159
0
  }
160
161
6.96M
  if (ctx->bignums == NULL) {
162
1.39k
    ctx->bignums = sk_BIGNUM_new_null();
163
1.39k
    if (ctx->bignums == NULL) {
164
0
      ctx->error = 1;
165
0
      return NULL;
166
0
    }
167
1.39k
  }
168
169
6.96M
  if (ctx->used == sk_BIGNUM_num(ctx->bignums)) {
170
12.1k
    BIGNUM *bn = BN_new();
171
12.1k
    if (bn == NULL || !sk_BIGNUM_push(ctx->bignums, bn)) {
172
0
      OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
173
0
      BN_free(bn);
174
0
      ctx->error = 1;
175
0
      return NULL;
176
0
    }
177
12.1k
  }
178
179
6.96M
  BIGNUM *ret = sk_BIGNUM_value(ctx->bignums, ctx->used);
180
6.96M
  BN_zero(ret);
181
  // This is bounded by |sk_BIGNUM_num|, so it cannot overflow.
182
6.96M
  ctx->used++;
183
6.96M
  return ret;
184
6.96M
}
185
186
6.30M
void BN_CTX_end(BN_CTX *ctx) {
187
6.30M
  if (ctx->error) {
188
    // Once an operation has failed, |ctx->stack| no longer matches the number
189
    // of |BN_CTX_end| calls to come. Do nothing.
190
0
    return;
191
0
  }
192
193
6.30M
  ctx->used = BN_STACK_pop(&ctx->stack);
194
6.30M
}
195
196
197
// BN_STACK
198
199
3.12k
static void BN_STACK_init(BN_STACK *st) {
200
3.12k
  st->indexes = NULL;
201
3.12k
  st->depth = st->size = 0;
202
3.12k
}
203
204
3.12k
static void BN_STACK_cleanup(BN_STACK *st) {
205
3.12k
  OPENSSL_free(st->indexes);
206
3.12k
}
207
208
6.30M
static int BN_STACK_push(BN_STACK *st, size_t idx) {
209
6.30M
  if (st->depth == st->size) {
210
    // This function intentionally does not push to the error queue on error.
211
    // Error-reporting is deferred to |BN_CTX_get|.
212
1.39k
    size_t new_size = st->size != 0 ? st->size * 3 / 2 : BN_CTX_START_FRAMES;
213
1.39k
    if (new_size <= st->size || new_size > SIZE_MAX / sizeof(size_t)) {
214
0
      return 0;
215
0
    }
216
1.39k
    size_t *new_indexes =
217
1.39k
        OPENSSL_realloc(st->indexes, new_size * sizeof(size_t));
218
1.39k
    if (new_indexes == NULL) {
219
0
      return 0;
220
0
    }
221
1.39k
    st->indexes = new_indexes;
222
1.39k
    st->size = new_size;
223
1.39k
  }
224
225
6.30M
  st->indexes[st->depth] = idx;
226
6.30M
  st->depth++;
227
6.30M
  return 1;
228
6.30M
}
229
230
6.30M
static size_t BN_STACK_pop(BN_STACK *st) {
231
6.30M
  assert(st->depth > 0);
232
6.30M
  st->depth--;
233
6.30M
  return st->indexes[st->depth];
234
6.30M
}