Skip to content

Commit

Permalink
Fix MIFARE and AES256 key variant definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxhy committed Jan 30, 2024
1 parent 2eb4fb1 commit dc8798f
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 57 deletions.
12 changes: 8 additions & 4 deletions KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public override Task Delete(KeyEntryId identifier, KeyEntryClass keClass, bool i
{
var keysdata = av2entry.getKeysData();
var keyVersions = keyEntry.Variant.KeyContainers.OfType<KeyVersion>().ToArray();
if (keysdata.Count < 2 || keysdata.Count != keyVersions.Length)
if (keysdata.Count < 1 || keysdata.Count != keyVersions.Length)
{
log.Error(string.Format("Unexpected number of keys ({0}) on the SAM Key Entry.", keysdata.Count));
throw new KeyStoreException("Unexpected number of keys on the SAM Key Entry.");
Expand All @@ -251,9 +251,12 @@ public override Task Delete(KeyEntryId identifier, KeyEntryClass keClass, bool i
keyVersions[0].Key.SetAggregatedValueAsString(string.Empty);
keyVersions[0].Version = infoav2.vera;
keyVersions[0].TrackChanges();
keyVersions[1].Key.SetAggregatedValueAsString(string.Empty);
keyVersions[1].Version = infoav2.verb;
keyVersions[1].TrackChanges();
if (keyEntry.Variant.KeyContainers.Count >= 2)
{
keyVersions[1].Key.SetAggregatedValueAsString(string.Empty);
keyVersions[1].Version = infoav2.verb;
keyVersions[1].TrackChanges();
}

if (keyEntry.Variant.KeyContainers.Count >= 3)
{
Expand Down Expand Up @@ -535,6 +538,7 @@ public override Task Update(IChangeKeyEntry change, bool ignoreIfMissing)
samkt = LibLogicalAccess.Card.SAMKeyType.SAM_KEY_3K3DES;
}
}
natkey.setKeyType(samkt); // Temporary fix, this could be removed with LLA version >= v3.1
natkey.setKeysData(keys, samkt);
}
natkey.setKeyEntryInformation(infoav2);
Expand Down
7 changes: 3 additions & 4 deletions KeyManager.Library.KeyStore.NXP_SAM/SAMSymmetricKeyEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ public override IList<KeyEntryVariant> GetAllVariants(KeyEntryClass? classFilter
variants.Add(aes192var);
var aes256var = new KeyEntryVariant { Name = "AES256" };
aes256var.KeyContainers.Add(new KeyVersion("Key Version A", 0, new Key(new[] { "AES", KeyEntryClass.Symmetric.ToString() }, 32)));
aes256var.KeyContainers.Add(new KeyVersion("Key Version B", 0, new Key(new[] { "AES", KeyEntryClass.Symmetric.ToString() }, 32)));
variants.Add(aes256var);
var mfvar = new KeyEntryVariant { Name = "MIFARE" };
mfvar.KeyContainers.Add(new KeyVersion("Key Version A", 0, new Key(new[] { "MIFARE", KeyEntryClass.Symmetric.ToString() }, 12)));
mfvar.KeyContainers.Add(new KeyVersion("Key Version B", 0, new Key(new[] { "MIFARE", KeyEntryClass.Symmetric.ToString() }, 12)));
mfvar.KeyContainers.Add(new KeyVersion("Key Version C", 0, new Key(new[] { "MIFARE", KeyEntryClass.Symmetric.ToString() }, 12)));
mfvar.KeyContainers.Add(new KeyVersion("Key Version A", 0, new Key(new[] { "MIFARE", KeyEntryClass.Symmetric.ToString() }, 6, new KeyMaterial(string.Empty, "Key A"), new KeyMaterial("00", "Key A Div KeyNo", 1), new KeyMaterial("00", "Key A Div KeyVer", 1), new KeyMaterial(string.Empty, "Key B"), new KeyMaterial("00", "Key B Div KeyNo", 1), new KeyMaterial("00", "Key B Div KeyVer", 1))));
mfvar.KeyContainers.Add(new KeyVersion("Key Version B", 0, new Key(new[] { "MIFARE", KeyEntryClass.Symmetric.ToString() }, 6, new KeyMaterial(string.Empty, "Key A"), new KeyMaterial("00", "Key A Div KeyNo", 1), new KeyMaterial("00", "Key A Div KeyVer", 1), new KeyMaterial(string.Empty, "Key B"), new KeyMaterial("00", "Key B Div KeyNo", 1), new KeyMaterial("00", "Key B Div KeyVer", 1))));
mfvar.KeyContainers.Add(new KeyVersion("Key Version C", 0, new Key(new[] { "MIFARE", KeyEntryClass.Symmetric.ToString() }, 6, new KeyMaterial(string.Empty, "Key A"), new KeyMaterial("00", "Key A Div KeyNo", 1), new KeyMaterial("00", "Key A Div KeyVer", 1), new KeyMaterial(string.Empty, "Key B"), new KeyMaterial("00", "Key B Div KeyNo", 1), new KeyMaterial("00", "Key B Div KeyVer", 1))));
variants.Add(mfvar);
}

Expand Down
9 changes: 9 additions & 0 deletions KeyManager.Library.Tests/KeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,14 @@ public void TwoMaterials_GetAggregatedValue_Binary()
Assert.IsNotNull(v);
Assert.AreEqual("00112233445566778899AABBCCDDEEFF", Convert.ToHexString(v), true);
}

[TestMethod]
public void ThreeMaterialsWithOneOverrideSize_GetAggregatedValue_Binary()
{
var key = new Key(null, 6, new KeyMaterial("001122334455"), new KeyMaterial("FFFFFFFF", null, 4), new KeyMaterial("8899AABBCCDD"));
var v = key.GetAggregatedValueAsBinary();
Assert.IsNotNull(v);
Assert.AreEqual("001122334455FFFFFFFF8899AABBCCDD", Convert.ToHexString(v), true);
}
}
}
2 changes: 1 addition & 1 deletion KeyManager.Library.UI/AsymmetricKeyControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" HorizontalAlignment="Right" Text="{Binding Name}" />
<TextBox Grid.Row="1" x:Name="tbxKeyValue" Margin="8,8,8,20" Width="380" Height="60"
<TextBox Grid.Row="1" Margin="8,8,8,20" Width="380" Height="60"
materialDesign:HintAssist.HelperText="{x:Static properties:Resources.KeyValueHelper}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.KeyValue}"
materialDesign:TextFieldAssist.HasClearButton="True"
Expand Down
9 changes: 9 additions & 0 deletions 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 @@ -228,6 +228,9 @@
<data name="GenerateWords" xml:space="preserve">
<value>Générer de nouveaux mots</value>
</data>
<data name="GenericValue" xml:space="preserve">
<value>Valeur</value>
</data>
<data name="GoToParent" xml:space="preserve">
<value>Aller au parent</value>
</data>
Expand Down
3 changes: 3 additions & 0 deletions KeyManager.Library.UI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@
<data name="GenerateWords" xml:space="preserve">
<value>Generate new words</value>
</data>
<data name="GenericValue" xml:space="preserve">
<value>Value</value>
</data>
<data name="GoToParent" xml:space="preserve">
<value>Go to parent</value>
</data>
Expand Down
96 changes: 56 additions & 40 deletions KeyManager.Library.UI/SymmetricKeyControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,65 @@
<DataTemplate>
<Grid>
<Grid.RowDefinitions>

<RowDefinition Height="20" />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.ColumnSpan="2" HorizontalAlignment="Right" Text="{Binding Name}" />
<PasswordBox x:Name="tbxKeyValue" Margin="8,8,8,20" Grid.Row="1" Grid.ColumnSpan="2" Width="380"
materialDesign:HintAssist.HelperText="{x:Static properties:Resources.KeyValueHelper}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.KeyValue}"
materialDesign:TextFieldAssist.HasClearButton="True"
materialDesign:TextFieldAssist.CharacterCounterVisibility="Visible"
MaxLength="{Binding Path=Key.KeySize, Converter={StaticResource ByteLengthToCharLengthConverter}, ElementName=keyControl}"
Style="{StaticResource MaterialDesignFloatingHintRevealPasswordBox}"
materialDesign:PasswordBoxAssist.Password="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}" />
<TextBox x:Name="tbxKCV" Grid.Column="0" Grid.Row="2" Margin="8"
materialDesign:HintAssist.HelperText="{x:Static properties:Resources.KCVHelper}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.KCV}"
Visibility="{Binding ShowKCV, ElementName=keyControl, Converter={StaticResource BooleanToVisibilityConverter}}"
IsReadOnly="True">
<TextBox.Text>
<MultiBinding Converter="{StaticResource KeyChecksumConverter}" Mode="OneWay">
<Binding Path="SelectedKeyChecksumAlgorithm" ElementName="keyControl" />
<Binding Path="Key" ElementName="keyControl" />
<Binding Path="KeyChecksumIV" ElementName="keyControl" />
<Binding Path="Value" />
</MultiBinding>
</TextBox.Text>
</TextBox>
<ComboBox Grid.Column="1" Grid.Row="2" Margin="8"
ItemsSource="{Binding Path=KeyChecksumAlgorithms, Mode=OneWay, ElementName=keyControl}"
SelectedItem="{Binding Path=SelectedKeyChecksumAlgorithm, Mode=TwoWay, ElementName=keyControl}"
Visibility="{Binding ShowKCV, ElementName=keyControl, Converter={StaticResource BooleanToVisibilityConverter}}"
materialDesign:HintAssist.HelperText="{x:Static properties:Resources.KCVAlgorithmHelper}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.KCVAlgorithm}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Grid.Row="0" HorizontalAlignment="Right" Text="{Binding Name}" />
<Grid Grid.Row="1" Visibility="{Binding IsRealKeyMateriel, Converter={StaticResource InverseBooleanToVisibilityConverter}}">
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Margin="8,8,8,20" Grid.Row="0" Width="380"
materialDesign:HintAssist.HelperText="{x:Static properties:Resources.GenericValue}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.GenericValue}"
MaxLength="{Binding Path=OverrideSize, Converter={StaticResource ByteLengthToCharLengthConverter}}"
Text="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}"/>
</Grid>
<Grid Grid.Row="1" Visibility="{Binding IsRealKeyMateriel, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<PasswordBox Margin="8,8,8,20" Grid.Row="0" Grid.ColumnSpan="2" Width="380"
materialDesign:HintAssist.HelperText="{x:Static properties:Resources.KeyValueHelper}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.KeyValue}"
materialDesign:TextFieldAssist.HasClearButton="True"
materialDesign:TextFieldAssist.CharacterCounterVisibility="Visible"
MaxLength="{Binding Path=Key.KeySize, Converter={StaticResource ByteLengthToCharLengthConverter}, ElementName=keyControl}"
Style="{StaticResource MaterialDesignFloatingHintRevealPasswordBox}"
materialDesign:PasswordBoxAssist.Password="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}" />
<TextBox x:Name="tbxKCV" Grid.Column="0" Grid.Row="1" Margin="8"
materialDesign:HintAssist.HelperText="{x:Static properties:Resources.KCVHelper}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.KCV}"
Visibility="{Binding ShowKCV, ElementName=keyControl, Converter={StaticResource BooleanToVisibilityConverter}}"
IsReadOnly="True">
<TextBox.Text>
<MultiBinding Converter="{StaticResource KeyChecksumConverter}" Mode="OneWay">
<Binding Path="SelectedKeyChecksumAlgorithm" ElementName="keyControl" />
<Binding Path="Key" ElementName="keyControl" />
<Binding Path="KeyChecksumIV" ElementName="keyControl" />
<Binding Path="Value" />
</MultiBinding>
</TextBox.Text>
</TextBox>
<ComboBox Grid.Column="1" Grid.Row="1" Margin="8"
ItemsSource="{Binding Path=KeyChecksumAlgorithms, Mode=OneWay, ElementName=keyControl}"
SelectedItem="{Binding Path=SelectedKeyChecksumAlgorithm, Mode=TwoWay, ElementName=keyControl}"
Visibility="{Binding ShowKCV, ElementName=keyControl, Converter={StaticResource BooleanToVisibilityConverter}}"
materialDesign:HintAssist.HelperText="{x:Static properties:Resources.KCVAlgorithmHelper}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.KCVAlgorithm}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
Expand Down
16 changes: 9 additions & 7 deletions KeyManager.Library/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,13 @@ public void ValidatePolicies(string value)
if (mdata != null)
{
data.AddRange(mdata);
var padsize = m.OverrideSize > 0 ? m.OverrideSize : KeySize;
if (padKeySize && padsize > 0 && mdata.Length < padsize)
{
data.AddRange(new byte[padsize - mdata.Length]);
}
}
}
if (padKeySize && KeySize > 0 && data.Count < KeySize)
{
data.AddRange(new byte[KeySize - data.Count]);
}
return data.ToArray();
}

Expand All @@ -188,14 +189,15 @@ public void SetAggregatedValueAsString(string? value, KeyValueStringFormat forma
var invariant = KeyMaterial.GetInvariantStringValue(value, format);
if (!string.IsNullOrEmpty(invariant))
{
int length = (int)KeySize * 2;
int i = 0;
int pos = 0;
do
{
int pos = i * length;
int length = (int)(Materials[i].OverrideSize > 0 ? Materials[i].OverrideSize : KeySize) * 2;
var sub = invariant.Substring(pos, (pos + length) < invariant.Length ? length : (invariant.Length - pos));
Materials[i++].SetValueAsString(sub, KeyValueStringFormat.HexString);
} while (i < Materials.Count && invariant.Length >= (i + 1) * KeySize * 2);
pos += length;
} while (i < Materials.Count && invariant.Length > pos);
}
}
}
Expand Down
24 changes: 23 additions & 1 deletion KeyManager.Library/KeyMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ public KeyMaterial(string value) : this(value, null)

}

public KeyMaterial(string value, string? name)
public KeyMaterial(string value, string? name) : this(value, name, 0)
{

}

public KeyMaterial(string value, string? name, uint overrideSize)
{
_value = value ?? string.Empty;
_name = name;
_overrideSize = overrideSize;
}

[JsonIgnore]
Expand Down Expand Up @@ -53,6 +59,14 @@ public string? Name
set { SetProperty(ref _name, value); }
}

private uint _overrideSize;

public uint OverrideSize
{
get => _overrideSize;
set { SetProperty(ref _overrideSize, value); }
}

public static string? GetValueAsString(string? value, KeyValueStringFormat format)
{
if (value != null)
Expand Down Expand Up @@ -101,5 +115,13 @@ public static string GetInvariantStringValue(string? value, KeyValueStringFormat

[GeneratedRegex(".{2}")]
private static partial Regex HexStringRegex();

/// <summary>
/// Some key materials shouldn't be treated as a key... (eg. MIFARE key entries).
/// </summary>
public bool IsRealKeyMateriel
{
get { return OverrideSize == 0; }
}
}
}

0 comments on commit dc8798f

Please sign in to comment.