Skip to content

Commit

Permalink
fix: Fixes for EC public key calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed Dec 4, 2024
1 parent 7e17bd2 commit 2433916
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/main/java/com/danubetech/keyformats/PublicKeyBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ public static ECPublicKey bytes_to_P_256PublicKey(byte[] publicKeyBytes) {
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);
byte[] k = new byte[33];
System.arraycopy(publicKeyBytes, 0, k, 0, k.length);
ECNamedCurveParameterSpec ecNamedCurveParameterSpec = ECNamedCurveTable.getParameterSpec("secp256k1");
org.bouncycastle.math.ec.ECPoint bcEcPoint = ecNamedCurveParameterSpec.getCurve().decodePoint(x);
org.bouncycastle.math.ec.ECPoint bcEcPoint = ecNamedCurveParameterSpec.getCurve().decodePoint(k);
x = bcEcPoint.getRawXCoord().getEncoded();
y = bcEcPoint.getRawYCoord().getEncoded();
}
Expand Down Expand Up @@ -239,9 +240,10 @@ public static ECPublicKey bytes_to_P_384PublicKey(byte[] publicKeyBytes) {
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);
byte[] k = new byte[49];
System.arraycopy(publicKeyBytes, 0, k, 0, k.length);
ECNamedCurveParameterSpec ecNamedCurveParameterSpec = ECNamedCurveTable.getParameterSpec("secp384r1");
org.bouncycastle.math.ec.ECPoint bcEcPoint = ecNamedCurveParameterSpec.getCurve().decodePoint(x);
org.bouncycastle.math.ec.ECPoint bcEcPoint = ecNamedCurveParameterSpec.getCurve().decodePoint(k);
x = bcEcPoint.getRawXCoord().getEncoded();
y = bcEcPoint.getRawYCoord().getEncoded();
}
Expand Down Expand Up @@ -292,9 +294,10 @@ public static ECPublicKey bytes_to_P_521PublicKey(byte[] publicKeyBytes) {
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);
byte[] k = new byte[133];
System.arraycopy(publicKeyBytes, 0, k, 0, k.length);
ECNamedCurveParameterSpec ecNamedCurveParameterSpec = ECNamedCurveTable.getParameterSpec("secp521r1");
org.bouncycastle.math.ec.ECPoint bcEcPoint = ecNamedCurveParameterSpec.getCurve().decodePoint(k);
x = bcEcPoint.getRawXCoord().getEncoded();
y = bcEcPoint.getRawYCoord().getEncoded();
}
Expand Down

0 comments on commit 2433916

Please sign in to comment.