Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement BN_CTX_get #8388

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .wolfssl_known_macro_extras
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ NO_WOLFSSL_AUTOSAR_CRYIF
NO_WOLFSSL_AUTOSAR_CRYPTO
NO_WOLFSSL_AUTOSAR_CSM
NO_WOLFSSL_BASE64_DECODE
NO_WOLFSSL_BN_CTX
NO_WOLFSSL_MSG_EX
NO_WOLFSSL_RENESAS_FSPSM_AES
NO_WOLFSSL_RENESAS_FSPSM_HASH
Expand Down
74 changes: 44 additions & 30 deletions src/ssl_bn.c
Original file line number Diff line number Diff line change
Expand Up @@ -2362,65 +2362,77 @@ int wolfSSL_BN_print_fp(XFILE fp, const WOLFSSL_BIGNUM *bn)
}
#endif /* !NO_FILESYSTEM && XFPRINTF */

#ifndef NO_WOLFSSL_BN_CTX
/*******************************************************************************
* 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;
SparkiDev marked this conversation as resolved.
Show resolved Hide resolved

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) {
XMEMSET(ctx, 0, sizeof(WOLFSSL_BN_CTX));
}

/* Initialize a BN context object.
*
* BN context not needed for operations.
*
* @param [in] ctx Dummy BN context.
*/
void wolfSSL_BN_CTX_init(WOLFSSL_BN_CTX* ctx)
{
(void)ctx;
WOLFSSL_ENTER("wolfSSL_BN_CTX_init");
return 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* node = (struct WOLFSSL_BN_CTX_LIST*)XMALLOC(
sizeof(struct WOLFSSL_BN_CTX_LIST), NULL, DYNAMIC_TYPE_OPENSSL);
if (node != NULL) {
XMEMSET(node, 0, sizeof(struct WOLFSSL_BN_CTX_LIST));
bn = node->bn = wolfSSL_BN_new();
if (node->bn != NULL) {
node->next = ctx->list;
ctx->list = node;
}
else {
XFREE(node, NULL, DYNAMIC_TYPE_OPENSSL);
node = NULL;
}
}
}

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

#ifndef NO_WOLFSSL_STUB
Expand All @@ -2440,6 +2452,8 @@ void wolfSSL_BN_CTX_start(WOLFSSL_BN_CTX *ctx)
}
#endif

#endif /* NO_WOLFSSL_BN_CTX */

/*******************************************************************************
* BN_MONT_CTX APIs
******************************************************************************/
Expand Down
20 changes: 10 additions & 10 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -62643,17 +62643,16 @@ 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 @@ -78011,7 +78010,7 @@ static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
int derLen;
unsigned char pub_buf[65];
const int pub_len = 65;
BN_CTX* ctx;
BN_CTX* ctx = NULL;
EC_GROUP* curve = NULL;
EC_KEY* ephemeral_key = NULL;
const EC_POINT* h = NULL;
Expand Down Expand Up @@ -78051,6 +78050,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
10 changes: 7 additions & 3 deletions wolfssl/openssl/bn.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,17 @@ 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;

WOLFSSL_API WOLFSSL_BN_CTX* wolfSSL_BN_CTX_new(void);
WOLFSSL_API void wolfSSL_BN_CTX_init(WOLFSSL_BN_CTX* ctx);
WOLFSSL_API void wolfSSL_BN_CTX_free(WOLFSSL_BN_CTX* ctx);

WOLFSSL_API WOLFSSL_BIGNUM* wolfSSL_BN_new(void);
Expand Down Expand Up @@ -209,7 +214,6 @@ typedef WOLFSSL_BN_MONT_CTX BN_MONT_CTX;
typedef WOLFSSL_BN_GENCB BN_GENCB;

#define BN_CTX_new wolfSSL_BN_CTX_new
#define BN_CTX_init wolfSSL_BN_CTX_init
#define BN_CTX_free wolfSSL_BN_CTX_free

#define BN_new wolfSSL_BN_new
Expand Down