@Override public InputStream getInputStreamUnbuffered() throws IOException { InputStream is = underlying.getInputStreamUnbuffered(); return AEADInputStream.createAES(is, key); }
@Override public int read() throws IOException { int length = read(onebyte); if(length <= 0) return -1; else return onebyte[0]; }
@Override public void close() throws IOException { if(!finished) // Must read the rest of the data to check hash integrity. skip(Long.MAX_VALUE); in.close(); }
/** Check whether we can close the stream early. * @throws IOException */ public void testCloseEarly() throws IOException { ArrayBucket input = new ArrayBucket(); BucketTools.fill(input, 2048); int keysize = 16; Random random = new Random(0x47f6709f); byte[] key = new byte[keysize]; random.nextBytes(key); Bucket output = new ArrayBucket(); OutputStream os = output.getOutputStream(); AEADOutputStream cos = AEADOutputStream.innerCreateAES(os, key, random); BucketTools.copyTo(input, cos, 2048); cos.close(); InputStream is = output.getInputStream(); AEADInputStream cis = AEADInputStream.createAES(is, key); byte[] first1KReadEncrypted = new byte[1024]; new DataInputStream(cis).readFully(first1KReadEncrypted); byte[] first1KReadOriginal = new byte[1024]; new DataInputStream(input.getInputStream()).readFully(first1KReadOriginal); assertTrue(Arrays.equals(first1KReadEncrypted, first1KReadOriginal)); cis.close(); }
public static AEADInputStream createAES(InputStream is, byte[] key) throws IOException { AESEngine mainCipher = new AESEngine(); AESLightEngine hashCipher = new AESLightEngine(); return new AEADInputStream(is, key, hashCipher, mainCipher); }
os.close(); InputStream is = output.getInputStream(); AEADInputStream cis = AEADInputStream.createAES(is, key); byte[] first1KReadEncrypted = new byte[1024]; new DataInputStream(cis).readFully(first1KReadEncrypted); assertTrue(Arrays.equals(first1KReadEncrypted, first1KReadOriginal)); try { cis.close(); fail("Hash should be bogus due to garbage data at end"); } catch (AEADVerificationFailedException e) {
public void checkFailedCorruptedRoundTrip(int keysize, Random random, Bucket input, Bucket output, Bucket decoded) throws IOException { byte[] key = new byte[keysize]; random.nextBytes(key); OutputStream os = output.getOutputStream(); CorruptingOutputStream kos = new CorruptingOutputStream(os, 16L, input.size() + 16, 10, random); AEADOutputStream cos = AEADOutputStream.innerCreateAES(kos, key, random); BucketTools.copyTo(input, cos, -1); cos.close(); assertTrue(output.size() > input.size()); InputStream is = output.getInputStream(); AEADInputStream cis = AEADInputStream.createAES(is, key); try { BucketTools.copyFrom(decoded, cis, -1); cis.close(); fail("Checksum error should have been seen"); } catch (AEADVerificationFailedException e) { // Expected. } assertEquals(decoded.size(), input.size()); assertFalse(BucketTools.equalBuckets(decoded, input)); }
public void checkSuccessfulRoundTrip(int keysize, Random random, Bucket input, Bucket output, Bucket decoded) throws IOException { byte[] key = new byte[keysize]; random.nextBytes(key); OutputStream os = output.getOutputStream(); AEADOutputStream cos = AEADOutputStream.innerCreateAES(os, key, random); BucketTools.copyTo(input, cos, -1); cos.close(); assertTrue(output.size() > input.size()); InputStream is = output.getInputStream(); AEADInputStream cis = AEADInputStream.createAES(is, key); BucketTools.copyFrom(decoded, cis, -1); assertEquals(decoded.size(), input.size()); assertTrue(BucketTools.equalBuckets(decoded, input)); }
@Override public int read(byte[] buf) throws IOException { return read(buf, 0, buf.length); }
public void checkSuccessfulRoundTripRandomSplits(int keysize, Random random, Bucket input, Bucket output, Bucket decoded) throws IOException { byte[] key = new byte[keysize]; random.nextBytes(key); OutputStream os = output.getOutputStream(); AEADOutputStream cos = AEADOutputStream.innerCreateAES(os, key, random); BucketTools.copyTo(input, new RandomShortWriteOutputStream(cos, random), -1); cos.close(); assertTrue(output.size() > input.size()); InputStream is = output.getInputStream(); AEADInputStream cis = AEADInputStream.createAES(is, key); BucketTools.copyFrom(decoded, new RandomShortReadInputStream(cis, random), -1); assertEquals(decoded.size(), input.size()); assertTrue(BucketTools.equalBuckets(decoded, input)); }
return read(buf, offset, length); else return -1;