Exemple #1
0
        //
        // Summary:
        //  Creates a new InfoCardKeyedHashAlgorithm based on a SymmetricCryptoHandle.
        //
        // Parameters:
        //  cryptoHandle  - The handle to the symmetric key on which to base the keyed hash.
        //
        public InfoCardKeyedHashAlgorithm(SymmetricCryptoHandle cryptoHandle)
        {
            InternalRefCountedHandle nativeHandle = null;

            try
            {
                //
                // Call native api to get a hashCryptoHandle.
                //
                int status = CardSpaceSelector.GetShim().m_csShimGetKeyedHash(cryptoHandle.InternalHandle, out nativeHandle);

                if (0 != status)
                {
                    IDT.CloseInvalidOutSafeHandle(nativeHandle);
                    ExceptionHelper.ThrowIfCardSpaceException(status);
                    throw IDT.ThrowHelperError(new Win32Exception(status));
                }

                m_cryptoHandle = (HashCryptoHandle)CryptoHandle.Create(nativeHandle);

                m_param = (RpcHashCryptoParameters)m_cryptoHandle.Parameters;
            }
            catch
            {
                if (null != m_cryptoHandle)
                {
                    m_cryptoHandle.Dispose();
                }
                throw;
            }
        }
            //
            // Parameters:
            //  symAlgo  - the algorithm being requested.
            //  cryptoDirection - determines whether the transform will encrypt or decrypt.
            //
            public CryptoTransform(InfoCardSymmetricAlgorithm symAlgo, Direction cryptoDirection)
            {
                InternalRefCountedHandle nativeHandle = null;

                byte[] iv = symAlgo.IV;
                using (HGlobalSafeHandle pIV = HGlobalSafeHandle.Construct(iv.Length))
                {
                    //
                    // Marshal the initialization vector.
                    //
                    Marshal.Copy(iv, 0, pIV.DangerousGetHandle(), iv.Length);

                    //
                    // Call native method to get a handle to a native transform.
                    //
                    int status = CardSpaceSelector.GetShim().m_csShimGetCryptoTransform(symAlgo.m_cryptoHandle.InternalHandle,
                                                                                        (int)symAlgo.Mode,
                                                                                        (int)symAlgo.Padding,
                                                                                        symAlgo.FeedbackSize,
                                                                                        (int)cryptoDirection,
                                                                                        iv.Length,
                                                                                        pIV,
                                                                                        out nativeHandle);

                    if (0 != status)
                    {
                        IDT.CloseInvalidOutSafeHandle(nativeHandle);
                        ExceptionHelper.ThrowIfCardSpaceException(status);
                        throw IDT.ThrowHelperError(new Win32Exception(status));
                    }

                    m_transCryptoHandle = (TransformCryptoHandle)CryptoHandle.Create(nativeHandle);

                    m_param = (RpcTransformCryptoParameters)m_transCryptoHandle.Parameters;
                }
            }