예제 #1
0
        /// <inheritdoc />
        public byte[] DeriveKeyMaterial(IECDiffieHellmanPublicKey otherParty)
        {
            Requires.NotNull(otherParty, nameof(otherParty));

            var other = (ECDiffieHellmanPublicKey)otherParty;
            return this.platformAlgorithm.DeriveKeyMaterial(other.PlatformPublicKey);
        }
예제 #2
0
        /// <inheritdoc />
        public byte[] DeriveKeyMaterial(IECDiffieHellmanPublicKey otherParty)
        {
            Requires.NotNull(otherParty, nameof(otherParty));

            var other = (ECDiffieHellmanPublicKey)otherParty;

            return(this.platformAlgorithm.DeriveKeyMaterial(other.PlatformPublicKey));
        }
예제 #3
0
        /// <inheritdoc />
        public byte[] DeriveKeyMaterial(IECDiffieHellmanPublicKey otherParty)
        {
            Requires.NotNull(otherParty, nameof(otherParty));

            var publicKey = (ECDiffieHellmanPublicKey)otherParty;
            using (var secret = BCryptSecretAgreement(this.PlatformKey, publicKey.Key))
            {
                IntPtr hashAlgorithmPtr = IntPtr.Zero;
                try
                {
                    string hashAlgorithmString = HashAlgorithmProviderFactory.GetHashAlgorithmName(this.HashAlgorithm);
                    try
                    {
                    }
                    finally
                    {
                        // Do this in a finally so that ThreadAbortException doesn't interrupt the
                        // assignment of a successfully allocated pointer.
                        hashAlgorithmPtr = Marshal.StringToHGlobalUni(hashAlgorithmString);
                    }

                    var parameters = new List<BCryptBuffer>();
                    parameters.Add(new BCryptBuffer
                    {
                        cbBuffer = (hashAlgorithmString.Length + 1) * sizeof(char),
                        BufferType = BufferType.KDF_HASH_ALGORITHM,
                        pvBuffer_IntPtr = hashAlgorithmPtr,
                    });

                    const string kdf = KeyDerivationFunctions.HASH;
                    unsafe
                    {
                        fixed (BCryptBuffer* pParameters = parameters.ToArray())
                        {
                            var parameterDesc = new BCryptBufferDesc
                            {
                                ulVersion = 0,
                                cBuffers = parameters.Count,
                                pBuffers = pParameters,
                            };

                            int secretLength;
                            BCryptDeriveKey(
                                secret,
                                kdf,
                                ref parameterDesc,
                                null,
                                0,
                                out secretLength,
                                BCryptDeriveKeyFlags.KDF_USE_SECRET_AS_HMAC_KEY_FLAG).ThrowOnError();

                            byte[] derivedKey = new byte[secretLength];
                            BCryptDeriveKey(
                                secret,
                                kdf,
                                ref parameterDesc,
                                derivedKey,
                                derivedKey.Length,
                                out secretLength,
                                0).ThrowOnError();
                            Assumes.True(secretLength == derivedKey.Length);
                            return derivedKey;
                        }
                    }
                }
                finally
                {
                    if (hashAlgorithmPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(hashAlgorithmPtr);
                    }
                }
            }
        }
예제 #4
0
        /// <inheritdoc />
        public byte[] DeriveKeyMaterial(IECDiffieHellmanPublicKey otherParty)
        {
            Requires.NotNull(otherParty, nameof(otherParty));

            var publicKey = (ECDiffieHellmanPublicKey)otherParty;

            using (var secret = BCryptSecretAgreement(this.PlatformKey, publicKey.Key))
            {
                IntPtr hashAlgorithmPtr = IntPtr.Zero;
                try
                {
                    string hashAlgorithmString = HashAlgorithmProviderFactory.GetHashAlgorithmName(this.HashAlgorithm);
                    try
                    {
                    }
                    finally
                    {
                        // Do this in a finally so that ThreadAbortException doesn't interrupt the
                        // assignment of a successfully allocated pointer.
                        hashAlgorithmPtr = Marshal.StringToHGlobalUni(hashAlgorithmString);
                    }

                    var parameters = new List <BCryptBuffer>();
                    parameters.Add(new BCryptBuffer
                    {
                        cbBuffer        = (hashAlgorithmString.Length + 1) * sizeof(char),
                        BufferType      = BufferType.KDF_HASH_ALGORITHM,
                        pvBuffer_IntPtr = hashAlgorithmPtr,
                    });

                    const string kdf = KeyDerivationFunctions.BCRYPT_KDF_HASH;
                    unsafe
                    {
                        fixed(BCryptBuffer *pParameters = parameters.ToArray())
                        {
                            var parameterDesc = new BCryptBufferDesc
                            {
                                ulVersion = 0,
                                cBuffers  = parameters.Count,
                                pBuffers  = pParameters,
                            };

                            int secretLength;

                            BCryptDeriveKey(
                                secret,
                                kdf,
                                ref parameterDesc,
                                null,
                                0,
                                out secretLength,
                                BCryptDeriveKeyFlags.KDF_USE_SECRET_AS_HMAC_KEY_FLAG).ThrowOnError();

                            byte[] derivedKey = new byte[secretLength];
                            BCryptDeriveKey(
                                secret,
                                kdf,
                                ref parameterDesc,
                                derivedKey,
                                derivedKey.Length,
                                out secretLength,
                                0).ThrowOnError();
                            Assumes.True(secretLength == derivedKey.Length);
                            return(derivedKey);
                        }
                    }
                }
                finally
                {
                    if (hashAlgorithmPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(hashAlgorithmPtr);
                    }
                }
            }
        }