public static void InitializeSession(SessionState sessionState, uint sessionVersion, AliceAxolotlParameters parameters) { try { sessionState.SetSessionVersion(sessionVersion); sessionState.SetRemoteIdentityKey(parameters.GetTheirIdentityKey()); sessionState.SetLocalIdentityKey(parameters.GetOurIdentityKey().GetPublicKey()); ECKeyPair sendingRatchetKey = Curve.GenerateKeyPair(); MemoryStream secrets = new MemoryStream(); if (sessionVersion >= 3) { byte[] discontinuityBytes = GetDiscontinuityBytes(); secrets.Write(discontinuityBytes, 0, discontinuityBytes.Length); } byte[] agree1 = Curve.CalculateAgreement(parameters.GetTheirSignedPreKey(), parameters.GetOurIdentityKey().GetPrivateKey()); byte[] agree2 = Curve.CalculateAgreement(parameters.GetTheirIdentityKey().GetPublicKey(), parameters.GetOurBaseKey().GetPrivateKey()); byte[] agree3 = Curve.CalculateAgreement(parameters.GetTheirSignedPreKey(), parameters.GetOurBaseKey().GetPrivateKey()); secrets.Write(agree1, 0, agree1.Length); secrets.Write(agree2, 0, agree2.Length); secrets.Write(agree3, 0, agree3.Length); if (sessionVersion >= 3 && parameters.GetTheirOneTimePreKey().HasValue) { byte[] agree4 = Curve.CalculateAgreement(parameters.GetTheirOneTimePreKey().ForceGetValue(), parameters.GetOurBaseKey().GetPrivateKey()); secrets.Write(agree4, 0, agree4.Length); } DerivedKeys derivedKeys = CalculateDerivedKeys(sessionVersion, secrets.ToArray()); Pair <RootKey, ChainKey> sendingChain = derivedKeys.GetRootKey().CreateChain(parameters.GetTheirRatchetKey(), sendingRatchetKey); sessionState.AddReceiverChain(parameters.GetTheirRatchetKey(), derivedKeys.GetChainKey()); sessionState.SetSenderChain(sendingRatchetKey, sendingChain.Second()); sessionState.SetRootKey(sendingChain.First()); } catch (IOException e) { throw new Exception(e.Message); } }
public static void InitializeSession(SessionState sessionState, BobSignalProtocolParameters parameters) { try { sessionState.SetSessionVersion(CiphertextMessage.CurrentVersion); sessionState.SetRemoteIdentityKey(parameters.GetTheirIdentityKey()); sessionState.SetLocalIdentityKey(parameters.GetOurIdentityKey().GetPublicKey()); MemoryStream secrets = new MemoryStream(); byte[] discontinuityBytes = GetDiscontinuityBytes(); secrets.Write(discontinuityBytes, 0, discontinuityBytes.Length); byte[] agree1 = Curve.CalculateAgreement(parameters.GetTheirIdentityKey().GetPublicKey(), parameters.GetOurSignedPreKey().GetPrivateKey()); byte[] agree2 = Curve.CalculateAgreement(parameters.GetTheirBaseKey(), parameters.GetOurIdentityKey().GetPrivateKey()); byte[] agree3 = Curve.CalculateAgreement(parameters.GetTheirBaseKey(), parameters.GetOurSignedPreKey().GetPrivateKey()); secrets.Write(agree1, 0, agree1.Length); secrets.Write(agree2, 0, agree2.Length); secrets.Write(agree3, 0, agree3.Length); if (parameters.GetOurOneTimePreKey().HasValue) { byte[] otAgree = Curve.CalculateAgreement(parameters.GetTheirBaseKey(), parameters.GetOurOneTimePreKey().ForceGetValue().GetPrivateKey()); secrets.Write(otAgree, 0, otAgree.Length); } DerivedKeys derivedKeys = CalculateDerivedKeys(secrets.ToArray()); sessionState.SetSenderChain(parameters.GetOurRatchetKey(), derivedKeys.GetChainKey()); sessionState.SetRootKey(derivedKeys.GetRootKey()); } catch (IOException e) { throw new Exception(e.Message); } }