Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #23

Merged
merged 3 commits into from
Oct 21, 2023
Merged

Dev #23

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ RND.FillBytesAsync = csrng.GetBytesAsync;
```

**NOTE**: When setting the `RND.FillBytes(Async)` callbacks, they may not be
used, if `/dev/urandom` was preferred. To disable `/dev/urandom`, set
`RND.UseDevUrandom` and `RND.RequireDevUrandom` to `false` also.
used, if `/dev/random` was preferred. To disable `/dev/random`, set
`RND.UseDevRandom` and `RND.RequireDevRandom` to `false` also.

**NOTE**: Currently only stream ciphers are supported, because the cipher RNG
implementation doesn't buffer pre-generated random data.
5 changes: 4 additions & 1 deletion src/wan24-Crypto-BC/BouncyCastleRandomGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public BouncyCastleRandomGenerator() { }
/// <inheritdoc/>
public void AddSeedMaterial(long seed)
{
using RentedArrayRefStruct<byte> buffer = new(sizeof(long));
using RentedArrayRefStruct<byte> buffer = new(sizeof(long), clean: false)
{
Clear = true
};
seed.GetBytes(buffer.Span);
RND.AddSeed(buffer.Span);
}
Expand Down
1 change: 1 addition & 0 deletions src/wan24-Crypto-BC/BouncyCastleRngWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public byte[] GetBytes(in int count)
return res;
}

/// <inheritdoc/>
public Task<byte[]> GetBytesAsync(int count, CancellationToken cancellationToken = default)
{
if (count < 1) return Task.FromResult(Array.Empty<byte>());
Expand Down
4 changes: 2 additions & 2 deletions src/wan24-Crypto-BC/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ RND.FillBytesAsync = csrng.GetBytesAsync;
```

**NOTE**: When setting the `RND.FillBytes(Async)` callbacks, they may not be
used, if `/dev/urandom` was preferred. To disable `/dev/urandom`, set
`RND.UseDevUrandom` and `RND.RequireDevUrandom` to `false` also.
used, if `/dev/random` was preferred. To disable `/dev/random`, set
`RND.UseDevRandom` and `RND.RequireDevRandom` to `false` also.

**NOTE**: Currently only stream ciphers are supported, because the cipher RNG
implementation doesn't buffer pre-generated random data.
9 changes: 4 additions & 5 deletions src/wan24-Crypto-BC/RandomDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace wan24.Crypto.BC
public class RandomDataProvider : RandomDataGenerator, IBouncyCastleRng // Note for myself: Do not try to move this to wan24-Crypto - it won't work...
{
/// <summary>
/// Random number generator
/// Random number generator (will be disposed)
/// </summary>
protected readonly ISeedableRng RNG;
/// <summary>
Expand All @@ -36,7 +36,7 @@ namespace wan24.Crypto.BC
/// <param name="rdp">Random data provider to attach to (will be used for seeding, if available - otherwise fallback to <see cref="RND"/>)</param>
/// <param name="seed">Initial seed length in bytes</param>
/// <param name="workerBufferSize">Worker buffer size in bytes</param>
/// <param name="rng">RNG to use</param>
/// <param name="rng">RNG to use (will be disposed)</param>
public RandomDataProvider(
in int capacity,
in RandomDataProvider? rdp = null,
Expand Down Expand Up @@ -64,7 +64,7 @@ protected RandomDataProvider(in RandomDataProvider? rdp, in int capacity, in int
_OnSeedAsync = new(this);
WorkerBufferSize = workerBufferSize ?? Settings.BufferSize;
UseFallback = false;
UseDevUrandom = false;
UseDevRandom = false;
SeedProvider = rdp;
if (rdp is not null)
{
Expand Down Expand Up @@ -236,12 +236,11 @@ protected virtual async Task InitialSeedAsync(int len, CancellationToken cancell
/// <inheritdoc/>
protected override async Task WorkerAsync()
{
bool isDefaultRng = RND.Generator == this;
using RentedArrayStructSimple<byte> buffer1 = new(WorkerBufferSize, clean: false)
{
Clear = true
};
if (isDefaultRng)// Avoids an endless recursion when using this instance as RND.Generator
if (RND.Generator == this)// Avoids an endless recursion when using this instance as RND.Generator
{
for (; !CancelToken.IsCancellationRequested;)
{
Expand Down
5 changes: 4 additions & 1 deletion src/wan24-Crypto-BC/StreamCipherRng.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ public virtual void AddSeedMaterial(long seed)
{
EnsureUndisposed();
using SemaphoreSyncContext ssc = RngSync;
using RentedArrayRefStruct<byte> buffer = new(sizeof(long));
using RentedArrayRefStruct<byte> buffer = new(sizeof(long), clean: false)
{
Clear = true
};
seed.GetBytes(buffer.Span);
RNG.AddSeed(buffer.Span);
}
Expand Down
4 changes: 1 addition & 3 deletions src/wan24-Crypto-BC/XSalsa20Rng.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Org.BouncyCastle.Crypto.Prng;

namespace wan24.Crypto.BC
namespace wan24.Crypto.BC
{
/// <summary>
/// XSalsa20 CSRNG
Expand Down
6 changes: 3 additions & 3 deletions src/wan24-Crypto-BC/wan24-Crypto-BC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PackageId>wan24-Crypto-BC</PackageId>
<Title>wan24-Crypto-BC</Title>
<Version>1.18.0</Version>
<Version>1.19.0</Version>
<Authors>nd1012</Authors>
<Company>Andreas Zimmermann, wan24.de</Company>
<Product>wan24-Crypto-BC</Product>
Expand All @@ -33,8 +33,8 @@

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.2.1" />
<PackageReference Include="wan24-Core" Version="1.38.1" Condition="'$(Configuration)' != 'Trunk'" />
<PackageReference Include="wan24-Crypto" Version="1.23.0" Condition="'$(Configuration)' != 'Trunk'" />
<PackageReference Include="wan24-Core" Version="1.39.0" Condition="'$(Configuration)' != 'Trunk'" />
<PackageReference Include="wan24-Crypto" Version="1.24.0" Condition="'$(Configuration)' != 'Trunk'" />
<ProjectReference Include="..\..\..\wan24-Core\src\Wan24-Core\Wan24-Core.csproj" Condition="'$(Configuration)' == 'Trunk'" />
<ProjectReference Include="..\..\..\wan24-Crypto\src\wan24-Crypto\wan24-Crypto.csproj" Condition="'$(Configuration)' == 'Trunk'" />
</ItemGroup>
Expand Down