Implement BN_CTX_get

This commit is contained in:
Juliusz Sosinowicz
2025-01-28 12:54:56 +01:00
parent eb15a1213c
commit 841d13e81c
4 changed files with 66 additions and 30 deletions

View File

@ -373,6 +373,7 @@ NO_WOLFSSL_AUTOSAR_CRYIF
NO_WOLFSSL_AUTOSAR_CRYPTO NO_WOLFSSL_AUTOSAR_CRYPTO
NO_WOLFSSL_AUTOSAR_CSM NO_WOLFSSL_AUTOSAR_CSM
NO_WOLFSSL_BASE64_DECODE NO_WOLFSSL_BASE64_DECODE
NO_WOLFSSL_BN_CTX
NO_WOLFSSL_MSG_EX NO_WOLFSSL_MSG_EX
NO_WOLFSSL_RENESAS_FSPSM_AES NO_WOLFSSL_RENESAS_FSPSM_AES
NO_WOLFSSL_RENESAS_FSPSM_HASH NO_WOLFSSL_RENESAS_FSPSM_HASH

View File

@ -2362,65 +2362,89 @@ int wolfSSL_BN_print_fp(XFILE fp, const WOLFSSL_BIGNUM *bn)
} }
#endif /* !NO_FILESYSTEM && XFPRINTF */ #endif /* !NO_FILESYSTEM && XFPRINTF */
#ifndef NO_WOLFSSL_BN_CTX
/******************************************************************************* /*******************************************************************************
* BN_CTX APIs * BN_CTX APIs
******************************************************************************/ ******************************************************************************/
/* Allocate and return a new BN context object. /* Create a new BN context object.
* *
* BN context not needed for operations. * @return BN context object on success.
* * @return NULL on failure.
* @return Pointer to dummy object.
*/ */
WOLFSSL_BN_CTX* wolfSSL_BN_CTX_new(void) WOLFSSL_BN_CTX* wolfSSL_BN_CTX_new(void)
{ {
/* wolfcrypt doesn't need BN context. */ WOLFSSL_BN_CTX* ctx = NULL;
static int ctx;
WOLFSSL_ENTER("wolfSSL_BN_CTX_new"); 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. /* Initialize a BN context object.
* *
* BN context not needed for operations. * @param [in] ctx BN context object.
*
* @param [in] ctx Dummy BN context.
*/ */
void wolfSSL_BN_CTX_init(WOLFSSL_BN_CTX* ctx) void wolfSSL_BN_CTX_init(WOLFSSL_BN_CTX* ctx)
{ {
(void)ctx;
WOLFSSL_ENTER("wolfSSL_BN_CTX_init"); WOLFSSL_ENTER("wolfSSL_BN_CTX_init");
if (ctx != NULL) {
XMEMSET(ctx, 0, sizeof(WOLFSSL_BN_CTX));
}
} }
/* Free a BN context object. /* Free a BN context object.
* *
* BN context not needed for operations. * @param [in] ctx BN context object.
*
* @param [in] ctx Dummy BN context.
*/ */
void wolfSSL_BN_CTX_free(WOLFSSL_BN_CTX* ctx) void wolfSSL_BN_CTX_free(WOLFSSL_BN_CTX* ctx)
{ {
(void)ctx;
WOLFSSL_ENTER("wolfSSL_BN_CTX_free"); 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 Big number on success.
* @return NULL on failure. * @return NULL on failure.
*/ */
WOLFSSL_BIGNUM *wolfSSL_BN_CTX_get(WOLFSSL_BN_CTX *ctx) WOLFSSL_BIGNUM *wolfSSL_BN_CTX_get(WOLFSSL_BN_CTX *ctx)
{ {
/* ctx is not used - returning a new big number. */ WOLFSSL_BIGNUM* bn = NULL;
(void)ctx;
WOLFSSL_ENTER("wolfSSL_BN_CTX_get"); 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 bn;
return wolfSSL_BN_new();
} }
#ifndef NO_WOLFSSL_STUB #ifndef NO_WOLFSSL_STUB
@ -2440,6 +2464,8 @@ void wolfSSL_BN_CTX_start(WOLFSSL_BN_CTX *ctx)
} }
#endif #endif
#endif /* NO_WOLFSSL_BN_CTX */
/******************************************************************************* /*******************************************************************************
* BN_MONT_CTX APIs * BN_MONT_CTX APIs
******************************************************************************/ ******************************************************************************/

View File

@ -62643,17 +62643,19 @@ static int test_wolfSSL_BN_CTX(void)
#if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \ #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
!defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH) !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
WOLFSSL_BN_CTX* bn_ctx = NULL; 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. */ /* No implementation. */
BN_CTX_init(NULL); BN_CTX_init(NULL);
ExpectNotNull(t = BN_CTX_get(NULL)); ExpectNull(BN_CTX_get(NULL));
BN_free(t); ExpectNotNull(BN_CTX_get(bn_ctx));
ExpectNotNull(t = BN_CTX_get(bn_ctx)); ExpectNotNull(BN_CTX_get(bn_ctx));
BN_free(t); 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 #ifndef NO_WOLFSSL_STUB
/* No implementation. */ /* No implementation. */
@ -78011,7 +78013,7 @@ static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
int derLen; int derLen;
unsigned char pub_buf[65]; unsigned char pub_buf[65];
const int pub_len = 65; const int pub_len = 65;
BN_CTX* ctx; BN_CTX* ctx = NULL;
EC_GROUP* curve = NULL; EC_GROUP* curve = NULL;
EC_KEY* ephemeral_key = NULL; EC_KEY* ephemeral_key = NULL;
const EC_POINT* h = NULL; const EC_POINT* h = NULL;
@ -78051,6 +78053,7 @@ static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
EVP_PKEY_free(pkey); EVP_PKEY_free(pkey);
EC_KEY_free(ephemeral_key); EC_KEY_free(ephemeral_key);
EC_GROUP_free(curve); EC_GROUP_free(curve);
BN_CTX_free(ctx);
#endif #endif
return EXPECT_RESULT(); return EXPECT_RESULT();
} }

View File

@ -77,7 +77,13 @@ typedef struct WOLFSSL_BIGNUM {
#define WOLFSSL_BN_MAX_VAL ((BN_ULONG)-1) #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_MONT_CTX WOLFSSL_BN_MONT_CTX;
typedef struct WOLFSSL_BN_GENCB WOLFSSL_BN_GENCB; typedef struct WOLFSSL_BN_GENCB WOLFSSL_BN_GENCB;