Ejemplo n.º 1
0
        public static void InitializeSession(SessionState sessionState,
					                          UInt32 sessionVersion,
					                          AliceAxolotlParameters parameters)
        {
            try {
                sessionState.SetSessionVersion(sessionVersion);
                sessionState.SetRemoteIdentityKey(parameters.TheirIdentityKey);
                sessionState.SetLocalIdentityKey(parameters.OurIdentityKey.PublicKey);

                ECKeyPair             sendingRatchetKey = Curve.GenerateKeyPair();

                byte[] secrets;
                using(var stream = new MemoryStream())
                using(var sw = new BinaryWriter(stream))
                {
                    byte[] buf;

                    if (sessionVersion >= 3) {
                        buf = GetDiscontinuityBytes();
                        sw.Write(buf);
                    }

                    buf = Curve.CalculateAgreement(parameters.TheirSignedPreKey,
                                                   parameters.OurIdentityKey.PrivateKey);

                    sw.Write(buf);

                    buf = Curve.CalculateAgreement(parameters.TheirIdentityKey.PublicKey,
                                                   parameters.OurBaseKey.PrivateKey);

                    sw.Write(buf);

                    buf = Curve.CalculateAgreement(parameters.TheirSignedPreKey,
                                                   parameters.OurBaseKey.PrivateKey);

                    sw.Write(buf);

                    if (sessionVersion >= 3 && parameters.TheirOneTimePreKey.IsSomething()) {
                        parameters.TheirOneTimePreKey.Do(pKey => {
                            buf = Curve.CalculateAgreement(pKey,
                                                           parameters.OurBaseKey.PrivateKey);
                            sw.Write(buf);
                        });
                    }

                    sw.Flush();

                    secrets = stream.ToArray();
                }

                DerivedKeys             derivedKeys  = CalculateDerivedKeys(sessionVersion, secrets);
                Tuple<RootKey, ChainKey> sendingChain = derivedKeys.RootKey.CreateChain(parameters.TheirRatchetKey, sendingRatchetKey);

                sessionState.AddReceiverChain(parameters.TheirRatchetKey, derivedKeys.ChainKey);
                sessionState.SetSenderChain(sendingRatchetKey, sendingChain.Item2);
                sessionState.RootKey = sendingChain.Item1;
            } catch (Exception e) {
                throw new InvalidOperationException("Assertion error" + e);
            }
        }
Ejemplo n.º 2
0
        public static void InitializeSession(SessionState sessionState,
					                          UInt32 sessionVersion,
					                          BobAxolotlParameters parameters)
        {
            try {
                sessionState.SetSessionVersion(sessionVersion);
                sessionState.SetRemoteIdentityKey(parameters.TheirIdentityKey);
                sessionState.SetLocalIdentityKey(parameters.OurIdentityKey.PublicKey);

                byte[] secrets;

                using(var stream = new MemoryStream())
                using(var sw = new BinaryWriter(stream))
                {
                    byte[] buffer;

                    if (sessionVersion >= 3) {
                        buffer = GetDiscontinuityBytes();
                        sw.Write(buffer);
                    }

                    buffer = Curve.CalculateAgreement(parameters.TheirIdentityKey.PublicKey,
                                                      parameters.OurSignedPreKey.PrivateKey);
                    sw.Write(buffer);

                    buffer = Curve.CalculateAgreement(parameters.TheirBaseKey,
                                                      parameters.OurIdentityKey.PrivateKey);
                    sw.Write(buffer);

                    buffer = Curve.CalculateAgreement(parameters.TheirBaseKey,
                                                      parameters.OurSignedPreKey.PrivateKey);
                    sw.Write(buffer);

                    if (sessionVersion >= 3 && parameters.OurOneTimePreKey.IsSomething()) {

                        parameters.OurOneTimePreKey.Do(otpK => {
                            buffer = Curve.CalculateAgreement(parameters.TheirBaseKey, otpK.PrivateKey);
                            sw.Write(buffer);
                        });
                    }

                    sw.Flush();

                    secrets = stream.ToArray();
                }

                DerivedKeys derivedKeys = CalculateDerivedKeys(sessionVersion, secrets);

                sessionState.SetSenderChain(parameters.OurRatchetKey, derivedKeys.ChainKey);
                sessionState.RootKey = derivedKeys.RootKey;
            } catch (Exception e) {
                throw new InvalidOperationException("Assertion error", e);
            }
        }