summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-02-28 20:45:20 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-02-28 20:45:20 +0700
commit3a208d8c441470d6dfb580d7594dfcc63e477d6f (patch)
tree71f261fd68ed5837d02ff2cd1435c1695dd58083
parentAsn1InputStream refactoring (diff)
downloadBouncyCastle.NET-ed25519-3a208d8c441470d6dfb580d7594dfcc63e477d6f.tar.xz
Refactoring around ASN.1 DL encodings
-rw-r--r--crypto/src/asn1/Asn1OutputStream.cs11
-rw-r--r--crypto/src/asn1/ConstructedDLEncoding.cs4
-rw-r--r--crypto/src/asn1/ConstructedLazyDLEncoding.cs4
-rw-r--r--crypto/src/asn1/DerInteger.cs5
-rw-r--r--crypto/src/asn1/DerSequence.cs5
-rw-r--r--crypto/src/asn1/PrimitiveEncoding.cs4
-rw-r--r--crypto/src/asn1/PrimitiveEncodingSuffixed.cs4
-rw-r--r--crypto/src/crypto/signers/StandardDsaEncoding.cs5
8 files changed, 23 insertions, 19 deletions
diff --git a/crypto/src/asn1/Asn1OutputStream.cs b/crypto/src/asn1/Asn1OutputStream.cs
index 1363aa676..78cb8e0a8 100644
--- a/crypto/src/asn1/Asn1OutputStream.cs
+++ b/crypto/src/asn1/Asn1OutputStream.cs
@@ -103,11 +103,11 @@ namespace Org.BouncyCastle.Asn1
 #endif
         }
 
-        internal void WriteIdentifier(int tagClass, int tagNo)
+        internal void WriteIdentifier(int flags, int tagNo)
         {
             if (tagNo < 31)
             {
-                WriteByte((byte)(tagClass | tagNo));
+                WriteByte((byte)(flags | tagNo));
                 return;
             }
 
@@ -125,7 +125,7 @@ namespace Org.BouncyCastle.Asn1
                 stack[--pos] = (byte)(tagNo & 0x7F | 0x80);
             }
 
-            stack[--pos] = (byte)(tagClass | 0x1F);
+            stack[--pos] = (byte)(flags | 0x1F);
 
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
             Write(stack[pos..]);
@@ -168,6 +168,11 @@ namespace Org.BouncyCastle.Asn1
             return length;
         }
 
+        internal static int GetLengthOfEncodingDL(int tagNo, int contentsLength)
+        {
+            return GetLengthOfIdentifier(tagNo) + GetLengthOfDL(contentsLength) + contentsLength;
+        }
+
         internal static int GetLengthOfIdentifier(int tagNo)
         {
             if (tagNo < 31)
diff --git a/crypto/src/asn1/ConstructedDLEncoding.cs b/crypto/src/asn1/ConstructedDLEncoding.cs
index f7d8bec5d..01ad042ba 100644
--- a/crypto/src/asn1/ConstructedDLEncoding.cs
+++ b/crypto/src/asn1/ConstructedDLEncoding.cs
@@ -27,9 +27,7 @@ namespace Org.BouncyCastle.Asn1
 
         int IAsn1Encoding.GetLength()
         {
-            return Asn1OutputStream.GetLengthOfIdentifier(m_tagNo)
-                +  Asn1OutputStream.GetLengthOfDL(m_contentsLength)
-                +  m_contentsLength;
+            return Asn1OutputStream.GetLengthOfEncodingDL(m_tagNo, m_contentsLength);
         }
     }
 }
diff --git a/crypto/src/asn1/ConstructedLazyDLEncoding.cs b/crypto/src/asn1/ConstructedLazyDLEncoding.cs
index 3847b465b..ee9d21c66 100644
--- a/crypto/src/asn1/ConstructedLazyDLEncoding.cs
+++ b/crypto/src/asn1/ConstructedLazyDLEncoding.cs
@@ -25,9 +25,7 @@ namespace Org.BouncyCastle.Asn1
 
         int IAsn1Encoding.GetLength()
         {
-            return Asn1OutputStream.GetLengthOfIdentifier(m_tagNo)
-                +  Asn1OutputStream.GetLengthOfDL(m_contentsOctets.Length)
-                +  m_contentsOctets.Length;
+            return Asn1OutputStream.GetLengthOfEncodingDL(m_tagNo, m_contentsOctets.Length);
         }
     }
 }
diff --git a/crypto/src/asn1/DerInteger.cs b/crypto/src/asn1/DerInteger.cs
index 05a790743..663a00169 100644
--- a/crypto/src/asn1/DerInteger.cs
+++ b/crypto/src/asn1/DerInteger.cs
@@ -220,6 +220,11 @@ namespace Org.BouncyCastle.Asn1
             return new DerInteger(contents, false);
         }
 
+        internal static int GetEncodingLength(BigInteger x)
+        {
+            return Asn1OutputStream.GetLengthOfEncodingDL(Asn1Tags.Integer, BigIntegers.GetByteLength(x));
+        }
+
         internal static int IntValue(byte[] bytes, int start, int signExt)
         {
             int length = bytes.Length;
diff --git a/crypto/src/asn1/DerSequence.cs b/crypto/src/asn1/DerSequence.cs
index 01bef61d3..a1d93d0da 100644
--- a/crypto/src/asn1/DerSequence.cs
+++ b/crypto/src/asn1/DerSequence.cs
@@ -86,5 +86,10 @@ namespace Org.BouncyCastle.Asn1
             // NOTE: DLSet is intentional, we don't want sorting
             return new DLSet(false, elements);
         }
+
+        internal static int GetEncodingLength(int contentsLength)
+        {
+            return Asn1OutputStream.GetLengthOfEncodingDL(Asn1Tags.Sequence, contentsLength);
+        }
     }
 }
diff --git a/crypto/src/asn1/PrimitiveEncoding.cs b/crypto/src/asn1/PrimitiveEncoding.cs
index 72c3219ef..70fc36517 100644
--- a/crypto/src/asn1/PrimitiveEncoding.cs
+++ b/crypto/src/asn1/PrimitiveEncoding.cs
@@ -25,9 +25,7 @@ namespace Org.BouncyCastle.Asn1
 
         int IAsn1Encoding.GetLength()
         {
-            return Asn1OutputStream.GetLengthOfIdentifier(m_tagNo)
-                +  Asn1OutputStream.GetLengthOfDL(m_contentsOctets.Length)
-                +  m_contentsOctets.Length;
+            return Asn1OutputStream.GetLengthOfEncodingDL(m_tagNo, m_contentsOctets.Length);
         }
     }
 }
diff --git a/crypto/src/asn1/PrimitiveEncodingSuffixed.cs b/crypto/src/asn1/PrimitiveEncodingSuffixed.cs
index ef0ef49ac..380bf479a 100644
--- a/crypto/src/asn1/PrimitiveEncodingSuffixed.cs
+++ b/crypto/src/asn1/PrimitiveEncodingSuffixed.cs
@@ -28,9 +28,7 @@ namespace Org.BouncyCastle.Asn1
 
         int IAsn1Encoding.GetLength()
         {
-            return Asn1OutputStream.GetLengthOfIdentifier(m_tagNo)
-                +  Asn1OutputStream.GetLengthOfDL(m_contentsOctets.Length)
-                +  m_contentsOctets.Length;
+            return Asn1OutputStream.GetLengthOfEncodingDL(m_tagNo, m_contentsOctets.Length);
         }
     }
 }
diff --git a/crypto/src/crypto/signers/StandardDsaEncoding.cs b/crypto/src/crypto/signers/StandardDsaEncoding.cs
index 8fa195982..44dd29ab1 100644
--- a/crypto/src/crypto/signers/StandardDsaEncoding.cs
+++ b/crypto/src/crypto/signers/StandardDsaEncoding.cs
@@ -46,10 +46,7 @@ namespace Org.BouncyCastle.Crypto.Signers
 
         public virtual int GetMaxEncodingSize(BigInteger n)
         {
-            int encodingLength = BigIntegers.GetByteLength(n);
-            int derIntegerLength = Asn1OutputStream.GetLengthOfDL(encodingLength) + encodingLength;
-            int seqContentsLength = 2 * derIntegerLength;
-            return Asn1OutputStream.GetLengthOfDL(seqContentsLength) + seqContentsLength;
+            return DerSequence.GetEncodingLength(DerInteger.GetEncodingLength(n) * 2);
         }
 
         protected virtual BigInteger CheckValue(BigInteger n, BigInteger x)