Skip to content

Commit

Permalink
added getPublicData() to ML-KEM, ML-DSA, and SLH-DSA public keys.
Browse files Browse the repository at this point in the history
aded toString() on ML-KEM, ML-DSA, and SLH-DSA public and private keys.
  • Loading branch information
dghgit committed Oct 4, 2024
1 parent d7b21fb commit a4f40dc
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
public interface MLDSAPublicKey
extends PublicKey, MLDSAKey
{
/**
* Return the raw encoded data representing the public key: rho || t1.
*
* @return the concatenation of rho and t1.
*/
byte[] getPublicData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
public interface MLKEMPublicKey
extends PublicKey, MLKEMKey
{
/**
* Return the raw encoded data representing the public key: t || rho.
*
* @return the concatenation of t and rho.
*/
byte[] getPublicData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
public interface SLHDSAPublicKey
extends PublicKey, SLHDSAKey
{
/**
* Return the raw encoded data representing the public key: seed || root.
*
* @return the concatenation of the seed and root values.
*/
byte[] getPublicData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import org.bouncycastle.pqc.crypto.util.PrivateKeyFactory;
import org.bouncycastle.pqc.jcajce.provider.util.KeyUtil;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Fingerprint;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Hex;

public class BCMLDSAPrivateKey
implements MLDSAPrivateKey
Expand All @@ -28,7 +31,7 @@ public BCMLDSAPrivateKey(
MLDSAPrivateKeyParameters params)
{
this.params = params;
algorithm = MLDSAParameterSpec.fromName(params.getParameters().getName()).getName().toUpperCase();
this.algorithm = MLDSAParameterSpec.fromName(params.getParameters().getName()).getName().toUpperCase();
}

public BCMLDSAPrivateKey(PrivateKeyInfo keyInfo)
Expand Down Expand Up @@ -79,7 +82,7 @@ public int hashCode()
}

/**
* @return name of the algorithm - "ML-DSA"
* @return name of the algorithm
*/
public final String getAlgorithm()
{
Expand Down Expand Up @@ -111,6 +114,26 @@ public String getFormat()
return "PKCS#8";
}

public String toString()
{
StringBuilder buf = new StringBuilder();
String nl = Strings.lineSeparator();
byte[] keyBytes = params.getPublicKey();

// -DM Hex.toHexString
buf.append(getAlgorithm())
.append(" ")
.append("Private Key").append(" [")
.append(new Fingerprint(keyBytes).toString())
.append("]")
.append(nl)
.append(" public data: ")
.append(Hex.toHexString(keyBytes))
.append(nl);

return buf.toString();
}

MLDSAPrivateKeyParameters getKeyParams()
{
return params;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import org.bouncycastle.pqc.crypto.util.PublicKeyFactory;
import org.bouncycastle.pqc.crypto.util.SubjectPublicKeyInfoFactory;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Fingerprint;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Hex;

public class BCMLDSAPublicKey
implements MLDSAPublicKey
Expand Down Expand Up @@ -74,6 +77,11 @@ public final String getAlgorithm()
return MLDSAParameterSpec.fromName(params.getParameters().getName()).getName();
}

public byte[] getPublicData()
{
return params.getEncoded();
}

public byte[] getEncoded()
{
try
Expand All @@ -98,6 +106,26 @@ public MLDSAParameterSpec getParameterSpec()
return MLDSAParameterSpec.fromName(params.getParameters().getName());
}

public String toString()
{
StringBuilder buf = new StringBuilder();
String nl = Strings.lineSeparator();
byte[] keyBytes = params.getEncoded();

// -DM Hex.toHexString
buf.append(getAlgorithm())
.append(" ")
.append("Public Key").append(" [")
.append(new Fingerprint(keyBytes).toString())
.append("]")
.append(nl)
.append(" public data: ")
.append(Hex.toHexString(keyBytes))
.append(nl);

return buf.toString();
}

CipherParameters getKeyParams()
{
return params;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.bouncycastle.jcajce.provider.asymmetric.mlkem;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.jcajce.interfaces.MLKEMPrivateKey;
Expand All @@ -9,10 +13,9 @@
import org.bouncycastle.pqc.crypto.util.PrivateKeyFactory;
import org.bouncycastle.pqc.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.util.Arrays;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.bouncycastle.util.Fingerprint;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Hex;

public class BCMLKEMPrivateKey
implements MLKEMPrivateKey
Expand Down Expand Up @@ -41,7 +44,7 @@ private void init(PrivateKeyInfo keyInfo)
{
this.attributes = keyInfo.getAttributes();;
this.params = (MLKEMPrivateKeyParameters)PrivateKeyFactory.createKey(keyInfo);
this.algorithm = params.getParameters().getName();
this.algorithm = MLKEMParameterSpec.fromName(params.getParameters().getName()).getName().toUpperCase();
}

/**
Expand Down Expand Up @@ -78,11 +81,10 @@ public int hashCode()
public final String getAlgorithm()
{
return algorithm;
// return MLKEMParameterSpec.fromName(params.getParameters().getName()).getName().toUpperCase();
}

public byte[] getEncoded()
{

try
{
PrivateKeyInfo pki = PrivateKeyInfoFactory.createPrivateKeyInfo(params, attributes);
Expand Down Expand Up @@ -110,7 +112,27 @@ public String getFormat()
return "PKCS#8";
}

public MLKEMPrivateKeyParameters getKeyParams()
public String toString()
{
StringBuilder buf = new StringBuilder();
String nl = Strings.lineSeparator();
byte[] keyBytes = params.getPublicKey();

// -DM Hex.toHexString
buf.append(getAlgorithm())
.append(" ")
.append("Private Key").append(" [")
.append(new Fingerprint(keyBytes).toString())
.append("]")
.append(nl)
.append(" public data: ")
.append(Hex.toHexString(keyBytes))
.append(nl);

return buf.toString();
}

MLKEMPrivateKeyParameters getKeyParams()
{
return params;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package org.bouncycastle.jcajce.provider.asymmetric.mlkem;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.jcajce.interfaces.MLKEMPublicKey;
import org.bouncycastle.jcajce.spec.MLKEMParameterSpec;
import org.bouncycastle.pqc.crypto.mlkem.MLKEMPublicKeyParameters;
import org.bouncycastle.pqc.crypto.util.PublicKeyFactory;
import org.bouncycastle.pqc.crypto.util.SubjectPublicKeyInfoFactory;
import org.bouncycastle.util.Arrays;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.bouncycastle.util.Fingerprint;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Hex;

public class BCMLKEMPublicKey
implements MLKEMPublicKey
Expand All @@ -20,7 +23,6 @@ public class BCMLKEMPublicKey
private transient MLKEMPublicKeyParameters params;

private transient String algorithm;
private transient byte[] encoding;

public BCMLKEMPublicKey(
MLKEMPublicKeyParameters params)
Expand All @@ -38,13 +40,13 @@ private void init(SubjectPublicKeyInfo keyInfo)
throws IOException
{
this.params = (MLKEMPublicKeyParameters)PublicKeyFactory.createKey(keyInfo);
this.algorithm = params.getParameters().getName();
this.algorithm = MLKEMParameterSpec.fromName(params.getParameters().getName()).getName().toUpperCase();
}

private void init(MLKEMPublicKeyParameters params)
{
this.params = params;
this.algorithm = params.getParameters().getName();
this.algorithm = MLKEMParameterSpec.fromName(params.getParameters().getName()).getName().toUpperCase();
}
/**
* Compare this ML-KEM public key with another object.
Expand Down Expand Up @@ -82,6 +84,11 @@ public final String getAlgorithm()
return algorithm;
}

public byte[] getPublicData()
{
return params.getEncoded();
}

public byte[] getEncoded()
{
try
Expand All @@ -106,7 +113,27 @@ public MLKEMParameterSpec getParameterSpec()
return MLKEMParameterSpec.fromName(params.getParameters().getName());
}

public MLKEMPublicKeyParameters getKeyParams()
public String toString()
{
StringBuilder buf = new StringBuilder();
String nl = Strings.lineSeparator();
byte[] keyBytes = params.getEncoded();

// -DM Hex.toHexString
buf.append(getAlgorithm())
.append(" ")
.append("Public Key").append(" [")
.append(new Fingerprint(keyBytes).toString())
.append("]")
.append(nl)
.append(" public data: ")
.append(Hex.toHexString(keyBytes))
.append(nl);

return buf.toString();
}

MLKEMPublicKeyParameters getKeyParams()
{
return params;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import org.bouncycastle.pqc.crypto.util.PrivateKeyFactory;
import org.bouncycastle.pqc.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Fingerprint;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Hex;

public class BCSLHDSAPrivateKey
implements SLHDSAPrivateKey
Expand Down Expand Up @@ -72,7 +74,7 @@ public int hashCode()
}

/**
* @return name of the algorithm - "SLH-DSA"
* @return name of the algorithm - "SLH-DSA..."
*/
public final String getAlgorithm()
{
Expand Down Expand Up @@ -109,6 +111,26 @@ public String getFormat()
return "PKCS#8";
}

public String toString()
{
StringBuilder buf = new StringBuilder();
String nl = Strings.lineSeparator();
byte[] keyBytes = params.getPublicKey();

// -DM Hex.toHexString
buf.append(getAlgorithm())
.append(" ")
.append("Private Key").append(" [")
.append(new Fingerprint(keyBytes).toString())
.append("]")
.append(nl)
.append(" public data: ")
.append(Hex.toHexString(keyBytes))
.append(nl);

return buf.toString();
}

SLHDSAPrivateKeyParameters getKeyParams()
{
return params;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import org.bouncycastle.pqc.crypto.util.PublicKeyFactory;
import org.bouncycastle.pqc.crypto.util.SubjectPublicKeyInfoFactory;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Fingerprint;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Hex;

public class BCSLHDSAPublicKey
implements SLHDSAPublicKey
Expand Down Expand Up @@ -75,6 +77,11 @@ public final String getAlgorithm()
return "SLH-DSA" + "-" + Strings.toUpperCase(params.getParameters().getName());
}

public byte[] getPublicData()
{
return params.getEncoded();
}

public byte[] getEncoded()
{
try
Expand All @@ -99,6 +106,26 @@ public SLHDSAParameterSpec getParameterSpec()
return SLHDSAParameterSpec.fromName(params.getParameters().getName());
}

public String toString()
{
StringBuilder buf = new StringBuilder();
String nl = Strings.lineSeparator();
byte[] keyBytes = params.getEncoded();

// -DM Hex.toHexString
buf.append(getAlgorithm())
.append(" ")
.append("Public Key").append(" [")
.append(new Fingerprint(keyBytes).toString())
.append("]")
.append(nl)
.append(" public data: ")
.append(Hex.toHexString(keyBytes))
.append(nl);

return buf.toString();
}

CipherParameters getKeyParams()
{
return params;
Expand Down

0 comments on commit a4f40dc

Please sign in to comment.