Skip to content

Commit

Permalink
Security Improvements: Introduction of SecureBigInteger
Browse files Browse the repository at this point in the history
Resolves: No entry
  • Loading branch information
shinji-san committed Jun 2, 2024
1 parent b4dbd2d commit 2a6d8b1
Show file tree
Hide file tree
Showing 8 changed files with 612 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/Cryptography/ShamirsSecretSharing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ namespace SecretSharingDotNet.Cryptography;
/// </summary>
public abstract class ShamirsSecretSharing
{
/// <summary>
/// The minimum number of shares required to reconstruct the secret
/// </summary>
protected const int MinimumShareLimit = 2;

/// <summary>
/// Saves the known security levels (Mersenne prime exponents)
/// </summary>
Expand Down
36 changes: 28 additions & 8 deletions src/Cryptography/ShamirsSecretSharing`3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class ShamirsSecretSharing<TNumber, TExtendedGcdAlgorithm, TExtendedGcdRe
public ShamirsSecretSharing(TExtendedGcdAlgorithm extendedGcd)
{
this.extendedGcd = extendedGcd ?? throw new ArgumentNullException(nameof(extendedGcd));
this.SecurityLevel = 13;
this.SecurityLevel = SecurityLevels[0];
}

/// <summary>
Expand All @@ -84,7 +84,7 @@ public int SecurityLevel

set
{
if (value < 13)
if (value < SecurityLevels[0])
{
throw new ArgumentOutOfRangeException(nameof(value), value, ErrorMessages.MinimumSecurityLevelExceeded);
}
Expand Down Expand Up @@ -128,7 +128,7 @@ public Shares<TNumber> MakeShares(TNumber numberOfMinimumShares, TNumber numberO

int min = ((Calculator<TNumber>)numberOfMinimumShares).ToInt32();
int max = ((Calculator<TNumber>)numberOfShares).ToInt32();
if (min < 2)
if (min < MinimumShareLimit)
{
throw new ArgumentOutOfRangeException(nameof(numberOfMinimumShares), numberOfMinimumShares, ErrorMessages.MinNumberOfSharesLowerThanTwo);
}
Expand Down Expand Up @@ -187,7 +187,7 @@ public Shares<TNumber> MakeShares(TNumber numberOfMinimumShares, TNumber numberO
{
int min = ((Calculator<TNumber>)numberOfMinimumShares).ToInt32();
int max = ((Calculator<TNumber>)numberOfShares).ToInt32();
if (min < 2)
if (min < MinimumShareLimit)
{
throw new ArgumentOutOfRangeException(nameof(numberOfMinimumShares), numberOfMinimumShares, ErrorMessages.MinNumberOfSharesLowerThanTwo);
}
Expand Down Expand Up @@ -215,20 +215,40 @@ public Shares<TNumber> MakeShares(TNumber numberOfMinimumShares, TNumber numberO
/// </summary>
/// <param name="numberOfMinimumShares">Minimum number of shared secrets for reconstruction</param>
/// <returns></returns>
#if NET6_0_OR_GREATER
private unsafe Calculator<TNumber>[] CreatePolynomial(int numberOfMinimumShares)
#else
private Calculator<TNumber>[] CreatePolynomial(int numberOfMinimumShares)
#endif
{
var polynomial = new Calculator<TNumber>[numberOfMinimumShares];
polynomial[0] = Calculator<TNumber>.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<byte>(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<TNumber>)?.Abs() % this.mersennePrime;
rng.GetBytes(span);
polynomial[i] = (Calculator.Create(randomNumber, typeof(TNumber)) as Calculator<TNumber>)?.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<TNumber>)?.Abs() %
this.mersennePrime;
}

Array.Clear(randomNumber, 0, randomNumber.Length);
#endif
return polynomial;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Cryptography/SharedSeparator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ internal static class SharedSeparator
/// <summary>
/// Separator array for <see cref="string.Split(char[])"/> method usage to avoid allocation of a new array.
/// </summary>
internal static readonly char[] CoordinateSeparatorArray = { CoordinateSeparator };
}
internal static readonly char[] CoordinateSeparatorArray = [CoordinateSeparator];
}
2 changes: 1 addition & 1 deletion src/Cryptography/Shares.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ internal Shares(Secret<TNumber> secret, IList<FinitePoint<TNumber>> shares)
public static implicit operator Shares<TNumber>(string s)
{
var points = s
.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
.Split([Environment.NewLine], StringSplitOptions.RemoveEmptyEntries)
.Select(line => new FinitePoint<TNumber>(line))
.ToArray();
return new Shares<TNumber>(points);
Expand Down
2 changes: 1 addition & 1 deletion src/Math/Calculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected static Dictionary<Type, Func<TParameter, TCalculator>> 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;
Expand Down
13 changes: 8 additions & 5 deletions src/Math/ExtendedEuclideanAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ public ExtendedGcdResult<TNumber> Compute(Calculator<TNumber> a, Calculator<TNum
checked
{
var quotient = lastR / r;

#if NET6_0_OR_GREATER
(lastR, r) = (r, lastR - quotient * r);
(lastX, x) = (x, lastX - quotient * x);
(lastY, y) = (y, lastY - quotient * y);
#else
var tmpR = r;
r = lastR - quotient * r;
lastR = tmpR;
Expand All @@ -72,11 +76,10 @@ public ExtendedGcdResult<TNumber> Compute(Calculator<TNumber> a, Calculator<TNum
var tmpY = y;
y = lastY - quotient * y;
lastY = tmpY;
#endif
}
}

var coefficients = new[] {lastX, lastY};
var quotients = new[] {x, y};
return new ExtendedGcdResult<TNumber>(lastR, coefficients, quotients);
return new ExtendedGcdResult<TNumber>(lastR, [lastX, lastY], [x, y]);
}
}
}
Loading

0 comments on commit 2a6d8b1

Please sign in to comment.