Skip to content

[crypto/25519] Propagate the masking in x25519#30087

Merged
nasahlpa merged 2 commits into
lowRISC:earlgrey_1.0.0from
siemen11:x25519_masking_connection
May 22, 2026
Merged

[crypto/25519] Propagate the masking in x25519#30087
nasahlpa merged 2 commits into
lowRISC:earlgrey_1.0.0from
siemen11:x25519_masking_connection

Conversation

@siemen11
Copy link
Copy Markdown
Contributor

Propagate the masking of the keys in x25519 to the OTBN and use the masked scalar multiplication from ed25519.
Note that we still need the clamping of the private key hence we need to unmask.

@siemen11 siemen11 requested a review from a team as a code owner May 11, 2026 12:32
@siemen11 siemen11 requested review from alees24, andrea-caforio and nasahlpa and removed request for a team and alees24 May 11, 2026 12:32
@siemen11 siemen11 force-pushed the x25519_masking_connection branch 6 times, most recently from a288411 to b61454e Compare May 11, 2026 20:59
@siemen11 siemen11 marked this pull request as draft May 14, 2026 18:37
Comment thread sw/otbn/crypto/25519_scalar.s Outdated
bn.wsrr w22, URND
bn.rshi w22, w31, w22 >> 128

/* Clear the lowest 3 bits of k so k*L is a multiple of 8.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to instead NOT clamp k, instead shift k left by 3

or i guess practically simply only shift right by 125 and keep the clamping equals the same outcome of 128b random from 130:3

Copy link
Copy Markdown
Contributor Author

@siemen11 siemen11 May 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Johann! Small sidenote that this increases the loop ext_scmul_sca from 382 iterations to 385

@siemen11 siemen11 force-pushed the x25519_masking_connection branch 2 times, most recently from eac8454 to ed0c1d9 Compare May 19, 2026 21:40
@siemen11 siemen11 marked this pull request as ready for review May 19, 2026 21:40
@siemen11 siemen11 force-pushed the x25519_masking_connection branch 5 times, most recently from dca945e to 2450520 Compare May 20, 2026 19:14
@siemen11 siemen11 added the CI:Rerun Rerun failed CI jobs label May 21, 2026
@github-actions github-actions Bot removed the CI:Rerun Rerun failed CI jobs label May 21, 2026
@siemen11 siemen11 added the CI:Rerun Rerun failed CI jobs label May 21, 2026
@github-actions github-actions Bot removed the CI:Rerun Rerun failed CI jobs label May 21, 2026
Copy link
Copy Markdown
Contributor

@johannheyszl johannheyszl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great! also particularly nice we reuse the a2b b2a from p256 (does not save flash code size since included in both binaries, but less code in repo)

@andrea-caforio
Copy link
Copy Markdown
Contributor

I think the second commit might be redundant because its content is deleted in the last commit.

@siemen11 siemen11 force-pushed the x25519_masking_connection branch from 2450520 to d3be24a Compare May 21, 2026 12:39
@siemen11
Copy link
Copy Markdown
Contributor Author

I think the second commit might be redundant because its content is deleted in the last commit.

Thank you @andrea-caforio! I squashed the commits now, plus the masking commit is now its own PR in #30167

Copy link
Copy Markdown
Contributor

@nasahlpa nasahlpa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

Comment thread sw/otbn/crypto/p256_b2a.s Outdated
Comment thread sw/otbn/crypto/p256_b2a.s Outdated
Comment on lines +92 to +95
bn.xor w4, w4, w21

/* [w2, w1] <= [w2, w1] ^ [w11, w10] = gamma ^ s1 = G */
bn.xor w1, w1, w10
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a dummy instruction in between?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we overwrite w21 by w10 which is a transition from one share to another (though via glitching). Though, I switched the operands of bn.xor w1, w1, w10 instead

Comment thread sw/otbn/crypto/p256_b2a.s
Comment on lines +96 to +99
bn.xor w2, w2, w11

/* [w21, w20] <= [w21, w20] ^ [w2, w1] = s0 ^ G */
bn.xor w20, w20, w1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here.

Copy link
Copy Markdown
Contributor Author

@siemen11 siemen11 May 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here IIUC there is no direct overwrite as we go from w11 (s0) to w1 (gamma)

siemen11 added 2 commits May 21, 2026 15:44
Get the arithmetic_to_boolean and boolean_to_arithmetic to be in their
own files and resrtucture p256. This should have no impact on p256
itself.

Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
Propagate the masking of the keys in x25519 to the OTBN and use the
masked scalar multiplication from ed25519.
Change the sc_blind subroutine to blind with a clamped blinding factor
(adding 3 bits to the size).

Use the s2b and b2a from p256.
Note that due to the sharing change (additive versus substraction) we
switch the masking by adding a new mask B.

Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
@siemen11 siemen11 force-pushed the x25519_masking_connection branch from d3be24a to 581a639 Compare May 21, 2026 13:45
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know the fact that two "smallish" routines for A2B/B2A are sufficient to implement a masked X25519, questions the design decisions for P256/P384. Why is the masking there so insanely convoluted?

@nasahlpa nasahlpa merged commit b64522c into lowRISC:earlgrey_1.0.0 May 22, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants