/// <summary>
        /// Constructs the core to use a stored CNG key.
        /// </summary>
        public CngSymmetricAlgorithmCore(ICngSymmetricAlgorithm outer, string keyName, CngProvider provider, CngKeyOpenOptions openOptions)
        {
            if (keyName == null)
            {
                throw new ArgumentNullException(nameof(keyName));
            }
            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            _outer = outer;

            _keyName       = keyName;
            _provider      = provider;
            _optionOptions = openOptions;

            using (CngKey cngKey = ProduceCngKey())
            {
                CngAlgorithm actualAlgorithm = cngKey.Algorithm;
                string       algorithm       = _outer.GetNCryptAlgorithmIdentifier();

                if (algorithm != actualAlgorithm.Algorithm)
                {
                    throw new CryptographicException(SR.Format(SR.Cryptography_CngKeyWrongAlgorithm, actualAlgorithm.Algorithm, algorithm));
                }

                _outer.BaseKeySize = cngKey.KeySize;
            }
        }
        /// <summary>
        /// Constructs the core to use a stored CNG key.
        /// </summary>
        public CngSymmetricAlgorithmCore(string algorithm, ICngSymmetricAlgorithm outer, string keyName, CngProvider provider, CngKeyOpenOptions openOptions)
        {
            if (keyName == null)
            {
                throw new ArgumentNullException("keyName");
            }
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }

            _algorithm = algorithm;
            _outer     = outer;

            _keyName       = keyName;
            _provider      = provider;
            _optionOptions = openOptions;

            using (CngKey cngKey = ProduceCngKey())
            {
                CngAlgorithm actualAlgorithm = cngKey.Algorithm;
                if (algorithm != actualAlgorithm.Algorithm)
                {
                    throw new CryptographicException(SR.GetString(SR.Cryptography_CngKeyWrongAlgorithm, actualAlgorithm.Algorithm, algorithm));
                }

                _outer.BaseKeySize = cngKey.KeySize;
            }
        }
        /// <summary>
        /// Configures the core to use plaintext keys (to be auto-generated when first needed.)
        /// </summary>
        public CngSymmetricAlgorithmCore(ICngSymmetricAlgorithm outer)
        {
            _outer = outer;

            _keyName       = null; // Setting _keyName to null signifies that this object is based on a plaintext key, not a stored CNG key.
            _provider      = null;
            _optionOptions = CngKeyOpenOptions.None;
        }
        /// <summary>
        /// Configures the core to use plaintext keys (to be auto-generated when first needed.)
        /// </summary>
        public CngSymmetricAlgorithmCore(ICngSymmetricAlgorithm outer)
        {
            _outer = outer;

            _keyName = null; // Setting _keyName to null signifies that this object is based on a plaintext key, not a stored CNG key.
            _provider = null;
            _optionOptions = CngKeyOpenOptions.None;
        }
        public void SetKeySize(int keySize, ICngSymmetricAlgorithm outer)
        {
            // Warning: This gets invoked once before "this" is initialized, due to Aes(), DES(), etc., setting the KeySize property in their
            // nullary constructor. That's why we require "outer" being passed as parameter.
            Debug.Assert(_outer == null || _outer == outer);

            outer.BaseKeySize = keySize;
            _keyName          = null; // Setting _keyName to null signifies that this object is now based on a plaintext key, not a stored CNG key.
        }
        public void SetKeySize(int keySize, ICngSymmetricAlgorithm outer)
        {
            // Warning: This gets invoked once before "this" is initialized, due to Aes(), DES(), etc., setting the KeySize property in their
            // nullary constructor. That's why we require "outer" being passed as parameter.
            Debug.Assert(_outer == null || _outer == outer);

            outer.BaseKeySize = keySize;
            _keyName = null; // Setting _keyName to null signifies that this object is now based on a plaintext key, not a stored CNG key.
        }
        /// <summary>
        /// Constructs the core to use a stored CNG key. 
        /// </summary>
        public CngSymmetricAlgorithmCore(string algorithm, ICngSymmetricAlgorithm outer, string keyName, CngProvider provider, CngKeyOpenOptions openOptions)
        {
            if (keyName == null)
                throw new ArgumentNullException("keyName");
            if (provider == null)
                throw new ArgumentNullException("provider");

            _algorithm = algorithm;
            _outer = outer;

            _keyName = keyName;
            _provider = provider;
            _optionOptions = openOptions;

            using (CngKey cngKey = ProduceCngKey())
            {
                CngAlgorithm actualAlgorithm = cngKey.Algorithm;
                if (algorithm != actualAlgorithm.Algorithm)
                    throw new CryptographicException(SR.Format(SR.Cryptography_CngKeyWrongAlgorithm, actualAlgorithm.Algorithm, algorithm));

                _outer.BaseKeySize = cngKey.KeySize;
            }
        }
        /// <summary>
        ///     Capture the parameters used for encryption, and verify that they make sense together
        /// </summary>
        internal AuthenticatedSymmetricEncryptionState(byte[] key,
                                                       byte[] iv,
                                                       byte[] authenticatedData,
                                                       AuthenticatedSymmetricAlgorithm algorithm)
            : base(key, iv, algorithm, typeof(AuthenticatedSymmetricAlgorithm), false)
        {
            if (authenticatedData != null)
            {
                m_authenticatedData = new byte[authenticatedData.Length];
                Array.Copy(authenticatedData, m_authenticatedData, m_authenticatedData.Length);
            }

            // Since CipherMode doesn't contain authenticated encryption modes, it's not very useful for
            // debugging mode mismatches in authenticated symmetric algorithms.  Our only current
            // authenticated symmetric algorithm implementations ues CNG chaining modes, so we'll grab that
            // if it's available to aid in debugging.
            ICngSymmetricAlgorithm cngAlgorithm = algorithm as ICngSymmetricAlgorithm;

            if (cngAlgorithm != null)
            {
                m_cngChainingMode = cngAlgorithm.CngMode;
            }
        }
Example #9
0
 /// <summary>
 /// Constructs the core to use a stored CNG key.
 /// </summary>
 public CngSymmetricAlgorithmCore(ICngSymmetricAlgorithm outer, string keyName !!, CngProvider provider !!, CngKeyOpenOptions openOptions)