Skip to content

Commit b6fb81c

Browse files
committed
refactor: CryptoArrayPool and CryptoBuffer
1 parent 5cc5307 commit b6fb81c

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

src/CryptoBase/CryptoArrayPool.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ public sealed class CryptoArrayPool<T>(int length) : IDisposable where T : struc
44
{
55
public readonly T[] Array = ArrayPool<T>.Shared.Rent(length);
66

7-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8-
public Span<T> GetSpan()
7+
public Span<T> Span
98
{
10-
return Array.AsSpan(0, length);
9+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
10+
get => Array.AsSpan(0, length);
1111
}
1212

1313
public void Dispose()
1414
{
15-
CryptographicOperations.ZeroMemory(MemoryMarshal.AsBytes(GetSpan()));
15+
CryptographicOperations.ZeroMemory(MemoryMarshal.AsBytes(Span));
1616
ArrayPool<T>.Shared.Return(Array);
1717
}
1818
}

src/CryptoBase/CryptoBuffer.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
namespace CryptoBase;
22

3-
public readonly ref struct CryptoBuffer : IDisposable
3+
public readonly ref struct CryptoBuffer<T> : IDisposable where T : struct
44
{
5-
private readonly byte[]? _buffer;
5+
private readonly T[]? _buffer;
66

7-
public Span<byte> Span { get; }
7+
public Span<T> Span { get; }
88

99
public CryptoBuffer(int length)
1010
{
11-
_buffer = ArrayPool<byte>.Shared.Rent(length);
11+
_buffer = ArrayPool<T>.Shared.Rent(length);
1212
Span = _buffer.AsSpan(0, length);
1313
}
1414

15-
public CryptoBuffer(Span<byte> buffer)
15+
public CryptoBuffer(Span<T> buffer)
1616
{
1717
Span = buffer;
1818
}
1919

2020
public void Dispose()
2121
{
22-
CryptographicOperations.ZeroMemory(Span);
22+
CryptographicOperations.ZeroMemory(MemoryMarshal.AsBytes(Span));
2323

2424
if (_buffer is not null)
2525
{
26-
ArrayPool<byte>.Shared.Return(_buffer);
26+
ArrayPool<T>.Shared.Return(_buffer);
2727
}
2828
}
2929
}

src/CryptoBase/KDF/Hkdf.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public static void DeriveKey(DigestType type, ReadOnlySpan<byte> ikm, Span<byte>
120120

121121
private static void DeriveKeyInternal(DigestType type, int hashLength, ReadOnlySpan<byte> ikm, Span<byte> output, ReadOnlySpan<byte> salt, ReadOnlySpan<byte> info)
122122
{
123-
using CryptoBuffer buffer = new(stackalloc byte[hashLength]);
123+
using CryptoBuffer<byte> buffer = new(stackalloc byte[hashLength]);
124124
Span<byte> prk = buffer.Span;
125125

126126
ExtractInternal(type, ikm, salt, prk);

src/CryptoBase/SymmetricCryptos/BlockCryptoModes/Xts/XtsMode.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ public void Encrypt(ReadOnlySpan<byte> iv, ReadOnlySpan<byte> source, Span<byte>
2525
ArgumentOutOfRangeException.ThrowIfLessThan(source.Length, BlockSize, nameof(source));
2626
ArgumentOutOfRangeException.ThrowIfLessThan(destination.Length, source.Length, nameof(destination));
2727

28-
using CryptoBuffer cryptoBuffer = new(stackalloc byte[BlockSize]);
28+
using CryptoBuffer<byte> cryptoBuffer = new(stackalloc byte[BlockSize]);
2929
Span<byte> tweak = cryptoBuffer.Span;
3030
_tweakCrypto.Encrypt(iv, tweak);
3131
IBlockCrypto crypto = _dataCrypto;
3232

3333
int left = source.Length % BlockSize;
3434
int size = source.Length - left;
3535

36-
using (CryptoBuffer buffer = new(size))
36+
using (CryptoBuffer<byte> buffer = new(size))
3737
{
3838
Span<byte> tweakBuffer = buffer.Span;
3939

@@ -74,15 +74,15 @@ public void Decrypt(ReadOnlySpan<byte> iv, ReadOnlySpan<byte> source, Span<byte>
7474
ArgumentOutOfRangeException.ThrowIfLessThan(source.Length, BlockSize, nameof(source));
7575
ArgumentOutOfRangeException.ThrowIfLessThan(destination.Length, source.Length, nameof(destination));
7676

77-
using CryptoBuffer cryptoBuffer = new(stackalloc byte[BlockSize]);
77+
using CryptoBuffer<byte> cryptoBuffer = new(stackalloc byte[BlockSize]);
7878
Span<byte> tweak = cryptoBuffer.Span;
7979
_tweakCrypto.Encrypt(iv, tweak);
8080
IBlockCrypto crypto = _dataCrypto;
8181

8282
int left = source.Length % BlockSize;
8383
int size = source.Length - left - (BlockSize & (left | -left) >> 31);
8484

85-
using (CryptoBuffer buffer = new(size))
85+
using (CryptoBuffer<byte> buffer = new(size))
8686
{
8787
Span<byte> tweakBuffer = buffer.Span;
8888

@@ -105,7 +105,7 @@ public void Decrypt(ReadOnlySpan<byte> iv, ReadOnlySpan<byte> source, Span<byte>
105105

106106
if (left is not 0)
107107
{
108-
using CryptoBuffer buffer = new(stackalloc byte[BlockSize]);
108+
using CryptoBuffer<byte> buffer = new(stackalloc byte[BlockSize]);
109109
Span<byte> finalTweak = buffer.Span;
110110
tweak.CopyTo(finalTweak);
111111
Gf128Mul(ref finalTweak);

0 commit comments

Comments
 (0)