Skip to content

Commit

Permalink
Add random number generator feature to Key Store and allow to use an …
Browse files Browse the repository at this point in the history
…existing favorite as a random number generator for local key generation
  • Loading branch information
Maxhy committed Oct 29, 2024
1 parent 1e7eafe commit 7f0306a
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,7 @@
<data name="OrderByName" xml:space="preserve">
<value>Trier par nom</value>
</data>
<data name="RandomGenerator" xml:space="preserve">
<value>Générateur de nombres aléatoires</value>
</data>
</root>
3 changes: 3 additions & 0 deletions KeyManager.Library.KeyStore.File.UI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,7 @@
<data name="OrderByName" xml:space="preserve">
<value>Order by Name</value>
</data>
<data name="RandomGenerator" xml:space="preserve">
<value>Random Number Generator</value>
</data>
</root>
24 changes: 24 additions & 0 deletions KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,30 @@ public override Task Delete(KeyEntryId identifier, KeyEntryClass keClass, bool i
throw new KeyStoreException("A SAM key entry cannot be deleted, only updated.");
}

public override Task<byte[]?> GenerateBytes(byte size)
{
var cmd = Chip?.getCommands();
if (cmd == null)
{
log.Error("No Command associated with the SAM chip.");
throw new KeyStoreException("No Command associated with the SAM chip.");
}

if (cmd is not LibLogicalAccess.Reader.SAMAV2ISO7816Commands av2cmd)
{
log.Error("Unexpected Command associated with the SAM chip.");
throw new KeyStoreException("Unexpected Command associated with the SAM chip.");
}

if (!_unlocked)
{
UnlockSAM(av2cmd, GetSAMProperties().AuthenticateKeyEntryIdentifier, GetSAMProperties().AuthenticateKeyVersion, KeyMaterial.GetValueAsString(Properties?.Secret, KeyValueStringFormat.HexStringWithSpace));
_unlocked = true;
}

return Task.FromResult<byte[]?>(av2cmd.getRandom(size).ToArray());
}

public override async Task<KeyEntry?> Get(KeyEntryId identifier, KeyEntryClass keClass)
{
log.Info(string.Format("Getting key entry `{0}`...", identifier));
Expand Down
13 changes: 12 additions & 1 deletion KeyManager.Library.UI/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions KeyManager.Library.UI/Properties/Resources.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,7 @@
<data name="OrderByLabel" xml:space="preserve">
<value>Trier par libellé</value>
</data>
<data name="RandomGenerator" xml:space="preserve">
<value>Générateur de nombres aléatoires</value>
</data>
</root>
3 changes: 3 additions & 0 deletions KeyManager.Library.UI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,7 @@
<data name="OrderByLabel" xml:space="preserve">
<value>Order by Label</value>
</data>
<data name="RandomGenerator" xml:space="preserve">
<value>Random Number Generator</value>
</data>
</root>
14 changes: 12 additions & 2 deletions KeyManager.Library.UI/SymmetricKeyGenerationDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<TextBlock Text="{x:Static properties:Resources.Random}" Margin="5 0 0 0" />
</WrapPanel>
</TabItem.Header>
<Grid>
<StackPanel VerticalAlignment="Center">
<Button x:Name="btnRandom" HorizontalAlignment="Center" VerticalAlignment="Center" Height="80" materialDesign:ShadowAssist.ShadowAnimationDuration="0:0:1.5" Click="BtnRandom_Click">
<Button.Content>
<StackPanel>
Expand All @@ -58,7 +58,17 @@
</StackPanel>
</Button.Content>
</Button>
</Grid>
<ComboBox DockPanel.Dock="Right" ItemsSource="{Binding RandomGenerators, ElementName=userControl}"
SelectedItem="{Binding SelectedRandomGenerator, ElementName=userControl}"
materialDesign:HintAssist.HelperText="{x:Static properties:Resources.RandomGenerator}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.RandomGenerator}" Margin="10">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</TabItem>
<TabItem>
<TabItem.Header>
Expand Down
48 changes: 44 additions & 4 deletions KeyManager.Library.UI/SymmetricKeyGenerationDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Leosac.WpfApp;
using Leosac.KeyManager.Library.KeyStore;
using Leosac.WpfApp;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
Expand All @@ -15,6 +16,7 @@ public SymmetricKeyGenerationDialog()
{
MnemonicLanguages = new ObservableCollection<KeyGen.Mnemonic.WordlistLang>(Enum.GetValues<KeyGen.Mnemonic.WordlistLang>());
MnemonicWords = new ObservableCollection<string>();
RandomGenerators = Favorites.GetSingletonInstance()?.KeyStores ?? new ObservableCollection<Favorite>();

InitializeComponent();
}
Expand All @@ -30,6 +32,17 @@ public KeyGen.Mnemonic.WordlistLang SelectedMnemonicLanguage
public static readonly DependencyProperty SelectedMnemonicLanguageProperty = DependencyProperty.Register(nameof(SelectedMnemonicLanguage), typeof(KeyGen.Mnemonic.WordlistLang), typeof(SymmetricKeyGenerationDialog),
new FrameworkPropertyMetadata(KeyGen.Mnemonic.WordlistLang.English));

public ObservableCollection<Favorite> RandomGenerators { get; set; }

public Favorite? SelectedRandomGenerator
{
get { return (Favorite?)GetValue(SelectedRandomGeneratorProperty); }
set { SetValue(SelectedRandomGeneratorProperty, value); }
}

public static readonly DependencyProperty SelectedRandomGeneratorProperty = DependencyProperty.Register(nameof(SelectedRandomGenerator), typeof(Favorite), typeof(SymmetricKeyGenerationDialog),
new FrameworkPropertyMetadata(null));

public int KeySize
{
get { return (int)GetValue(KeySizeProperty); }
Expand Down Expand Up @@ -65,10 +78,37 @@ public bool KeyGenerated

public static readonly DependencyProperty KeyGeneratedProperty = DependencyProperty.Register(nameof(KeyGenerated), typeof(bool), typeof(SymmetricKeyGenerationDialog));

private void BtnRandom_Click(object sender, RoutedEventArgs e)
private async void BtnRandom_Click(object sender, RoutedEventArgs e)
{
KeyValue = Convert.ToHexString(KeyGeneration.Random((uint)(KeySize > 0 ? KeySize : 16)));
ShowKeyComputationConfirmation();
var keySize = (byte)(KeySize > 0 ? KeySize : 16);
byte[]? bytes = null;
if (SelectedRandomGenerator != null)
{
var ks = SelectedRandomGenerator.CreateKeyStore();
if (ks != null)
{
try
{
await ks.Open();
bytes = await ks.GenerateBytes(keySize);
await ks.Close(true);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
else
{
bytes = KeyGeneration.Random(keySize);
}

if (bytes != null)
{
KeyValue = Convert.ToHexString(bytes);
ShowKeyComputationConfirmation();
}
}

private void BtnPassword_Click(object sender, RoutedEventArgs e)
Expand Down
5 changes: 5 additions & 0 deletions KeyManager.Library/KeyStore/KeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ public virtual async Task<KeyEntryId> Generate(KeyEntry keyEntry)
return keyEntry.Identifier;
}

public virtual Task<byte[]?> GenerateBytes(byte size)
{
return Task.FromResult<byte[]?>(KeyGeneration.Random(size));
}

/// <summary>
/// Get a key entry.
/// </summary>
Expand Down

0 comments on commit 7f0306a

Please sign in to comment.