Пример #1
0
 internal static extern bool EVP_CipherInit_ex(
     SafeEvpCipherCtxHandle ctx,
     IntPtr cipher,
     IntPtr engineNull,
     byte[] key,
     byte[] iv,
     int enc);
Пример #2
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_ctx != null)
                {
                    _ctx.Dispose();
                    _ctx = null;
                }
            }

            base.Dispose(disposing);
        }
        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);
        }
        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
 internal static extern unsafe bool EvpCipherFinalEx(
     SafeEvpCipherCtxHandle ctx,
     byte* outm,
     out int outl);
Пример #7
0
 internal static unsafe extern bool EvpCipherUpdate(
     SafeEvpCipherCtxHandle ctx,
     byte* @out,
     out int outl,
     byte* @in,
     int inl);
Пример #8
0
 internal static extern bool EvpCipherCtxSetPadding(SafeEvpCipherCtxHandle x, int padding);
Пример #9
0
 internal static extern bool EvpCipherReset(SafeEvpCipherCtxHandle ctx);
Пример #10
0
        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));
        }
        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
 internal static extern bool EVP_CIPHER_CTX_set_padding(SafeEvpCipherCtxHandle x, int padding);
Пример #14
0
 internal static extern void EVP_CIPHER_CTX_init(SafeEvpCipherCtxHandle ctx);
Пример #15
0
        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
        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));
        }