예제 #1
0
        public static void EndCrypt(SafeKeyHandleImpl symKeyHandle, Gost28147CryptoTransformMode transformMode)
        {
            bool success;
            uint dataLength = 0;

            if (transformMode == Gost28147CryptoTransformMode.Encrypt)
            {
                var data = new byte[32];
                success = CryptoApi.CryptEncrypt(symKeyHandle, SafeHashHandleImpl.InvalidHandle, true, 0, data, ref dataLength, (uint)data.Length);
            }
            else
            {
                var data = new byte[0];
                success = CryptoApi.CryptDecrypt(symKeyHandle, SafeHashHandleImpl.InvalidHandle, true, 0, data, ref dataLength) || (GostCryptoConfig.ProviderType == ProviderTypes.VipNet);
            }

            if (!success)
            {
                throw CreateWin32Error();
            }
        }
        private ICryptoTransform CreateCryptoTransform(SafeKeyHandleImpl hKey, byte[] iv, Gost28147CryptoTransformMode transformMode)
        {
            if (hKey == null)
            {
                hKey = CryptoApiHelper.GenerateKey(CryptoApiHelper.ProviderHandle, Constants.CALG_G28147, CspProviderFlags.NoFlags);
            }

            var keyParameters = new Dictionary <int, object>();

            if (ModeValue == CipherMode.CTS)
            {
                throw ExceptionUtility.CryptographicException(Resources.CipherTextSteamingNotSupported);
            }

            if ((Padding != PaddingMode.None) && ((ModeValue == CipherMode.OFB) || (ModeValue == CipherMode.CFB)))
            {
                throw ExceptionUtility.CryptographicException(Resources.InvalidPaddingMode);
            }

            // Установка KP_PADDING
            keyParameters.Add(Constants.KP_PADDING, Constants.ZERO_PADDING);

            if ((ModeValue == CipherMode.CFB) && (FeedbackSizeValue != DefaultFeedbackSize))
            {
                throw ExceptionUtility.CryptographicException(Resources.IncorrectFeedbackSize);
            }

            // Установка KP_MODE
            keyParameters.Add(Constants.KP_MODE, ModeValue);

            // Установка KP_IV
            if (ModeValue != CipherMode.ECB)
            {
                if (iv == null)
                {
                    iv = new byte[DefaultIvSize];
                    CryptoApiHelper.RandomNumberGenerator.GetBytes(iv);
                }

                if (iv.Length < DefaultIvSize)
                {
                    throw ExceptionUtility.CryptographicException(Resources.InvalidIvSize);
                }

                keyParameters.Add(Constants.KP_IV, iv);
            }

            return(new Gost28147CryptoTransform(hKey, keyParameters, PaddingValue, ModeValue, BlockSizeValue, transformMode));
        }
        private ICryptoTransform CreateCryptoTransform(SafeKeyHandleImpl hKey, byte[] iv, Gost28147CryptoTransformMode transformMode)
        {
            if (hKey == null)
            {
                hKey = CryptoApiHelper.GenerateKey(CryptoApiHelper.ProviderHandle, Constants.CALG_G28147, CspProviderFlags.NoFlags);
            }

            var keyParameters = new Dictionary<int, object>();

            if (ModeValue == CipherMode.CTS)
            {
                throw ExceptionUtility.CryptographicException(Resources.CipherTextSteamingNotSupported);
            }

            if ((Padding != PaddingMode.None) && ((ModeValue == CipherMode.OFB) || (ModeValue == CipherMode.CFB)))
            {
                throw ExceptionUtility.CryptographicException(Resources.InvalidPaddingMode);
            }

            // Установка KP_PADDING
            keyParameters.Add(Constants.KP_PADDING, Constants.ZERO_PADDING);

            if ((ModeValue == CipherMode.CFB) && (FeedbackSizeValue != DefaultFeedbackSize))
            {
                throw ExceptionUtility.CryptographicException(Resources.IncorrectFeedbackSize);
            }

            // Установка KP_MODE
            keyParameters.Add(Constants.KP_MODE, ModeValue);

            // Установка KP_IV
            if (ModeValue != CipherMode.ECB)
            {
                if (iv == null)
                {
                    iv = new byte[DefaultIvSize];
                    CryptoApiHelper.RandomNumberGenerator.GetBytes(iv);
                }

                if (iv.Length < DefaultIvSize)
                {
                    throw ExceptionUtility.CryptographicException(Resources.InvalidIvSize);
                }

                keyParameters.Add(Constants.KP_IV, iv);
            }

            return new Gost28147CryptoTransform(hKey, keyParameters, PaddingValue, ModeValue, BlockSizeValue, transformMode);
        }
        public Gost28147CryptoTransform(SafeKeyHandleImpl hKey, Dictionary<int, object> keyParameters, PaddingMode paddingValue, CipherMode modeValue, int blockSizeValue, Gost28147CryptoTransformMode transformMode)
        {
            _keyHandle = hKey;
            _paddingValue = paddingValue;
            _isStreamModeValue = (modeValue == CipherMode.OFB) || (modeValue == CipherMode.CFB);
            _blockSizeValue = blockSizeValue;
            _transformMode = transformMode;

            // Установка параметров ключа

            foreach (var keyParameter in keyParameters)
            {
                var keyParameterId = keyParameter.Key;
                var keyParameterValue = keyParameter.Value;

                // Копирование значения параметра

                if (keyParameterValue is byte[])
                {
                    var keyParamValueBytes = (byte[])keyParameterValue;
                    var copyKeyParamValueBytes = new byte[keyParamValueBytes.Length];
                    Array.Copy(keyParamValueBytes, copyKeyParamValueBytes, keyParamValueBytes.Length);

                    keyParameterValue = copyKeyParamValueBytes;
                }
                else if (keyParameterValue is int)
                {
                    keyParameterValue = (int)keyParameterValue;
                }
                else if (keyParameterValue is CipherMode)
                {
                    keyParameterValue = Convert.ToInt32(keyParameterValue);
                }
                else if (keyParameterValue is PaddingMode)
                {
                    keyParameterValue = Convert.ToInt32(keyParameterValue);
                }

                // Установка значения параметра

                switch (keyParameterId)
                {
                    case Constants.KP_IV:
                        {
                            _ivValue = (byte[])keyParameterValue;

                            var iv = _ivValue;
                            CryptoApiHelper.SetKeyParameter(_keyHandle, keyParameterId, iv);
                        }
                        break;
                    case Constants.KP_PADDING:
                        {
                            if (GostCryptoConfig.ProviderType != ProviderTypes.VipNet)
                            {
                                CryptoApiHelper.SetKeyParameterInt32(_keyHandle, keyParameterId, (int)keyParameterValue);
                            }
                        }
                        break;
                    case Constants.KP_MODE:
                        {
                            CryptoApiHelper.SetKeyParameterInt32(_keyHandle, keyParameterId, (int)keyParameterValue);
                        }
                        break;
                }
            }
        }
        public Gost28147CryptoTransform(SafeKeyHandleImpl hKey, Dictionary <int, object> keyParameters, PaddingMode paddingValue, CipherMode modeValue, int blockSizeValue, Gost28147CryptoTransformMode transformMode)
        {
            _keyHandle         = hKey;
            _paddingValue      = paddingValue;
            _isStreamModeValue = (modeValue == CipherMode.OFB) || (modeValue == CipherMode.CFB);
            _blockSizeValue    = blockSizeValue;
            _transformMode     = transformMode;

            // Установка параметров ключа

            foreach (var keyParameter in keyParameters)
            {
                var keyParameterId    = keyParameter.Key;
                var keyParameterValue = keyParameter.Value;

                // Копирование значения параметра

                if (keyParameterValue is byte[])
                {
                    var keyParamValueBytes     = (byte[])keyParameterValue;
                    var copyKeyParamValueBytes = new byte[keyParamValueBytes.Length];
                    Array.Copy(keyParamValueBytes, copyKeyParamValueBytes, keyParamValueBytes.Length);

                    keyParameterValue = copyKeyParamValueBytes;
                }
                else if (keyParameterValue is int)
                {
                    keyParameterValue = (int)keyParameterValue;
                }
                else if (keyParameterValue is CipherMode)
                {
                    keyParameterValue = Convert.ToInt32(keyParameterValue);
                }
                else if (keyParameterValue is PaddingMode)
                {
                    keyParameterValue = Convert.ToInt32(keyParameterValue);
                }

                // Установка значения параметра

                switch (keyParameterId)
                {
                case Constants.KP_IV:
                {
                    _ivValue = (byte[])keyParameterValue;

                    var iv = _ivValue;
                    CryptoApiHelper.SetKeyParameter(_keyHandle, keyParameterId, iv);
                }
                break;

                case Constants.KP_PADDING:
                {
                    if (GostCryptoConfig.ProviderType != ProviderTypes.VipNet)
                    {
                        CryptoApiHelper.SetKeyParameterInt32(_keyHandle, keyParameterId, (int)keyParameterValue);
                    }
                }
                break;

                case Constants.KP_MODE:
                {
                    CryptoApiHelper.SetKeyParameterInt32(_keyHandle, keyParameterId, (int)keyParameterValue);
                }
                break;
                }
            }
        }