Skip to content

improve ML-KEM algorithm handling and test coverage#1443

Open
jasonkatonica wants to merge 1 commit intoIBM:mainfrom
jasonkatonica:katonica/issue/mlkemhonorparam
Open

improve ML-KEM algorithm handling and test coverage#1443
jasonkatonica wants to merge 1 commit intoIBM:mainfrom
jasonkatonica:katonica/issue/mlkemhonorparam

Conversation

@jasonkatonica
Copy link
Copy Markdown
Member

@jasonkatonica jasonkatonica commented May 6, 2026

  • Fix ML-KEM encapsulation length calculation to use actual key algorithm instead of generic 'ML-KEM' string
  • Add validation for encapsulation message length in decapsulation
  • Improve key conversion to use actual algorithm from key instead of generic algorithm parameter
  • Add comprehensive test for invalid encapsulation length handling
  • Refactor test skip conditions to use assumeFalse for better JUnit 5 compatibility
  • Add new interoperability tests using NamedParameterSpec:
    • testMLKEMInteropWithNamedParameterSpec
    • testMLKEMInteropEmptyParamsWithNamedParameterSpec
    • testMLKEMInteropSmallerSecretWithNamedParameterSpec
    • testMLKEMBidirectionalInteropWithNamedParameterSpec
  • Remove unused import and improve code consistency

This ensures ML-KEM operations correctly handle different parameter sets (ML-KEM-512, ML-KEM-768, ML-KEM-1024) and provides better error messages when encapsulation length mismatches occur.

Signed-off-by: Jason Katonica katonica@us.ibm.com

Copy link
Copy Markdown
Member

@KostasTsiounis KostasTsiounis left a comment

Choose a reason for hiding this comment

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

LGTM

@jasonkatonica jasonkatonica force-pushed the katonica/issue/mlkemhonorparam branch 2 times, most recently from edc51bc to 2053b69 Compare May 6, 2026 22:15
- Fix ML-KEM encapsulation length calculation to use actual key
  algorithm instead of generic 'ML-KEM' string
- Add validation for encapsulation message length in decapsulation
- Improve key conversion to use actual algorithm from key instead
  of generic algorithm parameter
- Add comprehensive test for invalid encapsulation length handling
- Refactor test skip conditions to use assumeFalse for better
  JUnit 5 compatibility
- Add new interoperability tests using NamedParameterSpec:
  * testMLKEMInteropWithNamedParameterSpec
  * testMLKEMInteropEmptyParamsWithNamedParameterSpec
  * testMLKEMInteropSmallerSecretWithNamedParameterSpec
  * testMLKEMBidirectionalInteropWithNamedParameterSpec
- Remove test unused imports and improve code consistency

This ensures ML-KEM operations correctly handle different parameter
sets (ML-KEM-512, ML-KEM-768, ML-KEM-1024) and provides better error
messages when encapsulation length mismatches occur.

Signed-off-by: Jason Katonica <katonica@us.ibm.com>
@jasonkatonica jasonkatonica force-pushed the katonica/issue/mlkemhonorparam branch from 2053b69 to 3942042 Compare May 7, 2026 12:26
}

private int getEncapsulationLength() {
private int getEncapsulationLength(String algorithm) {
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.

Since, this is no longer using the Algorithm for this Implementation instance. Couldn't this cause issues with callers mix matching things easier?

// Use the key's actual algorithm, not the generic "ML-KEM"
try {
KeyFactory kf = KeyFactory.getInstance(this.alg, this.provider.getName());
KeyFactory kf = KeyFactory.getInstance(keyAlgorithm, this.provider.getName());
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.

This can cause a algorithm miss match if they create this instance as ML-KEM-786, but pass in a ML-KEM-512 key that should not be allowed.

public KEM.Encapsulated engineEncapsulate(int from, int to, String algorithm) {
int encapLen = getEncapsulationLength();
// Get the actual algorithm from the public key
String keyAlgorithm = publicKey.getAlgorithm();
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.

Same issue here.


return getEncapsulationLength();
String keyAlgorithm = privateKey.getAlgorithm();
return getEncapsulationLength(keyAlgorithm);
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.

Same type of issue here too. The algorithm the should be based on the one requested when this was created.

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.

3 participants