From 2deee2b1b7a4a3d14a1fbbe6c68183d37fe2863a Mon Sep 17 00:00:00 2001 From: zzz Date: Wed, 15 Dec 2010 16:10:03 +0000 Subject: [PATCH] AES cleanups and javadoc --- core/java/src/net/i2p/crypto/AESEngine.java | 32 ++++++++++++------- .../src/net/i2p/crypto/CryptixAESEngine.java | 8 ++--- .../net/i2p/crypto/CryptixAESKeyCache.java | 7 ++++ .../src/net/i2p/crypto/ElGamalAESEngine.java | 12 ++++--- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/core/java/src/net/i2p/crypto/AESEngine.java b/core/java/src/net/i2p/crypto/AESEngine.java index fa6a9110a..6f78df529 100644 --- a/core/java/src/net/i2p/crypto/AESEngine.java +++ b/core/java/src/net/i2p/crypto/AESEngine.java @@ -22,13 +22,14 @@ import net.i2p.util.RandomSource; * See CryptixAESEngine for the real thing. */ public class AESEngine { - private Log _log; - private I2PAppContext _context; + protected final Log _log; + protected final I2PAppContext _context; + public AESEngine(I2PAppContext ctx) { _context = ctx; - _log = _context.logManager().getLog(AESEngine.class); - if (getClass() == AESEngine.class) - _log.warn("Warning: AES is disabled"); + _log = _context.logManager().getLog(getClass()); + if (getClass().equals(AESEngine.class)) + _log.logAlways(Log.WARN, "AES is disabled"); } /** Encrypt the payload with the session key @@ -44,7 +45,10 @@ public class AESEngine { encrypt(payload, payloadIndex, out, outIndex, sessionKey, iv, 0, length); } - /** Encrypt the payload with the session key + /** + * Encrypt the payload with the session key. + * This just copies payload to out, see extension for the real thing. + * * @param payload data to be encrypted * @param payloadIndex index into the payload to start encrypting * @param out where to store the result @@ -55,7 +59,7 @@ public class AESEngine { */ public void encrypt(byte payload[], int payloadIndex, byte out[], int outIndex, SessionKey sessionKey, byte iv[], int ivOffset, int length) { System.arraycopy(payload, payloadIndex, out, outIndex, length); - _log.warn("Warning: AES is disabled"); + _log.logAlways(Log.WARN, "AES is disabled"); } public byte[] safeEncrypt(byte payload[], SessionKey sessionKey, byte iv[], int paddedSize) { @@ -118,7 +122,6 @@ public class AESEngine { return data; } - /** Decrypt the data with the session key * @param payload data to be decrypted * @param payloadIndex index into the payload to start decrypting @@ -132,7 +135,10 @@ public class AESEngine { decrypt(payload, payloadIndex, out, outIndex, sessionKey, iv, 0, length); } - /** Decrypt the data with the session key + /** + * Decrypt the data with the session key. + * This just copies payload to out, see extension for the real thing. + * * @param payload data to be decrypted * @param payloadIndex index into the payload to start decrypting * @param out where to store the cleartext @@ -143,18 +149,20 @@ public class AESEngine { */ public void decrypt(byte payload[], int payloadIndex, byte out[], int outIndex, SessionKey sessionKey, byte iv[], int ivOffset, int length) { System.arraycopy(payload, payloadIndex, out, outIndex, length); - _log.warn("Warning: AES is disabled"); + _log.logAlways(Log.WARN, "AES is disabled"); } /** - * Just copies payload to out + * This just copies payload to out, see extension for the real thing. * @param sessionKey unused */ public void encryptBlock(byte payload[], int inIndex, SessionKey sessionKey, byte out[], int outIndex) { System.arraycopy(payload, inIndex, out, outIndex, out.length - outIndex); } - /** decrypt the data with the session key provided + /** + * This just copies payload to rv, see extension for the real thing. + * * @param payload encrypted data * @param sessionKey private session key */ diff --git a/core/java/src/net/i2p/crypto/CryptixAESEngine.java b/core/java/src/net/i2p/crypto/CryptixAESEngine.java index acd2eb702..ea2338003 100644 --- a/core/java/src/net/i2p/crypto/CryptixAESEngine.java +++ b/core/java/src/net/i2p/crypto/CryptixAESEngine.java @@ -27,18 +27,16 @@ import net.i2p.util.Log; * @author jrandom, thecrypto */ public class CryptixAESEngine extends AESEngine { - private Log _log; private final static CryptixRijndael_Algorithm _algo = new CryptixRijndael_Algorithm(); private final static boolean USE_FAKE_CRYPTO = false; - private final static byte FAKE_KEY = 0x2A; - private CryptixAESKeyCache _cache; + // keys are now cached in the SessionKey objects + //private CryptixAESKeyCache _cache; private static final ByteCache _prevCache = ByteCache.getInstance(16, 16); public CryptixAESEngine(I2PAppContext context) { super(context); - _log = context.logManager().getLog(CryptixAESEngine.class); - _cache = new CryptixAESKeyCache(); + //_cache = new CryptixAESKeyCache(); } /** @param length must be a multiple of 16 */ diff --git a/core/java/src/net/i2p/crypto/CryptixAESKeyCache.java b/core/java/src/net/i2p/crypto/CryptixAESKeyCache.java index 5d39c1578..63b6dcfba 100644 --- a/core/java/src/net/i2p/crypto/CryptixAESKeyCache.java +++ b/core/java/src/net/i2p/crypto/CryptixAESKeyCache.java @@ -8,6 +8,8 @@ import java.util.concurrent.LinkedBlockingQueue; * data referenced in it is needed (which often is only one or two lines * of code) * + * Unused as a class, as the keys are cached in the SessionKey objects, + * but the static methods are used in FortunaStandalone. */ public final class CryptixAESKeyCache { private final LinkedBlockingQueue _availableKeys; @@ -20,6 +22,9 @@ public final class CryptixAESKeyCache { private static final int MAX_KEYS = 64; + /* + * @deprecated unused, keys are now cached in the SessionKey objects + */ public CryptixAESKeyCache() { _availableKeys = new LinkedBlockingQueue(MAX_KEYS); } @@ -27,6 +32,7 @@ public final class CryptixAESKeyCache { /** * Get the next available structure, either from the cache or a brand new one * + * @deprecated unused, keys are now cached in the SessionKey objects */ public final KeyCacheEntry acquireKey() { KeyCacheEntry rv = _availableKeys.poll(); @@ -38,6 +44,7 @@ public final class CryptixAESKeyCache { /** * Put this structure back onto the available cache for reuse * + * @deprecated unused, keys are now cached in the SessionKey objects */ public final void releaseKey(KeyCacheEntry key) { _availableKeys.offer(key); diff --git a/core/java/src/net/i2p/crypto/ElGamalAESEngine.java b/core/java/src/net/i2p/crypto/ElGamalAESEngine.java index f4f276de2..11010f312 100644 --- a/core/java/src/net/i2p/crypto/ElGamalAESEngine.java +++ b/core/java/src/net/i2p/crypto/ElGamalAESEngine.java @@ -29,17 +29,17 @@ import net.i2p.util.Log; /** * Handles the actual ElGamal+AES encryption and decryption scenarios using the * supplied keys and data. + * + * No, this does not extend AESEngine or CryptixAESEngine. */ public class ElGamalAESEngine { - private final static Log _log = new Log(ElGamalAESEngine.class); + private final Log _log; private final static int MIN_ENCRYPTED_SIZE = 80; // smallest possible resulting size - private I2PAppContext _context; - - private ElGamalAESEngine() { // nop - } + private final I2PAppContext _context; public ElGamalAESEngine(I2PAppContext ctx) { _context = ctx; + _log = _context.logManager().getLog(ElGamalAESEngine.class); _context.statManager().createFrequencyStat("crypto.elGamalAES.encryptNewSession", "how frequently we encrypt to a new ElGamal/AES+SessionTag session?", @@ -627,6 +627,7 @@ public class ElGamalAESEngine { return numPadding; } +/**** public static void main(String args[]) { I2PAppContext ctx = new I2PAppContext(); ElGamalAESEngine e = new ElGamalAESEngine(ctx); @@ -656,4 +657,5 @@ public class ElGamalAESEngine { } } } +****/ }