Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
3263e61
Constant `stackalloc` can be optimized by the JIT.
ycrumeyrolle Jul 22, 2021
a708a36
Add message to test
ycrumeyrolle Jul 26, 2021
e58054d
Revert some stackalloc conditions
ycrumeyrolle Jul 27, 2021
e8b833e
Update TFM for tests
ycrumeyrolle Jul 27, 2021
ed1c2a2
Rollback
ycrumeyrolle Jul 27, 2021
61cac22
Avoid a stackalloc of 512 bytes
ycrumeyrolle Jul 27, 2021
749d078
Fix build
ycrumeyrolle Jul 27, 2021
e4634df
Fix Linux tests
ycrumeyrolle Jul 27, 2021
4a64657
Add debug infos
ycrumeyrolle Jul 27, 2021
a647214
Test
ycrumeyrolle Jul 27, 2021
6fb0841
Update tests
ycrumeyrolle Jul 27, 2021
c5b1723
Fix ?
ycrumeyrolle Jul 27, 2021
8ad28c3
Adds debug info 2
ycrumeyrolle Jul 27, 2021
a23c0e2
fix test
ycrumeyrolle Jul 27, 2021
8a62ca1
Test
ycrumeyrolle Jul 27, 2021
6bf8da3
Try to fix
ycrumeyrolle Jul 27, 2021
f0c76ef
test
ycrumeyrolle Jul 27, 2021
842192d
test
ycrumeyrolle Jul 27, 2021
8ff1c0d
Update dotnetcore.yml
ycrumeyrolle Jul 27, 2021
6ebbee7
Update dotnetcore.yml
ycrumeyrolle Jul 27, 2021
24559a9
test
ycrumeyrolle Jul 27, 2021
dbea385
Merge branch 'stackalloc-constant' of https://github.com/uruk-project…
ycrumeyrolle Jul 27, 2021
5fdf868
test
ycrumeyrolle Jul 27, 2021
7b03811
test
ycrumeyrolle Jul 28, 2021
9e801cf
test
ycrumeyrolle Jul 28, 2021
3f718a1
test
ycrumeyrolle Jul 28, 2021
d386dd1
Finaly...
ycrumeyrolle Jul 28, 2021
39724ce
Add ES256K tests
ycrumeyrolle Jul 29, 2021
e0f7ad9
Simplify `ECParameters` export
ycrumeyrolle Jul 29, 2021
ef7b1e0
Update dotnetcore.yml
ycrumeyrolle Jul 29, 2021
2e14172
`EllipticalCurve` is now a class instead of a readonly struct.
ycrumeyrolle Aug 2, 2021
8f0216e
Merge branch 'stackalloc-constant' of https://github.com/uruk-project…
ycrumeyrolle Aug 3, 2021
7baf086
Remove ES256K test from MacOS ests
ycrumeyrolle Aug 3, 2021
94cb922
Fix build
ycrumeyrolle Aug 3, 2021
88aa30e
Dammit...
ycrumeyrolle Aug 3, 2021
45247f0
Next try
ycrumeyrolle Aug 3, 2021
a3f0ffc
next...
ycrumeyrolle Aug 3, 2021
1abd8c4
fix?
ycrumeyrolle Aug 3, 2021
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
Prev Previous commit
Next Next commit
Avoid a stackalloc of 512 bytes
  • Loading branch information
ycrumeyrolle committed Jul 27, 2021
commit 61cac2246837dbbdf9fecc77246835586cec780f
22 changes: 18 additions & 4 deletions src/JsonWebToken/Cryptography/EcdsaSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#if SUPPORT_ELLIPTIC_CURVE_SIGNATURE
using System;
using System.Buffers;
using System.Diagnostics;
using System.Security.Cryptography;

Expand Down Expand Up @@ -45,9 +46,22 @@ public override bool TrySign(ReadOnlySpan<byte> data, Span<byte> destination, ou

var ecdsa = _ecdsaPool.Get();
#if SUPPORT_SPAN_CRYPTO
Span<byte> hash = stackalloc byte[Sha2.BlockSizeStackallocThreshold].Slice(0, _sha.HashSize);
_sha.ComputeHash(data, hash);
return ecdsa.TrySignHash(hash, destination, out bytesWritten);
byte[]? array = null;
try
{
Span<byte> hash = _sha.HashSize < Sha2.BlockSizeStackallocThreshold ?
stackalloc byte[Sha2.BlockSizeStackallocThreshold].Slice(0, _sha.HashSize)
: (array = ArrayPool<byte>.Shared.Rent(_sha.HashSize)).AsSpan(0, _sha.HashSize);
_sha.ComputeHash(data, hash);
return ecdsa.TrySignHash(hash, destination, out bytesWritten);
}
finally
{
if (array != null)
{
ArrayPool<byte>.Shared.Return(array);
}
}
#else
byte[] hash = new byte[_sha.HashSize];
_sha.ComputeHash(data, hash);
Expand All @@ -72,6 +86,6 @@ protected override void Dispose(bool disposing)
}
}

}
}
}
#endif
2 changes: 1 addition & 1 deletion src/JsonWebToken/Cryptography/Sha2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class Sha2
public abstract int BlockSize { get; }

internal const int HashSizeStackallocThreshold = Sha512.Sha512HashSize;
internal const int BlockSizeStackallocThreshold = Sha512.Sha512BlockSize;
internal const int BlockSizeStackallocThreshold = Sha256.Sha512BlockSize;

#if SUPPORT_SIMD
private static ReadOnlySpan<byte> LittleEndianUInt64 => new byte[32]
Expand Down