2 * Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/cryptlib.h"
16 * 1. Check a bunch of "(words+1)" type hacks in various bignum functions and
17 * check they can be safely removed.
18 * - Check +1 and other ugliness in BN_from_montgomery()
20 * 2. Consider allowing a BN_new_ex() that, at least, lets you specify an
21 * appropriate 'block' size that will be honoured by bn_expand_internal() to
22 * prevent piddly little reallocations. OTOH, profiling bignum expansions in
23 * BN_CTX doesn't show this to be a big issue.
26 /* How many bignums are in each "pool item"; */
27 #define BN_CTX_POOL_SIZE 16
28 /* The stack frame info is resizing, set a first-time expansion size; */
29 #define BN_CTX_START_FRAMES 32
35 /* A bundle of bignums that can be linked with other bundles */
36 typedef struct bignum_pool_item
{
37 /* The bignum values */
38 BIGNUM vals
[BN_CTX_POOL_SIZE
];
39 /* Linked-list admin */
40 struct bignum_pool_item
*prev
, *next
;
42 /* A linked-list of bignums grouped in bundles */
43 typedef struct bignum_pool
{
44 /* Linked-list admin */
45 BN_POOL_ITEM
*head
, *current
, *tail
;
46 /* Stack depth and allocation size */
49 static void BN_POOL_init(BN_POOL
*);
50 static void BN_POOL_finish(BN_POOL
*);
51 static BIGNUM
*BN_POOL_get(BN_POOL
*, int);
52 static void BN_POOL_release(BN_POOL
*, unsigned int);
58 /* A wrapper to manage the "stack frames" */
59 typedef struct bignum_ctx_stack
{
60 /* Array of indexes into the bignum stack */
61 unsigned int *indexes
;
62 /* Number of stack frames, and the size of the allocated array */
63 unsigned int depth
, size
;
65 static void BN_STACK_init(BN_STACK
*);
66 static void BN_STACK_finish(BN_STACK
*);
67 static int BN_STACK_push(BN_STACK
*, unsigned int);
68 static unsigned int BN_STACK_pop(BN_STACK
*);
74 /* The opaque BN_CTX type */
76 /* The bignum bundles */
78 /* The "stack frames", if you will */
80 /* The number of bignums currently assigned */
82 /* Depth of stack overflow */
84 /* Block "gets" until an "end" (compatibility behaviour) */
90 /* Enable this to find BN_CTX bugs */
92 static const char *ctxdbg_cur
= NULL
;
93 static void ctxdbg(BN_CTX
*ctx
)
95 unsigned int bnidx
= 0, fpidx
= 0;
96 BN_POOL_ITEM
*item
= ctx
->pool
.head
;
97 BN_STACK
*stack
= &ctx
->stack
;
98 fprintf(stderr
, "(%16p): ", ctx
);
99 while (bnidx
< ctx
->used
) {
100 fprintf(stderr
, "%03x ", item
->vals
[bnidx
++ % BN_CTX_POOL_SIZE
].dmax
);
101 if (!(bnidx
% BN_CTX_POOL_SIZE
))
104 fprintf(stderr
, "\n");
106 fprintf(stderr
, " : ");
107 while (fpidx
< stack
->depth
) {
108 while (bnidx
++ < stack
->indexes
[fpidx
])
109 fprintf(stderr
, " ");
110 fprintf(stderr
, "^^^ ");
114 fprintf(stderr
, "\n");
117 # define CTXDBG_ENTRY(str, ctx) do { \
118 ctxdbg_cur = (str); \
119 fprintf(stderr,"Starting %s\n", ctxdbg_cur); \
122 # define CTXDBG_EXIT(ctx) do { \
123 fprintf(stderr,"Ending %s\n", ctxdbg_cur); \
126 # define CTXDBG_RET(ctx,ret)
128 # define CTXDBG_ENTRY(str, ctx)
129 # define CTXDBG_EXIT(ctx)
130 # define CTXDBG_RET(ctx,ret)
134 BN_CTX
*BN_CTX_new(void)
138 if ((ret
= OPENSSL_zalloc(sizeof(*ret
))) == NULL
) {
139 BNerr(BN_F_BN_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
142 /* Initialise the structure */
143 BN_POOL_init(&ret
->pool
);
144 BN_STACK_init(&ret
->stack
);
148 BN_CTX
*BN_CTX_secure_new(void)
150 BN_CTX
*ret
= BN_CTX_new();
153 ret
->flags
= BN_FLG_SECURE
;
157 void BN_CTX_free(BN_CTX
*ctx
)
163 BN_POOL_ITEM
*pool
= ctx
->pool
.head
;
164 fprintf(stderr
, "BN_CTX_free, stack-size=%d, pool-bignums=%d\n",
165 ctx
->stack
.size
, ctx
->pool
.size
);
166 fprintf(stderr
, "dmaxs: ");
169 while (loop
< BN_CTX_POOL_SIZE
)
170 fprintf(stderr
, "%02x ", pool
->vals
[loop
++].dmax
);
173 fprintf(stderr
, "\n");
176 BN_STACK_finish(&ctx
->stack
);
177 BN_POOL_finish(&ctx
->pool
);
181 void BN_CTX_start(BN_CTX
*ctx
)
183 CTXDBG_ENTRY("BN_CTX_start", ctx
);
184 /* If we're already overflowing ... */
185 if (ctx
->err_stack
|| ctx
->too_many
)
187 /* (Try to) get a new frame pointer */
188 else if (!BN_STACK_push(&ctx
->stack
, ctx
->used
)) {
189 BNerr(BN_F_BN_CTX_START
, BN_R_TOO_MANY_TEMPORARY_VARIABLES
);
195 void BN_CTX_end(BN_CTX
*ctx
)
197 CTXDBG_ENTRY("BN_CTX_end", ctx
);
201 unsigned int fp
= BN_STACK_pop(&ctx
->stack
);
202 /* Does this stack frame have anything to release? */
204 BN_POOL_release(&ctx
->pool
, ctx
->used
- fp
);
206 /* Unjam "too_many" in case "get" had failed */
212 BIGNUM
*BN_CTX_get(BN_CTX
*ctx
)
216 CTXDBG_ENTRY("BN_CTX_get", ctx
);
217 if (ctx
->err_stack
|| ctx
->too_many
)
219 if ((ret
= BN_POOL_get(&ctx
->pool
, ctx
->flags
)) == NULL
) {
221 * Setting too_many prevents repeated "get" attempts from cluttering
225 BNerr(BN_F_BN_CTX_GET
, BN_R_TOO_MANY_TEMPORARY_VARIABLES
);
228 /* OK, make sure the returned bignum is "zero" */
230 /* clear BN_FLG_CONSTTIME if leaked from previous frames */
231 ret
->flags
&= (~BN_FLG_CONSTTIME
);
233 CTXDBG_RET(ctx
, ret
);
241 static void BN_STACK_init(BN_STACK
*st
)
244 st
->depth
= st
->size
= 0;
247 static void BN_STACK_finish(BN_STACK
*st
)
249 OPENSSL_free(st
->indexes
);
254 static int BN_STACK_push(BN_STACK
*st
, unsigned int idx
)
256 if (st
->depth
== st
->size
) {
258 unsigned int newsize
=
259 st
->size
? (st
->size
* 3 / 2) : BN_CTX_START_FRAMES
;
260 unsigned int *newitems
;
262 if ((newitems
= OPENSSL_malloc(sizeof(*newitems
) * newsize
)) == NULL
) {
263 BNerr(BN_F_BN_STACK_PUSH
, ERR_R_MALLOC_FAILURE
);
267 memcpy(newitems
, st
->indexes
, sizeof(*newitems
) * st
->depth
);
268 OPENSSL_free(st
->indexes
);
269 st
->indexes
= newitems
;
272 st
->indexes
[(st
->depth
)++] = idx
;
276 static unsigned int BN_STACK_pop(BN_STACK
*st
)
278 return st
->indexes
[--(st
->depth
)];
285 static void BN_POOL_init(BN_POOL
*p
)
287 p
->head
= p
->current
= p
->tail
= NULL
;
288 p
->used
= p
->size
= 0;
291 static void BN_POOL_finish(BN_POOL
*p
)
297 for (loop
= 0, bn
= p
->head
->vals
; loop
++ < BN_CTX_POOL_SIZE
; bn
++)
300 p
->current
= p
->head
->next
;
301 OPENSSL_free(p
->head
);
302 p
->head
= p
->current
;
307 static BIGNUM
*BN_POOL_get(BN_POOL
*p
, int flag
)
312 /* Full; allocate a new pool item and link it in. */
313 if (p
->used
== p
->size
) {
316 if ((item
= OPENSSL_malloc(sizeof(*item
))) == NULL
) {
317 BNerr(BN_F_BN_POOL_GET
, ERR_R_MALLOC_FAILURE
);
320 for (loop
= 0, bn
= item
->vals
; loop
++ < BN_CTX_POOL_SIZE
; bn
++) {
322 if ((flag
& BN_FLG_SECURE
) != 0)
323 BN_set_flags(bn
, BN_FLG_SECURE
);
325 item
->prev
= p
->tail
;
329 p
->head
= p
->current
= p
->tail
= item
;
331 p
->tail
->next
= item
;
335 p
->size
+= BN_CTX_POOL_SIZE
;
337 /* Return the first bignum from the new pool */
342 p
->current
= p
->head
;
343 else if ((p
->used
% BN_CTX_POOL_SIZE
) == 0)
344 p
->current
= p
->current
->next
;
345 return p
->current
->vals
+ ((p
->used
++) % BN_CTX_POOL_SIZE
);
348 static void BN_POOL_release(BN_POOL
*p
, unsigned int num
)
350 unsigned int offset
= (p
->used
- 1) % BN_CTX_POOL_SIZE
;
354 bn_check_top(p
->current
->vals
+ offset
);
356 offset
= BN_CTX_POOL_SIZE
- 1;
357 p
->current
= p
->current
->prev
;