Class CipherAssertions
- java.lang.Object
-
- com.microej.crypto.test.util.CipherAssertions
-
public final class CipherAssertions extends java.lang.ObjectAssertions onCiphershared by the cipher test classes.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidassertIvRoundTrips(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 fromCipher.getIV()with the expected length, and that decrypting with that vector gives the plaintext back.static voidassertReinitMatchesFreshCipher(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 voidassertRoundTripAtOffsets(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.
-
-
-
Method Detail
-
assertIvRoundTrips
public static void assertIvRoundTrips(java.lang.String transformation, java.lang.String keyAlgorithm, byte[] key, byte[] plaintext, int expectedIvLength) throws java.security.GeneralSecurityExceptionAsserts that a cipher initialized without parameters generates an initialization vector, returns it fromCipher.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 vectorkeyAlgorithm- the algorithm of the keykey- the raw keyplaintext- the plaintext, a whole number of blocks when the transformation has no paddingexpectedIvLength- 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.GeneralSecurityExceptionAsserts 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 messagesencryptor- an initialized encrypting cipher, with its additional authenticated data already supplied when it takes somedecryptor- an initialized decrypting cipher, with the same additional authenticated dataplaintext- the plaintext, a whole number of blocks when the transformation has no paddingciphertextAtZero- 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.GeneralSecurityExceptionAsserts 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 transformationkeyAlgorithm- the algorithm of the keysfirstKey- the raw key of the first initializationfirstParameters- the parameters of the first initializationsecondKey- the raw key of the second initializationsecondParameters- the parameters of the second initialization, which must differ from the first ones on an authenticated modeplaintext- 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
-
-