Author: nextgens
Date: 2006-02-03 23:46:58 +0000 (Fri, 03 Feb 2006)
New Revision: 8002
Modified:
branches/freenet-freejvms/src/freenet/keys/CHKBlock.java
branches/freenet-freejvms/src/freenet/keys/NodeCHK.java
Log:
Build 429: Up and almost working with gcj : it throws NoSuchAlgorithmException
... but that is not a big deal
Modified: branches/freenet-freejvms/src/freenet/keys/CHKBlock.java
===================================================================
--- branches/freenet-freejvms/src/freenet/keys/CHKBlock.java 2006-02-03
23:24:42 UTC (rev 8001)
+++ branches/freenet-freejvms/src/freenet/keys/CHKBlock.java 2006-02-03
23:46:58 UTC (rev 8002)
@@ -3,16 +3,6 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
-import gnu.crypto.hash.Sha256;
-import gnu.crypto.Registry;
-import gnu.crypto.hash.HashFactory;
-import gnu.crypto.hash.IMessageDigest;
-import gnu.crypto.jce.GnuSecurity;
-import gnu.crypto.util.Util;
-import java.security.NoSuchProviderException;
-import java.security.NoSuchAlgorithmException;
-import gnu.crypto.Registry;
-
/**
* @author amphibian
*
@@ -65,10 +55,10 @@
// Check the hash
if(hashIdentifier != HASH_SHA256)
throw new CHKVerifyException("Hash not SHA-256");
- IMessageDigest md;
+ MessageDigest md;
try {
- md = HashFactory.getInstance("SHA-256");
- } catch (Exception e) {
+ md = MessageDigest.getInstance("SHA-256");
+ } catch (NoSuchAlgorithmException e) {
throw new Error(e);
}
@@ -82,108 +72,6 @@
// Otherwise it checks out
}
- /**
- * Decode into RAM, if short.
- * @throws CHKDecodeException
- */
- public byte[] memoryDecode(ClientCHK chk) throws CHKDecodeException {
- try {
- ArrayBucket a = (ArrayBucket) decode(chk, new
ArrayBucketFactory(), 32*1024);
- return BucketTools.toByteArray(a); // FIXME
- } catch (IOException e) {
- throw new Error(e);
- }
- }
-
- public Bucket decode(ClientKey key, BucketFactory bf, int maxLength)
throws KeyDecodeException, IOException {
- if(!(key instanceof ClientCHK))
- throw new KeyDecodeException("Not a CHK!: "+key);
- return decode((ClientCHK)key, bf, maxLength);
- }
-
- /**
- * Decode the CHK and recover the original data
- * @return the original data
- * @throws IOException If there is a bucket error.
- */
- public Bucket decode(ClientCHK key, BucketFactory bf, int maxLength)
throws CHKDecodeException, IOException {
- // Overall hash already verified, so first job is to decrypt.
- if(key.cryptoAlgorithm != ClientCHK.ALGO_AES_PCFB_256)
- throw new UnsupportedOperationException();
- BlockCipher cipher;
- try {
- cipher = new Rijndael(256, 256);
- } catch (UnsupportedCipherException e) {
- // FIXME - log this properly
- throw new Error(e);
- }
- byte[] cryptoKey = key.cryptoKey;
- if(cryptoKey.length < Node.SYMMETRIC_KEY_LENGTH)
- throw new CHKDecodeException("Crypto key too short");
- cipher.initialize(key.cryptoKey);
- PCFBMode pcfb = new PCFBMode(cipher);
- byte[] hbuf = new byte[header.length-2];
- System.arraycopy(header, 2, hbuf, 0, header.length-2);
- byte[] dbuf = new byte[data.length];
- System.arraycopy(data, 0, dbuf, 0, data.length);
- // Decipher header first - functions as IV
- pcfb.blockDecipher(hbuf, 0, hbuf.length);
- pcfb.blockDecipher(dbuf, 0, dbuf.length);
- // Check: Decryption key == hash of data (not including header)
- IMessageDigest md256;
- try {
- md256 = HashFactory.getInstance("SHA-256");
- } catch (Exception e1) {
- // FIXME: log this properly?
- throw new Error(e1);
- }
- md256.reset();
- md256.update(dbuf);
- byte[] dkey = md256.digest();
- if(!java.util.Arrays.equals(dkey, key.cryptoKey)) {
- throw new CHKDecodeException("Check failed: decrypt key ==
H(data)");
- }
- // Check: IV == hash of decryption key
- md256.reset();
- md256.update(dkey);
- byte[] predIV = md256.digest();
- // Extract the IV
- byte[] iv = new byte[32];
- System.arraycopy(hbuf, 0, iv, 0, 32);
- if(!Arrays.equals(iv, predIV))
- throw new CHKDecodeException("Check failed: Decrypted IV ==
H(decryption key)");
- // Checks complete
- int size = ((hbuf[32] & 0xff) << 8) + (hbuf[33] & 0xff);
- if(size > 32768 || size < 0)
- throw new CHKDecodeException("Invalid size: "+size);
- byte[] output = new byte[size];
- // No particular reason to check the padding, is there?
- System.arraycopy(dbuf, 0, output, 0, size);
- return decompress(key, output, bf, maxLength);
- }
-
- private Bucket decompress(ClientCHK key, byte[] output, BucketFactory bf,
int maxLength) throws CHKDecodeException, IOException {
- if(key.isCompressed()) {
- Logger.minor(this, "Decompressing in decode: "+key.getURI()+"
with codec "+key.compressionAlgorithm);
- if(output.length < 5) throw new CHKDecodeException("No bytes to
decompress");
- // Decompress
- // First get the length
- int len = ((((((output[0] & 0xff) << 8) + (output[1] & 0xff)) <<
8) + (output[2] & 0xff)) << 8) +
- (output[3] & 0xff);
- if(len > MAX_LENGTH_BEFORE_COMPRESSION)
- throw new CHKDecodeException("Invalid precompressed size:
"+len);
- Compressor decompressor =
Compressor.getCompressionAlgorithmByMetadataID(key.compressionAlgorithm);
- Bucket inputBucket = new SimpleReadOnlyArrayBucket(output, 4,
output.length-4);
- try {
- return decompressor.decompress(inputBucket, bf,
maxLength);
- } catch (CompressionOutputSizeException e) {
- throw new CHKDecodeException("Too big");
- }
- } else {
- return BucketTools.makeImmutableBucket(bf, output);
- }
- }
-
public Key getKey() {
return chk;
}
Modified: branches/freenet-freejvms/src/freenet/keys/NodeCHK.java
===================================================================
--- branches/freenet-freejvms/src/freenet/keys/NodeCHK.java 2006-02-03
23:24:42 UTC (rev 8001)
+++ branches/freenet-freejvms/src/freenet/keys/NodeCHK.java 2006-02-03
23:46:58 UTC (rev 8002)
@@ -7,15 +7,6 @@
import freenet.support.Base64;
-import gnu.crypto.hash.Sha256;
-import gnu.crypto.Registry;
-import gnu.crypto.hash.HashFactory;
-import gnu.crypto.hash.IMessageDigest;
-import gnu.crypto.jce.GnuSecurity;
-import gnu.crypto.util.Util;
-import java.security.NoSuchProviderException;
-import java.security.NoSuchAlgorithmException;
-import gnu.crypto.Registry;
/**
* @author amphibian
*
@@ -67,29 +58,6 @@
return false;
}
- /**
- * @return The key, hashed, converted to a double in the range
- * 0.0 to 1.0.
- */
- public synchronized double toNormalizedDouble() {
- if(cachedNormalizedDouble > 0) return cachedNormalizedDouble;
- IMessageDigest md;
- try {
- md = HashFactory.getInstance("SHA-256");
- } catch (Exception e) {
- throw new Error(e);
- }
- md.update(routingKey);
- md.update((byte)(TYPE >> 8));
- md.update((byte)TYPE);
- byte[] digest = md.digest();
- long asLong = Math.abs(Fields.bytesToLong(digest));
- // Math.abs can actually return negative...
- if(asLong == Long.MIN_VALUE)
- asLong = Long.MAX_VALUE;
- cachedNormalizedDouble = ((double)asLong)/((double)Long.MAX_VALUE);
- return cachedNormalizedDouble;
- }
public short getType() {
return TYPE;
}