From 5226b7c62d5b8eb9d27f64fd88c58c1522398a65 Mon Sep 17 00:00:00 2001 From: Sebastian Walther Date: Thu, 7 Mar 2024 22:46:39 +0100 Subject: [PATCH] Clear Memory Resolves: No entry --- src/Cryptography/ShamirsSecretSharing.cs | 5 +++ src/Cryptography/ShamirsSecretSharing`3.cs | 36 +++++++++++++++++----- src/Cryptography/SharedSeparator.cs | 4 +-- src/Math/Calculator.cs | 2 +- src/SecretSharingDotNet.csproj | 8 +++++ 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/Cryptography/ShamirsSecretSharing.cs b/src/Cryptography/ShamirsSecretSharing.cs index e00ff77..86bcc7e 100644 --- a/src/Cryptography/ShamirsSecretSharing.cs +++ b/src/Cryptography/ShamirsSecretSharing.cs @@ -5,6 +5,11 @@ namespace SecretSharingDotNet.Cryptography; /// public abstract class ShamirsSecretSharing { + /// + /// The minimum number of shares required to reconstruct the secret + /// + protected const int MinimumShareLimit = 2; + /// /// Saves the known security levels (Mersenne prime exponents) /// diff --git a/src/Cryptography/ShamirsSecretSharing`3.cs b/src/Cryptography/ShamirsSecretSharing`3.cs index 2f5531c..0ade8c4 100644 --- a/src/Cryptography/ShamirsSecretSharing`3.cs +++ b/src/Cryptography/ShamirsSecretSharing`3.cs @@ -70,7 +70,7 @@ public class ShamirsSecretSharing @@ -84,7 +84,7 @@ public int SecurityLevel set { - if (value < 13) + if (value < SecurityLevels[0]) { throw new ArgumentOutOfRangeException(nameof(value), value, ErrorMessages.MinimumSecurityLevelExceeded); } @@ -128,7 +128,7 @@ public Shares MakeShares(TNumber numberOfMinimumShares, TNumber numberO int min = ((Calculator)numberOfMinimumShares).ToInt32(); int max = ((Calculator)numberOfShares).ToInt32(); - if (min < 2) + if (min < MinimumShareLimit) { throw new ArgumentOutOfRangeException(nameof(numberOfMinimumShares), numberOfMinimumShares, ErrorMessages.MinNumberOfSharesLowerThanTwo); } @@ -187,7 +187,7 @@ public Shares MakeShares(TNumber numberOfMinimumShares, TNumber numberO { int min = ((Calculator)numberOfMinimumShares).ToInt32(); int max = ((Calculator)numberOfShares).ToInt32(); - if (min < 2) + if (min < MinimumShareLimit) { throw new ArgumentOutOfRangeException(nameof(numberOfMinimumShares), numberOfMinimumShares, ErrorMessages.MinNumberOfSharesLowerThanTwo); } @@ -215,20 +215,40 @@ public Shares MakeShares(TNumber numberOfMinimumShares, TNumber numberO /// /// Minimum number of shared secrets for reconstruction /// +#if NET6_0_OR_GREATER + private unsafe Calculator[] CreatePolynomial(int numberOfMinimumShares) +#else private Calculator[] CreatePolynomial(int numberOfMinimumShares) +#endif { var polynomial = new Calculator[numberOfMinimumShares]; polynomial[0] = Calculator.Zero; byte[] randomNumber = new byte[this.mersennePrime.ByteCount]; - using (var rng = RandomNumberGenerator.Create()) +#if NET6_0_OR_GREATER + fixed (byte* pointer = randomNumber) { + var span = new Span(pointer, this.mersennePrime.ByteCount); + using var rng = RandomNumberGenerator.Create(); for (int i = 1; i < numberOfMinimumShares; i++) { - rng.GetBytes(randomNumber); - polynomial[i] = (Calculator.Create(randomNumber, typeof(TNumber)) as Calculator)?.Abs() % this.mersennePrime; + rng.GetBytes(span); + polynomial[i] = (Calculator.Create(randomNumber, typeof(TNumber)) as Calculator)?.Abs() % + this.mersennePrime; } - } + span.Clear(); + } +#else + using var rng = RandomNumberGenerator.Create(); + for (int i = 1; i < numberOfMinimumShares; i++) + { + rng.GetBytes(randomNumber); + polynomial[i] = (Calculator.Create(randomNumber, typeof(TNumber)) as Calculator)?.Abs() % + this.mersennePrime; + } + + Array.Clear(randomNumber, 0, randomNumber.Length); +#endif return polynomial; } diff --git a/src/Cryptography/SharedSeparator.cs b/src/Cryptography/SharedSeparator.cs index e86345d..94588ec 100644 --- a/src/Cryptography/SharedSeparator.cs +++ b/src/Cryptography/SharedSeparator.cs @@ -41,5 +41,5 @@ internal static class SharedSeparator /// /// Separator array for method usage to avoid allocation of a new array. /// - internal static readonly char[] CoordinateSeparatorArray = { CoordinateSeparator }; -} \ No newline at end of file + internal static readonly char[] CoordinateSeparatorArray = [CoordinateSeparator]; +} diff --git a/src/Math/Calculator.cs b/src/Math/Calculator.cs index 840fbeb..9dbf0e3 100644 --- a/src/Math/Calculator.cs +++ b/src/Math/Calculator.cs @@ -117,7 +117,7 @@ protected static Dictionary> GetDerivedCtors var parameterExpression = Expression.Parameter(paramType); foreach (var childType in ChildTypes) { - var ctorInfo = childType.Value.GetConstructor(new[] {paramType}); + var ctorInfo = childType.Value.GetConstructor([paramType]); if (ctorInfo == null) { continue; diff --git a/src/SecretSharingDotNet.csproj b/src/SecretSharingDotNet.csproj index a196c0b..db83f4c 100644 --- a/src/SecretSharingDotNet.csproj +++ b/src/SecretSharingDotNet.csproj @@ -33,6 +33,14 @@ true + + true + + + + true + + ResXFileCodeGenerator