System.Security.Cryptography 的 DeriveKeyFromHash() 实际上做了什么?

What does System.Security.Cryptography's DeriveKeyFromHash() actually do?

我正在 C# 应用程序和用 C 语言编程的嵌入式设备之间实现安全交换。System.Security.Cryptography 没有关于其任何加密算法的功能的文档 "under the hood"。我需要知道这一点才能在 C 中实现相同的功能,为此我有更多的基本库。具体来说,我想知道 ECDiffieHellman.DeriveKeyFromHash(HashAlgorithmName.SHA256) 做了什么。

我试过跟随 API 向下,但它以调用 ncrypt.h 中的非托管 NCryptDeriveKey 结束。

该方法似乎并不简单地执行共享机密的 SHA256 哈希。我在 C 端做了这个,我得到了不同的值。我知道在 C# 端收到的 Public Key 是有效的,否则之前的方法会抛出异常。我猜数据是按照某些 NIST 文档的规定附加或前置的,但情况是这样吗?或者还有什么我想念的吗?

编辑:原来我在 C 端做错了什么。 C# 行为的额外确认有助于缩小搜索范围。

ECDiffieHellmanCng ecdh = new ECDiffieHellmanCng(myEccKeys);

//This would throw an exception if PeerRawPublicKey was invalid.
var PeerPublicKey = ECDiffieHellmanCngPublicKey.FromByteArray(PeerRawPublicKey.ToArray(), CngKeyBlobFormat.EccPublicBlob);

//How is keyMaterial generated?
byte[] keyMaterial = ecdh.DeriveKeyFromHash(PeerPublicKey, HashAlgorithmName.SHA256, null, null);

它只是计算 Hash(prepend || Z || append),其中 Z 是共享秘密的 X 坐标。

对于prepend 和append 中的每一个,null 值都被视为空值。

The corefx non-Windows implementation