mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 22:59:54 +01:00
Use Bouncy Castle for encoding public keys.
This commit is contained in:
@@ -164,8 +164,8 @@ class CryptoComponentImpl implements CryptoComponent {
|
|||||||
// Return a wrapper that uses the SEC 1 encoding
|
// Return a wrapper that uses the SEC 1 encoding
|
||||||
ECPublicKeyParameters ecPublicKey =
|
ECPublicKeyParameters ecPublicKey =
|
||||||
(ECPublicKeyParameters) keyPair.getPublic();
|
(ECPublicKeyParameters) keyPair.getPublic();
|
||||||
PublicKey publicKey = new Sec1PublicKey(ecPublicKey,
|
PublicKey publicKey = new Sec1PublicKey(ecPublicKey
|
||||||
AGREEMENT_KEY_PAIR_BITS);
|
);
|
||||||
ECPrivateKeyParameters ecPrivateKey =
|
ECPrivateKeyParameters ecPrivateKey =
|
||||||
(ECPrivateKeyParameters) keyPair.getPrivate();
|
(ECPrivateKeyParameters) keyPair.getPrivate();
|
||||||
PrivateKey privateKey = new Sec1PrivateKey(ecPrivateKey,
|
PrivateKey privateKey = new Sec1PrivateKey(ecPrivateKey,
|
||||||
@@ -183,8 +183,8 @@ class CryptoComponentImpl implements CryptoComponent {
|
|||||||
// Return a wrapper that uses the SEC 1 encoding
|
// Return a wrapper that uses the SEC 1 encoding
|
||||||
ECPublicKeyParameters ecPublicKey =
|
ECPublicKeyParameters ecPublicKey =
|
||||||
(ECPublicKeyParameters) keyPair.getPublic();
|
(ECPublicKeyParameters) keyPair.getPublic();
|
||||||
PublicKey publicKey = new Sec1PublicKey(ecPublicKey,
|
PublicKey publicKey = new Sec1PublicKey(ecPublicKey
|
||||||
SIGNATURE_KEY_PAIR_BITS);
|
);
|
||||||
ECPrivateKeyParameters ecPrivateKey =
|
ECPrivateKeyParameters ecPrivateKey =
|
||||||
(ECPrivateKeyParameters) keyPair.getPrivate();
|
(ECPrivateKeyParameters) keyPair.getPrivate();
|
||||||
PrivateKey privateKey = new Sec1PrivateKey(ecPrivateKey,
|
PrivateKey privateKey = new Sec1PrivateKey(ecPrivateKey,
|
||||||
|
|||||||
@@ -1,11 +1,5 @@
|
|||||||
package org.briarproject.crypto;
|
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.KeyParser;
|
||||||
import org.briarproject.api.crypto.PrivateKey;
|
import org.briarproject.api.crypto.PrivateKey;
|
||||||
import org.briarproject.api.crypto.PublicKey;
|
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.ECCurve;
|
||||||
import org.spongycastle.math.ec.ECPoint;
|
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
|
* A key parser that uses the encoding defined in "SEC 1: Elliptic Curve
|
||||||
* Cryptography", section 2.3 (Certicom Corporation, May 2009). Point
|
* Cryptography", section 2.3 (Certicom Corporation, May 2009). Point
|
||||||
@@ -73,7 +73,7 @@ class Sec1KeyParser implements KeyParser {
|
|||||||
throw new GeneralSecurityException();
|
throw new GeneralSecurityException();
|
||||||
// Construct a public key from the point (x, y) and the params
|
// Construct a public key from the point (x, y) and the params
|
||||||
ECPublicKeyParameters k = new ECPublicKeyParameters(pub, params);
|
ECPublicKeyParameters k = new ECPublicKeyParameters(pub, params);
|
||||||
PublicKey p = new Sec1PublicKey(k, keyBits);
|
PublicKey p = new Sec1PublicKey(k);
|
||||||
long duration = System.currentTimeMillis() - now;
|
long duration = System.currentTimeMillis() - now;
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Parsing public key took " + duration + " ms");
|
LOG.info("Parsing public key took " + duration + " ms");
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package org.briarproject.crypto;
|
|||||||
|
|
||||||
import org.briarproject.api.crypto.PublicKey;
|
import org.briarproject.api.crypto.PublicKey;
|
||||||
import org.spongycastle.crypto.params.ECPublicKeyParameters;
|
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:
|
* 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 {
|
class Sec1PublicKey implements PublicKey {
|
||||||
|
|
||||||
private final ECPublicKeyParameters key;
|
private final ECPublicKeyParameters key;
|
||||||
private final int bytesPerInt, publicKeyBytes;
|
|
||||||
|
|
||||||
Sec1PublicKey(ECPublicKeyParameters key, int keyBits) {
|
Sec1PublicKey(ECPublicKeyParameters key) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
bytesPerInt = (keyBits + 7) / 8;
|
|
||||||
publicKeyBytes = 1 + 2 * bytesPerInt;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getEncoded() {
|
public byte[] getEncoded() {
|
||||||
byte[] encodedKey = new byte[publicKeyBytes];
|
return key.getQ().getEncoded(false);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ECPublicKeyParameters getKey() {
|
ECPublicKeyParameters getKey() {
|
||||||
|
|||||||
Reference in New Issue
Block a user