Skip to content

Commit

Permalink
Dev (#33)
Browse files Browse the repository at this point in the history
+ Added `BouncyCastle.RegisterNetAlgrithms`
+ Added `HashBcSha3_256.SHA3_256`
+ Added `HashBcSha3_384.SHA3_384`
+ Added `HashBcSha3_512.SHA3_512`
+ Added `MacBcHmacSha3_256.HMACSHA3_256`
+ Added `MacBcHmacSha3_384.HMACSHA3_384`
+ Added `MacBcHmacSha3_512.HMACSHA3_512`
+ `BouncyCastleHashAlgorithm` is unsealed now
+ `BouncyCastleHmacAlgorithm` is unsealed now
  • Loading branch information
nd1012 authored Apr 13, 2024
1 parent 61518c4 commit 9d343c9
Show file tree
Hide file tree
Showing 33 changed files with 1,047 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,4 @@ nuget-publish.bat

# Others
**/log.txt
**/TestResults/**/*
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,30 @@ and use the default output length of the `wan24-Crypto` implementations
instead. The `NetShake128/256HashAlgorithmAdapter` can't be replaced for this
reason.

In order to override the .NET default SHA3 hash and HMAC algorithms, you can
call:

```cs
BouncyCastle.RegisterNetAlgorithms();
```

This will use Bouncy Castle SHA3 implementations for the .NET implemented SHA3
hash algorithms:

| Name | Type |
| ---- | ---- |
| SHA3-256 | `HashBcSha3_256.SHA3_256` |
| SHA3-384 | `HashBcSha3_384.SHA3_384` |
| SHA3-512 | `HashBcSha3_512.SHA3_512` |
| HMACSHA3-256 | `MacBcHmacSha3_256.HMACSHA3_256` |
| HMACSHA3-384 | `MacBcHmacSha3_384.HMACSHA3_384` |
| HMACSHA3-512 | `MacBcHmacSha3_512.HMACSHA3_512` |

This step is required, if you want to ensure that whenever a .NET crypto type
or any 3rd party crypto type requests a SHA3 hash or HMAC instance using the
static `HashAlgorithm.Create("NAME")` or `KeyedHashAlgorithm.Create("NAME")`
methods.

### Use as default algorithms

To set Bouncy Castle defaults as `wan24-Crypto` defaults:
Expand Down
15 changes: 0 additions & 15 deletions src/wan24-Crypto-BC Docs/wan24-Crypto-BC Docs.csproj

This file was deleted.

6 changes: 3 additions & 3 deletions src/wan24-Crypto-BC Tests/wan24-Crypto-BC Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
<PackageReference Include="coverlet.collector" Version="6.0.1">
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
5 changes: 0 additions & 5 deletions src/wan24-Crypto-BC.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "wan24-Crypto-BC", "wan24-Cr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "wan24-Crypto-BC Tests", "wan24-Crypto-BC Tests\wan24-Crypto-BC Tests.csproj", "{19293508-BC9C-4429-AB47-270C3C7ACE78}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "wan24-Crypto-BC Docs", "wan24-Crypto-BC Docs\wan24-Crypto-BC Docs.csproj", "{02DC0681-9170-4421-AC4E-801AF746388F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wan24-Core", "..\..\wan24-Core\src\Wan24-Core\Wan24-Core.csproj", "{678B09FC-7C24-4801-AF72-1800E4C7CD38}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "wan24-Crypto", "..\..\wan24-Crypto\src\wan24-Crypto\wan24-Crypto.csproj", "{C77DD1B2-571E-4BA2-94E7-7F9073F084B9}"
Expand Down Expand Up @@ -40,9 +38,6 @@ Global
{19293508-BC9C-4429-AB47-270C3C7ACE78}.Release|Any CPU.Build.0 = Release|Any CPU
{19293508-BC9C-4429-AB47-270C3C7ACE78}.Trunk|Any CPU.ActiveCfg = Trunk|Any CPU
{19293508-BC9C-4429-AB47-270C3C7ACE78}.Trunk|Any CPU.Build.0 = Trunk|Any CPU
{02DC0681-9170-4421-AC4E-801AF746388F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{02DC0681-9170-4421-AC4E-801AF746388F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02DC0681-9170-4421-AC4E-801AF746388F}.Trunk|Any CPU.ActiveCfg = Trunk|Any CPU
{678B09FC-7C24-4801-AF72-1800E4C7CD38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{678B09FC-7C24-4801-AF72-1800E4C7CD38}.Release|Any CPU.ActiveCfg = Release|Any CPU
{678B09FC-7C24-4801-AF72-1800E4C7CD38}.Trunk|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down
13 changes: 13 additions & 0 deletions src/wan24-Crypto-BC/BouncyCastle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,18 @@ public static void ReplaceNetAlgorithms()
break;
}
}

/// <summary>
/// Register (HMAC)SHA3-* algorithms to <see cref="CryptoAppConfig"/> (replace the .NET defaults!)
/// </summary>
public static void RegisterNetAlgrithms()
{
HashBcSha3_256Algorithm.SHA3_256.Register();
HashBcSha3_384Algorithm.SHA3_384.Register();
HashBcSha3_512Algorithm.SHA3_512.Register();
MacBcHmacSha3_256Algorithm.HMACSHA3_256.Register();
MacBcHmacSha3_384Algorithm.HMACSHA3_384.Register();
MacBcHmacSha3_512Algorithm.HMACSHA3_512.Register();
}
}
}
4 changes: 2 additions & 2 deletions src/wan24-Crypto-BC/BouncyCastleHashAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace wan24.Crypto.BC
/// Constructor
/// </remarks>
/// <param name="digest">Digest</param>
public sealed class BouncyCastleHashAlgorithm(IDigest digest) : HashAlgorithm()
public class BouncyCastleHashAlgorithm(IDigest digest) : HashAlgorithm()
{
/// <summary>
/// Digest
/// </summary>
private readonly IDigest Digest = digest;
protected readonly IDigest Digest = digest;

/// <inheritdoc/>
public override int HashSize => Digest.GetDigestSize() << 3;
Expand Down
4 changes: 2 additions & 2 deletions src/wan24-Crypto-BC/BouncyCastleHmacAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace wan24.Crypto.BC
/// Constructor
/// </remarks>
/// <param name="mac">MAC</param>
public sealed class BouncyCastleHmacAlgorithm(IMac mac) : KeyedHashAlgorithm()
public class BouncyCastleHmacAlgorithm(IMac mac) : KeyedHashAlgorithm()
{
/// <summary>
/// MAC
/// </summary>
private readonly IMac Mac = mac;
protected readonly IMac Mac = mac;

/// <inheritdoc/>
public override void Initialize() => Mac.Reset();
Expand Down
13 changes: 12 additions & 1 deletion src/wan24-Crypto-BC/HashBcSha3_256Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ private HashBcSha3_256Algorithm() : base(ALGORITHM_NAME, ALGORITHM_VALUE) { }
public override string DisplayName => DISPLAY_NAME;

/// <inheritdoc/>
protected override HashAlgorithm GetHashAlgorithmInt(CryptoOptions? options) => new BouncyCastleHashAlgorithm(new Sha3Digest(HASH_LENGTH << 3));
protected override HashAlgorithm GetHashAlgorithmInt(CryptoOptions? options) => new SHA3_256();

/// <summary>
/// SHA3-256
/// </summary>
public sealed class SHA3_256() : BouncyCastleHashAlgorithm(new Sha3Digest(HASH_LENGTH << 3))
{
/// <summary>
/// Register to the <see cref="CryptoConfig"/>
/// </summary>
public static void Register() => CryptoConfig.AddAlgorithm(typeof(SHA3_256), ALGORITHM_NAME);
}
}
}
13 changes: 12 additions & 1 deletion src/wan24-Crypto-BC/HashBcSha3_384Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ private HashBcSha3_384Algorithm() : base(ALGORITHM_NAME, ALGORITHM_VALUE) { }
public override string DisplayName => DISPLAY_NAME;

/// <inheritdoc/>
protected override HashAlgorithm GetHashAlgorithmInt(CryptoOptions? options) => new BouncyCastleHashAlgorithm(new Sha3Digest(HASH_LENGTH << 3));
protected override HashAlgorithm GetHashAlgorithmInt(CryptoOptions? options) => new SHA3_384();

/// <summary>
/// SHA3-384
/// </summary>
public sealed class SHA3_384() : BouncyCastleHashAlgorithm(new Sha3Digest(HASH_LENGTH << 3))
{
/// <summary>
/// Register to the <see cref="CryptoConfig"/>
/// </summary>
public static void Register() => CryptoConfig.AddAlgorithm(typeof(SHA3_384), ALGORITHM_NAME);
}
}
}
13 changes: 12 additions & 1 deletion src/wan24-Crypto-BC/HashBcSha3_512Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ private HashBcSha3_512Algorithm() : base(ALGORITHM_NAME, ALGORITHM_VALUE) { }
public override string DisplayName => DISPLAY_NAME;

/// <inheritdoc/>
protected override HashAlgorithm GetHashAlgorithmInt(CryptoOptions? options) => new BouncyCastleHashAlgorithm(new Sha3Digest(HASH_LENGTH << 3));
protected override HashAlgorithm GetHashAlgorithmInt(CryptoOptions? options) => new SHA3_512();

/// <summary>
/// SHA3-512
/// </summary>
public sealed class SHA3_512() : BouncyCastleHashAlgorithm(new Sha3Digest(HASH_LENGTH << 3))
{
/// <summary>
/// Register to the <see cref="CryptoConfig"/>
/// </summary>
public static void Register() => CryptoConfig.AddAlgorithm(typeof(SHA3_512), ALGORITHM_NAME);
}
}
}
20 changes: 16 additions & 4 deletions src/wan24-Crypto-BC/MacBcHmacSha3_256Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,23 @@ private MacBcHmacSha3_256Algorithm() : base(ALGORITHM_NAME, ALGORITHM_VALUE) { }
public override string DisplayName => DISPLAY_NAME;

/// <inheritdoc/>
protected override KeyedHashAlgorithm GetMacAlgorithmInt(byte[] pwd, CryptoOptions? options)
protected override KeyedHashAlgorithm GetMacAlgorithmInt(byte[] pwd, CryptoOptions? options) => new HMACSHA3_256(pwd);

/// <summary>
/// HMACSHA3-256
/// </summary>
public sealed class HMACSHA3_256 : BouncyCastleHmacAlgorithm
{
HMac mac = new(new Sha3Digest(MAC_LENGTH << 3));
mac.Init(new KeyParameter(pwd));
return new BouncyCastleHmacAlgorithm(mac);
/// <summary>
/// Constructor
/// </summary>
/// <param name="pwd">Password</param>
public HMACSHA3_256(byte[] pwd) : base(new HMac(new Sha3Digest(MAC_LENGTH << 3))) => Mac.Init(new KeyParameter(pwd));

/// <summary>
/// Register to the <see cref="CryptoConfig"/>
/// </summary>
public static void Register() => CryptoConfig.AddAlgorithm(typeof(HMACSHA3_256), "HMACSHA3-256");
}
}
}
20 changes: 16 additions & 4 deletions src/wan24-Crypto-BC/MacBcHmacSha3_384Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,23 @@ private MacBcHmacSha3_384Algorithm() : base(ALGORITHM_NAME, ALGORITHM_VALUE) { }
public override string DisplayName => DISPLAY_NAME;

/// <inheritdoc/>
protected override KeyedHashAlgorithm GetMacAlgorithmInt(byte[] pwd, CryptoOptions? options)
protected override KeyedHashAlgorithm GetMacAlgorithmInt(byte[] pwd, CryptoOptions? options) => new HMACSHA3_384(pwd);

/// <summary>
/// HMACSHA3-384
/// </summary>
public sealed class HMACSHA3_384 : BouncyCastleHmacAlgorithm
{
HMac mac = new(new Sha3Digest(MAC_LENGTH << 3));
mac.Init(new KeyParameter(pwd));
return new BouncyCastleHmacAlgorithm(mac);
/// <summary>
/// Constructor
/// </summary>
/// <param name="pwd">Password</param>
public HMACSHA3_384(byte[] pwd) : base(new HMac(new Sha3Digest(MAC_LENGTH << 3))) => Mac.Init(new KeyParameter(pwd));

/// <summary>
/// Register to the <see cref="CryptoConfig"/>
/// </summary>
public static void Register() => CryptoConfig.AddAlgorithm(typeof(HMACSHA3_384), "HMACSHA3-384");
}
}
}
20 changes: 16 additions & 4 deletions src/wan24-Crypto-BC/MacBcHmacSha3_512Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,23 @@ private MacBcHmacSha3_512Algorithm() : base(ALGORITHM_NAME, ALGORITHM_VALUE) { }
public override string DisplayName => DISPLAY_NAME;

/// <inheritdoc/>
protected override KeyedHashAlgorithm GetMacAlgorithmInt(byte[] pwd, CryptoOptions? options)
protected override KeyedHashAlgorithm GetMacAlgorithmInt(byte[] pwd, CryptoOptions? options) => new HMACSHA3_512(pwd);

/// <summary>
/// HMACSHA3-512
/// </summary>
public sealed class HMACSHA3_512 : BouncyCastleHmacAlgorithm
{
HMac mac = new(new Sha3Digest(MAC_LENGTH << 3));
mac.Init(new KeyParameter(pwd));
return new BouncyCastleHmacAlgorithm(mac);
/// <summary>
/// Constructor
/// </summary>
/// <param name="pwd">Password</param>
public HMACSHA3_512(byte[] pwd) : base(new HMac(new Sha3Digest(MAC_LENGTH << 3))) => Mac.Init(new KeyParameter(pwd));

/// <summary>
/// Register to the <see cref="CryptoConfig"/>
/// </summary>
public static void Register() => CryptoConfig.AddAlgorithm(typeof(HMACSHA3_512), "HMACSHA3-512");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

<!DOCTYPE html>

<html lang="en">
<head>
<link rel="stylesheet" href="/Content/Site.css" />
<title>&#39;MIT&#39; reference</title>
</head>
<body>
<div id="main-content">



<h1>MIT License</h1>

<h2>SPDX identifier</h2>
<div id="license-expression">MIT</div>

<h2>License text</h2>

<div class="optional-license-text">
<p>MIT License</p>

</div>
<div class="replaceable-license-text">
<p>Copyright (c) &lt;year&gt; &lt;copyright holders&gt;
</p>

</div>

<p>Permission is hereby granted, free of charge, to any person obtaining a copy of <var class="replaceable-license-text"> this software and
associated documentation files</var> (the &quot;Software&quot;), 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:</p>

<p>The above copyright notice and this permission notice
<var class="optional-license-text"> (including the next paragraph)</var>
shall be included in all copies or substantial
portions of the Software.</p>

<p>THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, 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 <var class="replaceable-license-text"> THE AUTHORS OR COPYRIGHT HOLDERS</var> 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.</p>






<h2>SPDX web page</h2>
<ul>
<li><a href="https://spdx.org/licenses/MIT.html">https://spdx.org/licenses/MIT.html</a></li>
</ul>

<h2>Notice</h2>
<p>This license content is provided by the <a href="https://spdx.dev/">SPDX project</a>. For more information about <b>licenses.nuget.org</b>, see <a href="https://aka.ms/licenses.nuget.org">our documentation</a>.

<p><i>Data pulled from <a href="https://github.com/spdx/license-list-data">spdx/license-list-data</a> on February 9, 2023.</i></p>
</div>
</body>
</html>
21 changes: 21 additions & 0 deletions src/wan24-Crypto-BC/wan24-Crypto-BC licenses/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Andreas Zimmermann, wan24.de

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.
Loading

0 comments on commit 9d343c9

Please sign in to comment.