internal SafeCapiKeyHandle Duplicate() { Contract.Requires(!IsInvalid && !IsClosed); Contract.Ensures(Contract.Result <SafeCapiKeyHandle>() != null && !Contract.Result <SafeCapiKeyHandle>().IsInvalid&& !Contract.Result <SafeCapiKeyHandle>().IsClosed); SafeCapiKeyHandle duplicate = null; RuntimeHelpers.PrepareConstrainedRegions(); try { if (!CapiNative.UnsafeNativeMethods.CryptDuplicateKey(this, IntPtr.Zero, 0, out duplicate)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } finally { if (duplicate != null && !duplicate.IsInvalid && ParentCsp != IntPtr.Zero) { duplicate.ParentCsp = ParentCsp; } } return(duplicate); }
public CapiSymmetricAlgorithm(int blockSize, int feedbackSize, SafeCspHandle provider, SafeCapiKeyHandle key, byte[] iv, CipherMode cipherMode, PaddingMode paddingMode, EncryptionMode encryptionMode) { this.m_blockSize = blockSize; this.m_encryptionMode = encryptionMode; this.m_paddingMode = paddingMode; this.m_provider = provider.Duplicate(); this.m_key = SetupKey(key, ProcessIV(iv, blockSize, cipherMode), cipherMode, feedbackSize); }
internal SafeCapiKeyHandle Duplicate() { SafeCapiKeyHandle phKey = null; if (!CapiNative.UnsafeNativeMethods.CryptDuplicateKey(this, IntPtr.Zero, 0, out phKey)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(phKey); }
public CapiSymmetricAlgorithm(int blockSize, int feedbackSize, SafeCspHandle provider, SafeCapiKeyHandle key, byte[] iv, CipherMode cipherMode, PaddingMode paddingMode, EncryptionMode encryptionMode) { Contract.Requires(0 < blockSize && blockSize % 8 == 0); Contract.Requires(0 <= feedbackSize); Contract.Requires(provider != null && !provider.IsInvalid && !provider.IsClosed); Contract.Requires(key != null && !key.IsInvalid && !key.IsClosed); Contract.Ensures(m_provider != null && !m_provider.IsInvalid && !m_provider.IsClosed); m_blockSize = blockSize; m_encryptionMode = encryptionMode; m_paddingMode = paddingMode; m_provider = provider.Duplicate(); m_key = SetupKey(key, ProcessIV(iv, blockSize, cipherMode), cipherMode, feedbackSize); }
public override void GenerateKey() { Contract.Ensures(m_key != null && !m_key.IsInvalid & !m_key.IsClosed); Contract.Assert(m_cspHandle != null); SafeCapiKeyHandle key = null; RuntimeHelpers.PrepareConstrainedRegions(); try { if (!CapiNative.UnsafeNativeMethods.CryptGenKey(m_cspHandle, GetAlgorithmId(KeySizeValue), CapiNative.KeyFlags.Exportable, out key)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } finally { if (key != null && !key.IsInvalid) { key.SetParentCsp(m_cspHandle); } } if (m_key != null) { m_key.Dispose(); } m_key = key; }
private ICryptoTransform CreateEncryptor(SafeCapiKeyHandle key, byte[] iv) { Contract.Requires(key != null); Contract.Ensures(Contract.Result<ICryptoTransform>() != null); return new CapiSymmetricAlgorithm(BlockSizeValue, FeedbackSizeValue, m_cspHandle, key, iv, Mode, PaddingValue, EncryptionMode.Encrypt); }
private static SafeCapiKeyHandle SetupKey(SafeCapiKeyHandle key, byte[] iv, CipherMode cipherMode, int feedbackSize) { Contract.Requires(key != null); Contract.Requires(cipherMode == CipherMode.ECB || iv != null); Contract.Requires(0 <= feedbackSize); Contract.Ensures(Contract.Result<SafeCapiKeyHandle>() != null && !Contract.Result<SafeCapiKeyHandle>().IsInvalid && !Contract.Result<SafeCapiKeyHandle>().IsClosed); // Make a copy of the key so that we don't modify the properties of the caller's copy SafeCapiKeyHandle encryptionKey = key.Duplicate(); // Setup the cipher mode first CapiNative.SetKeyParameter(encryptionKey, CapiNative.KeyParameter.Mode, (int)cipherMode); // If we're not in ECB mode then setup the IV if (cipherMode != CipherMode.ECB) { CapiNative.SetKeyParameter(encryptionKey, CapiNative.KeyParameter.IV, iv); } // OFB and CFB require a feedback loop size if (cipherMode == CipherMode.CFB || cipherMode == CipherMode.OFB) { CapiNative.SetKeyParameter(encryptionKey, CapiNative.KeyParameter.ModeBits, feedbackSize); } return encryptionKey; }
private static SafeCapiKeyHandle SetupKey(SafeCapiKeyHandle key, byte[] iv, CipherMode cipherMode, int feedbackSize) { SafeCapiKeyHandle handle = key.Duplicate(); CapiNative.SetKeyParameter(handle, CapiNative.KeyParameter.Mode, (int) cipherMode); if (cipherMode != CipherMode.ECB) { CapiNative.SetKeyParameter(handle, CapiNative.KeyParameter.IV, iv); } if ((cipherMode == CipherMode.CFB) || (cipherMode == CipherMode.OFB)) { CapiNative.SetKeyParameter(handle, CapiNative.KeyParameter.ModeBits, feedbackSize); } return handle; }
private ICryptoTransform CreateDecryptor(SafeCapiKeyHandle key, byte[] iv) { return new CapiSymmetricAlgorithm(base.BlockSizeValue, base.FeedbackSizeValue, this.m_cspHandle, key, iv, this.Mode, base.PaddingValue, EncryptionMode.Decrypt); }
public override void GenerateKey() { SafeCapiKeyHandle phKey = null; if (!CapiNative.UnsafeNativeMethods.CryptGenKey(this.m_cspHandle, GetAlgorithmId(base.KeySizeValue), CapiNative.KeyFlags.Exportable, out phKey)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (this.m_key != null) { this.m_key.Dispose(); } this.m_key = phKey; }