uio-bmi / crypt4gh

Crypt4GH standard implementation
https://uio-bmi.github.io/crypt4gh/
MIT License
2 stars 6 forks source link

Edit list not applied according to specification #89

Open lyytinen opened 1 year ago

lyytinen commented 1 year ago

Describe the bug

Section 4.2 of the specification outlines how the edit list is applied. In the example, it says that the last keep value "[...] could actually be left out as it extends all the way to the end of the file." While testing my PR #88, I noticed that the last keep value is not optional in this implementation. Is this a potential interoperability issue?

To Reproduce

Encrypt value "1234" with edit list [3].

Expected behavior

Expecting first 3 bytes to be discarded. Decrypted result should therefore be "4".

Actual behavior

All data is discarded resulting to an empty result. Changing the edit list to [3,1] produces the expected result "4".

Sample code

    @Test
    public void testEditListImplementation() throws Exception {
        PrivateKey writerPrivateKey = keyUtils.generatePrivateKey();
        KeyPair readerKeyPair = keyUtils.generateKeyPair();
        PrivateKey readerPrivateKey = readerKeyPair.getPrivate();
        PublicKey readerPublicKey = readerKeyPair.getPublic();

        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
            try (Crypt4GHOutputStream crypt4GHOutputStream = new Crypt4GHOutputStream(byteArrayOutputStream, new DataEditList(new long[]{ 3 }), writerPrivateKey, readerPublicKey)) {
                crypt4GHOutputStream.write("1234".getBytes());
            }
            try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
                 Crypt4GHInputStream crypt4GHInputStream = new Crypt4GHInputStream(byteArrayInputStream, readerPrivateKey)) {

                Assert.assertArrayEquals("4".getBytes(), crypt4GHInputStream.readAllBytes()); // Fails!
            }
        }
    }