From fc66f6ed8ac52a0c1d6c934405c676e1fceeebf9 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Sat, 22 Mar 2014 00:30:29 +0000
Subject: [PATCH] Log the running time of key validation and message
 verification.

---
 .../briarproject/crypto/Sec1KeyParser.java    | 20 +++++++++++++++++--
 .../messaging/MessageVerifierImpl.java        | 19 +++++++++++++-----
 2 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/briar-core/src/org/briarproject/crypto/Sec1KeyParser.java b/briar-core/src/org/briarproject/crypto/Sec1KeyParser.java
index 0446c6a523..efb3b7781d 100644
--- a/briar-core/src/org/briarproject/crypto/Sec1KeyParser.java
+++ b/briar-core/src/org/briarproject/crypto/Sec1KeyParser.java
@@ -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;
 	}
 }
diff --git a/briar-core/src/org/briarproject/messaging/MessageVerifierImpl.java b/briar-core/src/org/briarproject/messaging/MessageVerifierImpl.java
index ecc16ac196..636be555f4 100644
--- a/briar-core/src/org/briarproject/messaging/MessageVerifierImpl.java
+++ b/briar-core/src/org/briarproject/messaging/MessageVerifierImpl.java
@@ -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;
 	}
 }
-- 
GitLab