summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/bcpg/BcpgOutputStream.cs15
-rw-r--r--crypto/src/bcpg/SignaturePacket.cs6
-rw-r--r--crypto/src/bcpg/SignatureSubpacket.cs38
-rw-r--r--crypto/src/bcpg/UserAttributeSubpacket.cs26
-rw-r--r--crypto/src/crypto/util/SshBuilder.cs6
-rw-r--r--crypto/src/pqc/crypto/lms/Composer.cs20
6 files changed, 78 insertions, 33 deletions
diff --git a/crypto/src/bcpg/BcpgOutputStream.cs b/crypto/src/bcpg/BcpgOutputStream.cs
index 5b53bb28d..bbbe486ce 100644
--- a/crypto/src/bcpg/BcpgOutputStream.cs
+++ b/crypto/src/bcpg/BcpgOutputStream.cs
@@ -1,6 +1,7 @@
 using System;
 using System.IO;
 
+using Org.BouncyCastle.Crypto.Utilities;
 using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.IO;
 
@@ -125,11 +126,18 @@ namespace Org.BouncyCastle.Bcpg
             }
             else
             {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                Span<byte> buf = stackalloc byte[5];
+                buf[0] = 0xFF;
+                Pack.UInt32_To_BE((uint)bodyLen, buf, 1);
+                outStr.Write(buf);
+#else
                 outStr.WriteByte(0xff);
                 outStr.WriteByte((byte)(bodyLen >> 24));
                 outStr.WriteByte((byte)(bodyLen >> 16));
                 outStr.WriteByte((byte)(bodyLen >> 8));
                 outStr.WriteByte((byte)bodyLen);
+#endif
             }
         }
 
@@ -169,11 +177,18 @@ namespace Org.BouncyCastle.Bcpg
                     }
                     else
                     {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                        Span<byte> buf = stackalloc byte[5];
+                        buf[0] = (byte)(hdr | 0x02);
+                        Pack.UInt32_To_BE((uint)bodyLen, buf, 1);
+                        this.Write(buf);
+#else
                         this.WriteByte((byte)(hdr | 0x02));
                         this.WriteByte((byte)(bodyLen >> 24));
                         this.WriteByte((byte)(bodyLen >> 16));
                         this.WriteByte((byte)(bodyLen >> 8));
                         this.WriteByte((byte)bodyLen);
+#endif
                     }
                 }
             }
diff --git a/crypto/src/bcpg/SignaturePacket.cs b/crypto/src/bcpg/SignaturePacket.cs
index 305039e1c..ff3e5d3f9 100644
--- a/crypto/src/bcpg/SignaturePacket.cs
+++ b/crypto/src/bcpg/SignaturePacket.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.IO;
 
 using Org.BouncyCastle.Bcpg.Sig;
+using Org.BouncyCastle.Crypto.Utilities;
 using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.Date;
 using Org.BouncyCastle.Utilities.IO;
@@ -265,10 +266,7 @@ namespace Org.BouncyCastle.Bcpg
 
                 byte[] trailer = new byte[5];
 				trailer[0] = (byte)signatureType;
-                trailer[1] = (byte)(time >> 24);
-                trailer[2] = (byte)(time >> 16);
-                trailer[3] = (byte)(time >>  8);
-                trailer[4] = (byte)(time      );
+                Pack.UInt32_To_BE((uint)time, trailer, 1);
                 return trailer;
             }
 
diff --git a/crypto/src/bcpg/SignatureSubpacket.cs b/crypto/src/bcpg/SignatureSubpacket.cs
index f7f82b2a6..12c74bd6e 100644
--- a/crypto/src/bcpg/SignatureSubpacket.cs
+++ b/crypto/src/bcpg/SignatureSubpacket.cs
@@ -1,5 +1,7 @@
+using System;
 using System.IO;
 
+using Org.BouncyCastle.Crypto.Utilities;
 using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Bcpg
@@ -50,35 +52,31 @@ namespace Org.BouncyCastle.Bcpg
         {
             int bodyLen = data.Length + 1;
 
-            if (isLongLength)
+            if (isLongLength || bodyLen > 8383)
             {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                Span<byte> buf = stackalloc byte[5];
+                buf[0] = 0xFF;
+                Pack.UInt32_To_BE((uint)bodyLen, buf, 1);
+                os.Write(buf);
+#else
                 os.WriteByte(0xff);
                 os.WriteByte((byte)(bodyLen >> 24));
                 os.WriteByte((byte)(bodyLen >> 16));
                 os.WriteByte((byte)(bodyLen >> 8));
                 os.WriteByte((byte)bodyLen);
+#endif
+            }
+            else if (bodyLen < 192)
+            {
+                os.WriteByte((byte)bodyLen);
             }
             else
             {
-                if (bodyLen < 192)
-                {
-                    os.WriteByte((byte)bodyLen);
-                }
-                else if (bodyLen <= 8383)
-                {
-                    bodyLen -= 192;
-
-                    os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192));
-                    os.WriteByte((byte)bodyLen);
-                }
-                else
-                {
-                    os.WriteByte(0xff);
-                    os.WriteByte((byte)(bodyLen >> 24));
-                    os.WriteByte((byte)(bodyLen >> 16));
-                    os.WriteByte((byte)(bodyLen >> 8));
-                    os.WriteByte((byte)bodyLen);
-                }
+                bodyLen -= 192;
+
+                os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192));
+                os.WriteByte((byte)bodyLen);
             }
 
             if (critical)
diff --git a/crypto/src/bcpg/UserAttributeSubpacket.cs b/crypto/src/bcpg/UserAttributeSubpacket.cs
index 05f60ac17..246ac0759 100644
--- a/crypto/src/bcpg/UserAttributeSubpacket.cs
+++ b/crypto/src/bcpg/UserAttributeSubpacket.cs
@@ -1,6 +1,7 @@
 using System;
 using System.IO;
 
+using Org.BouncyCastle.Crypto.Utilities;
 using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Bcpg
@@ -43,23 +44,30 @@ namespace Org.BouncyCastle.Bcpg
         {
             int bodyLen = data.Length + 1;
 
-            if (bodyLen < 192 && !longLength)
+            if (longLength || bodyLen > 8383)
             {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                Span<byte> buf = stackalloc byte[5];
+                buf[0] = 0xFF;
+                Pack.UInt32_To_BE((uint)bodyLen, buf, 1);
+                os.Write(buf);
+#else
+                os.WriteByte(0xff);
+                os.WriteByte((byte)(bodyLen >> 24));
+                os.WriteByte((byte)(bodyLen >> 16));
+                os.WriteByte((byte)(bodyLen >> 8));
                 os.WriteByte((byte)bodyLen);
+#endif
             }
-            else if (bodyLen <= 8383 && !longLength)
+            else if (bodyLen < 192)
             {
-                bodyLen -= 192;
-
-                os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192));
                 os.WriteByte((byte)bodyLen);
             }
             else
             {
-                os.WriteByte(0xff);
-                os.WriteByte((byte)(bodyLen >> 24));
-                os.WriteByte((byte)(bodyLen >> 16));
-                os.WriteByte((byte)(bodyLen >> 8));
+                bodyLen -= 192;
+
+                os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192));
                 os.WriteByte((byte)bodyLen);
             }
 
diff --git a/crypto/src/crypto/util/SshBuilder.cs b/crypto/src/crypto/util/SshBuilder.cs
index d631448d5..c2609e3d8 100644
--- a/crypto/src/crypto/util/SshBuilder.cs
+++ b/crypto/src/crypto/util/SshBuilder.cs
@@ -12,10 +12,16 @@ namespace Org.BouncyCastle.Crypto.Utilities
 
         public void U32(uint value)
         {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            Span<byte> buf = stackalloc byte[4];
+            Pack.UInt32_To_BE(value, buf);
+            bos.Write(buf);
+#else
             bos.WriteByte(Convert.ToByte(value >> 24 & 0xFF));
             bos.WriteByte(Convert.ToByte(value >> 16 & 0xFF));
             bos.WriteByte(Convert.ToByte(value >> 8 & 0xFF));
             bos.WriteByte(Convert.ToByte(value & 0xFF));
+#endif
         }
 
         public void WriteMpint(BigInteger n)
diff --git a/crypto/src/pqc/crypto/lms/Composer.cs b/crypto/src/pqc/crypto/lms/Composer.cs
index 6ad044e34..bc54f87ea 100644
--- a/crypto/src/pqc/crypto/lms/Composer.cs
+++ b/crypto/src/pqc/crypto/lms/Composer.cs
@@ -1,5 +1,7 @@
+using System;
 using System.IO;
 
+using Org.BouncyCastle.Crypto.Utilities;
 using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Pqc.Crypto.Lms
@@ -23,25 +25,43 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
 
         public Composer U64Str(long n)
         {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            Span<byte> buf = stackalloc byte[8];
+            Pack.UInt64_To_BE((ulong)n, buf);
+            bos.Write(buf);
+#else
             U32Str((int)(n >> 32));
             U32Str((int)n);
+#endif
             return this;
         }
 
         public Composer U32Str(int n)
         {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            Span<byte> buf = stackalloc byte[4];
+            Pack.UInt32_To_BE((uint)n, buf);
+            bos.Write(buf);
+#else
             bos.WriteByte((byte)(n >> 24));
             bos.WriteByte((byte)(n >> 16));
             bos.WriteByte((byte)(n >> 8));
             bos.WriteByte((byte)n);
+#endif
             return this;
         }
 
         public Composer U16Str(int n)
         {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            Span<byte> buf = stackalloc byte[2];
+            Pack.UInt16_To_BE((ushort)n, buf);
+            bos.Write(buf);
+#else
             n &= 0xFFFF;
             bos.WriteByte((byte)(n >> 8));
             bos.WriteByte((byte)n);
+#endif
             return this;
         }