gnodet-bot commented on code in PR #30777:
URL: https://github.com/apache/camel/pull/30777#discussion_r4696242780


##########
components/camel-pqc/src/main/java/org/apache/camel/component/pqc/PQCAlgorithmId.java:
##########
@@ -78,11 +81,10 @@ public enum PQCAlgorithmId {
     @Deprecated(since = "4.24")
     CMCE(0x0404, "CMCE"),
     SABER(0x0405, "SABER"),
-    FRODO(0x0406, "FRODO"),
+    FRODO(0x0406, "FrodoKEM"),

Review Comment:
   ⚠️ **Undocumented behavioral breaking change — `FRODO` JCA name**
   
   The JCA name changes from `"FRODO"` to `"FrodoKEM"`. This means 
`PQCAlgorithmId.fromJcaName("FRODO")` now returns `UNKNOWN` instead of `FRODO`. 
Any code (including deserialization, config parsing, or inter-op code) that 
passed the string `"FRODO"` to this lookup will silently get `UNKNOWN` and lose 
its algorithm ID.
   
   The fix itself is correct (aligning with 
`PQCKeyEncapsulationAlgorithms.FRODO` in BouncyCastle), but it needs to be 
documented in the upgrade guide alongside the removed constants. As written, 
the guide lists `DILITHIUM`, `SPHINCSPLUS`, `PICNIC`, and `KYBER` as removed 
but omits the `FRODO` name change — users who relied on the JCA string 
`"FRODO"` will be surprised.
   
   ```suggestion
       FRODO(0x0406, "FrodoKEM"),
   ```
   
   (No code change needed — the fix is correct. Add a bullet to the upgrade 
guide instead, see the other inline comment.)



##########
components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/hybrid/HybridSignature.java:
##########
@@ -172,48 +248,159 @@ public static HybridSignatureComponents parse(byte[] 
hybridSignature) {
             throw new IllegalArgumentException("Invalid hybrid signature: too 
short");
         }
 
+        // Auto-detect v2 by checking the magic bytes
+        if (hybridSignature[0] == MAGIC_BYTE_1 && hybridSignature[1] == 
MAGIC_BYTE_2) {
+            return parseV2(hybridSignature);
+        }
+
+        return parseV1(hybridSignature);
+    }
+
+    /**
+     * Parses a v1 (legacy) hybrid signature.
+     * <p>
+     * Layout: {@code [4 bytes: classical sig length][classical sig][pqc sig]}
+     */
+    private static HybridSignatureComponents parseV1(byte[] hybridSignature) {
         ByteBuffer buffer = ByteBuffer.wrap(hybridSignature);
 
-        // Read classical signature length
         int classicalLength = buffer.getInt();
         if (classicalLength <= 0 || classicalLength > hybridSignature.length - 
4) {
             throw new IllegalArgumentException("Invalid hybrid signature: 
invalid classical signature length");
         }
 
+        byte[] classicalSignature = new byte[classicalLength];
+        buffer.get(classicalSignature);
+
+        int pqcLength = buffer.remaining();
+        if (pqcLength <= 0) {
+            throw new IllegalArgumentException("Invalid hybrid signature: 
missing PQC signature");
+        }
+        byte[] pqcSignature = new byte[pqcLength];
+        buffer.get(pqcSignature);
+
+        return new HybridSignatureComponents(classicalSignature, pqcSignature, 
null, null, 1);
+    }
+
+    /**
+     * Parses a v2 hybrid signature.
+     * <p>
+     * Layout:
+     *
+     * <pre>
+     * [2 bytes: magic][1 byte: version][1 byte: format type]
+     * [2 bytes: classical algorithm ID][2 bytes: PQC algorithm ID]
+     * [4 bytes: classical sig length][N bytes: classical sig][M bytes: PQC 
sig]
+     * </pre>
+     */
+    private static HybridSignatureComponents parseV2(byte[] hybridSignature) {
+        if (hybridSignature.length < V2_HEADER_SIZE + 2) {
+            throw new IllegalArgumentException("Invalid v2 hybrid signature: 
too short");
+        }
+
+        ByteBuffer buffer = ByteBuffer.wrap(hybridSignature);
+
+        // Skip magic (2 bytes)
+        buffer.get(); // 0x50
+        buffer.get(); // 0x51
+
+        byte version = buffer.get();
+        if (version != VERSION_V2) {
+            throw new IllegalArgumentException("Invalid v2 hybrid signature: 
unsupported version " + version);
+        }
+
+        byte formatType = buffer.get();
+        if (formatType != FORMAT_HYBRID_SIGNATURE) {
+            throw new IllegalArgumentException("Invalid v2 hybrid signature: 
unexpected format type " + formatType);
+        }
+
+        // Read algorithm IDs (16-bit each)
+        int classicalAlgRaw = buffer.getShort() & 0xFFFF;
+        int pqcAlgRaw = buffer.getShort() & 0xFFFF;
+        PQCAlgorithmId classicalAlgId = PQCAlgorithmId.fromId(classicalAlgRaw);
+        PQCAlgorithmId pqcAlgId = PQCAlgorithmId.fromId(pqcAlgRaw);
+
         // Read classical signature
+        int classicalLength = buffer.getInt();
+        int remaining = buffer.remaining();
+        if (classicalLength <= 0 || classicalLength > remaining) {
+            throw new IllegalArgumentException("Invalid v2 hybrid signature: 
invalid classical signature length");
+        }
         byte[] classicalSignature = new byte[classicalLength];
         buffer.get(classicalSignature);
 
         // Read PQC signature (remaining bytes)
         int pqcLength = buffer.remaining();
         if (pqcLength <= 0) {
-            throw new IllegalArgumentException("Invalid hybrid signature: 
missing PQC signature");
+            throw new IllegalArgumentException("Invalid v2 hybrid signature: 
missing PQC signature");
         }
         byte[] pqcSignature = new byte[pqcLength];
         buffer.get(pqcSignature);
 
-        return new HybridSignatureComponents(classicalSignature, pqcSignature);
+        return new HybridSignatureComponents(classicalSignature, pqcSignature, 
classicalAlgId, pqcAlgId, 2);
     }
 
     /**
-     * Combines classical and PQC signatures into a hybrid signature.
+     * Combines classical and PQC signatures into a v2 wire format hybrid 
signature embedding the given algorithm
+     * identifiers.
      *
      * @param  classicalSignature the classical signature bytes
      * @param  pqcSignature       the PQC signature bytes
-     * @return                    the combined hybrid signature
+     * @param  classicalAlgId     the classical algorithm identifier (use 
{@link PQCAlgorithmId#UNKNOWN} if not known)

Review Comment:
   🔖 **Missing `@since 4.24` Javadoc tag**
   
   Same as the `HybridKEM.combineEncapsulations` comment: the new 4-argument 
`combineSignatures(byte[], byte[], PQCAlgorithmId, PQCAlgorithmId)` overload is 
missing `@since 4.24`.



##########
docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc:
##########
@@ -167,6 +167,26 @@ Bouncy Castle deprecated Classic McEliece, as it is now 
believed to be unsafe to
 release. The `CMCE` key encapsulation algorithm of the `pqc` component and the 
`pqc` data format is deprecated too, and
 an endpoint or a data format that uses it logs a WARN at startup. Use `MLKEM` 
instead.
 
+==== camel-pqc - Hybrid wire format v2 is now the default
+
+The hybrid signature and hybrid KEM utilities now produce **wire format v2** 
by default.
+`sign()`, `encapsulate()`, and the 2-argument `combineSignatures()` / 
`combineEncapsulations()` overloads all emit v2
+output. A 4.24 writer paired with a pre-4.24 reader will fail with "invalid 
classical length" because the reader does
+not recognize the v2 magic header. Upgrade the readers before the writers: 
bring all consumers to 4.24 first, then
+upgrade the producers.
+
+The following `PQCAlgorithmId` constants, originally shipped in 4.19.0, have 
been removed because the algorithms they
+referred to were renamed or dropped upstream:
+
+* `DILITHIUM` (was `0x0206`)
+* `SPHINCSPLUS` (was `0x0208`)
+* `PICNIC` (was `0x0209`)
+* `KYBER` (was `0x040A`)

Review Comment:
   Add a bullet for the `FRODO` JCA name change here — it is a behavioral 
breaking change (`fromJcaName("FRODO")` now returns `UNKNOWN`) that belongs in 
the upgrade guide alongside the removed constants.
   
   ```suggestion
   * `DILITHIUM` (was `0x0206`)
   * `SPHINCSPLUS` (was `0x0208`)
   * `PICNIC` (was `0x0209`)
   * `KYBER` (was `0x040A`)
   * `FRODO` JCA lookup string changed from `"FRODO"` to `"FrodoKEM"` — 
`PQCAlgorithmId.fromJcaName("FRODO")` now returns `UNKNOWN`
   ```



##########
components/camel-pqc/src/main/java/org/apache/camel/component/pqc/crypto/hybrid/HybridKEM.java:
##########
@@ -219,42 +261,169 @@ public static HybridEncapsulationComponents parse(byte[] 
hybridEncapsulation) {
             throw new IllegalArgumentException("Invalid hybrid encapsulation: 
too short");
         }
 
+        // Auto-detect v2 by checking the magic bytes
+        if (hybridEncapsulation[0] == MAGIC_BYTE_1 && hybridEncapsulation[1] 
== MAGIC_BYTE_2) {
+            return parseV2(hybridEncapsulation);
+        }
+
+        return parseV1(hybridEncapsulation);
+    }
+
+    /**
+     * Parses a v1 (legacy) hybrid encapsulation.
+     * <p>
+     * Layout: {@code [4 bytes: classical encap length][classical encap][pqc 
encap]}
+     */
+    private static HybridEncapsulationComponents parseV1(byte[] 
hybridEncapsulation) {
         ByteBuffer buffer = ByteBuffer.wrap(hybridEncapsulation);
 
-        // Read classical encapsulation length
         int classicalLength = buffer.getInt();
         if (classicalLength <= 0 || classicalLength > 
hybridEncapsulation.length - 4) {
             throw new IllegalArgumentException("Invalid hybrid encapsulation: 
invalid classical length");
         }
 
+        byte[] classicalEncapsulation = new byte[classicalLength];
+        buffer.get(classicalEncapsulation);
+
+        int pqcLength = buffer.remaining();
+        if (pqcLength <= 0) {
+            throw new IllegalArgumentException("Invalid hybrid encapsulation: 
missing PQC encapsulation");
+        }
+        byte[] pqcEncapsulation = new byte[pqcLength];
+        buffer.get(pqcEncapsulation);
+
+        return new HybridEncapsulationComponents(classicalEncapsulation, 
pqcEncapsulation, null, null, 1);
+    }
+
+    /**
+     * Parses a v2 hybrid encapsulation.
+     * <p>
+     * Layout:
+     *
+     * <pre>
+     * [2 bytes: magic][1 byte: version][1 byte: format type]
+     * [2 bytes: classical KEM algorithm ID][2 bytes: PQC KEM algorithm ID]
+     * [4 bytes: classical encap length][N bytes: classical encap][M bytes: 
PQC encap]
+     * </pre>
+     */
+    private static HybridEncapsulationComponents parseV2(byte[] 
hybridEncapsulation) {
+        if (hybridEncapsulation.length < V2_HEADER_SIZE + 2) {
+            throw new IllegalArgumentException("Invalid v2 hybrid 
encapsulation: too short");
+        }
+
+        ByteBuffer buffer = ByteBuffer.wrap(hybridEncapsulation);
+
+        // Skip magic (2 bytes)
+        buffer.get(); // 0x50
+        buffer.get(); // 0x51
+
+        byte version = buffer.get();
+        if (version != VERSION_V2) {
+            throw new IllegalArgumentException("Invalid v2 hybrid 
encapsulation: unsupported version " + version);
+        }
+
+        byte formatType = buffer.get();
+        if (formatType != FORMAT_HYBRID_KEM) {
+            throw new IllegalArgumentException("Invalid v2 hybrid 
encapsulation: unexpected format type " + formatType);
+        }
+
+        // Read algorithm IDs (16-bit each)
+        int classicalAlgRaw = buffer.getShort() & 0xFFFF;
+        int pqcAlgRaw = buffer.getShort() & 0xFFFF;
+        PQCAlgorithmId classicalAlgId = PQCAlgorithmId.fromId(classicalAlgRaw);
+        PQCAlgorithmId pqcAlgId = PQCAlgorithmId.fromId(pqcAlgRaw);
+
         // Read classical encapsulation
+        int classicalLength = buffer.getInt();
+        int remaining = buffer.remaining();
+        if (classicalLength <= 0 || classicalLength > remaining) {
+            throw new IllegalArgumentException("Invalid v2 hybrid 
encapsulation: invalid classical length");
+        }
         byte[] classicalEncapsulation = new byte[classicalLength];
         buffer.get(classicalEncapsulation);
 
         // Read PQC encapsulation (remaining bytes)
         int pqcLength = buffer.remaining();
         if (pqcLength <= 0) {
-            throw new IllegalArgumentException("Invalid hybrid encapsulation: 
missing PQC encapsulation");
+            throw new IllegalArgumentException("Invalid v2 hybrid 
encapsulation: missing PQC encapsulation");
         }
         byte[] pqcEncapsulation = new byte[pqcLength];
         buffer.get(pqcEncapsulation);
 
-        return new HybridEncapsulationComponents(classicalEncapsulation, 
pqcEncapsulation);
+        return new HybridEncapsulationComponents(classicalEncapsulation, 
pqcEncapsulation, classicalAlgId, pqcAlgId, 2);
     }
 
     /**
-     * Combines classical and PQC encapsulations into a hybrid encapsulation.
+     * Combines classical and PQC encapsulations into a v2 wire format hybrid 
encapsulation embedding the given
+     * algorithm identifiers.
+     *
+     * @param  classicalEncapsulation the classical encapsulation bytes
+     * @param  pqcEncapsulation       the PQC encapsulation bytes
+     * @param  classicalAlgId         the classical KEM algorithm identifier
+     * @param  pqcAlgId               the PQC KEM algorithm identifier
+     * @return                        the combined v2 hybrid encapsulation
      */
-    public static byte[] combineEncapsulations(byte[] classicalEncapsulation, 
byte[] pqcEncapsulation) {
-        ByteBuffer buffer = ByteBuffer.allocate(4 + 
classicalEncapsulation.length + pqcEncapsulation.length);
+    public static byte[] combineEncapsulations(

Review Comment:
   🔖 **Missing `@since 4.24` Javadoc tag**
   
   The new 4-argument `combineEncapsulations(byte[], byte[], PQCAlgorithmId, 
PQCAlgorithmId)` overload is new public API added in 4.24, but its Javadoc 
block lacks the `@since 4.24` tag required by Camel conventions 
(`core/camel-api` public API, new methods in existing types). Same applies to 
the 4-argument `combineSignatures` overload in `HybridSignature.java` and to 
the new record components (`classicalAlgorithmId()`, `pqcAlgorithmId()`, 
`version()`) in both `HybridEncapsulationComponents` and 
`HybridSignatureComponents`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to