Skip to content

Commit

Permalink
Merge pull request #161 from shinji-san/release-v0.10.2
Browse files Browse the repository at this point in the history
Release -  v0.10.2

Changed
- FinitePoint: Use `ReadOnlySpan<char>` for `string` parameters in ctor and methods.

Deprecated
- The Secret Legacy Mode is marked as deprecated and will be removed in one of the next releases.

Fixed
- Fixed NETSDK1187 warning (see #151).  Locale 'de-de' is normalized to the standard locale 'de-DE'.

Resolves:  #161
  • Loading branch information
shinji-san authored Sep 16, 2023
2 parents de63002 + e63a63f commit a0f4dd2
Show file tree
Hide file tree
Showing 21 changed files with 300 additions and 72 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup .NET 6
uses: actions/setup-dotnet@v3.0.3
uses: actions/setup-dotnet@v3.2.0
with:
dotnet-version: 6.0.100
dotnet-version: 6.0.414

- name: Setup .NET 7
uses: actions/setup-dotnet@v3.0.3
uses: actions/setup-dotnet@v3.2.0
with:
dotnet-version: 7.0.100
dotnet-version: 7.0.401

- name: .NET Core SxS
run: |
rsync -a ${DOTNET_ROOT/7.0.100/6.0.100}/* $DOTNET_ROOT/
rsync -a ${DOTNET_ROOT/7.0.401/6.0.414}/* $DOTNET_ROOT/
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ jobs:
DOTNET_CLI_TELEMETRY_OPTOUT: true
strategy:
matrix:
dotnet: [ '6.0.100', '7.0.100' ]
dotnet: [ '6.0.414', '7.0.401' ]
name: Dotnet ${{ matrix.dotnet }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup .NET Core
uses: actions/setup-dotnet@v3.0.3
uses: actions/setup-dotnet@v3.2.0
with:
dotnet-version: ${{ matrix.dotnet }}
- name: Build with dotnet SDK v6.0
if: matrix.dotnet == '6.0.100'
if: matrix.dotnet == '6.0.414'
run: dotnet build --configuration Release SecretSharingDotNet6.sln
- name: Test with dotnet SDK v6.0
if: matrix.dotnet == '6.0.100'
if: matrix.dotnet == '6.0.414'
run: dotnet test --configuration Release SecretSharingDotNet6.sln
- name: Build with dotnet SDK v7.0
if: matrix.dotnet == '7.0.100'
if: matrix.dotnet == '7.0.401'
run: dotnet build --configuration Release SecretSharingDotNet7.sln
- name: Test with dotnet SDK v7.0
if: matrix.dotnet == '7.0.100'
if: matrix.dotnet == '7.0.401'
run: dotnet test --configuration Release SecretSharingDotNet7.sln
12 changes: 6 additions & 6 deletions .github/workflows/dotnetall.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ jobs:
env:
DOTNET_CLI_TELEMETRY_OPTOUT: true
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup .NET 6
uses: actions/setup-dotnet@v3.0.3
uses: actions/setup-dotnet@v3.2.0
with:
dotnet-version: 6.0.100
dotnet-version: 6.0.414

- name: Setup .NET 7
uses: actions/setup-dotnet@v3.0.3
uses: actions/setup-dotnet@v3.2.0
with:
dotnet-version: 7.0.100
dotnet-version: 7.0.401

- name: .NET Core SxS
run: |
rsync -a ${DOTNET_ROOT/7.0.100/6.0.100}/* $DOTNET_ROOT/
rsync -a ${DOTNET_ROOT/7.0.401/6.0.414}/* $DOTNET_ROOT/
- name: Restore
run: dotnet restore SecretSharingDotNet.sln
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dotnetfx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

steps:
- name: checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Nuget.exe
uses: nuget/[email protected]
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/publishing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ jobs:
env:
DOTNET_CLI_TELEMETRY_OPTOUT: true
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup .NET 6
uses: actions/setup-dotnet@v3.0.3
uses: actions/setup-dotnet@v3.2.0
with:
dotnet-version: 6.0.100
dotnet-version: 6.0.414

- name: Setup .NET 7
uses: actions/setup-dotnet@v3.0.3
uses: actions/setup-dotnet@v3.2.0
with:
dotnet-version: 7.0.100
dotnet-version: 7.0.401

- name: .NET Core SxS
run: |
rsync -a ${DOTNET_ROOT/7.0.100/6.0.100}/* $DOTNET_ROOT/
rsync -a ${DOTNET_ROOT/7.0.401/6.0.414}/* $DOTNET_ROOT/
- name: Decrypt large secret
run: ./.github/secrets/decrypt_publisher_snk.sh
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.10.2] - 2023-09-16
### Changed
- FinitePoint: Use `ReadOnlySpan<char>` for `string` parameters in ctor and methods.

### Deprecated
- The Secret Legacy Mode is marked as deprecated and will be removed in one of the next releases.

### Fixed
- Fixed NETSDK1187 warning (see #151). Locale 'de-de' is normalized to the standard locale 'de-DE'.

## [0.10.1] - 2023-05-08
### Fixed
- Fixed BigIntCalculator's Equals method to avoid timing attacks. The slow equal implementation is used now.
Expand Down Expand Up @@ -182,6 +192,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `LICENSE.md`
- Added `README.md`

[0.10.2]: https://github.com/shinji-san/SecretSharingDotNet/compare/v0.10.1...v0.10.2
[0.10.1]: https://github.com/shinji-san/SecretSharingDotNet/compare/v0.10.0...v0.10.1
[0.10.0]: https://github.com/shinji-san/SecretSharingDotNet/compare/v0.9.0...v0.10.0
[0.9.0]: https://github.com/shinji-san/SecretSharingDotNet/compare/v0.8.0...v0.9.0
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ An C# implementation of Shamir's Secret Sharing.
</thead>
<tbody>
<tr>
<td rowspan=9><a href="https://github.com/shinji-san/SecretSharingDotNet/actions?query=workflow%3A%22SecretSharingDotNet+NuGet%22" target="_blank"><img src="https://github.com/shinji-san/SecretSharingDotNet/workflows/SecretSharingDotNet%20NuGet/badge.svg?branch=v0.10.1" alt="SecretSharingDotNet NuGet"/></a></td>
<td rowspan=9><a href="https://badge.fury.io/nu/SecretSharingDotNet" target="_blank"><img src="https://badge.fury.io/nu/SecretSharingDotNet.svg" alt="NuGet Version 0.10.1"/></a></td>
<td rowspan=9><a href="https://github.com/shinji-san/SecretSharingDotNet/tree/v0.10.1" target="_blank"><img src="https://img.shields.io/badge/SecretSharingDotNet-0.10.1-green.svg?logo=github&logoColor=959da5&color=2ebb4e&labelColor=2b3137" alt="Tag"/></a></td>
<td rowspan=9><a href="https://github.com/shinji-san/SecretSharingDotNet/actions?query=workflow%3A%22SecretSharingDotNet+NuGet%22" target="_blank"><img src="https://github.com/shinji-san/SecretSharingDotNet/workflows/SecretSharingDotNet%20NuGet/badge.svg?branch=v0.10.2" alt="SecretSharingDotNet NuGet"/></a></td>
<td rowspan=9><a href="https://badge.fury.io/nu/SecretSharingDotNet" target="_blank"><img src="https://badge.fury.io/nu/SecretSharingDotNet.svg" alt="NuGet Version 0.10.2"/></a></td>
<td rowspan=9><a href="https://github.com/shinji-san/SecretSharingDotNet/tree/v0.10.2" target="_blank"><img src="https://img.shields.io/badge/SecretSharingDotNet-0.10.2-green.svg?logo=github&logoColor=959da5&color=2ebb4e&labelColor=2b3137" alt="Tag"/></a></td>
<td>Standard 2.0</td>
</tr>
<tr>
Expand Down Expand Up @@ -110,10 +110,10 @@ An C# implementation of Shamir's Secret Sharing.

1. Open a console and switch to the directory, containing your project file.

2. Use the following command to install version 0.10.1 of the SecretSharingDotNet package:
2. Use the following command to install version 0.10.2 of the SecretSharingDotNet package:

```dotnetcli
dotnet add package SecretSharingDotNet -v 0.10.1 -f <FRAMEWORK>
dotnet add package SecretSharingDotNet -v 0.10.2 -f <FRAMEWORK>
```
3. After the completition of the command, look at the project file to make sure that the package is successfuly installed.
Expand All @@ -122,7 +122,7 @@ An C# implementation of Shamir's Secret Sharing.
```xml
<ItemGroup>
<PackageReference Include="SecretSharingDotNet" Version="0.10.1" />
<PackageReference Include="SecretSharingDotNet" Version="0.10.2" />
</ItemGroup>
```
## Remove SecretSharingDotNet package
Expand All @@ -146,13 +146,13 @@ Afterwards, use the function `Reconstruction` to re-construct the original secre
The length of the shares is based on the security level. It's possible to pre-define a security level by `ctor` or the `SecurityLevel` property. The pre-defined security level will be overriden, if the secret size is greater than the Mersenne prime, which is calculated by means of the security level. It is not necessary to define a security level for a re-construction.
## Attention: Breaking change - Normal and legacy mode in v0.7.0
## Attention: Breaking change - Normal and legacy mode in v0.7.0 [Deprecated]
Library version 0.7.0 introduces a normal mode and a legacy mode for secrets. The normal mode is the new and default mode. The legacy mode is for backward compatibility.
*Why was the normal mode introduced?*
The normal mode supports positive secret values and also negative secret values like negative integer numbers or byte arrays with most significant byte greater than 0x7F. The legacy mode generates shares that can't be used to reconstruct negative secret values. So the original secret and the reconstructed secret aren't identical for negative secret values (e.g. `BigInetger secret = -2000`). The legacy mode only returns correct results for positive secret values.
The normal mode supports positive secret values and also negative secret values like negative integer numbers or byte arrays with most significant byte greater than 0x7F. The legacy mode generates shares that can't be used to reconstruct negative secret values. So the original secret and the reconstructed secret aren't identical for negative secret values (e.g. `BigInteger secret = -2000`). The legacy mode only returns correct results for positive secret values.
*Mode overview*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// ----------------------------------------------------------------------------
// <copyright file="FinitePoint.cs" company="Private">
// Copyright (c) 2019 All Rights Reserved
// <copyright file="FinitePoint`1.cs" company="Private">
// Copyright (c) 2023 All Rights Reserved
// </copyright>
// <author>Sebastian Walther</author>
// <date>04/20/2019 10:52:28 PM</date>
// <date>05/27/2023 06:05:12 PM</date>
// ----------------------------------------------------------------------------

#region License
Expand Down Expand Up @@ -36,7 +36,11 @@ namespace SecretSharingDotNet.Cryptography
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
#if !NET6_0_OR_GREATER
using System.Text;
#else
using System.Runtime.CompilerServices;
#endif

/// <summary>
/// Represents the support point of the polynomial
Expand Down Expand Up @@ -73,17 +77,33 @@ public FinitePoint(Calculator<TNumber> x, ICollection<Calculator<TNumber>> polyn
/// </summary>
/// <param name="serialized">string representation of the <see cref="FinitePoint{TNumber}"/> struct</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="serialized"/> is <see langword="null"/></exception>
#if NET6_0_OR_GREATER
public FinitePoint(ReadOnlySpan<char> serialized)
#else
public FinitePoint(string serialized)
#endif
{
#if NET6_0_OR_GREATER
if (serialized == null || serialized.IsEmpty)
#else
if (string.IsNullOrWhiteSpace(serialized))
#endif
{
throw new ArgumentNullException(nameof(serialized));
}

string[] s = serialized.Split('-');
#if NET6_0_OR_GREATER
var xReadOnlySpan = serialized[..serialized.IndexOf(SharedSeparator.CoordinateSeparator)];
var yReadOnlySpan = serialized[(serialized.IndexOf(SharedSeparator.CoordinateSeparator) + 1)..];
var numberType = typeof(TNumber);
this.x = Calculator.Create(ToByteArray(xReadOnlySpan), numberType) as Calculator<TNumber>;
this.y = Calculator.Create(ToByteArray(yReadOnlySpan), numberType) as Calculator<TNumber>;
#else
string[] s = serialized.Split(SharedSeparator.CoordinateSeparatorArray);
var numberType = typeof(TNumber);
this.x = Calculator.Create(ToByteArray(s[0]), numberType) as Calculator<TNumber>;
this.y = Calculator.Create(ToByteArray(s[1]), numberType) as Calculator<TNumber>;
#endif
}

/// <summary>
Expand All @@ -95,6 +115,16 @@ public FinitePoint(string serialized)
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "x")]
public FinitePoint(Calculator<TNumber> x, Calculator<TNumber> y)
{
if (x == null)
{
throw new ArgumentNullException(nameof(x));
}

if (y == null)
{
throw new ArgumentNullException(nameof(y));
}

this.x = x;
this.y = y;
}
Expand Down Expand Up @@ -196,7 +226,7 @@ public override bool Equals(object obj)
/// Returns the string representation of the <see cref="FinitePoint{TNumber}"/> structure.
/// </summary>
/// <returns></returns>
public override string ToString() => string.Format(CultureInfo.InvariantCulture, "{0}-{1}", ToHexString(this.x.ByteRepresentation), ToHexString(this.y.ByteRepresentation));
public override string ToString() => string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", ToHexString(this.x.ByteRepresentation), SharedSeparator.CoordinateSeparator.ToString(), ToHexString(this.y.ByteRepresentation));

/// <summary>
/// Evaluates polynomial (coefficient tuple) at x, used to generate a shamir pool.
Expand Down Expand Up @@ -225,8 +255,14 @@ private static Calculator<TNumber> Evaluate(IEnumerable<Calculator<TNumber>> pol
/// <remarks>
/// Based on discussion on <see href="https://stackoverflow.com/questions/623104/byte-to-hex-string/5919521#5919521">stackoverflow</see>
/// </remarks>
#if NET6_0_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
private static string ToHexString(IEnumerable<byte> bytes)
{
#if NET6_0_OR_GREATER
return Convert.ToHexString(bytes as byte[] ?? bytes.ToArray());
#else
byte[] byteArray = bytes as byte[] ?? bytes.ToArray();
var hexRepresentation = new StringBuilder(byteArray.Length * 2);
foreach (byte b in byteArray)
Expand All @@ -236,13 +272,18 @@ private static string ToHexString(IEnumerable<byte> bytes)
}

return hexRepresentation.ToString();
#endif
}

/// <summary>
/// Converts a hexadecimal string to a byte array.
/// </summary>
/// <param name="hexString">hexadecimal string</param>
/// <returns>Returns a byte array</returns>
#if NET6_0_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte[] ToByteArray(ReadOnlySpan<char> hexString) => Convert.FromHexString(hexString);
#else
private static byte[] ToByteArray(string hexString)
{
byte[] bytes = new byte[hexString.Length / 2];
Expand Down Expand Up @@ -281,5 +322,6 @@ private static byte[] ToByteArray(string hexString)

return bytes;
}
#endif
}
}
2 changes: 2 additions & 0 deletions src/Cryptography/Secret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Secret
/// 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>
Expand Down Expand Up @@ -118,6 +119,7 @@ internal static Secret<TNumber> CreateRandom<TNumber>(Calculator<TNumber> prime)
/// <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();
Expand Down
47 changes: 47 additions & 0 deletions src/Cryptography/SharedSeparator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// ----------------------------------------------------------------------------
// <copyright file="SharedSeparator.cs" company="Private">
// Copyright (c) 2023 All Rights Reserved
// </copyright>
// <author>Sebastian Walther</author>
// <date>05/27/2023 06:05:12 PM</date>
// ----------------------------------------------------------------------------

#region License
// ----------------------------------------------------------------------------
// Copyright 2019 Sebastian Walther
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion

namespace SecretSharingDotNet.Cryptography
{
internal static class SharedSeparator
{
/// <summary>
/// The separator between the X and Y coordinate
/// </summary>
internal const char CoordinateSeparator = '-';

/// <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 };
}

}
Loading

0 comments on commit a0f4dd2

Please sign in to comment.