summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2018-04-08 22:04:29 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2018-04-08 22:04:29 +0700
commit8d43318c18c8da5bc19c77601ad4508aa1b39297 (patch)
tree2c2a177e92ead5b4b1b273af9a21a222ddd18562 /crypto
parentAdded method for looking up name of SBox. (diff)
downloadBouncyCastle.NET-ed25519-8d43318c18c8da5bc19c77601ad4508aa1b39297.tar.xz
Added support for IVs.
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);