Наследование: IDerivationParameters
Пример #1
0
        private byte[] GenerateKdfBytes(
            KdfParameters	kParam,
            int				length)
        {
            byte[] buf = new byte[length];

            kdf.Init(kParam);

            kdf.GenerateBytes(buf, 0, buf.Length);

            return buf;
        }
Пример #2
0
        private byte[] DecryptBlock(
            byte[]  in_enc,
            int     inOff,
            int     inLen,
            byte[]  z)
        {
            byte[]          M = null;
            KeyParameter    macKey = null;
            KdfParameters   kParam = new KdfParameters(z, param.GetDerivationV());
            int             macKeySize = param.MacKeySize;

            kdf.Init(kParam);

            // Ensure that the length of the input is greater than the MAC in bytes
            if (inLen < mac.GetMacSize())
                throw new InvalidCipherTextException("Length of input must be greater than the MAC");

            inLen -= mac.GetMacSize();

            if (cipher == null)     // stream mode
            {
                byte[] Buffer = GenerateKdfBytes(kParam, inLen + (macKeySize / 8));

                M = new byte[inLen];

                for (int i = 0; i != inLen; i++)
                {
                    M[i] = (byte)(in_enc[inOff + i] ^ Buffer[i]);
                }

                macKey = new KeyParameter(Buffer, inLen, (macKeySize / 8));
            }
            else
            {
                int cipherKeySize = ((IesWithCipherParameters)param).CipherKeySize;
                byte[] Buffer = GenerateKdfBytes(kParam, (cipherKeySize / 8) + (macKeySize / 8));

                cipher.Init(false, new KeyParameter(Buffer, 0, (cipherKeySize / 8)));

                M = cipher.DoFinal(in_enc, inOff, inLen);

                macKey = new KeyParameter(Buffer, (cipherKeySize / 8), (macKeySize / 8));
            }

            byte[] macIV = param.GetEncodingV();

            mac.Init(macKey);
            mac.BlockUpdate(in_enc, inOff, inLen);
            mac.BlockUpdate(macIV, 0, macIV.Length);
            mac.DoFinal(macBuf, 0);

            inOff += inLen;

            byte[] T1 = Arrays.CopyOfRange(in_enc, inOff, inOff + macBuf.Length);

            if (!Arrays.ConstantTimeAreEqual(T1, macBuf))
                throw (new InvalidCipherTextException("Invalid MAC."));

            return M;
        }
Пример #3
0
        private byte[] EncryptBlock(
            byte[]  input,
            int     inOff,
            int     inLen,
            byte[]  z)
        {
            byte[]          C = null;
            KeyParameter    macKey = null;
            KdfParameters   kParam = new KdfParameters(z, param.GetDerivationV());
            int             c_text_length = 0;
            int             macKeySize = param.MacKeySize;

            if (cipher == null)     // stream mode
            {
                byte[] Buffer = GenerateKdfBytes(kParam, inLen + (macKeySize / 8));

                C = new byte[inLen + mac.GetMacSize()];
                c_text_length = inLen;

                for (int i = 0; i != inLen; i++)
                {
                    C[i] = (byte)(input[inOff + i] ^ Buffer[i]);
                }

                macKey = new KeyParameter(Buffer, inLen, (macKeySize / 8));
            }
            else
            {
                int cipherKeySize = ((IesWithCipherParameters)param).CipherKeySize;
                byte[] Buffer = GenerateKdfBytes(kParam, (cipherKeySize / 8) + (macKeySize / 8));

                cipher.Init(true, new KeyParameter(Buffer, 0, (cipherKeySize / 8)));

                c_text_length = cipher.GetOutputSize(inLen);
                byte[] tmp = new byte[c_text_length];

                int len = cipher.ProcessBytes(input, inOff, inLen, tmp, 0);
                len += cipher.DoFinal(tmp, len);

                C = new byte[len + mac.GetMacSize()];
                c_text_length = len;

                Array.Copy(tmp, 0, C, 0, len);

                macKey = new KeyParameter(Buffer, (cipherKeySize / 8), (macKeySize / 8));
            }

            byte[] macIV = param.GetEncodingV();

            mac.Init(macKey);
            mac.BlockUpdate(C, 0, c_text_length);
            mac.BlockUpdate(macIV, 0, macIV.Length);
            //
            // return the message and it's MAC
            //
            mac.DoFinal(C, c_text_length);
            return C;
        }
Пример #4
0
        private byte[] DecryptBlock(
            byte[]  in_enc,
            int     inOff,
            int     inLen,
            byte[]  z)
        {
            byte[]          M = null;
            KeyParameter    macKey = null;
            KdfParameters   kParam = new KdfParameters(z, param.GetDerivationV());
            int             macKeySize = param.MacKeySize;

            kdf.Init(kParam);

            inLen -= mac.GetMacSize();

            if (cipher == null)     // stream mode
            {
				byte[] Buffer = GenerateKdfBytes(kParam, inLen + (macKeySize / 8));

                M = new byte[inLen];

                for (int i = 0; i != inLen; i++)
                {
                    M[i] = (byte)(in_enc[inOff + i] ^ Buffer[i]);
                }

                macKey = new KeyParameter(Buffer, inLen, (macKeySize / 8));
            }
            else
            {
                int cipherKeySize = ((IesWithCipherParameters)param).CipherKeySize;
				byte[] Buffer = GenerateKdfBytes(kParam, (cipherKeySize / 8) + (macKeySize / 8));

                cipher.Init(false, new KeyParameter(Buffer, 0, (cipherKeySize / 8)));

				M = cipher.DoFinal(in_enc, inOff, inLen);

				macKey = new KeyParameter(Buffer, (cipherKeySize / 8), (macKeySize / 8));
            }

            byte[] macIV = param.GetEncodingV();

            mac.Init(macKey);
            mac.BlockUpdate(in_enc, inOff, inLen);
            mac.BlockUpdate(macIV, 0, macIV.Length);
            mac.DoFinal(macBuf, 0);

			inOff += inLen;

			for (int t = 0; t < macBuf.Length; t++)
            {
                if (macBuf[t] != in_enc[inOff + t])
                {
                    throw (new InvalidCipherTextException("IMac codes failed to equal."));
                }
            }

            return M;
        }
        private byte[] EncryptBlock(byte[] input, int inOff, int inLen, byte[] z)
        {
            byte[] c;
            KeyParameter macKey;
            var kParam = new KdfParameters(z, _param.Derivation);
            int cTextLength;
            var macKeySize = _param.MacKeySize;

            if (_cipher == null)     // stream mode
            {
                var buffer = GenerateKdfBytes(kParam, inLen + (macKeySize / 8));

                c = new byte[inLen + _mac.GetMacSize()];
                cTextLength = inLen;

                for (var i = 0; i != inLen; i++)
                {
                    c[i] = (byte)(input[inOff + i] ^ buffer[i]);
                }

                macKey = new KeyParameter(buffer, inLen, (macKeySize / 8));
            }
            else
            {
                var cipherKeySize = ((IesWithCipherParameters)_param).CipherKeySize;
                var buffer = GenerateKdfBytes(kParam, (cipherKeySize / 8) + (macKeySize / 8));

                _cipher.Init(true, new KeyParameter(buffer, 0, (cipherKeySize / 8)));

                cTextLength = _cipher.GetOutputSize(inLen);
                var tmp = new byte[cTextLength];

                var len = _cipher.ProcessBytes(input, inOff, inLen, tmp, 0);
                len += _cipher.DoFinal(tmp, len);

                c = new byte[len + _mac.GetMacSize()];
                cTextLength = len;

                Array.Copy(tmp, 0, c, 0, len);

                macKey = new KeyParameter(buffer, (cipherKeySize / 8), (macKeySize / 8));
            }

            var macIV = _param.Encoding;

            _mac.Init(macKey);
            _mac.BlockUpdate(c, 0, cTextLength);
            _mac.BlockUpdate(macIV, 0, macIV.Length);
            //
            // return the message and it's MAC
            //
            _mac.DoFinal(c, cTextLength);
            return c;
        }
        private byte[] DecryptBlock(byte[] inEnc, int inOff, int inLen, byte[] z)
        {
            byte[] m;
            KeyParameter macKey;
            var kParam = new KdfParameters(z, _param.Derivation);
            var macKeySize = _param.MacKeySize;

            _kdf.Init(kParam);

            inLen -= _mac.GetMacSize();

            if (_cipher == null)     // stream mode
            {
                var buffer = GenerateKdfBytes(kParam, inLen + (macKeySize / 8));

                m = new byte[inLen];

                for (var i = 0; i != inLen; i++)
                {
                    m[i] = (byte)(inEnc[inOff + i] ^ buffer[i]);
                }

                macKey = new KeyParameter(buffer, inLen, (macKeySize / 8));
            }
            else
            {
                var cipherKeySize = ((IesWithCipherParameters)_param).CipherKeySize;
                var buffer = GenerateKdfBytes(kParam, (cipherKeySize / 8) + (macKeySize / 8));

                _cipher.Init(false, new KeyParameter(buffer, 0, (cipherKeySize / 8)));

                m = _cipher.DoFinal(inEnc, inOff, inLen);

                macKey = new KeyParameter(buffer, (cipherKeySize / 8), (macKeySize / 8));
            }

            var macIV = _param.Encoding;

            _mac.Init(macKey);
            _mac.BlockUpdate(inEnc, inOff, inLen);
            _mac.BlockUpdate(macIV, 0, macIV.Length);
            _mac.DoFinal(_macBuf, 0);

            inOff += inLen;

            for (var t = 0; t < _macBuf.Length; t++)
            {
                if (_macBuf[t] != inEnc[inOff + t])
                {
                    throw (new InvalidCipherTextException("IMac codes failed to equal."));
                }
            }

            return m;
        }
Пример #7
0
        private void GenerateKey()
        {
            var sb = new StringBuilder();
            sb.Append("Error generating Key").Append(Environment.NewLine);
            if(Model.Password.IsNullOrEmpty())
            {
                sb.Append("Enter Password!").Append(Environment.NewLine);
            }
            if(Model.Salt.IsNullOrEmpty())
            {
                sb.Append("Enter Salt!").Append(Environment.NewLine);
            }
            if (Model.KeySize <= 0)
            {
                sb.Append("Enter KeySize!").Append(Environment.NewLine);
            }
            if (!Model.BouncyImplementation && Model.Iterations <= 0)
            {
                sb.Append("Enter Iterations value!").Append(Environment.NewLine);
            }
            if (Model.BouncyImplementation && Model.Digest == null)
            {
                sb.Append("Select Digest!").Append(Environment.NewLine);
            }
            if (Model.BouncyImplementation && Model.DerivationFunction == null)
            {
                sb.Append("Select derivation function!").Append(Environment.NewLine);
            }
            var errors = sb.ToString();
            if (!errors.IsNullOrEmpty())
            {
                MessageBoxContent = new MessageBoxViewModel(CloseMessageBox,
                                                            MessageBoxModel.Error(errors));
                IsMessageBoxVisible = true;
                return;
            }
            byte[] password = Encoding.UTF32.GetBytes(Model.Password);
            byte[] salt = Encoding.UTF32.GetBytes(Model.Salt);

            if (Model.BouncyImplementation)
            {
                Model.Key = new byte[Model.KeySize];
                var digest = Model.Digest.Instance<IDigest>();
                var kdf = Model.DerivationFunction.Instance<BaseKdfBytesGenerator>(digest);

                var kdfParams = new KdfParameters(password, salt);
                kdf.Init(kdfParams);
                kdf.GenerateBytes(Model.Key, 0, Model.Key.Length);
            }
            else
            {
                var k1 = new Rfc2898DeriveBytes(password, salt, Model.Iterations);
                Model.Key = k1.GetBytes(Model.KeySize);
            }
        }