Skip to content

Commit 8aa9e2c

Browse files
Always store human readable alg name in PQC keys (#819)
The algorithm name at times stores the OID instead of the human readable name when running on releases other then Java 25. This update uses our internal maps to always convert the OID to the human readable name within the PQC key objects. Signed-off-by: Jason Katonica <[email protected]>
1 parent 11ae27e commit 8aa9e2c

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

src/main/java/com/ibm/crypto/plus/provider/PQCPrivateKey.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public PQCPrivateKey(OpenJCEPlusProvider provider, byte[] keyBytes, String algNa
4343
throws InvalidKeyException {
4444

4545
this.algid = new AlgorithmId(PQCAlgorithmId.getOID(algName));
46-
this.name = algName;
46+
this.name = PQCKnownOIDs.findMatch(this.algid.getName()).stdName();
4747
this.provider = provider;
4848
byte[] key = null;
4949
DerValue pkOct = null;
@@ -97,7 +97,7 @@ public PQCPrivateKey(OpenJCEPlusProvider provider, PQCKey pqcKey) throws Invalid
9797
}
9898
}
9999

100-
this.name = pqcKey.getAlgorithm();
100+
this.name = PQCKnownOIDs.findMatch(pqcKey.getAlgorithm()).stdName();
101101
this.algid = new AlgorithmId(PQCAlgorithmId.getOID(name));
102102
} catch (Exception exception) {
103103
throw provider.providerException("Failure in PQCPrivateKey" + exception.getMessage(), exception);

src/main/java/com/ibm/crypto/plus/provider/PQCPublicKey.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public PQCPublicKey(OpenJCEPlusProvider provider, byte[] rawKey, String algName)
3939
throws InvalidKeyException {
4040
this.algid = new AlgorithmId(PQCAlgorithmId.getOID(algName));
4141
this.provider = provider;
42-
this.name = algName;
42+
this.name = PQCKnownOIDs.findMatch(this.algid.getName()).stdName();
4343

4444
setKey(new BitArray(rawKey.length * 8, rawKey));
4545
try {
@@ -61,9 +61,9 @@ public PQCPublicKey(OpenJCEPlusProvider provider, PQCKey pqcKey) {
6161
try {
6262
this.provider = provider;
6363
byte[] rawKey = pqcKey.getPublicKeyBytes();
64-
this.name = pqcKey.getAlgorithm();
64+
this.algid = new AlgorithmId(PQCAlgorithmId.getOID(pqcKey.getAlgorithm()));
6565

66-
this.algid = new AlgorithmId(PQCAlgorithmId.getOID(name));
66+
this.name = PQCKnownOIDs.findMatch(this.algid.getName()).stdName();
6767

6868
//OCKC puts the BITSTRING on the key. Need to remove it.
6969
setKey(new BitArray((rawKey.length - 5)*8, rawKey, 5));
@@ -80,7 +80,7 @@ public PQCPublicKey(OpenJCEPlusProvider provider, byte[] encoded) throws Invalid
8080
try {
8181
decode(encoded);
8282

83-
name = this.algid.toString();
83+
this.name = PQCKnownOIDs.findMatch(this.algid.getName()).stdName();
8484
DerOutputStream tmp = new DerOutputStream();
8585
tmp.putUnalignedBitString(getKey());
8686
byte[] b = tmp.toByteArray();

src/main/java/com/ibm/crypto/plus/provider/PQCSignatureImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException
114114
}
115115
//Validate that the alg of the key matchs the alg specified on creation of this object
116116
if (this.alg != null && !((keyPublic.getAlgorithm()).equalsIgnoreCase(this.alg))) {
117-
throw new InvalidKeyException("Key must be of algorithm " + this.alg);
117+
throw new InvalidKeyException("Expected algorithm " + this.alg + ", but got " + keyPublic.getAlgorithm());
118118
}
119119
try {
120120
this.signature.initialize(keyPublic.getPQCKey());

src/test/java/ibm/jceplus/junit/base/BaseTestPQCSignature.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,45 @@
88

99
package ibm.jceplus.junit.base;
1010

11+
import java.security.KeyFactory;
1112
import java.security.KeyPair;
1213
import java.security.KeyPairGenerator;
14+
import java.security.PrivateKey;
15+
import java.security.PublicKey;
16+
import java.security.spec.PKCS8EncodedKeySpec;
17+
import java.security.spec.X509EncodedKeySpec;
1318
import org.junit.jupiter.params.ParameterizedTest;
1419
import org.junit.jupiter.params.provider.CsvSource;
1520

1621
public class BaseTestPQCSignature extends BaseTestJunit5Signature {
1722

18-
1923
static final byte[] origMsg = "this is the original message to be signed".getBytes();
2024

2125
@ParameterizedTest
2226
@CsvSource({"ML_DSA_44","ML-DSA-65","ML_DSA_87"})
2327
public void testPQCKeySignature(String Algorithm) throws Exception {
2428

25-
if (getProviderName().equals("OpenJCEPlusFIPS")) {
26-
//FIPS does not supported
27-
return;
28-
}
29-
30-
try {
31-
KeyPair keyPair = generateKeyPair(Algorithm);
32-
doSignVerify(Algorithm, origMsg, keyPair.getPrivate(), keyPair.getPublic());
33-
} catch (Exception e) {
34-
throw new Exception(e.getCause() +" - "+Algorithm, e);
35-
}
29+
KeyPair keyPair = generateKeyPair(Algorithm);
30+
doSignVerify(Algorithm, origMsg, keyPair.getPrivate(), keyPair.getPublic());
31+
}
32+
33+
@ParameterizedTest
34+
@CsvSource({"ML_DSA_44","ML-DSA-65","ML_DSA_87"})
35+
public void testPQCKeySignatureEncodings(String Algorithm) throws Exception {
36+
37+
KeyPair keyPair = generateKeyPair(Algorithm);
38+
39+
PrivateKey privateKey = keyPair.getPrivate();
40+
PublicKey publicKey = keyPair.getPublic();
41+
42+
byte[] publicKeyBytes = publicKey.getEncoded();
43+
byte[] privateKeyBytes = privateKey.getEncoded();
44+
45+
KeyFactory keyFactory = KeyFactory.getInstance(Algorithm, getProviderName());
46+
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
47+
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
48+
49+
doSignVerify(Algorithm, origMsg, keyFactory.generatePrivate(privateKeySpec), keyFactory.generatePublic(publicKeySpec));
3650
}
3751

3852
protected KeyPair generateKeyPair(String Algorithm) throws Exception {

0 commit comments

Comments
 (0)