Skip to content

Add SM2 implementation in generic riscv64 asm - #25918

Closed
geliyaz wants to merge 1 commit into
openssl:masterfrom
geliyaz:master
Closed

geliyaz wants to merge 1 commit into
openssl:masterfrom
geliyaz:master

Conversation

@geliyaz

@geliyaz geliyaz commented Nov 11, 2024 •

Copy link
Copy Markdown
Contributor

This PR adds the support of SM2 implementation for RISC-V 64 platform with base integer instruction set.

Testing
For testing, Built-in test suite was run using user-mode qemu. All tests pass.
First, configuring the build

$ CC="riscv64-unknown-linux-gnu-gcc" ./Configure linux64-riscv64 no-afalgeng
$ make -j <num threads>

Next, I ran the built-in test suite using make test. The summary is below:

All tests successful.
FFiles=327, Tests=3842, 8418 wallclock secs (99.68 usr 26.07 sys + 3654.55 cusr 3638.32 csys = 7418.62 CPU)
Result: PASS

benchmark results run on SG2042:

sign verify sign/s verify/s
Before 0.0024s 0.0019s 414.2 525.1
After 0.0002s 0.0005s 6367.7 1839.8
After(no-sm2-precomp) 0.0005s 0.0009s 1953.2 1112.8
Checklist
  • documentation is added or updated
  • tests are added or updated

@t8m t8m left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we get an independent review from another RISC-V developer?

Comment thread crypto/ec/build.info Resolved
@t8m t8m added branch: master Applies to master branch approval: review pending This pull request needs review by a committer triaged: feature The issue/pr requests/adds a feature tests: exempted The PR is exempt from requirements for testing triaged: performance The issue/pr reports/fixes a performance concern labels Nov 11, 2024
@geliyaz
geliyaz force-pushed the master branch 2 times, most recently from b724adb to 2af3a2a Compare November 12, 2024 03:16
@geliyaz

geliyaz commented Nov 12, 2024

Copy link
Copy Markdown
Contributor Author

Could we get an independent review from another RISC-V developer?

Sure!

@geliyaz
geliyaz requested a review from t8m November 12, 2024 06:16
@openssl-machine

Copy link
Copy Markdown
Collaborator

This PR is in a state where it requires action by @openssl/committers but the last update was 30 days ago

@geliyaz

geliyaz commented Dec 15, 2024

Copy link
Copy Markdown
Contributor Author

Could we get an independent review from another RISC-V developer?

how is the review going?

@t8m

t8m commented Dec 16, 2024

Copy link
Copy Markdown
Member

Please drop the merge commit - git reset --hard HEAD~ ; git push --force

@openssl-machine

Copy link
Copy Markdown
Collaborator

This PR is in a state where it requires action by @openssl/committers but the last update was 30 days ago

@paulidale

Copy link
Copy Markdown
Contributor

We still need one of the RISC-V savvy developers to have a look at this.

Comment thread crypto/ec/asm/ecp_sm2p256-riscv64.pl Outdated
add $b0, $t8, $b0

// Select based on carry
bltu $c0, $b0, ${mod}_mod_add

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

(Don't know much about either SM2 or RISC-V, but daring to ask) Is this branch a side-channel? Or is there no requirement for constant-time in this function?

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.

Thank you for pointing that out! We've made adjustments to ensure the function operates in constant-time, reducing the risk of side-channel attacks. Here's the updated code:

	slt $c0, $c0, $c3
	negw $c1, $c0
	addw $c0, $c0, -1

	and $t0, $t0, $c1
	and $t1, $t1, $c1
	and $t2, $t2, $c1
	and $t3, $t3, $c1
	and $t4, $t4, $c0
	and $t5, $t5, $c0
	and $t6, $t6, $c0
	and $t7, $t7, $c0
	or $t0, $t0, $t4
	or $t1, $t1, $t5
	or $t2, $t2, $t6
	or $t3, $t3, $t7

Let me know if you have any further concerns or suggestions!

@openssl-machine

Copy link
Copy Markdown
Collaborator

This PR is in a state where it requires action by @openssl/committers but the last update was 30 days ago

@openssl-machine

Copy link
Copy Markdown
Collaborator

This PR is in a state where it requires action by @openssl/committers but the last update was 30 days ago

@HeliC829

HeliC829 commented May 3, 2025

Copy link
Copy Markdown
Contributor

Can you provide benchmark results run on a real RISC-V board for comparison, like in #20754 ?

@geliyaz

geliyaz commented May 6, 2025

Copy link
Copy Markdown
Contributor Author

Can you provide benchmark results run on a real RISC-V board for comparison, like in #20754 ?

Yes, I've updated the benchmark results run on a real RISC-V board(SG2042)

@geliyaz
geliyaz requested a review from vdukhovni May 19, 2025 10:35
@openssl-machine

Copy link
Copy Markdown
Collaborator

This PR is in a state where it requires action by @openssl/committers but the last update was 30 days ago

@slontis

slontis commented Aug 12, 2025

Copy link
Copy Markdown
Member

You should be able to see it...
It just says
Style report for commits 6b93db7..a9037a5
crypto/x509/v3_bitst.c:51:missing blank line after local decls:

Not sure how this relates to your PR. As you are just doing asm changes you can ignore it.. It will get skipped when it runs next as I added a label to skip it.

@geliyaz

geliyaz commented Aug 13, 2025

Copy link
Copy Markdown
Contributor Author

You should be able to see it... It just says Style report for commits 6b93db7..a9037a5 crypto/x509/v3_bitst.c:51:missing blank line after local decls:

Not sure how this relates to your PR. As you are just doing asm changes you can ignore it.. It will get skipped when it runs next as I added a label to skip it.

Not sure why, but on my end I'm only seeing a generic 'Error' message without detailed logs.
Thank you for helping resolve the check issue.
How to retrigger the check jobs?

@paulidale

Copy link
Copy Markdown
Contributor

Click on the "Style report" item about the error.

@geliyaz

geliyaz commented Aug 13, 2025

Copy link
Copy Markdown
Contributor Author

Click on the "Style report" item about the error.

This appears to be a Chrome-specific logging issue (detailed logs were visible when I switched to Firefox)

I've investigated the style check failure and confirmed it's unrelated to my changes:
The reported issue (crypto/x509/v3_bitst.c:51: missing blank line) is in a file I didn't modify.
slontis added the 'style: waived' label. Could you please re-run the CI check? Or if any action is required from my side?

@geliyaz

geliyaz commented Aug 14, 2025

Copy link
Copy Markdown
Contributor Author

Ping @vdukhovni for review.

@geliyaz

geliyaz commented Aug 19, 2025

Copy link
Copy Markdown
Contributor Author

@vdukhovni Could you please take a look when you have time? Let me know if you have any questions. Thanks!

@geliyaz

geliyaz commented Aug 25, 2025

Copy link
Copy Markdown
Contributor Author

@t8m @paulidale gentle ping for admin help on this one.
Merging is blocked by a required review from @vdukhovni (who may be too busy)
Could you please help us move this forward by merging it?

@geliyaz

geliyaz commented Sep 3, 2025

Copy link
Copy Markdown
Contributor Author

@t8m @slontis Could you please help move this PR forward for merging? Is there anything else required from our side?

@paulidale

Copy link
Copy Markdown
Contributor

I squashed the two commits and updated to the current master which has the improved RISC-V coverage testing.

@slontis slontis added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Sep 3, 2025
@openssl-machine

Copy link
Copy Markdown
Collaborator

24 hours has passed since 'approval: done' was set, but as this PR has been updated in that time the label 'approval: ready to merge' is not being automatically set. Please review the updates and set the label manually.

@openssl-machine openssl-machine added approval: ready to merge The 24 hour grace period has passed, ready to merge and removed approval: done This pull request has the required number of approvals labels Sep 4, 2025
@openssl-machine

Copy link
Copy Markdown
Collaborator

This pull request is ready to merge

openssl-machine pushed a commit that referenced this pull request Sep 8, 2025
Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Shane Lontis <[email protected]>
(Merged from #25918)
@slontis

slontis commented Sep 8, 2025

Copy link
Copy Markdown
Member

Merged back to master.. Thanks for your contribution and patience.

@slontis slontis closed this Sep 8, 2025
@t8m

t8m commented Oct 2, 2025

Copy link
Copy Markdown
Member

@geliyaz Unfortunately this does not work correctly anymore with dff94db

I've opened #28731

openssl-machine pushed a commit that referenced this pull request Jul 20, 2026
The ecp_sm2p256-riscv64.pl generator switches to .section .rodata
to emit constant data (.Lpoly, .Lord, .Lpoly_div_2, .Lord_div_2), but
never switches back before emitting function code.

As a result, the function symbols defined by this file are assembled
into .rodata instead of executable .text. On systems enforcing NX for
.rodata, calling the RISC-V64 SM2 assembly faults immediately.

Fix this by adding .previous after the constant block, restoring the
initial .text section before function emission.

Verified with readelf: before the fix, .text is empty and the function
symbols are in .rodata; after the fix, constants remain in .rodata and
the function symbols are in executable .text.

Introduced in commit 05301b1 (PR #25918).

Reviewed-by: Paul Yang <[email protected]>
Reviewed-by: Saša Nedvědický <[email protected]>
MergeDate: Mon Jul 20 09:47:49 2026
(Merged from #31874)
openssl-machine pushed a commit that referenced this pull request Jul 20, 2026
The ecp_sm2p256-riscv64.pl generator switches to .section .rodata
to emit constant data (.Lpoly, .Lord, .Lpoly_div_2, .Lord_div_2), but
never switches back before emitting function code.

As a result, the function symbols defined by this file are assembled
into .rodata instead of executable .text. On systems enforcing NX for
.rodata, calling the RISC-V64 SM2 assembly faults immediately.

Fix this by adding .previous after the constant block, restoring the
initial .text section before function emission.

Verified with readelf: before the fix, .text is empty and the function
symbols are in .rodata; after the fix, constants remain in .rodata and
the function symbols are in executable .text.

Introduced in commit 05301b1 (PR #25918).

Reviewed-by: Paul Yang <[email protected]>
Reviewed-by: Saša Nedvědický <[email protected]>
MergeDate: Mon Jul 20 09:47:35 2026
(Merged from #31874)
rickyringler pushed a commit to rickyringler/openssl that referenced this pull request Aug 21, 2026
The ecp_sm2p256-riscv64.pl generator switches to .section .rodata
to emit constant data (.Lpoly, .Lord, .Lpoly_div_2, .Lord_div_2), but
never switches back before emitting function code.

As a result, the function symbols defined by this file are assembled
into .rodata instead of executable .text. On systems enforcing NX for
.rodata, calling the RISC-V64 SM2 assembly faults immediately.

Fix this by adding .previous after the constant block, restoring the
initial .text section before function emission.

Verified with readelf: before the fix, .text is empty and the function
symbols are in .rodata; after the fix, constants remain in .rodata and
the function symbols are in executable .text.

Introduced in commit 05301b1 (PR openssl#25918).

Reviewed-by: Paul Yang <[email protected]>
Reviewed-by: Saša Nedvědický <[email protected]>
MergeDate: Mon Jul 20 09:47:35 2026
(Merged from openssl#31874)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: ready to merge The 24 hour grace period has passed, ready to merge branch: master Applies to master branch style: waived exempted from style checks tests: exempted The PR is exempt from requirements for testing triaged: feature The issue/pr requests/adds a feature triaged: performance The issue/pr reports/fixes a performance concern

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants