Author: nextgens
Date: 2007-10-21 11:12:42 +0000 (Sun, 21 Oct 2007)
New Revision: 15466

Modified:
   trunk/freenet/src/freenet/crypt/DiffieHellman.java
   trunk/freenet/src/freenet/crypt/DiffieHellmanLightContext.java
   trunk/freenet/src/freenet/node/FNPPacketMangler.java
   trunk/freenet/src/freenet/node/Node.java
   trunk/freenet/src/freenet/node/NodeCrypto.java
   trunk/freenet/src/freenet/node/NodeStarter.java
   trunk/freenet/src/freenet/node/PeerNode.java
Log:
revert r15453: I'll do it differently

Modified: trunk/freenet/src/freenet/crypt/DiffieHellman.java
===================================================================
--- trunk/freenet/src/freenet/crypt/DiffieHellman.java  2007-10-20 22:49:34 UTC 
(rev 15465)
+++ trunk/freenet/src/freenet/crypt/DiffieHellman.java  2007-10-21 11:12:42 UTC 
(rev 15466)
@@ -9,8 +9,6 @@
 import java.util.Random;
 import java.util.Stack;

-import freenet.node.FNPPacketMangler;
-import freenet.node.NodeCrypto;
 import freenet.support.Logger;
 import net.i2p.util.NativeBigInteger;

@@ -33,11 +31,9 @@
        private static final int PRECALC_TIMEOUT = 193 * 1000;

        private static Random r;
-       private static NodeCrypto crypt;
-       private static DSAGroup dsaGr;
-       private final static DHGroup group = Global.DHgroupA;
-       private final static Stack precalcBuffer = new Stack();
-       private final static Object precalcerWaitObj = new Object();
+       private static DHGroup group = Global.DHgroupA;
+       private static Stack precalcBuffer = new Stack();
+       private static Object precalcerWaitObj = new Object();

        private static Thread precalcThread;

@@ -82,10 +78,8 @@
                        }
                }

-       public static void init(Random random, NodeCrypto crypto, DSAGroup 
dsaGroup) {
-               crypt = crypto;
-               dsaGr = dsaGroup;
-               r = random;
+       public static void init(Random random) {
+           r = random;
                precalcThread.start();
        }

@@ -126,7 +120,7 @@
                if((time2 - time1) > 300) {
                        Logger.error(null, 
"DiffieHellman.generateLightContext(): time2 is more than 300ms after time1 
("+(time2 - time1)+ ')');
                }
-               return new DiffieHellmanLightContext(params[0], params[1], new 
DSASignature(params[2], params[3]));
+               return new DiffieHellmanLightContext(params[0], params[1]);
        }

        public static NativeBigInteger[] getParams() {
@@ -143,16 +137,10 @@
        }

        private static NativeBigInteger[] genParams() {
-               NativeBigInteger params[] = new NativeBigInteger[4];
-               
+               NativeBigInteger params[] = new NativeBigInteger[2];
+               // Don't need NativeBigInteger?
                params[0] = new NativeBigInteger(256, r);
-               NativeBigInteger exponential = (NativeBigInteger) 
group.getG().modPow(params[0], group.getP());
-               params[1] = exponential;
-               
-               DSASignature sig = 
crypt.sign(SHA256.digest(FNPPacketMangler.assembleDHParams(exponential, 
dsaGr)));
-               params[2] = new NativeBigInteger(sig.getR());
-               params[3] = new NativeBigInteger(sig.getS());
-               
+               params[1] = (NativeBigInteger) group.getG().modPow(params[0], 
group.getP());
                return params;
        }


Modified: trunk/freenet/src/freenet/crypt/DiffieHellmanLightContext.java
===================================================================
--- trunk/freenet/src/freenet/crypt/DiffieHellmanLightContext.java      
2007-10-20 22:49:34 UTC (rev 15465)
+++ trunk/freenet/src/freenet/crypt/DiffieHellmanLightContext.java      
2007-10-21 11:12:42 UTC (rev 15466)
@@ -29,13 +29,16 @@
                return sb.toString();
        }

-       public DiffieHellmanLightContext(NativeBigInteger myExponent, 
NativeBigInteger myExponential, DSASignature sig) {
+       public DiffieHellmanLightContext(NativeBigInteger myExponent, 
NativeBigInteger myExponential) {
                this.myExponent = myExponent;
                this.myExponential = myExponential;
-               this.signature = sig;
                logMINOR = Logger.shouldLog(Logger.MINOR, this);
        }

+       public void setSignature(DSASignature sig) {
+               this.signature = sig;
+       }
+       
        /*
         * Calling the following is costy; avoid
         */

Modified: trunk/freenet/src/freenet/node/FNPPacketMangler.java
===================================================================
--- trunk/freenet/src/freenet/node/FNPPacketMangler.java        2007-10-20 
22:49:34 UTC (rev 15465)
+++ trunk/freenet/src/freenet/node/FNPPacketMangler.java        2007-10-21 
11:12:42 UTC (rev 15466)
@@ -2347,10 +2347,6 @@
         * @see 
freenet.node.OutgoingPacketMangler#sendHandshake(freenet.node.PeerNode)
         */
        public void sendHandshake(PeerNode pn) {
-               if(!node.isHasStarted()) {
-                       Logger.normal(this, "Attempting to send a handshake 
while the node is starting up... cancel it.");
-                       return;
-               }
                int negType = pn.selectNegType(this);
                if(negType == -1) {
                        if(pn.isRoutingCompatible())
@@ -2435,7 +2431,7 @@
        }

        public int[] supportedNegTypes() {
-               return new int[] { 1, 2 };
+               return new int[] { 2, 1 };
        }

        public int fullHeadersLengthOneMessage() {
@@ -2465,6 +2461,7 @@
                        if((currentDHContext == null) || 
(currentDHContextLifetime + 1800000 /*30mins*/) < now) {
                                currentDHContextLifetime = now;
                                currentDHContext = 
DiffieHellman.generateLightContext();
+                               
currentDHContext.setSignature(crypto.sign(SHA256.digest(assembleDHParams(currentDHContext.myExponential,
 crypto.getCryptoGroup()))));
                        }
                }
                return currentDHContext;
@@ -2473,7 +2470,7 @@
        /*
         * Prepare DH parameters of message2 for them to be signed (useful in 
message3 to check the sig)
         */
-       public static byte[] assembleDHParams(NativeBigInteger exponential, 
DSAGroup group) {
+       private byte[] assembleDHParams(BigInteger exponential, DSAGroup group) 
{
                byte[] _myExponential = 
stripBigIntegerToNetworkFormat(exponential);
                byte[] _myGroup = group.getP().toByteArray();
                byte[] toSign = new byte[_myExponential.length + 
_myGroup.length];
@@ -2538,7 +2535,7 @@
                }
        }

-       public static byte[] stripBigIntegerToNetworkFormat(BigInteger 
exponential) {
+       private byte[] stripBigIntegerToNetworkFormat(BigInteger exponential) {
                byte[] data = exponential.toByteArray();
                int targetLength = DiffieHellman.modulusLengthInBytes();


Modified: trunk/freenet/src/freenet/node/Node.java
===================================================================
--- trunk/freenet/src/freenet/node/Node.java    2007-10-20 22:49:34 UTC (rev 
15465)
+++ trunk/freenet/src/freenet/node/Node.java    2007-10-21 11:12:42 UTC (rev 
15466)
@@ -45,7 +45,6 @@
 import freenet.config.PersistentConfig;
 import freenet.config.SubConfig;
 import freenet.crypt.DSAPublicKey;
-import freenet.crypt.DiffieHellman;
 import freenet.crypt.RandomSource;
 import freenet.crypt.SHA256;
 import freenet.crypt.Yarrow;
@@ -426,11 +425,6 @@
                }

                darknetCrypto.readCrypto(fs);
-               //TODO: That sucks. It breaks layering rules and will 
eventually break opennet as the key isn't likely to be the same.
-               // Tell me if you know how to improve it.
-               // No, generating two sigs isn't an option as it's sub-optimal.
-               // running one additionnal thread per mangler might be one.
-               DiffieHellman.init(random, darknetCrypto, 
darknetCrypto.getCryptoGroup());

                swapIdentifier = 
Fields.bytesToLong(darknetCrypto.identityHashHash);
                String loc = fs.get("location");

Modified: trunk/freenet/src/freenet/node/NodeCrypto.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeCrypto.java      2007-10-20 22:49:34 UTC 
(rev 15465)
+++ trunk/freenet/src/freenet/node/NodeCrypto.java      2007-10-21 11:12:42 UTC 
(rev 15466)
@@ -37,7 +37,7 @@
  * Cryptographic and transport level node identity. 
  * @author toad
  */
-public class NodeCrypto {
+class NodeCrypto {

        final Node node;
        final boolean isOpennet;
@@ -88,6 +88,7 @@
                logMINOR = Logger.shouldLog(Logger.MINOR, this);

                config.starting(this);
+               
                try {

                int port = config.getPort();
@@ -134,6 +135,7 @@
                socket.setLowLevelFilter(packetMangler = new 
FNPPacketMangler(node, this, socket));

                detector = new NodeIPPortDetector(node, node.ipDetector, this);
+               
                } catch (NodeInitException e) {
                        config.stopping(this);
                        throw e;
@@ -384,7 +386,7 @@
        }

        /** Sign a hash */
-       public DSASignature sign(byte[] hash) {
+       DSASignature sign(byte[] hash) {
                return DSA.sign(cryptoGroup, privKey, new NativeBigInteger(1, 
hash), random);
        }


Modified: trunk/freenet/src/freenet/node/NodeStarter.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeStarter.java     2007-10-20 22:49:34 UTC 
(rev 15465)
+++ trunk/freenet/src/freenet/node/NodeStarter.java     2007-10-21 11:12:42 UTC 
(rev 15466)
@@ -13,6 +13,7 @@
 import freenet.config.InvalidConfigValueException;
 import freenet.config.PersistentConfig;
 import freenet.config.SubConfig;
+import freenet.crypt.DiffieHellman;
 import freenet.crypt.RandomSource;
 import freenet.crypt.Yarrow;
 import freenet.support.Executor;
@@ -114,6 +115,8 @@

        // Setup RNG
        RandomSource random = new Yarrow();
+       
+       DiffieHellman.init(random);

                // Thread to keep the node up.
                // JVM deadlocks losing a lock when two threads of different 
types (daemon|app)
@@ -281,6 +284,7 @@
        // Setup RNG
        RandomSource random = new Yarrow();

+       DiffieHellman.init(random);

                // Thread to keep the node up.
                // JVM deadlocks losing a lock when two threads of different 
types (daemon|app)

Modified: trunk/freenet/src/freenet/node/PeerNode.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerNode.java        2007-10-20 22:49:34 UTC 
(rev 15465)
+++ trunk/freenet/src/freenet/node/PeerNode.java        2007-10-21 11:12:42 UTC 
(rev 15466)
@@ -2657,7 +2657,7 @@

        /**
         * Select the most appropriate negType, taking the user's preference 
into account
-        * order matters: last is best
+        * order matters
         * 
         * @param mangler
         * @return -1 if no common negType has been found


Reply via email to