From 1f1a173d5659b085ddd5b5a09519a50a9bca87de Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Wed, 8 Jan 2020 08:57:20 +1000 Subject: [PATCH] Fix in SP C impl for small stack Memset correct size and only when pointer is not NULL. --- wolfcrypt/src/sp_c32.c | 68 ++++++++++++++++++++++++++++-------------- wolfcrypt/src/sp_c64.c | 68 ++++++++++++++++++++++++++++-------------- 2 files changed, 92 insertions(+), 44 deletions(-) diff --git a/wolfcrypt/src/sp_c32.c b/wolfcrypt/src/sp_c32.c index b61b7fc1b..f7aae3d5d 100644 --- a/wolfcrypt/src/sp_c32.c +++ b/wolfcrypt/src/sp_c32.c @@ -3859,11 +3859,14 @@ int sp_ModExp_2048(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res) err = sp_2048_to_mp(r, res); } - XMEMSET(e, 0, sizeof(sp_digit) * 90U); #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 90U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 90U); #endif return err; @@ -4105,12 +4108,13 @@ static int sp_2048_mod_exp_2_90(sp_digit* r, const sp_digit* e, int bits, const #ifdef WOLFSSL_SMALL_STACK norm = td; tmp = td + 180; + XMEMSET(td, 0, sizeof(sp_digit) * 271); #else norm = nd; tmp = td; + XMEMSET(td, 0, sizeof(td)); #endif - XMEMSET(td, 0, sizeof(td)); sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_90(norm, m); @@ -4332,11 +4336,13 @@ int sp_DhExp_2048(mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } - XMEMSET(e, 0, sizeof(sp_digit) * 90U); - #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 90U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 90U); #endif return err; @@ -4471,11 +4477,14 @@ int sp_ModExp_1024(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res) err = sp_2048_to_mp(r, res); } - XMEMSET(e, 0, sizeof(sp_digit) * 45U); #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 45U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 45U); #endif return err; @@ -7723,11 +7732,14 @@ int sp_ModExp_3072(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res) err = sp_3072_to_mp(r, res); } - XMEMSET(e, 0, sizeof(sp_digit) * 134U); #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 134U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 134U); #endif return err; @@ -8057,12 +8069,13 @@ static int sp_3072_mod_exp_2_134(sp_digit* r, const sp_digit* e, int bits, const #ifdef WOLFSSL_SMALL_STACK norm = td; tmp = td + 268; + XMEMSET(td, 0, sizeof(sp_digit) * 403); #else norm = nd; tmp = td; + XMEMSET(td, 0, sizeof(td)); #endif - XMEMSET(td, 0, sizeof(td)); sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_134(norm, m); @@ -8284,11 +8297,13 @@ int sp_DhExp_3072(mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } - XMEMSET(e, 0, sizeof(sp_digit) * 134U); - #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 134U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 134U); #endif return err; @@ -8423,11 +8438,14 @@ int sp_ModExp_1536(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res) err = sp_3072_to_mp(r, res); } - XMEMSET(e, 0, sizeof(sp_digit) * 67U); #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 67U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 67U); #endif return err; @@ -11746,11 +11764,14 @@ int sp_ModExp_4096(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res) err = sp_4096_to_mp(r, res); } - XMEMSET(e, 0, sizeof(sp_digit) * 196U); #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 196U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 196U); #endif return err; @@ -12204,12 +12225,13 @@ static int sp_4096_mod_exp_2_196(sp_digit* r, const sp_digit* e, int bits, const #ifdef WOLFSSL_SMALL_STACK norm = td; tmp = td + 392; + XMEMSET(td, 0, sizeof(sp_digit) * 589); #else norm = nd; tmp = td; + XMEMSET(td, 0, sizeof(td)); #endif - XMEMSET(td, 0, sizeof(td)); sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_196(norm, m); @@ -12431,11 +12453,13 @@ int sp_DhExp_4096(mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } - XMEMSET(e, 0, sizeof(sp_digit) * 196U); - #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 196U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 196U); #endif return err; diff --git a/wolfcrypt/src/sp_c64.c b/wolfcrypt/src/sp_c64.c index 88fc8f9e2..ca98ecb27 100644 --- a/wolfcrypt/src/sp_c64.c +++ b/wolfcrypt/src/sp_c64.c @@ -3402,11 +3402,14 @@ int sp_ModExp_2048(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res) err = sp_2048_to_mp(r, res); } - XMEMSET(e, 0, sizeof(sp_digit) * 36U); #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 36U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 36U); #endif return err; @@ -3540,12 +3543,13 @@ static int sp_2048_mod_exp_2_36(sp_digit* r, const sp_digit* e, int bits, const #ifdef WOLFSSL_SMALL_STACK norm = td; tmp = td + 72; + XMEMSET(td, 0, sizeof(sp_digit) * 109); #else norm = nd; tmp = td; + XMEMSET(td, 0, sizeof(td)); #endif - XMEMSET(td, 0, sizeof(td)); sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_36(norm, m); @@ -3768,11 +3772,13 @@ int sp_DhExp_2048(mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } - XMEMSET(e, 0, sizeof(sp_digit) * 36U); - #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 36U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 36U); #endif return err; @@ -3907,11 +3913,14 @@ int sp_ModExp_1024(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res) err = sp_2048_to_mp(r, res); } - XMEMSET(e, 0, sizeof(sp_digit) * 18U); #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 18U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 18U); #endif return err; @@ -7446,11 +7455,14 @@ int sp_ModExp_3072(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res) err = sp_3072_to_mp(r, res); } - XMEMSET(e, 0, sizeof(sp_digit) * 54U); #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 54U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 54U); #endif return err; @@ -7620,12 +7632,13 @@ static int sp_3072_mod_exp_2_54(sp_digit* r, const sp_digit* e, int bits, const #ifdef WOLFSSL_SMALL_STACK norm = td; tmp = td + 108; + XMEMSET(td, 0, sizeof(sp_digit) * 163); #else norm = nd; tmp = td; + XMEMSET(td, 0, sizeof(td)); #endif - XMEMSET(td, 0, sizeof(td)); sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_54(norm, m); @@ -7848,11 +7861,13 @@ int sp_DhExp_3072(mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } - XMEMSET(e, 0, sizeof(sp_digit) * 54U); - #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 54U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 54U); #endif return err; @@ -7987,11 +8002,14 @@ int sp_ModExp_1536(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res) err = sp_3072_to_mp(r, res); } - XMEMSET(e, 0, sizeof(sp_digit) * 27U); #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 27U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 27U); #endif return err; @@ -11799,11 +11817,14 @@ int sp_ModExp_4096(mp_int* base, mp_int* exp, mp_int* mod, mp_int* res) err = sp_4096_to_mp(r, res); } - XMEMSET(e, 0, sizeof(sp_digit) * 78U); #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 78U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 78U); #endif return err; @@ -12021,12 +12042,13 @@ static int sp_4096_mod_exp_2_78(sp_digit* r, const sp_digit* e, int bits, const #ifdef WOLFSSL_SMALL_STACK norm = td; tmp = td + 156; + XMEMSET(td, 0, sizeof(sp_digit) * 235); #else norm = nd; tmp = td; + XMEMSET(td, 0, sizeof(td)); #endif - XMEMSET(td, 0, sizeof(td)); sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_78(norm, m); @@ -12249,11 +12271,13 @@ int sp_DhExp_4096(mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } - XMEMSET(e, 0, sizeof(sp_digit) * 78U); - #ifdef WOLFSSL_SMALL_STACK - if (d != NULL) + if (d != NULL) { + XMEMSET(e, 0, sizeof(sp_digit) * 78U); XFREE(d, NULL, DYNAMIC_TYPE_DH); + } +#else + XMEMSET(e, 0, sizeof(sp_digit) * 78U); #endif return err;