summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/crypto/macs/GOST28147Mac.cs30
1 files changed, 24 insertions, 6 deletions
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);