Esempio n. 1
0
        public void Ctor_Buffer_Success()
        {
            // Arrange
            var input = new byte[] { 0x20, 0x30, 0x40 };

            // Act
            var secret = new Secret(input);
            input[1] = 0xFF; // mutate original array - secret shouldn't be modified

            // Assert - length
            Assert.Equal(3, secret.Length);

            // Assert - managed buffer
            var outputSegment = new ArraySegment<byte>(new byte[7], 2, 3);
            secret.WriteSecretIntoBuffer(outputSegment);
            Assert.Equal(new byte[] { 0x20, 0x30, 0x40 }, outputSegment.AsStandaloneArray());

            // Assert - unmanaged buffer
            var outputBuffer = new byte[3];
            fixed (byte* pOutputBuffer = outputBuffer)
            {
                secret.WriteSecretIntoBuffer(pOutputBuffer, 3);
            }
            Assert.Equal(new byte[] { 0x20, 0x30, 0x40 }, outputBuffer);
        }
Esempio n. 2
0
        public void Ctor_Buffer_Success()
        {
            // Arrange
            var input = new byte[] { 0x20, 0x30, 0x40 };

            // Act
            var secret = new Secret(input);

            input[1] = 0xFF; // mutate original array - secret shouldn't be modified

            // Assert - length
            Assert.Equal(3, secret.Length);

            // Assert - managed buffer
            var outputSegment = new ArraySegment <byte>(new byte[7], 2, 3);

            secret.WriteSecretIntoBuffer(outputSegment);
            Assert.Equal(new byte[] { 0x20, 0x30, 0x40 }, outputSegment.AsStandaloneArray());

            // Assert - unmanaged buffer
            var outputBuffer = new byte[3];

            fixed(byte *pOutputBuffer = outputBuffer)
            {
                secret.WriteSecretIntoBuffer(pOutputBuffer, 3);
            }

            Assert.Equal(new byte[] { 0x20, 0x30, 0x40 }, outputBuffer);
        }
Esempio n. 3
0
        private static void TestManagedKeyDerivation(byte[] kdk, byte[] label, byte[] contextHeader, byte[] context, int numDerivedBytes, string expectedDerivedSubkeyAsBase64)
        {
            var labelSegment = new ArraySegment <byte>(new byte[label.Length + 10], 3, label.Length);

            Buffer.BlockCopy(label, 0, labelSegment.Array, labelSegment.Offset, labelSegment.Count);
            var contextSegment = new ArraySegment <byte>(new byte[context.Length + 10], 5, context.Length);

            Buffer.BlockCopy(context, 0, contextSegment.Array, contextSegment.Offset, contextSegment.Count);
            var derivedSubkeySegment = new ArraySegment <byte>(new byte[numDerivedBytes + 10], 4, numDerivedBytes);

            ManagedSP800_108_CTR_HMACSHA512.DeriveKeysWithContextHeader(kdk, labelSegment, contextHeader, contextSegment,
                                                                        bytes => new HMACSHA512(bytes), derivedSubkeySegment);
            Assert.Equal(expectedDerivedSubkeyAsBase64, Convert.ToBase64String(derivedSubkeySegment.AsStandaloneArray()));
        }
Esempio n. 4
0
        private static void TestManagedKeyDerivation(byte[] kdk, byte[] label, byte[] contextHeader, byte[] context, int numDerivedBytes, string expectedDerivedSubkeyAsBase64)
        {
            var labelSegment = new ArraySegment<byte>(new byte[label.Length + 10], 3, label.Length);
            Buffer.BlockCopy(label, 0, labelSegment.Array, labelSegment.Offset, labelSegment.Count);
            var contextSegment = new ArraySegment<byte>(new byte[context.Length + 10], 5, context.Length);
            Buffer.BlockCopy(context, 0, contextSegment.Array, contextSegment.Offset, contextSegment.Count);
            var derivedSubkeySegment = new ArraySegment<byte>(new byte[numDerivedBytes + 10], 4, numDerivedBytes);

            ManagedSP800_108_CTR_HMACSHA512.DeriveKeysWithContextHeader(kdk, labelSegment, contextHeader, contextSegment,
                bytes => new HMACSHA512(bytes), derivedSubkeySegment);
            Assert.Equal(expectedDerivedSubkeyAsBase64, Convert.ToBase64String(derivedSubkeySegment.AsStandaloneArray()));
        }