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

ml-kem: fix potential kyberslash attack #18

Merged
merged 9 commits into from
Jun 4, 2024

Conversation

supinie
Copy link
Contributor

@supinie supinie commented May 30, 2024

Updates the compress method to remove division of secret values by public value (q) as described in the kyberslash attack

Also updates tests to use inequality as per 4.7 in FIPS 203 draft to not use floating point arithmetic.

@tarcieri tarcieri changed the title fix potential kyberslash attack ml-kem: fix potential kyberslash attack May 30, 2024
@tarcieri
Copy link
Member

You should be able to enable the integer_division_remainder_used clippy lint to ban the use of division entirely (though it seems there's still some / 2 you may need to convert to >> 1): rust-lang/rust-clippy#12451

@tarcieri tarcieri requested a review from bifurcation May 30, 2024 14:37
@supinie
Copy link
Contributor Author

supinie commented May 30, 2024

I have updated the / 2 occurances in compress.rs, but did not add the integer_division_remainder_used as % is used in a number of other places, as well as some other integer divisions, although these divisions are only on fully public values to generate consts.

I am still working to understand why the nist tests failed with deterministic feature set.

Copy link
Contributor

@bifurcation bifurcation left a comment

Choose a reason for hiding this comment

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

Couple of nits, but overall LGTM. Thanks!

ml-kem/src/compress.rs Outdated Show resolved Hide resolved
ml-kem/src/compress.rs Outdated Show resolved Hide resolved
ml-kem/src/compress.rs Outdated Show resolved Hide resolved
ml-kem/src/compress.rs Outdated Show resolved Hide resolved
@tarcieri
Copy link
Member

but did not add the integer_division_remainder_used as % is used in a number of other places

You can explicitly annotate the places which operate on public values with allow(integer_division_remainder_used) or I can add it in a followup PR.

@supinie
Copy link
Contributor Author

supinie commented Jun 1, 2024

I have updated the initial commit as per @bifurcation 's comments as well as adding the integer_division_remainder_used lint but am still stuck on why the KAT nist tests fail whilst no others do.

ml-kem/src/lib.rs Outdated Show resolved Hide resolved
ml-kem/src/compress.rs Outdated Show resolved Hide resolved
@supinie
Copy link
Contributor Author

supinie commented Jun 3, 2024

in response to @bifurcation 's comment (I am unable to in a thread):

Yes, of course, I don't know how I didn't see that...

I've done a little bit of testing and it appears that it is not always true that x >= Decompress(Compress(x)), for example when x = 833 when d = 1. This is the case for both the new proposed and old compress. I do believe that this is expected output, and Compress should output 1 for 833 <= x < 2497 with 0 otherwise, and Decompress returning q/2 for 1 and 0 otherwise (when d = 1).

I have also done some testing of the preservation of input under Compress(Decompress(y)), and again, for both versions of compress, this does not hold.

Here is a link to a playground demonstrating my findings on the above. I will continue to do work on this, but hope you will be able to give some insight on the issue.

@tarcieri
Copy link
Member

tarcieri commented Jun 3, 2024

re: integer_division_remainder_used you'll need to bump the clippy version used in CI. You can bump it to the latest stable unless there's additional lint failures.

@supinie
Copy link
Contributor Author

supinie commented Jun 3, 2024

Will bumping the clippy version in tests also affect the MSRV? I've also been having trouble running that lint on my local on stable (despite my stable being v1.78.0) and only having it recognised on nightly/beta.

@tarcieri
Copy link
Member

tarcieri commented Jun 3, 2024

Nope, clippy is just a linter and doesn’t impact MSRV

@tarcieri
Copy link
Member

tarcieri commented Jun 3, 2024

Sidebar: it doesn't look like we're impacted by this? https://groups.google.com/a/list.nist.gov/g/pqc-forum/c/hqbtIGFKIpU/m/cnE3pbueBgAJ

@bifurcation
Copy link
Contributor

OK, I played around with this a little:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=08afd3b14736eee45eeb1cbf59a70406

  1. It would be good to make test cases that do a strict known-answer test, using num_rational::Ratio as ground-truth.
  2. In order to pass that test, I had to change the definition of Q_HALF to be (Q+1)/2 (not (Q-1)/2).
  3. Correctly computing $\mod^{\pm}$ and taking the absolute value, we do in fact pass the Equation 4.7 test.

@bifurcation
Copy link
Contributor

Sidebar: it doesn't look like we're impacted by this? https://groups.google.com/a/list.nist.gov/g/pqc-forum/c/hqbtIGFKIpU/m/cnE3pbueBgAJ

Doesn't seem obvious to me. Filed #25 to track this.

@supinie
Copy link
Contributor Author

supinie commented Jun 4, 2024

Thanks @bifurcation for that, I have updated all the tests and the clippy version in the workspace actions.

Copy link
Contributor

@bifurcation bifurcation left a comment

Choose a reason for hiding this comment

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

A few minor / aesthetic comments, but overall, this is getting pretty much done.

ml-kem/src/compress.rs Outdated Show resolved Hide resolved
ml-kem/src/compress.rs Outdated Show resolved Hide resolved
ml-kem/src/compress.rs Outdated Show resolved Hide resolved
ml-kem/src/compress.rs Outdated Show resolved Hide resolved
ml-kem/src/compress.rs Outdated Show resolved Hide resolved
ml-kem/src/compress.rs Outdated Show resolved Hide resolved
ml-kem/src/compress.rs Outdated Show resolved Hide resolved
ml-kem/src/compress.rs Outdated Show resolved Hide resolved
@tarcieri
Copy link
Member

tarcieri commented Jun 4, 2024

Build is green as of 3a205fd

@supinie
Copy link
Contributor Author

supinie commented Jun 4, 2024

pushed changes re. @bifurcation comments

@bifurcation
Copy link
Contributor

@tarcieri I'm good with merging this if you are.

@tarcieri tarcieri merged commit 3a0545c into RustCrypto:master Jun 4, 2024
13 checks passed
@tarcieri
Copy link
Member

tarcieri commented Jun 4, 2024

We should probably cut a release with this fix.

Should we file a RUSTSEC advisory as well?

@tarcieri tarcieri mentioned this pull request Jun 4, 2024
@tarcieri
Copy link
Member

tarcieri commented Jun 4, 2024

Released as v0.1.1 in #28

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.

None yet

3 participants