Exemplo n.º 1
0
        public void SymmetricStateConstructorTest()
        {
            uint N = 0;                    // TODO: Initialize to an appropriate value

            SymmetricType[] types  = null; // TODO: Initialize to an appropriate value
            SymmetricState  target = new SymmetricState(N, types);

            Assert.Inconclusive("TODO: Implement code to verify target");
        }
Exemplo n.º 2
0
        public HandshakeState(
            Protocol protocol,
            bool initiator,
            ReadOnlySpan <byte> prologue,
            ReadOnlySpan <byte> s,
            ReadOnlySpan <byte> rs,
            IEnumerable <byte[]> psks)
        {
            Debug.Assert(psks != null);

            if (!s.IsEmpty && s.Length != dh.DhLen)
            {
                throw new ArgumentException("Invalid local static private key.", nameof(s));
            }

            if (!rs.IsEmpty && rs.Length != dh.DhLen)
            {
                throw new ArgumentException("Invalid remote static public key.", nameof(rs));
            }

            if (s.IsEmpty && protocol.HandshakePattern.LocalStaticRequired(initiator))
            {
                throw new ArgumentException("Local static private key required, but not provided.", nameof(s));
            }

            if (!s.IsEmpty && !protocol.HandshakePattern.LocalStaticRequired(initiator))
            {
                throw new ArgumentException("Local static private key provided, but not required.", nameof(s));
            }

            if (rs.IsEmpty && protocol.HandshakePattern.RemoteStaticRequired(initiator))
            {
                throw new ArgumentException("Remote static public key required, but not provided.", nameof(rs));
            }

            if (!rs.IsEmpty && !protocol.HandshakePattern.RemoteStaticRequired(initiator))
            {
                throw new ArgumentException("Remote static public key provided, but not required.", nameof(rs));
            }

            state = new SymmetricState <CipherType, DhType, HashType>(protocol.Name);
            state.MixHash(prologue);

            this.initiator   = initiator;
            this.turnToWrite = initiator;
            this.s           = s.IsEmpty ? null : dh.GenerateKeyPair(s);
            this.rs          = rs.IsEmpty ? null : rs.ToArray();

            ProcessPreMessages(protocol.HandshakePattern);
            ProcessPreSharedKeys(protocol, psks);

            isOneWay = messagePatterns.Count == 1;
            isPsk    = protocol.Modifiers != PatternModifiers.None;
        }
Exemplo n.º 3
0
        public void ToStringTest()
        {
            uint N = 0;                                              // TODO: Initialize to an appropriate value

            SymmetricType[] types    = null;                         // TODO: Initialize to an appropriate value
            SymmetricState  target   = new SymmetricState(N, types); // TODO: Initialize to an appropriate value
            string          expected = string.Empty;                 // TODO: Initialize to an appropriate value
            string          actual;

            actual = target.ToString();
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
Exemplo n.º 4
0
        public void SameTypesTest()
        {
            uint N = 0;                                              // TODO: Initialize to an appropriate value

            SymmetricType[] types    = null;                         // TODO: Initialize to an appropriate value
            SymmetricState  target   = new SymmetricState(N, types); // TODO: Initialize to an appropriate value
            SymmetricState  compare  = null;                         // TODO: Initialize to an appropriate value
            bool            expected = false;                        // TODO: Initialize to an appropriate value
            bool            actual;

            actual = target.SameTypes(compare);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
Exemplo n.º 5
0
        public void Fallback(Protocol protocol, ProtocolConfig config)
        {
            ThrowIfDisposed();
            Exceptions.ThrowIfNull(protocol, nameof(protocol));
            Exceptions.ThrowIfNull(config, nameof(config));

            if (protocol.HandshakePattern != HandshakePattern.XX || protocol.Modifiers != PatternModifiers.Fallback)
            {
                throw new ArgumentException("The only fallback pattern currently supported is XXfallback.");
            }

            if (config.LocalStatic == null)
            {
                throw new ArgumentException("Local static private key is required for the XXfallback pattern.");
            }

            if (initiator == Role.Bob)
            {
                throw new InvalidOperationException("Fallback cannot be applied to a Bob-initiated pattern.");
            }

            if (messagePatterns.Count + 1 != this.protocol.HandshakePattern.Patterns.Count())
            {
                throw new InvalidOperationException("Fallback can only be applied after the first handshake message.");
            }

            this.protocol = null;
            initiator     = Role.Bob;
            turnToWrite   = role == Role.Bob;

            s  = dh.GenerateKeyPair(config.LocalStatic);
            rs = null;

            isPsk    = false;
            isOneWay = false;

            while (psks.Count > 0)
            {
                var psk = psks.Dequeue();
                Utilities.ZeroMemory(psk);
            }

            state.Dispose();
            state = new SymmetricState <CipherType, DhType, HashType>(protocol.Name);
            state.MixHash(config.Prologue);

            if (role == Role.Alice)
            {
                Debug.Assert(e != null && re == null);
                state.MixHash(e.PublicKey);
            }
            else
            {
                Debug.Assert(e == null && re != null);
                state.MixHash(re);
            }

            messagePatterns.Clear();

            foreach (var pattern in protocol.HandshakePattern.Patterns.Skip(1))
            {
                messagePatterns.Enqueue(pattern);
            }
        }
Exemplo n.º 6
0
        public HandshakeState(
            Protocol protocol,
            bool initiator,
            ReadOnlySpan <byte> prologue,
            ReadOnlySpan <byte> s,
            ReadOnlySpan <byte> rs,
            IEnumerable <byte[]> psks)
        {
            Debug.Assert(psks != null);

            if (!s.IsEmpty && s.Length != dh.DhLen)
            {
                throw new ArgumentException("Invalid local static private key.", nameof(s));
            }

            if (!rs.IsEmpty && rs.Length != dh.DhLen)
            {
                throw new ArgumentException("Invalid remote static public key.", nameof(rs));
            }

            if (s.IsEmpty && protocol.HandshakePattern.LocalStaticRequired(initiator))
            {
                throw new ArgumentException("Local static private key required, but not provided.", nameof(s));
            }

            if (!s.IsEmpty && !protocol.HandshakePattern.LocalStaticRequired(initiator))
            {
                throw new ArgumentException("Local static private key provided, but not required.", nameof(s));
            }

            if (rs.IsEmpty && protocol.HandshakePattern.RemoteStaticRequired(initiator))
            {
                throw new ArgumentException("Remote static public key required, but not provided.", nameof(rs));
            }

            if (!rs.IsEmpty && !protocol.HandshakePattern.RemoteStaticRequired(initiator))
            {
                throw new ArgumentException("Remote static public key provided, but not required.", nameof(rs));
            }

            if ((protocol.Modifiers & PatternModifiers.Fallback) != 0)
            {
                throw new ArgumentException($"Fallback modifier can only be applied by calling the {nameof(Fallback)} method.");
            }

            state = new SymmetricState <CipherType, DhType, HashType>(protocol.Name);
            state.MixHash(prologue);

            this.protocol    = protocol;
            this.role        = initiator ? Role.Alice : Role.Bob;
            this.initiator   = Role.Alice;
            this.turnToWrite = initiator;
            this.s           = s.IsEmpty ? null : dh.GenerateKeyPair(s);
            this.rs          = rs.IsEmpty ? null : rs.ToArray();

            ProcessPreMessages(protocol.HandshakePattern);
            ProcessPreSharedKeys(protocol, psks);

            var pskModifiers = PatternModifiers.Psk0 | PatternModifiers.Psk1 | PatternModifiers.Psk2 | PatternModifiers.Psk3;

            isPsk    = (protocol.Modifiers & pskModifiers) != 0;
            isOneWay = messagePatterns.Count == 1;
        }
Exemplo n.º 7
0
        public void ToConcreteTest()
        {
            uint N;
            uint TN;

            N  = 0;
            TN = 0;
            List <SymmetricType> types = new List <SymmetricType>();

            types.Add(new SymmetricType(TN, Controller.Instance.Z3.MkTrue()));
            SymmetricState target   = new SymmetricState(N, types.ToArray());
            Expr           expected = null;
            Expr           actual;

            actual = target.ToConcrete();
            //Assert.AreEqual(expected, actual);

            N     = 1;
            TN    = 1;
            types = new List <SymmetricType>();


            Holism sys = new Holism();
            ConcreteHybridAutomaton h = new ConcreteHybridAutomaton(sys, "test");
            ConcreteLocation        aloc;
            ConcreteLocation        bloc;

            StringReader  tr;
            XmlTextReader reader;

            tr     = new StringReader("<holism><mode id='0' name='aloc' initial='True'></mode><mode id='0' name='bloc' initial='False'></mode></holism>");
            reader = new XmlTextReader(tr);
            reader.Read();
            reader.Read();
            aloc = ParseHyXML.ParseLocation(reader, h);
            reader.Read();
            ParseHyXML.ParseLocationEnd(reader, h, aloc);
            reader.Read();
            bloc = ParseHyXML.ParseLocation(reader, h);
            reader.Read();
            ParseHyXML.ParseLocationEnd(reader, h, bloc);

            expected = LogicalExpression.CreateTerm("q[1] == aloc");
            types.Add(new SymmetricType(TN, LogicalExpression.CreateTerm("q[i] == aloc")));
            target = new SymmetricState(N, types.ToArray());
            actual = target.ToConcrete();
            //expected = expected.Substitute(Controller.Instance.Indices["i"], Controller.Instance.Z3.MkInt(1));
            //Assert.AreEqual(expected, actual);
            Assert.IsTrue(Controller.Instance.Z3.ProveEqual(actual, expected));

            expected = LogicalExpression.CreateTerm("q[1] == aloc and q[2] == aloc");
            types    = new List <SymmetricType>();
            types.Add(new SymmetricType(2, LogicalExpression.CreateTerm("q[i] == aloc")));
            target = new SymmetricState(2, types.ToArray());
            actual = target.ToConcrete();
            Assert.IsTrue(Controller.Instance.Z3.ProveEqual(actual, expected));

            expected = LogicalExpression.CreateTerm("q[1] == aloc and q[2] == aloc and q[3] == aloc");
            types    = new List <SymmetricType>();
            types.Add(new SymmetricType(3, LogicalExpression.CreateTerm("q[i] == aloc")));
            target = new SymmetricState(3, types.ToArray());
            actual = target.ToConcrete();
            Assert.IsTrue(Controller.Instance.Z3.ProveEqual(actual, expected));


            //target.visit(0, 3);


            List <uint> ids = new List <uint>();

            ids.Add(1);
            ids.Add(2);
            ids.Add(3);
            ids.Add(4);

            var perms = SymHelp.Permutations(ids);

            //ids.Add(5);
            //ids.Add(6);

            /*
             * IEnumerable<uint> t1 = SymmetricState.Combinations(ids, 1);
             * String s = "";
             * foreach (var v in t1)
             * {
             *  s += v + ", ";
             * }
             * IEnumerable<uint> t2 = SymmetricState.Combinations(ids, 2);
             * foreach (var v in t2)
             * {
             *  s += v + ", ";
             * }
             * IEnumerable<uint> t3 = SymmetricState.Combinations(ids, 3);
             * foreach (var v in t3)
             * {
             *  s += v + ", ";
             * }
             * IEnumerable<uint> t4 = SymmetricState.Combinations(ids, 4);
             * foreach (var v in t4)
             * {
             *  s += v + ", ";
             * }*/

            /*
             * uint T1 = 2;
             * uint T2 = 2;
             * uint T3 = (uint)(ids.Count - (T1 + T2));
             * var ids_t1 = SymHelp.Combinations(ids, (int)T1);
             * //s = "";
             * string s = "";
             * foreach (var v in ids_t1)
             * {
             *  List<uint> vt = new List<uint>(v);
             *  var ids_t2 = SymHelp.Combinations(ids.FindAll(id => !vt.Contains(id)), (int)T2);
             *
             *
             *  //foreach (var stmp in v)
             *  //{
             * //      s += stmp;
             * //  }
             * //  s += ", ";
             *
             *  foreach (var v2 in ids_t2)
             *  {
             *      List<uint> vt2 = new List<uint>(v2);
             *      var ids_t3 = SymHelp.Combinations(ids.FindAll(id => !vt.Contains(id) && !vt2.Contains(id)), (int)T3);
             *
             *      foreach (var v3 in ids_t3)
             *      {
             *          List<uint> idset = new List<uint>(v);
             *          idset.AddRange(v2);
             *          idset.AddRange(v3);
             *
             *          foreach (var stmp in idset)
             *          {
             *              s += stmp;
             *          }
             *          s += ", ";
             *      }
             *  }
             * }*/
            //IEnumerable<uint> combinations = ;


            expected = LogicalExpression.CreateTerm("(q[1] == aloc and q[2] == bloc) or (q[1] == bloc and q[2] == aloc)");
            types    = new List <SymmetricType>();
            types.Add(new SymmetricType(1, LogicalExpression.CreateTerm("q[i] == aloc")));
            types.Add(new SymmetricType(1, LogicalExpression.CreateTerm("q[i] == bloc")));
            target = new SymmetricState(2, types.ToArray());
            actual = target.ToConcrete();
            Assert.IsTrue(Controller.Instance.Z3.ProveEqual(actual, expected));
        }