-
Notifications
You must be signed in to change notification settings - Fork 3
feat(java-sdk): add ML-KEM-768 post-quantum key encapsulation (DSPX-2399) #370
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
Draft
dmihalcik-virtru
wants to merge
1
commit into
main
Choose a base branch
from
DSPX-2399-java-sdk
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.
Draft
Changes from all commits
Commits
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
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
60 changes: 60 additions & 0 deletions
60
sdk/src/main/java/io/opentdf/platform/sdk/MLKEMEncryption.java
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,60 @@ | ||||||||||||
| package io.opentdf.platform.sdk; | ||||||||||||
|
|
||||||||||||
| import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; | ||||||||||||
| import org.bouncycastle.crypto.SecretWithEncapsulation; | ||||||||||||
| import org.bouncycastle.crypto.util.PublicKeyFactory; | ||||||||||||
| import org.bouncycastle.openssl.PEMParser; | ||||||||||||
| import org.bouncycastle.pqc.crypto.mlkem.MLKEMGenerator; | ||||||||||||
| import org.bouncycastle.pqc.crypto.mlkem.MLKEMPublicKeyParameters; | ||||||||||||
|
|
||||||||||||
| import java.io.IOException; | ||||||||||||
| import java.io.StringReader; | ||||||||||||
| import java.security.SecureRandom; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Handles ML-KEM-768 key encapsulation for wrapping a symmetric DEK. | ||||||||||||
| * | ||||||||||||
| * Wire format: base64(ml_kem_ciphertext [1088 bytes] || aes_gcm_wrapped_dek) | ||||||||||||
| * No ephemeralPublicKey field; KeyAccess type is "wrapped". | ||||||||||||
| */ | ||||||||||||
| class MLKEMEncryption { | ||||||||||||
|
|
||||||||||||
| /** ML-KEM-768 ciphertext is always 1088 bytes. */ | ||||||||||||
| static final int CIPHERTEXT_SIZE = 1088; | ||||||||||||
|
|
||||||||||||
| private final MLKEMPublicKeyParameters publicKeyParams; | ||||||||||||
|
|
||||||||||||
| MLKEMEncryption(String pemPublicKey) { | ||||||||||||
| try { | ||||||||||||
| PEMParser parser = new PEMParser(new StringReader(pemPublicKey)); | ||||||||||||
| SubjectPublicKeyInfo spki = (SubjectPublicKeyInfo) parser.readObject(); | ||||||||||||
| parser.close(); | ||||||||||||
| publicKeyParams = (MLKEMPublicKeyParameters) PublicKeyFactory.createKey(spki); | ||||||||||||
| } catch (IOException e) { | ||||||||||||
| throw new SDKException("error parsing ML-KEM-768 public key", e); | ||||||||||||
| } catch (ClassCastException e) { | ||||||||||||
| throw new SDKException("public key is not an ML-KEM key", e); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Encapsulates against the KAS ML-KEM-768 public key and AES-GCM wraps the DEK. | ||||||||||||
| * | ||||||||||||
| * @return ciphertext (1088 bytes) concatenated with the AES-GCM wrapped DEK | ||||||||||||
| */ | ||||||||||||
| byte[] encapsulateAndWrap(byte[] dek) { | ||||||||||||
| MLKEMGenerator kemGen = new MLKEMGenerator(new SecureRandom()); | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||
| SecretWithEncapsulation swe = kemGen.generateEncapsulated(publicKeyParams); | ||||||||||||
|
|
||||||||||||
| byte[] ciphertext = swe.getEncapsulation(); | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leverage the defined
Suggested change
|
||||||||||||
| byte[] sharedSecret = swe.getSecret(); | ||||||||||||
|
|
||||||||||||
| byte[] sessionKey = ECKeyPair.calculateHKDF(TDF.GLOBAL_KEY_SALT, sharedSecret); | ||||||||||||
| byte[] aesWrappedDek = new AesGcm(sessionKey).encrypt(dek).asBytes(); | ||||||||||||
|
|
||||||||||||
| byte[] combined = new byte[ciphertext.length + aesWrappedDek.length]; | ||||||||||||
| System.arraycopy(ciphertext, 0, combined, 0, ciphertext.length); | ||||||||||||
| System.arraycopy(aesWrappedDek, 0, combined, ciphertext.length, aesWrappedDek.length); | ||||||||||||
| return combined; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
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,39 @@ | ||
| #!/usr/bin/env bash | ||
| # Cross-SDK test CLI helper for the OpenTDF Java SDK. | ||
| # Called by the xtest harness to check feature support and run encrypt/decrypt ops. | ||
| set -euo pipefail | ||
|
|
||
| REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)" | ||
| JAR="${REPO_ROOT}/cmdline/target/cmdline.jar" | ||
|
|
||
| _jar_help() { | ||
| java -jar "${JAR}" "$@" --help 2>&1 || true | ||
| } | ||
|
|
||
| case "${1:-}" in | ||
| supports) | ||
| feature="${2:-}" | ||
| case "$feature" in | ||
| mechanism-mlkem) | ||
| # mlkem:768 is a valid --encap-key-type value; picocli lists it in the | ||
| # encrypt help as a COMPLETION-CANDIDATE from KeyType.MLKEM768Key.toString() | ||
| _jar_help encrypt | grep -q "mlkem:768" | ||
| ;; | ||
| *) | ||
| exit 1 | ||
| ;; | ||
| esac | ||
| ;; | ||
| encrypt) | ||
| shift | ||
| java -jar "${JAR}" encrypt "$@" | ||
| ;; | ||
| decrypt) | ||
| shift | ||
| java -jar "${JAR}" decrypt "$@" | ||
| ;; | ||
| *) | ||
| echo "usage: $0 {supports <feature>|encrypt ...|decrypt ...}" >&2 | ||
| exit 1 | ||
| ;; | ||
| esac |
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.
Improve resource management by using try-with-resources for
PEMParserto ensure it is closed even if an exception occurs during key parsing. Additionally, performance can be optimized by reusing a staticSecureRandominstance instead of creating a new one for every encapsulation operation.