Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid curve25519 "left shift of negative value" #333

Merged
merged 1 commit into from
Dec 4, 2024
Merged

Conversation

mkj
Copy link
Owner

@mkj mkj commented Dec 4, 2024

Cast to unsigned before performing the left shift. The shifted result is then sign extended back before subtraction, giving the same output. Generated assembly is unaltered with this change, checking gcc 14.2.0 and clang 19.1.1 (x86-64).

The same unsigned cast fix is present in crypto++'s tweetnacl version. weidai11/cryptopp@5be0c0a#diff-067e6f20b212f32eb7fcdec3395fb1d145612f5d44736d041c1dfc9bc44a9d5b
Jeffery Walton's modification to modL() is also applied here.

Running with -fsanitize=undefined could report a left shift of a negative value from car25519

        o[i]-=c<<16;

This is valid for gcc (and presumably clang), but the sanitizer report is a problem.
https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Integers-implementation.html

Fixes #312 on github

Cast to unsigned before performing the left shift. The shifted result is
then sign extended back before subtraction, giving the same output.
Generated assembly is unaltered with this change, checking gcc 14.2.0
and clang 19.1.1 (x86-64).

The same unsigned cast fix is present in crypto++'s tweetnacl version.
weidai11/cryptopp@5be0c0a#diff-067e6f20b212f32eb7fcdec3395fb1d145612f5d44736d041c1dfc9bc44a9d5b
Jeffery Walton's modification to modL() is also applied here.

Running with -fsanitize=undefined could report a left shift of a negative
value from car25519
        o[i]-=c<<16;

This is valid for gcc (and presumably clang), but the sanitizer report
is a problem.
https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Integers-implementation.html

Fixes #312 on github
@mkj mkj merged commit ae19826 into master Dec 4, 2024
17 checks passed
@mkj mkj deleted the pr/tweetnacl-shift branch December 4, 2024 16:13
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.

curve255.19.c:91:12: runtime error: left shift of negative value -329
1 participant