예제 #1
0
        protected override bool TryEncryptCfbCore(
            ReadOnlySpan <byte> plaintext,
            ReadOnlySpan <byte> iv,
            Span <byte> destination,
            PaddingMode paddingMode,
            int feedbackSizeInBits,
            out int bytesWritten)
        {
            ValidateCFBFeedbackSize(feedbackSizeInBits);

            UniversalCryptoTransform transform = CreateTransformCore(
                CipherMode.CFB,
                paddingMode,
                Key,
                iv: iv.ToArray(),
                blockSize: BlockSize / BitsPerByte,
                paddingSize: feedbackSizeInBits / BitsPerByte,
                feedbackSizeInBits / BitsPerByte,
                encrypting: true);

            using (transform)
            {
                return(transform.TransformOneShot(plaintext, destination, out bytesWritten));
            }
        }
예제 #2
0
        protected override bool TryDecryptEcbCore(
            ReadOnlySpan <byte> ciphertext,
            Span <byte> destination,
            PaddingMode paddingMode,
            out int bytesWritten)
        {
            if (!ValidKeySize(Key.Length, out int keySize))
            {
                throw new InvalidOperationException(SR.Cryptography_InvalidKeySize);
            }

            int effectiveKeySize = EffectiveKeySizeValue == 0 ? keySize : EffectiveKeySize;
            UniversalCryptoTransform transform = CreateTransformCore(
                CipherMode.ECB,
                paddingMode,
                Key,
                effectiveKeyLength: effectiveKeySize,
                iv: null,
                blockSize: BlockSize / BitsPerByte,
                0, /*feedback size */
                paddingSize: BlockSize / BitsPerByte,
                encrypting: false);

            using (transform)
            {
                return(transform.TransformOneShot(ciphertext, destination, out bytesWritten));
            }
        }
        private ICryptoTransform CreateCryptoTransformCore(Func <CngKey> cngKeyFactory, byte[] iv, bool encrypting)
        {
            int blockSizeInBytes        = _outer.BlockSize.BitSizeToByteSize();
            BasicSymmetricCipher cipher = new BasicSymmetricCipherCng(cngKeyFactory, _outer.Mode, blockSizeInBytes, iv, encrypting);

            return(UniversalCryptoTransform.Create(_outer.Padding, cipher, encrypting));
        }
예제 #4
0
        private static ICryptoTransform CreateTransformCore(
            CipherMode cipherMode,
            PaddingMode paddingMode,
            byte[] key,
            int effectiveKeyLength,
            byte[]?iv,
            int blockSize,
            int feedbackSize,
            int paddingSize,
            bool encrypting)
        {
            // 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.EvpRC2Cbc();
                break;

            case CipherMode.ECB:
                algorithm = Interop.Crypto.EvpRC2Ecb();
                break;

            default:
                throw new NotSupportedException();
            }

            BasicSymmetricCipher cipher = new OpenSslCipher(algorithm, cipherMode, blockSize, paddingSize, key, effectiveKeyLength, iv, encrypting);

            return(UniversalCryptoTransform.Create(paddingMode, cipher, encrypting));
        }
        private ICryptoTransform CreatePersistedCryptoTransformCore(Func <CngKey> cngKeyFactory, byte[] iv, bool encrypting)
        {
            // note: iv is guaranteed to be cloned before this method, so no need to clone it again

            int blockSizeInBytes        = _outer.BlockSize.BitSizeToByteSize();
            BasicSymmetricCipher cipher = new BasicSymmetricCipherNCrypt(cngKeyFactory, _outer.Mode, blockSizeInBytes, iv, encrypting);

            return(UniversalCryptoTransform.Create(_outer.Padding, cipher, encrypting));
        }
        private ICryptoTransform CreateTransformCore(
            CipherMode cipherMode,
            PaddingMode paddingMode,
            byte[] key,
            byte[] iv,
            int blockSize,
            bool encrypting)
        {
            BasicSymmetricCipher cipher = new TripleDesOpenSslCipher(cipherMode, blockSize, key, iv, encrypting);

            return(UniversalCryptoTransform.Create(paddingMode, cipher, encrypting));
        }
예제 #7
0
        private static ICryptoTransform CreateTransformCore(
            CipherMode cipherMode,
            PaddingMode paddingMode,
            byte[] key,
            byte[] iv,
            int blockSize,
            bool encrypting)
        {
            SafeAlgorithmHandle algorithm = DesBCryptModes.GetSharedHandle(cipherMode);

            BasicSymmetricCipher cipher = new BasicSymmetricCipherBCrypt(algorithm, cipherMode, blockSize, key, false, iv, encrypting);

            return(UniversalCryptoTransform.Create(paddingMode, cipher, encrypting));
        }
예제 #8
0
        private static ICryptoTransform CreateTransformCore(
            CipherMode cipherMode,
            PaddingMode paddingMode,
            byte[] key,
            byte[] iv,
            int blockSize,
            bool encrypting)
        {
            SafeAlgorithmHandle algorithm = GetCipherAlgorithm(cipherMode);

            BasicSymmetricCipher cipher = new CngCipher(algorithm, cipherMode, blockSize, key, iv, encrypting);

            return(UniversalCryptoTransform.Create(paddingMode, cipher, encrypting));
        }
        private ICryptoTransform CreateEphemeralCryptoTransformCore(byte[] key, byte[] iv, bool encrypting)
        {
            int blockSizeInBytes = _outer.BlockSize.BitSizeToByteSize();
            SafeAlgorithmHandle algorithmModeHandle = _outer.GetEphemeralModeHandle();

            BasicSymmetricCipher cipher = new BasicSymmetricCipherBCrypt(
                algorithmModeHandle,
                _outer.Mode,
                blockSizeInBytes,
                key,
                iv,
                encrypting);

            return(UniversalCryptoTransform.Create(_outer.Padding, cipher, encrypting));
        }
예제 #10
0
        private static ICryptoTransform CreateTransformCore(
            CipherMode cipherMode,
            PaddingMode paddingMode,
            byte[] key,
            byte[] iv,
            int blockSize,
            bool encrypting)
        {
            // The algorithm pointer is a static pointer, so not having any cleanup code is correct.
            IntPtr algorithm = GetAlgorithm(key.Length * 8, cipherMode);

            BasicSymmetricCipher cipher = new OpenSslCipher(algorithm, cipherMode, blockSize, key, iv, encrypting);

            return(UniversalCryptoTransform.Create(paddingMode, cipher, encrypting));
        }
 private static ICryptoTransform CreateTransformCore(
     CipherMode cipherMode,
     PaddingMode paddingMode,
     byte[] key,
     int effectiveKeyLength,
     byte[]?iv,
     int blockSize,
     bool encrypting)
 {
     using (SafeAlgorithmHandle algorithm = RC2BCryptModes.GetHandle(cipherMode, effectiveKeyLength))
     {
         // The BasicSymmetricCipherBCrypt ctor will increase algorithm reference count and take ownership.
         BasicSymmetricCipher cipher = new BasicSymmetricCipherBCrypt(algorithm, cipherMode, blockSize, key, true, iv, encrypting);
         return(UniversalCryptoTransform.Create(paddingMode, cipher, encrypting));
     }
 }
        private UniversalCryptoTransform CreatePersistedCryptoTransformCore(Func <CngKey> cngKeyFactory, byte[]?iv, bool encrypting, PaddingMode padding, CipherMode mode)
        {
            // note: iv is guaranteed to be cloned before this method, so no need to clone it again

            int blockSizeInBytes        = _outer.BlockSize.BitSizeToByteSize();
            int feedbackSizeInBytes     = _outer.FeedbackSize;
            BasicSymmetricCipher cipher = new BasicSymmetricCipherNCrypt(
                cngKeyFactory,
                mode,
                blockSizeInBytes,
                iv,
                encrypting,
                feedbackSizeInBytes,
                _outer.GetPaddingSize(mode, _outer.FeedbackSize));

            return(UniversalCryptoTransform.Create(padding, cipher, encrypting));
        }
        private UniversalCryptoTransform CreateEphemeralCryptoTransformCore(byte[] key, byte[]?iv, bool encrypting, PaddingMode padding, CipherMode mode)
        {
            int blockSizeInBytes = _outer.BlockSize.BitSizeToByteSize();
            SafeAlgorithmHandle algorithmModeHandle = _outer.GetEphemeralModeHandle(mode);

            BasicSymmetricCipher cipher = new BasicSymmetricCipherBCrypt(
                algorithmModeHandle,
                mode,
                blockSizeInBytes,
                _outer.GetPaddingSize(mode, _outer.FeedbackSize),
                key,
                ownsParentHandle: false,
                iv,
                encrypting);

            return(UniversalCryptoTransform.Create(padding, cipher, encrypting));
        }
예제 #14
0
        private static ICryptoTransform CreateTransformCore(
            CipherMode cipherMode,
            PaddingMode paddingMode,
            byte[] key,
            byte[] iv,
            int blockSize,
            bool encrypting)
        {
            BasicSymmetricCipher cipher = new AppleCCCryptor(
                Interop.AppleCrypto.PAL_SymmetricAlgorithm.TripleDES,
                cipherMode,
                blockSize,
                key,
                iv,
                encrypting);

            return(UniversalCryptoTransform.Create(paddingMode, cipher, encrypting));
        }
예제 #15
0
        private static UniversalCryptoTransform CreateTransformCore(
            CipherMode cipherMode,
            PaddingMode paddingMode,
            byte[] key,
            byte[]?iv,
            int blockSize,
            int feedbackSize,
            int paddingSize,
            bool encrypting)
        {
            // The algorithm pointer is a static pointer, so not having any cleanup code is correct.
            IntPtr algorithm = GetAlgorithm(cipherMode, feedbackSize);

            Interop.Crypto.EnsureLegacyAlgorithmsRegistered();

            BasicSymmetricCipher cipher = new OpenSslCipher(algorithm, cipherMode, blockSize, paddingSize, key, iv, encrypting);

            return(UniversalCryptoTransform.Create(paddingMode, cipher, encrypting));
        }
예제 #16
0
        protected override bool TryEncryptEcbCore(
            ReadOnlySpan <byte> plaintext,
            Span <byte> destination,
            PaddingMode paddingMode,
            out int bytesWritten)
        {
            UniversalCryptoTransform transform = CreateTransformCore(
                CipherMode.ECB,
                paddingMode,
                Key,
                iv: null,
                blockSize: BlockSize / BitsPerByte,
                paddingSize: BlockSize / BitsPerByte,
                0, /*feedback size */
                encrypting: true);

            using (transform)
            {
                return(transform.TransformOneShot(plaintext, destination, out bytesWritten));
            }
        }
예제 #17
0
        private static UniversalCryptoTransform CreateTransformCore(
            CipherMode cipherMode,
            PaddingMode paddingMode,
            byte[] key,
            byte[]?iv,
            int blockSize,
            int feedbackSize,
            int paddingSize,
            bool encrypting)
        {
            // The algorithm pointer is a static pointer, so not having any cleanup code is correct.
            IntPtr algorithm = IntPtr.Zero;

            switch (cipherMode)
            {
            case CipherMode.CBC:
                algorithm = Interop.Crypto.EvpDesCbc();
                break;

            case CipherMode.ECB:
                algorithm = Interop.Crypto.EvpDesEcb();
                break;

            case CipherMode.CFB:

                Debug.Assert(feedbackSize == 1, "DES with CFB should have FeedbackSize set to 1");
                algorithm = Interop.Crypto.EvpDesCfb8();

                break;

            default:
                throw new NotSupportedException();
            }

            Interop.Crypto.EnsureLegacyAlgorithmsRegistered();

            BasicSymmetricCipher cipher = new OpenSslCipher(algorithm, cipherMode, blockSize, paddingSize, key, 0, iv, encrypting);

            return(UniversalCryptoTransform.Create(paddingMode, cipher, encrypting));
        }
예제 #18
0
        private static UniversalCryptoTransform CreateTransformCore(
            CipherMode cipherMode,
            PaddingMode paddingMode,
            byte[] key,
            byte[]?iv,
            int blockSize,
            int feedbackSizeInBytes,
            int paddingSize,
            bool encrypting)
        {
            BasicSymmetricCipher cipher = new AppleCCCryptor(
                Interop.AppleCrypto.PAL_SymmetricAlgorithm.RC2,
                cipherMode,
                blockSize,
                key,
                iv,
                encrypting,
                feedbackSizeInBytes,
                paddingSize);

            return(UniversalCryptoTransform.Create(paddingMode, cipher, encrypting));
        }
예제 #19
0
        protected override bool TryDecryptCbcCore(
            ReadOnlySpan <byte> ciphertext,
            ReadOnlySpan <byte> iv,
            Span <byte> destination,
            PaddingMode paddingMode,
            out int bytesWritten)
        {
            UniversalCryptoTransform transform = CreateTransformCore(
                CipherMode.CBC,
                paddingMode,
                Key,
                iv: iv.ToArray(),
                blockSize: BlockSize / BitsPerByte,
                paddingSize: BlockSize / BitsPerByte,
                0, /*feedback size */
                encrypting: false);

            using (transform)
            {
                return(transform.TransformOneShot(ciphertext, destination, out bytesWritten));
            }
        }
예제 #20
0
        private static UniversalCryptoTransform CreateTransformCore(
            CipherMode cipherMode,
            PaddingMode paddingMode,
            byte[] key,
            byte[]?iv,
            int blockSize,
            int paddingSize,
            int feedbackSize,
            bool encrypting)
        {
            // The algorithm pointer is a static pointer, so not having any cleanup code is correct.
            IntPtr algorithm;

            switch ((cipherMode, feedbackSize))
            {
            case (CipherMode.CBC, _):
                algorithm = Interop.Crypto.EvpDes3Cbc();
                break;

            case (CipherMode.ECB, _):
                algorithm = Interop.Crypto.EvpDes3Ecb();
                break;

            case (CipherMode.CFB, 1):
                algorithm = Interop.Crypto.EvpDes3Cfb8();
                break;

            case (CipherMode.CFB, 8):
                algorithm = Interop.Crypto.EvpDes3Cfb64();
                break;

            default:
                throw new NotSupportedException();
            }

            BasicSymmetricCipher cipher = new OpenSslCipher(algorithm, cipherMode, blockSize, paddingSize, key, 0, iv, encrypting);

            return(UniversalCryptoTransform.Create(paddingMode, cipher, encrypting));
        }