diff --git a/src/desfire/desfire.rs b/src/desfire/desfire.rs index 53cdc4b..502db6c 100644 --- a/src/desfire/desfire.rs +++ b/src/desfire/desfire.rs @@ -13,9 +13,9 @@ use crate::iso7816_4::apduresponse::APDUResponse; use num_traits::FromPrimitive; pub struct Desfire { - card: Box, - session_key: Option>, - cbc_iv: Option>, + pub card: Box, + pub session_key: Option>, + pub cbc_iv: Option>, } impl Desfire { @@ -227,7 +227,7 @@ impl Desfire { /// Format PICC /// Need Authentication for PICC / Application 0x000000 /// - fn format_picc(&self) -> Result<()> { + pub fn format_picc(&self) -> Result<()> { let cmd_format = APDUCommand { case: IsoCase::Case2Short, cla: 0x90, @@ -244,7 +244,7 @@ impl Desfire { /// Create Application for ApplicationID /// /// 3 Byte ID - 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 { return Err(InvalidApplicationID); } @@ -271,7 +271,7 @@ impl Desfire { /// 0x01 - 0x0D /// Array of 16 Bytes /// Version of Key(min. 0x10) - 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 { return Err(InvalidKeyID); } @@ -331,7 +331,7 @@ impl Desfire { /// Array of 16 Bytes /// Array of 16 Bytes /// Version of Key(min. 0x10) - 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 { return Err(InvalidKeyID); } @@ -393,7 +393,7 @@ impl Desfire { 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 { return Err(InvalidFileID); } @@ -413,6 +413,7 @@ impl Desfire { }; let response = self.card.transmit(cmd_create_file_standard).unwrap(); + println!("RESPONSE: {}", response); response.check() } @@ -423,7 +424,7 @@ impl Desfire { /// ID of File (0x00 - 0x20) /// Offset for File /// Data to write - 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 { return Err(InvalidFileID); } @@ -475,7 +476,7 @@ impl Desfire { /// ID of File (0x00 - 0x20) /// Offset for File /// Lenght of Data - fn read_data(&self, file_id: u8, offset: u32, length: usize) -> Result> { + pub fn read_data(&self, file_id: u8, offset: u32, length: usize) -> Result> { if file_id >= 0x20 { return Err(InvalidFileID); } @@ -509,6 +510,7 @@ impl Desfire { println!("RESPONSE: {}", response); 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()); }; @@ -542,7 +544,7 @@ fn generate_session_key_aes(rnd_a: &[u8], rnd_b: &[u8]) -> Option> { /// /// ID of Key for changing Application Keys /// generated keysettings -fn generate_keysetting1(change_key: u8, change_masterkey_settings: ChangeMasterKeySettings, create_delete_file: CreateDeleteFile, file_directory_access: FileDirectoryAccess, change_master_key: ChangeMasterKey) -> Result { +pub fn generate_keysetting1(change_key: u8, change_masterkey_settings: ChangeMasterKeySettings, create_delete_file: CreateDeleteFile, file_directory_access: FileDirectoryAccess, change_master_key: ChangeMasterKey) -> Result { 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), None => { @@ -560,7 +562,7 @@ fn generate_keysetting1(change_key: u8, change_masterkey_settings: ChangeMasterK /// /// Number of keys that can be stored within the application (0x01-0x0D) /// generated keysettings -fn generate_keysetting2(crypto_operations: CryptoOperationsType, file_identifier: FileIdentifiers, num_keys: u8) -> Result { +pub fn generate_keysetting2(crypto_operations: CryptoOperationsType, file_identifier: FileIdentifiers, num_keys: u8) -> Result { return if num_keys < 0x01 || num_keys >= 0x0D { Err(NumKeys) } else { @@ -576,7 +578,7 @@ fn generate_keysetting2(crypto_operations: CryptoOperationsType, file_identifier /// KeyID for Write Access /// KeyID for Read and Write Access /// KeyID for Configuration Access -fn generate_file_access_rights(read: u8, write: u8, read_write: u8, configure: u8) -> Result { +pub fn generate_file_access_rights(read: u8, write: u8, read_write: u8, configure: u8) -> Result { return if read > 0x0F || write >= 0x0F || read_write >= 0x0F || configure >= 0x0F { Err(InvalidKeyID) } else { diff --git a/src/desfire/mod.rs b/src/desfire/mod.rs index 2de8c1d..b37928e 100644 --- a/src/desfire/mod.rs +++ b/src/desfire/mod.rs @@ -166,4 +166,4 @@ pub enum FileTypes { mod apduinstructions; mod apdustatuscodes; -mod desfire; +pub mod desfire;