Class CipherAssertions


  • public final class CipherAssertions
    extends java.lang.Object
    Assertions on Cipher shared by the cipher test classes.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertIvRoundTrips​(java.lang.String transformation, java.lang.String keyAlgorithm, byte[] key, byte[] plaintext, int expectedIvLength)
      Asserts that a cipher initialized without parameters generates an initialization vector, returns it from Cipher.getIV() with the expected length, and that decrypting with that vector gives the plaintext back.
      static void assertReinitMatchesFreshCipher​(java.lang.String transformation, java.lang.String keyAlgorithm, byte[] firstKey, java.security.spec.AlgorithmParameterSpec firstParameters, byte[] secondKey, java.security.spec.AlgorithmParameterSpec secondParameters, byte[] plaintext)
      Asserts that a cipher initialized, fed one block, then initialized again with another key and other parameters encrypts as a freshly created cipher does, and that the result decrypts.
      static void assertRoundTripAtOffsets​(java.lang.String label, javax.crypto.Cipher encryptor, javax.crypto.Cipher decryptor, byte[] plaintext, byte[] ciphertextAtZero)
      Asserts that encrypting from a sub-range into an offset produces the given ciphertext, that decrypting it back from a sub-range into an offset gives the plaintext, and that neither operation touches the bytes around its result.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • assertIvRoundTrips

        public static void assertIvRoundTrips​(java.lang.String transformation,
                                              java.lang.String keyAlgorithm,
                                              byte[] key,
                                              byte[] plaintext,
                                              int expectedIvLength)
                                       throws java.security.GeneralSecurityException
        Asserts that a cipher initialized without parameters generates an initialization vector, returns it from Cipher.getIV() with the expected length, and that decrypting with that vector gives the plaintext back.

        The round trip is what shows the value returned is the value actually used.

        Parameters:
        transformation - the transformation, with a mode that takes an initialization vector
        keyAlgorithm - the algorithm of the key
        key - the raw key
        plaintext - the plaintext, a whole number of blocks when the transformation has no padding
        expectedIvLength - the length in bytes of the initialization vector the transformation implies
        Throws:
        java.security.GeneralSecurityException - if the transformation or the key is rejected
      • assertRoundTripAtOffsets

        public static void assertRoundTripAtOffsets​(java.lang.String label,
                                                    javax.crypto.Cipher encryptor,
                                                    javax.crypto.Cipher decryptor,
                                                    byte[] plaintext,
                                                    byte[] ciphertextAtZero)
                                             throws java.security.GeneralSecurityException
        Asserts that encrypting from a sub-range into an offset produces the given ciphertext, that decrypting it back from a sub-range into an offset gives the plaintext, and that neither operation touches the bytes around its result.

        The expected ciphertext is the one of the same operation at offset zero, so the assertion holds for every mode without a written-out vector per transformation. Each output is sized from Cipher.getOutputSize(int): a backend may require more room than it writes and reject a smaller array before it writes anything. The simulator mock does not, so a size taken from the result is only caught on a target.

        Parameters:
        label - the transformation, for the assertion messages
        encryptor - an initialized encrypting cipher, with its additional authenticated data already supplied when it takes some
        decryptor - an initialized decrypting cipher, with the same additional authenticated data
        plaintext - the plaintext, a whole number of blocks when the transformation has no padding
        ciphertextAtZero - the ciphertext of the same operation at offset zero
        Throws:
        java.security.GeneralSecurityException - if an operation is rejected
      • assertReinitMatchesFreshCipher

        public static void assertReinitMatchesFreshCipher​(java.lang.String transformation,
                                                          java.lang.String keyAlgorithm,
                                                          byte[] firstKey,
                                                          java.security.spec.AlgorithmParameterSpec firstParameters,
                                                          byte[] secondKey,
                                                          java.security.spec.AlgorithmParameterSpec secondParameters,
                                                          byte[] plaintext)
                                                   throws java.security.GeneralSecurityException
        Asserts that a cipher initialized, fed one block, then initialized again with another key and other parameters encrypts as a freshly created cipher does, and that the result decrypts.

        A second initialization is how an application reaches the backend's cipher-close path: the context of the first initialization has to be released before the second one is allocated. Releasing too late leaks, releasing too early leaves the second context using freed memory. The data fed to the first context is never finalized: the second initialization abandons a context that holds data, and additional authenticated data on an authenticated mode, which is the harder case for a backend to release. A second context built on the remains of the first fails the comparison with the fresh cipher.

        Parameters:
        transformation - the transformation
        keyAlgorithm - the algorithm of the keys
        firstKey - the raw key of the first initialization
        firstParameters - the parameters of the first initialization
        secondKey - the raw key of the second initialization
        secondParameters - the parameters of the second initialization, which must differ from the first ones on an authenticated mode
        plaintext - the plaintext, a whole number of blocks when the transformation has no padding
        Throws:
        java.security.GeneralSecurityException - if the transformation, a key or a parameter is rejected