AES cleanups and javadoc

This commit is contained in:
zzz
2010-12-15 16:10:03 +00:00
parent 8e709eec2e
commit 2deee2b1b7
4 changed files with 37 additions and 22 deletions

View File

@ -22,13 +22,14 @@ import net.i2p.util.RandomSource;
* See CryptixAESEngine for the real thing. * See CryptixAESEngine for the real thing.
*/ */
public class AESEngine { public class AESEngine {
private Log _log; protected final Log _log;
private I2PAppContext _context; protected final I2PAppContext _context;
public AESEngine(I2PAppContext ctx) { public AESEngine(I2PAppContext ctx) {
_context = ctx; _context = ctx;
_log = _context.logManager().getLog(AESEngine.class); _log = _context.logManager().getLog(getClass());
if (getClass() == AESEngine.class) if (getClass().equals(AESEngine.class))
_log.warn("Warning: AES is disabled"); _log.logAlways(Log.WARN, "AES is disabled");
} }
/** Encrypt the payload with the session key /** Encrypt the payload with the session key
@ -44,7 +45,10 @@ public class AESEngine {
encrypt(payload, payloadIndex, out, outIndex, sessionKey, iv, 0, length); 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 payload data to be encrypted
* @param payloadIndex index into the payload to start encrypting * @param payloadIndex index into the payload to start encrypting
* @param out where to store the result * @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) { 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); 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) { public byte[] safeEncrypt(byte payload[], SessionKey sessionKey, byte iv[], int paddedSize) {
@ -118,7 +122,6 @@ public class AESEngine {
return data; return data;
} }
/** Decrypt the data with the session key /** Decrypt the data with the session key
* @param payload data to be decrypted * @param payload data to be decrypted
* @param payloadIndex index into the payload to start decrypting * @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(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 payload data to be decrypted
* @param payloadIndex index into the payload to start decrypting * @param payloadIndex index into the payload to start decrypting
* @param out where to store the cleartext * @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) { 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); 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 * @param sessionKey unused
*/ */
public void encryptBlock(byte payload[], int inIndex, SessionKey sessionKey, byte out[], int outIndex) { public void encryptBlock(byte payload[], int inIndex, SessionKey sessionKey, byte out[], int outIndex) {
System.arraycopy(payload, inIndex, out, outIndex, out.length - 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 payload encrypted data
* @param sessionKey private session key * @param sessionKey private session key
*/ */

View File

@ -27,18 +27,16 @@ import net.i2p.util.Log;
* @author jrandom, thecrypto * @author jrandom, thecrypto
*/ */
public class CryptixAESEngine extends AESEngine { public class CryptixAESEngine extends AESEngine {
private Log _log;
private final static CryptixRijndael_Algorithm _algo = new CryptixRijndael_Algorithm(); private final static CryptixRijndael_Algorithm _algo = new CryptixRijndael_Algorithm();
private final static boolean USE_FAKE_CRYPTO = false; private final static boolean USE_FAKE_CRYPTO = false;
private final static byte FAKE_KEY = 0x2A; // keys are now cached in the SessionKey objects
private CryptixAESKeyCache _cache; //private CryptixAESKeyCache _cache;
private static final ByteCache _prevCache = ByteCache.getInstance(16, 16); private static final ByteCache _prevCache = ByteCache.getInstance(16, 16);
public CryptixAESEngine(I2PAppContext context) { public CryptixAESEngine(I2PAppContext context) {
super(context); super(context);
_log = context.logManager().getLog(CryptixAESEngine.class); //_cache = new CryptixAESKeyCache();
_cache = new CryptixAESKeyCache();
} }
/** @param length must be a multiple of 16 */ /** @param length must be a multiple of 16 */

View File

@ -8,6 +8,8 @@ import java.util.concurrent.LinkedBlockingQueue;
* data referenced in it is needed (which often is only one or two lines * data referenced in it is needed (which often is only one or two lines
* of code) * 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 { public final class CryptixAESKeyCache {
private final LinkedBlockingQueue<KeyCacheEntry> _availableKeys; private final LinkedBlockingQueue<KeyCacheEntry> _availableKeys;
@ -20,6 +22,9 @@ public final class CryptixAESKeyCache {
private static final int MAX_KEYS = 64; private static final int MAX_KEYS = 64;
/*
* @deprecated unused, keys are now cached in the SessionKey objects
*/
public CryptixAESKeyCache() { public CryptixAESKeyCache() {
_availableKeys = new LinkedBlockingQueue(MAX_KEYS); _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 * 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() { public final KeyCacheEntry acquireKey() {
KeyCacheEntry rv = _availableKeys.poll(); KeyCacheEntry rv = _availableKeys.poll();
@ -38,6 +44,7 @@ public final class CryptixAESKeyCache {
/** /**
* Put this structure back onto the available cache for reuse * 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) { public final void releaseKey(KeyCacheEntry key) {
_availableKeys.offer(key); _availableKeys.offer(key);

View File

@ -29,17 +29,17 @@ import net.i2p.util.Log;
/** /**
* Handles the actual ElGamal+AES encryption and decryption scenarios using the * Handles the actual ElGamal+AES encryption and decryption scenarios using the
* supplied keys and data. * supplied keys and data.
*
* No, this does not extend AESEngine or CryptixAESEngine.
*/ */
public class ElGamalAESEngine { 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 final static int MIN_ENCRYPTED_SIZE = 80; // smallest possible resulting size
private I2PAppContext _context; private final I2PAppContext _context;
private ElGamalAESEngine() { // nop
}
public ElGamalAESEngine(I2PAppContext ctx) { public ElGamalAESEngine(I2PAppContext ctx) {
_context = ctx; _context = ctx;
_log = _context.logManager().getLog(ElGamalAESEngine.class);
_context.statManager().createFrequencyStat("crypto.elGamalAES.encryptNewSession", _context.statManager().createFrequencyStat("crypto.elGamalAES.encryptNewSession",
"how frequently we encrypt to a new ElGamal/AES+SessionTag session?", "how frequently we encrypt to a new ElGamal/AES+SessionTag session?",
@ -627,6 +627,7 @@ public class ElGamalAESEngine {
return numPadding; return numPadding;
} }
/****
public static void main(String args[]) { public static void main(String args[]) {
I2PAppContext ctx = new I2PAppContext(); I2PAppContext ctx = new I2PAppContext();
ElGamalAESEngine e = new ElGamalAESEngine(ctx); ElGamalAESEngine e = new ElGamalAESEngine(ctx);
@ -656,4 +657,5 @@ public class ElGamalAESEngine {
} }
} }
} }
****/
} }