Beispiel #1
0
        protected void Verify(ReadOnlySpan <byte> input, string output)
        {
            byte[] expected = ByteUtils.HexToByteArray(output);
            byte[] actual;

            // Too small
            actual = new byte[expected.Length - 1];
            try
            {
                Sha.ComputeHash(input, actual);
            }
            catch (ArgumentException e)
            {
                Assert.Equal("destination", e.ParamName);
            }

            // Just right
            actual = new byte[expected.Length];
            Sha.ComputeHash(input, actual);
            Assert.Equal(expected, actual);

            // Bigger than needed
            actual = new byte[expected.Length + 1];
            actual[actual.Length - 1] = 42;
            Sha.ComputeHash(input, actual);
            Assert.Equal(expected, actual.AsSpan(0, expected.Length).ToArray());
            Assert.Equal(42, actual[actual.Length - 1]);
        }
Beispiel #2
0
        protected void VerifyHmac_KeyAlreadySet(
            HMAC hmac,
            int testCaseId,
            string digest)
        {
            byte[] digestBytes = ByteUtils.HexToByteArray(digest);
            byte[] computedDigest;

            computedDigest = hmac.ComputeHash(_testData[testCaseId]);
            Assert.Equal(digestBytes, computedDigest);
        }
Beispiel #3
0
        protected void VerifyGfsBoxKat(ReadOnlySpan <byte> plaintext, ReadOnlySpan <byte> expectedCiphertext, ReadOnlySpan <byte> key)
        {
            var         iv         = ByteUtils.HexToByteArray("00000000000000000000000000000000");
            var         encryptor  = CreateEncryptor();
            Span <byte> ciphertext = new byte[(plaintext.Length + 16) & ~15];

            encryptor.Encrypt(key, plaintext, iv, ciphertext);

            // The last 16 bytes are ignored as the test data sets are for ECB mode
            Assert.Equal(expectedCiphertext.ToArray(), ciphertext.Slice(0, ciphertext.Length - 16).ToArray());
        }
Beispiel #4
0
        protected void VerifyHmac_Empty(string digest)
        {
            // using ReadOnlySpan<byte>.Empty may throw NRE if length not validated correctly
            var hmac = Create(ReadOnlySpan <byte> .Empty);

            byte[] digestBytes    = ByteUtils.HexToByteArray(digest);
            byte[] computedDigest = new byte[hmac.HashSize];
            hmac.ComputeHash(ByteUtils.AsciiBytes("Empty key"), computedDigest);

            Assert.Equal(digestBytes, computedDigest);

            hmac = Create(Array.Empty <byte>());
            hmac.ComputeHash(ByteUtils.AsciiBytes("Empty key"), computedDigest);

            Assert.Equal(digestBytes, computedDigest);
        }
Beispiel #5
0
        protected void Verify(byte[] input, byte[] prepend, string output)
        {
            byte[] expected = ByteUtils.HexToByteArray(output);
            byte[] actual;

            // Too small
            actual = new byte[expected.Length - 1];
            Assert.Throws <ArgumentException>("destination", () => Sha.ComputeHash(input, prepend, actual));

            // Just right
            actual = new byte[expected.Length];
            Sha.ComputeHash(input, prepend, actual);
            Assert.Equal(expected, actual);

            // Bigger than needed
            actual = new byte[expected.Length + 1];
            actual[actual.Length - 1] = 42;
            Sha.ComputeHash(input, prepend, actual);
            Assert.Equal(expected, actual.AsSpan(0, expected.Length).ToArray());
            Assert.Equal(42, actual[actual.Length - 1]);
        }
Beispiel #6
0
        protected void VerifyHmac(
            int testCaseId,
            string digest,
            int truncateSize = -1)
        {
            var hmac = Create(_testKeys[testCaseId]);

            byte[] digestBytes    = ByteUtils.HexToByteArray(digest);
            byte[] computedDigest = new byte[hmac.HashSize];
            hmac.ComputeHash(_testData[testCaseId], computedDigest);

            if (truncateSize != -1)
            {
                byte[] tmp = new byte[truncateSize];
                Array.Copy(computedDigest, tmp, truncateSize);
                computedDigest = tmp;
                tmp            = new byte[truncateSize];
                Array.Copy(digestBytes, tmp, truncateSize);
                digestBytes = tmp;
            }

            Assert.Equal(digestBytes, computedDigest);
        }