summary refs log tree commit diff
path: root/crypto/test
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-04-27 15:07:36 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-04-27 15:07:36 +0700
commit31da29d6869989dd0b049596df77c0c8aef37e8f (patch)
treeb84ab2b2804ecad48be4c592c87e3abf836082d5 /crypto/test
parentAdd default case to switch statements (diff)
downloadBouncyCastle.NET-ed25519-31da29d6869989dd0b049596df77c0c8aef37e8f.tar.xz
Fix Ascon decryption buffering bug
- add test coverage for all buffer splits
Diffstat (limited to 'crypto/test')
-rw-r--r--crypto/test/src/crypto/test/AsconTest.cs70
1 files changed, 70 insertions, 0 deletions
diff --git a/crypto/test/src/crypto/test/AsconTest.cs b/crypto/test/src/crypto/test/AsconTest.cs
index eabf7e043..400767df0 100644
--- a/crypto/test/src/crypto/test/AsconTest.cs
+++ b/crypto/test/src/crypto/test/AsconTest.cs
@@ -95,6 +95,24 @@ namespace Org.BouncyCastle.Crypto.Tests
         }
 
         [Test]
+        public void TestBufferingEngine_ascon128()
+        {
+            ImplTestBufferingEngine(AsconEngine.AsconParameters.ascon128);
+        }
+
+        [Test]
+        public void TestBufferingEngine_ascon128a()
+        {
+            ImplTestBufferingEngine(AsconEngine.AsconParameters.ascon128a);
+        }
+
+        [Test]
+        public void TestBufferingEngine_ascon80()
+        {
+            ImplTestBufferingEngine(AsconEngine.AsconParameters.ascon80pq);
+        }
+
+        [Test]
         public void TestExceptionsDigest_AsconHash()
         {
             ImplTestExceptionsDigest(AsconDigest.AsconParameters.AsconHash);
@@ -327,6 +345,58 @@ namespace Org.BouncyCastle.Crypto.Tests
             }
         }
 
+        private static void ImplTestBufferingEngine(AsconEngine.AsconParameters asconParameters)
+        {
+            Random random = new Random();
+
+            int plaintextLength = 256;
+            byte[] plaintext = new byte[plaintextLength];
+            random.NextBytes(plaintext);
+
+            var ascon0 = CreateEngine(asconParameters);
+            InitEngine(ascon0, true);
+
+            byte[] ciphertext = new byte[ascon0.GetOutputSize(plaintextLength)];
+            random.NextBytes(ciphertext);
+
+            int ciphertextLength = ascon0.ProcessBytes(plaintext, 0, plaintextLength, ciphertext, 0);
+            ciphertextLength += ascon0.DoFinal(ciphertext, ciphertextLength);
+
+            byte[] output = new byte[ciphertextLength];
+
+            // Encryption
+            for (int split = 1; split < plaintextLength; ++split)
+            {
+                var ascon = CreateEngine(asconParameters);
+                InitEngine(ascon, true);
+
+                random.NextBytes(output);
+
+                int length = ascon.ProcessBytes(plaintext, 0, split, output, 0);
+                length += ascon.ProcessBytes(plaintext, split, plaintextLength - split, output, length);
+                length += ascon.DoFinal(output, length);
+
+                Assert.IsTrue(Arrays.AreEqual(ciphertext, 0, ciphertextLength, output, 0, length),
+                    "encryption failed with split: " + split);
+            }
+
+            // Decryption
+            for (int split = 1; split < ciphertextLength; ++split)
+            {
+                var ascon = CreateEngine(asconParameters);
+                InitEngine(ascon, false);
+
+                random.NextBytes(output);
+
+                int length = ascon.ProcessBytes(ciphertext, 0, split, output, 0);
+                length += ascon.ProcessBytes(ciphertext, split, ciphertextLength - split, output, length);
+                length += ascon.DoFinal(output, length);
+
+                Assert.IsTrue(Arrays.AreEqual(plaintext, 0, plaintextLength, output, 0, length),
+                    "decryption failed with split: " + split);
+            }
+        }
+
         private static void ImplTestExceptionsDigest(AsconDigest.AsconParameters asconParameters)
         {
             var ascon = new AsconDigest(asconParameters);