From d3564dfcb5aa74209db06fdf32bdaa8e7b993ab3 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Tue, 29 Nov 2011 13:54:19 +0000
Subject: [PATCH]   * Random: Use new nextBytes(buf, off, len) for efficiency

---
 .../java/src/net/i2p/crypto/ElGamalAESEngine.java |  8 +++-----
 core/java/src/net/i2p/data/LeaseSet.java          |  4 +---
 .../src/net/i2p/data/i2np/BuildRequestRecord.java |  4 +---
 .../peermanager/ProfilePersistenceHelper.java     |  2 ++
 .../i2p/router/transport/ntcp/EstablishState.java | 15 +++++----------
 .../i2p/router/transport/udp/PacketBuilder.java   |  6 +-----
 6 files changed, 13 insertions(+), 26 deletions(-)

diff --git a/core/java/src/net/i2p/crypto/ElGamalAESEngine.java b/core/java/src/net/i2p/crypto/ElGamalAESEngine.java
index 99cdb95239..6f1c55888c 100644
--- a/core/java/src/net/i2p/crypto/ElGamalAESEngine.java
+++ b/core/java/src/net/i2p/crypto/ElGamalAESEngine.java
@@ -473,12 +473,10 @@ public class ElGamalAESEngine {
         //_log.debug("Encrypting to a NEW session");
         byte elgSrcData[] = new byte[SessionKey.KEYSIZE_BYTES+32+158];
         System.arraycopy(key.getData(), 0, elgSrcData, 0, SessionKey.KEYSIZE_BYTES);
+        // get both the preIV and the padding at once, then copy to the preIV array
+        _context.random().nextBytes(elgSrcData, SessionKey.KEYSIZE_BYTES, 32 + 158);
         byte preIV[] = SimpleByteCache.acquire(32);
-        _context.random().nextBytes(preIV);
-        System.arraycopy(preIV, 0, elgSrcData, SessionKey.KEYSIZE_BYTES, 32);
-        byte rnd[] = new byte[158];
-        _context.random().nextBytes(rnd);
-        System.arraycopy(rnd, 0, elgSrcData, SessionKey.KEYSIZE_BYTES+32, 158);
+        System.arraycopy(elgSrcData, SessionKey.KEYSIZE_BYTES, preIV, 0, 32);
 
         //_log.debug("Pre IV for encryptNewSession: " + DataHelper.toString(preIV, 32));
         //_log.debug("SessionKey for encryptNewSession: " + DataHelper.toString(key.getData(), 32));
diff --git a/core/java/src/net/i2p/data/LeaseSet.java b/core/java/src/net/i2p/data/LeaseSet.java
index 8f64043a3b..a1ba569cc9 100644
--- a/core/java/src/net/i2p/data/LeaseSet.java
+++ b/core/java/src/net/i2p/data/LeaseSet.java
@@ -393,9 +393,7 @@ public class LeaseSet extends DatabaseEntry {
         // pad out to multiple of 36 with random data after encryption
         // (even for 4 leases, where 36*4 is a multiple of 16, we add another, just to be consistent)
         padlen = enc.length - datalen;
-        pad = new byte[padlen];
-        RandomSource.getInstance().nextBytes(pad);
-        System.arraycopy(pad, 0, enc, datalen, padlen);
+        RandomSource.getInstance().nextBytes(enc, datalen, padlen);
         // add the padded lease...
         Lease padLease = new Lease();
         padLease.setEndDate(((Lease)_leases.get(0)).getEndDate());
diff --git a/router/java/src/net/i2p/data/i2np/BuildRequestRecord.java b/router/java/src/net/i2p/data/i2np/BuildRequestRecord.java
index cb44498aaa..10533cfac8 100644
--- a/router/java/src/net/i2p/data/i2np/BuildRequestRecord.java
+++ b/router/java/src/net/i2p/data/i2np/BuildRequestRecord.java
@@ -253,9 +253,7 @@ public class BuildRequestRecord {
         truncatedHour /= (60l*60l*1000l);
         DataHelper.toLong(buf, OFF_REQ_TIME, 4, truncatedHour);
         DataHelper.toLong(buf, OFF_SEND_MSG_ID, 4, nextMsgId);
-        byte rnd[] = new byte[PADDING_SIZE];
-        ctx.random().nextBytes(rnd);
-        System.arraycopy(rnd, 0, buf, OFF_SEND_MSG_ID+4, rnd.length);
+        ctx.random().nextBytes(buf, OFF_SEND_MSG_ID+4, PADDING_SIZE);
         
         byte wroteIV[] = readReplyIV();
         if (!DataHelper.eq(iv, wroteIV))
diff --git a/router/java/src/net/i2p/router/peermanager/ProfilePersistenceHelper.java b/router/java/src/net/i2p/router/peermanager/ProfilePersistenceHelper.java
index 3cce57c47d..756e4bf78a 100644
--- a/router/java/src/net/i2p/router/peermanager/ProfilePersistenceHelper.java
+++ b/router/java/src/net/i2p/router/peermanager/ProfilePersistenceHelper.java
@@ -356,6 +356,7 @@ class ProfilePersistenceHelper {
     }
     
     /** generate 1000 profiles */
+/****
     public static void main(String args[]) {
         System.out.println("Generating 1000 profiles");
         File dir = new File("profiles");
@@ -373,4 +374,5 @@ class ProfilePersistenceHelper {
         }
         System.out.println("1000 peers created in " + dir.getAbsolutePath());
     }
+****/
 }
diff --git a/router/java/src/net/i2p/router/transport/ntcp/EstablishState.java b/router/java/src/net/i2p/router/transport/ntcp/EstablishState.java
index ac8f9a49bf..ee287fd267 100644
--- a/router/java/src/net/i2p/router/transport/ntcp/EstablishState.java
+++ b/router/java/src/net/i2p/router/transport/ntcp/EstablishState.java
@@ -241,14 +241,12 @@ class EstablishState {
                     System.arraycopy(_Y, 0, xy, _X.length, _Y.length);
                     Hash hxy = _context.sha().calculateHash(xy);
                     _tsB = (_context.clock().now() + 500) / 1000l; // our (Bob's) timestamp in seconds
-                    byte padding[] = new byte[12]; // the encrypted data needs an extra 12 bytes
-                    _context.random().nextBytes(padding);
-                    byte toEncrypt[] = new byte[hxy.getData().length+4+padding.length];
+                    byte toEncrypt[] = new byte[hxy.getData().length + (4 + 12)];
                     System.arraycopy(hxy.getData(), 0, toEncrypt, 0, hxy.getData().length);
                     byte tsB[] = DataHelper.toLong(4, _tsB);
                     System.arraycopy(tsB, 0, toEncrypt, hxy.getData().length, tsB.length);
                     //DataHelper.toLong(toEncrypt, hxy.getData().length, 4, _tsB);
-                    System.arraycopy(padding, 0,toEncrypt, hxy.getData().length+4, padding.length);
+                    _context.random().nextBytes(toEncrypt, hxy.getData().length + 4, 12);
                     if (_log.shouldLog(Log.DEBUG)) {
                         //_log.debug(prefix()+"Y="+Base64.encode(_Y));
                         //_log.debug(prefix()+"x+y="+Base64.encode(xy));
@@ -453,9 +451,8 @@ class EstablishState {
                 DataHelper.toLong(preEncrypt, 0, 2, ident.length);
                 System.arraycopy(ident, 0, preEncrypt, 2, ident.length);
                 DataHelper.toLong(preEncrypt, 2+ident.length, 4, _tsA);
-                byte pad[] = new byte[padding];
-                _context.random().nextBytes(pad);
-                System.arraycopy(pad, 0, preEncrypt, 2+ident.length+4, padding);
+                if (padding > 0)
+                    _context.random().nextBytes(preEncrypt, 2 + ident.length + 4, padding);
                 System.arraycopy(sig.getData(), 0, preEncrypt, 2+ident.length+4+padding, Signature.SIGNATURE_BYTES);
 
                 _prevEncrypted = new byte[preEncrypt.length];
@@ -681,10 +678,8 @@ class EstablishState {
 
         Signature sig = _context.dsa().sign(toSign, _context.keyManager().getSigningPrivateKey());
         byte preSig[] = new byte[Signature.SIGNATURE_BYTES+8];
-        byte pad[] = new byte[8];
-        _context.random().nextBytes(pad);
         System.arraycopy(sig.getData(), 0, preSig, 0, Signature.SIGNATURE_BYTES);
-        System.arraycopy(pad, 0, preSig, Signature.SIGNATURE_BYTES, pad.length);
+        _context.random().nextBytes(preSig, Signature.SIGNATURE_BYTES, 8);
         _e_bobSig = new byte[preSig.length];
         _context.aes().encrypt(preSig, 0, _e_bobSig, 0, _dh.getSessionKey(), _e_hXY_tsB, _e_hXY_tsB.length-16, _e_bobSig.length);
 
diff --git a/router/java/src/net/i2p/router/transport/udp/PacketBuilder.java b/router/java/src/net/i2p/router/transport/udp/PacketBuilder.java
index 7db9bc3feb..3fd9c146c8 100644
--- a/router/java/src/net/i2p/router/transport/udp/PacketBuilder.java
+++ b/router/java/src/net/i2p/router/transport/udp/PacketBuilder.java
@@ -101,7 +101,6 @@ class PacketBuilder {
     
     private static final ByteCache _ivCache = ByteCache.getInstance(64, UDPPacket.IV_SIZE);
     private static final ByteCache _hmacCache = ByteCache.getInstance(64, Hash.HASH_LENGTH);
-    private static final ByteCache _blockCache = ByteCache.getInstance(64, 16);
 
     /**
      *  For debugging and stats only - does not go out on the wire.
@@ -280,10 +279,7 @@ class PacketBuilder {
         // pad up so we're on the encryption boundary
         int padSize = 16 - (off % 16);
         if (padSize > 0) {
-            ByteArray block = _blockCache.acquire();
-            _context.random().nextBytes(block.getData());
-            System.arraycopy(block.getData(), 0, data, off, padSize);
-            _blockCache.release(block);
+            _context.random().nextBytes(data, off, padSize);
             off += padSize;
         }
         packet.getPacket().setLength(off);
-- 
GitLab