Commit 7bc081dd authored by Matt Caswell's avatar Matt Caswell
Browse files

Create BN_CTX_new_ex() and BN_CTX_secure_new_ex()



These variants of BN_CTX_new() and BN_CTX_secure_new() enable passing
an OPENSSL_CTX so that we can access this where needed throughout the
BIGNUM sub library.

Reviewed-by: default avatarRichard Levitte <levitte@openssl.org>
Reviewed-by: default avatarShane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9130)
parent f35819d1
Loading
Loading
Loading
Loading
+17 −5
Original line number Diff line number Diff line
@@ -86,6 +86,8 @@ struct bignum_ctx {
    int too_many;
    /* Flags. */
    int flags;
    /* The library context */
    OPENSSL_CTX *libctx;
};

/* Debugging functionality */
@@ -121,30 +123,40 @@ static void ctxdbg(BIO *channel, const char *text, BN_CTX *ctx)
        ctxdbg(trc_out, str, ctx);  \
    } OSSL_TRACE_END(BN_CTX)


BN_CTX *BN_CTX_new(void)
BN_CTX *BN_CTX_new_ex(OPENSSL_CTX *ctx)
{
    BN_CTX *ret;

    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
        BNerr(BN_F_BN_CTX_NEW, ERR_R_MALLOC_FAILURE);
        BNerr(BN_F_BN_CTX_NEW_EX, ERR_R_MALLOC_FAILURE);
        return NULL;
    }
    /* Initialise the structure */
    BN_POOL_init(&ret->pool);
    BN_STACK_init(&ret->stack);
    ret->libctx = ctx;
    return ret;
}

BN_CTX *BN_CTX_secure_new(void)
BN_CTX *BN_CTX_new(void)
{
    BN_CTX *ret = BN_CTX_new();
    return BN_CTX_new_ex(NULL);
}

BN_CTX *BN_CTX_secure_new_ex(OPENSSL_CTX *ctx)
{
    BN_CTX *ret = BN_CTX_new_ex(ctx);

    if (ret != NULL)
        ret->flags = BN_FLG_SECURE;
    return ret;
}

BN_CTX *BN_CTX_secure_new(void)
{
    return BN_CTX_secure_new_ex(NULL);
}

void BN_CTX_free(BN_CTX *ctx)
{
    if (ctx == NULL)
+2 −1
Original line number Diff line number Diff line
/*
 * Generated by util/mkerr.pl DO NOT EDIT
 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
@@ -29,6 +29,7 @@ static const ERR_STRING_DATA BN_str_functs[] = {
    {ERR_PACK(ERR_LIB_BN, BN_F_BN_COMPUTE_WNAF, 0), "bn_compute_wNAF"},
    {ERR_PACK(ERR_LIB_BN, BN_F_BN_CTX_GET, 0), "BN_CTX_get"},
    {ERR_PACK(ERR_LIB_BN, BN_F_BN_CTX_NEW, 0), "BN_CTX_new"},
    {ERR_PACK(ERR_LIB_BN, BN_F_BN_CTX_NEW_EX, 0), "BN_CTX_new_ex"},
    {ERR_PACK(ERR_LIB_BN, BN_F_BN_CTX_START, 0), "BN_CTX_start"},
    {ERR_PACK(ERR_LIB_BN, BN_F_BN_DIV, 0), "BN_div"},
    {ERR_PACK(ERR_LIB_BN, BN_F_BN_DIV_RECP, 0), "BN_div_recp"},
+1 −0
Original line number Diff line number Diff line
@@ -196,6 +196,7 @@ BN_F_BN_BN2HEX:105:BN_bn2hex
BN_F_BN_COMPUTE_WNAF:142:bn_compute_wNAF
BN_F_BN_CTX_GET:116:BN_CTX_get
BN_F_BN_CTX_NEW:106:BN_CTX_new
BN_F_BN_CTX_NEW_EX:151:BN_CTX_new_ex
BN_F_BN_CTX_START:129:BN_CTX_start
BN_F_BN_DIV:107:BN_div
BN_F_BN_DIV_RECP:130:BN_div_recp
+14 −4
Original line number Diff line number Diff line
@@ -2,14 +2,17 @@

=head1 NAME

BN_CTX_new, BN_CTX_secure_new, BN_CTX_free - allocate and free BN_CTX structures
BN_CTX_new_ex, BN_CTX_new, BN_CTX_secure_new_ex, BN_CTX_secure_new, BN_CTX_free
- allocate and free BN_CTX structures

=head1 SYNOPSIS

 #include <openssl/bn.h>

 BN_CTX *BN_CTX_new_ex(OPENSSL_CTX *ctx);
 BN_CTX *BN_CTX_new(void);

 BN_CTX *BN_CTX_secure_new_ex(OPENSSL_CTX *ctx);
 BN_CTX *BN_CTX_secure_new(void);

 void BN_CTX_free(BN_CTX *c);
@@ -21,10 +24,17 @@ library functions. Since dynamic memory allocation to create B<BIGNUM>s
is rather expensive when used in conjunction with repeated subroutine
calls, the B<BN_CTX> structure is used.

BN_CTX_new() allocates and initializes a B<BN_CTX> structure.
BN_CTX_secure_new() allocates and initializes a B<BN_CTX> structure
BN_CTX_new_ex() allocates and initializes a B<BN_CTX> structure for the given
library context B<ctx>. The <ctx> value may be NULL in which case the default
library context will be used. BN_CTX_new() is the same as BN_CTX_new_ex() except
that the default library context is always used.

BN_CTX_secure_new_ex() allocates and initializes a B<BN_CTX> structure
but uses the secure heap (see L<CRYPTO_secure_malloc(3)>) to hold the
B<BIGNUM>s.
B<BIGNUM>s for the given library context B<ctx>. The <ctx> value may be NULL in
which case the default library context will be used. BN_CTX_secure_new() is the
same as BN_CTX_secure_new_ex() except that the default library context is always
used.

BN_CTX_free() frees the components of the B<BN_CTX> and the structure itself.
Since BN_CTX_start() is required in order to obtain B<BIGNUM>s from the
+2 −0
Original line number Diff line number Diff line
@@ -198,7 +198,9 @@ void BN_zero_ex(BIGNUM *a);

const BIGNUM *BN_value_one(void);
char *BN_options(void);
BN_CTX *BN_CTX_new_ex(OPENSSL_CTX *ctx);
BN_CTX *BN_CTX_new(void);
BN_CTX *BN_CTX_secure_new_ex(OPENSSL_CTX *ctx);
BN_CTX *BN_CTX_secure_new(void);
void BN_CTX_free(BN_CTX *c);
void BN_CTX_start(BN_CTX *ctx);
Loading