From 63e9160e93870ca10b7dd0aad6f3e9aef8df948d Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 21 Nov 2023 11:21:44 +0700 Subject: Refactor HmacDsaKCalculator --- crypto/src/crypto/signers/HMacDsaKCalculator.cs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'crypto') 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; } } -- cgit 1.4.1