From bf3678ca81204f8ea9c253cc35a96c6c7ce12b42 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sat, 8 Oct 2022 20:06:43 +0700 Subject: New Spans methods --- crypto/src/crypto/io/CipherStream.cs | 2 +- crypto/src/crypto/modes/KCcmBlockCipher.cs | 2 +- crypto/src/tls/ByteQueue.cs | 4 +++- crypto/src/tls/crypto/impl/bc/BcTlsCrypto.cs | 2 +- crypto/src/util/Spans.cs | 24 ++++++++++++++++++++++++ 5 files changed, 30 insertions(+), 4 deletions(-) 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.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 { /// A queue for bytes. This file could be more optimized. @@ -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 @@ -8,11 +8,35 @@ namespace Org.BouncyCastle.Utilities { internal static class Spans { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void CopyFrom(this Span output, ReadOnlySpan input) + { + input[..output.Length].CopyTo(output); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static Span FromNullable(T[]? array) + { + return array == null ? Span.Empty : array.AsSpan(); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Span FromNullable(T[]? array, int start) { return array == null ? Span.Empty : array.AsSpan(start); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static ReadOnlySpan FromNullableReadOnly(T[]? array) + { + return array == null ? Span.Empty : array.AsSpan(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static ReadOnlySpan FromNullableReadOnly(T[]? array, int start) + { + return array == null ? Span.Empty : array.AsSpan(start); + } } } #endif -- cgit 1.4.1