[freenet-cvs] r17650 - in trunk/freenet/src/freenet: crypt node

nextgens at freenetproject.org nextgens at freenetproject.org
Thu Feb 7 12:37:11 UTC 2008


Author: nextgens
Date: 2008-02-07 12:37:10 +0000 (Thu, 07 Feb 2008)
New Revision: 17650

Modified:
   trunk/freenet/src/freenet/crypt/HMAC.java
   trunk/freenet/src/freenet/crypt/Util.java
   trunk/freenet/src/freenet/node/FNPPacketMangler.java
   trunk/freenet/src/freenet/node/NodeDispatcher.java
   trunk/freenet/src/freenet/node/PeerNode.java
Log:
Get rid of Digests in favour of MessageDigests so that the logic in the HMAC class is cleaner... and we can recycle MessageDigests

Modified: trunk/freenet/src/freenet/crypt/HMAC.java
===================================================================
--- trunk/freenet/src/freenet/crypt/HMAC.java	2008-02-07 12:34:02 UTC (rev 17649)
+++ trunk/freenet/src/freenet/crypt/HMAC.java	2008-02-07 12:37:10 UTC (rev 17650)
@@ -7,6 +7,8 @@
 import java.util.Arrays;
 
 import freenet.support.HexUtil;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 
 /**
  * Implements the HMAC Keyed Message Authentication function, as described
@@ -24,18 +26,10 @@
 	}
     }
 
-    protected Digest d;
-
-    public HMAC(Digest d) {
-	this.d=d;
-    }
-
-    public int digestSize() {
-	return d.digestSize();
-    }
+    protected MessageDigest d;
     
-    public Digest getDigest() {
-	return d;
+    public HMAC(MessageDigest md) {
+	    this.d = md;
     }
 
     public boolean verify(byte[] K, byte[] text, byte[] mac) {
@@ -87,7 +81,8 @@
     }
 
     public static void main(String[] args) throws UnsupportedEncodingException {
-	HMAC s=new HMAC(SHA1.getInstance());
+	HMAC s = null;
+	try { s = new HMAC(MessageDigest.getInstance("SHA1")); } catch (NoSuchAlgorithmException e) {}
 	byte[] key=new byte[20];
 	System.err.println("20x0b, 'Hi There':");
 	byte[] text;
@@ -123,4 +118,27 @@
 
     }
 
+	public static byte[] macWithSHA256(byte[] K, byte[] text, int macbytes) {
+		MessageDigest sha256 = null;
+		try {
+			sha256 = SHA256.getMessageDigest();
+			HMAC hash = new HMAC(sha256);
+			return hash.mac(K, text, macbytes);
+		} finally {
+			if(sha256 != null)
+				SHA256.returnMessageDigest(sha256);
+		}
+	}
+
+	public static boolean verifyWithSHA256(byte[] K, byte[] text, byte[] mac) {
+		MessageDigest sha256 = null;
+		try {
+			sha256 = SHA256.getMessageDigest();
+			HMAC hash = new HMAC(sha256);
+			return hash.verify(K, text, mac);
+		} finally {
+			if(sha256 != null)
+				SHA256.returnMessageDigest(sha256);
+		}
+	}
 }	

Modified: trunk/freenet/src/freenet/crypt/Util.java
===================================================================
--- trunk/freenet/src/freenet/crypt/Util.java	2008-02-07 12:34:02 UTC (rev 17649)
+++ trunk/freenet/src/freenet/crypt/Util.java	2008-02-07 12:37:10 UTC (rev 17650)
@@ -14,6 +14,7 @@
 import freenet.crypt.ciphers.Rijndael;
 import freenet.support.HexUtil;
 import freenet.support.Loader;
+import java.security.MessageDigest;
 import net.i2p.util.NativeBigInteger;
 
 public class Util {
@@ -131,12 +132,12 @@
 		return new NativeBigInteger(bl, r);
 	}
 
-	public static byte[] hashBytes(Digest d, byte[] b) {
+	public static byte[] hashBytes(MessageDigest d, byte[] b) {
 		return hashBytes(d, b, 0, b.length);
 	}
 
 	public static byte[] hashBytes(
-		Digest d,
+		MessageDigest d,
 		byte[] b,
 		int offset,
 		int length) {

Modified: trunk/freenet/src/freenet/node/FNPPacketMangler.java
===================================================================
--- trunk/freenet/src/freenet/node/FNPPacketMangler.java	2008-02-07 12:34:02 UTC (rev 17649)
+++ trunk/freenet/src/freenet/node/FNPPacketMangler.java	2008-02-07 12:37:10 UTC (rev 17650)
@@ -819,8 +819,7 @@
 		node.random.nextBytes(myNonce);
 		byte[] r = ctx.signature.getRBytes(Node.SIGNATURE_PARAMETER_LENGTH);
 		byte[] s = ctx.signature.getSBytes(Node.SIGNATURE_PARAMETER_LENGTH);
-		HMAC hash = new HMAC(SHA256.getInstance());
-		byte[] authenticator = hash.mac(getTransientKey(),assembleJFKAuthenticator(myExponential, hisExponential, myNonce, nonceInitator, replyTo.getAddress().getAddress()), HASH_LENGTH);
+		byte[] authenticator = HMAC.macWithSHA256(getTransientKey(),assembleJFKAuthenticator(myExponential, hisExponential, myNonce, nonceInitator, replyTo.getAddress().getAddress()), HASH_LENGTH);
 		if(logMINOR) Logger.minor(this, "We are using the following HMAC : " + HexUtil.bytesToHex(authenticator));
 
 		byte[] message2 = new byte[NONCE_SIZE*2+DiffieHellman.modulusLengthInBytes()+
@@ -1036,8 +1035,7 @@
 
 		// We *WANT* to check the hmac before we do the lookup on the hashmap
 		// @see https://bugs.freenetproject.org/view.php?id=1604
-		HMAC mac = new HMAC(SHA256.getInstance());
-		if(!mac.verify(getTransientKey(), assembleJFKAuthenticator(responderExponential, initiatorExponential, nonceResponder, nonceInitiator, replyTo.getAddress().getAddress()) , authenticator)) {
+		if(!HMAC.verifyWithSHA256(getTransientKey(), assembleJFKAuthenticator(responderExponential, initiatorExponential, nonceResponder, nonceInitiator, replyTo.getAddress().getAddress()) , authenticator)) {
 			if(shouldLogErrorInHandshake(t1))
 				Logger.normal(this, "The HMAC doesn't match; let's discard the packet (either we rekeyed or we are victim of forgery) - JFK3 - "+pn);
 			return;
@@ -1080,7 +1078,7 @@
 		System.arraycopy(JFK_PREFIX_INITIATOR, 0, decypheredPayload, decypheredPayloadOffset, JFK_PREFIX_INITIATOR.length);
 		decypheredPayloadOffset += JFK_PREFIX_INITIATOR.length;
 		System.arraycopy(payload, inputOffset, decypheredPayload, decypheredPayloadOffset, decypheredPayload.length-decypheredPayloadOffset);
-		if(!mac.verify(Ka, decypheredPayload, hmac)) {
+		if(!HMAC.verifyWithSHA256(Ka, decypheredPayload, hmac)) {
 			Logger.error(this, "The inner-HMAC doesn't match; let's discard the packet JFK(3) - "+pn);
 			return;
 		}
@@ -1249,8 +1247,7 @@
 		System.arraycopy(JFK_PREFIX_RESPONDER, 0, decypheredPayload, decypheredPayloadOffset, JFK_PREFIX_RESPONDER.length);
 		decypheredPayloadOffset += JFK_PREFIX_RESPONDER.length;
 		System.arraycopy(payload, inputOffset, decypheredPayload, decypheredPayloadOffset, payload.length-inputOffset);
-		HMAC mac = new HMAC(SHA256.getInstance());
-		if(!mac.verify(pn.jfkKa, decypheredPayload, hmac)) {
+		if(!HMAC.verifyWithSHA256(pn.jfkKa, decypheredPayload, hmac)) {
 			Logger.normal(this, "The digest-HMAC doesn't match; let's discard the packet - "+pn.getPeer());
 			return false;
 		}
@@ -1429,8 +1426,7 @@
 		pcfb.blockEncipher(cleartext, cleartextToEncypherOffset, cleartext.length-cleartextToEncypherOffset);
 		
 		// We compute the HMAC of (prefix + cyphertext) Includes the IV!
-		HMAC mac = new HMAC(SHA256.getInstance());
-		byte[] hmac = mac.mac(pn.jfkKa, cleartext, HASH_LENGTH);
+		byte[] hmac = HMAC.macWithSHA256(pn.jfkKa, cleartext, HASH_LENGTH);
 		
 		// copy stuffs back to the message
 		System.arraycopy(hmac, 0, message3, offset, HASH_LENGTH);
@@ -1521,8 +1517,7 @@
 		pk.blockEncipher(cyphertext, cleartextToEncypherOffset, cyphertext.length - cleartextToEncypherOffset);
 		
 		// We compute the HMAC of (prefix + iv + signature)
-		HMAC mac = new HMAC(SHA256.getInstance());
-		byte[] hmac = mac.mac(Ka, cyphertext, HASH_LENGTH);
+		byte[] hmac = HMAC.macWithSHA256(Ka, cyphertext, HASH_LENGTH);
 		
 		// Message4 = hmac + IV + encryptedSignature
 		byte[] message4 = new byte[HASH_LENGTH + ivLength + (cyphertext.length - cleartextToEncypherOffset)]; 
@@ -2704,7 +2699,6 @@
 	
 	private byte[] computeJFKSharedKey(BigInteger exponential, byte[] nI, byte[] nR, String what) {
 		assert("0".equals(what) || "1".equals(what) || "2".equals(what));
-		HMAC mac = new HMAC(SHA256.getInstance());
 		byte[] number = null;
 		try { number = what.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) {}
 		
@@ -2716,7 +2710,7 @@
 		offset += NONCE_SIZE;
 		System.arraycopy(number, 0, toHash, offset, number.length);
 		
-		return mac.mac(exponential.toByteArray(), toHash, HASH_LENGTH);
+		return HMAC.macWithSHA256(exponential.toByteArray(), toHash, HASH_LENGTH);
 	}
 
 	private long timeLastReset = -1;

Modified: trunk/freenet/src/freenet/node/NodeDispatcher.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeDispatcher.java	2008-02-07 12:34:02 UTC (rev 17649)
+++ trunk/freenet/src/freenet/node/NodeDispatcher.java	2008-02-07 12:37:10 UTC (rev 17650)
@@ -183,8 +183,7 @@
 		Key key = (Key) m.getObject(DMT.KEY);
 		byte[] authenticator = ((ShortBuffer) m.getObject(DMT.OFFER_AUTHENTICATOR)).getData();
 		long uid = m.getLong(DMT.UID);
-		HMAC hash = new HMAC(SHA256.getInstance());
-		if(!hash.verify(node.failureTable.offerAuthenticatorKey, key.getFullKey(), authenticator)) {
+		if(!HMAC.verifyWithSHA256(node.failureTable.offerAuthenticatorKey, key.getFullKey(), authenticator)) {
 			Logger.error(this, "Invalid offer request from "+source+" : authenticator did not verify");
 			try {
 				source.sendAsync(DMT.createFNPGetOfferedKeyInvalid(uid, DMT.GET_OFFERED_KEY_REJECTED_BAD_AUTHENTICATOR), null, 0, null);

Modified: trunk/freenet/src/freenet/node/PeerNode.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerNode.java	2008-02-07 12:34:02 UTC (rev 17649)
+++ trunk/freenet/src/freenet/node/PeerNode.java	2008-02-07 12:37:10 UTC (rev 17650)
@@ -3083,9 +3083,8 @@
 	/** Offer a key to this node */
 	public void offer(Key key) {
 		byte[] keyBytes = key.getFullKey();
-		HMAC hash = new HMAC(SHA256.getInstance());
 		// FIXME maybe the authenticator should be shorter than 32 bytes to save memory?
-		byte[] authenticator = hash.mac(node.failureTable.offerAuthenticatorKey, keyBytes, 32);
+		byte[] authenticator = HMAC.macWithSHA256(node.failureTable.offerAuthenticatorKey, keyBytes, 32);
 		Message msg = DMT.createFNPOfferKey(key, authenticator);
 		try {
 			sendAsync(msg, null, 0, null);




More information about the cvs mailing list