From a65cfc5f4b45c603336057da66b54bed071eb86f Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Tue, 17 Dec 2019 08:19:42 +0100 Subject: [PATCH 1/4] Fix auth1 response. --- client/cmdhffelica.c | 10 ++++++++-- include/mifare.h | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index 19bc807df..1986cb9b8 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -631,8 +631,14 @@ static int CmdHFFelicaAuthentication1(const char *Cmd) { PrintAndLogEx(ERR, "\nGot no Response from card"); return PM3_ERFTRANS; } else { - PrintAndLogEx(NORMAL, "AUTH1 SUCCESS!"); - PrintAndLogEx(NORMAL, "%s", sprint_hex(resp.data.asBytes, 256)); + felica_auth1_response_t auth1_response; + memcpy(&auth1_response, (felica_auth1_response_t *)resp.data.asBytes, sizeof(felica_auth1_response_t)); + if (auth1_response.frame_response.IDm[0] != 0) { + PrintAndLogEx(SUCCESS, "\nGot auth1 response:"); + PrintAndLogEx(SUCCESS, "IDm: %s", sprint_hex(auth1_response.frame_response.IDm, sizeof(auth1_response.frame_response.IDm))); + PrintAndLogEx(SUCCESS, "M2C: %s", sprint_hex(auth1_response.m2c, sizeof(auth1_response.m2c))); + PrintAndLogEx(SUCCESS, "M3C: %s", sprint_hex(auth1_response.m3c, sizeof(auth1_response.m3c))); + } } return PM3_SUCCESS; } diff --git a/include/mifare.h b/include/mifare.h index e278b3794..0f2758f56 100644 --- a/include/mifare.h +++ b/include/mifare.h @@ -220,6 +220,12 @@ typedef struct { uint8_t option_version_list[4]; } PACKED felica_request_spec_response_t; +typedef struct { + felica_frame_response_t frame_response; + uint8_t m2c[8]; + uint8_t m3c[8]; +} PACKED felica_auth1_response_t; + typedef enum FELICA_COMMAND { FELICA_CONNECT = (1 << 0), FELICA_NO_DISCONNECT = (1 << 1), From bb53a40adbbc7cb46ad962a2aa62ac7d5d024fe4 Mon Sep 17 00:00:00 2001 From: Thomas Sutter Date: Tue, 17 Dec 2019 13:09:14 +0100 Subject: [PATCH 2/4] Add auth1 output - experimental. Add auth2 command - experimental. --- client/cmdhffelica.c | 173 +++++++++++++++++++++++++++++++++++++++++-- include/mifare.h | 8 ++ 2 files changed, 176 insertions(+), 5 deletions(-) diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index 1986cb9b8..a8835e1e7 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -292,7 +292,7 @@ static int usage_hf_felica_authentication1() { PrintAndLogEx(NORMAL, " - Number of Services m: 1-byte (1 <= n <= 8)"); PrintAndLogEx(NORMAL, " - Service Code List: 2n byte"); PrintAndLogEx(NORMAL, " - 3DES-Key: 128-bit master secret used for the encryption"); - PrintAndLogEx(NORMAL, " - M1c: Encrypted random number (challenge for tag authentication) 8-byte"); + PrintAndLogEx(NORMAL, " - M1c: Encrypted random number - challenge for tag authentication (8-byte)"); PrintAndLogEx(NORMAL, " - Response:"); PrintAndLogEx(NORMAL, " - Response Code: 11h 1-byte"); PrintAndLogEx(NORMAL, " - Manufacture ID(IDm): 8-byte"); @@ -301,7 +301,7 @@ static int usage_hf_felica_authentication1() { PrintAndLogEx(NORMAL, " - Success: Card Mode switches to Mode1. You can check this with the request response command."); PrintAndLogEx(NORMAL, " - Unsuccessful: Card should not respond at all."); - PrintAndLogEx(NORMAL, "\nUsage: hf felica auth1 [-h][-i] <01 Number of Areas hex> <0A0B... Area Code List hex> <01 Number of Services hex> <0A0B... Service Code List hex> <0x0102030405060809 3DES-key hex (128bit)>"); + PrintAndLogEx(NORMAL, "\nUsage: hf felica auth1 [-h][-i] <01 Number of Areas hex> <0A0B... Area Code List hex> <01 Number of Services hex> <0A0B... Service Code List hex> <0x0102030405060809... 3DES-key hex (16-byte)>"); PrintAndLogEx(NORMAL, " -h this help"); PrintAndLogEx(NORMAL, " -i <0A0B0C ... hex> set custom IDm to use"); PrintAndLogEx(NORMAL, "\nExamples: "); @@ -311,6 +311,30 @@ static int usage_hf_felica_authentication1() { return PM3_SUCCESS; } +static int usage_hf_felica_authentication2() { + PrintAndLogEx(NORMAL, "\nInfo: Complete mutual authentication. This command can only be executed subsquent to Authentication1" + " command."); + PrintAndLogEx(NORMAL, " - Auth2 Parameters:"); + PrintAndLogEx(NORMAL, " - Manufacturer IDm: (8-byte)"); + PrintAndLogEx(NORMAL, " - M3c: card challenge (8-byte)"); + PrintAndLogEx(NORMAL, " - 3DES Key: key used for decryption of M3c (16-byte)"); + PrintAndLogEx(NORMAL, " - Response (encrypted):"); + PrintAndLogEx(NORMAL, " - Response Code: 13h (1-byte)"); + PrintAndLogEx(NORMAL, " - IDtc: (8-byte)"); + PrintAndLogEx(NORMAL, " - IDi (encrypted): (8-byte)"); + PrintAndLogEx(NORMAL, " - PMi (encrypted): (8-byte)"); + PrintAndLogEx(NORMAL, " - Success: Card switches to mode2 and sends response frame."); + PrintAndLogEx(NORMAL, " - Unsuccessful: Card should not respond at all."); + PrintAndLogEx(NORMAL, "\nUsage: hf felica auth2 [-h][-i] <0102030405060708 M3c hex> <0x0102030405060809... 3DES-key hex (16-byte)>"); + PrintAndLogEx(NORMAL, " -h this help"); + PrintAndLogEx(NORMAL, " -i <0A0B0C ... hex> set custom IDm to use"); + PrintAndLogEx(NORMAL, "\nExamples: "); + PrintAndLogEx(NORMAL, " hf felica auth2 0102030405060708 AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBB"); + PrintAndLogEx(NORMAL, " hf felica auth2 -i 11100910C11BC407 0102030405060708 AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBB\n\n"); + + return PM3_SUCCESS; +} + /** * Wait for response from pm3 or timeout. * Checks if receveid bytes have a valid CRC. @@ -530,6 +554,12 @@ int send_wr_unencrypted(uint8_t flags, uint16_t datalen, uint8_t *data, bool ver } } +static void reverse_3des_key(uint8_t *master_key, int length, uint8_t *reverse_master_key){ + for(int i=0; i Date: Tue, 17 Dec 2019 13:10:06 +0100 Subject: [PATCH 3/4] Make style. --- client/cmdhffelica.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index a8835e1e7..1013518a8 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -313,7 +313,7 @@ static int usage_hf_felica_authentication1() { static int usage_hf_felica_authentication2() { PrintAndLogEx(NORMAL, "\nInfo: Complete mutual authentication. This command can only be executed subsquent to Authentication1" - " command."); + " command."); PrintAndLogEx(NORMAL, " - Auth2 Parameters:"); PrintAndLogEx(NORMAL, " - Manufacturer IDm: (8-byte)"); PrintAndLogEx(NORMAL, " - M3c: card challenge (8-byte)"); @@ -554,9 +554,9 @@ int send_wr_unencrypted(uint8_t flags, uint16_t datalen, uint8_t *data, bool ver } } -static void reverse_3des_key(uint8_t *master_key, int length, uint8_t *reverse_master_key){ - for(int i=0; i Date: Tue, 17 Dec 2019 13:18:00 +0100 Subject: [PATCH 4/4] Fix swapping. Make Style. --- client/cmdhffelica.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index 1013518a8..398b771d3 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -554,9 +554,15 @@ int send_wr_unencrypted(uint8_t flags, uint16_t datalen, uint8_t *data, bool ver } } +/** + * Reverses the master secret. Example: AA AA AA AA AA AA AA BB to BB AA AA AA AA AA AA AA + * @param master_key the secret which order will be reversed. + * @param length in bytes of the master secret. + * @param reverse_master_key output in which the reversed secret is stored. + */ static void reverse_3des_key(uint8_t *master_key, int length, uint8_t *reverse_master_key) { for (int i = 0; i < length; i++) { - reverse_master_key[i] = master_key[(length + 1) - i]; + reverse_master_key[i] = master_key[(length - 1) - i]; } }; @@ -689,7 +695,8 @@ static int CmdHFFelicaAuthentication1(const char *Cmd) { if (isKeyCorrect) { PrintAndLogEx(SUCCESS, "\nAuth1 done with correct key material! Use Auth2 now with M3C and same key"); } else { - PrintAndLogEx(INFO, "\n\nP2c: %s", sprint_hex(p2c, 8)); + PrintAndLogEx(INFO, "3DES secret (swapped decryption): %s", sprint_hex(reverse_master_key, 16)); + PrintAndLogEx(INFO, "P2c: %s", sprint_hex(p2c, 8)); PrintAndLogEx(ERR, "Can't decrypt M2C with master secret (P1c != P2c)! Probably wrong keys or wrong decryption method"); } }