Skip to content

Commit a4f40dc

Browse files
committed
added getPublicData() to ML-KEM, ML-DSA, and SLH-DSA public keys.
aded toString() on ML-KEM, ML-DSA, and SLH-DSA public and private keys.
1 parent d7b21fb commit a4f40dc

File tree

9 files changed

+186
-19
lines changed

9 files changed

+186
-19
lines changed

prov/src/main/java/org/bouncycastle/jcajce/interfaces/MLDSAPublicKey.java

+6
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@
55
public interface MLDSAPublicKey
66
extends PublicKey, MLDSAKey
77
{
8+
/**
9+
* Return the raw encoded data representing the public key: rho || t1.
10+
*
11+
* @return the concatenation of rho and t1.
12+
*/
13+
byte[] getPublicData();
814
}

prov/src/main/java/org/bouncycastle/jcajce/interfaces/MLKEMPublicKey.java

+6
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@
55
public interface MLKEMPublicKey
66
extends PublicKey, MLKEMKey
77
{
8+
/**
9+
* Return the raw encoded data representing the public key: t || rho.
10+
*
11+
* @return the concatenation of t and rho.
12+
*/
13+
byte[] getPublicData();
814
}

prov/src/main/java/org/bouncycastle/jcajce/interfaces/SLHDSAPublicKey.java

+6
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@
55
public interface SLHDSAPublicKey
66
extends PublicKey, SLHDSAKey
77
{
8+
/**
9+
* Return the raw encoded data representing the public key: seed || root.
10+
*
11+
* @return the concatenation of the seed and root values.
12+
*/
13+
byte[] getPublicData();
814
}

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/mldsa/BCMLDSAPrivateKey.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
import org.bouncycastle.pqc.crypto.util.PrivateKeyFactory;
1414
import org.bouncycastle.pqc.jcajce.provider.util.KeyUtil;
1515
import org.bouncycastle.util.Arrays;
16+
import org.bouncycastle.util.Fingerprint;
17+
import org.bouncycastle.util.Strings;
18+
import org.bouncycastle.util.encoders.Hex;
1619

1720
public class BCMLDSAPrivateKey
1821
implements MLDSAPrivateKey
@@ -28,7 +31,7 @@ public BCMLDSAPrivateKey(
2831
MLDSAPrivateKeyParameters params)
2932
{
3033
this.params = params;
31-
algorithm = MLDSAParameterSpec.fromName(params.getParameters().getName()).getName().toUpperCase();
34+
this.algorithm = MLDSAParameterSpec.fromName(params.getParameters().getName()).getName().toUpperCase();
3235
}
3336

3437
public BCMLDSAPrivateKey(PrivateKeyInfo keyInfo)
@@ -79,7 +82,7 @@ public int hashCode()
7982
}
8083

8184
/**
82-
* @return name of the algorithm - "ML-DSA"
85+
* @return name of the algorithm
8386
*/
8487
public final String getAlgorithm()
8588
{
@@ -111,6 +114,26 @@ public String getFormat()
111114
return "PKCS#8";
112115
}
113116

117+
public String toString()
118+
{
119+
StringBuilder buf = new StringBuilder();
120+
String nl = Strings.lineSeparator();
121+
byte[] keyBytes = params.getPublicKey();
122+
123+
// -DM Hex.toHexString
124+
buf.append(getAlgorithm())
125+
.append(" ")
126+
.append("Private Key").append(" [")
127+
.append(new Fingerprint(keyBytes).toString())
128+
.append("]")
129+
.append(nl)
130+
.append(" public data: ")
131+
.append(Hex.toHexString(keyBytes))
132+
.append(nl);
133+
134+
return buf.toString();
135+
}
136+
114137
MLDSAPrivateKeyParameters getKeyParams()
115138
{
116139
return params;

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/mldsa/BCMLDSAPublicKey.java

+28
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import org.bouncycastle.pqc.crypto.util.PublicKeyFactory;
1313
import org.bouncycastle.pqc.crypto.util.SubjectPublicKeyInfoFactory;
1414
import org.bouncycastle.util.Arrays;
15+
import org.bouncycastle.util.Fingerprint;
16+
import org.bouncycastle.util.Strings;
17+
import org.bouncycastle.util.encoders.Hex;
1518

1619
public class BCMLDSAPublicKey
1720
implements MLDSAPublicKey
@@ -74,6 +77,11 @@ public final String getAlgorithm()
7477
return MLDSAParameterSpec.fromName(params.getParameters().getName()).getName();
7578
}
7679

80+
public byte[] getPublicData()
81+
{
82+
return params.getEncoded();
83+
}
84+
7785
public byte[] getEncoded()
7886
{
7987
try
@@ -98,6 +106,26 @@ public MLDSAParameterSpec getParameterSpec()
98106
return MLDSAParameterSpec.fromName(params.getParameters().getName());
99107
}
100108

109+
public String toString()
110+
{
111+
StringBuilder buf = new StringBuilder();
112+
String nl = Strings.lineSeparator();
113+
byte[] keyBytes = params.getEncoded();
114+
115+
// -DM Hex.toHexString
116+
buf.append(getAlgorithm())
117+
.append(" ")
118+
.append("Public Key").append(" [")
119+
.append(new Fingerprint(keyBytes).toString())
120+
.append("]")
121+
.append(nl)
122+
.append(" public data: ")
123+
.append(Hex.toHexString(keyBytes))
124+
.append(nl);
125+
126+
return buf.toString();
127+
}
128+
101129
CipherParameters getKeyParams()
102130
{
103131
return params;

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/mlkem/BCMLKEMPrivateKey.java

+30-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.bouncycastle.jcajce.provider.asymmetric.mlkem;
22

3+
import java.io.IOException;
4+
import java.io.ObjectInputStream;
5+
import java.io.ObjectOutputStream;
6+
37
import org.bouncycastle.asn1.ASN1Set;
48
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
59
import org.bouncycastle.jcajce.interfaces.MLKEMPrivateKey;
@@ -9,10 +13,9 @@
913
import org.bouncycastle.pqc.crypto.util.PrivateKeyFactory;
1014
import org.bouncycastle.pqc.crypto.util.PrivateKeyInfoFactory;
1115
import org.bouncycastle.util.Arrays;
12-
13-
import java.io.IOException;
14-
import java.io.ObjectInputStream;
15-
import java.io.ObjectOutputStream;
16+
import org.bouncycastle.util.Fingerprint;
17+
import org.bouncycastle.util.Strings;
18+
import org.bouncycastle.util.encoders.Hex;
1619

1720
public class BCMLKEMPrivateKey
1821
implements MLKEMPrivateKey
@@ -41,7 +44,7 @@ private void init(PrivateKeyInfo keyInfo)
4144
{
4245
this.attributes = keyInfo.getAttributes();;
4346
this.params = (MLKEMPrivateKeyParameters)PrivateKeyFactory.createKey(keyInfo);
44-
this.algorithm = params.getParameters().getName();
47+
this.algorithm = MLKEMParameterSpec.fromName(params.getParameters().getName()).getName().toUpperCase();
4548
}
4649

4750
/**
@@ -78,11 +81,10 @@ public int hashCode()
7881
public final String getAlgorithm()
7982
{
8083
return algorithm;
81-
// return MLKEMParameterSpec.fromName(params.getParameters().getName()).getName().toUpperCase();
8284
}
85+
8386
public byte[] getEncoded()
8487
{
85-
8688
try
8789
{
8890
PrivateKeyInfo pki = PrivateKeyInfoFactory.createPrivateKeyInfo(params, attributes);
@@ -110,7 +112,27 @@ public String getFormat()
110112
return "PKCS#8";
111113
}
112114

113-
public MLKEMPrivateKeyParameters getKeyParams()
115+
public String toString()
116+
{
117+
StringBuilder buf = new StringBuilder();
118+
String nl = Strings.lineSeparator();
119+
byte[] keyBytes = params.getPublicKey();
120+
121+
// -DM Hex.toHexString
122+
buf.append(getAlgorithm())
123+
.append(" ")
124+
.append("Private Key").append(" [")
125+
.append(new Fingerprint(keyBytes).toString())
126+
.append("]")
127+
.append(nl)
128+
.append(" public data: ")
129+
.append(Hex.toHexString(keyBytes))
130+
.append(nl);
131+
132+
return buf.toString();
133+
}
134+
135+
MLKEMPrivateKeyParameters getKeyParams()
114136
{
115137
return params;
116138
}

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/mlkem/BCMLKEMPublicKey.java

+35-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package org.bouncycastle.jcajce.provider.asymmetric.mlkem;
22

3+
import java.io.IOException;
4+
import java.io.ObjectInputStream;
5+
import java.io.ObjectOutputStream;
6+
37
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
48
import org.bouncycastle.jcajce.interfaces.MLKEMPublicKey;
59
import org.bouncycastle.jcajce.spec.MLKEMParameterSpec;
610
import org.bouncycastle.pqc.crypto.mlkem.MLKEMPublicKeyParameters;
711
import org.bouncycastle.pqc.crypto.util.PublicKeyFactory;
812
import org.bouncycastle.pqc.crypto.util.SubjectPublicKeyInfoFactory;
913
import org.bouncycastle.util.Arrays;
10-
11-
import java.io.IOException;
12-
import java.io.ObjectInputStream;
13-
import java.io.ObjectOutputStream;
14+
import org.bouncycastle.util.Fingerprint;
15+
import org.bouncycastle.util.Strings;
16+
import org.bouncycastle.util.encoders.Hex;
1417

1518
public class BCMLKEMPublicKey
1619
implements MLKEMPublicKey
@@ -20,7 +23,6 @@ public class BCMLKEMPublicKey
2023
private transient MLKEMPublicKeyParameters params;
2124

2225
private transient String algorithm;
23-
private transient byte[] encoding;
2426

2527
public BCMLKEMPublicKey(
2628
MLKEMPublicKeyParameters params)
@@ -38,13 +40,13 @@ private void init(SubjectPublicKeyInfo keyInfo)
3840
throws IOException
3941
{
4042
this.params = (MLKEMPublicKeyParameters)PublicKeyFactory.createKey(keyInfo);
41-
this.algorithm = params.getParameters().getName();
43+
this.algorithm = MLKEMParameterSpec.fromName(params.getParameters().getName()).getName().toUpperCase();
4244
}
4345

4446
private void init(MLKEMPublicKeyParameters params)
4547
{
4648
this.params = params;
47-
this.algorithm = params.getParameters().getName();
49+
this.algorithm = MLKEMParameterSpec.fromName(params.getParameters().getName()).getName().toUpperCase();
4850
}
4951
/**
5052
* Compare this ML-KEM public key with another object.
@@ -82,6 +84,11 @@ public final String getAlgorithm()
8284
return algorithm;
8385
}
8486

87+
public byte[] getPublicData()
88+
{
89+
return params.getEncoded();
90+
}
91+
8592
public byte[] getEncoded()
8693
{
8794
try
@@ -106,7 +113,27 @@ public MLKEMParameterSpec getParameterSpec()
106113
return MLKEMParameterSpec.fromName(params.getParameters().getName());
107114
}
108115

109-
public MLKEMPublicKeyParameters getKeyParams()
116+
public String toString()
117+
{
118+
StringBuilder buf = new StringBuilder();
119+
String nl = Strings.lineSeparator();
120+
byte[] keyBytes = params.getEncoded();
121+
122+
// -DM Hex.toHexString
123+
buf.append(getAlgorithm())
124+
.append(" ")
125+
.append("Public Key").append(" [")
126+
.append(new Fingerprint(keyBytes).toString())
127+
.append("]")
128+
.append(nl)
129+
.append(" public data: ")
130+
.append(Hex.toHexString(keyBytes))
131+
.append(nl);
132+
133+
return buf.toString();
134+
}
135+
136+
MLKEMPublicKeyParameters getKeyParams()
110137
{
111138
return params;
112139
}

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/slhdsa/BCSLHDSAPrivateKey.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import org.bouncycastle.pqc.crypto.util.PrivateKeyFactory;
1515
import org.bouncycastle.pqc.crypto.util.PrivateKeyInfoFactory;
1616
import org.bouncycastle.util.Arrays;
17+
import org.bouncycastle.util.Fingerprint;
1718
import org.bouncycastle.util.Strings;
19+
import org.bouncycastle.util.encoders.Hex;
1820

1921
public class BCSLHDSAPrivateKey
2022
implements SLHDSAPrivateKey
@@ -72,7 +74,7 @@ public int hashCode()
7274
}
7375

7476
/**
75-
* @return name of the algorithm - "SLH-DSA"
77+
* @return name of the algorithm - "SLH-DSA..."
7678
*/
7779
public final String getAlgorithm()
7880
{
@@ -109,6 +111,26 @@ public String getFormat()
109111
return "PKCS#8";
110112
}
111113

114+
public String toString()
115+
{
116+
StringBuilder buf = new StringBuilder();
117+
String nl = Strings.lineSeparator();
118+
byte[] keyBytes = params.getPublicKey();
119+
120+
// -DM Hex.toHexString
121+
buf.append(getAlgorithm())
122+
.append(" ")
123+
.append("Private Key").append(" [")
124+
.append(new Fingerprint(keyBytes).toString())
125+
.append("]")
126+
.append(nl)
127+
.append(" public data: ")
128+
.append(Hex.toHexString(keyBytes))
129+
.append(nl);
130+
131+
return buf.toString();
132+
}
133+
112134
SLHDSAPrivateKeyParameters getKeyParams()
113135
{
114136
return params;

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/slhdsa/BCSLHDSAPublicKey.java

+27
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import org.bouncycastle.pqc.crypto.util.PublicKeyFactory;
1313
import org.bouncycastle.pqc.crypto.util.SubjectPublicKeyInfoFactory;
1414
import org.bouncycastle.util.Arrays;
15+
import org.bouncycastle.util.Fingerprint;
1516
import org.bouncycastle.util.Strings;
17+
import org.bouncycastle.util.encoders.Hex;
1618

1719
public class BCSLHDSAPublicKey
1820
implements SLHDSAPublicKey
@@ -75,6 +77,11 @@ public final String getAlgorithm()
7577
return "SLH-DSA" + "-" + Strings.toUpperCase(params.getParameters().getName());
7678
}
7779

80+
public byte[] getPublicData()
81+
{
82+
return params.getEncoded();
83+
}
84+
7885
public byte[] getEncoded()
7986
{
8087
try
@@ -99,6 +106,26 @@ public SLHDSAParameterSpec getParameterSpec()
99106
return SLHDSAParameterSpec.fromName(params.getParameters().getName());
100107
}
101108

109+
public String toString()
110+
{
111+
StringBuilder buf = new StringBuilder();
112+
String nl = Strings.lineSeparator();
113+
byte[] keyBytes = params.getEncoded();
114+
115+
// -DM Hex.toHexString
116+
buf.append(getAlgorithm())
117+
.append(" ")
118+
.append("Public Key").append(" [")
119+
.append(new Fingerprint(keyBytes).toString())
120+
.append("]")
121+
.append(nl)
122+
.append(" public data: ")
123+
.append(Hex.toHexString(keyBytes))
124+
.append(nl);
125+
126+
return buf.toString();
127+
}
128+
102129
CipherParameters getKeyParams()
103130
{
104131
return params;

0 commit comments

Comments
 (0)