Update Kyber poly_tomsg to fix timing leak (w/ -Os)#320
Merged
Conversation
This (partially) addresses #319. The function poly_tomsg from the reference implementation of Kyber (which was copied into the M4-optimized implementations) would result in a variable-time udiv instruction operating on secret data when compiled with gcc using -Os. I tried a couple of versions from gcc 11 to gcc 13, but did not see any difference. This commit updates the m4-specific code to use the patch from pq-crystals/kyber@dda29cc. Note that the code in PQClean has not yet been updated and hence the clean implementation within pqm4 is still vulnerable.
Contributor
|
No difference between the GCC 11-13, with |
smuellerDD
added a commit
to smuellerDD/leancrypto
that referenced
this pull request
Jan 25, 2024
This is a port of issue PQClean/PQClean#534 Description from mupq/pqm4#320: The function poly_tomsg from the reference implementation of Kyber (which was copied into the M4-optimized implementations) would result in a variable-time udiv instruction operating on secret data when compiled with gcc using -Os. I tried a couple of versions from gcc 11 to gcc 13, but did not see any difference. Description from mupq/pqm4#319 This bit of code or similar is used in your various implementations of Kyber to compress a polynomial ring element into a (secret) message: ``` t = (((a->coeffs[8 * i + j] << 1) + KYBER_Q / 2) / KYBER_Q) & 1; ``` To do so, it performs a division by Q that might not necessarily compile to a multiplication instruction: looking at the output of some C compilers using https://godbolt.org/z/sKn3TKKGq and https://godbolt.org/z/8GqKoTfYh for example, a division instruction is emitted even when -O3 is specified. Should a division instruction be emitted, its execution time would likely be variable and leak information about its secret input. Signed-off-by: Stephan Mueller <smueller@chronox.de>
infra-riderju74
added a commit
to infra-riderju74/leancrypto
that referenced
this pull request
Sep 28, 2025
This is a port of issue PQClean/PQClean#534 Description from mupq/pqm4#320: The function poly_tomsg from the reference implementation of Kyber (which was copied into the M4-optimized implementations) would result in a variable-time udiv instruction operating on secret data when compiled with gcc using -Os. I tried a couple of versions from gcc 11 to gcc 13, but did not see any difference. Description from mupq/pqm4#319 This bit of code or similar is used in your various implementations of Kyber to compress a polynomial ring element into a (secret) message: ``` t = (((a->coeffs[8 * i + j] << 1) + KYBER_Q / 2) / KYBER_Q) & 1; ``` To do so, it performs a division by Q that might not necessarily compile to a multiplication instruction: looking at the output of some C compilers using https://godbolt.org/z/sKn3TKKGq and https://godbolt.org/z/8GqKoTfYh for example, a division instruction is emitted even when -O3 is specified. Should a division instruction be emitted, its execution time would likely be variable and leak information about its secret input. Signed-off-by: Stephan Mueller <smueller@chronox.de>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This (partially) addresses #319.
The function poly_tomsg from the reference implementation of Kyber (which was copied into the M4-optimized implementations) would result in a variable-time udiv instruction operating on secret data when compiled with gcc using -Os. I tried a couple of versions from gcc 11 to gcc 13, but did not see any difference.
This commit updates the m4-specific code to use the patch from pq-crystals/kyber@dda29cc. Note that the code in PQClean has not yet been updated and hence the clean implementation within pqm4 is still vulnerable.