internal static void TestTampering(ITest test, IAeadCipher cipher, ICipherParameters parameters) { byte[] plaintext = new byte[1000]; for (int i = 0; i < plaintext.Length; i++) { plaintext[i] = (byte)i; } cipher.Init(true, parameters); byte[] ciphertext = new byte[cipher.GetOutputSize(plaintext.Length)]; int len = cipher.ProcessBytes(plaintext, 0, plaintext.Length, ciphertext, 0); cipher.DoFinal(ciphertext, len); int macLength = cipher.GetMac().Length; // Test tampering with a single byte cipher.Init(false, parameters); byte[] tampered = new byte[ciphertext.Length]; byte[] output = new byte[plaintext.Length]; Array.Copy(ciphertext, 0, tampered, 0, tampered.Length); tampered[0] += 1; cipher.ProcessBytes(tampered, 0, tampered.Length, output, 0); try { cipher.DoFinal(output, 0); throw new TestFailedException( new SimpleTestResult(false, test + " : tampering of ciphertext not detected.")); } catch (InvalidCipherTextException e) { // Expected } // Test truncation of ciphertext to < tag length cipher.Init(false, parameters); byte[] truncated = new byte[macLength - 1]; Array.Copy(ciphertext, 0, truncated, 0, truncated.Length); cipher.ProcessBytes(truncated, 0, truncated.Length, output, 0); try { cipher.DoFinal(output, 0); Fail(test, "tampering of ciphertext not detected."); } catch (InvalidCipherTextException e) { // Expected } }
/** * return the size of the output buffer required for an update plus a * doFinal with an input of len bytes. * * @param len the length of the input. * @return the space required to accommodate a call to update and doFinal * with len bytes of input. */ public override int GetOutputSize( int length) { return(cipher.GetOutputSize(length)); }