summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2020-07-30 15:33:18 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2020-07-30 15:33:18 +0700
commit5e0b449eae68f6adf1f988f9601a299f3aa6c468 (patch)
treecc622c2de05cd5bd27f1044b6bb614c520d2cd7e
parentEdDSA updates (diff)
downloadBouncyCastle.NET-ed25519-5e0b449eae68f6adf1f988f9601a299f3aa6c468.tar.xz
Misc. updates from bc-java
-rw-r--r--crypto/src/asn1/cmp/PKIHeaderBuilder.cs4
-rw-r--r--crypto/src/asn1/misc/MiscObjectIdentifiers.cs4
-rw-r--r--crypto/src/cms/SignerInformation.cs2
-rw-r--r--crypto/src/math/raw/Mod.cs14
-rw-r--r--crypto/src/util/BigIntegers.cs45
-rw-r--r--crypto/src/util/Integers.cs14
6 files changed, 61 insertions, 22 deletions
diff --git a/crypto/src/asn1/cmp/PKIHeaderBuilder.cs b/crypto/src/asn1/cmp/PKIHeaderBuilder.cs
index 00073c062..d771dda4c 100644
--- a/crypto/src/asn1/cmp/PKIHeaderBuilder.cs
+++ b/crypto/src/asn1/cmp/PKIHeaderBuilder.cs
@@ -64,8 +64,8 @@ namespace Org.BouncyCastle.Asn1.Cmp
 		{
             return SetRecipKID(kid == null ? null : new DerOctetString(kid));
 		}
-		
-		public virtual PkiHeaderBuilder SetRecipKID(DerOctetString kid)
+
+		public virtual PkiHeaderBuilder SetRecipKID(Asn1OctetString kid)
 		{
 			recipKID = kid;
 			return this;
diff --git a/crypto/src/asn1/misc/MiscObjectIdentifiers.cs b/crypto/src/asn1/misc/MiscObjectIdentifiers.cs
index 9a817f5c3..1f101882f 100644
--- a/crypto/src/asn1/misc/MiscObjectIdentifiers.cs
+++ b/crypto/src/asn1/misc/MiscObjectIdentifiers.cs
@@ -52,6 +52,10 @@ namespace Org.BouncyCastle.Asn1.Misc
 
         public static readonly DerObjectIdentifier cast5CBC = new DerObjectIdentifier(Entrust+ ".66.10");
 
+        //
+        // HMAC-SHA1       hMAC-SHA1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
+        //       dod(6) internet(1) security(5) mechanisms(5) 8 1 2 }
+        //
         public static readonly DerObjectIdentifier HMAC_SHA1 = new DerObjectIdentifier("1.3.6.1.5.5.8.1.2");
 
         //
diff --git a/crypto/src/cms/SignerInformation.cs b/crypto/src/cms/SignerInformation.cs
index 3643c6fe3..ea9330db1 100644
--- a/crypto/src/cms/SignerInformation.cs
+++ b/crypto/src/cms/SignerInformation.cs
@@ -88,7 +88,7 @@ namespace Org.BouncyCastle.Cms
         /**
          * Protected constructor. In some cases clients have their own idea about how to encode
          * the signed attributes and calculate the signature. This constructor is to allow developers
-         * to deal with that by extending off the class and overridng methods like getSignedAttributes().
+         * to deal with that by extending off the class and overriding e.g. SignedAttributes property.
          *
          * @param baseInfo the SignerInformation to base this one on.
          */
diff --git a/crypto/src/math/raw/Mod.cs b/crypto/src/math/raw/Mod.cs
index 8d9e8fd21..197b5c82b 100644
--- a/crypto/src/math/raw/Mod.cs
+++ b/crypto/src/math/raw/Mod.cs
@@ -144,7 +144,7 @@ namespace Org.BouncyCastle.Math.Raw
             }
 
             {
-                int zeroes = GetTrailingZeroes(u[0]);
+                int zeroes = Integers.NumberOfTrailingZeros((int)u[0]);
                 if (zeroes > 0)
                 {
                     Nat.ShiftDownBits(uLen, u, zeroes, 0);
@@ -170,17 +170,5 @@ namespace Org.BouncyCastle.Math.Raw
                 Nat.ShiftDownBit(len, x, (uint)xc);
             }
         }
-
-        private static int GetTrailingZeroes(uint x)
-        {
-            Debug.Assert(x != 0);
-            int count = 0;
-            while ((x & 1) == 0)
-            {
-                x >>= 1;
-                ++count;
-            }
-            return count;
-        }
     }
 }
diff --git a/crypto/src/util/BigIntegers.cs b/crypto/src/util/BigIntegers.cs
index 6674750bb..bac5f12c0 100644
--- a/crypto/src/util/BigIntegers.cs
+++ b/crypto/src/util/BigIntegers.cs
@@ -15,7 +15,7 @@ namespace Org.BouncyCastle.Utilities
         /**
         * Return the passed in value as an unsigned byte array.
         *
-        * @param value value to be converted.
+        * @param value the value to be converted.
         * @return a byte array without a leading zero byte if present in the signed encoding.
         */
         public static byte[] AsUnsignedByteArray(
@@ -25,11 +25,11 @@ namespace Org.BouncyCastle.Utilities
         }
 
         /**
-         * Return the passed in value as an unsigned byte array of specified length, zero-extended as necessary.
-         *
-         * @param length desired length of result array.
-         * @param n value to be converted.
-         * @return a byte array of specified length, with leading zeroes as necessary given the size of n.
+         * Return the passed in value as an unsigned byte array of the specified length, padded with
+         * leading zeros as necessary.
+         * @param length the fixed length of the result.
+         * @param n the value to be converted.
+         * @return a byte array padded to a fixed length with leading zeros.
          */
         public static byte[] AsUnsignedByteArray(int length, BigInteger n)
         {
@@ -46,6 +46,39 @@ namespace Org.BouncyCastle.Utilities
             return tmp;
         }
 
+        /**
+         * Write the passed in value as unsigned bytes to the specified buffer range, padded with
+         * leading zeros as necessary.
+         *
+         * @param value
+         *            the value to be converted.
+         * @param buf
+         *            the buffer to which the value is written.
+         * @param off
+         *            the start offset in array <code>buf</code> at which the data is written.
+         * @param len
+         *            the fixed length of data written (possibly padded with leading zeros).
+         */
+        public static void AsUnsignedByteArray(BigInteger value, byte[] buf, int off, int len)
+        {
+            byte[] bytes = value.ToByteArrayUnsigned();
+            if (bytes.Length == len)
+            {
+                Array.Copy(bytes, 0, buf, off, len);
+                return;
+            }
+
+            int start = bytes[0] == 0 ? 1 : 0;
+            int count = bytes.Length - start;
+
+            if (count > len)
+                throw new ArgumentException("standard length exceeded for value");
+
+            int padLen = len - count;
+            Arrays.Fill(buf, off, off + padLen, 0);
+            Array.Copy(bytes, start, buf, off + padLen, count);
+        }
+
         /// <summary>
         /// Creates a Random BigInteger from the secure random of a given bit length.
         /// </summary>
diff --git a/crypto/src/util/Integers.cs b/crypto/src/util/Integers.cs
index bd05a053e..afb4b827f 100644
--- a/crypto/src/util/Integers.cs
+++ b/crypto/src/util/Integers.cs
@@ -19,6 +19,20 @@ namespace Org.BouncyCastle.Utilities
             return n;
         }
 
+        public static int NumberOfTrailingZeros(int i)
+        {
+            if (i == 0)
+                return 32;
+
+            int count = 0;
+            while ((i & 1) == 0)
+            {
+                i >>= 1;
+                ++count;
+            }
+            return count;
+        }
+
         public static int RotateLeft(int i, int distance)
         {
             return (i << distance) ^ (int)((uint)i >> -distance);