Skip to content

Commit d89c73d

Browse files
authored
CryptographicOperations (#561)
1 parent 89df194 commit d89c73d

39 files changed

Lines changed: 8971 additions & 0 deletions

api_list.include.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,40 @@
14611461
* Note: When growing, new elements are default(T); the BCL exposes uninitialized data.
14621462

14631463

1464+
#### CryptographicOperations
1465+
1466+
* `bool FixedTimeEquals(ReadOnlySpan<byte>, byte)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.fixedtimeequals?view=net-11.0#system-security-cryptography-cryptographicoperations-fixedtimeequals(system-readonlyspan((system-byte))-system-byte))
1467+
* Note: Best-effort constant-time comparison; a managed loop cannot offer the same guarantees as the BCL intrinsic.
1468+
* `bool FixedTimeEquals(ReadOnlySpan<byte>, ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.fixedtimeequals?view=net-11.0#system-security-cryptography-cryptographicoperations-fixedtimeequals(system-readonlyspan((system-byte))-system-readonlyspan((system-byte))))
1469+
* Note: Best-effort constant-time comparison; a managed loop cannot offer the same guarantees as the BCL intrinsic.
1470+
* `byte[] HashData(HashAlgorithmName, byte[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hashdata?view=net-11.0#system-security-cryptography-cryptographicoperations-hashdata(system-security-cryptography-hashalgorithmname-system-byte()))
1471+
* `int HashData(HashAlgorithmName, ReadOnlySpan<byte>, Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hashdata?view=net-11.0#system-security-cryptography-cryptographicoperations-hashdata(system-security-cryptography-hashalgorithmname-system-readonlyspan((system-byte))-system-span((system-byte))))
1472+
* `byte[] HashData(HashAlgorithmName, ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hashdata?view=net-11.0#system-security-cryptography-cryptographicoperations-hashdata(system-security-cryptography-hashalgorithmname-system-readonlyspan((system-byte))))
1473+
* `int HashData(HashAlgorithmName, Stream, Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hashdata?view=net-11.0#system-security-cryptography-cryptographicoperations-hashdata(system-security-cryptography-hashalgorithmname-system-io-stream-system-span((system-byte))))
1474+
* `byte[] HashData(HashAlgorithmName, Stream)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hashdata?view=net-11.0#system-security-cryptography-cryptographicoperations-hashdata(system-security-cryptography-hashalgorithmname-system-io-stream))
1475+
* `ValueTask<byte[]> HashDataAsync(HashAlgorithmName, Stream, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hashdataasync?view=net-11.0#system-security-cryptography-cryptographicoperations-hashdataasync(system-security-cryptography-hashalgorithmname-system-io-stream-system-threading-cancellationtoken))
1476+
* `ValueTask<int> HashDataAsync(HashAlgorithmName, Stream, Memory<byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hashdataasync?view=net-11.0#system-security-cryptography-cryptographicoperations-hashdataasync(system-security-cryptography-hashalgorithmname-system-io-stream-system-memory((system-byte))-system-threading-cancellationtoken))
1477+
* `byte[] HmacData(HashAlgorithmName, byte[], byte[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hmacdata?view=net-11.0#system-security-cryptography-cryptographicoperations-hmacdata(system-security-cryptography-hashalgorithmname-system-byte()-system-byte()))
1478+
* `byte[] HmacData(HashAlgorithmName, byte[], Stream)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hmacdata?view=net-11.0#system-security-cryptography-cryptographicoperations-hmacdata(system-security-cryptography-hashalgorithmname-system-byte()-system-io-stream))
1479+
* `int HmacData(HashAlgorithmName, ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hmacdata?view=net-11.0#system-security-cryptography-cryptographicoperations-hmacdata(system-security-cryptography-hashalgorithmname-system-readonlyspan((system-byte))-system-readonlyspan((system-byte))-system-span((system-byte))))
1480+
* `byte[] HmacData(HashAlgorithmName, ReadOnlySpan<byte>, ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hmacdata?view=net-11.0#system-security-cryptography-cryptographicoperations-hmacdata(system-security-cryptography-hashalgorithmname-system-readonlyspan((system-byte))-system-readonlyspan((system-byte))))
1481+
* `int HmacData(HashAlgorithmName, ReadOnlySpan<byte>, Stream, Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hmacdata?view=net-11.0#system-security-cryptography-cryptographicoperations-hmacdata(system-security-cryptography-hashalgorithmname-system-readonlyspan((system-byte))-system-io-stream-system-span((system-byte))))
1482+
* `byte[] HmacData(HashAlgorithmName, ReadOnlySpan<byte>, Stream)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hmacdata?view=net-11.0#system-security-cryptography-cryptographicoperations-hmacdata(system-security-cryptography-hashalgorithmname-system-readonlyspan((system-byte))-system-io-stream))
1483+
* `ValueTask<byte[]> HmacDataAsync(HashAlgorithmName, byte[], Stream, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hmacdataasync?view=net-11.0#system-security-cryptography-cryptographicoperations-hmacdataasync(system-security-cryptography-hashalgorithmname-system-byte()-system-io-stream-system-threading-cancellationtoken))
1484+
* `ValueTask<byte[]> HmacDataAsync(HashAlgorithmName, ReadOnlyMemory<byte>, Stream, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hmacdataasync?view=net-11.0#system-security-cryptography-cryptographicoperations-hmacdataasync(system-security-cryptography-hashalgorithmname-system-readonlymemory((system-byte))-system-io-stream-system-threading-cancellationtoken))
1485+
* `ValueTask<int> HmacDataAsync(HashAlgorithmName, ReadOnlyMemory<byte>, Stream, Memory<byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.hmacdataasync?view=net-11.0#system-security-cryptography-cryptographicoperations-hmacdataasync(system-security-cryptography-hashalgorithmname-system-readonlymemory((system-byte))-system-io-stream-system-memory((system-byte))-system-threading-cancellationtoken))
1486+
* `bool TryHashData(HashAlgorithmName, ReadOnlySpan<byte>, Span<byte>, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.tryhashdata?view=net-11.0)
1487+
* `bool TryHmacData(HashAlgorithmName, ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.tryhmacdata?view=net-11.0)
1488+
* `bool VerifyHmac(HashAlgorithmName, byte[], byte[], byte[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.verifyhmac?view=net-11.0#system-security-cryptography-cryptographicoperations-verifyhmac(system-security-cryptography-hashalgorithmname-system-byte()-system-byte()-system-byte()))
1489+
* `bool VerifyHmac(HashAlgorithmName, byte[], Stream, byte[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.verifyhmac?view=net-11.0#system-security-cryptography-cryptographicoperations-verifyhmac(system-security-cryptography-hashalgorithmname-system-byte()-system-io-stream-system-byte()))
1490+
* `bool VerifyHmac(HashAlgorithmName, ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.verifyhmac?view=net-11.0#system-security-cryptography-cryptographicoperations-verifyhmac(system-security-cryptography-hashalgorithmname-system-readonlyspan((system-byte))-system-readonlyspan((system-byte))-system-readonlyspan((system-byte))))
1491+
* `bool VerifyHmac(HashAlgorithmName, ReadOnlySpan<byte>, Stream, ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.verifyhmac?view=net-11.0#system-security-cryptography-cryptographicoperations-verifyhmac(system-security-cryptography-hashalgorithmname-system-readonlyspan((system-byte))-system-io-stream-system-readonlyspan((system-byte))))
1492+
* `ValueTask<bool> VerifyHmacAsync(HashAlgorithmName, byte[], Stream, byte[], CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.verifyhmacasync?view=net-11.0#system-security-cryptography-cryptographicoperations-verifyhmacasync(system-security-cryptography-hashalgorithmname-system-byte()-system-io-stream-system-byte()-system-threading-cancellationtoken))
1493+
* `ValueTask<bool> VerifyHmacAsync(HashAlgorithmName, ReadOnlyMemory<byte>, Stream, ReadOnlyMemory<byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.verifyhmacasync?view=net-11.0#system-security-cryptography-cryptographicoperations-verifyhmacasync(system-security-cryptography-hashalgorithmname-system-readonlymemory((system-byte))-system-io-stream-system-readonlymemory((system-byte))-system-threading-cancellationtoken))
1494+
* `void ZeroMemory(Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptographicoperations.zeromemory?view=net-11.0)
1495+
* Note: Best-effort zeroing; unlike the BCL intrinsic the JIT may elide the clear if the buffer is not observed afterwards.
1496+
1497+
14641498
#### Utf16
14651499

14661500
* `int IndexOfInvalidSubsequence(ReadOnlySpan<char>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.unicode.utf16.indexofinvalidsubsequence?view=net-11.0)

src/ApiBuilderTests/BuildApiTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public Task RunWithRoslyn()
1919
WriteHelper("Lock", writer, ref count);
2020
WriteHelper(nameof(KeyValuePair), writer, ref count);
2121
WriteHelper("CollectionsMarshal", writer, ref count);
22+
WriteHelper("CryptographicOperations", writer, ref count);
2223
WriteHelper("Utf16", writer, ref count);
2324
WriteType(nameof(TaskCompletionSource), writer, ref count);
2425
WriteType(nameof(UnreachableException), writer, ref count);

src/Consume/Consume.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,53 @@ void MD5Usage()
416416
MD5.HashDataAsync(source: null!, destination: memory);
417417
MD5.HashDataAsync(source: null!, destination: memory, cancellationToken: CancellationToken.None);
418418
#endif
419+
#endif
420+
}
421+
422+
void CryptographicOperationsUsage()
423+
{
424+
var name = HashAlgorithmName.SHA256;
425+
426+
CryptographicOperations.HashData(name, (byte[]) null!);
427+
CryptographicOperations.HashData(name, (Stream) null!);
428+
CryptographicOperations.HmacData(name, (byte[]) null!, (byte[]) null!);
429+
CryptographicOperations.HmacData(name, (byte[]) null!, (Stream) null!);
430+
CryptographicOperations.VerifyHmac(name, (byte[]) null!, (byte[]) null!, (byte[]) null!);
431+
CryptographicOperations.VerifyHmac(name, (byte[]) null!, (Stream) null!, (byte[]) null!);
432+
#if FeatureValueTask
433+
CryptographicOperations.HashDataAsync(name, (Stream) null!, CancellationToken.None);
434+
CryptographicOperations.HmacDataAsync(name, (byte[]) null!, (Stream) null!, CancellationToken.None);
435+
CryptographicOperations.VerifyHmacAsync(name, (byte[]) null!, (Stream) null!, (byte[]) null!, CancellationToken.None);
436+
#endif
437+
#if FeatureMemory
438+
Span<byte> span = default;
439+
ReadOnlySpan<byte> readOnlySpan = default;
440+
Memory<byte> memory = default;
441+
ReadOnlyMemory<byte> readOnlyMemory = default;
442+
443+
CryptographicOperations.FixedTimeEquals(readOnlySpan, readOnlySpan);
444+
CryptographicOperations.FixedTimeEquals(readOnlySpan, (byte) 0);
445+
CryptographicOperations.ZeroMemory(span);
446+
CryptographicOperations.HashData(name, readOnlySpan);
447+
CryptographicOperations.HashData(name, readOnlySpan, span);
448+
CryptographicOperations.HashData(name, (Stream) null!, span);
449+
CryptographicOperations.TryHashData(name, readOnlySpan, span, out _);
450+
CryptographicOperations.HmacData(name, readOnlySpan, readOnlySpan);
451+
CryptographicOperations.HmacData(name, readOnlySpan, readOnlySpan, span);
452+
CryptographicOperations.HmacData(name, readOnlySpan, (Stream) null!);
453+
CryptographicOperations.HmacData(name, readOnlySpan, (Stream) null!, span);
454+
CryptographicOperations.TryHmacData(name, readOnlySpan, readOnlySpan, span, out _);
455+
CryptographicOperations.VerifyHmac(name, readOnlySpan, readOnlySpan, readOnlySpan);
456+
CryptographicOperations.VerifyHmac(name, readOnlySpan, (Stream) null!, readOnlySpan);
457+
#if FeatureValueTask
458+
CryptographicOperations.HashDataAsync(name, (Stream) null!, memory);
459+
CryptographicOperations.HashDataAsync(name, (Stream) null!, memory, CancellationToken.None);
460+
CryptographicOperations.HmacDataAsync(name, readOnlyMemory, (Stream) null!);
461+
CryptographicOperations.HmacDataAsync(name, readOnlyMemory, (Stream) null!, CancellationToken.None);
462+
CryptographicOperations.HmacDataAsync(name, readOnlyMemory, (Stream) null!, memory, CancellationToken.None);
463+
CryptographicOperations.VerifyHmacAsync(name, readOnlyMemory, (Stream) null!, readOnlyMemory);
464+
CryptographicOperations.VerifyHmacAsync(name, readOnlyMemory, (Stream) null!, readOnlyMemory, CancellationToken.None);
465+
#endif
419466
#endif
420467
}
421468

0 commit comments

Comments
 (0)