private static void SetSBox(Gost28147Mac mac, byte[] sbox) { var field = mac.GetType().GetField("S", BindingFlags.Instance | BindingFlags.NonPublic); var s = (byte[])field.GetValue(mac); sbox.CopyTo(s, 0); }
public static byte[] Compute_gost28147(byte[] paket, byte[] key) { Gost28147Mac gost = new Gost28147Mac(); KeyParameter param = new KeyParameter(key); gost.Init(param); byte[] output = new byte[gost.GetMacSize()]; gost.DoFinal(output, 0); return(output); }
/// <summary> /// IMPLEMENTACIJA JE TAKVA DA VRATI 4 bajta /// </summary> /// <param name="paket"></param> /// <param name="key"></param> /// <returns></returns> public static byte[] Compute_gost28147(byte[] paket, byte[] key) { Gost28147Mac gost = new Gost28147Mac(); KeyParameter param = new KeyParameter(key, 0, key.Length); gost.Init(param); gost.BlockUpdate(key, 0, key.Length); var macS = gost.GetMacSize(); byte[] output = new byte[macS]; int b = gost.DoFinal(output, 0); return(output); }
private static void CheckMac(byte[] key, byte[] cek, byte[] data, byte[] mac) { var m = new Gost28147Mac(); var keyPrm = ParameterUtilities.CreateKeyParameter("GOST", key); m.Init(keyPrm); var cekmac = new byte[4]; m.BlockUpdate(data, 0, data.Length); m.DoFinal(cekmac, 0); if (!mac.SequenceEqual(cekmac)) { throw new CryptographicException("Неверный ПИН-код"); } }
public ITestResult Perform() { // test1 IMac mac = new Gost28147Mac(); KeyParameter key = new KeyParameter(gkeyBytes1); mac.Init(key); mac.BlockUpdate(input3, 0, input3.Length); byte[] outBytes = new byte[4]; mac.DoFinal(outBytes, 0); if (!Arrays.AreEqual(outBytes, output7)) { return(new SimpleTestResult(false, Name + ": Failed test 1 - expected " + Hex.ToHexString(output7) + " got " + Hex.ToHexString(outBytes))); } // test2 key = new KeyParameter(gkeyBytes2); ParametersWithSBox gparam = new ParametersWithSBox(key, Gost28147Engine.GetSBox("E-A")); mac.Init(gparam); mac.BlockUpdate(input4, 0, input4.Length); outBytes = new byte[4]; mac.DoFinal(outBytes, 0); if (!Arrays.AreEqual(outBytes, output8)) { return(new SimpleTestResult(false, Name + ": Failed test 2 - expected " + Hex.ToHexString(output8) + " got " + Hex.ToHexString(outBytes))); } return(new SimpleTestResult(true, Name + ": Okay")); }
private void CheckMac(byte[] cekDecrypted, byte[] kek) { var cekmac = new byte[4]; var mac = new Gost28147Mac(); var key = ParameterUtilities.CreateKeyParameter("GOST", kek); var prms = new ParametersWithIV(key, UKM); mac.Init(prms); SetSBox(mac, Gost28147_SBox); mac.BlockUpdate(cekDecrypted, 0, cekDecrypted.Length); mac.DoFinal(cekmac, 0); for (int i = 0; i < 4; ++i) { if (cekmac[i] != MAC[i]) { throw new CryptographicException("CEK decryption error, MAC values do not match."); } } }