1 files changed, 20 insertions, 9 deletions
diff --git a/crypto/src/tls/crypto/impl/TlsNullCipher.cs b/crypto/src/tls/crypto/impl/TlsNullCipher.cs
index 3ca4951a6..b21e46eed 100644
--- a/crypto/src/tls/crypto/impl/TlsNullCipher.cs
+++ b/crypto/src/tls/crypto/impl/TlsNullCipher.cs
@@ -16,20 +16,31 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl
if (TlsImplUtilities.IsTlsV13(cryptoParams))
throw new TlsFatalAlert(AlertDescription.internal_error);
- this.m_cryptoParams = cryptoParams;
+ m_cryptoParams = cryptoParams;
- int key_block_size = clientMac.MacLength + serverMac.MacLength;
- byte[] key_block = TlsImplUtilities.CalculateKeyBlock(cryptoParams, key_block_size);
+ int keyBlockSize = clientMac.MacLength + serverMac.MacLength;
- int offset = 0;
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+ Span<byte> keyBlock = stackalloc byte[keyBlockSize];
+ TlsImplUtilities.CalculateKeyBlock(cryptoParams, keyBlock);
- clientMac.SetKey(key_block, offset, clientMac.MacLength);
- offset += clientMac.MacLength;
- serverMac.SetKey(key_block, offset, serverMac.MacLength);
- offset += serverMac.MacLength;
+ clientMac.SetKey(keyBlock[..clientMac.MacLength]); keyBlock = keyBlock[clientMac.MacLength..];
+ serverMac.SetKey(keyBlock[..serverMac.MacLength]); keyBlock = keyBlock[serverMac.MacLength..];
- if (offset != key_block_size)
+ if (!keyBlock.IsEmpty)
throw new TlsFatalAlert(AlertDescription.internal_error);
+#else
+ byte[] keyBlock = TlsImplUtilities.CalculateKeyBlock(cryptoParams, keyBlockSize);
+ int pos = 0;
+
+ clientMac.SetKey(keyBlock, pos, clientMac.MacLength);
+ pos += clientMac.MacLength;
+ serverMac.SetKey(keyBlock, pos, serverMac.MacLength);
+ pos += serverMac.MacLength;
+
+ if (pos != keyBlockSize)
+ throw new TlsFatalAlert(AlertDescription.internal_error);
+#endif
if (cryptoParams.IsServer)
{
|