Skip to content

Commit 2f65efa

Browse files
committed
Fix CryptoUtil ML-KEM methods to accept key size in bits
Change encapsulateMLKEM() and decapsulateMLKEM() to accept key size in bits instead of bytes, consistent with PKI convention and other CryptoUtil methods like generateKey(). The methods now convert bits to bytes internally when calling the Java KEM API, making them compatible with params.getSkLength() which returns key size in bits. IDM-6295
1 parent 425d0c1 commit 2f65efa

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

base/common/src/main/java/com/netscape/cmsutil/crypto/CryptoUtil.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ public KEMEncapsulation(SymmetricKey sharedSecret, byte[] ciphertext) {
667667
* Performs ML-KEM encapsulation to generate a shared secret.
668668
*
669669
* @param publicKey ML-KEM public key
670-
* @param keySize Derived key size in bytes (typically 32 for AES-256)
670+
* @param keySize Derived key size in bits (e.g., 128 or 256 for AES)
671671
* @return KEMEncapsulation containing shared secret and ciphertext
672672
* @throws Exception if encapsulation fails
673673
*/
@@ -680,20 +680,21 @@ public static KEMEncapsulation encapsulateMLKEM(PublicKey publicKey, int keySize
680680
}
681681

682682
logger.debug("CryptoUtil: ML-KEM encapsulation");
683-
logger.debug("CryptoUtil: - Key size: " + keySize + " bytes");
683+
logger.debug("CryptoUtil: - Key size: " + keySize + " bits");
684684

685+
int keySizeBytes = keySize / 8;
685686
javax.crypto.KEM kem = javax.crypto.KEM.getInstance("ML-KEM", "Mozilla-JSS");
686687
javax.crypto.KEM.Encapsulator encapsulator = kem.newEncapsulator(publicKey);
687688
javax.crypto.KEM.Encapsulated encapsulated = encapsulator.encapsulate(
688-
0, // offset
689-
keySize, // derived key size in bytes
690-
"AES-ECB" // algorithm for derived key
689+
0, // offset
690+
keySizeBytes, // derived key size in bytes
691+
"AES-ECB" // algorithm for derived key
691692
);
692693

693694
SymmetricKey sharedSecret = (SymmetricKey) encapsulated.key();
694695
byte[] ciphertext = encapsulated.encapsulation();
695696

696-
logger.debug("CryptoUtil: - Shared secret size: " + keySize + " bytes");
697+
logger.debug("CryptoUtil: - Shared secret size: " + keySizeBytes + " bytes");
697698
logger.debug("CryptoUtil: - Ciphertext size: " + ciphertext.length + " bytes");
698699

699700
return new KEMEncapsulation(sharedSecret, ciphertext);
@@ -704,7 +705,7 @@ public static KEMEncapsulation encapsulateMLKEM(PublicKey publicKey, int keySize
704705
*
705706
* @param privateKey ML-KEM private key
706707
* @param ciphertext KEM ciphertext from encapsulation
707-
* @param keySize Derived key size in bytes (must match encapsulation)
708+
* @param keySize Derived key size in bits (must match encapsulation, e.g., 128 or 256)
708709
* @return Recovered shared secret (symmetric key)
709710
* @throws Exception if decapsulation fails
710711
*/
@@ -724,16 +725,17 @@ public static SymmetricKey decapsulateMLKEM(
724725
}
725726

726727
logger.debug("CryptoUtil: ML-KEM decapsulation");
727-
logger.debug("CryptoUtil: - Key size: " + keySize + " bytes");
728+
logger.debug("CryptoUtil: - Key size: " + keySize + " bits");
728729
logger.debug("CryptoUtil: - Ciphertext size: " + ciphertext.length + " bytes");
729730

731+
int keySizeBytes = keySize / 8;
730732
javax.crypto.KEM kem = javax.crypto.KEM.getInstance("ML-KEM", "Mozilla-JSS");
731733
javax.crypto.KEM.Decapsulator decapsulator = kem.newDecapsulator(privateKey);
732734
SymmetricKey sharedSecret = (SymmetricKey) decapsulator.decapsulate(
733735
ciphertext,
734-
0, // offset
735-
keySize, // derived key size in bytes
736-
"AES-ECB" // algorithm for derived key
736+
0, // offset
737+
keySizeBytes, // derived key size in bytes
738+
"AES-ECB" // algorithm for derived key
737739
);
738740

739741
logger.debug("CryptoUtil: - Shared secret recovered");

0 commit comments

Comments
 (0)