forked from I2P_Developers/i2p.i2p
2006-02-15 jrandom
* Merged in the i2p_0_6_1_10_PRE branch to the trunk, so CVS HEAD is no longer backwards compatible (and should not be used until 0.6.1.1 is out)
This commit is contained in:
@ -135,6 +135,8 @@ public class CPUID {
|
||||
{
|
||||
if(!_nativeOk)
|
||||
throw new UnknownCPUException("Failed to read CPU information from the system. Please verify the existence of the jcpuid dll/so.");
|
||||
if(getCPUVendorID().equals("CentaurHauls"))
|
||||
return new VIAC3Impl();
|
||||
if(!isX86)
|
||||
throw new UnknownCPUException("Failed to read CPU information from the system. The CPUID instruction exists on x86 CPU's only");
|
||||
if(getCPUVendorID().equals("AuthenticAMD"))
|
||||
@ -159,6 +161,11 @@ public class CPUID {
|
||||
public boolean hasSSE2(){
|
||||
return (getCPUFlags() & 0x4000000) >0; //Bit 26
|
||||
}
|
||||
public boolean IsC3Compatible() { return false; }
|
||||
}
|
||||
protected static class VIAC3Impl extends CPUIDCPUInfo implements CPUInfo {
|
||||
public boolean isC3Compatible() { return true; }
|
||||
public String getCPUModelString() { return "VIA C3"; }
|
||||
}
|
||||
protected static class AMDInfoImpl extends CPUIDCPUInfo implements AMDCPUInfo
|
||||
{
|
||||
|
@ -41,5 +41,6 @@ public interface CPUInfo
|
||||
* @return true iff the CPU support the SSE2 instruction set.
|
||||
*/
|
||||
public boolean hasSSE2();
|
||||
|
||||
|
||||
public boolean IsC3Compatible();
|
||||
}
|
||||
|
@ -34,10 +34,11 @@ class SessionStatusMessageHandler extends HandlerImpl {
|
||||
break;
|
||||
case SessionStatusMessage.STATUS_DESTROYED:
|
||||
_log.info("Session destroyed");
|
||||
session.destroySession();
|
||||
//session.destroySession();
|
||||
session.reconnect(); // la la la
|
||||
break;
|
||||
case SessionStatusMessage.STATUS_INVALID:
|
||||
session.destroySession();
|
||||
session.destroySession(); // ok, honor this destroy message, because we're b0rked
|
||||
break;
|
||||
case SessionStatusMessage.STATUS_UPDATED:
|
||||
_log.info("Session status updated");
|
||||
|
@ -228,7 +228,7 @@ public class DHSessionKeyBuilder {
|
||||
*/
|
||||
public BigInteger generateMyValue() {
|
||||
long start = System.currentTimeMillis();
|
||||
_myPrivateValue = new NativeBigInteger(2048, RandomSource.getInstance());
|
||||
_myPrivateValue = new NativeBigInteger(KeyGenerator.PUBKEY_EXPONENT_SIZE, RandomSource.getInstance());
|
||||
BigInteger myValue = CryptoConstants.elgg.modPow(_myPrivateValue, CryptoConstants.elgp);
|
||||
long end = System.currentTimeMillis();
|
||||
long diff = end - start;
|
||||
|
@ -40,7 +40,7 @@ public class HMACSHA256Generator {
|
||||
_useMD5 = true;
|
||||
else
|
||||
_useMD5 = false;
|
||||
if ("true".equals(context.getProperty("i2p.HMACBrokenSize", "true")))
|
||||
if ("true".equals(context.getProperty("i2p.HMACBrokenSize", "false")))
|
||||
_macSize = 32;
|
||||
else
|
||||
_macSize = (_useMD5 ? 16 : 32);
|
||||
|
@ -52,13 +52,27 @@ public class KeyGenerator {
|
||||
key.setData(data);
|
||||
return key;
|
||||
}
|
||||
|
||||
/** standard exponent size */
|
||||
private static final int PUBKEY_EXPONENT_SIZE_FULL = 2048;
|
||||
/**
|
||||
* short exponent size, which should be safe for use with the Oakley primes,
|
||||
* per "On Diffie-Hellman Key Agreement with Short Exponents" - van Oorschot, Weiner
|
||||
* at EuroCrypt 96, and crypto++'s benchmarks at http://www.eskimo.com/~weidai/benchmarks.html
|
||||
* Also, "Koshiba & Kurosawa: Short Exponent Diffie-Hellman Problems" (PKC 2004, LNCS 2947, pp. 173-186)
|
||||
* aparently supports this, according to
|
||||
* http://groups.google.com/group/sci.crypt/browse_thread/thread/1855a5efa7416677/339fa2f945cc9ba0#339fa2f945cc9ba0
|
||||
* (damn commercial access to http://www.springerlink.com/(xrkdvv45w0cmnur4aimsxx55)/app/home/contribution.asp?referrer=parent&backto=issue,13,31;journal,893,3280;linkingpublicationresults,1:105633,1 )
|
||||
*/
|
||||
private static final int PUBKEY_EXPONENT_SIZE_SHORT = 226;
|
||||
public static final int PUBKEY_EXPONENT_SIZE = PUBKEY_EXPONENT_SIZE_SHORT;
|
||||
|
||||
/** Generate a pair of keys, where index 0 is a PublicKey, and
|
||||
* index 1 is a PrivateKey
|
||||
* @return pair of keys
|
||||
*/
|
||||
public Object[] generatePKIKeypair() {
|
||||
BigInteger a = new NativeBigInteger(2048, _context.random());
|
||||
BigInteger a = new NativeBigInteger(PUBKEY_EXPONENT_SIZE, _context.random());
|
||||
BigInteger aalpha = CryptoConstants.elgg.modPow(a, CryptoConstants.elgp);
|
||||
|
||||
Object[] keys = new Object[2];
|
||||
@ -130,7 +144,7 @@ public class KeyGenerator {
|
||||
* Pad the buffer w/ leading 0s or trim off leading bits so the result is the
|
||||
* given length.
|
||||
*/
|
||||
private final static byte[] padBuffer(byte src[], int length) {
|
||||
final static byte[] padBuffer(byte src[], int length) {
|
||||
byte buf[] = new byte[length];
|
||||
|
||||
if (src.length > buf.length) // extra bits, chop leading bits
|
||||
|
@ -129,7 +129,7 @@ class YKGenerator {
|
||||
long t1 = 0;
|
||||
while (k == null) {
|
||||
t0 = Clock.getInstance().now();
|
||||
k = new NativeBigInteger(2048, RandomSource.getInstance());
|
||||
k = new NativeBigInteger(KeyGenerator.PUBKEY_EXPONENT_SIZE, RandomSource.getInstance());
|
||||
t1 = Clock.getInstance().now();
|
||||
if (BigInteger.ZERO.compareTo(k) == 0) {
|
||||
k = null;
|
||||
|
@ -34,6 +34,8 @@ public class Certificate extends DataStructureImpl {
|
||||
public final static int CERTIFICATE_TYPE_NULL = 0;
|
||||
/** specifies a Hashcash style certificate */
|
||||
public final static int CERTIFICATE_TYPE_HASHCASH = 1;
|
||||
/** we should not be used for anything (don't use us in the netDb, in tunnels, or tell others about us) */
|
||||
public final static int CERTIFICATE_TYPE_HIDDEN = 2;
|
||||
|
||||
public Certificate() {
|
||||
_type = 0;
|
||||
@ -76,7 +78,7 @@ public class Certificate extends DataStructureImpl {
|
||||
|
||||
public void writeBytes(OutputStream out) throws DataFormatException, IOException {
|
||||
if (_type < 0) throw new DataFormatException("Invalid certificate type: " + _type);
|
||||
if ((_type != 0) && (_payload == null)) throw new DataFormatException("Payload is required for non null type");
|
||||
//if ((_type != 0) && (_payload == null)) throw new DataFormatException("Payload is required for non null type");
|
||||
|
||||
DataHelper.writeLong(out, 1, _type);
|
||||
if (_payload != null) {
|
||||
|
@ -63,6 +63,16 @@ public class RouterIdentity extends DataStructureImpl {
|
||||
_signingKey = key;
|
||||
__calculatedHash = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This router specified that they should not be used as a part of a tunnel,
|
||||
* nor queried for the netDb, and that disclosure of their contact information
|
||||
* should be limited.
|
||||
*
|
||||
*/
|
||||
public boolean isHidden() {
|
||||
return (_certificate != null) && (_certificate.getCertificateType() == Certificate.CERTIFICATE_TYPE_HIDDEN);
|
||||
}
|
||||
|
||||
public void readBytes(InputStream in) throws DataFormatException, IOException {
|
||||
_publicKey = new PublicKey();
|
||||
|
@ -80,14 +80,17 @@ public class DecayingBloomFilter {
|
||||
*
|
||||
*/
|
||||
public boolean add(byte entry[]) {
|
||||
return add(entry, 0, entry.length);
|
||||
}
|
||||
public boolean add(byte entry[], int off, int len) {
|
||||
if (ALWAYS_MISS) return false;
|
||||
if (entry == null)
|
||||
throw new IllegalArgumentException("Null entry");
|
||||
if (entry.length != _entryBytes)
|
||||
throw new IllegalArgumentException("Bad entry [" + entry.length + ", expected "
|
||||
if (len != _entryBytes)
|
||||
throw new IllegalArgumentException("Bad entry [" + len + ", expected "
|
||||
+ _entryBytes + "]");
|
||||
synchronized (this) {
|
||||
return locked_add(entry);
|
||||
return locked_add(entry, off, len);
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,14 +104,15 @@ public class DecayingBloomFilter {
|
||||
if (ALWAYS_MISS) return false;
|
||||
synchronized (this) {
|
||||
if (_entryBytes <= 7)
|
||||
entry &= _longToEntryMask;
|
||||
entry = ((entry ^ _longToEntryMask) & ((1 << 31)-1)) | (entry ^ _longToEntryMask);
|
||||
//entry &= _longToEntryMask;
|
||||
if (entry < 0) {
|
||||
DataHelper.toLong(_longToEntry, 0, _entryBytes, 0-entry);
|
||||
_longToEntry[0] |= (1 << 7);
|
||||
} else {
|
||||
DataHelper.toLong(_longToEntry, 0, _entryBytes, entry);
|
||||
}
|
||||
return locked_add(_longToEntry);
|
||||
return locked_add(_longToEntry, 0, _longToEntry.length);
|
||||
}
|
||||
}
|
||||
|
||||
@ -121,26 +125,26 @@ public class DecayingBloomFilter {
|
||||
if (ALWAYS_MISS) return false;
|
||||
synchronized (this) {
|
||||
if (_entryBytes <= 7)
|
||||
entry &= _longToEntryMask;
|
||||
entry = ((entry ^ _longToEntryMask) & ((1 << 31)-1)) | (entry ^ _longToEntryMask);
|
||||
if (entry < 0) {
|
||||
DataHelper.toLong(_longToEntry, 0, _entryBytes, 0-entry);
|
||||
_longToEntry[0] |= (1 << 7);
|
||||
} else {
|
||||
DataHelper.toLong(_longToEntry, 0, _entryBytes, entry);
|
||||
}
|
||||
return locked_add(_longToEntry, false);
|
||||
return locked_add(_longToEntry, 0, _longToEntry.length, false);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean locked_add(byte entry[]) {
|
||||
return locked_add(entry, true);
|
||||
private boolean locked_add(byte entry[], int offset, int len) {
|
||||
return locked_add(entry, offset, len, true);
|
||||
}
|
||||
private boolean locked_add(byte entry[], boolean addIfNew) {
|
||||
private boolean locked_add(byte entry[], int offset, int len, boolean addIfNew) {
|
||||
if (_extended != null) {
|
||||
// extend the entry to 32 bytes
|
||||
System.arraycopy(entry, 0, _extended, 0, entry.length);
|
||||
System.arraycopy(entry, offset, _extended, 0, len);
|
||||
for (int i = 0; i < _extenders.length; i++)
|
||||
DataHelper.xor(entry, 0, _extenders[i], 0, _extended, _entryBytes * (i+1), _entryBytes);
|
||||
DataHelper.xor(entry, offset, _extenders[i], 0, _extended, _entryBytes * (i+1), _entryBytes);
|
||||
|
||||
boolean seen = _current.member(_extended);
|
||||
seen = seen || _previous.member(_extended);
|
||||
@ -155,15 +159,15 @@ public class DecayingBloomFilter {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
boolean seen = _current.locked_member(entry);
|
||||
seen = seen || _previous.locked_member(entry);
|
||||
boolean seen = _current.locked_member(entry, offset, len);
|
||||
seen = seen || _previous.locked_member(entry, offset, len);
|
||||
if (seen) {
|
||||
_currentDuplicates++;
|
||||
return true;
|
||||
} else {
|
||||
if (addIfNew) {
|
||||
_current.locked_insert(entry);
|
||||
_previous.locked_insert(entry);
|
||||
_current.locked_insert(entry, offset, len);
|
||||
_previous.locked_insert(entry, offset, len);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -103,6 +103,7 @@ public class NativeBigInteger extends BigInteger {
|
||||
private final static String JBIGI_OPTIMIZATION_PENTIUM2 = "pentium2";
|
||||
private final static String JBIGI_OPTIMIZATION_PENTIUM3 = "pentium3";
|
||||
private final static String JBIGI_OPTIMIZATION_PENTIUM4 = "pentium4";
|
||||
private final static String JBIGI_OPTIMIZATION_VIAC3 = "viac3";
|
||||
|
||||
private static final boolean _isWin = System.getProperty("os.name").startsWith("Win");
|
||||
private static final boolean _isOS2 = System.getProperty("os.name").startsWith("OS/2");
|
||||
@ -134,6 +135,8 @@ public class NativeBigInteger extends BigInteger {
|
||||
|
||||
try {
|
||||
CPUInfo c = CPUID.getInfo();
|
||||
if (c.IsC3Compatible())
|
||||
return JBIGI_OPTIMIZATION_VIAC3;
|
||||
if (c instanceof AMDCPUInfo) {
|
||||
AMDCPUInfo amdcpu = (AMDCPUInfo) c;
|
||||
if (amdcpu.IsAthlon64Compatible())
|
||||
@ -146,20 +149,18 @@ public class NativeBigInteger extends BigInteger {
|
||||
return JBIGI_OPTIMIZATION_K6_2;
|
||||
if (amdcpu.IsK6Compatible())
|
||||
return JBIGI_OPTIMIZATION_K6;
|
||||
} else {
|
||||
if (c instanceof IntelCPUInfo) {
|
||||
IntelCPUInfo intelcpu = (IntelCPUInfo) c;
|
||||
if (intelcpu.IsPentium4Compatible())
|
||||
return JBIGI_OPTIMIZATION_PENTIUM4;
|
||||
if (intelcpu.IsPentium3Compatible())
|
||||
return JBIGI_OPTIMIZATION_PENTIUM3;
|
||||
if (intelcpu.IsPentium2Compatible())
|
||||
return JBIGI_OPTIMIZATION_PENTIUM2;
|
||||
if (intelcpu.IsPentiumMMXCompatible())
|
||||
return JBIGI_OPTIMIZATION_PENTIUMMMX;
|
||||
if (intelcpu.IsPentiumCompatible())
|
||||
return JBIGI_OPTIMIZATION_PENTIUM;
|
||||
}
|
||||
} else if (c instanceof IntelCPUInfo) {
|
||||
IntelCPUInfo intelcpu = (IntelCPUInfo) c;
|
||||
if (intelcpu.IsPentium4Compatible())
|
||||
return JBIGI_OPTIMIZATION_PENTIUM4;
|
||||
if (intelcpu.IsPentium3Compatible())
|
||||
return JBIGI_OPTIMIZATION_PENTIUM3;
|
||||
if (intelcpu.IsPentium2Compatible())
|
||||
return JBIGI_OPTIMIZATION_PENTIUM2;
|
||||
if (intelcpu.IsPentiumMMXCompatible())
|
||||
return JBIGI_OPTIMIZATION_PENTIUMMMX;
|
||||
if (intelcpu.IsPentiumCompatible())
|
||||
return JBIGI_OPTIMIZATION_PENTIUM;
|
||||
}
|
||||
return null;
|
||||
} catch (UnknownCPUException e) {
|
||||
@ -287,7 +288,7 @@ public class NativeBigInteger extends BigInteger {
|
||||
|
||||
int runsProcessed = 0;
|
||||
for (runsProcessed = 0; runsProcessed < numRuns; runsProcessed++) {
|
||||
BigInteger bi = new BigInteger(2048, rand);
|
||||
BigInteger bi = new BigInteger(226, rand); // 2048, rand); //
|
||||
NativeBigInteger g = new NativeBigInteger(_sampleGenerator);
|
||||
NativeBigInteger p = new NativeBigInteger(_samplePrime);
|
||||
NativeBigInteger k = new NativeBigInteger(1, bi.toByteArray());
|
||||
|
@ -148,14 +148,16 @@ public class BloomSHA1 {
|
||||
*
|
||||
* @param b byte array representing a key (SHA1 digest)
|
||||
*/
|
||||
public void insert (byte[]b) {
|
||||
public void insert (byte[]b) { insert(b, 0, b.length); }
|
||||
public void insert (byte[]b, int offset, int len) {
|
||||
synchronized(this) {
|
||||
locked_insert(b);
|
||||
}
|
||||
}
|
||||
|
||||
public final void locked_insert(byte[]b) {
|
||||
ks.getOffsets(b);
|
||||
public final void locked_insert(byte[]b) { locked_insert(b, 0, b.length); }
|
||||
public final void locked_insert(byte[]b, int offset, int len) {
|
||||
ks.getOffsets(b, offset, len);
|
||||
for (int i = 0; i < k; i++) {
|
||||
filter[wordOffset[i]] |= 1 << bitOffset[i];
|
||||
}
|
||||
@ -168,8 +170,9 @@ public class BloomSHA1 {
|
||||
* @param b byte array representing a key (SHA1 digest)
|
||||
* @return true if b is in the filter
|
||||
*/
|
||||
protected final boolean isMember(byte[] b) {
|
||||
ks.getOffsets(b);
|
||||
protected final boolean isMember(byte[] b) { return isMember(b, 0, b.length); }
|
||||
protected final boolean isMember(byte[] b, int offset, int len) {
|
||||
ks.getOffsets(b, offset, len);
|
||||
for (int i = 0; i < k; i++) {
|
||||
if (! ((filter[wordOffset[i]] & (1 << bitOffset[i])) != 0) ) {
|
||||
return false;
|
||||
@ -179,6 +182,7 @@ public class BloomSHA1 {
|
||||
}
|
||||
|
||||
public final boolean locked_member(byte[]b) { return isMember(b); }
|
||||
public final boolean locked_member(byte[]b, int offset, int len) { return isMember(b, offset, len); }
|
||||
|
||||
/**
|
||||
* Is a key in the filter. External interface, internally synchronized.
|
||||
@ -186,9 +190,10 @@ public class BloomSHA1 {
|
||||
* @param b byte array representing a key (SHA1 digest)
|
||||
* @return true if b is in the filter
|
||||
*/
|
||||
public final boolean member(byte[]b) {
|
||||
public final boolean member(byte[]b) { return member(b, 0, b.length); }
|
||||
public final boolean member(byte[]b, int offset, int len) {
|
||||
synchronized (this) {
|
||||
return isMember(b);
|
||||
return isMember(b, offset, len);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,8 @@ public class KeySelector {
|
||||
private int m;
|
||||
private int k;
|
||||
private byte[] b;
|
||||
private int offset; // index into b to select
|
||||
private int length; // length into b to select
|
||||
private int[] bitOffset;
|
||||
private int[] wordOffset;
|
||||
private BitSelector bitSel;
|
||||
@ -70,7 +72,7 @@ public class KeySelector {
|
||||
public class GenericBitSelector implements BitSelector {
|
||||
/** Do the extraction */
|
||||
public void getBitSelectors() {
|
||||
int curBit = 0;
|
||||
int curBit = 8 * offset;
|
||||
int curByte;
|
||||
for (int j = 0; j < k; j++) {
|
||||
curByte = curBit / 8;
|
||||
@ -126,7 +128,7 @@ public class KeySelector {
|
||||
public void getWordSelectors() {
|
||||
int stride = m - 5;
|
||||
//assert true: stride<16;
|
||||
int curBit = k * 5;
|
||||
int curBit = (k * 5) + (offset * 8);
|
||||
int curByte;
|
||||
for (int j = 0; j < k; j++) {
|
||||
curByte = curBit / 8;
|
||||
@ -216,15 +218,18 @@ public class KeySelector {
|
||||
*
|
||||
* @param key cryptographic key used in populating the arrays
|
||||
*/
|
||||
public void getOffsets (byte[] key) {
|
||||
public void getOffsets (byte[] key) { getOffsets(key, 0, key.length); }
|
||||
public void getOffsets (byte[] key, int off, int len) {
|
||||
if (key == null) {
|
||||
throw new IllegalArgumentException("null key");
|
||||
}
|
||||
if (key.length < 20) {
|
||||
if (len < 20) {
|
||||
throw new IllegalArgumentException(
|
||||
"key must be at least 20 bytes long");
|
||||
}
|
||||
b = key;
|
||||
offset = off;
|
||||
length = len;
|
||||
// // DEBUG
|
||||
// System.out.println("KeySelector.getOffsets for "
|
||||
// + BloomSHA1.keyToString(b));
|
||||
|
Reference in New Issue
Block a user