public static void initializeSession(SessionState sessionState,
                                             uint sessionVersion,
                                             BobAxolotlParameters parameters)
        {
            try
            {
                sessionState.setSessionVersion(sessionVersion);
                sessionState.setRemoteIdentityKey(parameters.getTheirIdentityKey());
                sessionState.setLocalIdentityKey(parameters.getOurIdentityKey().getPublicKey());

                MemoryStream secrets = new MemoryStream();

                if (sessionVersion >= 3)
                {
                    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 (sessionVersion >= 3 && parameters.getOurOneTimePreKey().HasValue)
                {
                    byte[] agree4 = Curve.calculateAgreement(parameters.getTheirBaseKey(),
                                                             parameters.getOurOneTimePreKey().ForceGetValue().getPrivateKey());
                    secrets.Write(agree4, 0, agree4.Length);
                }

                DerivedKeys derivedKeys = calculateDerivedKeys(sessionVersion, secrets.ToArray());

                sessionState.setSenderChain(parameters.getOurRatchetKey(), derivedKeys.getChainKey());
                sessionState.setRootKey(derivedKeys.getRootKey());
            }
            catch (IOException e)
            {
                throw new Exception(e.Message);
            }
        }
Example #2
0
        public static void initializeSession(SessionState sessionState,
                                             uint sessionVersion,
                                             BobAxolotlParameters parameters)
        {
            try
            {
                sessionState.setSessionVersion(sessionVersion);
                sessionState.setRemoteIdentityKey(parameters.getTheirIdentityKey());
                sessionState.setLocalIdentityKey(parameters.getOurIdentityKey().getPublicKey());

                MemoryStream secrets = new MemoryStream();

                if (sessionVersion >= 3)
                {
                    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 (sessionVersion >= 3 && parameters.getOurOneTimePreKey().HasValue)
                {
                    byte[] agree4 = Curve.calculateAgreement(parameters.getTheirBaseKey(),
                                                           parameters.getOurOneTimePreKey().ForceGetValue().getPrivateKey());
                    secrets.Write(agree4, 0, agree4.Length);
                }

                DerivedKeys derivedKeys = calculateDerivedKeys(sessionVersion, secrets.ToArray());

                sessionState.setSenderChain(parameters.getOurRatchetKey(), derivedKeys.getChainKey());
                sessionState.setRootKey(derivedKeys.getRootKey());
            }
            catch (IOException e)
            {
                throw new Exception(e.Message);
            }
        }