ECDH Подпись с ECDHsaCng - PullRequest
       62

ECDH Подпись с ECDHsaCng

0 голосов
/ 04 марта 2019

Я обмениваюсь данными с устройством, использующим шифрование AES-CCM, и мне нужно настроить его, используя ключ, полученный из ECDH.У моего машинного сертификата есть закрытый ключ ECC в TPM.

Я немного новичок в этом, поэтому, пожалуйста, потерпите меня.

Вот код, который я сейчас просматриваю.Это правильный способ подписать ключ моим сертификатом?

//this will actually be loaded by another method.  Only here for demo purposes;
X509Certificate2 masterEndEntityCert;

//we are using the NST p-256 curve
using (var ecdh = new ECDiffieHellmanCng(ECCurve.NamedCurves.nistP256))
{
    //our HKDF should be HMAC
    ecdh.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hmac;
    ecdh.HashAlgorithm = CngAlgorithm.Sha256;

    //get the ephemeral key to send to the other device
    var ephemeralKey = ecdh.PublicKey.ToByteArray();

    //get the ecdsa private key from my cert for signing
    using (var alg = masterEndEntityCert.GetECDsaPrivateKey() as ECDsaCng)
    {
        //sign the sha256 hash of the key
        var sig = alg.SignData(ephemeralKey, HashAlgorithmName.SHA256);

        //concat the ephemeral key and the signed hash together for transmission
        this.SignedEphemeralPublicKey = ephemeralKey.Concat(sig).ToArray();
    }
}
...