make desfire pub

This commit is contained in:
Kai Jan Kriegel 2021-12-26 05:19:48 +01:00
parent cd299c01bb
commit f0f0207221
2 changed files with 16 additions and 14 deletions

View File

@ -13,9 +13,9 @@ use crate::iso7816_4::apduresponse::APDUResponse;
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
pub struct Desfire { pub struct Desfire {
card: Box<dyn Card>, pub card: Box<dyn Card>,
session_key: Option<Vec<u8>>, pub session_key: Option<Vec<u8>>,
cbc_iv: Option<Vec<u8>>, pub cbc_iv: Option<Vec<u8>>,
} }
impl Desfire { impl Desfire {
@ -227,7 +227,7 @@ impl Desfire {
/// Format PICC /// Format PICC
/// Need Authentication for PICC / Application 0x000000 /// Need Authentication for PICC / Application 0x000000
/// </summary> /// </summary>
fn format_picc(&self) -> Result<()> { pub fn format_picc(&self) -> Result<()> {
let cmd_format = APDUCommand { let cmd_format = APDUCommand {
case: IsoCase::Case2Short, case: IsoCase::Case2Short,
cla: 0x90, cla: 0x90,
@ -244,7 +244,7 @@ impl Desfire {
/// Create Application for ApplicationID /// Create Application for ApplicationID
/// </summary> /// </summary>
/// <param name="aid">3 Byte ID</param> /// <param name="aid">3 Byte ID</param>
fn create_application(&self, aid: u32, keysetting1: u8, keysetting2: u8) -> Result<()> { pub fn create_application(&self, aid: u32, keysetting1: u8, keysetting2: u8) -> Result<()> {
if aid > 0xFFFFFF { if aid > 0xFFFFFF {
return Err(InvalidApplicationID); return Err(InvalidApplicationID);
} }
@ -271,7 +271,7 @@ impl Desfire {
/// <param name="key_id">0x01 - 0x0D</param> /// <param name="key_id">0x01 - 0x0D</param>
/// <param name="new_key">Array of 16 Bytes</param> /// <param name="new_key">Array of 16 Bytes</param>
/// <param name="key_version">Version of Key(min. 0x10)</param> /// <param name="key_version">Version of Key(min. 0x10)</param>
fn change_key_aes(&mut self, key_id: u8, new_key: &[u8], key_version: u8) -> Result<()> { pub fn change_key_aes(&mut self, key_id: u8, new_key: &[u8], key_version: u8) -> Result<()> {
if key_id >= 0x0E { if key_id >= 0x0E {
return Err(InvalidKeyID); return Err(InvalidKeyID);
} }
@ -331,7 +331,7 @@ impl Desfire {
/// <param name="new_key">Array of 16 Bytes</param> /// <param name="new_key">Array of 16 Bytes</param>
/// <param name="old_key">Array of 16 Bytes</param> /// <param name="old_key">Array of 16 Bytes</param>
/// <param name="key_version">Version of Key(min. 0x10)</param> /// <param name="key_version">Version of Key(min. 0x10)</param>
fn change_other_key_aes(&mut self, key_id: u8, new_key: &[u8], old_key: &[u8], key_version: u8) -> Result<()> { pub fn change_other_key_aes(&mut self, key_id: u8, new_key: &[u8], old_key: &[u8], key_version: u8) -> Result<()> {
if key_id >= 0x0E { if key_id >= 0x0E {
return Err(InvalidKeyID); return Err(InvalidKeyID);
} }
@ -393,7 +393,7 @@ impl Desfire {
response.check() response.check()
} }
fn create_file_standard(&self, file_id: u8, communication: FileCommunication, access_rights: u16, size: u32) -> Result<()> { pub fn create_file_standard(&self, file_id: u8, communication: FileCommunication, access_rights: u16, size: u32) -> Result<()> {
if file_id >= 0x20 { if file_id >= 0x20 {
return Err(InvalidFileID); return Err(InvalidFileID);
} }
@ -413,6 +413,7 @@ impl Desfire {
}; };
let response = self.card.transmit(cmd_create_file_standard).unwrap(); let response = self.card.transmit(cmd_create_file_standard).unwrap();
println!("RESPONSE: {}", response);
response.check() response.check()
} }
@ -423,7 +424,7 @@ impl Desfire {
/// <param name="file_id">ID of File (0x00 - 0x20)</param> /// <param name="file_id">ID of File (0x00 - 0x20)</param>
/// <param name="offset">Offset for File</param> /// <param name="offset">Offset for File</param>
/// <param name="data">Data to write</param> /// <param name="data">Data to write</param>
fn write_data(&self, file_id: u8, offset: u32, data: &[u8]) -> Result<()> { pub fn write_data(&self, file_id: u8, offset: u32, data: &[u8]) -> Result<()> {
if file_id >= 0x20 { if file_id >= 0x20 {
return Err(InvalidFileID); return Err(InvalidFileID);
} }
@ -475,7 +476,7 @@ impl Desfire {
/// <param name="file_id">ID of File (0x00 - 0x20)</param> /// <param name="file_id">ID of File (0x00 - 0x20)</param>
/// <param name="offset">Offset for File</param> /// <param name="offset">Offset for File</param>
/// <param name="length">Lenght of Data</param> /// <param name="length">Lenght of Data</param>
fn read_data(&self, file_id: u8, offset: u32, length: usize) -> Result<Vec<u8>> { pub fn read_data(&self, file_id: u8, offset: u32, length: usize) -> Result<Vec<u8>> {
if file_id >= 0x20 { if file_id >= 0x20 {
return Err(InvalidFileID); return Err(InvalidFileID);
} }
@ -509,6 +510,7 @@ impl Desfire {
println!("RESPONSE: {}", response); println!("RESPONSE: {}", response);
response.check().or_else(|e| return Err(e)); response.check().or_else(|e| return Err(e));
// println!("RESPONSE_DATA: {:x?}, WITHOUT_CMAC: {:x?}", response.body.as_ref().unwrap(), response.body.as_ref().unwrap()[..bytes_toread].to_vec());
read_buffer.append(&mut response.body.unwrap()[..bytes_toread].to_vec()); read_buffer.append(&mut response.body.unwrap()[..bytes_toread].to_vec());
}; };
@ -542,7 +544,7 @@ fn generate_session_key_aes(rnd_a: &[u8], rnd_b: &[u8]) -> Option<Vec<u8>> {
/// </summary> /// </summary>
/// <param name="changeKey">ID of Key for changing Application Keys</param> /// <param name="changeKey">ID of Key for changing Application Keys</param>
/// <returns>generated keysettings</returns> /// <returns>generated keysettings</returns>
fn generate_keysetting1(change_key: u8, change_masterkey_settings: ChangeMasterKeySettings, create_delete_file: CreateDeleteFile, file_directory_access: FileDirectoryAccess, change_master_key: ChangeMasterKey) -> Result<u8> { pub fn generate_keysetting1(change_key: u8, change_masterkey_settings: ChangeMasterKeySettings, create_delete_file: CreateDeleteFile, file_directory_access: FileDirectoryAccess, change_master_key: ChangeMasterKey) -> Result<u8> {
return match FromPrimitive::from_u8(change_key) { return match FromPrimitive::from_u8(change_key) {
Some(ChangeApplicationKey::MASTERKEY) | Some(ChangeApplicationKey::SAMEKEY) | Some(ChangeApplicationKey::ALLKEYS) => Ok((change_key << 4) | change_masterkey_settings as u8 | create_delete_file as u8 | file_directory_access as u8 | change_master_key as u8), Some(ChangeApplicationKey::MASTERKEY) | Some(ChangeApplicationKey::SAMEKEY) | Some(ChangeApplicationKey::ALLKEYS) => Ok((change_key << 4) | change_masterkey_settings as u8 | create_delete_file as u8 | file_directory_access as u8 | change_master_key as u8),
None => { None => {
@ -560,7 +562,7 @@ fn generate_keysetting1(change_key: u8, change_masterkey_settings: ChangeMasterK
/// </summary> /// </summary>
/// <param name="numberOfKeys">Number of keys that can be stored within the application (0x01-0x0D)</param> /// <param name="numberOfKeys">Number of keys that can be stored within the application (0x01-0x0D)</param>
/// <returns>generated keysettings</returns> /// <returns>generated keysettings</returns>
fn generate_keysetting2(crypto_operations: CryptoOperationsType, file_identifier: FileIdentifiers, num_keys: u8) -> Result<u8> { pub fn generate_keysetting2(crypto_operations: CryptoOperationsType, file_identifier: FileIdentifiers, num_keys: u8) -> Result<u8> {
return if num_keys < 0x01 || num_keys >= 0x0D { return if num_keys < 0x01 || num_keys >= 0x0D {
Err(NumKeys) Err(NumKeys)
} else { } else {
@ -576,7 +578,7 @@ fn generate_keysetting2(crypto_operations: CryptoOperationsType, file_identifier
/// <param name="write">KeyID for Write Access</param> /// <param name="write">KeyID for Write Access</param>
/// <param name="read_write">KeyID for Read and Write Access</param> /// <param name="read_write">KeyID for Read and Write Access</param>
/// <param name="configure">KeyID for Configuration Access</param> /// <param name="configure">KeyID for Configuration Access</param>
fn generate_file_access_rights(read: u8, write: u8, read_write: u8, configure: u8) -> Result<u16> { pub fn generate_file_access_rights(read: u8, write: u8, read_write: u8, configure: u8) -> Result<u16> {
return if read > 0x0F || write >= 0x0F || read_write >= 0x0F || configure >= 0x0F { return if read > 0x0F || write >= 0x0F || read_write >= 0x0F || configure >= 0x0F {
Err(InvalidKeyID) Err(InvalidKeyID)
} else { } else {

View File

@ -166,4 +166,4 @@ pub enum FileTypes {
mod apduinstructions; mod apduinstructions;
mod apdustatuscodes; mod apdustatuscodes;
mod desfire; pub mod desfire;