コード例 #1
0
ファイル: Interop.EVP.Cipher.cs プロジェクト: johnhhm/corefx
 internal static extern bool EVP_CipherInit_ex(
     SafeEvpCipherCtxHandle ctx,
     IntPtr cipher,
     IntPtr engineNull,
     byte[] key,
     byte[] iv,
     int enc);
コード例 #2
0
ファイル: OpenSslCipher.cs プロジェクト: dotnet/corefx
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_ctx != null)
                {
                    _ctx.Dispose();
                    _ctx = null;
                }
            }

            base.Dispose(disposing);
        }
コード例 #3
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_ctx != null)
                {
                    _ctx.Dispose();
                    _ctx = null;
                }

                if (_decryptBuffer != null)
                {
                    Array.Clear(_decryptBuffer, 0, _decryptBuffer.Length);
                    _decryptBuffer = null;
                }
            }

            base.Dispose(disposing);
        }
コード例 #4
0
        internal static SafeEvpCipherCtxHandle Create()
        {
            IntPtr memPtr    = IntPtr.Zero;
            bool   succeeded = false;
            SafeEvpCipherCtxHandle safeHandle = null;

            try
            {
                memPtr     = Marshal.AllocHGlobal(Interop.libcrypto.EVP_CIPHER_CTX_SIZE);
                safeHandle = new SafeEvpCipherCtxHandle();
                safeHandle.SetHandle(memPtr);

                Interop.libcrypto.EVP_CIPHER_CTX_init(safeHandle);

                succeeded = true;
                return(safeHandle);
            }
            finally
            {
                if (!succeeded)
                {
                    // If we made it to SetHandle, and failed calling EVP_CIPHER_CTX_init
                    // then OpenSSL hasn't built this object yet, and we shouldn't call
                    // EVP_CIPHER_CTX_cleanup.
                    if (safeHandle != null && !safeHandle.IsInvalid)
                    {
                        safeHandle.SetHandleAsInvalid();
                    }

                    if (memPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(memPtr);
                    }
                }
            }
        }
コード例 #5
0
        internal static SafeEvpCipherCtxHandle Create()
        {
            IntPtr memPtr = IntPtr.Zero;
            bool succeeded = false;
            SafeEvpCipherCtxHandle safeHandle = null;

            try
            {
                memPtr = Marshal.AllocHGlobal(Interop.libcrypto.EVP_CIPHER_CTX_SIZE);
                safeHandle = new SafeEvpCipherCtxHandle();
                safeHandle.SetHandle(memPtr);

                Interop.libcrypto.EVP_CIPHER_CTX_init(safeHandle);

                succeeded = true;
                return safeHandle;
            }
            finally
            {
                if (!succeeded)
                {
                    // If we made it to SetHandle, and failed calling EVP_CIPHER_CTX_init
                    // then OpenSSL hasn't built this object yet, and we shouldn't call
                    // EVP_CIPHER_CTX_cleanup.
                    if (safeHandle != null && !safeHandle.IsInvalid)
                    {
                        safeHandle.SetHandleAsInvalid();
                    }

                    if (memPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(memPtr);
                    }
                }
            }
        }
コード例 #6
0
ファイル: Interop.EVP.Cipher.cs プロジェクト: jemmy655/corefx
 internal static extern unsafe bool EvpCipherFinalEx(
     SafeEvpCipherCtxHandle ctx,
     byte* outm,
     out int outl);
コード例 #7
0
ファイル: Interop.EVP.Cipher.cs プロジェクト: jemmy655/corefx
 internal static unsafe extern bool EvpCipherUpdate(
     SafeEvpCipherCtxHandle ctx,
     byte* @out,
     out int outl,
     byte* @in,
     int inl);
コード例 #8
0
ファイル: Interop.EVP.Cipher.cs プロジェクト: jemmy655/corefx
 internal static extern bool EvpCipherCtxSetPadding(SafeEvpCipherCtxHandle x, int padding);
コード例 #9
0
ファイル: Interop.EVP.Cipher.cs プロジェクト: jemmy655/corefx
 internal static extern bool EvpCipherReset(SafeEvpCipherCtxHandle ctx);
コード例 #10
0
ファイル: OpenSslCipher.cs プロジェクト: dotnet/corefx
        private void OpenKey(IntPtr algorithm, byte[] key, int effectiveKeyLength)
        {
            _ctx = Interop.Crypto.EvpCipherCreate(
                algorithm,
                key,
                key.Length * 8,
                effectiveKeyLength,
                IV,
                _encrypting ? 1 : 0);

            Interop.Crypto.CheckValidOpenSslHandle(_ctx);

            // OpenSSL will happily do PKCS#7 padding for us, but since we support padding modes
            // that it doesn't (PaddingMode.Zeros) we'll just always pad the blocks ourselves.
            CheckBoolReturn(Interop.Crypto.EvpCipherCtxSetPadding(_ctx, 0));
        }
コード例 #11
0
        private void OpenKey(byte[] key, byte[] iv)
        {
            Func<IntPtr> algorithmFunc = FindAlgorithmSelector(key.Length * 8);

            // The algorithm pointer is a static pointer, so not having any cleanup code is correct.
            IntPtr algorithm = algorithmFunc();

            _ctx = Interop.Crypto.EvpCipherCreate(
                algorithm,
                key,
                iv,
                _encryptor ? 1 : 0);

            if (_ctx == null)
            {
                throw Interop.Crypto.CreateOpenSslCryptographicException();
            }

            // OpenSSL will happily do PKCS#7 padding for us, but since we support padding modes
            // that it doesn't (PaddingMode.Zeros) we'll just always pad the blocks ourselves.
            bool status = Interop.Crypto.EvpCipherCtxSetPadding(_ctx, 0);

            CheckBoolReturn(status);
        }
コード例 #12
0
        private void OpenKey(byte[] key, byte[] iv)
        {
            Func<IntPtr> algorithmFunc = FindAlgorithmSelector(key.Length * 8);

            _ctx = SafeEvpCipherCtxHandle.Create();

            // The algorithm pointer is a static pointer, so not having any cleanup code is correct.
            IntPtr algorithm = algorithmFunc();

            bool status = Interop.libcrypto.EVP_CipherInit_ex(
                _ctx,
                algorithm,
                IntPtr.Zero,
                key,
                iv,
                _encryptor ? 1 : 0);

            CheckBoolReturn(status);

            // OpenSSL will happily do PKCS#7 padding for us, but since we support padding modes
            // that it doesn't (PaddingMode.Zeros) we'll just always pad the blocks ourselves.
            status = Interop.libcrypto.EVP_CIPHER_CTX_set_padding(_ctx, 0);

            CheckBoolReturn(status);
        }
コード例 #13
0
ファイル: Interop.EVP.Cipher.cs プロジェクト: johnhhm/corefx
 internal static extern bool EVP_CIPHER_CTX_set_padding(SafeEvpCipherCtxHandle x, int padding);
コード例 #14
0
ファイル: Interop.EVP.Cipher.cs プロジェクト: johnhhm/corefx
 internal static extern void EVP_CIPHER_CTX_init(SafeEvpCipherCtxHandle ctx);
コード例 #15
0
ファイル: TripleDesOpenSslCipher.cs プロジェクト: jeh0/corefx
        private void OpenKey(CipherMode cipherMode, byte[] key)
        {
            // The algorithm pointer is a static pointer, so not having any cleanup code is correct.
            IntPtr algorithm;
            switch (cipherMode)
            {
                case CipherMode.CBC:
                    algorithm = Interop.Crypto.EvpDes3Cbc();
                    break;
                case CipherMode.ECB:
                    algorithm = Interop.Crypto.EvpDes3Ecb();
                    break;
                default:
                    // This is what AesCngCryptoTransform::GetCipherAlgorithm throws when it doesn't understand the value.
                    throw new NotSupportedException();
            }

            _ctx = Interop.Crypto.EvpCipherCreate(
                algorithm,
                key,
                IV,
                _encrypting ? 1 : 0);

            if (_ctx == null)
            {
                throw Interop.Crypto.CreateOpenSslCryptographicException();
            }

            // OpenSSL will happily do PKCS#7 padding for us, but since we support padding modes
            // that it doesn't (PaddingMode.Zeros) we'll just always pad the blocks ourselves.
            CheckBoolReturn(Interop.Crypto.EvpCipherCtxSetPadding(_ctx, 0));
        }
コード例 #16
0
ファイル: OpenSslCipher.cs プロジェクト: noahfalk/corefx
        private void OpenKey(IntPtr algorithm, byte[] key)
        {
            _ctx = Interop.Crypto.EvpCipherCreate(
                algorithm,
                key,
                IV,
                _encrypting ? 1 : 0);

            if (_ctx == null)
            {
                throw Interop.Crypto.CreateOpenSslCryptographicException();
            }

            // OpenSSL will happily do PKCS#7 padding for us, but since we support padding modes
            // that it doesn't (PaddingMode.Zeros) we'll just always pad the blocks ourselves.
            CheckBoolReturn(Interop.Crypto.EvpCipherCtxSetPadding(_ctx, 0));
        }