propagate from branch 'i2p.i2p' (head 44eff3cb8553cf29a7e4eb6c02f624648f91b124)

to branch 'i2p.i2p.zzz.android' (head 66bd014debdd51906e18555d12906ee20c016ef6)
This commit is contained in:
zzz
2009-06-29 14:04:49 +00:00
23 changed files with 935 additions and 29 deletions

View File

@ -12,10 +12,11 @@ import net.i2p.util.Log;
* has been eaten)
*/
public class AsyncFortunaStandalone extends FortunaStandalone implements Runnable {
private static final int BUFFERS = 16;
private static final int DEFAULT_BUFFERS = 16;
private static final int BUFSIZE = 256*1024;
private final byte asyncBuffers[][] = new byte[BUFFERS][BUFSIZE];
private final int status[] = new int[BUFFERS];
private int _bufferCount;
private final byte asyncBuffers[][];
private final int status[];
private int nextBuf = 0;
private I2PAppContext _context;
private Log _log;
@ -27,7 +28,10 @@ public class AsyncFortunaStandalone extends FortunaStandalone implements Runnabl
public AsyncFortunaStandalone(I2PAppContext context) {
super();
for (int i = 0; i < BUFFERS; i++)
_bufferCount = context.getProperty("router.prng.buffers", DEFAULT_BUFFERS);
asyncBuffers = new byte[_bufferCount][BUFSIZE];
status = new int[_bufferCount];
for (int i = 0; i < _bufferCount; i++)
status[i] = STATUS_NEED_FILL;
_context = context;
context.statManager().createRateStat("prng.bufferWaitTime", "", "Encryption", new long[] { 60*1000, 10*60*1000, 60*60*1000 } );
@ -80,11 +84,11 @@ public class AsyncFortunaStandalone extends FortunaStandalone implements Runnabl
status[nextBuf] = STATUS_LIVE;
int prev=nextBuf-1;
if (prev<0)
prev = BUFFERS-1;
prev = _bufferCount-1;
if (status[prev] == STATUS_LIVE)
status[prev] = STATUS_NEED_FILL;
nextBuf++;
if (nextBuf >= BUFFERS)
if (nextBuf >= _bufferCount)
nextBuf = 0;
asyncBuffers.notify();
}
@ -95,7 +99,7 @@ public class AsyncFortunaStandalone extends FortunaStandalone implements Runnabl
int toFill = -1;
try {
synchronized (asyncBuffers) {
for (int i = 0; i < BUFFERS; i++) {
for (int i = 0; i < _bufferCount; i++) {
if (status[i] == STATUS_NEED_FILL) {
status[i] = STATUS_FILLING;
toFill = i;

View File

@ -7,7 +7,8 @@ import net.i2p.data.Hash;
import net.i2p.data.SessionKey;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.Mac;
import org.bouncycastle.crypto.macs.I2PHMac;
/**
* Calculate the HMAC-SHA256 of a key+message. All the good stuff occurs
@ -19,15 +20,15 @@ public class HMAC256Generator extends HMACGenerator {
public HMAC256Generator(I2PAppContext context) { super(context); }
@Override
protected HMac acquire() {
protected I2PHMac acquire() {
synchronized (_available) {
if (_available.size() > 0)
return (HMac)_available.remove(0);
return (I2PHMac)_available.remove(0);
}
// the HMAC is hardcoded to use SHA256 digest size
// for backwards compatability. next time we have a backwards
// incompatible change, we should update this by removing ", 32"
return new HMac(new Sha256ForMAC());
return new I2PHMac(new Sha256ForMAC());
}
private class Sha256ForMAC extends Sha256Standalone implements Digest {

View File

@ -10,7 +10,8 @@ import net.i2p.data.Hash;
import net.i2p.data.SessionKey;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.Mac;
import org.bouncycastle.crypto.macs.I2PHMac;
/**
* Calculate the HMAC-MD5 of a key+message. All the good stuff occurs
@ -49,7 +50,7 @@ public class HMACGenerator {
if ((key == null) || (key.getData() == null) || (data == null))
throw new NullPointerException("Null arguments for HMAC");
HMac mac = acquire();
I2PHMac mac = acquire();
mac.init(key.getData());
mac.update(data, offset, length);
//byte rv[] = new byte[Hash.HASH_LENGTH];
@ -73,7 +74,7 @@ public class HMACGenerator {
if ((key == null) || (key.getData() == null) || (curData == null))
throw new NullPointerException("Null arguments for HMAC");
HMac mac = acquire();
Mac mac = acquire();
mac.init(key.getData());
mac.update(curData, curOffset, curLength);
byte rv[] = acquireTmp();
@ -86,17 +87,17 @@ public class HMACGenerator {
return eq;
}
protected HMac acquire() {
protected I2PHMac acquire() {
synchronized (_available) {
if (_available.size() > 0)
return (HMac)_available.remove(0);
return (I2PHMac)_available.remove(0);
}
// the HMAC is hardcoded to use SHA256 digest size
// for backwards compatability. next time we have a backwards
// incompatible change, we should update this by removing ", 32"
return new HMac(new MD5Digest(), 32);
return new I2PHMac(new MD5Digest(), 32);
}
private void release(HMac mac) {
private void release(Mac mac) {
synchronized (_available) {
if (_available.size() < 64)
_available.add(mac);
@ -122,4 +123,4 @@ public class HMACGenerator {
_availableTmp.add((Object)tmp);
}
}
}
}

View File

@ -37,6 +37,7 @@ import java.util.TreeMap;
import java.util.zip.Deflater;
import net.i2p.util.ByteCache;
import net.i2p.util.FileStreamFactory;
import net.i2p.util.OrderedProperties;
import net.i2p.util.ReusableGZIPInputStream;
import net.i2p.util.ReusableGZIPOutputStream;
@ -217,7 +218,7 @@ public class DataHelper {
loadProps(props, file, false);
}
public static void loadProps(Properties props, File file, boolean forceLowerCase) throws IOException {
loadProps(props, new FileInputStream(file), forceLowerCase);
loadProps(props, FileStreamFactory.getFileInputStream(file), forceLowerCase);
}
public static void loadProps(Properties props, InputStream inStr) throws IOException {
loadProps(props, inStr, false);

View File

@ -30,6 +30,7 @@ public class DecayingBloomFilter {
private boolean _keepDecaying;
private DecayEvent _decayEvent;
private static final int DEFAULT_M = 23;
private static final boolean ALWAYS_MISS = false;
/**
@ -44,8 +45,12 @@ public class DecayingBloomFilter {
_context = context;
_log = context.logManager().getLog(DecayingBloomFilter.class);
_entryBytes = entryBytes;
_current = new BloomSHA1(23, 11); //new BloomSHA1(23, 11);
_previous = new BloomSHA1(23, 11); //new BloomSHA1(23, 11);
// this is instantiated in four different places, they may have different
// requirements, but for now use this as a gross method of memory reduction.
// m == 23 => 2MB each BloomSHA1 (8MB total)
int m = context.getProperty("router.decayingBloomFilterM", DEFAULT_M);
_current = new BloomSHA1(m, 11); //new BloomSHA1(23, 11);
_previous = new BloomSHA1(m, 11); //new BloomSHA1(23, 11);
_durationMs = durationMs;
int numExtenders = (32+ (entryBytes-1))/entryBytes - 1;
if (numExtenders < 0)

View File

@ -0,0 +1,36 @@
/*
* public domain
*/
package net.i2p.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* This is pulled out and replaced in the android build.
*
* @author zzz
*/
public class FileStreamFactory {
public static FileInputStream getFileInputStream(String f) throws FileNotFoundException {
return new FileInputStream(f);
}
public static FileInputStream getFileInputStream(File f) throws FileNotFoundException {
return new FileInputStream(f);
}
public static FileOutputStream getFileOutputStream(String f) throws FileNotFoundException {
return new FileOutputStream(f);
}
public static FileOutputStream getFileOutputStream(File f) throws FileNotFoundException {
return new FileOutputStream(f);
}
}

View File

@ -0,0 +1,24 @@
/*
* public domain
*/
package net.i2p.util;
import java.io.File;
/**
* This is pulled out and replaced in the android build.
*
* @author zzz
*/
public class I2PFile extends File {
public I2PFile (String f) {
super(f);
}
public I2PFile (File p, String f) {
super(p, f);
}
}

View File

@ -42,8 +42,12 @@ import org.bouncycastle.crypto.Mac;
* a frequently used buffer (called on doFinal). changes released into the public
* domain in 2005.
*
* This is renamed from HMac because the constructor HMac(digest, sz) does not exist
* in the standard bouncycastle library, thus it conflicts in JVMs that contain the
* standard library (Android).
*
*/
public class HMac
public class I2PHMac
implements Mac
{
private final static int BLOCK_LENGTH = 64;
@ -56,12 +60,12 @@ implements Mac
private byte[] inputPad = new byte[BLOCK_LENGTH];
private byte[] outputPad = new byte[BLOCK_LENGTH];
public HMac(
public I2PHMac(
Digest digest)
{
this(digest, digest.getDigestSize());
}
public HMac(
public I2PHMac(
Digest digest, int sz)
{
this.digest = digest;