From 2f44022b826651b7d7380dbe7dce95bf5eb59fe7 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Wed, 10 Apr 2019 12:21:22 +1200 Subject: [PATCH] fix(EphermalKey): correct byte length shared secret #59 (#82) The byte length of the shared secret is constant. --- PeerTalk/src/Cryptography/EphermalKey.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/PeerTalk/src/Cryptography/EphermalKey.cs b/PeerTalk/src/Cryptography/EphermalKey.cs index 2a0992d4..4bdad86c 100644 --- a/PeerTalk/src/Cryptography/EphermalKey.cs +++ b/PeerTalk/src/Cryptography/EphermalKey.cs @@ -2,6 +2,7 @@ using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; +using Org.BouncyCastle.Utilities; using ProtoBuf; using System; using System.Collections.Generic; @@ -42,15 +43,21 @@ public byte[] PublicKeyBytes() /// /// Create a shared secret between this key and another. /// - /// - /// + /// + /// Another ephermal key. + /// + /// + /// The shared secret as a byte array. + /// + /// + /// Uses the ECDH agreement algorithm to generate the shared secet. + /// public byte[] GenerateSharedSecret(EphermalKey other) { var agreement = AgreementUtilities.GetBasicAgreement("ECDH"); agreement.Init(privateKey); - return agreement - .CalculateAgreement(other.publicKey) - .ToByteArrayUnsigned(); + var secret = agreement.CalculateAgreement(other.publicKey); + return BigIntegers.AsUnsignedByteArray(agreement.GetFieldSize(), secret); } ///