From 1ff848678b070a07d01191cdf427b0415c2e8165 Mon Sep 17 00:00:00 2001 From: Maxhy Date: Tue, 24 Oct 2023 22:23:35 +0200 Subject: [PATCH] Move from KeyCeremonyType enum to SecretSharing classes, implements missing logic (share generation) and add related unit tests --- .../Wizard/SAMAccessControlWizardFactory.cs | 2 +- .../SAMKeyStore.cs | 3 + .../ConcatSecretSharingTests.cs | 61 +++++++++ .../KeyGenerationTests.cs | 4 +- KeyManager.Library.Tests/KeyTests.cs | 6 +- .../ShamirsSecretSharingTests.cs | 48 +++++++ .../XorSecretSharingTests.cs | 60 +++++++++ .../Domain/KeyChecksumConverter.cs | 5 + .../Domain/KeyLeakUriConverter.cs | 7 + .../KeyCeremonyBeginControl.xaml | 4 +- .../KeyCeremonyFragmentControl.xaml | 2 +- .../KeyCeremonySetupControl.xaml | 16 ++- .../KeyCeremonySetupControl.xaml.cs | 125 ++++++------------ KeyManager.Library.UI/KeyCeremonyType.cs | 14 -- KeyManager.Library.UI/KeyEntriesControl.xaml | 2 +- .../KeyManager.Library.UI.csproj | 1 - .../Properties/Resources.Designer.cs | 2 +- .../Properties/Resources.fr.resx | 2 +- .../Properties/Resources.resx | 2 +- .../SymmetricKeyGenerationDialog.xaml.cs | 4 +- KeyManager.Library/Key.cs | 4 - KeyManager.Library/KeyChecksum.cs | 7 +- KeyManager.Library/KeyGeneration.cs | 15 +-- KeyManager.Library/KeyManager.Library.csproj | 1 + KeyManager.Library/KeyStore/KeyStore.cs | 2 +- .../SecretSharing/ConcatSecretSharing.cs | 38 ++++++ .../SecretSharing/SecretSharingBase.cs | 21 +++ .../SecretSharing/ShamirsSecretSharing.cs | 40 ++++++ .../SecretSharing/XorSecretSharing.cs | 64 +++++++++ 29 files changed, 428 insertions(+), 134 deletions(-) create mode 100644 KeyManager.Library.Tests/ConcatSecretSharingTests.cs create mode 100644 KeyManager.Library.Tests/ShamirsSecretSharingTests.cs create mode 100644 KeyManager.Library.Tests/XorSecretSharingTests.cs delete mode 100644 KeyManager.Library.UI/KeyCeremonyType.cs create mode 100644 KeyManager.Library/SecretSharing/ConcatSecretSharing.cs create mode 100644 KeyManager.Library/SecretSharing/SecretSharingBase.cs create mode 100644 KeyManager.Library/SecretSharing/ShamirsSecretSharing.cs create mode 100644 KeyManager.Library/SecretSharing/XorSecretSharing.cs diff --git a/KeyManager.Library.KeyStore.NXP_SAM.UI/Wizard/SAMAccessControlWizardFactory.cs b/KeyManager.Library.KeyStore.NXP_SAM.UI/Wizard/SAMAccessControlWizardFactory.cs index 9ec64c9..08eedd1 100644 --- a/KeyManager.Library.KeyStore.NXP_SAM.UI/Wizard/SAMAccessControlWizardFactory.cs +++ b/KeyManager.Library.KeyStore.NXP_SAM.UI/Wizard/SAMAccessControlWizardFactory.cs @@ -67,7 +67,7 @@ private static SAMSymmetricKeyEntry CreateKeyEntry(string id, string label, bool if (ke.Variant.KeyContainers[i] is KeyVersion keyVersion) { keyVersion.Version = i; - keyVersion.Key.Materials[0].Value = KeyGeneration.Random(keyVersion.Key.KeySize); + keyVersion.Key.Materials[0].SetValueBinary(KeyGeneration.Random(keyVersion.Key.KeySize)); } } } diff --git a/KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStore.cs b/KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStore.cs index 77d78c7..daf94b6 100644 --- a/KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStore.cs +++ b/KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStore.cs @@ -428,6 +428,7 @@ public override Task Update(IChangeKeyEntry change, bool ignoreIfMissing) }; if (!containers[1].Key.IsEmpty()) { + log.Info("Updating value for key version A."); updateSettings.keyVa = 1; } if (containers[0] is KeyVersion keyVersionA) @@ -438,6 +439,7 @@ public override Task Update(IChangeKeyEntry change, bool ignoreIfMissing) { if (!containers[1].Key.IsEmpty()) { + log.Info("Updating value for key version B."); updateSettings.keyVb = 1; } keys.Add(new LibLogicalAccess.ByteVector(containers[1].Key.GetAggregatedValueBinary(true))); @@ -450,6 +452,7 @@ public override Task Update(IChangeKeyEntry change, bool ignoreIfMissing) { if (!containers[2].Key.IsEmpty()) { + log.Info("Updating value for key version C."); updateSettings.keyVc = 1; } keys.Add(new LibLogicalAccess.ByteVector(containers[2].Key.GetAggregatedValueBinary(true))); diff --git a/KeyManager.Library.Tests/ConcatSecretSharingTests.cs b/KeyManager.Library.Tests/ConcatSecretSharingTests.cs new file mode 100644 index 0000000..ea1f5af --- /dev/null +++ b/KeyManager.Library.Tests/ConcatSecretSharingTests.cs @@ -0,0 +1,61 @@ +using Leosac.KeyManager.Library.SecretSharing; + +namespace Leosac.KeyManager.Library.Tests +{ + [TestClass] + public class ConcatSecretSharingTests + { + private readonly ConcatSecretSharing _sharing; + + public ConcatSecretSharingTests() + { + _sharing = new ConcatSecretSharing(); + } + + [TestMethod] + public void Test_ComputeTwoFragments() + { + var key = _sharing.ComputeFragments(new[] + { + "0011223344556677", + "8899AABBCCDDEEFF" + }); + Assert.IsNotNull(key); + Assert.AreEqual("00112233445566778899AABBCCDDEEFF", Convert.ToHexString(key), true); + } + + [TestMethod] + public void Test_ComputeThreeFragments() + { + var key = _sharing.ComputeFragments(new[] + { + "00112233445", + "566778899AA", + "BBCCDDEEFF" + }); + Assert.IsNotNull(key); + Assert.AreEqual("00112233445566778899AABBCCDDEEFF", Convert.ToHexString(key), true); + } + + [TestMethod] + public void Test_CreateTwoFragments() + { + var fragments = _sharing.CreateFragments(Convert.FromHexString("00112233445566778899AABBCCDDEEFF"), 2); + Assert.IsNotNull(fragments); + Assert.IsTrue(fragments.Length == 2); + Assert.AreEqual("0011223344556677", fragments[0], true); + Assert.AreEqual("8899AABBCCDDEEFF", fragments[1], true); + } + + [TestMethod] + public void Test_CreateThreeFragments() + { + var fragments = _sharing.CreateFragments(Convert.FromHexString("00112233445566778899AABBCCDDEEFF"), 3); + Assert.IsNotNull(fragments); + Assert.IsTrue(fragments.Length == 3); + Assert.AreEqual("00112233445", fragments[0], true); + Assert.AreEqual("566778899AA", fragments[1], true); + Assert.AreEqual("BBCCDDEEFF", fragments[2], true); + } + } +} diff --git a/KeyManager.Library.Tests/KeyGenerationTests.cs b/KeyManager.Library.Tests/KeyGenerationTests.cs index e9c55c1..582f368 100644 --- a/KeyManager.Library.Tests/KeyGenerationTests.cs +++ b/KeyManager.Library.Tests/KeyGenerationTests.cs @@ -10,7 +10,7 @@ public class KeyGenerationTests public void Test_Random(int keySize) { var key1 = KeyGeneration.Random((uint)keySize); - Assert.AreEqual(keySize * 2, key1.Length); + Assert.AreEqual(keySize, key1.Length); var key2 = KeyGeneration.Random((uint)keySize); Assert.AreNotEqual(key1, key2); @@ -22,7 +22,7 @@ public void Test_Random(int keySize) [DataRow(32)] public void Test_FromPassword(int keySize) { - var key = KeyGeneration.FromPassword("test", "Security Freedom", keySize); + var key = Convert.ToHexString(KeyGeneration.FromPassword("test", "Security Freedom", keySize)); var rkey = "E088566240571EAD486818BE1199F53EB407411014BA1E36101C242FC34DEBAF"[..(keySize * 2)]; Assert.AreEqual(rkey, key, true); } diff --git a/KeyManager.Library.Tests/KeyTests.cs b/KeyManager.Library.Tests/KeyTests.cs index 4ae9f31..c4be6c4 100644 --- a/KeyManager.Library.Tests/KeyTests.cs +++ b/KeyManager.Library.Tests/KeyTests.cs @@ -6,7 +6,7 @@ public class KeyTests [TestMethod] public void OneMaterial_GetAggregatedValue_HexString() { - var key = new Key(null, "00112233445566778899AABBCCDDEEFF"); + var key = new Key(null, 16, "00112233445566778899AABBCCDDEEFF"); var v = key.GetAggregatedValueString(); Assert.AreEqual("00112233445566778899AABBCCDDEEFF", v, true); } @@ -22,7 +22,7 @@ public void OneMaterial_SetAggregatedValue_HexString() [TestMethod] public void OneMaterial_GetAggregatedValue_Binary() { - var key = new Key(null, "00112233445566778899AABBCCDDEEFF"); + var key = new Key(null, 16, "00112233445566778899AABBCCDDEEFF"); var v = key.GetAggregatedValueBinary(); Assert.IsNotNull(v); Assert.AreEqual("00112233445566778899AABBCCDDEEFF", Convert.ToHexString(v), true); @@ -31,7 +31,7 @@ public void OneMaterial_GetAggregatedValue_Binary() [TestMethod] public void OneMaterial_GetAggregatedValue_HexStringWithSpace() { - var key = new Key(null, "00112233445566778899AABBCCDDEEFF"); + var key = new Key(null, 16, "00112233445566778899AABBCCDDEEFF"); var v = key.GetAggregatedValueString(KeyValueStringFormat.HexStringWithSpace); Assert.AreEqual("00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF", v, true); } diff --git a/KeyManager.Library.Tests/ShamirsSecretSharingTests.cs b/KeyManager.Library.Tests/ShamirsSecretSharingTests.cs new file mode 100644 index 0000000..6f6233e --- /dev/null +++ b/KeyManager.Library.Tests/ShamirsSecretSharingTests.cs @@ -0,0 +1,48 @@ +using Leosac.KeyManager.Library.SecretSharing; + +namespace Leosac.KeyManager.Library.Tests +{ + [TestClass] + public class ShamirsSecretSharingTests + { + private readonly ShamirsSecretSharing _sharing; + + public ShamirsSecretSharingTests() + { + _sharing = new ShamirsSecretSharing(); + } + + [TestMethod] + public void Test_ComputeTwoFragments() + { + var key = _sharing.ComputeFragments(new[] + { + "01-34E95FCCCA0B73B74DEC515D2EA4B988A582D6EF55B6EFC0638B2B3CBBE171A30BD85DC2ABEB16F93991ADCCB2107F421F2C3BAEE69EFE271CE81A93C1E1EF3D9700", + "03-9C99DBFED7788C37D891A0A0F1304F9AC48783CF0123CF422BA282B431A555EA2288194703C344EBADB3086618327DC75D84B10AB4DCFB7754B850B944A5CFB9C501" + }); + Assert.IsNotNull(key); + Assert.AreEqual("00112233445566778899AABBCCDDEEFF", Convert.ToHexString(key), true); + } + + [TestMethod] + public void Test_ComputeThreeFragments() + { + var key = _sharing.ComputeFragments(new[] + { + "01-34E95FCCCA0B73B74DEC515D2EA4B988A582D6EF55B6EFC0638B2B3CBBE171A30BD85DC2ABEB16F93991ADCCB2107F421F2C3BAEE69EFE271CE81A93C1E1EF3D9700", + "02-68C19D6551C27FF7123FF9FE8F6A84113505ADDFAB6CDF81C716577876C3E34617B0BB8457D72DF273225B996521FE843E58765CCD3DFD4F38D0352683C3DF7B2E01", + "03-9C99DBFED7788C37D891A0A0F1304F9AC48783CF0123CF422BA282B431A555EA2288194703C344EBADB3086618327DC75D84B10AB4DCFB7754B850B944A5CFB9C501" + }); + Assert.IsNotNull(key); + Assert.AreEqual("00112233445566778899AABBCCDDEEFF", Convert.ToHexString(key), true); + } + + [TestMethod] + public void Test_CreateThreeFragments() + { + var fragments = _sharing.CreateFragments(Convert.FromHexString("00112233445566778899AABBCCDDEEFF"), 3); + Assert.IsNotNull(fragments); + Assert.IsTrue(fragments.Length == 3); + } + } +} diff --git a/KeyManager.Library.Tests/XorSecretSharingTests.cs b/KeyManager.Library.Tests/XorSecretSharingTests.cs new file mode 100644 index 0000000..4f89aa7 --- /dev/null +++ b/KeyManager.Library.Tests/XorSecretSharingTests.cs @@ -0,0 +1,60 @@ +using Leosac.KeyManager.Library.SecretSharing; + +namespace Leosac.KeyManager.Library.Tests +{ + [TestClass] + public class XorSecretSharingTests + { + private readonly XorSecretSharing _sharing; + + public XorSecretSharingTests() + { + _sharing = new XorSecretSharing(); + } + + [TestMethod] + public void Test_ComputeTwoFragments() + { + var key = _sharing.ComputeFragments(new[] + { + "C37CAE51E95AF034DE05EEFA1AF48CF8", + "C36D8C62AD0F9643569C4441D6296207" + }); + Assert.IsNotNull(key); + Assert.AreEqual("00112233445566778899AABBCCDDEEFF", Convert.ToHexString(key), true); + } + + [TestMethod] + public void Test_ComputeThreeFragments() + { + var key = _sharing.ComputeFragments(new[] + { + "77BE07C2B01AE24B7D20E1988898107A", + "F84B15A46502C6B4589CD51579A4B23F", + "8FE43055914D4288AD259E363DE14CBA" + }); + Assert.IsNotNull(key); + Assert.AreEqual("00112233445566778899AABBCCDDEEFF", Convert.ToHexString(key), true); + } + + [TestMethod] + public void Test_CreateTwoFragments() + { + var fragments = _sharing.CreateFragments(Convert.FromHexString("00112233445566778899AABBCCDDEEFF"), 2); + Assert.IsNotNull(fragments); + Assert.IsTrue(fragments.Length == 2); + Assert.AreEqual(fragments[0].Length, fragments[1].Length); + Assert.AreNotEqual(fragments[0], fragments[1], true); + } + + [TestMethod] + public void Test_CreateThreeFragments() + { + var fragments = _sharing.CreateFragments(Convert.FromHexString("00112233445566778899AABBCCDDEEFF"), 3); + Assert.IsNotNull(fragments); + Assert.IsTrue(fragments.Length == 3); + Assert.AreEqual(fragments[0].Length, fragments[1].Length); + Assert.AreEqual(fragments[1].Length, fragments[2].Length); + } + } +} diff --git a/KeyManager.Library.UI/Domain/KeyChecksumConverter.cs b/KeyManager.Library.UI/Domain/KeyChecksumConverter.cs index 606355e..73c840e 100644 --- a/KeyManager.Library.UI/Domain/KeyChecksumConverter.cs +++ b/KeyManager.Library.UI/Domain/KeyChecksumConverter.cs @@ -12,6 +12,11 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur return Binding.DoNothing; } + if (value2.KeySize == 0) + { + return string.Empty; + } + if (string.IsNullOrEmpty(value2.GetAggregatedValueString())) { return Binding.DoNothing; diff --git a/KeyManager.Library.UI/Domain/KeyLeakUriConverter.cs b/KeyManager.Library.UI/Domain/KeyLeakUriConverter.cs index 3af887a..02c3ed8 100644 --- a/KeyManager.Library.UI/Domain/KeyLeakUriConverter.cs +++ b/KeyManager.Library.UI/Domain/KeyLeakUriConverter.cs @@ -14,6 +14,13 @@ public object Convert(object value, Type targetType, object parameter, CultureIn return Binding.DoNothing; } + if (key.KeySize == 0) + { +#pragma warning disable CS8603 // Possible null reference return. + return null; +#pragma warning restore CS8603 // Possible null reference return. + } + var checksum = new Sha256Checksum(); string uri = string.Format("https://leak.leosac.com/key/{0}", checksum.ComputeKCV(key, "53656375726974792046726565646f6d")); if (targetType == typeof(ImageSource)) diff --git a/KeyManager.Library.UI/KeyCeremonyBeginControl.xaml b/KeyManager.Library.UI/KeyCeremonyBeginControl.xaml index 4168290..86db4b3 100644 --- a/KeyManager.Library.UI/KeyCeremonyBeginControl.xaml +++ b/KeyManager.Library.UI/KeyCeremonyBeginControl.xaml @@ -9,8 +9,8 @@ mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="500"> - - + + -