2005-04-05 jrandom
* After a successfull netDb search for a leaseSet, republish it to all of the peers we have tried so far who did not give us the key (up to 10), rather than the old K closest (which may include peers who had given us the key) * Don't wait 5 minutes to publish a leaseSet (duh!), and rather than republish it every 5 minutes, republish it every 3. In addition, always republish as soon as the leaseSet changes (duh^2). * Minor fix for oddball startup race (thanks travis_bickle!) * Minor AES update to allow in-place decryption.
This commit is contained in:
@ -40,6 +40,19 @@ public class AESEngine {
|
||||
* @param length how much data to encrypt
|
||||
*/
|
||||
public void encrypt(byte payload[], int payloadIndex, byte out[], int outIndex, SessionKey sessionKey, byte iv[], int length) {
|
||||
encrypt(payload, payloadIndex, out, outIndex, sessionKey, iv, 0, length);
|
||||
}
|
||||
|
||||
/** Encrypt the payload with the session key
|
||||
* @param payload data to be encrypted
|
||||
* @param payloadIndex index into the payload to start encrypting
|
||||
* @param out where to store the result
|
||||
* @param outIndex where in out to start writing
|
||||
* @param sessionKey private esession key to encrypt to
|
||||
* @param iv IV for CBC
|
||||
* @param length how much data to encrypt
|
||||
*/
|
||||
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");
|
||||
}
|
||||
@ -120,6 +133,19 @@ public class AESEngine {
|
||||
* @param length how much data to decrypt
|
||||
*/
|
||||
public void decrypt(byte payload[], int payloadIndex, byte out[], int outIndex, SessionKey sessionKey, byte iv[], int length) {
|
||||
decrypt(payload, payloadIndex, out, outIndex, sessionKey, iv, 0, length);
|
||||
}
|
||||
|
||||
/** Decrypt the data with the session key
|
||||
* @param payload data to be decrypted
|
||||
* @param payloadIndex index into the payload to start decrypting
|
||||
* @param out where to store the cleartext
|
||||
* @param outIndex where in out to start writing
|
||||
* @param sessionKey private session key to decrypt to
|
||||
* @param iv IV for CBC
|
||||
* @param length how much data to decrypt
|
||||
*/
|
||||
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");
|
||||
}
|
||||
|
@ -12,8 +12,10 @@ package net.i2p.crypto;
|
||||
import java.security.InvalidKeyException;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.ByteArray;
|
||||
import net.i2p.data.DataHelper;
|
||||
import net.i2p.data.SessionKey;
|
||||
import net.i2p.util.ByteCache;
|
||||
import net.i2p.util.Log;
|
||||
|
||||
/**
|
||||
@ -31,14 +33,20 @@ public class CryptixAESEngine extends AESEngine {
|
||||
private final static byte FAKE_KEY = 0x2A;
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
public void encrypt(byte payload[], int payloadIndex, byte out[], int outIndex, SessionKey sessionKey, byte iv[], int length) {
|
||||
if ( (payload == null) || (out == null) || (sessionKey == null) || (iv == null) || (iv.length != 16) )
|
||||
encrypt(payload, payloadIndex, out, outIndex, sessionKey, iv, 0, length);
|
||||
}
|
||||
|
||||
public void encrypt(byte payload[], int payloadIndex, byte out[], int outIndex, SessionKey sessionKey, byte iv[], int ivOffset, int length) {
|
||||
if ( (payload == null) || (out == null) || (sessionKey == null) || (iv == null) )
|
||||
throw new NullPointerException("invalid args to aes");
|
||||
if (payload.length < payloadIndex + length)
|
||||
throw new IllegalArgumentException("Payload is too short");
|
||||
@ -57,7 +65,7 @@ public class CryptixAESEngine extends AESEngine {
|
||||
|
||||
int numblock = length / 16;
|
||||
|
||||
DataHelper.xor(iv, 0, payload, payloadIndex, out, outIndex, 16);
|
||||
DataHelper.xor(iv, ivOffset, payload, payloadIndex, out, outIndex, 16);
|
||||
encryptBlock(out, outIndex, sessionKey, out, outIndex);
|
||||
for (int x = 1; x < numblock; x++) {
|
||||
DataHelper.xor(out, outIndex + (x-1) * 16, payload, payloadIndex + x * 16, out, outIndex + x * 16, 16);
|
||||
@ -66,8 +74,10 @@ public class CryptixAESEngine extends AESEngine {
|
||||
}
|
||||
|
||||
public void decrypt(byte payload[], int payloadIndex, byte out[], int outIndex, SessionKey sessionKey, byte iv[], int length) {
|
||||
if ((iv== null) || (payload == null) || (payload.length <= 0) || (sessionKey == null)
|
||||
|| (iv.length != 16) )
|
||||
decrypt(payload, payloadIndex, out, outIndex, sessionKey, iv, 0, length);
|
||||
}
|
||||
public void decrypt(byte payload[], int payloadIndex, byte out[], int outIndex, SessionKey sessionKey, byte iv[], int ivOffset, int length) {
|
||||
if ((iv== null) || (payload == null) || (payload.length <= 0) || (sessionKey == null) )
|
||||
throw new IllegalArgumentException("bad setup");
|
||||
else if (out == null)
|
||||
throw new IllegalArgumentException("out is null");
|
||||
@ -84,12 +94,32 @@ public class CryptixAESEngine extends AESEngine {
|
||||
int numblock = length / 16;
|
||||
if (length % 16 != 0) numblock++;
|
||||
|
||||
ByteArray prevA = _prevCache.acquire();
|
||||
byte prev[] = prevA.getData();
|
||||
ByteArray curA = _prevCache.acquire();
|
||||
byte cur[] = curA.getData();
|
||||
System.arraycopy(iv, ivOffset, prev, 0, 16);
|
||||
|
||||
for (int x = 0; x < numblock; x++) {
|
||||
System.arraycopy(payload, payloadIndex + (x * 16), cur, 0, 16);
|
||||
decryptBlock(payload, payloadIndex + (x * 16), sessionKey, out, outIndex + (x * 16));
|
||||
DataHelper.xor(out, outIndex + x * 16, prev, 0, out, outIndex + x * 16, 16);
|
||||
iv = prev; // just use IV to switch 'em around
|
||||
prev = cur;
|
||||
cur = iv;
|
||||
}
|
||||
|
||||
/*
|
||||
decryptBlock(payload, payloadIndex, sessionKey, out, outIndex);
|
||||
DataHelper.xor(out, outIndex, iv, 0, out, outIndex, 16);
|
||||
for (int x = 1; x < numblock; x++) {
|
||||
decryptBlock(payload, payloadIndex + (x * 16), sessionKey, out, outIndex + (x * 16));
|
||||
DataHelper.xor(out, outIndex + x * 16, payload, payloadIndex + (x - 1) * 16, out, outIndex + x * 16, 16);
|
||||
}
|
||||
*/
|
||||
|
||||
_prevCache.release(prevA);
|
||||
_prevCache.release(curA);
|
||||
}
|
||||
|
||||
public final void encryptBlock(byte payload[], int inIndex, SessionKey sessionKey, byte out[], int outIndex) {
|
||||
|
@ -63,11 +63,20 @@ public class HMACSHA256Generator {
|
||||
* Calculate the HMAC of the data with the given key
|
||||
*/
|
||||
public Hash calculate(SessionKey key, byte data[]) {
|
||||
if ((key == null) || (key.getData() == null) || (data == null))
|
||||
throw new NullPointerException("Null arguments for HMAC");
|
||||
return calculate(key, data, 0, data.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the HMAC of the data with the given key
|
||||
*/
|
||||
public Hash calculate(SessionKey key, byte data[], int offset, int length) {
|
||||
if ((key == null) || (key.getData() == null) || (data == null))
|
||||
throw new NullPointerException("Null arguments for HMAC");
|
||||
|
||||
Buffer buf = new Buffer(data.length);
|
||||
calculate(key, data, buf);
|
||||
Buffer buf = new Buffer(length);
|
||||
calculate(key, data, offset, length, buf);
|
||||
Hash rv = new Hash(buf.rv);
|
||||
buf.releaseCached();
|
||||
return rv;
|
||||
@ -77,10 +86,17 @@ public class HMACSHA256Generator {
|
||||
* Calculate the HMAC of the data with the given key
|
||||
*/
|
||||
public void calculate(SessionKey key, byte data[], Buffer buf) {
|
||||
calculate(key, data, 0, data.length, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the HMAC of the data with the given key
|
||||
*/
|
||||
public void calculate(SessionKey key, byte data[], int offset, int length, Buffer buf) {
|
||||
// inner hash
|
||||
padKey(key.getData(), _IPAD, buf.padded);
|
||||
System.arraycopy(buf.padded, 0, buf.innerBuf, 0, PAD_LENGTH);
|
||||
System.arraycopy(data, 0, buf.innerBuf, PAD_LENGTH, data.length);
|
||||
System.arraycopy(data, offset, buf.innerBuf, PAD_LENGTH, length);
|
||||
|
||||
Hash h = _context.sha().calculateHash(buf.innerBuf, buf.innerEntry);
|
||||
|
||||
|
Reference in New Issue
Block a user