Skip to content

Commit 71f3306

Browse files
committed
[sw,cryptolib] Fix trigger_fault_if_fg0 functions
The trigger_fault_if_fg0 functions had a bug where we never trigger a fault. This new implementation should now correctly trigger the fault. For the case where no fault is triggered, we load address 0 into w31. For the error case we try to load address 0 into w39 (which doesn't exist), which triggers a ILLEGAL_INSN error. Signed-off-by: Hakim Filali <[email protected]>
1 parent 44a0b23 commit 71f3306

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

sw/otbn/crypto/p256_base.s

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,27 @@
4343
* sensitive; since aborting the program will be quicker than completing it,
4444
* the flag's value is likely clearly visible to an attacker through timing.
4545
*
46-
* @param[in] w31: all-zero
47-
* @param[in] FG0.Z: boolean indicating fault condition
46+
* @param[in] FG0.Z: boolean indicating fault condition when 1
4847
*
49-
* clobbered registers: x2
48+
* clobbered registers: x2, w31
5049
* clobbered flag groups: none
5150
*/
5251
trigger_fault_if_fg0_z:
5352
/* Read the FG0.Z flag (position 3).
5453
x2 <= FG0.Z */
5554
csrrw x2, FG0, x0
5655
andi x2, x2, 8
57-
srli x2, x2, 3
56+
addi x2, x2, 31
5857

59-
/* Subtract FG0.Z from 0.
60-
x2 <= 0 - x2 = FG0.Z ? 2^32 - 1 : 0 */
61-
sub x2, x0, x2
62-
63-
/* The `bn.lid` instruction causes an `BAD_DATA_ADDR` error if the
64-
memory address is out of bounds. Therefore, if FG0.Z is 1, this
65-
instruction causes an error, but if FG0.Z is 0 it simply loads the word at
66-
address 0 into w31. */
67-
li x3, 31
68-
bn.lid x3, 0(x2)
58+
/* The `bn.lid` instruction causes an `ILLEGAL_INSN` error if the index of the
59+
bignum register (stored in x2 in this case) is invalid. Therefore, if FG0.Z
60+
is 1, this instruction causes an error, but if FG0.Z is 0 it simply loads
61+
the word at address 0 into w31. */
62+
bn.lid x2, 0(x0)
6963

7064
/* If we get here, the flag must have been 0. Restore w31 to zero and return.
7165
w31 <= 0 */
72-
bn.xor w31, w31, w31
66+
bn.xor w31, w31, w31
7367

7468
ret
7569

@@ -84,29 +78,28 @@ trigger_fault_if_fg0_z:
8478
* sensitive; since aborting the program will be quicker than completing it,
8579
* the flag's value is likely clearly visible to an attacker through timing.
8680
*
87-
* @param[in] w31: all-zero
88-
* @param[in] FG0.Z: boolean indicating fault condition
81+
* @param[in] FG0.Z: boolean indicating fault condition when 0
8982
*
90-
* clobbered registers: x2
83+
* clobbered registers: x2, w31
9184
* clobbered flag groups: none
9285
*/
9386
trigger_fault_if_fg0_not_z:
9487
/* Read the FG0.Z flag (position 3).
9588
x2 <= FG0.Z */
9689
csrrw x2, FG0, x0
9790
andi x2, x2, 8
98-
slli x2, x2, 3
91+
xori x2, x2, 8
92+
addi x2, x2, 31
9993

100-
/* The `bn.lid` instruction causes an `BAD_DATA_ADDR` error if the
101-
memory address is out of bounds. Therefore, if FG0.Z is 1, this
102-
instruction causes an error, but if FG0.Z is 0 it simply loads the word at
103-
address 0 into w31. */
104-
li x3, 31
105-
bn.lid x3, 0(x2)
94+
/* The `bn.lid` instruction causes an `ILLEGAL_INSN` error if the index of the
95+
bignum register (stored in x2 in this case) is invalid. Therefore, if FG0.Z
96+
is 0, this instruction causes an error, but if FG0.Z is 1 it simply loads
97+
the word at address 0 into w31. */
98+
bn.lid x2, 0(x0)
10699

107100
/* If we get here, the flag must have been 1. Restore w31 to zero and return.
108101
w31 <= 0 */
109-
bn.xor w31, w31, w31
102+
bn.xor w31, w31, w31
110103

111104
ret
112105

sw/otbn/crypto/p384_isoncurve.s

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
* sensitive; since aborting the program will be quicker than completing it,
2828
* the flag's value is likely clearly visible to an attacker through timing.
2929
*
30-
* @param[in] w31: all-zero
31-
* @param[in] FG0.Z: boolean indicating fault condition
30+
* @param[in] FG0.Z: boolean indicating fault condition when 0
3231
*
33-
* clobbered registers: x2, x3
32+
* clobbered registers: x2, w31
3433
* clobbered flag groups: none
3534
*/
3635
.globl trigger_fault_if_fg0_not_z
@@ -39,18 +38,18 @@ trigger_fault_if_fg0_not_z:
3938
x2 <= FG0.Z */
4039
csrrw x2, FG0, x0
4140
andi x2, x2, 8
42-
slli x2, x2, 3
41+
xori x2, x2, 8
42+
addi x2, x2, 31
4343

44-
/* The `bn.lid` instruction causes an `BAD_DATA_ADDR` error if the
45-
memory address is out of bounds. Therefore, if FG0.Z is 1, this
46-
instruction causes an error, but if FG0.Z is 0 it simply loads the word at
47-
address 0 into w31. */
48-
li x3, 31
49-
bn.lid x3, 0(x2)
44+
/* The `bn.lid` instruction causes an `ILLEGAL_INSN` error if the index of the
45+
bignum register (stored in x2 in this case) is invalid. Therefore, if FG0.Z
46+
is 1, this instruction causes an error, but if FG0.Z is 0 it simply loads
47+
the word at address 0 into w31. */
48+
bn.lid x2, 0(x0)
5049

5150
/* If we get here, the flag must have been 1. Restore w31 to zero and return.
5251
w31 <= 0 */
53-
bn.xor w31, w31, w31
52+
bn.xor w31, w31, w31
5453

5554
ret
5655

0 commit comments

Comments
 (0)