From 94cca5924975af03b4f4027f0020bbe1ec074606 Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Tue, 23 Feb 2016 16:16:29 +0000 Subject: [PATCH] Use Bouncy Castle for encoding public keys. --- .../briarproject/crypto/CryptoComponentImpl.java | 8 ++++---- .../org/briarproject/crypto/Sec1KeyParser.java | 14 +++++++------- .../org/briarproject/crypto/Sec1PublicKey.java | 16 ++-------------- 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/briar-core/src/org/briarproject/crypto/CryptoComponentImpl.java b/briar-core/src/org/briarproject/crypto/CryptoComponentImpl.java index 825c3720f5..bdf74dfaa8 100644 --- a/briar-core/src/org/briarproject/crypto/CryptoComponentImpl.java +++ b/briar-core/src/org/briarproject/crypto/CryptoComponentImpl.java @@ -164,8 +164,8 @@ class CryptoComponentImpl implements CryptoComponent { // Return a wrapper that uses the SEC 1 encoding ECPublicKeyParameters ecPublicKey = (ECPublicKeyParameters) keyPair.getPublic(); - PublicKey publicKey = new Sec1PublicKey(ecPublicKey, - AGREEMENT_KEY_PAIR_BITS); + PublicKey publicKey = new Sec1PublicKey(ecPublicKey + ); ECPrivateKeyParameters ecPrivateKey = (ECPrivateKeyParameters) keyPair.getPrivate(); PrivateKey privateKey = new Sec1PrivateKey(ecPrivateKey, @@ -183,8 +183,8 @@ class CryptoComponentImpl implements CryptoComponent { // Return a wrapper that uses the SEC 1 encoding ECPublicKeyParameters ecPublicKey = (ECPublicKeyParameters) keyPair.getPublic(); - PublicKey publicKey = new Sec1PublicKey(ecPublicKey, - SIGNATURE_KEY_PAIR_BITS); + PublicKey publicKey = new Sec1PublicKey(ecPublicKey + ); ECPrivateKeyParameters ecPrivateKey = (ECPrivateKeyParameters) keyPair.getPrivate(); PrivateKey privateKey = new Sec1PrivateKey(ecPrivateKey, diff --git a/briar-core/src/org/briarproject/crypto/Sec1KeyParser.java b/briar-core/src/org/briarproject/crypto/Sec1KeyParser.java index 99b3d2e727..a3bc7c71ee 100644 --- a/briar-core/src/org/briarproject/crypto/Sec1KeyParser.java +++ b/briar-core/src/org/briarproject/crypto/Sec1KeyParser.java @@ -1,11 +1,5 @@ package org.briarproject.crypto; -import static java.util.logging.Level.INFO; - -import java.math.BigInteger; -import java.security.GeneralSecurityException; -import java.util.logging.Logger; - import org.briarproject.api.crypto.KeyParser; import org.briarproject.api.crypto.PrivateKey; import org.briarproject.api.crypto.PublicKey; @@ -15,6 +9,12 @@ import org.spongycastle.crypto.params.ECPublicKeyParameters; import org.spongycastle.math.ec.ECCurve; import org.spongycastle.math.ec.ECPoint; +import java.math.BigInteger; +import java.security.GeneralSecurityException; +import java.util.logging.Logger; + +import static java.util.logging.Level.INFO; + /** * A key parser that uses the encoding defined in "SEC 1: Elliptic Curve * Cryptography", section 2.3 (Certicom Corporation, May 2009). Point @@ -73,7 +73,7 @@ class Sec1KeyParser implements KeyParser { throw new GeneralSecurityException(); // Construct a public key from the point (x, y) and the params ECPublicKeyParameters k = new ECPublicKeyParameters(pub, params); - PublicKey p = new Sec1PublicKey(k, keyBits); + PublicKey p = new Sec1PublicKey(k); long duration = System.currentTimeMillis() - now; if (LOG.isLoggable(INFO)) LOG.info("Parsing public key took " + duration + " ms"); diff --git a/briar-core/src/org/briarproject/crypto/Sec1PublicKey.java b/briar-core/src/org/briarproject/crypto/Sec1PublicKey.java index 95523c79b8..f85a34b8d7 100644 --- a/briar-core/src/org/briarproject/crypto/Sec1PublicKey.java +++ b/briar-core/src/org/briarproject/crypto/Sec1PublicKey.java @@ -2,7 +2,6 @@ package org.briarproject.crypto; import org.briarproject.api.crypto.PublicKey; import org.spongycastle.crypto.params.ECPublicKeyParameters; -import org.spongycastle.math.ec.ECPoint; /** * An elliptic curve public key that uses the encoding defined in "SEC 1: @@ -12,24 +11,13 @@ import org.spongycastle.math.ec.ECPoint; class Sec1PublicKey implements PublicKey { private final ECPublicKeyParameters key; - private final int bytesPerInt, publicKeyBytes; - Sec1PublicKey(ECPublicKeyParameters key, int keyBits) { + Sec1PublicKey(ECPublicKeyParameters key) { this.key = key; - bytesPerInt = (keyBits + 7) / 8; - publicKeyBytes = 1 + 2 * bytesPerInt; } public byte[] getEncoded() { - byte[] encodedKey = new byte[publicKeyBytes]; - encodedKey[0] = 4; - ECPoint pub = key.getQ().normalize(); - byte[] x = pub.getAffineXCoord().toBigInteger().toByteArray(); - Sec1Utils.convertToFixedLength(x, encodedKey, 1, bytesPerInt); - byte[] y = pub.getAffineYCoord().toBigInteger().toByteArray(); - Sec1Utils.convertToFixedLength(y, encodedKey, 1 + bytesPerInt, - bytesPerInt); - return encodedKey; + return key.getQ().getEncoded(false); } ECPublicKeyParameters getKey() { -- GitLab