Class MacReuseTest


  • public class MacReuseTest
    extends java.lang.Object
    Checks that a Mac is usable again after doFinal() without being re-initialized.

    javax.crypto.Mac specifies that doFinal() leaves the object reset and ready for another message with the same key. Nothing exercised that: every MAC in the rest of the suite is computed by a freshly initialized object, so the reset the backend performs between messages has its effect discarded every time and a reset that keeps the buffered bytes, or leaves the context unusable, is not caught.

    The expected values are the published RFC 2202 and RFC 4231 vectors, so a wrong result is wrong against the standard rather than against a previous run.

    Mac.reset() is deliberately never called here. Reuse after doFinal() is the path the specification promises and the one an application takes; calling reset() explicitly would suggest the backend reset is being driven directly, which it is not.

    • Constructor Detail

      • MacReuseTest

        public MacReuseTest()
    • Method Detail

      • testSameMessageTwiceWithoutReinit

        public void testSameMessageTwiceWithoutReinit()
                                               throws java.lang.Exception
        The same message twice through one object gives the same MAC twice.

        A reset that leaves the previous message buffered gives a different second value.

        Throws:
        java.lang.Exception
      • testDifferentSecondMessageWithoutReinit

        public void testDifferentSecondMessageWithoutReinit()
                                                     throws java.lang.Exception
        A different second message through the same object gives that message's MAC.

        Repeating one message cannot tell a correct reset from one that reproduces the previous state; a second message can. The key is longer than the block size, so the object also has to keep the hashed key across doFinal().

        Throws:
        java.lang.Exception
      • testReusedMacFedInTwoParts

        public void testReusedMacFedInTwoParts()
                                        throws java.lang.Exception
        Feeding the reused object through update() before doFinal() gives the same MAC as one call.

        This is the ordering that fails when the reset happens on the first update() after doFinal() rather than at doFinal() itself: the first fragment is discarded and the MAC covers only the rest.

        Throws:
        java.lang.Exception