2017-10-13 4 views
1

:.net 프레임 워크 4.7에서 ECDiffieHellmanCng 구현의 비밀 계약에 대한 질문이 있으십니까? 내가 다음 코드를 한

 var curve = ECCurve.NamedCurves.nistP256; 
     var ecdhSender = ECDiffieHellman.Create(curve); 
     var ecdhReceiver = ECDiffieHellman.Create(curve); 

나의 이해는 나도 ecdhSender 객체를 사용 ecdhReceiver.PublicKey를 사용하거나 ecdhSender.PublicKey와 ecdhReceiver 객체를 사용하는 비밀 계약을 계산 할 수 있어야하고 모두 비밀 계약 값을해야한다는 것입니다 동일 하십시요. 이것이 잘못된 가정이라면 알려주십시오.

 string receiverHexString = null; 
     using (SafeNCryptSecretHandle secretAgreement = (ecdhReceiver as ECDiffieHellmanCng).DeriveSecretAgreementHandle(ecdhSender.PublicKey)) 
     { 
      byte[] secretAgreementBytes = new byte[32]; 
      IntPtr pointer = secretAgreement.DangerousGetHandle(); 
      Marshal.Copy(pointer, secretAgreementBytes, 0, secretAgreementBytes.Length); 
      receiverHexString = BitConverter.ToString(secretAgreementBytes).Replace("-", string.Empty).ToLower(); 
      Console.WriteLine($"receiver secretAgreement: 0x{receiverHexString}"); 
     } 

     string senderHexString = null; 
     using (SafeNCryptSecretHandle secretAgreement = (ecdhSender as ECDiffieHellmanCng).DeriveSecretAgreementHandle(ecdhReceiver.PublicKey)) 
     { 
      byte[] secretAgreementBytes = new byte[32]; 
      IntPtr pointer = secretAgreement.DangerousGetHandle(); 
      Marshal.Copy(pointer, secretAgreementBytes, 0, secretAgreementBytes.Length); 
      senderHexString = BitConverter.ToString(secretAgreementBytes).Replace("-", string.Empty).ToLower(); 
      Console.WriteLine($"sender secretAgreement: 0x{senderHexString}"); 
     } 

     Assert.AreEqual(receiverHexString, senderHexString); 

내 주장이 실패 :

ECDiffieHellman.Create이 ECDiffieHellman 형식을 반환하기 때문에

, 내가 코드를 다음과 쓴 것은 비밀 계약을 얻을 수 있습니다. 분명히 나는 ​​비밀 계약이 동일하다고 가정한다면 어떤 것을 잘못하고있다.

어떻게 핸들에서 바이트를 추출합니까? 또는 다른 것?

+0

달성하려는 목표는 무엇입니까? NCRYPT_SECRET_HANDLE 구조체는 불투명합니다. 메모리를 읽지 않아야합니다. – bartonjs

+0

자세한 단계가있는 파트너의 결과를 확인하려고합니다. 제 질문의 첫 부분에서 내 가정이 정확하거나 틀린 경우 의견을 남길 수 있습니까? – Raghu

답변

1

last time I checked 현재 CNG에서 원시 비밀 계약 값을 얻는 방법에 대한 문서화 된 방법이 없으므로 .NET에서는이를 노출하지 않습니다. 또한

  • DeriveKeyFromHash(ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, byte[] secretPrepend, byte[] secretAppend)
  • DeriveKeyFromHmac(ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, byte[] hmacKey)
  • DeriveKeyFromHmac(ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, byte[] hmacKey, byte[] secretPrepend, byte[] secretAppend)
  • DeriveKeyTls(ECDiffieHellmanPublicKey otherPartyPublicKey, byte[] prfLabel, byte[] prfSeed)
  • ,691있다

    ECCurve curve = ECCurve.NamedCurves.nistP256; 
    
    // Usually you'll only have one private key (yours), so this isn't representative of 
    // real code. An `ECDiffieHellman` object doesn't necessarily have a private key, 
    // but an `ECDiffieHellmanPublicKey` object definitely doesn't. 
    using (ECDiffieHellman bobPrivate = ECDiffieHellman.Create(curve)) 
    using (ECDiffieHellman alicePrivate = ECDiffieHellman.Create(curve)) 
    using (ECDiffieHellmanPublicKey bobPublic = bobPrivate.PublicKey) 
    using (ECDiffieHellmanPublicKey alicePublic = alicePrivate.PublicKey) 
    { 
        byte[] ba = bobPrivate.DeriveKeyFromHash(alicePublic, HashAlgorithmName.SHA256); 
        byte[] ab = alicePrivate.DeriveKeyFromHash(bobPublic, HashAlgorithmName.SHA256); 
    
        // ba and ab have the same contents. 
    } 
    

    .NET에서 ECDH의 예상 사용은

bobPrivate 및 alicePublic (또는 반대로 alicePrivate 및 bobPublic)이 변경되지 않은 경우 동일한 입력을 제공하면 동일한 출력이 생성됩니다.

참고 : 가능한 경우 ECDiffieHellmanCng에 대해 이야기하지 마십시오. ECDH가 결국 .NET 표준으로 변환되면 ECDHCng 클래스는 그렇지 않습니다. .NET 4.6.2부터 모든 것이 기본 클래스에서 수행 될 수 있습니다 (지속 된 키 열기 제외). .NET 코어는 Create() 팩토리 메소드에서 공용 유형을 리턴하지 않는 경향이 있습니다.

관련 문제