Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #20 from ZainabF92/issue15598
Browse files Browse the repository at this point in the history
Fix race condition when creating the native pointer for an EC key
  • Loading branch information
keithc-ca authored Aug 4, 2022
2 parents c2c80f7 + 87af1ee commit 524cd44
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ boolean isECFieldF2m() {
* @return the native EC public key context pointer or -1 on error
*/
long getNativePtr() {
if (nativeECKey == 0x0) {
if (this.nativeECKey == 0x0) {
synchronized (this) {
if (nativeECKey == 0x0) {
if (this.nativeECKey == 0x0) {
ECPoint generator = this.params.getGenerator();
EllipticCurve curve = this.params.getCurve();
ECField field = curve.getField();
Expand All @@ -240,26 +240,27 @@ long getNativePtr() {
byte[] gy = generator.getAffineY().toByteArray();
byte[] n = this.params.getOrder().toByteArray();
byte[] h = BigInteger.valueOf(this.params.getCofactor()).toByteArray();
byte[] p = new byte[0];
long nativePointer;
if (field instanceof ECFieldFp) {
p = ((ECFieldFp)field).getP().toByteArray();
nativeECKey = nativeCrypto.ECEncodeGFp(a, a.length, b, b.length, p, p.length, gx, gx.length, gy, gy.length, n, n.length, h, h.length);
byte[] p = ((ECFieldFp)field).getP().toByteArray();
nativePointer = nativeCrypto.ECEncodeGFp(a, a.length, b, b.length, p, p.length, gx, gx.length, gy, gy.length, n, n.length, h, h.length);
} else if (field instanceof ECFieldF2m) {
p = ((ECFieldF2m)field).getReductionPolynomial().toByteArray();
nativeECKey = nativeCrypto.ECEncodeGF2m(a, a.length, b, b.length, p, p.length, gx, gx.length, gy, gy.length, n, n.length, h, h.length);
byte[] p = ((ECFieldF2m)field).getReductionPolynomial().toByteArray();
nativePointer = nativeCrypto.ECEncodeGF2m(a, a.length, b, b.length, p, p.length, gx, gx.length, gy, gy.length, n, n.length, h, h.length);
} else {
nativeECKey = -1;
nativePointer = -1;
}
if (nativeECKey != -1) {
nativeCrypto.createECKeyCleaner(this, nativeECKey);
if (nativePointer != -1) {
nativeCrypto.createECKeyCleaner(this, nativePointer);
byte[] value = this.getS().toByteArray();
if (nativeCrypto.ECCreatePrivateKey(nativeECKey, value, value.length) == -1) {
nativeECKey = -1;
if (nativeCrypto.ECCreatePrivateKey(nativePointer, value, value.length) == -1) {
nativePointer = -1;
}
}
this.nativeECKey = nativePointer;
}
}
}
return nativeECKey;
return this.nativeECKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ boolean isECFieldF2m() {
* @return the native EC public key context pointer or -1 on error
*/
long getNativePtr() {
if (nativeECKey == 0x0) {
if (this.nativeECKey == 0x0) {
synchronized (this) {
if (nativeECKey == 0x0) {
if (this.nativeECKey == 0x0) {
ECPoint generator = this.params.getGenerator();
EllipticCurve curve = this.params.getCurve();
ECField field = curve.getField();
Expand All @@ -166,29 +166,30 @@ long getNativePtr() {
byte[] gy = generator.getAffineY().toByteArray();
byte[] n = this.params.getOrder().toByteArray();
byte[] h = BigInteger.valueOf(this.params.getCofactor()).toByteArray();
byte[] p = new byte[0];
long nativePointer;
int fieldType = 0;
if (field instanceof ECFieldFp) {
p = ((ECFieldFp)field).getP().toByteArray();
nativeECKey = nativeCrypto.ECEncodeGFp(a, a.length, b, b.length, p, p.length, gx, gx.length, gy, gy.length, n, n.length, h, h.length);
byte[] p = ((ECFieldFp)field).getP().toByteArray();
nativePointer = nativeCrypto.ECEncodeGFp(a, a.length, b, b.length, p, p.length, gx, gx.length, gy, gy.length, n, n.length, h, h.length);
} else if (field instanceof ECFieldF2m) {
fieldType = 1;
p = ((ECFieldF2m)field).getReductionPolynomial().toByteArray();
nativeECKey = nativeCrypto.ECEncodeGF2m(a, a.length, b, b.length, p, p.length, gx, gx.length, gy, gy.length, n, n.length, h, h.length);
byte[] p = ((ECFieldF2m)field).getReductionPolynomial().toByteArray();
nativePointer = nativeCrypto.ECEncodeGF2m(a, a.length, b, b.length, p, p.length, gx, gx.length, gy, gy.length, n, n.length, h, h.length);
} else {
nativeECKey = -1;
nativePointer = -1;
}
if (nativeECKey != -1) {
nativeCrypto.createECKeyCleaner(this, nativeECKey);
if (nativePointer != -1) {
nativeCrypto.createECKeyCleaner(this, nativePointer);
byte[] x = this.w.getAffineX().toByteArray();
byte[] y = this.w.getAffineY().toByteArray();
if (nativeCrypto.ECCreatePublicKey(nativeECKey, x, x.length, y, y.length, fieldType) == -1) {
nativeECKey = -1;
if (nativeCrypto.ECCreatePublicKey(nativePointer, x, x.length, y, y.length, fieldType) == -1) {
nativePointer = -1;
}
}
this.nativeECKey = nativePointer;
}
}
}
return nativeECKey;
return this.nativeECKey;
}
}

0 comments on commit 524cd44

Please sign in to comment.