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); } }
public virtual void Init(bool forEncryption, ICipherParameters parameters) { if (parameters is ParametersWithSBox) { ParametersWithSBox parametersWithSBox = (ParametersWithSBox)parameters; byte[] sBox = parametersWithSBox.GetSBox(); if (sBox.Length != Sbox_Default.Length) { throw new ArgumentException("invalid S-box passed to GOST28147 init"); } S = Arrays.Clone(sBox); if (parametersWithSBox.Parameters != null) { workingKey = generateWorkingKey(forEncryption, ((KeyParameter)parametersWithSBox.Parameters).GetKey()); } } else if (parameters is KeyParameter) { workingKey = generateWorkingKey(forEncryption, ((KeyParameter)parameters).GetKey()); } else if (parameters != null) { throw new ArgumentException("invalid parameter passed to Gost28147 init - " + Platform.GetTypeName(parameters)); } }
public void Init( ICipherParameters parameters) { Reset(); buf = new byte[blockSize]; if (parameters is ParametersWithSBox) { ParametersWithSBox param = (ParametersWithSBox)parameters; // // Set the S-Box // param.GetSBox().CopyTo(this.S, 0); // // set key if there is one // if (param.Parameters != null) { workingKey = generateWorkingKey(((KeyParameter)param.Parameters).GetKey()); } } else if (parameters is KeyParameter) { workingKey = generateWorkingKey(((KeyParameter)parameters).GetKey()); } else { throw new ArgumentException("invalid parameter passed to Gost28147 init - " + Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters)); } }
public virtual void Init(bool forEncryption, ICipherParameters parameters) { if (parameters is ParametersWithSBox) { ParametersWithSBox parametersWithSBox = (ParametersWithSBox)parameters; byte[] sBox = parametersWithSBox.GetSBox(); if (sBox.Length != Gost28147Engine.Sbox_Default.Length) { throw new ArgumentException("invalid S-box passed to GOST28147 init"); } this.S = Arrays.Clone(sBox); if (parametersWithSBox.Parameters != null) { this.workingKey = this.generateWorkingKey(forEncryption, ((KeyParameter)parametersWithSBox.Parameters).GetKey()); return; } } else { if (parameters is KeyParameter) { this.workingKey = this.generateWorkingKey(forEncryption, ((KeyParameter)parameters).GetKey()); return; } if (parameters != null) { throw new ArgumentException("invalid parameter passed to Gost28147 init - " + parameters.GetType().Name); } } }
/** * initialise an Gost28147 cipher. * * @param forEncryption whether or not we are for encryption. * @param parameters the parameters required to set up the cipher. * @exception ArgumentException if the parameters argument is inappropriate. */ public void Init( bool forEncryption, ICipherParameters parameters) { if (parameters is ParametersWithSBox) { ParametersWithSBox param = (ParametersWithSBox)parameters; // // Set the S-Box // Array.Copy(param.GetSBox(), 0, this.S, 0, param.GetSBox().Length); // // set key if there is one // if (param.Parameters != null) { workingKey = generateWorkingKey(forEncryption, ((KeyParameter)param.Parameters).GetKey()); } } else if (parameters is KeyParameter) { workingKey = generateWorkingKey(forEncryption, ((KeyParameter)parameters).GetKey()); } else { throw new ArgumentException("invalid parameter passed to Gost28147 init - " + parameters.GetType().Name); } }
private static byte[] DecryptKey(byte[] key, byte[] cek, byte[] iv = null) { var cipher = CipherUtilities.GetCipher("GOST/CFB/NOPADDING"); ICipherParameters prms = ParameterUtilities.CreateKeyParameter("GOST", key); prms = new ParametersWithSBox(prms, Gost28147Engine.GetSBox("E-A")); cipher.Init(false, iv == null ? prms : new ParametersWithIV(prms, iv)); return(cipher.ProcessBytes(cek)); }
private BigInteger DecodePrimaryKey(byte[] decodeKey, byte[] primaryKey) { var engine = new Gost28147Engine(); var param = new ParametersWithSBox( new KeyParameter(decodeKey), Gost28147Engine.GetSBox("E-A")); engine.Init(false, param); var buf = new byte[32]; engine.ProcessBlock(primaryKey, 0, buf, 0); engine.ProcessBlock(primaryKey, 8, buf, 8); engine.ProcessBlock(primaryKey, 16, buf, 16); engine.ProcessBlock(primaryKey, 24, buf, 24); return(new BigInteger(1, buf.Reverse().ToArray())); }
// https://tools.ietf.org/html/rfc4357#section-6.5 protected override byte[] KEKDiversification(byte[] kek, byte[] ukm) { var cipher = CipherUtilities.GetCipher("GOST/CFB/NOPADDING"); var result = new byte[32]; Array.Copy(kek, result, 32); var S = new byte[8]; for (int i = 0; i < 8; ++i) { int sum1 = 0; int sum2 = 0; for (int j = 0, mask = 1; j < 8; ++j, mask <<= 1) { var kj = (result[4 * j]) | (result[4 * j + 1] << 8) | (result[4 * j + 2] << 16) | (result[4 * j + 3] << 24); if ((mask & ukm[i]) != 0) { sum1 += kj; } else { sum2 += kj; } } S[0] = (byte)(sum1 & 0xff); S[1] = (byte)((sum1 >> 8) & 0xff); S[2] = (byte)((sum1 >> 16) & 0xff); S[3] = (byte)((sum1 >> 24) & 0xff); S[4] = (byte)(sum2 & 0xff); S[5] = (byte)((sum2 >> 8) & 0xff); S[6] = (byte)((sum2 >> 16) & 0xff); S[7] = (byte)((sum2 >> 24) & 0xff); var key = ParameterUtilities.CreateKeyParameter("GOST", result); var sbox = new ParametersWithSBox(key, Gost28147Engine.GetSBox("E-A")); var prms = new ParametersWithIV(sbox, S); cipher.Init(true, prms); result = cipher.ProcessBytes(result); } return(result); }
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 Init( ICipherParameters parameters) { Reset(); buf = new byte[blockSize]; macIV = null; if (parameters is ParametersWithSBox) { ParametersWithSBox param = (ParametersWithSBox)parameters; // // Set the S-Box // param.GetSBox().CopyTo(this.S, 0); // // set key if there is one // if (param.Parameters != null) { workingKey = GenerateWorkingKey(((KeyParameter)param.Parameters).GetKey()); } } else if (parameters is KeyParameter) { 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 - " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters)); } }
public void Init(ICipherParameters parameters) { this.Reset(); this.buf = new byte[8]; if (parameters is ParametersWithSBox) { ParametersWithSBox box = (ParametersWithSBox)parameters; box.GetSBox().CopyTo(this.S, 0); if (box.Parameters != null) { this.workingKey = generateWorkingKey(((KeyParameter)box.Parameters).GetKey()); } } else { if (!(parameters is KeyParameter)) { throw new ArgumentException("invalid parameter passed to Gost28147 init - " + Platform.GetTypeName(parameters)); } this.workingKey = generateWorkingKey(((KeyParameter)parameters).GetKey()); } }
private BigInteger DecodePrimaryKey(byte[] decodeKey, byte[] primaryKey) { Gost28147Engine engine = new Gost28147Engine(); byte[] sbox = ProviderType == ProviderType.CryptoPro_2001 ? Gost28147Engine.GetSBox("E-A") : Gost28147_TC26ParamSetZ; ParametersWithSBox param = new ParametersWithSBox( new KeyParameter(decodeKey), sbox); engine.Init(false, param); byte[] buf = new byte[primaryKey.Length]; for (int i = 0; i < primaryKey.Length; i += 8) { engine.ProcessBlock(primaryKey, i, buf, i); } return(new BigInteger(1, buf.Reverse().ToArray())); }
/** * initialise an Gost28147 cipher. * * @param forEncryption whether or not we are for encryption. * @param parameters the parameters required to set up the cipher. * @exception ArgumentException if the parameters argument is inappropriate. */ public virtual void Init( bool forEncryption, ICipherParameters parameters) { if (parameters is ParametersWithSBox) { ParametersWithSBox param = (ParametersWithSBox)parameters; // // Set the S-Box // byte[] sBox = param.GetSBox(); if (sBox.Length != Sbox_Default.Length) { throw new ArgumentException("invalid S-box passed to GOST28147 init"); } this.S = Arrays.Clone(sBox); // // set key if there is one // if (param.Parameters != null) { workingKey = generateWorkingKey(forEncryption, ((KeyParameter)param.Parameters).GetKey()); } } else if (parameters is KeyParameter) { workingKey = generateWorkingKey(forEncryption, ((KeyParameter)parameters).GetKey()); } else if (parameters != null) { throw new ArgumentException("invalid parameter passed to Gost28147 init - " + Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters)); } }
public void Init(ICipherParameters parameters) { //IL_0086: Unknown result type (might be due to invalid IL or missing references) Reset(); buf = new byte[8]; if (parameters is ParametersWithSBox) { ParametersWithSBox parametersWithSBox = (ParametersWithSBox)parameters; ((global::System.Array)parametersWithSBox.GetSBox()).CopyTo((global::System.Array)S, 0); if (parametersWithSBox.Parameters != null) { workingKey = generateWorkingKey(((KeyParameter)parametersWithSBox.Parameters).GetKey()); } } else { if (!(parameters is KeyParameter)) { throw new ArgumentException("invalid parameter passed to Gost28147 init - " + Platform.GetTypeName(parameters)); } workingKey = generateWorkingKey(((KeyParameter)parameters).GetKey()); } }
public void Init( ICipherParameters parameters) { Reset(); buf = new byte[blockSize]; if (parameters is ParametersWithSBox) { ParametersWithSBox param = (ParametersWithSBox)parameters; // // Set the S-Box // param.GetSBox().CopyTo(this.S, 0); // // set key if there is one // if (param.Parameters != null) { workingKey = generateWorkingKey(((KeyParameter)param.Parameters).GetKey()); } } else if (parameters is KeyParameter) { workingKey = generateWorkingKey(((KeyParameter)parameters).GetKey()); } else if (parameters is ParametersWithIV) { ParametersWithIV param = (ParametersWithIV)parameters; iv = (byte[])param.GetIV().Clone(); workingKey = generateWorkingKey(((KeyParameter)param.Parameters).GetKey()); } else { throw new ArgumentException("invalid parameter passed to Gost28147 init"); } }
public override void PerformTest() { base.PerformTest(); //advanced tests with Gost28147KeyGenerator: //encrypt on hesh message; ECB mode: byte[] inBytes = Hex.Decode("4e6f77206973207468652074696d6520666f7220616c6c20"); byte[] output = Hex.Decode("8ad3c8f56b27ff1fbd46409359bdc796bc350e71aac5f5c0"); byte[] outBytes = new byte[inBytes.Length]; byte[] key = generateKey(Hex.Decode("0123456789abcdef")); //!!! heshing start_key - get 256 bits !!! // System.out.println(new string(Hex.Encode(key))); ICipherParameters param = new ParametersWithSBox(new KeyParameter(key), Gost28147Engine.GetSBox("E-A")); //CipherParameters param = new Gost28147Parameters(key,"D-Test"); BufferedBlockCipher cipher = new BufferedBlockCipher(new Gost28147Engine()); cipher.Init(true, param); int len1 = cipher.ProcessBytes(inBytes, 0, inBytes.Length, outBytes, 0); try { cipher.DoFinal(outBytes, len1); } catch (CryptoException e) { Fail("failed - exception " + e.ToString(), e); } if (outBytes.Length != output.Length) { Fail("failed - " + "expected " + Hex.ToHexString(output) + " got " + Hex.ToHexString(outBytes)); } for (int i = 0; i != outBytes.Length; i++) { if (outBytes[i] != output[i]) { Fail("failed - " + "expected " + Hex.ToHexString(output) + " got " + Hex.ToHexString(outBytes)); } } //encrypt on hesh message; CFB mode: inBytes = Hex.Decode("bc350e71aac5f5c2"); output = Hex.Decode("0ebbbafcf38f14a5"); outBytes = new byte[inBytes.Length]; key = generateKey(Hex.Decode("0123456789abcdef")); //!!! heshing start_key - get 256 bits !!! param = new ParametersWithIV( new ParametersWithSBox( new KeyParameter(key), //key Gost28147Engine.GetSBox("E-A")), //type S-box Hex.Decode("1234567890abcdef")); //IV cipher = new BufferedBlockCipher(new CfbBlockCipher(new Gost28147Engine(), 64)); cipher.Init(true, param); len1 = cipher.ProcessBytes(inBytes, 0, inBytes.Length, outBytes, 0); try { cipher.DoFinal(outBytes, len1); } catch (CryptoException e) { Fail("failed - exception " + e.ToString(), e); } if (outBytes.Length != output.Length) { Fail("failed - " + "expected " + Hex.ToHexString(output) + " got " + Hex.ToHexString(outBytes)); } for (int i = 0; i != outBytes.Length; i++) { if (outBytes[i] != output[i]) { Fail("failed - " + "expected " + Hex.ToHexString(output) + " got " + Hex.ToHexString(outBytes)); } } //encrypt on hesh message; CFB mode: inBytes = Hex.Decode("000102030405060708090a0b0c0d0e0fff0102030405060708090a0b0c0d0e0f"); output = Hex.Decode("64988982819f0a1655e226e19ecad79d10cc73bac95c5d7da034786c12294225"); outBytes = new byte[inBytes.Length]; key = generateKey(Hex.Decode("aafd12f659cae63489b479e5076ddec2f06cb58faafd12f659cae63489b479e5")); //!!! heshing start_key - get 256 bits !!! param = new ParametersWithIV( new ParametersWithSBox( new KeyParameter(key), //key Gost28147Engine.GetSBox("E-A")), //type S-box Hex.Decode("aafd12f659cae634")); //IV cipher = new BufferedBlockCipher(new CfbBlockCipher(new Gost28147Engine(), 64)); cipher.Init(true, param); len1 = cipher.ProcessBytes(inBytes, 0, inBytes.Length, outBytes, 0); cipher.DoFinal(outBytes, len1); if (outBytes.Length != output.Length) { Fail("failed - " + "expected " + Hex.ToHexString(output) + " got " + Hex.ToHexString(outBytes)); } for (int i = 0; i != outBytes.Length; i++) { if (outBytes[i] != output[i]) { Fail("failed - " + "expected " + Hex.ToHexString(output) + " got " + Hex.ToHexString(outBytes)); } } //encrypt on hesh message; OFB mode: inBytes = Hex.Decode("bc350e71aa11345709acde"); output = Hex.Decode("1bcc2282707c676fb656dc"); outBytes = new byte[inBytes.Length]; key = generateKey(Hex.Decode("0123456789abcdef")); //!!! heshing start_key - get 256 bits !!! param = new ParametersWithIV( new ParametersWithSBox( new KeyParameter(key), //key Gost28147Engine.GetSBox("E-A")), //type S-box Hex.Decode("1234567890abcdef")); //IV cipher = new BufferedBlockCipher(new GOfbBlockCipher(new Gost28147Engine())); cipher.Init(true, param); len1 = cipher.ProcessBytes(inBytes, 0, inBytes.Length, outBytes, 0); cipher.DoFinal(outBytes, len1); if (outBytes.Length != output.Length) { Fail("failed - " + "expected " + Hex.ToHexString(output) + " got " + Hex.ToHexString(outBytes)); } for (int i = 0; i != outBytes.Length; i++) { if (outBytes[i] != output[i]) { Fail("failed - " + "expected " + Hex.ToHexString(output) + " got " + Hex.ToHexString(outBytes)); } } }