generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 155
Import mldsa-native #2849
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
Open
jakemas
wants to merge
25
commits into
aws:main
Choose a base branch
from
jakemas:import-mldsa-native
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+13,014
−5,943
Open
Import mldsa-native #2849
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
e0d56e0
Import mldsa-native
jakemas 36f59e8
static and inline config
jakemas cec4b95
Apply new API to modulewapper
jakemas 6ea2f37
add statics to config
jakemas 94d6256
Fix PCT break return value
jakemas 985551c
Test trampoline function for e_aes and gcm to fix delocator issue in bcm
jakemas 92f0293
more delocator attempts
jakemas 167e828
update e_aes.c
jakemas 79ee751
delocator issue found
jakemas a3da447
mode wrapper addition
jakemas 27a9f1d
debug CI output to show problematic instruction
jakemas 38872b4
Revert "debug CI output to show problematic instruction"
jakemas cc45a8b
test delocator peg fix for division
jakemas 5980e8f
Merge branch 'main' into import-mldsa-native
jakemas be91c0f
remove delocator fixes on aes gcm code
jakemas 1b3b661
AES GCM trampoline functions to fix delocator
jakemas 41b1d1c
test fix for windows-11-arm
jakemas f4ba7c1
test window-11-arm fix part 2
jakemas 9dcd2aa
Merge origin/main into import-mldsa-native - Keep deletion of ml_dsa_…
jakemas 9272e31
test fix for windows-11-arm
jakemas 65eebb1
Revert "test fix for windows-11-arm"
jakemas a154781
preserve mode_wrapper conditional logic
jakemas 480537e
Revert "preserve mode_wrapper conditional logic"
jakemas 798a877
Merge branch 'main' into import-mldsa-native
jakemas 3aa7951
fix modulewrapper rebase
jakemas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| name: mldsa-native | ||
| source: pq-code-package/mldsa-native.git | ||
| branch: mldsa-pk-from-sk | ||
| commit: bd3181cd84eaba93a38a05461eed771290768e23 | ||
| imported-at: 2025-11-19T11:54:14-0800 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| # ML-DSA | ||
|
|
||
| The source code in this directory implements ML-DSA as defined in | ||
| the [FIPS 204 Module-Lattice-Based Digital Signature Standard](https://csrc.nist.gov/pubs/fips/204/final). | ||
| It is imported from [mldsa-native](https://github.com/pq-code-package/mldsa-native) | ||
| using [importer.sh](importer.sh); see [META.yml](META.yml) for import details. | ||
|
|
||
| ## Running the importer | ||
|
|
||
| To re-run the importer, do | ||
|
|
||
| ```bash | ||
| rm -rf mldsa # Remove old mldsa source | ||
| ./importer.sh | ||
| ``` | ||
|
|
||
| By default, the importer will not run if [mldsa](mldsa) already/still exists. To force removal of any existing [mldsa](mldsa), use `./importer.sh --force`. | ||
|
|
||
| The repository and branch to be used for the import can be configured through the environment variables `GITHUB_REPOSITORY` and `GITHUB_SHA`, respectively. The default is equivalent to | ||
|
|
||
| ```bash | ||
| GITHUB_REPOSITORY=pq-code-package/mldsa-native.git GITHUB_SHA=main ./importer.sh | ||
| ``` | ||
|
|
||
| That is, by default importer.sh will clone and install the latest [main](https://github.com/pq-code-package/mldsa-native/tree/main) of mldsa-native. | ||
|
|
||
| After a successful import, [META.yml](META.yml) will reflect the source, branch, commit and timestamp of the import. | ||
|
|
||
| ### Import Scope | ||
|
|
||
| mldsa-native has a C-only version as well as native 'backends' in AVX2 and | ||
| Neon for high performance. At present, [importer.sh](importer.sh) imports only | ||
| the C-only version. | ||
|
|
||
| mldsa-native offers its own FIPS-202 implementation, including fast | ||
| versions of batched FIPS-202. [importer.sh](importer.sh) does _not_ import those. | ||
| Instead, glue-code around AWS-LC's own FIPS-202 implementation is provided in | ||
| [fips202_glue.h](fips202_glue.h) and [fips202x4_glue.h](fips202x4_glue.h). | ||
|
|
||
| ## Configuration and compatibility layer | ||
|
|
||
| mldsa-native is used with a custom configuration file [mldsa_native_config.h](mldsa_native_config.h). This file includes | ||
| a compatibility layer between AWS-LC/OpenSSL and mldsa-native, covering: | ||
|
|
||
| * FIPS/PCT: If `AWSLC_FIPS` is set, `MLD_CONFIG_KEYGEN_PCT` is | ||
| enabled to include a PCT. | ||
| * FIPS/PCT: If `BORINGSSL_FIPS_BREAK_TESTS` is set, | ||
| `MLD_CONFIG_KEYGEN_PCT_BREAKAGE_TEST` is set and `mld_break_pct` | ||
| defined via `boringssl_fips_break_test("MLDSA_PWCT")`, to include | ||
| runtime-breakage of the PCT for testing purposes. | ||
| * CT: If `BORINGSSL_CONSTANT_TIME_VALIDATION` is set, then | ||
| `MLD_CONFIG_CT_TESTING_ENABLED` is set to enable valgrind testing. | ||
| * Zeroization: `MLD_CONFIG_CUSTOM_ZEROIZE` is set and `mld_zeroize` | ||
| mapped to `OPENSSL_cleanse` to use OpenSSL's zeroization function. | ||
| * Randombytes: `MLD_CONFIG_CUSTOM_RANDOMBYTES` is set and `mld_randombytes` | ||
| mapped to `RAND_bytes` to use AWS-LC's randombytes function. | ||
|
|
||
| ## Build process | ||
|
|
||
| At the core, mldsa-native is a 'single-level' implementation of ML-DSA: | ||
| A build of the main source tree provides an implementation of | ||
| exactly one of ML-DSA-44/65/87, depending on the MLD_CONFIG_PARAMETER_SET | ||
| parameter. All source files for a single-build of mldsa-native are bundled in | ||
| [mldsa_native_bcm.c](mldsa/mldsa_native_bcm.c), which is also imported from | ||
| mldsa-native. | ||
|
|
||
| To build all security levels, [mldsa_native_bcm.c](mldsa/mldsa_native_bcm.c) | ||
| is included three times into [ml_dsa.c](ml_dsa.c), once per security level. | ||
| Level-independent code is included only once and shared across the levels; | ||
| this is controlled through the configuration options | ||
| `MLD_CONFIG_MULTILEVEL_WITH_SHARED` and `MLD_CONFIG_MULTILEVEL_NO_SHARED` | ||
| used prior to importing the instances of [mldsa_native_bcm.c](mldsa/mldsa_native_bcm.c) into [ml_dsa.c](ml_dsa.c). | ||
|
|
||
| Note that the multilevel build process is entirely internal to `ml_dsa.c`, | ||
| and does not affect the AWS-LC build otherwise. | ||
|
|
||
| ## Formal Verification | ||
|
|
||
| All C-code imported by [importer.sh](importer.sh) is formally verified using the | ||
| C Bounded Model Checker ([CBMC](https://github.com/diffblue/cbmc/)) to be free of | ||
| various classes of undefined behaviour, including out-of-bounds memory accesses and | ||
| arithmetic overflow; the latter is of particular interest for ML-DSA because of | ||
| the use of lazy modular reduction for improved performance. | ||
|
|
||
| The heart of the CBMC proofs are function contract and loop annotations to | ||
| the C-code. Function contracts are denoted `__contract__(...)` clauses and | ||
| occur at the time of declaration, while loop contracts are denoted | ||
| `__loop__` and follow the `for` statement. | ||
|
|
||
| The function contract and loop statements are kept in the source, but | ||
| removed by the preprocessor so long as the CBMC macro is undefined. Keeping | ||
| them simplifies the import, and care has been taken to make them readable | ||
| to the non-expert, and thereby serve as precise documentation of | ||
| assumptions and guarantees upheld by the code. | ||
|
|
||
| ## Testing | ||
|
|
||
| We KAT ML-DSA with test vectors obtained from https://github.com/post-quantum-cryptography/KAT within `PQDSAParameterTest.KAT`. We select the KATs for the signing mode `hedged`, which derives the signing private random seed (rho) pseudorandomly from the signer's private key, the message to be signed, and a 256-bit string `rnd` which is generated at random. The `pure` variant of these KATs were used, as they provide test vector inputs for "pure" i.e., non-pre-hashed messages. The KAT files have been modified to insert linebreaks between each test vector set. | ||
|
|
||
| We also run the ACVP test vectors obtained from https://github.com/usnistgov/ACVP-Server within the three functions `PerMLDSATest.ACVPKeyGen`, `PerMLDSATest.ACVPSigGen` and `PerMLDSATest.ACVPSigVer`. These correspond to the tests found at [ML-DSA-keyGen-FIPS204](https://github.com/usnistgov/ACVP-Server/tree/master/gen-val/json-files/ML-DSA-keyGen-FIPS204), [ML-DSA-sigGen-FIPS204](https://github.com/usnistgov/ACVP-Server/tree/master/gen-val/json-files/ML-DSA-sigGen-FIPS204), and [ML-DSA-sigVer-FIPS204](https://github.com/usnistgov/ACVP-Server/tree/master/gen-val/json-files/ML-DSA-sigVer-FIPS204). | ||
| To test ML-DSA pure, non-deterministic mode, we use `tgId = 19, 21, 23` of sigGen and `tgId = 7, 9, 11` of sigVer. | ||
| To test ML-DSA ExternalMu, non-deterministic mode, we use `tgId = 20, 22, 24` of sigGen and `tgId = 8, 10, 12` of sigVer. | ||
|
|
||
| The test suite includes: | ||
|
|
||
| * Known Answer Tests (KAT) for all three parameter sets (ML-DSA-44/65/87) | ||
| * Functional tests for key generation, signing, and verification | ||
| * ExtMu (External Mu) variant tests for pre-hash modes | ||
| * ACVP (Automated Cryptographic Validation Protocol) test vectors | ||
| * Pairwise Consistency Test (PCT) validation when FIPS mode is enabled | ||
| * Key consistency tests including public key derivation from secret key | ||
|
|
||
| ## Side-channels | ||
|
|
||
| mldsa-native's CI uses a patched version of valgrind to check for various | ||
| compilers and compile flags that there are no secret-dependent memory | ||
| accesses, branches, or divisions. The relevant assertions are kept | ||
| and used if `MLD_CONFIG_CT_TESTING_ENABLED` is set, which is the case | ||
| if and only if `BORINGSSL_CONSTANT_TIME_VALIDATION` is set. | ||
|
|
||
| mldsa-native uses value barriers to block | ||
| potentially harmful compiler reasoning and optimization. Where standard | ||
| gcc/clang inline assembly is not available, mldsa-native falls back to a | ||
| slower 'opt blocker' based on a volatile global -- both are described in | ||
| [ct.h](https://github.com/pq-code-package/mldsa-native/blob/main/mldsa/ct.h). | ||
|
|
||
| ## Comparison to reference implementation | ||
|
|
||
| mldsa-native is a fork of the ML-DSA [reference | ||
| implementation](https://github.com/pq-crystals/dilithium) (Dilithium). | ||
|
|
||
| The following gives an overview of the major changes: | ||
|
|
||
| - CBMC and debug annotations, and minor code restructurings or signature | ||
| changes to facilitate the CBMC proofs. For example, functions are structured | ||
| to make loop bounds and memory access patterns explicit for formal verification. | ||
| - Introduction of 4x-batched versions of some functions from the reference | ||
| implementation. This is to leverage 4x-batched Keccak-f1600 implementations | ||
| if present. The batching happens at the C level even if no native backend | ||
| for FIPS 202 is present. | ||
| - FIPS 204 compliance: Introduced optional PCT (FIPS 204, Section 4.4, Pairwise | ||
| Consistency) and zeroization of stack buffers as required by (FIPS 204, | ||
| Section 3.6.3, Destruction of intermediate values). | ||
| - Introduction of native backend implementations for AVX2. Those are drop-in | ||
| replacements for the corresponding C functions and dispatched at compile-time. | ||
| - Restructuring of files to separate level-specific from level-generic | ||
| functionality. This is needed to enable a multi-level build of mldsa-native | ||
| where level-generic code is shared between levels. | ||
| - More pervasive use of value barriers to harden constant-time primitives, | ||
| even when Link-Time-Optimization (LTO) is enabled. The use of LTO can lead | ||
| to insecure compilation in case of the reference implementation. | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be cleaner to use
git ls-remoteto check if the current folder is up-to-date, and then replace iff there's a new version (rather than force deleting the existing repo.)