Skip to content

Commit 017fade

Browse files
authored
Merge pull request #12104 from SparkiDev/sp-ecc-recode-ct
SP ECC: constant-time scalar recoding
2 parents 58cc6d2 + fdd4406 commit 017fade

8 files changed

Lines changed: 408 additions & 794 deletions

File tree

‎wolfcrypt/src/sp_arm32.c‎

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -76968,24 +76968,6 @@ typedef struct ecc_recode_256 {
7696876968
word8 neg;
7696976969
} ecc_recode_256;
7697076970

76971-
/* The index into pre-computation table to use. */
76972-
static const word8 recode_index_8_6[66] = {
76973-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
76974-
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
76975-
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17,
76976-
16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
76977-
0, 1,
76978-
};
76979-
76980-
/* Whether to negate y-ordinate. */
76981-
static const word8 recode_neg_8_6[66] = {
76982-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76983-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76984-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
76985-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
76986-
0, 0,
76987-
};
76988-
7698976971
/* Recode the scalar for multiplication using pre-computed values and
7699076972
* subtraction.
7699176973
*
@@ -76997,6 +76979,8 @@ static void sp_256_ecc_recode_6_8(const sp_digit* k, ecc_recode_256* v)
7699776979
int i;
7699876980
int j;
7699976981
word8 y;
76982+
word8 neg;
76983+
word8 m;
7700076984
int carry = 0;
7700176985
int o;
7700276986
sp_digit n;
@@ -77025,9 +77009,16 @@ static void sp_256_ecc_recode_6_8(const sp_digit* k, ecc_recode_256* v)
7702577009
}
7702677010

7702777011
y = (word8)(y + carry);
77028-
v[i].i = recode_index_8_6[y];
77029-
v[i].neg = recode_neg_8_6[y];
77030-
carry = (y >> 6) + v[i].neg;
77012+
/* Constant time recoding - no secret dependent table lookup.
77013+
* y in [0, 31]: index y, no negate.
77014+
* y in [32, 63]: index 64 - y, negate.
77015+
* y in [64, 65]: index y - 64, no negate.
77016+
*/
77017+
neg = (word8)((y >> 5) & 1);
77018+
m = (word8)(0 - neg);
77019+
v[i].i = (word8)(((y ^ m) + neg) & 0x3f);
77020+
v[i].neg = neg;
77021+
carry = (y >> 6) + neg;
7703177022
}
7703277023
}
7703377024

@@ -95483,24 +95474,6 @@ typedef struct ecc_recode_384 {
9548395474
word8 neg;
9548495475
} ecc_recode_384;
9548595476

95486-
/* The index into pre-computation table to use. */
95487-
static const word8 recode_index_12_6[66] = {
95488-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
95489-
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
95490-
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17,
95491-
16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
95492-
0, 1,
95493-
};
95494-
95495-
/* Whether to negate y-ordinate. */
95496-
static const word8 recode_neg_12_6[66] = {
95497-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95498-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95499-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
95500-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
95501-
0, 0,
95502-
};
95503-
9550495477
/* Recode the scalar for multiplication using pre-computed values and
9550595478
* subtraction.
9550695479
*
@@ -95512,6 +95485,8 @@ static void sp_384_ecc_recode_6_12(const sp_digit* k, ecc_recode_384* v)
9551295485
int i;
9551395486
int j;
9551495487
word8 y;
95488+
word8 neg;
95489+
word8 m;
9551595490
int carry = 0;
9551695491
int o;
9551795492
sp_digit n;
@@ -95540,9 +95515,16 @@ static void sp_384_ecc_recode_6_12(const sp_digit* k, ecc_recode_384* v)
9554095515
}
9554195516

9554295517
y = (word8)(y + carry);
95543-
v[i].i = recode_index_12_6[y];
95544-
v[i].neg = recode_neg_12_6[y];
95545-
carry = (y >> 6) + v[i].neg;
95518+
/* Constant time recoding - no secret dependent table lookup.
95519+
* y in [0, 31]: index y, no negate.
95520+
* y in [32, 63]: index 64 - y, negate.
95521+
* y in [64, 65]: index y - 64, no negate.
95522+
*/
95523+
neg = (word8)((y >> 5) & 1);
95524+
m = (word8)(0 - neg);
95525+
v[i].i = (word8)(((y ^ m) + neg) & 0x3f);
95526+
v[i].neg = neg;
95527+
carry = (y >> 6) + neg;
9554695528
}
9554795529
}
9554895530

@@ -123148,24 +123130,6 @@ typedef struct ecc_recode_521 {
123148123130
word8 neg;
123149123131
} ecc_recode_521;
123150123132

123151-
/* The index into pre-computation table to use. */
123152-
static const word8 recode_index_17_6[66] = {
123153-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
123154-
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
123155-
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17,
123156-
16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
123157-
0, 1,
123158-
};
123159-
123160-
/* Whether to negate y-ordinate. */
123161-
static const word8 recode_neg_17_6[66] = {
123162-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123163-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123164-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
123165-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
123166-
0, 0,
123167-
};
123168-
123169123133
/* Recode the scalar for multiplication using pre-computed values and
123170123134
* subtraction.
123171123135
*
@@ -123177,6 +123141,8 @@ static void sp_521_ecc_recode_6_17(const sp_digit* k, ecc_recode_521* v)
123177123141
int i;
123178123142
int j;
123179123143
word8 y;
123144+
word8 neg;
123145+
word8 m;
123180123146
int carry = 0;
123181123147
int o;
123182123148
sp_digit n;
@@ -123205,9 +123171,16 @@ static void sp_521_ecc_recode_6_17(const sp_digit* k, ecc_recode_521* v)
123205123171
}
123206123172

123207123173
y = (word8)(y + carry);
123208-
v[i].i = recode_index_17_6[y];
123209-
v[i].neg = recode_neg_17_6[y];
123210-
carry = (y >> 6) + v[i].neg;
123174+
/* Constant time recoding - no secret dependent table lookup.
123175+
* y in [0, 31]: index y, no negate.
123176+
* y in [32, 63]: index 64 - y, negate.
123177+
* y in [64, 65]: index y - 64, no negate.
123178+
*/
123179+
neg = (word8)((y >> 5) & 1);
123180+
m = (word8)(0 - neg);
123181+
v[i].i = (word8)(((y ^ m) + neg) & 0x3f);
123182+
v[i].neg = neg;
123183+
carry = (y >> 6) + neg;
123211123184
}
123212123185
}
123213123186

0 commit comments

Comments
 (0)