From 8d43318c18c8da5bc19c77601ad4508aa1b39297 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sun, 8 Apr 2018 22:04:29 +0700 Subject: Added support for IVs. --- crypto/src/crypto/macs/GOST28147Mac.cs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'crypto') diff --git a/crypto/src/crypto/macs/GOST28147Mac.cs b/crypto/src/crypto/macs/GOST28147Mac.cs index cc6b723d6..33c2d67ee 100644 --- a/crypto/src/crypto/macs/GOST28147Mac.cs +++ b/crypto/src/crypto/macs/GOST28147Mac.cs @@ -18,6 +18,7 @@ namespace Org.BouncyCastle.Crypto.Macs private byte[] mac; private bool firstStep = true; private int[] workingKey; + private byte[] macIV = null; // // This is default S-box - E_A. @@ -40,7 +41,7 @@ namespace Org.BouncyCastle.Crypto.Macs bufOff = 0; } - private static int[] generateWorkingKey( + private static int[] GenerateWorkingKey( byte[] userKey) { if (userKey.Length != 32) @@ -60,7 +61,8 @@ namespace Org.BouncyCastle.Crypto.Macs { Reset(); buf = new byte[blockSize]; - if (parameters is ParametersWithSBox) + macIV = null; + if (parameters is ParametersWithSBox) { ParametersWithSBox param = (ParametersWithSBox)parameters; @@ -74,13 +76,21 @@ namespace Org.BouncyCastle.Crypto.Macs // if (param.Parameters != null) { - workingKey = generateWorkingKey(((KeyParameter)param.Parameters).GetKey()); + workingKey = GenerateWorkingKey(((KeyParameter)param.Parameters).GetKey()); } } else if (parameters is KeyParameter) { - workingKey = generateWorkingKey(((KeyParameter)parameters).GetKey()); + workingKey = GenerateWorkingKey(((KeyParameter)parameters).GetKey()); } + else if (parameters is ParametersWithIV) + { + ParametersWithIV p = (ParametersWithIV)parameters; + + workingKey = GenerateWorkingKey(((KeyParameter)p.Parameters).GetKey()); + Array.Copy(p.GetIV(), 0, mac, 0, mac.Length); + macIV = p.GetIV(); // don't skip the initial CM5Func + } else { throw new ArgumentException("invalid parameter passed to Gost28147 init - " @@ -194,7 +204,11 @@ namespace Org.BouncyCastle.Crypto.Macs if (firstStep) { firstStep = false; - } + if (macIV != null) + { + sumbuf = CM5func(buf, 0, macIV); + } + } else { sumbuf = CM5func(buf, 0, mac); @@ -227,7 +241,11 @@ namespace Org.BouncyCastle.Crypto.Macs if (firstStep) { firstStep = false; - } + if (macIV != null) + { + sumbuf = CM5func(buf, 0, macIV); + } + } else { sumbuf = CM5func(buf, 0, mac); -- cgit 1.4.1