Skip to content

Commit 19a6c46

Browse files
[crypto] ML-DSA-87: Further improve secret isolation prevention
The previous attempt at doing was not watertight. This commit improves it by making sure that really all registers are initialized with randomness. Signed-off-by: Andrea Caforio <andrea.caforio@lowrisc.org>
1 parent eddde1f commit 19a6c46

3 files changed

Lines changed: 34 additions & 20 deletions

File tree

sw/otbn/crypto/mldsa87/keygen/mldsa87_keygen_sample.s

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,20 @@ rej_bounded_poly:
6464
addi x7, x0, 0
6565
addi x8, x0, 1
6666

67+
/* Initialize the WDRs that hold intermediate results with randomness. */
68+
bn.wsrr w4, URND
69+
bn.wsrr w5, URND
70+
bn.wsrr w10, URND
71+
bn.wsrr w11, URND
72+
6773
/*
6874
* The following loop unfolds in two parts. First, rejection sample a
6975
* Boolean shared vector x consisting of 8 4-bit coefficients in the interval
7076
* [0, 14]. Second, compute x mod 5 and convert the coefficients to
7177
* arithmetic shares. Repeat this 32 times until all the coefficients of the
7278
* polynomial have been sampled.
7379
*/
74-
loopi 32, 42
75-
76-
/* Initialize the WDRs that hold intermediate results with randomness. */
77-
bn.wsrr w4, URND
78-
bn.wsrr w5, URND
79-
bn.wsrr w10, URND
80-
bn.wsrr w11, URND
81-
80+
loopi 32, 38
8281
loopi 8, 27
8382
/* If the squeezed buffer is empty re-squeeze a new batch of 64 4-bit
8483
coefficients. */

sw/otbn/crypto/mldsa87/mldsa87_decoding.s

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,13 @@ _bit_unpack_s:
9999
bn.not w13, w31
100100
bn.shv.8s w13, w13 >> 29
101101

102+
/* Initialize the WDRs that hold intermediate results with randomness. */
103+
bn.wsrr w0, URND
104+
bn.wsrr w1, URND
105+
102106
/* In each iteration, we decode 8 Boolean-shared coefficients that are
103107
bit-unpacked and converted to arithmetic shares in w0 and w1. */
104-
loopi 8, 21
105-
106-
/* Initialize the WDRs that hold intermediate results with randomness. */
107-
bn.wsrr w0, URND
108-
bn.wsrr w1, URND
109-
108+
loopi 8, 19
110109
loopi 8, 9
111110

112111
/* Randomness to shift into registers when a coefficient is extracted.

sw/otbn/crypto/mldsa87/sign/mldsa87_sign_sample.s

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,28 @@ sample_mask_poly:
5151
addi x4, x0, 0
5252
addi x5, x0, 1
5353

54+
/* Initialize the registers that hold the compressed polynomial shares with
55+
randomness. This avoids isolating secrets bits in an all-zero register
56+
during the shifting operations. */
57+
58+
/* Share 0. */
59+
bn.wsrr w0, URND
60+
bn.wsrr w3, URND
61+
bn.wsrr w4, URND
62+
bn.wsrr w5, URND
63+
bn.wsrr w6, URND
64+
bn.wsrr w7, URND
65+
66+
/* Share 1. */
67+
bn.wsrr w1, URND
68+
bn.wsrr w8, URND
69+
bn.wsrr w9, URND
70+
bn.wsrr w10, URND
71+
bn.wsrr w11, URND
72+
bn.wsrr w12, URND
73+
5474
/* In each iteration, we sample 64 coefficients. */
55-
loopi 4, 51
75+
loopi 4, 49
5676

5777
/*
5878
* Each coefficient of the mask polynomial has a size of 20 bits. Since
@@ -87,11 +107,7 @@ sample_mask_poly:
87107
bn.mov w12, w30
88108

89109
/* Sample 64 coefficients in steps of eight at at time. */
90-
loopi 8, 29
91-
92-
/* Initialize the WDRs that hold intermediate results with randomness. */
93-
bn.wsrr w0, URND
94-
bn.wsrr w1, URND
110+
loopi 8, 27
95111

96112
/* Sample one shared vector of eight coefficients. */
97113
loopi 8, 17

0 commit comments

Comments
 (0)