Exemple #1
0
        private unsafe void OpenCryptor(
            Interop.AppleCrypto.PAL_SymmetricAlgorithm algorithm,
            CipherMode cipherMode,
            byte[] key)
        {
            int ret;
            int ccStatus;

            byte[]? iv = IV;

            fixed(byte *pbKey = key)
            fixed(byte *pbIv = iv)
            {
                ret = Interop.AppleCrypto.CryptorCreate(
                    _encrypting
                        ? Interop.AppleCrypto.PAL_SymmetricOperation.Encrypt
                        : Interop.AppleCrypto.PAL_SymmetricOperation.Decrypt,
                    algorithm,
                    GetPalChainMode(cipherMode),
                    Interop.AppleCrypto.PAL_PaddingMode.None,
                    pbKey,
                    key.Length,
                    pbIv,
                    Interop.AppleCrypto.PAL_SymmetricOptions.None,
                    out _cryptor,
                    out ccStatus);
            }

            ProcessInteropError(ret, ccStatus);
        }
Exemple #2
0
        private Interop.AppleCrypto.PAL_ChainingMode GetPalChainMode(Interop.AppleCrypto.PAL_SymmetricAlgorithm algorithm, CipherMode cipherMode, int feedbackSizeInBytes)
        {
            switch (cipherMode)
            {
            case CipherMode.CBC:
                return(Interop.AppleCrypto.PAL_ChainingMode.CBC);

            case CipherMode.ECB:
                return(Interop.AppleCrypto.PAL_ChainingMode.ECB);

            case CipherMode.CFB:
                if (feedbackSizeInBytes == 1)
                {
                    return(Interop.AppleCrypto.PAL_ChainingMode.CFB8);
                }

                Debug.Assert(
                    (algorithm == Interop.AppleCrypto.PAL_SymmetricAlgorithm.AES && feedbackSizeInBytes == 16) ||
                    (algorithm == Interop.AppleCrypto.PAL_SymmetricAlgorithm.TripleDES && feedbackSizeInBytes == 8));

                return(Interop.AppleCrypto.PAL_ChainingMode.CFB);

            default:
                throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_CipherModeNotSupported, cipherMode));
            }
        }
Exemple #3
0
 private PAL_ChainingMode GetPalChainMode(PAL_SymmetricAlgorithm algorithm, CipherMode cipherMode, int feedbackSizeInBytes)
 {
     return(cipherMode switch
     {
         CipherMode.CBC => PAL_ChainingMode.CBC,
         CipherMode.ECB => PAL_ChainingMode.ECB,
         CipherMode.CFB when feedbackSizeInBytes == 1 => PAL_ChainingMode.CFB8,
         CipherMode.CFB => PAL_ChainingMode.CFB,
         _ => throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_CipherModeNotSupported, cipherMode)),
     });
Exemple #4
0
        public AppleCCCryptor(
            Interop.AppleCrypto.PAL_SymmetricAlgorithm algorithm,
            CipherMode cipherMode,
            int blockSizeInBytes,
            byte[] key,
            byte[]?iv,
            bool encrypting)
            : base(cipherMode.GetCipherIv(iv), blockSizeInBytes)
        {
            _encrypting = encrypting;

            OpenCryptor(algorithm, cipherMode, key);
        }
Exemple #5
0
        public AppleCCCryptor(
            Interop.AppleCrypto.PAL_SymmetricAlgorithm algorithm,
            CipherMode cipherMode,
            int blockSizeInBytes,
            byte[] key,
            byte[]?iv,
            bool encrypting,
            int feedbackSizeInBytes,
            int paddingSizeInBytes)
            : base(cipherMode.GetCipherIv(iv), blockSizeInBytes, paddingSizeInBytes)
        {
            _encrypting = encrypting;

            // CFB is streaming cipher, calling CCCryptorReset is not implemented (and is effectively noop)
            _supportsReset = cipherMode != CipherMode.CFB;

            _algorithm           = algorithm;
            _cipherMode          = cipherMode;
            _key                 = key;
            _feedbackSizeInBytes = feedbackSizeInBytes;

            OpenCryptor();
        }
Exemple #6
0
        public unsafe AppleCCCryptorLite(
            PAL_SymmetricAlgorithm algorithm,
            CipherMode cipherMode,
            int blockSizeInBytes,
            ReadOnlySpan <byte> key,
            ReadOnlySpan <byte> iv,
            bool encrypting,
            int feedbackSizeInBytes,
            int paddingSizeInBytes)
        {
            int ret;
            int ccStatus;

            fixed(byte *pbKey = key)
            fixed(byte *pbIv = iv)
            {
                ret = Interop.AppleCrypto.CryptorCreate(
                    encrypting
                        ? Interop.AppleCrypto.PAL_SymmetricOperation.Encrypt
                        : Interop.AppleCrypto.PAL_SymmetricOperation.Decrypt,
                    algorithm,
                    GetPalChainMode(algorithm, cipherMode, feedbackSizeInBytes),
                    Interop.AppleCrypto.PAL_PaddingMode.None,
                    pbKey,
                    key.Length,
                    pbIv,
                    Interop.AppleCrypto.PAL_SymmetricOptions.None,
                    out _cryptor,
                    out ccStatus);
            }

            ProcessInteropError(ret, ccStatus);

            _canReset          = cipherMode != CipherMode.CFB;
            BlockSizeInBytes   = blockSizeInBytes;
            PaddingSizeInBytes = paddingSizeInBytes;
        }