public void Init(ICipherParameters parameters) { this.Reset(); this.buf = new byte[8]; if (parameters is ParametersWithSBox) { ParametersWithSBox parametersWithSBox = (ParametersWithSBox)parameters; parametersWithSBox.GetSBox().CopyTo(this.S, 0); if (parametersWithSBox.Parameters != null) { this.workingKey = Gost28147Mac.generateWorkingKey(((KeyParameter)parametersWithSBox.Parameters).GetKey()); return; } return; } else { if (parameters is KeyParameter) { this.workingKey = Gost28147Mac.generateWorkingKey(((KeyParameter)parameters).GetKey()); return; } throw new ArgumentException("invalid parameter passed to Gost28147 init - " + parameters.GetType().Name); } }
private static int[] generateWorkingKey(byte[] userKey) { if (userKey.Length != 32) { throw new ArgumentException("Key length invalid. Key needs to be 32 byte - 256 bit!!!"); } int[] array = new int[8]; for (int num = 0; num != 8; num++) { array[num] = Gost28147Mac.bytesToint(userKey, num * 4); } return(array); }
private void gost28147MacFunc(int[] workingKey, byte[] input, int inOff, byte[] output, int outOff) { int num = Gost28147Mac.bytesToint(input, inOff); int num2 = Gost28147Mac.bytesToint(input, inOff + 4); for (int i = 0; i < 2; i++) { for (int j = 0; j < 8; j++) { int num3 = num; num = (num2 ^ this.gost28147_mainStep(num, workingKey[j])); num2 = num3; } } Gost28147Mac.intTobytes(num, output, outOff); Gost28147Mac.intTobytes(num2, output, outOff + 4); }
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"); }
public void Update(byte input) { if (this.bufOff == this.buf.Length) { byte[] array = new byte[this.buf.Length]; Array.Copy(this.buf, 0, array, 0, this.mac.Length); if (this.firstStep) { this.firstStep = false; } else { array = Gost28147Mac.CM5func(this.buf, 0, this.mac); } this.gost28147MacFunc(this.workingKey, array, 0, this.mac, 0); this.bufOff = 0; } this.buf[this.bufOff++] = input; }
public int DoFinal(byte[] output, int outOff) { while (this.bufOff < 8) { this.buf[this.bufOff++] = 0; } byte[] array = new byte[this.buf.Length]; Array.Copy(this.buf, 0, array, 0, this.mac.Length); if (this.firstStep) { this.firstStep = false; } else { array = Gost28147Mac.CM5func(this.buf, 0, this.mac); } this.gost28147MacFunc(this.workingKey, array, 0, this.mac, 0); Array.Copy(this.mac, this.mac.Length / 2 - 4, output, outOff, 4); this.Reset(); return(4); }
public void BlockUpdate(byte[] input, int inOff, int len) { if (len < 0) { throw new ArgumentException("Can't have a negative input length!"); } int num = 8 - this.bufOff; if (len > num) { Array.Copy(input, inOff, this.buf, this.bufOff, num); byte[] array = new byte[this.buf.Length]; Array.Copy(this.buf, 0, array, 0, this.mac.Length); if (this.firstStep) { this.firstStep = false; } else { array = Gost28147Mac.CM5func(this.buf, 0, this.mac); } this.gost28147MacFunc(this.workingKey, array, 0, this.mac, 0); this.bufOff = 0; len -= num; inOff += num; while (len > 8) { array = Gost28147Mac.CM5func(input, inOff, this.mac); this.gost28147MacFunc(this.workingKey, array, 0, this.mac, 0); len -= 8; inOff += 8; } } Array.Copy(input, inOff, this.buf, this.bufOff, len); this.bufOff += len; }