Log the running time of key validation and message verification.

This commit is contained in:
akwizgran
2014-03-22 00:30:29 +00:00
parent d83513c5f9
commit fc66f6ed8a
2 changed files with 32 additions and 7 deletions

View File

@@ -1,7 +1,10 @@
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;
@@ -19,6 +22,9 @@ import org.spongycastle.math.ec.ECPoint;
*/
class Sec1KeyParser implements KeyParser {
private static final Logger LOG =
Logger.getLogger(Sec1KeyParser.class.getName());
private final ECDomainParameters params;
private final BigInteger modulus;
private final int keyBits, bytesPerInt, publicKeyBytes, privateKeyBytes;
@@ -36,6 +42,7 @@ class Sec1KeyParser implements KeyParser {
throws GeneralSecurityException {
// The validation procedure comes from SEC 1, section 3.2.2.1. Note
// that SEC 1 parameter names are used below, not RFC 5639 names
long now = System.currentTimeMillis();
if(encodedKey.length != publicKeyBytes)
throw new GeneralSecurityException();
// The first byte must be 0x04
@@ -66,11 +73,16 @@ 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);
return new Sec1PublicKey(k, keyBits);
PublicKey p = new Sec1PublicKey(k, keyBits);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Parsing public key took " + duration + " ms");
return p;
}
public PrivateKey parsePrivateKey(byte[] encodedKey)
throws GeneralSecurityException {
long now = System.currentTimeMillis();
if(encodedKey.length != privateKeyBytes)
throw new GeneralSecurityException();
BigInteger d = new BigInteger(1, encodedKey); // Positive signum
@@ -79,6 +91,10 @@ class Sec1KeyParser implements KeyParser {
throw new GeneralSecurityException();
// Construct a private key from the private value and the params
ECPrivateKeyParameters k = new ECPrivateKeyParameters(d, params);
return new Sec1PrivateKey(k, keyBits);
PrivateKey p = new Sec1PrivateKey(k, keyBits);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Parsing private key took " + duration + " ms");
return p;
}
}

View File

@@ -1,8 +1,10 @@
package org.briarproject.messaging;
import static java.util.logging.Level.INFO;
import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
import java.security.GeneralSecurityException;
import java.util.logging.Logger;
import javax.inject.Inject;
@@ -20,6 +22,9 @@ import org.briarproject.api.system.Clock;
class MessageVerifierImpl implements MessageVerifier {
private static final Logger LOG =
Logger.getLogger(MessageVerifierImpl.class.getName());
private final CryptoComponent crypto;
private final Clock clock;
private final KeyParser keyParser;
@@ -33,11 +38,11 @@ class MessageVerifierImpl implements MessageVerifier {
public Message verifyMessage(UnverifiedMessage m)
throws GeneralSecurityException {
long now = System.currentTimeMillis();
MessageDigest messageDigest = crypto.getMessageDigest();
Signature signature = crypto.getSignature();
// Reject the message if it's too far in the future
long now = clock.currentTimeMillis();
if(m.getTimestamp() > now + MAX_CLOCK_DIFFERENCE)
if(m.getTimestamp() > clock.currentTimeMillis() + MAX_CLOCK_DIFFERENCE)
throw new GeneralSecurityException();
// Hash the message to get the message ID
byte[] raw = m.getSerialised();
@@ -52,8 +57,12 @@ class MessageVerifierImpl implements MessageVerifier {
if(!signature.verify(m.getSignature()))
throw new GeneralSecurityException();
}
return new MessageImpl(id, m.getParent(), m.getGroup(), author,
m.getContentType(), m.getTimestamp(), raw, m.getBodyStart(),
m.getBodyLength());
Message verified = new MessageImpl(id, m.getParent(), m.getGroup(),
author, m.getContentType(), m.getTimestamp(), raw,
m.getBodyStart(), m.getBodyLength());
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Verifying message took " + duration + " ms");
return verified;
}
}