using GostCryptography.Asn1.Gost.Gost_R3410; using GostCryptography.Base; using GostCryptography.Native; using GostCryptography.Properties; using System; using System.Security; using System.Security.Cryptography; namespace GostCryptography.Gost_R3410 { /// /// Реализация алгоритма формирования общих ключей на основе алгоритма ГОСТ Р 34.10 и эфимерного ключа /// public abstract class Gost_R3410_EphemeralAsymmetricAlgorithm : Gost_R3410_AsymmetricAlgorithmBase, ISafeHandleProvider, ISafeHandleProvider where TKeyParams : Gost_R3410_KeyExchangeParams where TKeyAlgorithm : Gost_R3410_KeyExchangeAlgorithm { /// [SecuritySafeCritical] protected Gost_R3410_EphemeralAsymmetricAlgorithm(ProviderType providerType, int keySize) : base(providerType, keySize) { _providerHandle = CryptoApiHelper.GetProviderHandle(ProviderType).DangerousAddRef(); _keyHandle = CryptoApiHelper.GenerateKey(_providerHandle, ExchangeAlgId, CspProviderFlags.NoFlags); } /// /// Конструктор /// /// Тип криптографического провайдера /// Параметры ключа, используемого для создания общего секретного ключа /// Размер ключа в битах /// /// /// В параметре достаточно передать идентификатор OID параметров хэширования /// и идентификатор OID параметров открытого ключа /// . Остальные параметры не используются. /// [SecuritySafeCritical] protected Gost_R3410_EphemeralAsymmetricAlgorithm(ProviderType providerType, TKeyParams keyParameters, int keySize) : base(providerType, keySize) { if (keyParameters == null) { throw ExceptionUtility.ArgumentNull(nameof(keyParameters)); } _providerHandle = CryptoApiHelper.GetProviderHandle(ProviderType).DangerousAddRef(); _keyHandle = CryptoApiHelper.GenerateDhEphemeralKey(providerType, _providerHandle, ExchangeAlgId, keyParameters.DigestParamSet, keyParameters.PublicKeyParamSet); } [SecurityCritical] private readonly SafeProvHandleImpl _providerHandle; [SecurityCritical] private readonly SafeKeyHandleImpl _keyHandle; /// SafeProvHandleImpl ISafeHandleProvider.SafeHandle { [SecurityCritical] get => _providerHandle; } /// SafeKeyHandleImpl ISafeHandleProvider.SafeHandle { [SecurityCritical] get => _keyHandle; } /// public override byte[] CreateSignature(byte[] hash) { throw ExceptionUtility.NotSupported(Resources.EphemKeyOperationNotSupported); } /// public override bool VerifySignature(byte[] hash, byte[] signature) { throw ExceptionUtility.NotSupported(Resources.EphemKeyOperationNotSupported); } /// [SecuritySafeCritical] public override TKeyAlgorithm CreateKeyExchange(TKeyParams keyParameters) { return CreateKeyExchangeAlgorithm(ProviderType, _providerHandle, _keyHandle, (TKeyParams)keyParameters.Clone()); } /// [SecuritySafeCritical] public override TKeyParams ExportParameters(bool includePrivateKey) { if (includePrivateKey) { throw ExceptionUtility.NotSupported(Resources.EphemKeyOperationNotSupported); } return CryptoApiHelper.ExportPublicKey(_keyHandle, CreateKeyExchangeParams(), KeySizeValue); } /// public override void ImportParameters(TKeyParams keyParameters) { throw ExceptionUtility.NotSupported(Resources.EphemKeyOperationNotSupported); } /// [SecuritySafeCritical] protected override void Dispose(bool disposing) { _keyHandle.TryDispose(); _providerHandle.TryDispose(); base.Dispose(disposing); } } }