From 55df84d7ae0c22c3459f322d6845e7ce321b379a Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 5 Oct 2022 00:44:55 +0700 Subject: Various span usage in TLS code --- crypto/src/tls/TlsUtilities.cs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'crypto/src/tls/TlsUtilities.cs') diff --git a/crypto/src/tls/TlsUtilities.cs b/crypto/src/tls/TlsUtilities.cs index a417336be..f12198082 100644 --- a/crypto/src/tls/TlsUtilities.cs +++ b/crypto/src/tls/TlsUtilities.cs @@ -309,6 +309,13 @@ namespace Org.BouncyCastle.Tls buf[offset] = (byte)i; } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public static void WriteUint8(int i, Span buf) + { + buf[0] = (byte)i; + } +#endif + public static void WriteUint16(int i, Stream output) { output.WriteByte((byte)(i >> 8)); @@ -321,6 +328,14 @@ namespace Org.BouncyCastle.Tls buf[offset + 1] = (byte)i; } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public static void WriteUint16(int i, Span buf) + { + buf[0] = (byte)(i >> 8); + buf[1] = (byte)i; + } +#endif + public static void WriteUint24(int i, Stream output) { output.WriteByte((byte)(i >> 16)); @@ -409,6 +424,15 @@ namespace Org.BouncyCastle.Tls Array.Copy(data, 0, buf, off + 1, data.Length); } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public static void WriteOpaque8(ReadOnlySpan data, Span buf) + { + CheckUint8(data.Length); + WriteUint8(data.Length, buf); + data.CopyTo(buf[1..]); + } +#endif + public static void WriteOpaque16(byte[] buf, Stream output) { CheckUint16(buf.Length); @@ -826,6 +850,15 @@ namespace Org.BouncyCastle.Tls throw new EndOfStreamException(); } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public static void ReadFully(Span buf, Stream input) + { + int length = buf.Length; + if (length > 0 && length != Streams.ReadFully(input, buf)) + throw new EndOfStreamException(); + } +#endif + public static byte[] ReadOpaque8(Stream input) { short length = ReadUint8(input); @@ -1382,6 +1415,14 @@ namespace Org.BouncyCastle.Tls return secret.DeriveUsingPrf(securityParameters.PrfAlgorithm, asciiLabel, seed, length); } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public static TlsSecret Prf(SecurityParameters securityParameters, TlsSecret secret, + ReadOnlySpan asciiLabel, ReadOnlySpan seed, int length) + { + return secret.DeriveUsingPrf(securityParameters.PrfAlgorithm, asciiLabel, seed, length); + } +#endif + public static byte[] Clone(byte[] data) { return null == data ? null : data.Length == 0 ? EmptyBytes : (byte[])data.Clone(); -- cgit 1.4.1