157 lines
3.0 KiB
C
Raw Normal View History

2023-06-04 16:48:43 -07:00
/* $OpenBSD: bn_ctx.c,v 1.21 2023/04/25 16:41:29 tb Exp $ */
/*
* Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
*
2023-06-04 16:48:43 -07:00
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
2023-06-04 16:48:43 -07:00
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
2023-06-04 16:48:43 -07:00
#include <stddef.h>
#include <string.h>
#include <openssl/opensslconf.h>
#include <openssl/err.h>
2023-06-04 16:48:43 -07:00
#include "bn_local.h"
2023-06-04 16:48:43 -07:00
#define BN_CTX_INITIAL_LEN 8
struct bignum_ctx {
2023-06-04 16:48:43 -07:00
BIGNUM **bignums;
uint8_t *groups;
uint8_t group;
size_t index;
size_t len;
2023-06-04 16:48:43 -07:00
int error;
};
2023-06-04 16:48:43 -07:00
static int
bn_ctx_grow(BN_CTX *bctx)
{
2023-06-04 16:48:43 -07:00
BIGNUM **bignums = NULL;
uint8_t *groups = NULL;
size_t len;
2023-06-04 16:48:43 -07:00
if ((len = bctx->len) == 0) {
len = BN_CTX_INITIAL_LEN;
} else {
if (SIZE_MAX - len < len)
return 0;
len *= 2;
}
2023-06-04 16:48:43 -07:00
if ((bignums = recallocarray(bctx->bignums, bctx->len, len,
sizeof(bctx->bignums[0]))) == NULL)
return 0;
bctx->bignums = bignums;
2023-06-04 16:48:43 -07:00
if ((groups = reallocarray(bctx->groups, len,
sizeof(bctx->groups[0]))) == NULL)
return 0;
bctx->groups = groups;
2023-06-04 16:48:43 -07:00
bctx->len = len;
return 1;
}
BN_CTX *
BN_CTX_new(void)
{
2023-06-04 16:48:43 -07:00
return calloc(1, sizeof(struct bignum_ctx));
}
void
2023-06-04 16:48:43 -07:00
BN_CTX_free(BN_CTX *bctx)
{
2023-06-04 16:48:43 -07:00
size_t i;
if (bctx == NULL)
return;
2023-06-04 16:48:43 -07:00
for (i = 0; i < bctx->len; i++) {
BN_free(bctx->bignums[i]);
bctx->bignums[i] = NULL;
}
2023-06-04 16:48:43 -07:00
free(bctx->bignums);
free(bctx->groups);
2023-06-04 16:48:43 -07:00
freezero(bctx, sizeof(*bctx));
}
void
2023-06-04 16:48:43 -07:00
BN_CTX_start(BN_CTX *bctx)
{
2023-06-04 16:48:43 -07:00
bctx->group++;
2023-06-04 16:48:43 -07:00
if (bctx->group == 0) {
BNerror(BN_R_TOO_MANY_TEMPORARY_VARIABLES);
bctx->error = 1;
return;
}
}
BIGNUM *
2023-06-04 16:48:43 -07:00
BN_CTX_get(BN_CTX *bctx)
{
2023-06-04 16:48:43 -07:00
BIGNUM *bn = NULL;
2023-06-04 16:48:43 -07:00
if (bctx->error)
return NULL;
2023-06-04 16:48:43 -07:00
if (bctx->group == 0) {
BNerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
bctx->error = 1;
return NULL;
}
2023-06-04 16:48:43 -07:00
if (bctx->index == bctx->len) {
if (!bn_ctx_grow(bctx)) {
BNerror(BN_R_TOO_MANY_TEMPORARY_VARIABLES);
bctx->error = 1;
return NULL;
}
}
2023-06-04 16:48:43 -07:00
if ((bn = bctx->bignums[bctx->index]) == NULL) {
if ((bn = BN_new()) == NULL) {
BNerror(BN_R_TOO_MANY_TEMPORARY_VARIABLES);
bctx->error = 1;
return NULL;
}
2023-06-04 16:48:43 -07:00
bctx->bignums[bctx->index] = bn;
}
2023-06-04 16:48:43 -07:00
bctx->groups[bctx->index] = bctx->group;
bctx->index++;
BN_zero(bn);
return bn;
}
2023-06-04 16:48:43 -07:00
void
BN_CTX_end(BN_CTX *bctx)
{
2023-06-04 16:48:43 -07:00
if (bctx == NULL || bctx->error || bctx->group == 0)
return;
2023-06-04 16:48:43 -07:00
while (bctx->index > 0 && bctx->groups[bctx->index - 1] == bctx->group) {
BN_zero(bctx->bignums[bctx->index - 1]);
bctx->groups[bctx->index - 1] = 0;
bctx->index--;
}
2023-06-04 16:48:43 -07:00
bctx->group--;
}