public static void DeriveBytesWithNegativeCount(Type algorithmType) { var a = (KeyDerivationAlgorithm)Activator.CreateInstance(algorithmType); var x = new X25519(); using (var k = new Key(x)) using (var s = x.Agree(k, k.PublicKey)) { Assert.Throws <ArgumentOutOfRangeException>("count", () => a.DeriveBytes(s, ReadOnlySpan <byte> .Empty, ReadOnlySpan <byte> .Empty, -1)); } }
public static void DeriveKeyWithNullAlgorithm(Type algorithmType) { var a = (KeyDerivationAlgorithm)Activator.CreateInstance(algorithmType); var x = new X25519(); using (var k = new Key(x)) using (var s = x.Agree(k, k.PublicKey)) { Assert.Throws <ArgumentNullException>("algorithm", () => a.DeriveKey(s, ReadOnlySpan <byte> .Empty, ReadOnlySpan <byte> .Empty, null)); } }
public static void DeriveBytesWithMaxSpan(Type algorithmType) { var a = (KeyDerivationAlgorithm)Activator.CreateInstance(algorithmType); var x = new X25519(); using (var k = new Key(x)) using (var s = x.Agree(k, k.PublicKey)) { a.DeriveBytes(s, ReadOnlySpan <byte> .Empty, ReadOnlySpan <byte> .Empty, new byte[a.MaxOutputSize]); } }
public static void DeriveBytesWithZeroCount(Type algorithmType) { var a = (KeyDerivationAlgorithm)Activator.CreateInstance(algorithmType); var x = new X25519(); using (var k = new Key(x)) using (var s = x.Agree(k, k.PublicKey)) { var b = a.DeriveBytes(s, ReadOnlySpan <byte> .Empty, ReadOnlySpan <byte> .Empty, 0); Assert.NotNull(b); Assert.Equal(0, b.Length); } }
public static void DeriveBytesWithCountTooLarge(Type algorithmType) { var a = (KeyDerivationAlgorithm)Activator.CreateInstance(algorithmType); var x = new X25519(); if (a.MaxOutputSize < int.MaxValue) { using (var k = new Key(x)) using (var s = x.Agree(k, k.PublicKey)) { Assert.Throws <ArgumentOutOfRangeException>("count", () => a.DeriveBytes(s, ReadOnlySpan <byte> .Empty, ReadOnlySpan <byte> .Empty, a.MaxOutputSize + 1)); } } }
public static void DeriveBytesWithInfoOverlapping(Type algorithmType) { var a = (KeyDerivationAlgorithm)Activator.CreateInstance(algorithmType); var x = new X25519(); using (var k = new Key(x)) using (var s = x.Agree(k, k.PublicKey)) { var b = new byte[200]; Assert.Throws <ArgumentException>("bytes", () => a.DeriveBytes(s, ReadOnlySpan <byte> .Empty, b.AsSpan().Slice(10, 100), b.AsSpan().Slice(60, 100))); Assert.Throws <ArgumentException>("bytes", () => a.DeriveBytes(s, ReadOnlySpan <byte> .Empty, b.AsSpan().Slice(60, 100), b.AsSpan().Slice(10, 100))); } }
public static void TestAllZeros(string privateKey, string publicKey) { var a = new X25519(); var pk = PublicKey.Import(a, publicKey.DecodeHex(), KeyBlobFormat.RawPublicKey); using (var k = Key.Import(a, privateKey.DecodeHex(), KeyBlobFormat.RawPrivateKey)) { Assert.False(a.TryAgree(k, pk, out SharedSecret s)); Assert.Null(s); Assert.Throws <CryptographicException>(() => a.Agree(k, pk)); } }
public static void DeriveBytesWithUnusedSalt(Type algorithmType) { var a = (KeyDerivationAlgorithm)Activator.CreateInstance(algorithmType); if (!a.SupportsSalt) { var x = new X25519(); using (var k = new Key(x)) using (var s = x.Agree(k, k.PublicKey)) { Assert.Throws <ArgumentException>("salt", () => a.DeriveBytes(s, new byte[1], ReadOnlySpan <byte> .Empty, 0)); } } }
public static void Test(string privateKey, string publicKey, string sharedSecret) { var a = new X25519(); var kdf = new HkdfSha256(); using (var k = Key.Import(a, privateKey.DecodeHex(), KeyBlobFormat.RawPrivateKey)) using (var sharedSecretExpected = SharedSecret.Import(sharedSecret.DecodeHex())) using (var sharedSecretActual = a.Agree(k, PublicKey.Import(a, publicKey.DecodeHex(), KeyBlobFormat.RawPublicKey))) { var expected = kdf.Extract(sharedSecretExpected, ReadOnlySpan <byte> .Empty); var actual = kdf.Extract(sharedSecretActual, ReadOnlySpan <byte> .Empty); Assert.Equal(expected, actual); } }
public static void BitMaskedAgree(string privateKey, string publicKey, string sharedSecret) { var a = new X25519(); var kdf = new HkdfSha256(); var pk1 = publicKey.DecodeHex(); var pk2 = publicKey.DecodeHex(); pk1[pk1.Length - 1] &= 0x7F; pk2[pk2.Length - 1] |= 0x80; using (var k = Key.Import(a, privateKey.DecodeHex(), KeyBlobFormat.RawPrivateKey)) using (var sharedSecretExpected = SharedSecret.Import(sharedSecret.DecodeHex())) using (var sharedSecretActual1 = a.Agree(k, PublicKey.Import(a, pk1, KeyBlobFormat.RawPublicKey))) using (var sharedSecretActual2 = a.Agree(k, PublicKey.Import(a, pk2, KeyBlobFormat.RawPublicKey))) { var expected = kdf.Extract(sharedSecretExpected, ReadOnlySpan <byte> .Empty); var actual1 = kdf.Extract(sharedSecretActual1, ReadOnlySpan <byte> .Empty); var actual2 = kdf.Extract(sharedSecretActual2, ReadOnlySpan <byte> .Empty); Assert.Equal(expected, actual1); Assert.Equal(expected, actual2); } }
public static void ExpandWithInfoOverlapping() { var a = new HkdfSha256(); var x = new X25519(); using (var k = new Key(x)) using (var s = x.Agree(k, k.PublicKey)) { var b = new byte[200]; var prk = a.Extract(s, ReadOnlySpan <byte> .Empty); Assert.Throws <ArgumentException>("bytes", () => a.Expand(prk, b.AsSpan().Slice(10, 100), b.AsSpan().Slice(60, 100))); Assert.Throws <ArgumentException>("bytes", () => a.Expand(prk, b.AsSpan().Slice(60, 100), b.AsSpan().Slice(10, 100))); } }
public static void DeriveBytesWithSpanTooLarge(Type algorithmType) { var a = (KeyDerivationAlgorithm)Activator.CreateInstance(algorithmType); var x = new X25519(); if (a.MaxOutputSize == int.MaxValue) { return; } using (var k = new Key(x)) using (var s = x.Agree(k, k.PublicKey)) { Assert.Throws <ArgumentException>("bytes", () => a.DeriveBytes(s, ReadOnlySpan <byte> .Empty, ReadOnlySpan <byte> .Empty, new byte[a.MaxOutputSize + 1])); } }
public static void DeriveBytesWithMaxCount(Type algorithmType) { var a = (KeyDerivationAlgorithm)Activator.CreateInstance(algorithmType); var x = new X25519(); using (var k = new Key(x)) using (var s = x.Agree(k, k.PublicKey)) { var count = Math.Min(a.MaxOutputSize, 500173); var b = a.DeriveBytes(s, ReadOnlySpan <byte> .Empty, ReadOnlySpan <byte> .Empty, count); Assert.NotNull(b); Assert.Equal(count, b.Length); } }
public static void DeriveBytesWithSaltOverlapping(Type algorithmType) { var a = (KeyDerivationAlgorithm)Activator.CreateInstance(algorithmType); var x = new X25519(); using (var k = new Key(x)) using (var s = x.Agree(k, k.PublicKey)) { var actual = new byte[100]; var expected = new byte[100]; Utilities.RandomBytes.Slice(0, 100).CopyTo(actual); a.DeriveBytes(s, actual, ReadOnlySpan <byte> .Empty, expected); a.DeriveBytes(s, actual, ReadOnlySpan <byte> .Empty, actual); Assert.Equal(expected, actual); } }