Skip to content

Commit

Permalink
Secret Legacy Mode: Remove
Browse files Browse the repository at this point in the history
Resolves: No entry
  • Loading branch information
shinji-san committed Sep 20, 2023
1 parent 4998af9 commit 4d7eccc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 117 deletions.
28 changes: 2 additions & 26 deletions src/Cryptography/Secret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ namespace SecretSharingDotNet.Cryptography
/// </summary>
public class Secret
{
/// <summary>
/// Gets or sets the legacy mode on (<see langword="true"/>) or <see langword="off"/> to be compatible with
/// v0.6.0 or older.
/// </summary>
[Obsolete("Legacy mode is deprecated and will be removed in the next versions.")]
public static readonly ThreadLocal<bool> LegacyMode = new ThreadLocal<bool> {Value = false};

/// <summary>
/// Maximum mark byte to terminate the secret array and to avoid negative secret values.
/// </summary>
Expand Down Expand Up @@ -84,11 +77,6 @@ internal static Secret<TNumber> CreateRandom<TNumber>(Calculator<TNumber> prime)
rng.GetBytes(randomSecretBytes);
}

if (LegacyMode.Value)
{
return (Calculator.Create(randomSecretBytes, typeof(TNumber)) as Calculator<TNumber>)?.Abs() % prime;
}

int i = randomSecretBytes.Length - 1;
while (i > 0)
{
Expand All @@ -112,20 +100,8 @@ internal static Secret<TNumber> CreateRandom<TNumber>(Calculator<TNumber> prime)
}

/// <summary>
/// Gets the MarkByte count in dependency of the <see cref="LegacyMode"/>.
/// Gets the MarkByte count.
/// </summary>
protected static int MarkByteCount => LegacyMode.Value ? 0 : 1;

/// <summary>
/// Creates an array from a base64 string as in version 0.6.0 or older
/// </summary>
[Obsolete("Legacy mode is deprecated and will be removed in the next versions.")]
protected static readonly Func<string, byte[]> FromBase64Legacy = base64 =>
{
var bytes = Convert.FromBase64String(base64).ToList();
bytes.Insert(0, 0x00);
bytes.Add(0x78);
return bytes.ToArray();
};
protected static int MarkByteCount => 1;
}
}
14 changes: 2 additions & 12 deletions src/Cryptography/Secret`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/// <summary>
Expand All @@ -93,7 +93,7 @@ public Secret(Calculator secretSource) : this(secretSource.ByteRepresentation.To
/// <param name="encoded">Secret encoded as base-64</param>
/// <remarks>For normal text use the implicit cast from <see cref="string"/> to <see cref="Secret{TNumber}"/></remarks>
/// <exception cref="T:System.ArgumentNullException"><paramref name="encoded"/> is <see langword="null"/>, empty or consists exclusively of white-space characters</exception>
public Secret(string encoded) : this(LegacyMode.Value ? FromBase64Legacy(encoded) : Convert.FromBase64String(encoded)) { }
public Secret(string encoded) : this(Convert.FromBase64String(encoded)) { }

/// <summary>
/// Gets the <see cref="Secret{TNumber}"/> byte size.
Expand Down Expand Up @@ -216,11 +216,6 @@ public static implicit operator Calculator<TNumber>(Secret<TNumber> secret)
/// If <paramref name="other"/> is <see langword="null"/>, the method returns <see langword="false"/>.</returns>
public bool Equals(Secret<TNumber> 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));
}
Expand Down Expand Up @@ -277,11 +272,6 @@ public byte[] ToByteArray()
/// <returns>The <see cref="string"/> representation in base 64</returns>
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);
}
}
Expand Down
33 changes: 16 additions & 17 deletions src/Cryptography/ShamirsSecretSharing`3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ShamirsSecretSharing<TNumber, TExtendedGcdAlgorithm, TExtendedGcdRe
/// </summary>
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
};
Expand Down Expand Up @@ -86,30 +86,25 @@ public ShamirsSecretSharing(TExtendedGcdAlgorithm extendedGcd)
/// <summary>
/// Gets or sets the security level
/// </summary>
/// <remarks>Value is lower than 5 or greater than 43112609.</remarks>
/// <exception cref="T:System.ArgumentOutOfRangeException" accessor="set">Value is lower than 5 or greater than 43112609.</exception>
/// <remarks>Value is lower than 13 or greater than 43112609.</remarks>
/// <exception cref="T:System.ArgumentOutOfRangeException" accessor="set">Value is lower than 13 or greater than 43112609.</exception>
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)
{
Expand All @@ -127,9 +122,9 @@ public int SecurityLevel
/// </summary>
/// <param name="numberOfMinimumShares">Minimum number of shared secrets for reconstruction</param>
/// <param name="numberOfShares">Maximum number of shared secrets</param>
/// <param name="securityLevel">Security level (in number of bits). Minimum is 5 for legacy mode and 13 for normal mode.</param>
/// <param name="securityLevel">Security level (in number of bits). Minimum is 13.</param>
/// <returns></returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">The <paramref name="securityLevel"/> parameter is lower than 5 or greater than 43112609. OR The <paramref name="numberOfMinimumShares"/> parameter is lower than 2 or greater than <paramref name="numberOfShares"/>.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">The <paramref name="securityLevel"/> parameter is lower than 13 or greater than 43112609. OR The <paramref name="numberOfMinimumShares"/> parameter is lower than 2 or greater than <paramref name="numberOfShares"/>.</exception>
public Shares<TNumber> MakeShares(TNumber numberOfMinimumShares, TNumber numberOfShares, int securityLevel)
{
try
Expand Down Expand Up @@ -171,11 +166,11 @@ public Shares<TNumber> MakeShares(TNumber numberOfMinimumShares, TNumber numberO
/// <param name="numberOfMinimumShares">Minimum number of shared secrets for reconstruction</param>
/// <param name="numberOfShares">Maximum number of shared secrets</param>
/// <param name="secret">secret text as <see cref="Secret{TNumber}"/> or see cref="string"/></param>
/// <param name="securityLevel">Security level (in number of bits). Minimum is 5 for legacy mode and 13 for normal mode.</param>
/// <param name="securityLevel">Security level (in number of bits). Minimum is 13.</param>
/// <returns></returns>
/// <remarks>This method can modify the <see cref="SecurityLevel"/> based on the <paramref name="secret"/> length.</remarks>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="secret"/> parameter is <see langword="null"/>.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">The <paramref name="securityLevel"/> is lower than 5 or greater than 43112609. OR <paramref name="numberOfMinimumShares"/> is lower than 2 or greater than <paramref name="numberOfShares"/>.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">The <paramref name="securityLevel"/> is lower than 13 or greater than 43112609. OR <paramref name="numberOfMinimumShares"/> is lower than 2 or greater than <paramref name="numberOfShares"/>.</exception>
public Shares<TNumber> MakeShares(TNumber numberOfMinimumShares, TNumber numberOfShares, Secret<TNumber> secret, int securityLevel)
{
try
Expand Down Expand Up @@ -447,12 +442,16 @@ public Secret<TNumber> Reconstruction(FinitePoint<TNumber>[] 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);
}
Expand Down
38 changes: 0 additions & 38 deletions tests/ShamirsSecretSharingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,26 +263,6 @@ public void TestReconstructFromStringArray()
Assert.Equal(TestData.DefaultTestPassword, secret);
}

/// <summary>
/// Tests the secret reconstruction from array of shares represented by strings (legacy mode to compatible to v0.6.0 or older)
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
[Fact]
public void TestReconstructFromStringArrayLegacy()
{
Secret.LegacyMode.Value = true;
try
{
var combine = new ShamirsSecretSharing<BigInteger>(new ExtendedEuclideanAlgorithm<BigInteger>());
var secret = combine.Reconstruction(TestData.GetPredefinedSharesLegacy());
Assert.Equal(TestData.DefaultTestPassword, secret);
}
finally
{
Secret.LegacyMode.Value = false;
}
}

/// <summary>
/// Tests the secret reconstruction from shares represented by a single string (separated by newline)
/// </summary>
Expand Down Expand Up @@ -329,23 +309,5 @@ public void ReconstructionFailsAtRnd(int byteArraySize)

Assert.Equal(1.0, (double)ok / total);
}

/// <summary>
/// Tests whether or not bug #60 occurs [Reconstruction fails at random].
/// </summary>
[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;
}
}
}
}
39 changes: 15 additions & 24 deletions tests/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public static class TestData
public static IEnumerable<object[]> TestNumberData =>
new List<object[]>
{
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},
Expand All @@ -80,12 +80,12 @@ public static class TestData
public static IEnumerable<object[]> TestPasswordData =>
new List<object[]>
{
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},
Expand All @@ -100,10 +100,11 @@ public static class TestData
public static IEnumerable<object[]> TestRandomSecretData =>
new List<object[]>
{
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},
Expand All @@ -125,6 +126,7 @@ public static class TestData
/// <summary>
/// A set of pre-defined shares for reconstruction tests
/// </summary>
/// <remarks>The reconstruction with these shares should be result in <see cref="DefaultTestPassword"/></remarks>
public static string[] GetPredefinedShares() => new[]
{
"01-0131621CFFE838F31347293CC1093C91C7BF50F64AD0F3F09AAF1844F26EECC7F84A23376E5786E8B34DDDFAC957F025201A42114D4C114B42DBC70B96453A19D600",
Expand All @@ -137,27 +139,16 @@ public static string[] GetPredefinedShares() => new[]

};

/// <summary>
/// A set of pre-defined shares for reconstruction tests (Legacy mode for v0.6.0 or older)
/// </summary>
public static string[] GetPredefinedSharesLegacy() => new[]
{
"01-A096198683E02AA999D66B4710E69E0118EB81511E5971B3DFA1916DBC00A1B2B12F21802A4B350A562DFDD0376A2D930FCD5AFFEFA553FEB0F739F063B452E962",
"02-D71CFE40BF6AB68BF92C24E9D5C8C5C3AB02714E9C001761B0F71D2C627995E65932DB4EE3F85827C14CD756B6D1D4731F4E5E442E97717C21975A062C6EB9910201",
"03-ED9212311F9F0EA88E0349E5A7A8E3462D4739F7DDF611099301A53BF169DD9BF8072E6C2A096B57415E8E917B36F6A12F830ACFBAD3597A51DE6142582D34F9DE01",
"04-E3F85656A37D33FE585ADA3B8685F88A9CB8DA4BE33B62AB87BF269C69D278D28DB019D8FF7B6B9AD66122818798911D406C5F9F955B0CF840CD4FA4E8F1C21FF800",
"05-B84ECBB04B06258E5831D8EC705F0490F956554CACCF07488E31A34DCBB2678A192C9E9263515AF080579325DAF7A6E650095DB5BE2E89F5EF63242CDDBB65054E",
"06-6B946F401839E3578D8842F8673607564422A9F838B202DFA6571A50160BAAC39B7ABB9B55893759403FE17E735436FD615A0311364DD0725EA2DFD9358B1CAAE001",
"07-FEC9430509166E5BF75F195E6B0A01DD7C1AD65089E35270D1318CA34ADB3F7E149C71F3D52303D514190C8D53AE3F61735F52B2FBB6E16F8C8881ADF25FE70DB001"
};

/// <summary>
/// Gets a list of byte array sizes for several tests
/// </summary>
public static IEnumerable<object[]> ByteArraySize =>
new List<object[]>
{
new object[] { 1},
new object[] { 2},
new object[] { 3},
new object[] { 4},
new object[] { 27},
new object[] { 32},
new object[] { 53},
Expand Down

0 comments on commit 4d7eccc

Please sign in to comment.