summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 11:16:39 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 11:16:39 +0700
commit7ab750bc8ad1952914c4217773e954bf23c62179 (patch)
treec91efec693a1364cb4814cb4a4c277cab26b4bfd
parentGenerics migration in Tls (diff)
downloadBouncyCastle.NET-ed25519-7ab750bc8ad1952914c4217773e954bf23c62179.tar.xz
Generics migration in Pqc
-rw-r--r--crypto/src/pqc/crypto/lms/HSS.cs13
-rw-r--r--crypto/src/pqc/crypto/lms/HSSPrivateKeyParameters.cs70
-rw-r--r--crypto/src/pqc/crypto/lms/LMSSignature.cs3
-rw-r--r--crypto/src/pqc/crypto/sphincsplus/Fors.cs6
-rw-r--r--crypto/src/pqc/crypto/sphincsplus/HT.cs6
-rw-r--r--crypto/test/src/pqc/crypto/lms/HSSTests.cs29
-rw-r--r--crypto/test/src/pqc/crypto/lms/TypeTests.cs5
7 files changed, 59 insertions, 73 deletions
diff --git a/crypto/src/pqc/crypto/lms/HSS.cs b/crypto/src/pqc/crypto/lms/HSS.cs
index 8fc5dee3b..556ffac26 100644
--- a/crypto/src/pqc/crypto/lms/HSS.cs
+++ b/crypto/src/pqc/crypto/lms/HSS.cs
@@ -1,6 +1,5 @@
 using System;
-using System.Collections;
-using Org.BouncyCastle.Utilities;
+using System.Collections.Generic;
 
 namespace Org.BouncyCastle.Pqc.Crypto.Lms
 {
@@ -63,8 +62,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
 
             return new HSSPrivateKeyParameters(
                 parameters.GetDepth(),
-                Platform.CreateArrayList(keys),
-                Platform.CreateArrayList(sig),
+                new List<LMSPrivateKeyParameters>(keys),
+                new List<LMSSignature>(sig),
                 0, hssKeyMaxIndex);
         }
 
@@ -103,7 +102,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
 
                 int L = keyPair.L;
                 int d = L;
-                IList prv = keyPair.GetKeys();
+                var prv = keyPair.GetKeys();
                 while ((prv[d - 1] as LMSPrivateKeyParameters).GetIndex() == 1 << ((prv[(d - 1)] as LMSPrivateKeyParameters ).GetSigParameters().GetH()))
                 {
                     d = d - 1;
@@ -136,8 +135,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
             {
                 RangeTestKeys(keyPair);
                 
-                IList keys = keyPair.GetKeys();
-                IList sig = keyPair.GetSig();
+                var keys = keyPair.GetKeys();
+                var sig = keyPair.GetSig();
 
                 nextKey = keyPair.GetKeys()[L - 1] as LMSPrivateKeyParameters;
 
diff --git a/crypto/src/pqc/crypto/lms/HSSPrivateKeyParameters.cs b/crypto/src/pqc/crypto/lms/HSSPrivateKeyParameters.cs
index 8e4b2463b..fc85af1aa 100644
--- a/crypto/src/pqc/crypto/lms/HSSPrivateKeyParameters.cs
+++ b/crypto/src/pqc/crypto/lms/HSSPrivateKeyParameters.cs
@@ -1,37 +1,34 @@
-
 using System;
-using System.Collections;
 using System.Collections.Generic;
 using System.IO;
+
 using Org.BouncyCastle.Security;
 using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.IO;
-using Org.BouncyCastle.Utilities.Collections;
 
 // using static Org.BouncyCastle.Pqc.Crypto.Lms.HSS.rangeTestKeys;
 
 namespace Org.BouncyCastle.Pqc.Crypto.Lms
 {
-    
     public class HSSPrivateKeyParameters
         : LMSKeyParameters, ILMSContextBasedSigner
     {
         private int l;
         private bool isShard;
-        private IList keys; //LMSPrivateKeyParameters
-        private IList sig; //LMSSignature
+        private IList<LMSPrivateKeyParameters> keys;
+        private IList<LMSSignature> sig;
         private long indexLimit;
         private long index = 0;
 
         private HSSPublicKeyParameters publicKey;
 
-        public HSSPrivateKeyParameters(int l, IList keys, IList sig, long index, long indexLimit)
+        public HSSPrivateKeyParameters(int l, IList<LMSPrivateKeyParameters> keys, IList<LMSSignature> sig, long index,
+            long indexLimit)
     	    :base(true)
         {
-
             this.l = l;
-            this.keys = Platform.CreateArrayList(keys);
-            this.sig =  Platform.CreateArrayList(sig);
+            this.keys = new List<LMSPrivateKeyParameters>(keys);
+            this.sig = new List<LMSSignature>(sig);
             this.index = index;
             this.indexLimit = indexLimit;
             this.isShard = false;
@@ -42,15 +39,16 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
             ResetKeyToIndex();
         }
 
-        private HSSPrivateKeyParameters(int l, IList keys, IList sig, long index, long indexLimit, bool isShard)
+        private HSSPrivateKeyParameters(int l, IList<LMSPrivateKeyParameters> keys, IList<LMSSignature> sig, long index,
+            long indexLimit, bool isShard)
     	    :base(true)
         {
 
             this.l = l;
             // this.keys =  new UnmodifiableListProxy(keys);
             // this.sig =  new UnmodifiableListProxy(sig);
-            this.keys =  Platform.CreateArrayList(keys);
-            this.sig =  Platform.CreateArrayList(sig);
+            this.keys = new List<LMSPrivateKeyParameters>(keys);
+            this.sig = new List<LMSSignature>(sig);
             this.index = index;
             this.indexLimit = indexLimit;
             this.isShard = isShard;
@@ -97,8 +95,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
                 bool limited =  BitConverter.ToBoolean(data, 0);
                 
 
-                ArrayList keys = new ArrayList();
-                ArrayList signatures = new ArrayList();
+                var keys = new List<LMSPrivateKeyParameters>();
+                var signatures = new List<LMSSignature>();
 
                 for (int t = 0; t < d; t++)
                 {
@@ -183,12 +181,12 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
             }
         }
 
-        protected void UpdateHierarchy(IList newKeys, IList newSig)
+        protected void UpdateHierarchy(IList<LMSPrivateKeyParameters> newKeys, IList<LMSSignature> newSig)
         {
             lock (this)
             {
-                keys = Platform.CreateArrayList(newKeys);
-                sig =  Platform.CreateArrayList(newSig);
+                keys = new List<LMSPrivateKeyParameters>(newKeys);
+                sig = new List<LMSSignature>(newSig);
             }
         }
 
@@ -222,11 +220,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
         {
             lock (this)
             {
-
                 if (GetUsagesRemaining() < usageCount)
-                {
                     throw new ArgumentException("usageCount exceeds usages remaining in current leaf");
-                }
 
                 long maxIndexForShard = index + usageCount;
                 long shardStartIndex = index;
@@ -236,10 +231,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
                 //
                 index += usageCount;
 
-                IList keys = new ArrayList(this.GetKeys());
-                IList sig = new ArrayList(this.GetSig());
+                var keys = new List<LMSPrivateKeyParameters>(this.GetKeys());
+                var sig = new List<LMSSignature>(this.GetSig());
 
-                HSSPrivateKeyParameters shard = MakeCopy(new HSSPrivateKeyParameters(l, keys, sig, shardStartIndex, maxIndexForShard, true));
+                HSSPrivateKeyParameters shard = MakeCopy(new HSSPrivateKeyParameters(l, keys, sig, shardStartIndex,
+                    maxIndexForShard, true));
 
                 ResetKeyToIndex();
 
@@ -248,7 +244,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
         }
 
 
-        public IList GetKeys()
+        public IList<LMSPrivateKeyParameters> GetKeys()
         {
             lock (this)
             {
@@ -256,7 +252,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
             }
         }
 
-        internal IList GetSig()
+        internal IList<LMSSignature>GetSig()
         {
             lock (this)
             {
@@ -273,7 +269,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
         void ResetKeyToIndex()
         {
             // Extract the original keys
-            IList originalKeys = GetKeys();
+            var originalKeys = GetKeys();
 
 
             long[] qTreePath = new long[originalKeys.Count];
@@ -404,7 +400,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
             byte[] childI = new byte[16];
             Array.Copy(postImage, 0, childI, 0, childI.Length);
 
-            IList newKeys = Platform.CreateArrayList(keys);
+            var newKeys = new List<LMSPrivateKeyParameters>(keys);
 
             //
             // We need the parameters from the LMS key we are replacing.
@@ -414,15 +410,13 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
 
             newKeys[d] = LMS.GenerateKeys(oldPk.GetSigParameters(), oldPk.GetOtsParameters(), 0, childI, childRootSeed);
 
-            IList newSig = Platform.CreateArrayList(sig);
+            var newSig = new List<LMSSignature>(sig);
 
             newSig[d - 1] = LMS.GenerateSign(newKeys[d - 1] as LMSPrivateKeyParameters,
                 (newKeys[d] as LMSPrivateKeyParameters).GetPublicKey().ToByteArray());
 
-
-            this.keys = Platform.CreateArrayList(newKeys);
-            this.sig = Platform.CreateArrayList(newSig);
-
+            this.keys = new List<LMSPrivateKeyParameters>(newKeys);
+            this.sig = new List<LMSSignature>(newSig);
         }
 
         public override bool Equals(Object o)
@@ -454,14 +448,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
             {
                 return false;
             }
-            if (!CompareArrayLists(keys, that.keys))
+            if (!CompareLists(keys, that.keys))
             {
                 return false;
             }
-            return CompareArrayLists(sig, that.sig);
+            return CompareLists(sig, that.sig);
         }
 
-        private bool CompareArrayLists(IList arr1, IList arr2)
+        private bool CompareLists<T>(IList<T> arr1, IList<T> arr2)
         {
             for (int i=0; i<arr1.Count && i<arr2.Count; i++)
             {
@@ -529,8 +523,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
             {
                 HSS.RangeTestKeys(this);
 
-                IList keys = this.GetKeys();
-                IList sig = this.GetSig();
+                var keys = this.GetKeys();
+                var sig = this.GetSig();
 
                 nextKey = this.GetKeys()[(L - 1)] as LMSPrivateKeyParameters;
 
diff --git a/crypto/src/pqc/crypto/lms/LMSSignature.cs b/crypto/src/pqc/crypto/lms/LMSSignature.cs
index d777ee75e..8769160b4 100644
--- a/crypto/src/pqc/crypto/lms/LMSSignature.cs
+++ b/crypto/src/pqc/crypto/lms/LMSSignature.cs
@@ -1,7 +1,6 @@
 using System;
-using System.Collections;
 using System.IO;
-using Org.BouncyCastle.Pqc.Crypto.Lms;
+
 using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.IO;
 
diff --git a/crypto/src/pqc/crypto/sphincsplus/Fors.cs b/crypto/src/pqc/crypto/sphincsplus/Fors.cs
index 164a6e9f0..af86eec10 100644
--- a/crypto/src/pqc/crypto/sphincsplus/Fors.cs
+++ b/crypto/src/pqc/crypto/sphincsplus/Fors.cs
@@ -1,5 +1,6 @@
+using System;
+using System.Collections.Generic;
 
-using System.Collections;
 using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Pqc.Crypto.SphincsPlus
@@ -17,8 +18,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.SphincsPlus
         // Output: n-byte root node - top node on Stack
         byte[] TreeHash(byte[] skSeed, uint s, int z, byte[] pkSeed, Adrs adrsParam)
         {
-
-            IList stack = Platform.CreateArrayList();
+            var stack = new List<NodeEntry>();
 
             if (s % (1 << z) != 0)
             {
diff --git a/crypto/src/pqc/crypto/sphincsplus/HT.cs b/crypto/src/pqc/crypto/sphincsplus/HT.cs
index 765fb8d4a..2cd149f0d 100644
--- a/crypto/src/pqc/crypto/sphincsplus/HT.cs
+++ b/crypto/src/pqc/crypto/sphincsplus/HT.cs
@@ -1,6 +1,6 @@
-
-using System.Collections;
+using System;
 using System.Collections.Generic;
+
 using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Pqc.Crypto.SphincsPlus
@@ -155,7 +155,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.SphincsPlus
         {
             Adrs adrs = new Adrs(adrsParam);
 
-            IList stack = Platform.CreateArrayList();
+            var stack = new List<NodeEntry>();
 
             if (s % (1 << (int)z) != 0)
             {
diff --git a/crypto/test/src/pqc/crypto/lms/HSSTests.cs b/crypto/test/src/pqc/crypto/lms/HSSTests.cs
index d97038d3b..0d01e5d1f 100644
--- a/crypto/test/src/pqc/crypto/lms/HSSTests.cs
+++ b/crypto/test/src/pqc/crypto/lms/HSSTests.cs
@@ -1,18 +1,15 @@
 using System;
-using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Text;
+
 using NUnit.Framework;
-using Org.BouncyCastle.Bcpg;
-using Org.BouncyCastle.Pqc.Crypto.Lms;
+
 using Org.BouncyCastle.Security;
 using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.Encoders;
-using Org.BouncyCastle.Utilities.IO;
 using Org.BouncyCastle.Utilities.Test;
 
-
 namespace Org.BouncyCastle.Pqc.Crypto.Lms
 {
 
@@ -76,7 +73,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
         [Test]
         public void TestHSSVector_1()
         {
-            IList blocks = LoadVector("pqc.lms.testcase_1.txt");
+            var blocks = LoadVector("pqc.lms.testcase_1.txt");
 
             HSSPublicKeyParameters publicKey = HSSPublicKeyParameters.GetInstance(blocks[0]);
             byte[] message = (byte[]) blocks[1];
@@ -93,7 +90,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
         [Test]
         public void TestHSSVector_2()
         {
-            IList blocks = LoadVector("pqc.lms.testcase_2.txt");
+            var blocks = LoadVector("pqc.lms.testcase_2.txt");
 
             HSSPublicKeyParameters publicKey = HSSPublicKeyParameters.GetInstance(blocks[0]);
             byte[] message = blocks[1] as byte[];
@@ -107,10 +104,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
             Assert.True(LMS.VerifySignature(lmsPub, lmsSignature, message), "Test Case 2 Signature 2");
         }
 
-        private IList LoadVector(string vector)
+        private IList<byte[]> LoadVector(string vector)
         {
             StreamReader bin = new StreamReader(SimpleTest.GetTestDataAsStream(vector));
-            IList blocks = new ArrayList();
+            var blocks = new List<byte[]>();
             StringBuilder sw = new StringBuilder();
 
             string line;
@@ -326,8 +323,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
         {
             StreamReader sr = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.lms.depth_1.txt"));
 
-            IList lmsParameters = new ArrayList();
-            IList lmOtsParameters = new ArrayList();
+            var lmsParameters = new List<LMSigParameters>();
+            var lmOtsParameters = new List<LMOtsParameters>();
             byte[] message = null;
             byte[] hssPubEnc = null;
             MemoryStream fixedESBuffer = new MemoryStream();
@@ -384,7 +381,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
                     // Deserialize pub key from reference impl.
                     //
                     HSSPublicKeyParameters vectorSourcedPubKey = HSSPublicKeyParameters.GetInstance(hssPubEnc);
-                    IList lmsParams = new ArrayList();
+                    var lmsParams = new List<LMSParameters>();
 
                     for (int i = 0; i != lmsParameters.Count; i++)
                     {
@@ -464,12 +461,12 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
         {
             using (StreamReader sr = new StreamReader(SimpleTest.GetTestDataAsStream("pqc.lms.expansion.txt")))
             {
-                IList lmsParameters = new ArrayList();
-                IList lmOtsParameters = new ArrayList();
+                var lmsParameters = new List<LMSigParameters>();
+                var lmOtsParameters = new List<LMOtsParameters>();
                 byte[] message = null;
                 byte[] hssPubEnc = null;
                 MemoryStream fixedESBuffer = new MemoryStream();
-                IList sigVectors = new ArrayList();
+                var sigVectors = new List<byte[]>();
                 int d = 0;
 
                 string line;
@@ -518,7 +515,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
                 FixedSecureRandom.Source[] source = {new FixedSecureRandom.Source(fixedESBuffer.ToArray())};
                 FixedSecureRandom fixRnd = new FixedSecureRandom(source);
                 fixedESBuffer.SetLength(0);
-                IList lmsParams = new ArrayList();
+                var lmsParams = new List<LMSParameters>();
 
                 for (int i = 0; i != lmsParameters.Count; i++)
                 {
diff --git a/crypto/test/src/pqc/crypto/lms/TypeTests.cs b/crypto/test/src/pqc/crypto/lms/TypeTests.cs
index 17229585b..28d76aa2b 100644
--- a/crypto/test/src/pqc/crypto/lms/TypeTests.cs
+++ b/crypto/test/src/pqc/crypto/lms/TypeTests.cs
@@ -1,9 +1,6 @@
 using System;
-using System.Collections;
-using System.Collections.Generic;
+
 using NUnit.Framework;
-using Org.BouncyCastle.Pqc.Crypto.Lms;
-using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Pqc.Crypto.Lms
 {