Exemplo n.º 1
0
        public void TestLargeInput()
        {
            var factor = 1031;
            var data   = new byte[factor];

            using (var hasher = new Blake2s())
            {
                int count = 4 * factor * factor;

                for (int i = 0; i < count; ++i)
                {
                    hasher.AppendData(data);
                }

                var hash = new byte[hasher.HashLen];
                hasher.GetHashAndReset(hash);

                string expected = "3c965aaac533c5a1715a40ae8beaf8d1fe1242502f2c30db34239b16c54b1d78";
                string actual   = Hex.Encode(hash);

                Assert.Equal(expected, actual);
            }
        }
Exemplo n.º 2
0
 public override void WriteJson(JsonWriter writer, byte[] value, JsonSerializer serializer)
 {
     writer.WriteValue(Hex.Encode(value));
 }
Exemplo n.º 3
0
        public static async Task <IEnumerable <Vector> > Generate()
        {
            var vectors = new List <Vector>();

            using (var stream = new MemoryStream())
            {
                foreach (var test in GetTests())
                {
                    var aliceConfig = new ProtocolConfig(
                        initiator: true,
                        prologue: PrologueRaw,
                        s: test.InitStaticRequired ? AliceStaticRaw : null,
                        rs: test.InitRemoteStaticRequired ? BobStaticPublicRaw : null,
                        psks: test.PsksRequired ? psksRaw : null
                        );

                    var bobConfig = new ProtocolConfig(
                        initiator: false,
                        prologue: PrologueRaw,
                        s: test.RespStaticRequired ? BobStaticRaw : null,
                        rs: test.RespRemoteStaticRequired ? AliceStaticPublicRaw : null,
                        psks: test.PsksRequired ? psksRaw : null
                        );

                    var alice = new NoiseSocket(test.Protocol, aliceConfig, stream, true);
                    var bob   = new NoiseSocket(test.Protocol, bobConfig, stream, true);

                    alice.SetInitializer(handshakeState => Utilities.SetDh(handshakeState, AliceEphemeralRaw.ToArray()));
                    bob.SetInitializer(handshakeState => Utilities.SetDh(handshakeState, BobEphemeralRaw.ToArray()));

                    var proxy = new SocketProxy(stream, (ushort)test.PaddedLength);
                    var queue = new Queue <byte[]>(payloads);

                    var writer = alice;
                    var reader = bob;

                    if (test.Response == Response.Accept)
                    {
                        await proxy.WriteHandshakeMessageAsync(alice, InitialNegotiationData, queue.Dequeue());

                        await bob.ReadNegotiationDataAsync();

                        bob.Accept(test.Protocol, bobConfig);

                        await bob.ReadHandshakeMessageAsync();

                        stream.Position = 0;

                        Utilities.Swap(ref writer, ref reader);
                    }
                    else if (test.Response == Response.Switch)
                    {
                        await proxy.WriteHandshakeMessageAsync(alice, InitialNegotiationData, queue.Dequeue());

                        await bob.ReadNegotiationDataAsync();

                        if (test.Fallback != null)
                        {
                            await bob.ReadHandshakeMessageAsync();

                            stream.Position = 0;

                            bob.Switch(test.Fallback, new ProtocolConfig(true, PrologueRaw, BobStaticRaw));

                            await proxy.WriteHandshakeMessageAsync(bob, SwitchNegotiationData, queue.Dequeue());

                            await alice.ReadNegotiationDataAsync();

                            alice.Switch(test.Fallback, new ProtocolConfig(false, PrologueRaw, AliceStaticRaw));

                            await alice.ReadHandshakeMessageAsync();

                            stream.Position = 0;
                        }
                        else
                        {
                            bob.Switch(test.Protocol, aliceConfig);
                            bob.SetInitializer(handshakeState => Utilities.SetDh(handshakeState, AliceEphemeralRaw.ToArray()));

                            await bob.IgnoreHandshakeMessageAsync();

                            stream.Position = 0;

                            await proxy.WriteHandshakeMessageAsync(bob, SwitchNegotiationData, queue.Dequeue());

                            await alice.ReadNegotiationDataAsync();

                            alice.Switch(test.Protocol, bobConfig);
                            alice.SetInitializer(handshakeState => Utilities.SetDh(handshakeState, BobEphemeralRaw.ToArray()));

                            await alice.ReadHandshakeMessageAsync();

                            stream.Position = 0;
                        }
                    }
                    else if (test.Response == Response.Retry)
                    {
                        await proxy.WriteHandshakeMessageAsync(alice, InitialNegotiationData, queue.Dequeue());

                        await bob.ReadNegotiationDataAsync();

                        bob.Retry(test.Protocol, bobConfig);

                        await bob.IgnoreHandshakeMessageAsync();

                        stream.Position = 0;

                        await proxy.WriteEmptyHandshakeMessageAsync(bob, RetryNegotiationData);

                        await alice.ReadNegotiationDataAsync();

                        alice.Retry(test.Protocol, aliceConfig);

                        await alice.IgnoreHandshakeMessageAsync();

                        stream.Position = 0;
                    }

                    while (queue.Count > 0)
                    {
                        if (writer.HandshakeHash.IsEmpty)
                        {
                            await proxy.WriteHandshakeMessageAsync(writer, null, queue.Dequeue());

                            await reader.ReadNegotiationDataAsync();

                            await reader.ReadHandshakeMessageAsync();

                            stream.Position = 0;
                        }
                        else
                        {
                            await proxy.WriteMessageAsync(writer, queue.Dequeue());
                        }

                        Utilities.Swap(ref writer, ref reader);
                    }

                    var initialConfig = new Config
                    {
                        ProtocolName      = Encoding.ASCII.GetString(test.Protocol.Name),
                        AliceStatic       = aliceConfig.LocalStatic,
                        AliceEphemeral    = AliceEphemeralRaw,
                        AliceRemoteStatic = aliceConfig.RemoteStatic,
                        BobStatic         = bobConfig.LocalStatic,
                        BobEphemeral      = BobEphemeralRaw,
                        BobRemoteStatic   = bobConfig.RemoteStatic
                    };

                    var switchConfig = new Config
                    {
                        ProtocolName      = Encoding.ASCII.GetString(test.Protocol.Name),
                        AliceStatic       = bobConfig.LocalStatic,
                        AliceEphemeral    = BobEphemeralRaw,
                        AliceRemoteStatic = bobConfig.RemoteStatic,
                        BobStatic         = aliceConfig.LocalStatic,
                        BobEphemeral      = AliceEphemeralRaw,
                        BobRemoteStatic   = aliceConfig.RemoteStatic
                    };

                    if (test.Fallback != null)
                    {
                        switchConfig = new Config
                        {
                            ProtocolName   = Encoding.ASCII.GetString(test.Fallback.Name),
                            AliceStatic    = AliceStaticRaw,
                            AliceEphemeral = AliceEphemeralRaw,
                            BobStatic      = BobStaticRaw,
                            BobEphemeral   = BobEphemeralRaw
                        };
                    }

                    var vector = new Vector
                    {
                        Initial       = initialConfig,
                        Switch        = test.Response == Response.Switch ? switchConfig : null,
                        Retry         = test.Response == Response.Retry ? initialConfig : null,
                        AlicePrologue = PrologueHex,
                        AlicePsks     = test.PsksRequired ? psksHex : null,
                        BobPrologue   = PrologueHex,
                        BobPsks       = test.PsksRequired ? psksHex : null,
                        HandshakeHash = Hex.Encode(writer.HandshakeHash.ToArray()),
                        Messages      = proxy.Messages
                    };

                    alice.Dispose();
                    bob.Dispose();

                    vectors.Add(vector);
                }
            }

            return(vectors);
        }