bcgit / bc-java

Bouncy Castle Java Distribution (Mirror)
https://www.bouncycastle.org/java.html
MIT License
2.33k stars 1.14k forks source link

PhotonBeetle and Xoodyak digest do not reset properly on doFinal() #1839

Closed tonywasher closed 1 month ago

tonywasher commented 2 months ago

If the PhotonBeetle and Xoodyak digests are re-used after the call to doFinal(), they return differing results for the same input data.

Please see following test code

` public class DigestTest { private static final int DATALEN = 100;

public static void main(final String[] pArgs) 
{
    checkDigest(new PhotonBeetleDigest());
    checkDigest(new XoodyakDigest());
}

private static void checkDigest(final Digest pDigest) 
{
    /* Obtain some random data */
    final byte[] myData = new byte[DATALEN];
    final SecureRandom myRandom = new SecureRandom();
    myRandom.nextBytes(myData);

    /* Update and finalise digest */
    final int myHashLen = pDigest.getDigestSize();
    final byte[] myFirst = new byte[myHashLen];
    pDigest.update(myData, 0, DATALEN);
    pDigest.doFinal(myFirst, 0);

    /* Reuse the digest */
    final byte[] mySecond = new byte[myHashLen];
    pDigest.update(myData, 0, DATALEN);
    pDigest.doFinal(mySecond, 0);

    /* Check that we have the same result */
    if (!Arrays.equals(myFirst, mySecond)) 
    {
        System.out.println("Digest " + pDigest.getAlgorithmName() + " does not reset properly on doFinal()");
    }
}

} `

ligefeiBouncycastle commented 1 month ago

Fix the issue in https://github.com/bcgit/bc-java/commit/a933b7ed3447641d67d72a6eaa2f47dbd5c5bc98