From 4d7ecccdbfe3294908db9db71cba400a528a3143 Mon Sep 17 00:00:00 2001 From: Sebastian Walther Date: Tue, 29 Aug 2023 22:55:34 +0200 Subject: [PATCH] Secret Legacy Mode: Remove Resolves: No entry --- src/Cryptography/Secret.cs | 28 ++-------------- src/Cryptography/Secret`1.cs | 14 ++------ src/Cryptography/ShamirsSecretSharing`3.cs | 33 +++++++++--------- tests/ShamirsSecretSharingTest.cs | 38 --------------------- tests/TestData.cs | 39 +++++++++------------- 5 files changed, 35 insertions(+), 117 deletions(-) diff --git a/src/Cryptography/Secret.cs b/src/Cryptography/Secret.cs index ff69b5b..e80fb41 100644 --- a/src/Cryptography/Secret.cs +++ b/src/Cryptography/Secret.cs @@ -43,13 +43,6 @@ namespace SecretSharingDotNet.Cryptography /// public class Secret { - /// - /// Gets or sets the legacy mode on () or to be compatible with - /// v0.6.0 or older. - /// - [Obsolete("Legacy mode is deprecated and will be removed in the next versions.")] - public static readonly ThreadLocal LegacyMode = new ThreadLocal {Value = false}; - /// /// Maximum mark byte to terminate the secret array and to avoid negative secret values. /// @@ -84,11 +77,6 @@ internal static Secret CreateRandom(Calculator prime) rng.GetBytes(randomSecretBytes); } - if (LegacyMode.Value) - { - return (Calculator.Create(randomSecretBytes, typeof(TNumber)) as Calculator)?.Abs() % prime; - } - int i = randomSecretBytes.Length - 1; while (i > 0) { @@ -112,20 +100,8 @@ internal static Secret CreateRandom(Calculator prime) } /// - /// Gets the MarkByte count in dependency of the . + /// Gets the MarkByte count. /// - protected static int MarkByteCount => LegacyMode.Value ? 0 : 1; - - /// - /// Creates an array from a base64 string as in version 0.6.0 or older - /// - [Obsolete("Legacy mode is deprecated and will be removed in the next versions.")] - protected static readonly Func FromBase64Legacy = base64 => - { - var bytes = Convert.FromBase64String(base64).ToList(); - bytes.Insert(0, 0x00); - bytes.Add(0x78); - return bytes.ToArray(); - }; + protected static int MarkByteCount => 1; } } diff --git a/src/Cryptography/Secret`1.cs b/src/Cryptography/Secret`1.cs index 20b5eec..7d79dee 100644 --- a/src/Cryptography/Secret`1.cs +++ b/src/Cryptography/Secret`1.cs @@ -77,7 +77,7 @@ public Secret(byte[] secretSource) byte maxMarkByte = secretSource.Length == 1 ? MinMarkByte : MaxMarkByte; byte markByte = (byte)(new Random(buffer[0]).Next(0x01, maxMarkByte) % maxMarkByte); byte[] bytes = (byte[])secretSource.Clone(); - this.secretNumber = LegacyMode.Value ? bytes : bytes.Concat(new[] {markByte}).ToArray(); + this.secretNumber = bytes.Concat(new[] {markByte}).ToArray(); } /// @@ -93,7 +93,7 @@ public Secret(Calculator secretSource) : this(secretSource.ByteRepresentation.To /// Secret encoded as base-64 /// For normal text use the implicit cast from to /// is , empty or consists exclusively of white-space characters - public Secret(string encoded) : this(LegacyMode.Value ? FromBase64Legacy(encoded) : Convert.FromBase64String(encoded)) { } + public Secret(string encoded) : this(Convert.FromBase64String(encoded)) { } /// /// Gets the byte size. @@ -216,11 +216,6 @@ public static implicit operator Calculator(Secret secret) /// If is , the method returns . public bool Equals(Secret other) { - if (LegacyMode.Value) - { - return !(other is null) && Calculator.Create(this.secretNumber, typeof(TNumber)).Equals(Calculator.Create(other.secretNumber, typeof(TNumber))); - } - return !(other is null) && this.secretNumber.Subset(0, this.SecretByteSize - MarkByteCount) .SequenceEqual(other.secretNumber.Subset(0, other.SecretByteSize - MarkByteCount)); } @@ -277,11 +272,6 @@ public byte[] ToByteArray() /// The representation in base 64 public string ToBase64() { - if (LegacyMode.Value) - { - return Convert.ToBase64String(this.secretNumber, 1, this.secretNumber.Length - 2); - } - return Convert.ToBase64String(this.secretNumber, 0, this.secretNumber.Length - MarkByteCount); } } diff --git a/src/Cryptography/ShamirsSecretSharing`3.cs b/src/Cryptography/ShamirsSecretSharing`3.cs index 18cd383..742683d 100644 --- a/src/Cryptography/ShamirsSecretSharing`3.cs +++ b/src/Cryptography/ShamirsSecretSharing`3.cs @@ -52,7 +52,7 @@ public class ShamirsSecretSharing private readonly int[] securityLevels = new int[] { - 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, + 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 86243, 110503, 132049, 216091, 756839, 859433, 1257787, 1398269, 2976221, 3021377, 6972593, 13466917, 20996011, 24036583, 25964951, 30402457, 32582657, 37156667, 42643801, 43112609 }; @@ -86,30 +86,25 @@ public ShamirsSecretSharing(TExtendedGcdAlgorithm extendedGcd) /// /// Gets or sets the security level /// - /// Value is lower than 5 or greater than 43112609. - /// Value is lower than 5 or greater than 43112609. + /// Value is lower than 13 or greater than 43112609. + /// Value is lower than 13 or greater than 43112609. public int SecurityLevel { get => this.fixedSecurityLevel; set { - if (value < 5) + if (value < 13) { throw new ArgumentOutOfRangeException(nameof(value), value, ErrorMessages.MinimumSecurityLevelExceeded); } - if (!Secret.LegacyMode.Value && value < 13) - { - value = 13; - } - int index = Array.BinarySearch(this.securityLevels, value); if (index < 0) { try { - value = this.securityLevels.ElementAt(~index); + value = this.securityLevels[~index]; } catch (ArgumentOutOfRangeException) { @@ -127,9 +122,9 @@ public int SecurityLevel /// /// Minimum number of shared secrets for reconstruction /// Maximum number of shared secrets - /// Security level (in number of bits). Minimum is 5 for legacy mode and 13 for normal mode. + /// Security level (in number of bits). Minimum is 13. /// - /// The parameter is lower than 5 or greater than 43112609. OR The parameter is lower than 2 or greater than . + /// The parameter is lower than 13 or greater than 43112609. OR The parameter is lower than 2 or greater than . public Shares MakeShares(TNumber numberOfMinimumShares, TNumber numberOfShares, int securityLevel) { try @@ -171,11 +166,11 @@ public Shares MakeShares(TNumber numberOfMinimumShares, TNumber numberO /// Minimum number of shared secrets for reconstruction /// Maximum number of shared secrets /// secret text as or see cref="string"/> - /// Security level (in number of bits). Minimum is 5 for legacy mode and 13 for normal mode. + /// Security level (in number of bits). Minimum is 13. /// /// This method can modify the based on the length. /// The parameter is . - /// The is lower than 5 or greater than 43112609. OR is lower than 2 or greater than . + /// The is lower than 13 or greater than 43112609. OR is lower than 2 or greater than . public Shares MakeShares(TNumber numberOfMinimumShares, TNumber numberOfShares, Secret secret, int securityLevel) { try @@ -447,12 +442,16 @@ public Secret Reconstruction(FinitePoint[] shares) this.SecurityLevel = maximumY.ByteCount * 8; int index = Array.IndexOf(this.securityLevels, this.SecurityLevel); - while ((maximumY % this.mersennePrime + this.mersennePrime) % this.mersennePrime == maximumY && index > 0 && this.SecurityLevel > 5) + while ((maximumY % this.mersennePrime + this.mersennePrime) % this.mersennePrime == maximumY && index >= 0) { - this.SecurityLevel = this.securityLevels[--index]; + index--; + if (index >= 0) + { + this.SecurityLevel = this.securityLevels[index]; + } } - this.SecurityLevel = this.securityLevels[this.SecurityLevel > 5 ? ++index : index]; + this.SecurityLevel = this.securityLevels[index + 1]; return this.LagrangeInterpolate(shares, this.mersennePrime); } diff --git a/tests/ShamirsSecretSharingTest.cs b/tests/ShamirsSecretSharingTest.cs index ab69528..a0213f9 100644 --- a/tests/ShamirsSecretSharingTest.cs +++ b/tests/ShamirsSecretSharingTest.cs @@ -263,26 +263,6 @@ public void TestReconstructFromStringArray() Assert.Equal(TestData.DefaultTestPassword, secret); } - /// - /// Tests the secret reconstruction from array of shares represented by strings (legacy mode to compatible to v0.6.0 or older) - /// - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")] - [Fact] - public void TestReconstructFromStringArrayLegacy() - { - Secret.LegacyMode.Value = true; - try - { - var combine = new ShamirsSecretSharing(new ExtendedEuclideanAlgorithm()); - var secret = combine.Reconstruction(TestData.GetPredefinedSharesLegacy()); - Assert.Equal(TestData.DefaultTestPassword, secret); - } - finally - { - Secret.LegacyMode.Value = false; - } - } - /// /// Tests the secret reconstruction from shares represented by a single string (separated by newline) /// @@ -329,23 +309,5 @@ public void ReconstructionFailsAtRnd(int byteArraySize) Assert.Equal(1.0, (double)ok / total); } - - /// - /// Tests whether or not bug #60 occurs [Reconstruction fails at random]. - /// - [Theory] - [MemberData(nameof(TestData.ByteArraySize), MemberType = typeof(TestData))] - public void ReconstructionFailsAtRndLegacy(int byteArraySize) - { - Secret.LegacyMode.Value = true; - try - { - ReconstructionFailsAtRnd(byteArraySize); - } - finally - { - Secret.LegacyMode.Value = false; - } - } } } diff --git a/tests/TestData.cs b/tests/TestData.cs index f923359..9c5ceda 100644 --- a/tests/TestData.cs +++ b/tests/TestData.cs @@ -58,14 +58,14 @@ public static class TestData public static IEnumerable TestNumberData => new List { - new object[] {5, 31, DefaultPosTestNumber}, + new object[] {13, 31, DefaultPosTestNumber}, new object[] {17, 31, DefaultPosTestNumber}, new object[] {127, 127, DefaultPosTestNumber}, new object[] {130, 521, DefaultPosTestNumber}, new object[] {500, 521, DefaultPosTestNumber}, new object[] {1279, 1279, DefaultPosTestNumber}, - new object[] {5, 31, DefaultNegTestNumber}, + new object[] {13, 31, DefaultNegTestNumber}, new object[] {17, 31, DefaultNegTestNumber}, new object[] {127, 127, DefaultNegTestNumber}, new object[] {130, 521, DefaultNegTestNumber}, @@ -80,12 +80,12 @@ public static class TestData public static IEnumerable TestPasswordData => new List { - new object[] {5, 31, " "}, - new object[] {5, 31, "0"}, - new object[] {5, 31, "A"}, - new object[] {5, 31, "Z"}, - new object[] {5, 31, "ÿ"}, - new object[] {5, 521, DefaultTestPassword}, + new object[] {13, 31, " "}, + new object[] {13, 31, "0"}, + new object[] {13, 31, "A"}, + new object[] {13, 31, "Z"}, + new object[] {13, 31, "ÿ"}, + new object[] {13, 521, DefaultTestPassword}, new object[] {17, 521, DefaultTestPassword}, new object[] {127, 521, DefaultTestPassword}, new object[] {130, 521, DefaultTestPassword}, @@ -100,10 +100,11 @@ public static class TestData public static IEnumerable TestRandomSecretData => new List { - new object[] {5, 13}, - new object[] {7, 13}, new object[] {13, 13}, new object[] {17, 17}, + new object[] {31, 31}, + new object[] {61, 61}, + new object[] {89, 89}, new object[] {127, 127}, new object[] {130, 521}, new object[] {500, 521}, @@ -125,6 +126,7 @@ public static class TestData /// /// A set of pre-defined shares for reconstruction tests /// + /// The reconstruction with these shares should be result in public static string[] GetPredefinedShares() => new[] { "01-0131621CFFE838F31347293CC1093C91C7BF50F64AD0F3F09AAF1844F26EECC7F84A23376E5786E8B34DDDFAC957F025201A42114D4C114B42DBC70B96453A19D600", @@ -137,20 +139,6 @@ public static string[] GetPredefinedShares() => new[] }; - /// - /// A set of pre-defined shares for reconstruction tests (Legacy mode for v0.6.0 or older) - /// - public static string[] GetPredefinedSharesLegacy() => new[] - { - "01-A096198683E02AA999D66B4710E69E0118EB81511E5971B3DFA1916DBC00A1B2B12F21802A4B350A562DFDD0376A2D930FCD5AFFEFA553FEB0F739F063B452E962", - "02-D71CFE40BF6AB68BF92C24E9D5C8C5C3AB02714E9C001761B0F71D2C627995E65932DB4EE3F85827C14CD756B6D1D4731F4E5E442E97717C21975A062C6EB9910201", - "03-ED9212311F9F0EA88E0349E5A7A8E3462D4739F7DDF611099301A53BF169DD9BF8072E6C2A096B57415E8E917B36F6A12F830ACFBAD3597A51DE6142582D34F9DE01", - "04-E3F85656A37D33FE585ADA3B8685F88A9CB8DA4BE33B62AB87BF269C69D278D28DB019D8FF7B6B9AD66122818798911D406C5F9F955B0CF840CD4FA4E8F1C21FF800", - "05-B84ECBB04B06258E5831D8EC705F0490F956554CACCF07488E31A34DCBB2678A192C9E9263515AF080579325DAF7A6E650095DB5BE2E89F5EF63242CDDBB65054E", - "06-6B946F401839E3578D8842F8673607564422A9F838B202DFA6571A50160BAAC39B7ABB9B55893759403FE17E735436FD615A0311364DD0725EA2DFD9358B1CAAE001", - "07-FEC9430509166E5BF75F195E6B0A01DD7C1AD65089E35270D1318CA34ADB3F7E149C71F3D52303D514190C8D53AE3F61735F52B2FBB6E16F8C8881ADF25FE70DB001" - }; - /// /// Gets a list of byte array sizes for several tests /// @@ -158,6 +146,9 @@ public static string[] GetPredefinedSharesLegacy() => new[] new List { new object[] { 1}, + new object[] { 2}, + new object[] { 3}, + new object[] { 4}, new object[] { 27}, new object[] { 32}, new object[] { 53},