Skip to content

Commit

Permalink
Implement BN_CTX_get
Browse files Browse the repository at this point in the history
  • Loading branch information
julek-wolfssl committed Jan 28, 2025
1 parent c48ba69 commit fb0cb02
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
67 changes: 45 additions & 22 deletions src/ssl_bn.c
Original file line number Diff line number Diff line change
Expand Up @@ -2366,61 +2366,84 @@ int wolfSSL_BN_print_fp(XFILE fp, const WOLFSSL_BIGNUM *bn)
* BN_CTX APIs
******************************************************************************/

/* Allocate and return a new BN context object.
/* Create a new BN context object.
*
* BN context not needed for operations.
*
* @return Pointer to dummy object.
* @return BN context object on success.
* @return NULL on failure.
*/
WOLFSSL_BN_CTX* wolfSSL_BN_CTX_new(void)
{
/* wolfcrypt doesn't need BN context. */
static int ctx;
WOLFSSL_BN_CTX* ctx = NULL;

WOLFSSL_ENTER("wolfSSL_BN_CTX_new");
return (WOLFSSL_BN_CTX*)&ctx;
ctx = (WOLFSSL_BN_CTX*)XMALLOC(sizeof(WOLFSSL_BN_CTX), NULL,
DYNAMIC_TYPE_OPENSSL);
if (ctx != NULL) {
wolfSSL_BN_CTX_init(ctx);
}

return ctx;
}

/* Initialize a BN context object.
*
* BN context not needed for operations.
*
* @param [in] ctx Dummy BN context.
* @param [in] ctx BN context object.
*/
void wolfSSL_BN_CTX_init(WOLFSSL_BN_CTX* ctx)
{
(void)ctx;
WOLFSSL_ENTER("wolfSSL_BN_CTX_init");
if (ctx != NULL) {
XMEMSET(ctx, 0, sizeof(WOLFSSL_BN_CTX));
}
}


/* Free a BN context object.
*
* BN context not needed for operations.
*
* @param [in] ctx Dummy BN context.
* @param [in] ctx BN context object.
*/
void wolfSSL_BN_CTX_free(WOLFSSL_BN_CTX* ctx)
{
(void)ctx;
WOLFSSL_ENTER("wolfSSL_BN_CTX_free");
/* Don't do anything since using dummy, static BN context. */
if (ctx != NULL) {
while (ctx->list != NULL) {
struct WOLFSSL_BN_CTX_LIST* tmp = ctx->list;
ctx->list = ctx->list->next;
wolfSSL_BN_free(tmp->bn);
XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
}
XFREE(ctx, NULL, DYNAMIC_TYPE_OPENSSL);
}
}

/* Get a big number based on the BN context.
/* Get a big number from the BN context.
*
* @param [in] ctx BN context. Not used.
* @param [in] ctx BN context object.
* @return Big number on success.
* @return NULL on failure.
*/
WOLFSSL_BIGNUM *wolfSSL_BN_CTX_get(WOLFSSL_BN_CTX *ctx)
{
/* ctx is not used - returning a new big number. */
(void)ctx;
WOLFSSL_BIGNUM* bn = NULL;

WOLFSSL_ENTER("wolfSSL_BN_CTX_get");
if (ctx != NULL) {
struct WOLFSSL_BN_CTX_LIST** prev = &ctx->list;
while (*prev != NULL)
prev = &(*prev)->next;
*prev = (struct WOLFSSL_BN_CTX_LIST*)XMALLOC(
sizeof(struct WOLFSSL_BN_CTX_LIST), NULL, DYNAMIC_TYPE_OPENSSL);
if (*prev != NULL) {
XMEMSET(*prev, 0, sizeof(struct WOLFSSL_BN_CTX_LIST));
bn = (*prev)->bn = wolfSSL_BN_new();
if ((*prev)->bn == NULL) {
XFREE(*prev, NULL, DYNAMIC_TYPE_OPENSSL);
*prev = NULL;
}
}
}

/* Return a new big number. */
return wolfSSL_BN_new();
return bn;
}

#ifndef NO_WOLFSSL_STUB
Expand Down
15 changes: 9 additions & 6 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -62628,17 +62628,19 @@ static int test_wolfSSL_BN_CTX(void)
#if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
!defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
WOLFSSL_BN_CTX* bn_ctx = NULL;
WOLFSSL_BIGNUM* t = NULL;

ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
ExpectNotNull(bn_ctx = BN_CTX_new());

/* No implementation. */
BN_CTX_init(NULL);

ExpectNotNull(t = BN_CTX_get(NULL));
BN_free(t);
ExpectNotNull(t = BN_CTX_get(bn_ctx));
BN_free(t);
ExpectNull(BN_CTX_get(NULL));
ExpectNotNull(BN_CTX_get(bn_ctx));
ExpectNotNull(BN_CTX_get(bn_ctx));
ExpectNotNull(BN_CTX_get(bn_ctx));
ExpectNotNull(BN_CTX_get(bn_ctx));
ExpectNotNull(BN_CTX_get(bn_ctx));
ExpectNotNull(BN_CTX_get(bn_ctx));

#ifndef NO_WOLFSSL_STUB
/* No implementation. */
Expand Down Expand Up @@ -78036,6 +78038,7 @@ static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
EVP_PKEY_free(pkey);
EC_KEY_free(ephemeral_key);
EC_GROUP_free(curve);
BN_CTX_free(ctx);
#endif
return EXPECT_RESULT();
}
Expand Down
8 changes: 7 additions & 1 deletion wolfssl/openssl/bn.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ typedef struct WOLFSSL_BIGNUM {

#define WOLFSSL_BN_MAX_VAL ((BN_ULONG)-1)

typedef struct WOLFSSL_BN_CTX WOLFSSL_BN_CTX;
struct WOLFSSL_BN_CTX_LIST {
WOLFSSL_BIGNUM* bn;
struct WOLFSSL_BN_CTX_LIST* next;
};
typedef struct WOLFSSL_BN_CTX {
struct WOLFSSL_BN_CTX_LIST* list;
} WOLFSSL_BN_CTX;
typedef struct WOLFSSL_BN_MONT_CTX WOLFSSL_BN_MONT_CTX;
typedef struct WOLFSSL_BN_GENCB WOLFSSL_BN_GENCB;

Expand Down

0 comments on commit fb0cb02

Please sign in to comment.