From 01440974708c9d6bf3a7780a31f73d8ef7dada9e Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Thu, 23 Jan 2025 20:48:41 +0100 Subject: [PATCH] Changes for libimobiledevice 860ffb --- src/bio.c | 24 ++- src/ssl_bn.c | 375 ++++++++++++++++++++---------------------- tests/api.c | 43 +++++ wolfssl/openssl/bio.h | 1 + wolfssl/openssl/bn.h | 28 +++- wolfssl/ssl.h | 4 +- 6 files changed, 271 insertions(+), 204 deletions(-) diff --git a/src/bio.c b/src/bio.c index 2433727317..89663c336e 100644 --- a/src/bio.c +++ b/src/bio.c @@ -389,6 +389,10 @@ int wolfSSL_BIO_read(WOLFSSL_BIO* bio, void* buf, int len) #endif break; + case WOLFSSL_BIO_NULL: + ret = 0; + break; + } /* switch */ } @@ -813,6 +817,10 @@ int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len) #endif break; + case WOLFSSL_BIO_NULL: + ret = len; + break; + } /* switch */ } @@ -1161,6 +1169,10 @@ int wolfSSL_BIO_gets(WOLFSSL_BIO* bio, char* buf, int sz) break; #endif /* WOLFCRYPT_ONLY */ + case WOLFSSL_BIO_NULL: + ret = 0; + break; + default: WOLFSSL_MSG("BIO type not supported yet with wolfSSL_BIO_gets"); } @@ -1908,7 +1920,7 @@ long wolfSSL_BIO_set_mem_eof_return(WOLFSSL_BIO *bio, int v) int wolfSSL_BIO_get_len(WOLFSSL_BIO *bio) { - int len; + int len = 0; #ifndef NO_FILESYSTEM long memSz = 0; XFILE file; @@ -2309,6 +2321,15 @@ int wolfSSL_BIO_flush(WOLFSSL_BIO* bio) return &meth; } + WOLFSSL_BIO_METHOD *wolfSSL_BIO_s_null(void) + { + static WOLFSSL_BIO_METHOD meth = + WOLFSSL_BIO_METHOD_INIT(WOLFSSL_BIO_NULL); + + WOLFSSL_ENTER("wolfSSL_BIO_s_null"); + + return &meth; + } WOLFSSL_BIO_METHOD *wolfSSL_BIO_s_socket(void) { @@ -2353,7 +2374,6 @@ int wolfSSL_BIO_flush(WOLFSSL_BIO* bio) WOLFSSL_ENTER("wolfSSL_BIO_new_dgram"); if (bio) { - bio->type = WOLFSSL_BIO_DGRAM; bio->shutdown = (byte)closeF; bio->num.fd = (SOCKET_T)fd; } diff --git a/src/ssl_bn.c b/src/ssl_bn.c index 9264ac7b83..06fbd17689 100644 --- a/src/ssl_bn.c +++ b/src/ssl_bn.c @@ -1273,43 +1273,29 @@ int wolfSSL_BN_is_word(const WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w) * Word operation APIs. ******************************************************************************/ -/* Add/subtract a word to/from a big number. - * - * Internal function for adding/subtracting an unsigned long from a - * WOLFSSL_BIGNUM. To add, pass "sub" as 0. To subtract, pass it as 1. - * - * @param [in, out] bn Big number to operate on. - * @param [in] w Word to operate with. - * @param [in] sub Indicates whether operation to perform is a subtract. +enum BN_WORD_OP { + BN_WORD_ADD = 0, + BN_WORD_SUB = 1, + BN_WORD_MUL = 2, + BN_WORD_DIV = 3, + BN_WORD_MOD = 4 +}; + +/* Helper function for word operations. + * + * @param [in, out] bn Big number to operate on. + * @param [in] w Word to operate with. + * @param [in] op Operation to perform. See BN_WORD_OP for valid values. + * @param [out] mod_res Result of the modulo operation. * @return 1 on success. - * @return 0 in failure. + * @return 0 on failure. */ -static int wolfssl_bn_add_word_int(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w, - int sub) +static int bn_word_helper(const WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w, + enum BN_WORD_OP op, WOLFSSL_BN_ULONG* mod_res) { int ret = 1; -#if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT) -#ifdef WOLFSSL_SMALL_STACK - mp_int* w_mp = NULL; -#else - mp_int w_mp[1]; -#endif /* WOLFSSL_SMALL_STACK */ -#endif -#if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT) -#ifdef WOLFSSL_SMALL_STACK - /* Allocate temporary MP integer. */ - w_mp = (mp_int*)XMALLOC(sizeof(*w_mp), NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (w_mp == NULL) { - ret = 0; - } - else -#endif /* WOLFSSL_SMALL_STACK */ - { - /* Clear out MP integer so it can be freed. */ - XMEMSET(w_mp, 0, sizeof(*w_mp)); - } -#endif + WOLFSSL_ENTER("bn_word_helper"); /* Validate parameters. */ if (ret == 1 && BN_IS_NULL(bn)) { @@ -1318,60 +1304,96 @@ static int wolfssl_bn_add_word_int(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w, } if (ret == 1) { - int rc = 0; + int rc = MP_OKAY; #if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT) if (w > (WOLFSSL_BN_ULONG)MP_MASK) { - /* Initialize temporary MP integer. */ - if (mp_init(w_mp) != MP_OKAY) { + DECL_MP_INT_SIZE_DYN(w_mp, sizeof(WOLFSSL_BN_ULONG) * CHAR_BIT, + sizeof(WOLFSSL_BN_ULONG) * CHAR_BIT); + NEW_MP_INT_SIZE(w_mp, sizeof(WOLFSSL_BN_ULONG) * CHAR_BIT, NULL, + DYNAMIC_TYPE_TMP_BUFFER); +#ifdef MP_INT_SIZE_CHECK_NULL + if (w_mp == NULL) { + WOLFSSL_MSG("NEW_MP_INT_SIZE error"); ret = 0; } - /* Set value into temporary MP integer. */ - if ((ret == 1) && (mp_set_int(w_mp, w) != MP_OKAY)) { +#endif + if (ret == 1 && mp_set_int(w_mp, w) != MP_OKAY) { + WOLFSSL_MSG("mp_set_int error"); ret = 0; } if (ret == 1) { - if (sub) { - /* Subtract as MP integer. */ - rc = mp_sub((mp_int *)bn->internal, w_mp, - (mp_int *)bn->internal); - } - else { - /* Add as MP integer. */ - rc = mp_add((mp_int *)bn->internal, w_mp, - (mp_int *)bn->internal); - } - if (rc != MP_OKAY) { - WOLFSSL_MSG("mp_add/sub error"); - ret = 0; + switch (op) { + case BN_WORD_ADD: + rc = mp_add((mp_int*)bn->internal, w_mp, + (mp_int*)bn->internal); + break; + case BN_WORD_SUB: + rc = mp_sub((mp_int*)bn->internal, w_mp, + (mp_int*)bn->internal); + break; + case BN_WORD_MUL: + rc = mp_mul((mp_int*)bn->internal, w_mp, + (mp_int*)bn->internal); + break; + case BN_WORD_DIV: + rc = mp_div((mp_int*)bn->internal, w_mp, + (mp_int*)bn->internal, NULL); + break; + case BN_WORD_MOD: + rc = mp_mod((mp_int*) bn->internal, w_mp, + w_mp); + if (rc == MP_OKAY && mod_res != NULL) + *mod_res = wolfssl_bn_get_word_1(w_mp); + break; + default: + rc = WOLFSSL_NOT_IMPLEMENTED; + break; } } + FREE_MP_INT_SIZE(w_mp, NULL, DYNAMIC_TYPE_RSA); } else #endif { - if (sub) { - /* Subtract word from MP integer. */ - rc = mp_sub_d((mp_int*)bn->internal, (mp_digit)w, - (mp_int*)bn->internal); - } - else { - /* Add word from MP integer. */ - rc = mp_add_d((mp_int*)bn->internal, (mp_digit)w, - (mp_int*)bn->internal); - } - if (rc != MP_OKAY) { - WOLFSSL_MSG("mp_add/sub_d error"); - ret = 0; + switch (op) { + case BN_WORD_ADD: + rc = mp_add_d((mp_int*)bn->internal, (mp_digit)w, + (mp_int*)bn->internal); + break; + case BN_WORD_SUB: + rc = mp_sub_d((mp_int*)bn->internal, (mp_digit)w, + (mp_int*)bn->internal); + break; + case BN_WORD_MUL: + rc = mp_mul_d((mp_int*)bn->internal, (mp_digit)w, + (mp_int*)bn->internal); + break; + case BN_WORD_DIV: + rc = mp_div_d((mp_int*)bn->internal, (mp_digit)w, + (mp_int*)bn->internal, NULL); + break; + case BN_WORD_MOD: + { + mp_digit _mod_res; + rc = mp_mod_d((mp_int*) bn->internal, (mp_digit) w, + &_mod_res); + if (rc == MP_OKAY && mod_res != NULL) + *mod_res = (WOLFSSL_BN_ULONG)_mod_res; + } + break; + default: + rc = WOLFSSL_NOT_IMPLEMENTED; + break; } } + if (ret == 1 && rc != MP_OKAY) { + WOLFSSL_MSG("mp word operation error or not implemented"); + ret = 0; + } } -#if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT) - mp_free(w_mp); -#ifdef WOLFSSL_SMALL_STACK - XFREE(w_mp, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif /* WOLFSSL_SMALL_STACK */ -#endif + WOLFSSL_LEAVE("bn_word_helper", ret); + return ret; } @@ -1390,7 +1412,7 @@ int wolfSSL_BN_add_word(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w) WOLFSSL_ENTER("wolfSSL_BN_add_word"); - ret = wolfssl_bn_add_word_int(bn, w, 0); + ret = bn_word_helper(bn, w, BN_WORD_ADD, NULL); WOLFSSL_LEAVE("wolfSSL_BN_add_word", ret); @@ -1412,7 +1434,7 @@ int wolfSSL_BN_sub_word(WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w) WOLFSSL_ENTER("wolfSSL_BN_sub_word"); - ret = wolfssl_bn_add_word_int(bn, w, 1); + ret = bn_word_helper(bn, w, BN_WORD_SUB, NULL); WOLFSSL_LEAVE("wolfSSL_BN_sub_word", ret); @@ -1421,79 +1443,27 @@ int wolfSSL_BN_sub_word(WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w) int wolfSSL_BN_mul_word(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w) { - int ret = 1; -#if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT) -#ifdef WOLFSSL_SMALL_STACK - mp_int* w_mp = NULL; -#else - mp_int w_mp[1]; -#endif /* WOLFSSL_SMALL_STACK */ -#endif + int ret; WOLFSSL_ENTER("wolfSSL_BN_mul_word"); -#if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT) -#ifdef WOLFSSL_SMALL_STACK - /* Allocate temporary MP integer. */ - w_mp = (mp_int*)XMALLOC(sizeof(*w_mp), NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (w_mp == NULL) { - ret = 0; - } - else -#endif /* WOLFSSL_SMALL_STACK */ - { - /* Clear out MP integer so it can be freed. */ - XMEMSET(w_mp, 0, sizeof(*w_mp)); - } -#endif + ret = bn_word_helper(bn, w, BN_WORD_MUL, NULL); - /* Validate parameters. */ - if (ret == 1 && BN_IS_NULL(bn)) { - WOLFSSL_MSG("bn NULL error"); - ret = 0; - } + WOLFSSL_LEAVE("wolfSSL_BN_mul_word", ret); - if (ret == 1) { - int rc = 0; -#if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT) - if (w > (WOLFSSL_BN_ULONG)MP_MASK) { - /* Initialize temporary MP integer. */ - if (mp_init(w_mp) != MP_OKAY) { - ret = 0; - } - /* Set value into temporary MP integer. */ - if ((ret == 1) && (mp_set_int(w_mp, w) != MP_OKAY)) { - ret = 0; - } - if (ret == 1) { - rc = mp_mul((mp_int*)bn->internal, w_mp, - (mp_int*)bn->internal); - if (rc != MP_OKAY) { - WOLFSSL_MSG("mp_mul error"); - ret = 0; - } - } - } - else -#endif - { - rc = mp_mul_d((mp_int*)bn->internal, (mp_digit)w, - (mp_int*)bn->internal); - if (rc != MP_OKAY) { - WOLFSSL_MSG("mp_mul_d error"); - ret = 0; - } - } - } + return ret; +} -#if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT) - mp_free(w_mp); -#ifdef WOLFSSL_SMALL_STACK - XFREE(w_mp, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif /* WOLFSSL_SMALL_STACK */ -#endif - WOLFSSL_LEAVE("wolfSSL_BN_mul_word", ret); +int wolfSSL_BN_div_word(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w) +{ + int ret; + + WOLFSSL_ENTER("wolfSSL_BN_div_word"); + + ret = bn_word_helper(bn, w, BN_WORD_DIV, NULL); + + WOLFSSL_LEAVE("wolfSSL_BN_div_word", ret); return ret; } @@ -1510,70 +1480,16 @@ int wolfSSL_BN_mul_word(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w) WOLFSSL_BN_ULONG wolfSSL_BN_mod_word(const WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w) { - WOLFSSL_BN_ULONG ret = 0; + int ret; + WOLFSSL_BN_ULONG res = 0; WOLFSSL_ENTER("wolfSSL_BN_mod_word"); - /* Validate parameters. */ - if (BN_IS_NULL(bn)) { - WOLFSSL_MSG("bn NULL error"); - ret = (WOLFSSL_BN_ULONG)-1; - } - -#if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT) - if ((ret == 0) && (w > (WOLFSSL_BN_ULONG)MP_MASK)) { - /* TODO: small stack */ - mp_int w_mp; - mp_int r_mp; - - /* Memset MP integers to be safe to free. */ - XMEMSET(&w_mp, 0, sizeof(w_mp)); - XMEMSET(&r_mp, 0, sizeof(r_mp)); - - /* Initialize MP integer to hold word. */ - if (mp_init(&w_mp) != MP_OKAY) { - ret = (WOLFSSL_BN_ULONG)-1; - } - /* Initialize MP integer to hold result word. */ - if ((ret == 0) && (mp_init(&r_mp) != MP_OKAY)) { - ret = (WOLFSSL_BN_ULONG)-1; - } - /* Set modulus word into MP integer. */ - if ((ret == 0) && (mp_set_int(&w_mp, w) != MP_OKAY)) { - ret = (WOLFSSL_BN_ULONG)-1; - } - /* Calculate modulus result. */ - if ((ret == 0) && (mp_mod((mp_int *)bn->internal, &w_mp, &r_mp) != - MP_OKAY)) { - WOLFSSL_MSG("mp_mod error"); - ret = (WOLFSSL_BN_ULONG)-1; - } - if (ret == 0) { - /* Get modulus result into an unsigned long. */ - ret = wolfssl_bn_get_word_1(&r_mp); - } - - /* Dispose of dynamically allocated data. */ - mp_free(&r_mp); - mp_free(&w_mp); - } - else -#endif - if (ret == 0) { - mp_digit mp_ret; + ret = bn_word_helper(bn, w, BN_WORD_MOD, &res); - /* Calculate modulus result using wolfCrypt. */ - if (mp_mod_d((mp_int*)bn->internal, (mp_digit)w, &mp_ret) != MP_OKAY) { - WOLFSSL_MSG("mp_add_d error"); - ret = (WOLFSSL_BN_ULONG)-1; - } - else { - /* Return result. */ - ret = (WOLFSSL_BN_ULONG)mp_ret; - } - } + WOLFSSL_LEAVE("wolfSSL_BN_mod_word", ret); - return ret; + return ret == 1 ? res : -1; } #endif /* WOLFSSL_KEY_GEN && (!NO_RSA || !NO_DH || !NO_DSA) */ @@ -2512,6 +2428,73 @@ void wolfSSL_BN_CTX_start(WOLFSSL_BN_CTX *ctx) } #endif +/******************************************************************************* + * BN_MONT_CTX APIs + ******************************************************************************/ + +WOLFSSL_BN_MONT_CTX* wolfSSL_BN_MONT_CTX_new(void) +{ + /* wolfcrypt doesn't need BN MONT context. */ + static int mont; + WOLFSSL_ENTER("wolfSSL_BN_MONT_CTX_new"); + return (WOLFSSL_BN_MONT_CTX*)&mont; +} + +void wolfSSL_BN_MONT_CTX_free(WOLFSSL_BN_MONT_CTX *mont) +{ + (void)mont; + WOLFSSL_ENTER("wolfSSL_BN_MONT_CTX_free"); + /* Don't do anything since using dummy, static BN context. */ +} + +int wolfSSL_BN_MONT_CTX_set(WOLFSSL_BN_MONT_CTX *mont, + const WOLFSSL_BIGNUM *mod, WOLFSSL_BN_CTX *ctx) +{ + (void) mont; + (void) mod; + (void) ctx; + WOLFSSL_ENTER("wolfSSL_BN_MONT_CTX_set"); + return WOLFSSL_SUCCESS; +} + +/* Calculate r = a ^ p % m. + * + * @param [out] r Big number to store the result. + * @param [in] a Base as an unsigned long. + * @param [in] p Exponent as a big number. + * @param [in] m Modulus as a big number. + * @param [in] ctx BN context object. Unused. + * @param [in] mont Montgomery context object. Unused. + * + * @return 1 on success. + * @return 0 on failure. + */ +int wolfSSL_BN_mod_exp_mont_word(WOLFSSL_BIGNUM *r, WOLFSSL_BN_ULONG a, + const WOLFSSL_BIGNUM *p, const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx, + WOLFSSL_BN_MONT_CTX *mont) +{ + WOLFSSL_BIGNUM* tmp = NULL; + int ret = WOLFSSL_SUCCESS; + + (void)mont; + WOLFSSL_ENTER("wolfSSL_BN_mod_exp_mont_word"); + + if (ret == WOLFSSL_SUCCESS && (tmp = wolfSSL_BN_new()) == NULL) { + WOLFSSL_MSG("wolfSSL_BN_new failed"); + ret = WOLFSSL_FAILURE; + } + if (ret == WOLFSSL_SUCCESS && + (wolfSSL_BN_set_word(tmp, (unsigned long)a)) == WOLFSSL_FAILURE) { + WOLFSSL_MSG("wolfSSL_BN_set_word failed"); + ret = WOLFSSL_FAILURE; + } + if (ret == WOLFSSL_SUCCESS) + ret = wolfSSL_BN_mod_exp(r, tmp, p, m, ctx); + + wolfSSL_BN_free(tmp); + return ret; +} + #endif /* OPENSSL_EXTRA */ #endif /* !WOLFSSL_SSL_BN_INCLUDED */ diff --git a/tests/api.c b/tests/api.c index e85298adcf..c4b6ed9975 100644 --- a/tests/api.c +++ b/tests/api.c @@ -62685,6 +62685,21 @@ static int test_wolfSSL_BN_init(void) /* check result 3*2 mod 5 */ ExpectIntEQ(BN_get_word(&dv), 1); + { + BN_MONT_CTX* montCtx = NULL; + ExpectNotNull(montCtx = BN_MONT_CTX_new()); + + ExpectIntEQ(BN_MONT_CTX_set(montCtx, &cv, NULL), SSL_SUCCESS); + ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS); + ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS); + ExpectIntEQ(BN_mod_exp_mont_word(&dv, 3, &bv, &cv, NULL, NULL), + WOLFSSL_SUCCESS); + /* check result 3^2 mod 5 */ + ExpectIntEQ(BN_get_word(&dv), 4); + + BN_MONT_CTX_free(montCtx); + } + BN_free(ap); #endif #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */ @@ -62873,6 +62888,14 @@ static int test_wolfSSL_BN_word(void) ExpectIntEQ(BN_mod_word(a, 0), -1); #endif + ExpectIntEQ(BN_set_word(a, 5), 1); + ExpectIntEQ(BN_mul_word(a, 5), 1); + /* check result 5 * 5 */ + ExpectIntEQ(BN_get_word(a), 25); + ExpectIntEQ(BN_div_word(a, 5), 1); + /* check result 25 / 5 */ + ExpectIntEQ(BN_get_word(a), 5); + BN_free(c); BN_free(b); BN_free(a); @@ -71027,6 +71050,25 @@ static int test_wolfSSL_BIO_datagram(void) return EXPECT_RESULT(); } +static int test_wolfSSL_BIO_s_null(void) +{ + EXPECT_DECLS; +#if !defined(NO_BIO) && defined(OPENSSL_EXTRA) + BIO *b = NULL; + char testData[10] = {'t','e','s','t',0}; + + ExpectNotNull(b = BIO_new(BIO_s_null())); + ExpectIntEQ(BIO_write(b, testData, sizeof(testData)), sizeof(testData)); + ExpectIntEQ(BIO_read(b, testData, sizeof(testData)), 0); + ExpectIntEQ(BIO_puts(b, testData), 4); + ExpectIntEQ(BIO_gets(b, testData, sizeof(testData)), 0); + ExpectIntEQ(BIO_pending(b), 0); + ExpectIntEQ(BIO_eof(b), 1); + + BIO_free(b); +#endif + return EXPECT_RESULT(); +} #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \ defined(HAVE_HTTP_CLIENT) @@ -102427,6 +102469,7 @@ TEST_CASE testCases[] = { /* Can't memory test as server Asserts in thread. */ TEST_DECL(test_wolfSSL_BIO_accept), TEST_DECL(test_wolfSSL_BIO_tls), + TEST_DECL(test_wolfSSL_BIO_s_null), TEST_DECL(test_wolfSSL_BIO_datagram), #endif diff --git a/wolfssl/openssl/bio.h b/wolfssl/openssl/bio.h index 91caa61079..73214abcca 100644 --- a/wolfssl/openssl/bio.h +++ b/wolfssl/openssl/bio.h @@ -104,6 +104,7 @@ #endif #define BIO_int_ctrl wolfSSL_BIO_int_ctrl #define BIO_reset wolfSSL_BIO_reset +#define BIO_s_null wolfSSL_BIO_s_null #define BIO_s_file wolfSSL_BIO_s_file #define BIO_s_bio wolfSSL_BIO_s_bio #define BIO_s_socket wolfSSL_BIO_s_socket diff --git a/wolfssl/openssl/bn.h b/wolfssl/openssl/bn.h index df2b725090..39b6bf384e 100644 --- a/wolfssl/openssl/bn.h +++ b/wolfssl/openssl/bn.h @@ -77,8 +77,9 @@ typedef struct WOLFSSL_BIGNUM { #define WOLFSSL_BN_MAX_VAL ((BN_ULONG)-1) -typedef struct WOLFSSL_BN_CTX WOLFSSL_BN_CTX; -typedef struct WOLFSSL_BN_GENCB WOLFSSL_BN_GENCB; +typedef struct WOLFSSL_BN_CTX 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); @@ -151,6 +152,7 @@ WOLFSSL_API int wolfSSL_BN_lshift(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* bn, WOLFSSL_API int wolfSSL_BN_add_word(WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w); WOLFSSL_API int wolfSSL_BN_sub_word(WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w); WOLFSSL_API int wolfSSL_BN_mul_word(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w); +WOLFSSL_API int wolfSSL_BN_div_word(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w); WOLFSSL_API int wolfSSL_BN_set_bit(WOLFSSL_BIGNUM* bn, int n); WOLFSSL_API int wolfSSL_BN_clear_bit(WOLFSSL_BIGNUM* bn, int n); WOLFSSL_API int wolfSSL_BN_set_word(WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w); @@ -184,6 +186,13 @@ WOLFSSL_API WOLFSSL_BIGNUM *wolfSSL_BN_mod_inverse( const WOLFSSL_BIGNUM *n, WOLFSSL_BN_CTX *ctx); +WOLFSSL_API WOLFSSL_BN_MONT_CTX* wolfSSL_BN_MONT_CTX_new(void); +WOLFSSL_API void wolfSSL_BN_MONT_CTX_free(WOLFSSL_BN_MONT_CTX *mont); +WOLFSSL_API int wolfSSL_BN_MONT_CTX_set(WOLFSSL_BN_MONT_CTX *mont, + const WOLFSSL_BIGNUM *mod, WOLFSSL_BN_CTX *ctx); +WOLFSSL_API int wolfSSL_BN_mod_exp_mont_word(WOLFSSL_BIGNUM *r, + WOLFSSL_BN_ULONG a, const WOLFSSL_BIGNUM *p, const WOLFSSL_BIGNUM *m, + WOLFSSL_BN_CTX *ctx, WOLFSSL_BN_MONT_CTX *mont); #if !defined(OPENSSL_COEXIST) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) @@ -194,9 +203,10 @@ WOLFSSL_API WOLFSSL_BIGNUM *wolfSSL_BN_mod_inverse( #define BN_RAND_BOTTOM_ANY WOLFSSL_BN_RAND_BOTTOM_ANY #define BN_RAND_BOTTOM_ODD WOLFSSL_BN_RAND_BOTTOM_ODD -typedef WOLFSSL_BIGNUM BIGNUM; -typedef WOLFSSL_BN_CTX BN_CTX; -typedef WOLFSSL_BN_GENCB BN_GENCB; +typedef WOLFSSL_BIGNUM BIGNUM; +typedef WOLFSSL_BN_CTX BN_CTX; +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 @@ -228,6 +238,8 @@ typedef WOLFSSL_BN_GENCB BN_GENCB; #define BN_mod wolfSSL_BN_mod #define BN_mod_exp wolfSSL_BN_mod_exp +#define BN_mod_exp_mont(a,b,c,d,e,f) \ + ((void)(f), wolfSSL_BN_mod_exp((a),(b),(c),(d),(e))) #define BN_mod_mul wolfSSL_BN_mod_mul #define BN_sub wolfSSL_BN_sub #define BN_mul wolfSSL_BN_mul @@ -257,6 +269,7 @@ typedef WOLFSSL_BN_GENCB BN_GENCB; #define BN_add_word wolfSSL_BN_add_word #define BN_mul_word wolfSSL_BN_mul_word #define BN_sub_word wolfSSL_BN_sub_word +#define BN_div_word wolfSSL_BN_div_word #define BN_add wolfSSL_BN_add #define BN_mod_add wolfSSL_BN_mod_add #define BN_set_word wolfSSL_BN_set_word @@ -290,6 +303,11 @@ typedef WOLFSSL_BN_GENCB BN_GENCB; #define BN_prime_checks 0 +#define BN_MONT_CTX_new wolfSSL_BN_MONT_CTX_new +#define BN_MONT_CTX_free wolfSSL_BN_MONT_CTX_free +#define BN_MONT_CTX_set wolfSSL_BN_MONT_CTX_set +#define BN_mod_exp_mont_word wolfSSL_BN_mod_exp_mont_word + #endif /* !OPENSSL_COEXIST && (OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL) */ diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index 50e3d5107c..4b14499223 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -706,7 +706,8 @@ enum BIO_TYPE { WOLFSSL_BIO_FILE = 6, WOLFSSL_BIO_BASE64 = 7, WOLFSSL_BIO_MD = 8, - WOLFSSL_BIO_DGRAM = 9 + WOLFSSL_BIO_DGRAM = 9, + WOLFSSL_BIO_NULL = 10 }; enum BIO_FLAGS { @@ -2043,6 +2044,7 @@ WOLFSSL_API WOLFSSL_BIO *wolfSSL_BIO_new_fd(int fd, int close_flag); WOLFSSL_API WOLFSSL_BIO_METHOD *wolfSSL_BIO_s_bio(void); WOLFSSL_API WOLFSSL_BIO_METHOD *wolfSSL_BIO_s_socket(void); WOLFSSL_API WOLFSSL_BIO_METHOD *wolfSSL_BIO_s_datagram(void); +WOLFSSL_API WOLFSSL_BIO_METHOD *wolfSSL_BIO_s_null(void); WOLFSSL_API WOLFSSL_BIO *wolfSSL_BIO_new_connect(const char *str); WOLFSSL_API WOLFSSL_BIO *wolfSSL_BIO_new_accept(const char *port);