Skip to content

Commit

Permalink
fix(EphermalKey): correct byte length shared secret #59 (#82)
Browse files Browse the repository at this point in the history
The byte length of the shared secret is constant.
  • Loading branch information
richardschneider authored Apr 10, 2019
1 parent 2f283bd commit 2f44022
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions PeerTalk/src/Cryptography/EphermalKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,15 +43,21 @@ public byte[] PublicKeyBytes()
/// <summary>
/// Create a shared secret between this key and another.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
/// <param name="other">
/// Another ephermal key.
/// </param>
/// <returns>
/// The shared secret as a byte array.
/// </returns>
/// <remarks>
/// Uses the ECDH agreement algorithm to generate the shared secet.
/// </remarks>
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);
}

/// <summary>
Expand Down

0 comments on commit 2f44022

Please sign in to comment.