Skip to content

Commit 4550397

Browse files
committed
scan-build fixes
sp_mulmod - scan-build getting confused with size of result - don't check result size as checked already - split out implementation of sp_mulmod from check StoreEccKey - ensure pubKey is not NULL even though all uses will not be GetCertKey - ensure source is not NULL - cert->source may be NULL in incorrect usages of APIs
1 parent a595f10 commit 4550397

2 files changed

Lines changed: 57 additions & 20 deletions

File tree

wolfcrypt/src/asn.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11451,6 +11451,7 @@ enum {
1145111451
* @param [in] pubKey Buffer holding encoded public key.
1145211452
* @param [in] pubKeyLen Length of encoded public key in bytes.
1145311453
* @return 0 on success.
11454+
* @return BAD_FUNC_ARG when pubKey is NULL.
1145411455
* @return ASN_PARSE_E when BER encoded data does not match ASN.1 items or
1145511456
* is invalid.
1145611457
* @return BUFFER_E when data in buffer is too small.
@@ -11470,6 +11471,10 @@ static int StoreEccKey(DecodedCert* cert, const byte* source, word32* srcIdx,
1147011471
byte tag;
1147111472
int length;
1147211473

11474+
if (pubKey == NULL) {
11475+
return BAD_FUNC_ARG;
11476+
}
11477+
1147311478
localIdx = *srcIdx;
1147411479
if (GetASNTag(source, &localIdx, &tag, maxIdx) < 0)
1147511480
return ASN_PARSE_E;
@@ -11527,6 +11532,11 @@ static int StoreEccKey(DecodedCert* cert, const byte* source, word32* srcIdx,
1152711532
DECL_ASNGETDATA(dataASN, eccCertKeyASN_Length);
1152811533
byte* publicKey;
1152911534

11535+
/* Validate parameters. */
11536+
if (pubKey == NULL) {
11537+
ret = BAD_FUNC_ARG;
11538+
}
11539+
1153011540
/* Clear dynamic data and check OID is a curve. */
1153111541
CALLOC_ASNGETDATA(dataASN, eccCertKeyASN_Length, ret, cert->heap);
1153211542
if (ret == 0) {
@@ -11695,6 +11705,11 @@ static int GetCertKey(DecodedCert* cert, const byte* source, word32* inOutIdx,
1169511705
int ret = 0;
1169611706
int length;
1169711707

11708+
/* Validate paramaters. */
11709+
if (source == NULL) {
11710+
return ASN_PARSE_E;
11711+
}
11712+
1169811713
#ifndef WOLFSSL_ASN_TEMPLATE
1169911714
if (GetSequence(source, &srcIdx, &length, maxIdx) < 0)
1170011715
#else

wolfcrypt/src/sp_int.c

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11696,7 +11696,7 @@ int sp_mul(const sp_int* a, const sp_int* b, sp_int* r)
1169611696
* @return MP_OKAY on success.
1169711697
* @return MP_MEM when dynamic memory allocation fails.
1169811698
*/
11699-
static int _sp_mulmod(const sp_int* a, const sp_int* b, const sp_int* m,
11699+
static int _sp_mulmod_tmp(const sp_int* a, const sp_int* b, const sp_int* m,
1170011700
sp_int* r)
1170111701
{
1170211702
int err = MP_OKAY;
@@ -11722,6 +11722,39 @@ static int _sp_mulmod(const sp_int* a, const sp_int* b, const sp_int* m,
1172211722
return err;
1172311723
}
1172411724

11725+
/* Multiply a by b mod m and store in r: r = (a * b) mod m
11726+
*
11727+
* @param [in] a SP integer to multiply.
11728+
* @param [in] b SP integer to multiply.
11729+
* @param [in] m SP integer that is the modulus.
11730+
* @param [out] r SP integer result.
11731+
*
11732+
* @return MP_OKAY on success.
11733+
* @return MP_MEM when dynamic memory allocation fails.
11734+
*/
11735+
static int _sp_mulmod(const sp_int* a, const sp_int* b, const sp_int* m,
11736+
sp_int* r)
11737+
{
11738+
int err = MP_OKAY;
11739+
11740+
/* Use r as intermediate result if not same as pointer m which is needed
11741+
* after first intermediate result.
11742+
*/
11743+
if (r != m) {
11744+
/* Multiply and reduce. */
11745+
err = sp_mul(a, b, r);
11746+
if (err == MP_OKAY) {
11747+
err = sp_mod(r, m, r);
11748+
}
11749+
}
11750+
else {
11751+
/* Do operation using temporary. */
11752+
_sp_mulmod_tmp(a, b, m, r);
11753+
}
11754+
11755+
return err;
11756+
}
11757+
1172511758
/* Multiply a by b mod m and store in r: r = (a * b) mod m
1172611759
*
1172711760
* @param [in] a SP integer to multiply.
@@ -11755,19 +11788,8 @@ int sp_mulmod(const sp_int* a, const sp_int* b, const sp_int* m, sp_int* r)
1175511788
}
1175611789
#endif
1175711790

11758-
/* Use r as intermediate result if not same as pointer m which is needed
11759-
* after first intermediate result.
11760-
*/
11761-
if ((err == MP_OKAY) && (r != m)) {
11762-
/* Multiply and reduce. */
11763-
err = sp_mul(a, b, r);
11764-
if (err == MP_OKAY) {
11765-
err = sp_mod(r, m, r);
11766-
}
11767-
}
11768-
else if (err == MP_OKAY) {
11769-
/* Do operation using temporary. */
11770-
_sp_mulmod(a, b, m, r);
11791+
if (err == MP_OKAY) {
11792+
err = _sp_mulmod(a, b, m, r);
1177111793
}
1177211794

1177311795
#if 0
@@ -12562,7 +12584,7 @@ static int _sp_exptmod_ex(const sp_int* b, const sp_int* e, int bits,
1256212584
/* 4.4 s = s | y */
1256312585
s |= y;
1256412586
/* 4.5. t[j] = t[j] * b */
12565-
err = sp_mulmod(t[j], b, m, t[j]);
12587+
err = _sp_mulmod(t[j], b, m, t[j]);
1256612588
}
1256712589
#else
1256812590
/* 4.1. t[s] = t[s] ^ 2 */
@@ -12585,7 +12607,7 @@ static int _sp_exptmod_ex(const sp_int* b, const sp_int* e, int bits,
1258512607
_sp_copy((sp_int*)(((size_t)t[0] & sp_off_on_addr[j^1]) +
1258612608
((size_t)t[1] & sp_off_on_addr[j ])),
1258712609
t[2]);
12588-
err = sp_mulmod(t[2], b, m, t[2]);
12610+
err = _sp_mulmod(t[2], b, m, t[2]);
1258912611
_sp_copy(t[2],
1259012612
(sp_int*)(((size_t)t[0] & sp_off_on_addr[j^1]) +
1259112613
((size_t)t[1] & sp_off_on_addr[j ])));
@@ -12682,7 +12704,7 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1268212704
*/
1268312705
err = sp_mont_norm(t[1], m);
1268412706
if (err == MP_OKAY) {
12685-
err = sp_mulmod(t[0], t[1], m, t[0]);
12707+
err = _sp_mulmod(t[0], t[1], m, t[0]);
1268612708
}
1268712709
if (err == MP_OKAY) {
1268812710
/* 4. t[1] = t[0]
@@ -12860,7 +12882,7 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1286012882
err = sp_mont_norm(t[0], m);
1286112883
if (err == MP_OKAY) {
1286212884
/* 3. t[1] = ToMont(t[1]) */
12863-
err = sp_mulmod(t[1], t[0], m, t[1]);
12885+
err = _sp_mulmod(t[1], t[0], m, t[1]);
1286412886
}
1286512887

1286612888
/* 4. For i in 2..(2 ^ w) - 1 */
@@ -13556,7 +13578,7 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1355613578
err = sp_mont_norm(t[0], m);
1355713579
if (err == MP_OKAY) {
1355813580
/* 2. Convert base to Montgomery form. */
13559-
err = sp_mulmod(bm, t[0], m, bm);
13581+
err = _sp_mulmod(bm, t[0], m, bm);
1356013582
}
1356113583
if (err == MP_OKAY) {
1356213584
/* Copy Montgomery form of base into first element of table. */
@@ -13807,7 +13829,7 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1380713829
err = sp_mont_norm(t[1], m);
1380813830
if (err == MP_OKAY) {
1380913831
/* 1. Convert base to Montgomery form. */
13810-
err = sp_mulmod(t[0], t[1], m, t[0]);
13832+
err = _sp_mulmod(t[0], t[1], m, t[0]);
1381113833
}
1381213834
if (err == MP_OKAY) {
1381313835
/* 2. Result starts as Montgomery form of base (assuming e > 0). */

0 commit comments

Comments
 (0)