Skip to content

Commit c3a8476

Browse files
committed
SP int: _sp_copy don't check a == b, change calls to _sp_copy
Simplify code to make it easier for software analysers. Reduce work done by using _sp_copy instead of sp_copy where possible.
1 parent bdd4535 commit c3a8476

1 file changed

Lines changed: 50 additions & 47 deletions

File tree

wolfcrypt/src/sp_int.c

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5059,22 +5059,19 @@ void sp_forcezero(sp_int* a)
50595059
*/
50605060
static void _sp_copy(const sp_int* a, sp_int* r)
50615061
{
5062-
/* Only copy if different pointers. */
5063-
if (a != r) {
5064-
/* Copy words across. */
5065-
if (a->used == 0) {
5066-
r->dp[0] = 0;
5067-
}
5068-
else {
5069-
XMEMCPY(r->dp, a->dp, a->used * SP_WORD_SIZEOF);
5070-
}
5071-
/* Set number of used words in result. */
5072-
r->used = a->used;
5073-
#ifdef WOLFSSL_SP_INT_NEGATIVE
5074-
/* Set sign of result. */
5075-
r->sign = a->sign;
5076-
#endif
5062+
/* Copy words across. */
5063+
if (a->used == 0) {
5064+
r->dp[0] = 0;
5065+
}
5066+
else {
5067+
XMEMCPY(r->dp, a->dp, a->used * SP_WORD_SIZEOF);
50775068
}
5069+
/* Set number of used words in result. */
5070+
r->used = a->used;
5071+
#ifdef WOLFSSL_SP_INT_NEGATIVE
5072+
/* Set sign of result. */
5073+
r->sign = a->sign;
5074+
#endif
50785075
}
50795076

50805077
/* Copy value of multi-precision number a into r.
@@ -5092,12 +5089,15 @@ int sp_copy(const sp_int* a, sp_int* r)
50925089
if ((a == NULL) || (r == NULL)) {
50935090
err = MP_VAL;
50945091
}
5095-
/* Validated space in result. */
5096-
if ((err == MP_OKAY) && (a->used > r->size)) {
5097-
err = MP_VAL;
5098-
}
5099-
if (err == MP_OKAY) {
5100-
_sp_copy(a, r);
5092+
/* Only copy if different pointers. */
5093+
if (a != r) {
5094+
/* Validated space in result. */
5095+
if ((err == MP_OKAY) && (a->used > r->size)) {
5096+
err = MP_VAL;
5097+
}
5098+
if (err == MP_OKAY) {
5099+
_sp_copy(a, r);
5100+
}
51015101
}
51025102

51035103
return err;
@@ -8374,7 +8374,7 @@ static int _sp_div(const sp_int* a, const sp_int* d, sp_int* r, sp_int* rem,
83748374
ret = _sp_cmp_abs(a, d);
83758375
if (ret == MP_LT) {
83768376
/* a = 0 * d + a */
8377-
if (rem != NULL) {
8377+
if ((rem != NULL) && (a != rem)) {
83788378
_sp_copy(a, rem);
83798379
}
83808380
if (r != NULL) {
@@ -8622,7 +8622,7 @@ static int _sp_mod(const sp_int* a, const sp_int* m, sp_int* r)
86228622
err = sp_add(t, m, r);
86238623
}
86248624
else {
8625-
err = sp_copy(t, r);
8625+
_sp_copy(t, r);
86268626
}
86278627
}
86288628
FREE_SP_INT(t, NULL);
@@ -11818,7 +11818,9 @@ static int _sp_invmod_bin(const sp_int* a, const sp_int* m, sp_int* u,
1181811818

1181911819
/* 1. u = m, v = a, b = 0, c = 1 */
1182011820
_sp_copy(m, u);
11821-
_sp_copy(a, v);
11821+
if (a != v) {
11822+
_sp_copy(a, v);
11823+
}
1182211824
_sp_zero(b);
1182311825
_sp_set(c, 1);
1182411826

@@ -11920,7 +11922,9 @@ static int _sp_invmod_div(const sp_int* a, const sp_int* m, sp_int* x,
1192011922
mp_init(d);
1192111923

1192211924
/* 1. x = m, y = a, b = 1, c = 0 */
11923-
_sp_copy(a, y);
11925+
if (a != y) {
11926+
_sp_copy(a, y);
11927+
}
1192411928
_sp_copy(m, x);
1192511929
_sp_set(b, 1);
1192611930
_sp_zero(c);
@@ -12128,7 +12132,7 @@ static int _sp_invmod(const sp_int* a, const sp_int* m, sp_int* r)
1212812132
}
1212912133
}
1213012134
else if (err == MP_OKAY) {
12131-
err = sp_copy(c, r);
12135+
_sp_copy(c, r);
1213212136
}
1213312137
}
1213412138

@@ -12290,7 +12294,7 @@ static int _sp_invmod_mont_ct(const sp_int* a, const sp_int* m, sp_int* r,
1229012294
/* 1. pre[0] = 2^0 * a mod m
1229112295
* Start with 1.a = a.
1229212296
*/
12293-
err = sp_copy(a, pre[0]);
12297+
_sp_copy(a, pre[0]);
1229412298
/* 2. For i in 2..CT_INV_MOD_PRE_CNT
1229512299
* For rest of entries in table.
1229612300
*/
@@ -12325,7 +12329,7 @@ static int _sp_invmod_mont_ct(const sp_int* a, const sp_int* m, sp_int* r,
1232512329
}
1232612330
}
1232712331
/* 3. Set tmp to product of leading bits. */
12328-
err = sp_copy(pre[j-1], t);
12332+
_sp_copy(pre[j-1], t);
1232912333

1233012334
/* 4. s = 0 */
1233112335
s = 0;
@@ -12402,7 +12406,7 @@ static int _sp_invmod_mont_ct(const sp_int* a, const sp_int* m, sp_int* r,
1240212406
}
1240312407
/* 9. Else r = t */
1240412408
else {
12405-
err = sp_copy(t, r);
12409+
_sp_copy(t, r);
1240612410
}
1240712411
}
1240812412

@@ -12535,15 +12539,15 @@ static int _sp_exptmod_ex(const sp_int* b, const sp_int* e, int bits,
1253512539
}
1253612540
else {
1253712541
/* Copy base into working variable. */
12538-
err = sp_copy(b, t[0]);
12542+
_sp_copy(b, t[0]);
1253912543
}
1254012544
}
1254112545

1254212546
if ((!done) && (err == MP_OKAY)) {
1254312547
/* 3. t[1] = t[0]
1254412548
* Set real working value to base.
1254512549
*/
12546-
err = sp_copy(t[0], t[1]);
12550+
_sp_copy(t[0], t[1]);
1254712551

1254812552
/* 4. For i in (bits-1)...0 */
1254912553
for (i = bits - 1; (err == MP_OKAY) && (i >= 0); i--) {
@@ -12591,7 +12595,7 @@ static int _sp_exptmod_ex(const sp_int* b, const sp_int* e, int bits,
1259112595
}
1259212596
if ((!done) && (err == MP_OKAY)) {
1259312597
/* 5. r = t[1] */
12594-
err = sp_copy(t[1], r);
12598+
_sp_copy(t[1], r);
1259512599
}
1259612600

1259712601
FREE_SP_INT_ARRAY(t, NULL);
@@ -12661,7 +12665,7 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1266112665
}
1266212666
else {
1266312667
/* Copy base into working variable. */
12664-
err = sp_copy(b, t[0]);
12668+
_sp_copy(b, t[0]);
1266512669
}
1266612670
}
1266712671

@@ -12732,7 +12736,7 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1273212736
}
1273312737
if ((!done) && (err == MP_OKAY)) {
1273412738
/* 8. r = t[1] */
12735-
err = sp_copy(t[1], r);
12739+
_sp_copy(t[1], r);
1273612740
}
1273712741

1273812742
FREE_SP_INT_ARRAY(t, NULL);
@@ -12842,7 +12846,7 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1284212846
}
1284312847
else {
1284412848
/* Copy base into entry of table to contain b^1. */
12845-
err = sp_copy(b, t[1]);
12849+
_sp_copy(b, t[1]);
1284612850
}
1284712851
}
1284812852

@@ -12954,7 +12958,7 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1295412958
}
1295512959
if ((!done) && (err == MP_OKAY)) {
1295612960
/* 8. r = tr */
12957-
err = sp_copy(tr, r);
12961+
_sp_copy(tr, r);
1295812962
}
1295912963

1296012964
FREE_SP_INT_ARRAY(t, NULL);
@@ -13188,7 +13192,7 @@ static int _sp_exptmod_base_2(const sp_int* e, int digits, const sp_int* m,
1318813192
}
1318913193
if (err == MP_OKAY) {
1319013194
/* 8. r = tr */
13191-
err = sp_copy(tr, r);
13195+
_sp_copy(tr, r);
1319213196
}
1319313197

1319413198
#if 0
@@ -13538,7 +13542,7 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1353813542
}
1353913543
else {
1354013544
/* Copy base into Montogmery base variable. */
13541-
err = sp_copy(b, bm);
13545+
_sp_copy(b, bm);
1354213546
}
1354313547
}
1354413548

@@ -13556,7 +13560,7 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1355613560
}
1355713561
if (err == MP_OKAY) {
1355813562
/* Copy Montgomery form of base into first element of table. */
13559-
err = sp_copy(bm, t[0]);
13563+
_sp_copy(bm, t[0]);
1356013564
}
1356113565
/* Calculate b^(2^(winBits-1)) */
1356213566
for (i = 1; (i < winBits) && (err == MP_OKAY); i++) {
@@ -13605,7 +13609,7 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1360513609
n <<= winBits;
1360613610
c -= winBits;
1360713611
}
13608-
err = sp_copy(t[y], tr);
13612+
_sp_copy(t[y], tr);
1360913613
}
1361013614
else {
1361113615
/* 1 in Montgomery form. */
@@ -13729,7 +13733,7 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1372913733
}
1373013734
if ((!done) && (err == MP_OKAY)) {
1373113735
/* Copy temporary result into parameter. */
13732-
err = sp_copy(tr, r);
13736+
_sp_copy(tr, r);
1373313737
}
1373413738

1373513739
#ifndef WOLFSSL_SP_NO_MALLOC
@@ -13792,7 +13796,7 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1379213796
}
1379313797
else {
1379413798
/* Copy base into temp. */
13795-
err = sp_copy(b, t[0]);
13799+
_sp_copy(b, t[0]);
1379613800
}
1379713801
}
1379813802

@@ -13838,7 +13842,7 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1383813842
}
1383913843
if ((!done) && (err == MP_OKAY)) {
1384013844
/* Copy temporary result into parameter. */
13841-
err = sp_copy(t[0], r);
13845+
_sp_copy(t[0], r);
1384213846
}
1384313847

1384413848
FREE_SP_INT_ARRAY(t, NULL);
@@ -17817,10 +17821,9 @@ int sp_todecimal(const sp_int* a, char* str)
1781717821

1781817822
ALLOC_SP_INT_SIZE(t, a->used + 1, err, NULL);
1781917823
if (err == MP_OKAY) {
17820-
err = sp_copy(a, t);
17824+
_sp_copy(a, t);
1782117825
}
1782217826
if (err == MP_OKAY) {
17823-
1782417827
#ifdef WOLFSSL_SP_INT_NEGATIVE
1782517828
if (a->sign == MP_NEG) {
1782617829
/* Add negative sign character. */
@@ -17969,7 +17972,7 @@ int sp_radix_size(const sp_int* a, int radix, int* size)
1796917972
ALLOC_SP_INT(t, a->used, err, NULL);
1797017973
if (err == MP_OKAY) {
1797117974
t->size = a->used;
17972-
err = sp_copy(a, t);
17975+
_sp_copy(a, t);
1797317976
}
1797417977

1797517978
if (err == MP_OKAY) {
@@ -18786,7 +18789,7 @@ static WC_INLINE int _sp_gcd(const sp_int* a, const sp_int* b, sp_int* r)
1878618789
}
1878718790
if (err == MP_OKAY) {
1878818791
/* 5. r = u */
18789-
err = sp_copy(u, r);
18792+
_sp_copy(u, r);
1879018793
}
1879118794

1879218795
FREE_SP_INT_ARRAY(d, NULL);

0 commit comments

Comments
 (0)