diff --git a/crypto/src/crypto/io/CipherStream.cs b/crypto/src/crypto/io/CipherStream.cs
index 145fa7fd3..6802e81d7 100644
--- a/crypto/src/crypto/io/CipherStream.cs
+++ b/crypto/src/crypto/io/CipherStream.cs
@@ -219,7 +219,7 @@ namespace Org.BouncyCastle.Crypto.IO
byte[] output = outputSize > 0 ? ArrayPool<byte>.Shared.Rent(outputSize) : null;
try
{
- int length = m_writeCipher.ProcessBytes(buffer, Spans.FromNullable(output, 0));
+ int length = m_writeCipher.ProcessBytes(buffer, Spans.FromNullable(output));
if (length > 0)
{
m_stream.Write(output[..length]);
diff --git a/crypto/src/crypto/modes/KCcmBlockCipher.cs b/crypto/src/crypto/modes/KCcmBlockCipher.cs
index cc4a5060c..5fb8d77c6 100644
--- a/crypto/src/crypto/modes/KCcmBlockCipher.cs
+++ b/crypto/src/crypto/modes/KCcmBlockCipher.cs
@@ -463,7 +463,7 @@ namespace Org.BouncyCastle.Crypto.Modes
? stackalloc byte[macSize]
: new byte[macSize];
- buffer.AsSpan(0, macSize).CopyTo(calculatedMac);
+ calculatedMac.CopyFrom(buffer);
if (!Arrays.ConstantTimeAreEqual(mac.AsSpan(0, macSize), calculatedMac))
throw new InvalidCipherTextException("mac check failed");
diff --git a/crypto/src/tls/ByteQueue.cs b/crypto/src/tls/ByteQueue.cs
index b85106528..74a7104ba 100644
--- a/crypto/src/tls/ByteQueue.cs
+++ b/crypto/src/tls/ByteQueue.cs
@@ -1,6 +1,8 @@
using System;
using System.IO;
+using Org.BouncyCastle.Utilities;
+
namespace Org.BouncyCastle.Tls
{
/// <summary>A queue for bytes. This file could be more optimized.</summary>
@@ -171,7 +173,7 @@ namespace Org.BouncyCastle.Tls
if ((m_available - skip) < buffer.Length)
throw new InvalidOperationException("Not enough data to read");
- m_databuf.AsSpan(m_skipped + skip, buffer.Length).CopyTo(buffer);
+ buffer.CopyFrom(m_databuf.AsSpan(m_skipped + skip));
}
#endif
diff --git a/crypto/src/tls/crypto/impl/bc/BcTlsCrypto.cs b/crypto/src/tls/crypto/impl/bc/BcTlsCrypto.cs
index 79c994fc0..a4964c4b3 100644
--- a/crypto/src/tls/crypto/impl/bc/BcTlsCrypto.cs
+++ b/crypto/src/tls/crypto/impl/bc/BcTlsCrypto.cs
@@ -149,7 +149,7 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC
public override TlsNonceGenerator CreateNonceGenerator(byte[] additionalSeedMaterial)
{
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
- return CreateNonceGenerator(Spans.FromNullable(additionalSeedMaterial, 0));
+ return CreateNonceGenerator(Spans.FromNullableReadOnly(additionalSeedMaterial));
#else
int cryptoHashAlgorithm = CryptoHashAlgorithm.sha256;
IDigest digest = CreateDigest(cryptoHashAlgorithm);
diff --git a/crypto/src/util/Spans.cs b/crypto/src/util/Spans.cs
index 5e1b3737c..fa2b768d9 100644
--- a/crypto/src/util/Spans.cs
+++ b/crypto/src/util/Spans.cs
@@ -9,10 +9,34 @@ namespace Org.BouncyCastle.Utilities
internal static class Spans
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static void CopyFrom<T>(this Span<T> output, ReadOnlySpan<T> input)
+ {
+ input[..output.Length].CopyTo(output);
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static Span<T> FromNullable<T>(T[]? array)
+ {
+ return array == null ? Span<T>.Empty : array.AsSpan();
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Span<T> FromNullable<T>(T[]? array, int start)
{
return array == null ? Span<T>.Empty : array.AsSpan(start);
}
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static ReadOnlySpan<T> FromNullableReadOnly<T>(T[]? array)
+ {
+ return array == null ? Span<T>.Empty : array.AsSpan();
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static ReadOnlySpan<T> FromNullableReadOnly<T>(T[]? array, int start)
+ {
+ return array == null ? Span<T>.Empty : array.AsSpan(start);
+ }
}
}
#endif
|