summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-11-02 14:30:18 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-11-02 14:30:18 +0700
commiteac2a4f8cf6025b7be8c1e7974785a5f968b4eba (patch)
tree978cb71cc962af812824720f9652e820d17dd9ec
parentRefactoring in Pqc.Crypto.Lms (diff)
downloadBouncyCastle.NET-ed25519-eac2a4f8cf6025b7be8c1e7974785a5f968b4eba.tar.xz
A few span opts.
-rw-r--r--crypto/src/crypto/modes/ChaCha20Poly1305.cs13
-rw-r--r--crypto/src/tls/crypto/impl/bc/BcChaCha20Poly1305.cs9
2 files changed, 21 insertions, 1 deletions
diff --git a/crypto/src/crypto/modes/ChaCha20Poly1305.cs b/crypto/src/crypto/modes/ChaCha20Poly1305.cs
index 299387cdf..2fce81e22 100644
--- a/crypto/src/crypto/modes/ChaCha20Poly1305.cs
+++ b/crypto/src/crypto/modes/ChaCha20Poly1305.cs
@@ -763,6 +763,18 @@ namespace Org.BouncyCastle.Crypto.Modes
 
         private void InitMac()
         {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            Span<byte> firstBlock = stackalloc byte[64];
+            try
+            {
+                mChacha20.ProcessBytes(firstBlock, firstBlock);
+                mPoly1305.Init(new KeyParameter(firstBlock[..32]));
+            }
+            finally
+            {
+                firstBlock.Fill(0x00);
+            }
+#else
             byte[] firstBlock = new byte[64];
             try
             {
@@ -773,6 +785,7 @@ namespace Org.BouncyCastle.Crypto.Modes
             {
                 Array.Clear(firstBlock, 0, 64);
             }
+#endif
         }
 
         private void PadMac(ulong count)
diff --git a/crypto/src/tls/crypto/impl/bc/BcChaCha20Poly1305.cs b/crypto/src/tls/crypto/impl/bc/BcChaCha20Poly1305.cs
index 6b87c100a..f8e36a245 100644
--- a/crypto/src/tls/crypto/impl/bc/BcChaCha20Poly1305.cs
+++ b/crypto/src/tls/crypto/impl/bc/BcChaCha20Poly1305.cs
@@ -106,16 +106,23 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC
         public void SetKey(ReadOnlySpan<byte> key)
         {
             KeyParameter cipherKey = new KeyParameter(key);
-            m_cipher.Init(m_isEncrypting, new ParametersWithIV(cipherKey, Zeroes[..12]));
+            m_cipher.Init(m_isEncrypting, new ParametersWithIV(cipherKey, Zeroes.AsSpan(0, 12)));
         }
 #endif
 
         private void InitMac()
         {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            Span<byte> firstBlock = stackalloc byte[64];
+            m_cipher.ProcessBytes(firstBlock, firstBlock);
+            m_mac.Init(new KeyParameter(firstBlock[..32]));
+            firstBlock.Fill(0x00);
+#else
             byte[] firstBlock = new byte[64];
             m_cipher.ProcessBytes(firstBlock, 0, 64, firstBlock, 0);
             m_mac.Init(new KeyParameter(firstBlock, 0, 32));
             Array.Clear(firstBlock, 0, firstBlock.Length);
+#endif
         }
 
         private void UpdateMac(byte[] buf, int off, int len)