Example #1
0
        private void AssertEncrypt(JArray jarray)
        {
            foreach (var token in jarray)
            {
                var jobj     = (JObject)token;
                var vecK     = _hex.DecodeData(jobj.GetValue("k").ToString());
                var vecNonce = _hex.DecodeData(jobj.GetValue("nonce").ToString());

                var data  = new List <byte[]>();
                var jdata = (JArray)jobj.GetValue("data");
                foreach (var v in jdata)
                {
                    var d = _hex.DecodeData(v.ToString());
                    data.Add(d);
                }
                var vecM = _hex.DecodeData(jobj.GetValue("m").ToString());
                var vecC = _hex.DecodeData(jobj.GetValue("c").ToString());

                var vecTau = jobj.Value <int>("tau");
                var dst    = Span <byte> .Empty;
                var c      = AEZ.Encrypt(vecK, vecNonce, data.ToArray(), vecTau, vecM, dst);
                Assert.Equal(_hex.EncodeData(vecC), _hex.EncodeData(c));

                var m = AEZ.Decrypt(vecK, vecNonce, data.ToArray(), vecTau, vecC, dst);
                Assert.Equal(_hex.EncodeData(vecM), _hex.EncodeData(m));
            }
        }
Example #2
0
        public void TestExtract()
        {
            var extractVectors = (ReadJson("extract.json"));

            Assert.NotEmpty(extractVectors);
            foreach (var token in extractVectors)
            {
                var a            = ((JObject)token).GetValue("a").ToString();
                var extractedKey = new byte[ExtractedKeySize];
                var vecA         = _hex.DecodeData(a);
                var b            = ((JObject)token).GetValue("b").ToString();
                var vecB         = _hex.DecodeData(b);
                AEZ.Extract(vecA, extractedKey);
                Assert.Equal(_hex.EncodeData(vecB), _hex.EncodeData(extractedKey));
            }
        }
Example #3
0
        public void TestInvalidEncoding()
        {
            // testcase found in aezeed.
            var salt       = new byte[] { 0, 0, 0, 1, 98 };
            var ad         = new byte[] { 0, 0, 0, 0, 1, 98 };
            var cipherText = new byte[]
            {
                159,
                217,
                93,
                187,
                7,
                127,
                101,
                132,
                202,
                38,
                118,
                248,
                146,
                251,
                87,
                3,
                48,
                10,
                44,
                82,
                129,
                249,
                179,
            };
            var pass      = Encoding.UTF8.GetBytes("test");
            var wrongPass = Encoding.UTF8.GetBytes("kek");
            var scryptKey =
                // b301317b8a1f874e2d2ad563ade52a268d0c4b7fec528110158bdcab94704d26
                NBitcoin.Crypto.SCrypt.ComputeDerivedKey(pass, salt, 16, 8, 1, null, 32);
            var wrongScryptKey =
                // 4d0d138242993176ab9ccc56463773f265168f21f21a6f6146622fa8c06bc034
                NBitcoin.Crypto.SCrypt.ComputeDerivedKey(wrongPass, salt, 16, 8, 1, null, 32);

            Assert.Equal(0, AEZ.Decrypt(scryptKey, ReadOnlySpan <byte> .Empty, new [] { ad }, 4, cipherText, Span <byte> .Empty)[0]);
            Assert.Throws <CryptographicException>(() => AEZ.Decrypt(wrongScryptKey, ReadOnlySpan <byte> .Empty, new [] { ad }, 4, cipherText, Span <byte> .Empty));
        }