summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-11-21 11:21:44 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-11-21 11:21:44 +0700
commit63e9160e93870ca10b7dd0aad6f3e9aef8df948d (patch)
tree2dd71b3d771c86bf5d0721e309344f9785981f78
parentAdd InitAdditionalInput1 to HMacDsaKCalculator (diff)
downloadBouncyCastle.NET-ed25519-63e9160e93870ca10b7dd0aad6f3e9aef8df948d.tar.xz
Refactor HmacDsaKCalculator
-rw-r--r--crypto/src/crypto/signers/HMacDsaKCalculator.cs21
1 files changed, 12 insertions, 9 deletions
diff --git a/crypto/src/crypto/signers/HMacDsaKCalculator.cs b/crypto/src/crypto/signers/HMacDsaKCalculator.cs
index 780d4d465..2559ae3c8 100644
--- a/crypto/src/crypto/signers/HMacDsaKCalculator.cs
+++ b/crypto/src/crypto/signers/HMacDsaKCalculator.cs
@@ -28,8 +28,10 @@ namespace Org.BouncyCastle.Crypto.Signers
         public HMacDsaKCalculator(IDigest digest)
         {
             this.hMac = new HMac(digest);
-            this.V = new byte[hMac.GetMacSize()];
-            this.K = new byte[hMac.GetMacSize()];
+
+            int macSize = hMac.GetMacSize();
+            this.V = new byte[macSize];
+            this.K = new byte[macSize];
         }
 
         public virtual bool IsDeterministic
@@ -46,9 +48,6 @@ namespace Org.BouncyCastle.Crypto.Signers
         {
             this.n = n;
 
-            Arrays.Fill(V, 0x01);
-            Arrays.Fill(K, 0);
-
             BigInteger mInt = BitsToInt(message);
             if (mInt.CompareTo(n) >= 0)
             {
@@ -69,6 +68,9 @@ namespace Org.BouncyCastle.Crypto.Signers
             byte[] m = BigIntegers.AsUnsignedByteArray(size, mInt);
 #endif
 
+            Arrays.Fill(K, 0x00);
+            Arrays.Fill(V, 0x01);
+
             hMac.Init(new KeyParameter(K));
 
             hMac.BlockUpdate(V, 0, V.Length);
@@ -162,13 +164,14 @@ namespace Org.BouncyCastle.Crypto.Signers
 
         private BigInteger BitsToInt(byte[] t)
         {
-            BigInteger v = new BigInteger(1, t);
+            int blen = t.Length * 8;
+            int qlen = n.BitLength;
 
-            if (t.Length * 8 > n.BitLength)
+            BigInteger v = BigIntegers.FromUnsignedByteArray(t);
+            if (blen > qlen)
             {
-                v = v.ShiftRight(t.Length * 8 - n.BitLength);
+                v = v.ShiftRight(blen - qlen);
             }
-
             return v;
         }
     }