Skip to content

Commit 57b2a9b

Browse files
[crypto] ML-DSA-87: Secure unmasking gadget
Signed-off-by: Andrea Caforio <andrea.caforio@lowrisc.org>
1 parent 61afe7b commit 57b2a9b

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

sw/otbn/crypto/mldsa87/mldsa87_gadgets.s

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
.globl sec_a2b_8x32
88
.globl sec_b2a_8x32
99
.globl sec_add_8x32
10+
.globl sec_unmask_8x32
1011

1112
/*
1213
@@ -133,3 +134,24 @@ sec_add_8x32:
133134
bn.wsrr w1, MAI_RES_S1
134135

135136
ret
137+
138+
/**
139+
* Securely unmask a vector of 8 Boolean-shared coefficients.
140+
*
141+
* This is an implementation of the `SecUnMask` function (Algorithm 3 in [1]).
142+
*
143+
* @param[in] w0: x0_B, first Boolean share of x
144+
* @param[in] w1: x1_B, second Boolean share of x.
145+
* @param[out] w0: x, unmasked value x.
146+
*/
147+
sec_unmask_8x32:
148+
/* Sample a fresh random mask and XOR it to the shares before unmasking. */
149+
bn.wsrr w20, RND
150+
151+
bn.xor w0, w0, w20
152+
bn.addi w31, w31, 0 /* dummy */
153+
bn.xor w1, w1, w20
154+
155+
bn.xor w0, w0, w1
156+
157+
ret

sw/otbn/crypto/mldsa87/tests/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ unit_tests = [
3636
"mldsa87_sec_a2b_test",
3737
"mldsa87_sec_b2a_test",
3838
"mldsa87_sec_add_test",
39+
"mldsa87_sec_unmask_test",
3940
]
4041

4142
[
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright lowRISC contributors (OpenTitan project).
2+
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
{
6+
"entrypoint": "main",
7+
"output": {
8+
"regs": {
9+
"w2": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
10+
}
11+
}
12+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Copyright lowRISC contributors (OpenTitan project). */
2+
/* Licensed under the Apache License, Version 2.0, see LICENSE for details. */
3+
/* SPDX-License-Identifier: Apache-2.0 */
4+
5+
/* Randomized test to verify the secure addition. */
6+
7+
.section .text.start
8+
9+
main:
10+
la x31, _stack
11+
bn.xor w31, w31, w31
12+
13+
la x2, _params
14+
bn.lid x0, 0(x2)
15+
bn.wsrw MOD, w0
16+
17+
bn.not w2, w31 /* flag */
18+
19+
/* Generate 100 random Boolean-shared vectors and verify that they can be
20+
correctly unmasked. */
21+
loopi 100, 7
22+
/* Random vectors x and y. */
23+
bn.wsrr w3, URND
24+
25+
/* Random masks. */
26+
bn.wsrr w4, URND
27+
28+
/* Create the two Boolean shares and trigger the conversion. */
29+
bn.xor w0, w3, w4
30+
bn.mov w1, w4
31+
jal x1, sec_unmask_8x32
32+
33+
/* Check that the unmask result is equal to the initial vector. */
34+
bn.cmp w0, w3, FG0
35+
bn.sel w2, w2, w31, FG0.Z
36+
/* End of loop */
37+
38+
ecall
39+
40+
.data
41+
.balign 32
42+
43+
_params:
44+
.word 0x007fe001 /* q */
45+
.word 0xfc7fdfff /* mu */
46+
.word 0x0000a3fa /* n^-1 * R^3 mod q */
47+
.word 0x00000000
48+
.word 0x00000000
49+
.word 0x00000000
50+
.word 0x00000000
51+
.word 0x00000000
52+
53+
_stack:
54+
.zero 4

0 commit comments

Comments
 (0)