/// <summary>
 ///     Create an AuthenticatedSymmetricAlgorithm which logs the encryption operation of the input
 ///     algorithm, but does not monitor for thread safe access to the object.
 /// </summary>
 public static AuthenticatedSymmetricAlgorithm EnableLogging(this AuthenticatedSymmetricAlgorithm loggedAlgorithm)
 {
     return(loggedAlgorithm.EnableLogging(new SymmetricAlgorithmDiagnosticOptions()
     {
         CheckThreadSafety = false
     }));
 }
        /// <summary>
        ///     Create a SymmetricAlgorithm which logs the encryption operation of the input algorithm
        /// </summary>
        public static SymmetricAlgorithm EnableLogging(this SymmetricAlgorithm loggedAlgorithm,
                                                       SymmetricAlgorithmDiagnosticOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

#if !FXONLY_BUILD
            AuthenticatedSymmetricAlgorithm authenticatedLoggedAlgorithm =
                loggedAlgorithm as AuthenticatedSymmetricAlgorithm;

            if (authenticatedLoggedAlgorithm != null)
            {
                return(new AuthenticatedSymmetricAlgorithmLogger(authenticatedLoggedAlgorithm,
                                                                 options.CheckThreadSafety ? options.LockCheckCallback : null,
                                                                 options.CheckThreadSafety ? options.LockCheckParameter : null));
            }
            else
#endif // !FXONLY_BUILD
            {
                return(new SymmetricAlgorithmLogger(loggedAlgorithm,
                                                    options.CheckThreadSafety ? options.LockCheckCallback : null,
                                                    options.CheckThreadSafety ? options.LockCheckParameter : null));
            }
        }
 /// <summary>
 ///     Create an AuthenticatedSymmetricAlgorithm which verifies the decryption operations done on it
 ///     have state which matches captured encryption state. This overload does not monitor for thread
 ///     safe access to the object.
 /// </summary>
 public static AuthenticatedSymmetricAlgorithm EnableDecryptionVerification(this AuthenticatedSymmetricAlgorithm loggedAlgorithm,
                                                                            AuthenticatedSymmetricEncryptionState encryptionState)
 {
     return(loggedAlgorithm.EnableDecryptionVerification(encryptionState,
                                                         new SymmetricAlgorithmDiagnosticOptions()
     {
         CheckThreadSafety = false
     }));
 }
        public static AuthenticatedSymmetricAlgorithm EnableLogging(this AuthenticatedSymmetricAlgorithm loggedAlgorithm,
                                                                    SymmetricAlgorithmDiagnosticOptions options)
        {
            AuthenticatedSymmetricAlgorithm wrappedAlgorithm =
                (loggedAlgorithm as SymmetricAlgorithm).EnableLogging(options) as AuthenticatedSymmetricAlgorithm;

            Debug.Assert(wrappedAlgorithm != null, "Logged authenticated algorithm did not wrap into an authenticated algortihm");
            return(wrappedAlgorithm);
        }
Пример #5
0
 internal AuthenticatedSymmetricAlgorithmVerifier(AuthenticatedSymmetricAlgorithm verificationAlgorithm,
                                                  AuthenticatedSymmetricEncryptionState encryptionState,
                                                  Predicate <CryptographyLockContext <SymmetricAlgorithm> > lockCheckCallback,
                                                  object lockCheckParameter)
     : base(verificationAlgorithm, lockCheckCallback, lockCheckParameter)
 {
     Debug.Assert(encryptionState != null, "encryptionState != null");
     m_encryptionState = encryptionState;
 }
        public static AuthenticatedSymmetricAlgorithm EnableDecryptionVerification(this AuthenticatedSymmetricAlgorithm loggedAlgorithm,
                                                                                   AuthenticatedSymmetricEncryptionState encryptionState,
                                                                                   SymmetricAlgorithmDiagnosticOptions options)
        {
            AuthenticatedSymmetricAlgorithm wrappedAlgorithm =
                (loggedAlgorithm as SymmetricAlgorithm).EnableDecryptionVerification(encryptionState, options) as AuthenticatedSymmetricAlgorithm;

            Debug.Assert(wrappedAlgorithm != null, "Logged authenticated algorithm did not wrap into an authenticated algortihm");
            return(wrappedAlgorithm);
        }
        /// <summary>
        ///     Get the last encryption state from an algorithm logged with an AuthenticatedSymmetricAlgorithmLogger.
        /// </summary>
        public static AuthenticatedSymmetricEncryptionState GetLastEncryptionState(this AuthenticatedSymmetricAlgorithm loggedAlgorithm)
        {
            AuthenticatedSymmetricAlgorithmLogger logger = loggedAlgorithm as AuthenticatedSymmetricAlgorithmLogger;

            if (logger == null)
            {
                throw new InvalidOperationException(Properties.Resources.EncryptionStateRequiresSymetricAlgorithmLogger);
            }

            return(logger.LastEncryptionState);
        }
        internal AuthenticatedSymmetricAlgorithmShim(AuthenticatedSymmetricAlgorithm wrappedAlgorithm,
                                                     Predicate <CryptographyLockContext <SymmetricAlgorithm> > lockCheckCallback,
                                                     object lockCheckParameter)
        {
            Debug.Assert(wrappedAlgorithm != null, "wrappedAlgorithm != null");
            m_wrappedAlgorithm = wrappedAlgorithm;

            if (lockCheckCallback != null)
            {
                m_lockCheckCallback = lockCheckCallback;
                m_lockCheckContext  = new CryptographyLockContext <SymmetricAlgorithm>(m_wrappedAlgorithm, lockCheckParameter);
            }
        }
        /// <summary>
        ///     Create a SymmetricAlgorithm which verifies the decryption operations done on it have state
        ///     which matches captured encryption state.
        /// </summary>
        public static SymmetricAlgorithm EnableDecryptionVerification(this SymmetricAlgorithm loggedAlgorithm,
                                                                      SymmetricEncryptionState encryptionState,
                                                                      SymmetricAlgorithmDiagnosticOptions options)
        {
            if (encryptionState == null)
            {
                throw new ArgumentNullException("encryptionState");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

#if !FXONLY_BUILD
            AuthenticatedSymmetricAlgorithm authenticatedLoggedAlgorithm =
                loggedAlgorithm as AuthenticatedSymmetricAlgorithm;

            if (authenticatedLoggedAlgorithm != null)
            {
                AuthenticatedSymmetricEncryptionState authenticatedEncryptionState =
                    encryptionState as AuthenticatedSymmetricEncryptionState;

                if (authenticatedEncryptionState == null)
                {
                    throw new ArgumentException(Resources.NeedAuthenticatedEncryptionState, "encryptionState");
                }

                return(new AuthenticatedSymmetricAlgorithmVerifier(authenticatedLoggedAlgorithm,
                                                                   authenticatedEncryptionState,
                                                                   options.CheckThreadSafety ? options.LockCheckCallback : null,
                                                                   options.CheckThreadSafety ? options.LockCheckParameter : null));
            }
            else
#endif // !FXONLY_BUILD
            {
                return(new SymmetricAlgorithmVerifier(loggedAlgorithm,
                                                      encryptionState,
                                                      options.CheckThreadSafety ? options.LockCheckCallback : null,
                                                      options.CheckThreadSafety ? options.LockCheckParameter : null));
            }
        }
        /// <summary>
        ///     Capture the parameters used for encryption, and verify that they make sense together
        /// </summary>
        internal AuthenticatedSymmetricEncryptionState(byte[] key,
                                                       byte[] iv,
                                                       byte[] authenticatedData,
                                                       AuthenticatedSymmetricAlgorithm algorithm)
            : base(key, iv, algorithm, typeof(AuthenticatedSymmetricAlgorithm), false)
        {
            if (authenticatedData != null)
            {
                m_authenticatedData = new byte[authenticatedData.Length];
                Array.Copy(authenticatedData, m_authenticatedData, m_authenticatedData.Length);
            }

            // Since CipherMode doesn't contain authenticated encryption modes, it's not very useful for
            // debugging mode mismatches in authenticated symmetric algorithms.  Our only current
            // authenticated symmetric algorithm implementations ues CNG chaining modes, so we'll grab that
            // if it's available to aid in debugging.
            ICngSymmetricAlgorithm cngAlgorithm = algorithm as ICngSymmetricAlgorithm;

            if (cngAlgorithm != null)
            {
                m_cngChainingMode = cngAlgorithm.CngMode;
            }
        }
 internal AuthenticatedSymmetricAlgorithmLogger(AuthenticatedSymmetricAlgorithm wrappedAlgorithm,
                                                Predicate <CryptographyLockContext <SymmetricAlgorithm> > lockCheckCallback,
                                                object lockCheckParameter)
     : base(wrappedAlgorithm, lockCheckCallback, lockCheckParameter)
 {
 }