You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2026/10/19 16:47:55 UTC
(camel) branch main updated: CAMEL-25800: camel-pqc - the hybrid KEM ephemeral key is generated on the curve of the recipient key (#28090)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new da4fb69a6689 CAMEL-25800: camel-pqc - the hybrid KEM ephemeral key is generated on the curve of the recipient key (#28090)
da4fb69a6689 is described below
commit da4fb69a668981ec1c4e996beec139630bffd488
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Mon Oct 19 18:47:48 2026 +0200
CAMEL-25800: camel-pqc - the hybrid KEM ephemeral key is generated on the curve of the recipient key (#28090)
Co-authored-by: Claude Opus 5 <no...@anthropic.com>
---
.../component/pqc/crypto/hybrid/HybridKEM.java | 54 ++++---
.../pqc/crypto/hybrid/HybridKEMCurveTest.java | 180 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 13 ++
3 files changed, 228 insertions(+), 19 deletions(-)
diff --git a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/hybrid/HybridKEM.java b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/hybrid/HybridKEM.java
index a83afdb3a950..16493c36a476 100644
--- a/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/hybrid/HybridKEM.java
+++ b/components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/hybrid/HybridKEM.java
@@ -17,8 +17,11 @@
package org.apache.camel.component.pqc.crypto.hybrid;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.security.*;
-import java.security.spec.ECGenParameterSpec;
+import java.security.interfaces.ECPublicKey;
+import java.security.interfaces.XECPublicKey;
+import java.security.spec.NamedParameterSpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.KeyAgreement;
@@ -88,7 +91,7 @@ public final class HybridKEM {
}
// Generate ephemeral key pair for classical key agreement
- KeyPair ephemeralKeyPair = generateEphemeralKeyPair(classicalPublicKey.getAlgorithm());
+ KeyPair ephemeralKeyPair = generateEphemeralKeyPair(classicalPublicKey);
// Perform classical key agreement
byte[] classicalSharedSecret;
@@ -263,7 +266,7 @@ public final class HybridKEM {
// Use HKDF to derive the final key
HKDFBytesGenerator hkdf = createHKDF(kdfAlgorithm);
- hkdf.init(new HKDFParameters(ikm, null, info.getBytes()));
+ hkdf.init(new HKDFParameters(ikm, null, info.getBytes(StandardCharsets.UTF_8)));
byte[] output = new byte[outputLength];
hkdf.generateBytes(output, 0, outputLength);
@@ -279,26 +282,40 @@ public final class HybridKEM {
case "HKDF-SHA256" -> new HKDFBytesGenerator(new SHA256Digest());
case "HKDF-SHA384" -> new HKDFBytesGenerator(new SHA384Digest());
case "HKDF-SHA512" -> new HKDFBytesGenerator(new SHA512Digest());
- default -> new HKDFBytesGenerator(new SHA256Digest());
+ // Falling back to SHA-256 here would let a typo derive a different key on each side of the exchange,
+ // with both ends reporting success
+ default -> throw new IllegalArgumentException(
+ "Unsupported hybrid KDF algorithm: " + algorithm
+ + ". Supported values are HKDF-SHA256, HKDF-SHA384 and HKDF-SHA512");
};
}
/**
- * Generates an ephemeral key pair for the given algorithm.
+ * Generates the ephemeral key pair used for the classical half of the key agreement.
+ * <p/>
+ * The ephemeral key must live in the same group as the recipient's key, so the group is taken from that key rather
+ * than from its algorithm name: {@code getAlgorithm()} returns a bare {@code EC} for every curve, so deriving the
+ * curve from it would pin every agreement to one curve regardless of the key actually in use.
*/
- private static KeyPair generateEphemeralKeyPair(String algorithm) throws Exception {
+ private static KeyPair generateEphemeralKeyPair(PublicKey recipientPublicKey) throws Exception {
KeyPairGenerator kpg;
- if ("EC".equals(algorithm)) {
+ if (recipientPublicKey instanceof ECPublicKey ecPublicKey) {
kpg = KeyPairGenerator.getInstance("EC");
- // Default to P-256
- kpg.initialize(new ECGenParameterSpec("secp256r1"), SecureRandomHelper.getSecureRandom());
- } else if ("X25519".equals(algorithm) || "XDH".equals(algorithm)) {
- kpg = KeyPairGenerator.getInstance("X25519");
- } else if ("X448".equals(algorithm)) {
- kpg = KeyPairGenerator.getInstance("X448");
+ kpg.initialize(ecPublicKey.getParams(), SecureRandomHelper.getSecureRandom());
+ } else if (recipientPublicKey instanceof XECPublicKey xecPublicKey
+ && xecPublicKey.getParams() instanceof NamedParameterSpec namedSpec) {
+ kpg = KeyPairGenerator.getInstance(namedSpec.getName());
+ kpg.initialize(namedSpec, SecureRandomHelper.getSecureRandom());
} else {
- // For other algorithms, try to use the same algorithm
+ // BouncyCastle XDH keys report the concrete name (X25519 / X448), which fully determines the group
+ String algorithm = recipientPublicKey.getAlgorithm();
+ if ("XDH".equals(algorithm)) {
+ throw new InvalidKeyException(
+ "Cannot determine the X25519/X448 group of the classical public key of type "
+ + recipientPublicKey.getClass().getName()
+ + ": it exposes neither XECPublicKey parameters nor a concrete algorithm name");
+ }
kpg = KeyPairGenerator.getInstance(algorithm);
}
@@ -306,17 +323,16 @@ public final class HybridKEM {
}
/**
- * Reconstructs a public key from encoded bytes.
+ * Reconstructs a public key from encoded bytes. The X.509 encoding carries its own domain parameters, so a key
+ * factory for the family is enough - naming a concrete curve here would reject the other curves of that family.
*/
private static PublicKey reconstructPublicKey(byte[] encoded, String algorithm) throws Exception {
KeyFactory kf;
if ("EC".equals(algorithm) || "ECDH".equals(algorithm)) {
kf = KeyFactory.getInstance("EC");
- } else if ("X25519".equals(algorithm) || "XDH".equals(algorithm)) {
- kf = KeyFactory.getInstance("X25519");
- } else if ("X448".equals(algorithm)) {
- kf = KeyFactory.getInstance("X448");
+ } else if ("XDH".equals(algorithm)) {
+ kf = KeyFactory.getInstance("XDH");
} else {
kf = KeyFactory.getInstance(algorithm);
}
diff --git a/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/crypto/hybrid/HybridKEMCurveTest.java b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/crypto/hybrid/HybridKEMCurveTest.java
new file mode 100644
index 000000000000..097f05b1e5cd
--- /dev/null
+++ b/components/camel-pqc/src/test/java/org/apache/camel/component/pqc/crypto/hybrid/HybridKEMCurveTest.java
@@ -0,0 +1,180 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.pqc.crypto.hybrid;
+
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.security.spec.ECGenParameterSpec;
+
+import javax.crypto.KeyAgreement;
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKey;
+
+import org.bouncycastle.jcajce.spec.MLKEMParameterSpec;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.pqc.jcajce.provider.BouncyCastlePQCProvider;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The ephemeral key of the classical half must be generated on the curve of the recipient's key. An EC public key
+ * reports a bare "EC" as its algorithm, so the curve has to come from the key itself.
+ */
+class HybridKEMCurveTest {
+
+ @BeforeAll
+ static void setup() {
+ if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
+ Security.addProvider(new BouncyCastleProvider());
+ }
+ if (Security.getProvider(BouncyCastlePQCProvider.PROVIDER_NAME) == null) {
+ Security.addProvider(new BouncyCastlePQCProvider());
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = { "secp256r1", "secp384r1", "secp521r1" })
+ void hybridKemRoundTripsOnEveryNistCurve(String curve) throws Exception {
+ KeyPairGenerator ecKpg = KeyPairGenerator.getInstance("EC", "BC");
+ ecKpg.initialize(new ECGenParameterSpec(curve), new SecureRandom());
+ KeyPair ecKeyPair = ecKpg.generateKeyPair();
+
+ KeyPairGenerator mlkemKpg = KeyPairGenerator.getInstance("ML-KEM", "BC");
+ mlkemKpg.initialize(MLKEMParameterSpec.ml_kem_768, new SecureRandom());
+ KeyPair mlkemKeyPair = mlkemKpg.generateKeyPair();
+
+ HybridKEM.HybridEncapsulationResult encapResult = HybridKEM.encapsulate(
+ ecKeyPair.getPublic(),
+ mlkemKeyPair.getPublic(),
+ KeyAgreement.getInstance("ECDH", "BC"),
+ KeyGenerator.getInstance("ML-KEM", "BC"),
+ "AES",
+ 256,
+ "HKDF-SHA256");
+
+ assertTrue(encapResult.encapsulation().length > 0, "an encapsulation must be produced for " + curve);
+
+ SecretKey extracted = HybridKEM.extract(
+ encapResult.encapsulation(),
+ ecKeyPair.getPrivate(),
+ mlkemKeyPair.getPrivate(),
+ KeyAgreement.getInstance("ECDH", "BC"),
+ KeyGenerator.getInstance("ML-KEM", "BC"),
+ "AES",
+ 256,
+ "HKDF-SHA256");
+
+ assertArrayEquals(encapResult.sharedSecret().getEncoded(), extracted.getEncoded(),
+ "sender and recipient must derive the same secret on " + curve);
+ assertEquals(32, extracted.getEncoded().length, "a 256 bit key is 32 bytes");
+ }
+
+ @Test
+ void x25519RoundTrips() throws Exception {
+ KeyPairGenerator x25519Kpg = KeyPairGenerator.getInstance("X25519", "BC");
+ KeyPair x25519KeyPair = x25519Kpg.generateKeyPair();
+
+ KeyPairGenerator mlkemKpg = KeyPairGenerator.getInstance("ML-KEM", "BC");
+ mlkemKpg.initialize(MLKEMParameterSpec.ml_kem_768, new SecureRandom());
+ KeyPair mlkemKeyPair = mlkemKpg.generateKeyPair();
+
+ HybridKEM.HybridEncapsulationResult encapResult = HybridKEM.encapsulate(
+ x25519KeyPair.getPublic(),
+ mlkemKeyPair.getPublic(),
+ KeyAgreement.getInstance("X25519", "BC"),
+ KeyGenerator.getInstance("ML-KEM", "BC"),
+ "AES",
+ 256,
+ "HKDF-SHA256");
+
+ SecretKey extracted = HybridKEM.extract(
+ encapResult.encapsulation(),
+ x25519KeyPair.getPrivate(),
+ mlkemKeyPair.getPrivate(),
+ KeyAgreement.getInstance("X25519", "BC"),
+ KeyGenerator.getInstance("ML-KEM", "BC"),
+ "AES",
+ 256,
+ "HKDF-SHA256");
+
+ assertArrayEquals(encapResult.sharedSecret().getEncoded(), extracted.getEncoded());
+ }
+
+ @Test
+ void x448RoundTrips() throws Exception {
+ KeyPairGenerator x448Kpg = KeyPairGenerator.getInstance("X448", "BC");
+ KeyPair x448KeyPair = x448Kpg.generateKeyPair();
+
+ KeyPairGenerator mlkemKpg = KeyPairGenerator.getInstance("ML-KEM", "BC");
+ mlkemKpg.initialize(MLKEMParameterSpec.ml_kem_768, new SecureRandom());
+ KeyPair mlkemKeyPair = mlkemKpg.generateKeyPair();
+
+ HybridKEM.HybridEncapsulationResult encapResult = HybridKEM.encapsulate(
+ x448KeyPair.getPublic(),
+ mlkemKeyPair.getPublic(),
+ KeyAgreement.getInstance("X448", "BC"),
+ KeyGenerator.getInstance("ML-KEM", "BC"),
+ "AES",
+ 256,
+ "HKDF-SHA256");
+
+ SecretKey extracted = HybridKEM.extract(
+ encapResult.encapsulation(),
+ x448KeyPair.getPrivate(),
+ mlkemKeyPair.getPrivate(),
+ KeyAgreement.getInstance("X448", "BC"),
+ KeyGenerator.getInstance("ML-KEM", "BC"),
+ "AES",
+ 256,
+ "HKDF-SHA256");
+
+ assertArrayEquals(encapResult.sharedSecret().getEncoded(), extracted.getEncoded());
+ }
+
+ @Test
+ void anUnknownKdfAlgorithmIsRejected() throws Exception {
+ KeyPairGenerator ecKpg = KeyPairGenerator.getInstance("EC", "BC");
+ ecKpg.initialize(new ECGenParameterSpec("secp256r1"), new SecureRandom());
+ KeyPair ecKeyPair = ecKpg.generateKeyPair();
+
+ KeyPairGenerator mlkemKpg = KeyPairGenerator.getInstance("ML-KEM", "BC");
+ mlkemKpg.initialize(MLKEMParameterSpec.ml_kem_768, new SecureRandom());
+ KeyPair mlkemKeyPair = mlkemKpg.generateKeyPair();
+
+ // a typo must fail loudly: silently falling back to SHA-256 lets the two ends derive different keys
+ IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
+ () -> HybridKEM.encapsulate(
+ ecKeyPair.getPublic(),
+ mlkemKeyPair.getPublic(),
+ KeyAgreement.getInstance("ECDH", "BC"),
+ KeyGenerator.getInstance("ML-KEM", "BC"),
+ "AES",
+ 256,
+ "HKDF-SHA-512"));
+
+ assertTrue(e.getMessage().contains("HKDF-SHA-512"), "the rejected value must be named: " + e.getMessage());
+ }
+}
diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 8cd5c733d623..bf551ed44027 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -217,6 +217,19 @@ also applies to `pollEnrich` and `ConsumerTemplate.receive`, which take one file
until the last file of the directory is received, where before the first received file deleted it. This applies to
camel-ftp, camel-sftp, camel-smb and camel-azure-files as well.
+=== camel-pqc - an unknown hybridKdfAlgorithm is rejected
+
+The hybrid KEM operations now fail with an `IllegalArgumentException` when `hybridKdfAlgorithm` is not one of
+`HKDF-SHA256`, `HKDF-SHA384` or `HKDF-SHA512`. Before, any other value silently fell back to `HKDF-SHA256`, so a typo
+such as `HKDF-SHA-512` went unnoticed - and if only one end of an exchange carried the typo, both ends still reported
+success while deriving the key with different digests.
+
+The HKDF info bytes of the hybrid KEM are also now encoded explicitly as UTF-8, where they previously used the
+platform default charset. The info string is the symmetric algorithm name, so this has no effect for the usual
+values such as `AES`, which are pure ASCII. A deployment running on a JVM whose default charset cannot encode that
+name identically to UTF-8 would, however, derive a different key than the previous version, and could not decrypt
+material produced by it.
+
=== camel-jbang - camel validate normalize writes one file per input file to an output directory
`camel validate normalize <files> --output=<dir>` now writes one normalized file per input file into the