diff --git a/components/net/sf/briar/crypto/CryptoModule.java b/components/net/sf/briar/crypto/CryptoModule.java index 64efb11c12bb1062621dde37cd4d238f79db353d..221c0bb1307aef949f8fbe206b6ac2d842a7d21a 100644 --- a/components/net/sf/briar/crypto/CryptoModule.java +++ b/components/net/sf/briar/crypto/CryptoModule.java @@ -15,8 +15,9 @@ public class CryptoModule extends AbstractModule { protected void configure() { bind(CryptoComponent.class).to( CryptoComponentImpl.class).in(Singleton.class); + // FIXME: Use a real key bind(SecretKey.class).annotatedWith(SecretStorageKey.class).toInstance( - new SecretKeySpec(new byte[32], "AES")); // FIXME + new SecretKeySpec(new byte[32], "AES")); } } diff --git a/components/net/sf/briar/protocol/SigningConsumer.java b/components/net/sf/briar/protocol/SigningConsumer.java deleted file mode 100644 index 3b8ee7edc98cd8fd643a94ffd1e1fe20e8af7aeb..0000000000000000000000000000000000000000 --- a/components/net/sf/briar/protocol/SigningConsumer.java +++ /dev/null @@ -1,43 +0,0 @@ -package net.sf.briar.protocol; - -import java.io.IOException; -import java.security.Signature; -import java.security.SignatureException; - -import net.sf.briar.api.serial.Consumer; - -// FIXME: This class is no longer used - remove it? - -/** A consumer that passes its input through a signature. */ -class SigningConsumer implements Consumer { - - private final Signature signature; - - SigningConsumer(Signature signature) { - this.signature = signature; - } - - public void write(byte b) throws IOException { - try { - signature.update(b); - } catch(SignatureException e) { - throw new IOException(e.getMessage()); - } - } - - public void write(byte[] b) throws IOException { - try { - signature.update(b); - } catch(SignatureException e) { - throw new IOException(e.getMessage()); - } - } - - public void write(byte[] b, int off, int len) throws IOException { - try { - signature.update(b, off, len); - } catch(SignatureException e) { - throw new IOException(e.getMessage()); - } - } -} diff --git a/components/net/sf/briar/protocol/SigningDigestingOutputStream.java b/components/net/sf/briar/protocol/SigningDigestingOutputStream.java deleted file mode 100644 index 5709a523891805a903ad5ad7d25fcee49d10f3e5..0000000000000000000000000000000000000000 --- a/components/net/sf/briar/protocol/SigningDigestingOutputStream.java +++ /dev/null @@ -1,67 +0,0 @@ -package net.sf.briar.protocol; - -import java.io.FilterOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.security.MessageDigest; -import java.security.Signature; -import java.security.SignatureException; - -// FIXME: This class is no longer used - remove it? - -/** - * An output stream that passes its output through a signature and a message - * digest. - */ -class SigningDigestingOutputStream extends FilterOutputStream { - - private final Signature signature; - private final MessageDigest messageDigest; - private boolean signing = false, digesting = false; - - public SigningDigestingOutputStream(OutputStream out, Signature signature, - MessageDigest messageDigest) { - super(out); - this.signature = signature; - this.messageDigest = messageDigest; - } - - void setSigning(boolean signing) { - this.signing = signing; - } - - void setDigesting(boolean digesting) { - this.digesting = digesting; - } - - @Override - public void write(byte[] b) throws IOException { - write(b, 0, b.length); - } - - @Override - public void write(byte[] b, int off, int len) throws IOException { - out.write(b, off, len); - if(signing) { - try { - signature.update(b, off, len); - } catch(SignatureException e) { - throw new IOException(e.getMessage()); - } - } - if(digesting) messageDigest.update(b, off, len); - } - - @Override - public void write(int b) throws IOException { - out.write(b); - if(signing) { - try { - signature.update((byte) b); - } catch(SignatureException e) { - throw new IOException(e.getMessage()); - } - } - if(digesting) messageDigest.update((byte) b); - } -} diff --git a/test/net/sf/briar/protocol/ConsumersTest.java b/test/net/sf/briar/protocol/ConsumersTest.java index cb03476b616886a23c6e0924eb978d6089b8efab..49590336e907d2848c83b932c9fc8d032f919b7b 100644 --- a/test/net/sf/briar/protocol/ConsumersTest.java +++ b/test/net/sf/briar/protocol/ConsumersTest.java @@ -1,8 +1,6 @@ package net.sf.briar.protocol; -import java.security.KeyPair; import java.security.MessageDigest; -import java.security.Signature; import java.util.Arrays; import java.util.Random; @@ -27,25 +25,6 @@ public class ConsumersTest extends TestCase { crypto = i.getInstance(CryptoComponent.class); } - @Test - public void testSigningConsumer() throws Exception { - Signature signature = crypto.getSignature(); - KeyPair keyPair = crypto.generateKeyPair(); - byte[] data = new byte[1234]; - // Generate some random data and sign it - new Random().nextBytes(data); - signature.initSign(keyPair.getPrivate()); - signature.update(data); - byte[] sig = signature.sign(); - // Check that a SigningConsumer fed with the same data verifies the sig - signature.initVerify(keyPair.getPublic()); - SigningConsumer sc = new SigningConsumer(signature); - sc.write(data[0]); - sc.write(data, 1, data.length - 2); - sc.write(data[data.length - 1]); - assertTrue(signature.verify(sig)); - } - @Test public void testDigestingConsumer() throws Exception { MessageDigest messageDigest = crypto.getMessageDigest(); diff --git a/test/net/sf/briar/protocol/SigningDigestingOutputStreamTest.java b/test/net/sf/briar/protocol/SigningDigestingOutputStreamTest.java deleted file mode 100644 index 678cc1cf32a241bdd70104df9f13c42090f92da4..0000000000000000000000000000000000000000 --- a/test/net/sf/briar/protocol/SigningDigestingOutputStreamTest.java +++ /dev/null @@ -1,82 +0,0 @@ -package net.sf.briar.protocol; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.security.KeyPair; -import java.security.MessageDigest; -import java.security.Signature; -import java.util.Arrays; -import java.util.Random; - -import junit.framework.TestCase; -import net.sf.briar.api.crypto.CryptoComponent; -import net.sf.briar.crypto.CryptoModule; - -import org.junit.Before; -import org.junit.Test; - -import com.google.inject.Guice; -import com.google.inject.Injector; - -public class SigningDigestingOutputStreamTest extends TestCase { - - private CryptoComponent crypto = null; - - @Before - public void setUp() throws Exception { - Injector i = Guice.createInjector(new CryptoModule()); - crypto = i.getInstance(CryptoComponent.class); - } - - @Test - public void testStopAndStart() throws Exception { - Signature signature = crypto.getSignature(); - KeyPair keyPair = crypto.generateKeyPair(); - MessageDigest messageDigest = crypto.getMessageDigest(); - byte[] input = new byte[1024]; - new Random().nextBytes(input); - ByteArrayOutputStream out = new ByteArrayOutputStream(input.length); - SigningDigestingOutputStream s = - new SigningDigestingOutputStream(out, signature, messageDigest); - signature.initSign(keyPair.getPrivate()); - messageDigest.reset(); - // Sign the first 256 bytes, digest all but the last 256 bytes - s.setDigesting(true); - s.setSigning(true); - s.write(input, 0, 256); - s.setSigning(false); - s.write(input, 256, 512); - s.setDigesting(false); - s.write(input, 768, 256); - s.close(); - // Get the signature and the digest - byte[] sig = signature.sign(); - byte[] digest = messageDigest.digest(); - // Check that the output matches the input - assertTrue(Arrays.equals(input, out.toByteArray())); - // Verify the signature over the first 256 bytes - signature.initVerify(keyPair.getPublic()); - signature.update(input, 0, 256); - assertTrue(signature.verify(sig)); - // Check that the digest matches a digest over all but the last 256 - // bytes - messageDigest.reset(); - messageDigest.update(input, 0, 768); - byte[] directDigest = messageDigest.digest(); - assertTrue(Arrays.equals(directDigest, digest)); - } - - @Test - public void testSignatureExceptionThrowsIOException() throws Exception { - Signature signature = crypto.getSignature(); - MessageDigest messageDigest = crypto.getMessageDigest(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - SigningDigestingOutputStream s = - new SigningDigestingOutputStream(out, signature, messageDigest); - s.setSigning(true); // Signature hasn't been initialised yet - try { - s.write((byte) 0); - fail(); - } catch(IOException expected) {}; - } -}