Skip to content

Commit

Permalink
feat: More support for compressed EC keys
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed Dec 4, 2024
1 parent e0696af commit 7e17bd2
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/main/java/com/danubetech/keyformats/PublicKeyBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,22 @@ public static byte[] P_384PublicKey_to_bytes(ECPublicKey publicKey) {

public static ECPublicKey bytes_to_P_384PublicKey(byte[] publicKeyBytes) {

if (publicKeyBytes.length != 97) throw new IllegalArgumentException("Expected 97 bytes instead of " + publicKeyBytes.length);
if (publicKeyBytes.length != 49 && publicKeyBytes.length != 97) throw new IllegalArgumentException("Expected 97 bytes instead of " + publicKeyBytes.length);

byte[] x = new byte[48];
byte[] y = new byte[48];
if (publicKeyBytes[0] != 4) throw new IllegalArgumentException("Expected 0x04 as first byte instead of " + publicKeyBytes[0] + " (length: " + publicKeyBytes.length + ")");
System.arraycopy(publicKeyBytes, 1, x, 0, x.length);
System.arraycopy(publicKeyBytes, 1+x.length, y, 0, y.length);

if (publicKeyBytes.length == 97) {
System.arraycopy(publicKeyBytes, 1, x, 0, x.length);
System.arraycopy(publicKeyBytes, 1+x.length, y, 0, y.length);
} else {
System.arraycopy(publicKeyBytes, 1, x, 0, x.length);
ECNamedCurveParameterSpec ecNamedCurveParameterSpec = ECNamedCurveTable.getParameterSpec("secp384r1");
org.bouncycastle.math.ec.ECPoint bcEcPoint = ecNamedCurveParameterSpec.getCurve().decodePoint(x);
x = bcEcPoint.getRawXCoord().getEncoded();
y = bcEcPoint.getRawYCoord().getEncoded();
}

ECPublicKey publicKey;
try {
Expand Down Expand Up @@ -274,13 +283,21 @@ public static byte[] P_521PublicKey_to_bytes(ECPublicKey publicKey) {

public static ECPublicKey bytes_to_P_521PublicKey(byte[] publicKeyBytes) {

if (! (publicKeyBytes.length >= 129 && publicKeyBytes.length <= 133)) throw new IllegalArgumentException("Expected >=129 and <=133 bytes instead of " + publicKeyBytes.length);
if ((! (publicKeyBytes.length >= 65 && publicKeyBytes.length <= 67)) && (! (publicKeyBytes.length >= 129 && publicKeyBytes.length <= 133))) throw new IllegalArgumentException("Expected >=65 and <=67 OR >=129 and <=133 bytes instead of " + publicKeyBytes.length);

byte[] x = new byte[(publicKeyBytes.length-1)/2];
byte[] y = new byte[(publicKeyBytes.length-1)/2];
if (publicKeyBytes[0] != 4) throw new IllegalArgumentException("Expected 0x04 as first byte instead of " + publicKeyBytes[0] + " (length: " + publicKeyBytes.length + ")");
System.arraycopy(publicKeyBytes, 1, x, 0, x.length);
System.arraycopy(publicKeyBytes, 1+x.length, y, 0, y.length);

if (publicKeyBytes.length >= 129 && publicKeyBytes.length <= 133) {
System.arraycopy(publicKeyBytes, 1, x, 0, x.length);
System.arraycopy(publicKeyBytes, 1+x.length, y, 0, y.length);
} else {
System.arraycopy(publicKeyBytes, 1, x, 0, x.length);
ECNamedCurveParameterSpec ecNamedCurveParameterSpec = ECNamedCurveTable.getParameterSpec("secp384r1");
org.bouncycastle.math.ec.ECPoint bcEcPoint = ecNamedCurveParameterSpec.getCurve().decodePoint(x);
x = bcEcPoint.getRawXCoord().getEncoded();
y = bcEcPoint.getRawYCoord().getEncoded();
}

ECPublicKey publicKey;
try {
Expand Down

0 comments on commit 7e17bd2

Please sign in to comment.