Beispiel #1
0
    private static void Test(IAEADCrypto crypto, string nonceHex, string associatedDataHex, string tagHex, string plainHex, string cipherHex)
    {
        Assert.AreEqual(@"XChaCha20-Poly1305", crypto.Name);

        ReadOnlySpan <byte> nonce          = nonceHex.FromHex();
        ReadOnlySpan <byte> associatedData = associatedDataHex.FromHex();
        ReadOnlySpan <byte> tag            = tagHex.FromHex();
        ReadOnlySpan <byte> plain          = plainHex.FromHex();
        ReadOnlySpan <byte> cipher         = cipherHex.FromHex();
        Span <byte>         o1             = stackalloc byte[plain.Length];
        Span <byte>         o2             = stackalloc byte[16];

        crypto.Encrypt(nonce, plain, o1, o2, associatedData);
        Assert.IsTrue(o1.SequenceEqual(cipher));
        Assert.IsTrue(o2.SequenceEqual(tag));

        crypto.Encrypt(nonce, plain, o1, o2, associatedData);
        Assert.IsTrue(o1.SequenceEqual(cipher));
        Assert.IsTrue(o2.SequenceEqual(tag));

        crypto.Decrypt(nonce, cipher, tag, o1, associatedData);
        Assert.IsTrue(o1.SequenceEqual(plain));

        crypto.Decrypt(nonce, cipher, tag, o1, associatedData);
        Assert.IsTrue(o1.SequenceEqual(plain));

        crypto.Dispose();
    }
Beispiel #2
0
    public static void AEADTest(this IAEADCrypto crypto,
                                string nonceHex, string associatedDataHex, string tagHex,
                                string plainHex, string cipherHex)
    {
        ReadOnlySpan <byte> nonce          = nonceHex.FromHex();
        ReadOnlySpan <byte> associatedData = associatedDataHex.FromHex();
        ReadOnlySpan <byte> tag            = tagHex.FromHex();
        ReadOnlySpan <byte> plain          = plainHex.FromHex();
        ReadOnlySpan <byte> cipher         = cipherHex.FromHex();
        Span <byte>         o1             = stackalloc byte[plain.Length];
        Span <byte>         o2             = stackalloc byte[tag.Length];

        crypto.Encrypt(nonce, plain, o1, o2, associatedData);
        Assert.IsTrue(o1.SequenceEqual(cipher));
        Assert.IsTrue(o2.SequenceEqual(tag));

        crypto.Encrypt(nonce, plain, o1, o2, associatedData);
        Assert.IsTrue(o1.SequenceEqual(cipher));
        Assert.IsTrue(o2.SequenceEqual(tag));

        crypto.Decrypt(nonce, cipher, tag, o1, associatedData);
        Assert.IsTrue(o1.SequenceEqual(plain));

        crypto.Decrypt(nonce, cipher, tag, o1, associatedData);
        Assert.IsTrue(o1.SequenceEqual(plain));

        crypto.Dispose();
    }
Beispiel #3
0
        private void TestEncrypt(IAEADCrypto crypto)
        {
            Span <byte> o   = stackalloc byte[Length];
            Span <byte> tag = stackalloc byte[16];

            crypto.Encrypt(_randomIv.Span, _randombytes.Span, o, tag);

            crypto.Dispose();
        }
Beispiel #4
0
        public static void Test(IAEADCrypto crypto, int nonceLength = 12)
        {
            ReadOnlySpan <byte> i     = new byte[Step];
            Span <byte>         o     = new byte[Step];
            ReadOnlySpan <byte> nonce = IV.Slice(0, nonceLength);
            Span <byte>         tag   = stackalloc byte[16];

            var sw     = Stopwatch.StartNew();
            var length = 0ul;

            do
            {
                crypto.Encrypt(nonce, i, o, tag);
                ++length;
            } while (sw.ElapsedMilliseconds < 2000);

            sw.Stop();
            crypto.Dispose();

            var result = length * Step / sw.Elapsed.TotalSeconds / 1024.0 / 1024.0;

            Console.WriteLine($@"{result:F2} MB/s");
        }
 private void InitCipher()
 {
     _crypto?.Dispose();
     _crypto = CreateSessionCrypto();
 }