diff --git a/client/src/cmdlfgallagher.c b/client/src/cmdlfgallagher.c index c8d13d62b..ed46c8143 100644 --- a/client/src/cmdlfgallagher.c +++ b/client/src/cmdlfgallagher.c @@ -75,15 +75,12 @@ int demodGallagher(bool verbose) { uint8_t crc = bytebits_to_byte(g_DemodBuffer + 16 + (9 * 8), 8); uint8_t calc_crc = CRC8Cardx(arr, ARRAYLEN(arr)); - uint8_t region_code, issue_level; - uint16_t facility_code; - uint32_t card_number; - - decodeCardholderCredentials(arr, ®ion_code, &facility_code, &card_number, &issue_level); + GallagherCredentials_t creds = {0}; + decodeCardholderCredentials(arr, &creds); PrintAndLogEx(SUCCESS, "GALLAGHER - Region: " _GREEN_("%u") " Facility: " _GREEN_("%u") " Card No.: " _GREEN_("%u") " Issue Level: " _GREEN_("%u"), - region_code, facility_code, card_number, issue_level); - PrintAndLogEx(SUCCESS, " Displayed: " _GREEN_("%C%u"), region_code + 'A', facility_code); + creds.region_code, creds.facility_code, creds.card_number, creds.issue_level); + PrintAndLogEx(SUCCESS, " Displayed: " _GREEN_("%C%u"), creds.region_code + 'A', creds.facility_code); PrintAndLogEx(SUCCESS, " Raw: %08X%08X%08X", raw1, raw2, raw3); PrintAndLogEx(SUCCESS, " CRC: %02X - %02X (%s)", crc, calc_crc, (crc == calc_crc) ? "ok" : "fail"); return PM3_SUCCESS; @@ -142,10 +139,10 @@ static void setBitsInBlocks(uint32_t *blocks, uint8_t *pos, uint32_t data, uint8 } } -static void createBlocks(uint32_t *blocks, uint8_t rc, uint16_t fc, uint32_t cn, uint8_t il) { +static void createBlocks(uint32_t *blocks, GallagherCredentials_t *creds) { // put data into the correct places (Gallagher obfuscation) - uint8_t arr[8] = { 0x00 }; - encodeCardholderCredentials(arr, rc, fc, cn, il); + uint8_t arr[8] = {0}; + encodeCardholderCredentials(arr, creds); blocks[0] = blocks[1] = blocks[2] = 0; uint8_t pos = 0; @@ -236,8 +233,14 @@ static int CmdGallagherClone(const char *Cmd) { blocks[i] = bytes_to_num(raw + ((i - 1) * 4), sizeof(uint32_t)); } } else { + GallagherCredentials_t creds = { + .region_code = region_code, + .facility_code = facility_code, + .card_number = card_number, + .issue_level = issue_level, + }; // fill blocks 1 to 3 with Gallagher data - createBlocks(blocks + 1, region_code, facility_code, card_number, issue_level); + createBlocks(blocks + 1, &creds); } //Pac - compat mode, NRZ, data rate 40, 3 data blocks @@ -326,8 +329,14 @@ static int CmdGallagherSim(const char *Cmd) { if (!use_raw) { // generate Gallagher data + GallagherCredentials_t creds = { + .region_code = region_code, + .facility_code = facility_code, + .card_number = card_number, + .issue_level = issue_level, + }; uint32_t blocks[3]; - createBlocks(blocks, region_code, facility_code, card_number, issue_level); + createBlocks(blocks, &creds); // convert to the normal 'raw' format for (int i = 0; i < ARRAYLEN(blocks); i++) { diff --git a/client/src/mifare/gallaghercore.c b/client/src/mifare/gallaghercore.c index 71ad88300..1613fd3c6 100644 --- a/client/src/mifare/gallaghercore.c +++ b/client/src/mifare/gallaghercore.c @@ -62,34 +62,39 @@ static void descramble(uint8_t *arr, uint8_t len) { } } -void decodeCardholderCredentials(uint8_t *eight_bytes, uint8_t *region_code, uint16_t *facility_code, uint32_t *card_number, uint8_t *issue_level) { +void decodeCardholderCredentials(uint8_t *eight_bytes, GallagherCredentials_t *creds) { uint8_t *arr = eight_bytes; descramble(arr, 8); // 4bit region code - *region_code = (arr[3] & 0x1E) >> 1; + creds->region_code = (arr[3] & 0x1E) >> 1; // 16bit facility code - *facility_code = (arr[5] & 0x0F) << 12 | arr[1] << 4 | ((arr[7] >> 4) & 0x0F); + creds->facility_code = (arr[5] & 0x0F) << 12 | arr[1] << 4 | ((arr[7] >> 4) & 0x0F); // 24bit card number - *card_number = arr[0] << 16 | (arr[4] & 0x1F) << 11 | arr[2] << 3 | (arr[3] & 0xE0) >> 5; + creds->card_number = arr[0] << 16 | (arr[4] & 0x1F) << 11 | arr[2] << 3 | (arr[3] & 0xE0) >> 5; // 4bit issue level - *issue_level = arr[7] & 0x0F; + creds->issue_level = arr[7] & 0x0F; } -void encodeCardholderCredentials(uint8_t *eight_bytes, uint8_t region_code, uint16_t facility_code, uint32_t card_number, uint8_t issue_level) { +void encodeCardholderCredentials(uint8_t *eight_bytes, GallagherCredentials_t *creds) { + uint8_t rc = creds->region_code; + uint8_t fc = creds->facility_code; + uint8_t cn = creds->card_number; + uint8_t il = creds->issue_level; + // put data into the correct places (Gallagher obfuscation) - eight_bytes[0] = (card_number & 0xffffff) >> 16; - eight_bytes[1] = (facility_code & 0xfff) >> 4; - eight_bytes[2] = (card_number & 0x7ff) >> 3; - eight_bytes[3] = (card_number & 0x7) << 5 | (region_code & 0xf) << 1; - eight_bytes[4] = (card_number & 0xffff) >> 11; - eight_bytes[5] = (facility_code & 0xffff) >> 12; + eight_bytes[0] = (cn & 0xffffff) >> 16; + eight_bytes[1] = (fc & 0xfff) >> 4; + eight_bytes[2] = (cn & 0x7ff) >> 3; + eight_bytes[3] = (cn & 0x7) << 5 | (rc & 0xf) << 1; + eight_bytes[4] = (cn & 0xffff) >> 11; + eight_bytes[5] = (fc & 0xffff) >> 12; eight_bytes[6] = 0; - eight_bytes[7] = (facility_code & 0xf) << 4 | (issue_level & 0xf); + eight_bytes[7] = (fc & 0xf) << 4 | (il & 0xf); // more obfuscation scramble(eight_bytes, 8); diff --git a/client/src/mifare/gallaghercore.h b/client/src/mifare/gallaghercore.h index e55f2e693..37164f92e 100644 --- a/client/src/mifare/gallaghercore.h +++ b/client/src/mifare/gallaghercore.h @@ -12,10 +12,18 @@ #define MIFARE_GALLAGHERCORE_H__ #include "common.h" +#include -void encodeCardholderCredentials(uint8_t *eight_bytes, uint8_t region_code, uint16_t facility_code, uint32_t card_number, uint8_t issue_level); +typedef struct { + uint8_t region_code; + uint16_t facility_code; + uint32_t card_number; + uint8_t issue_level; +} GallagherCredentials_t; -void decodeCardholderCredentials(uint8_t *eight_bytes, uint8_t *region_code, uint16_t *facility_code, uint32_t *card_number, uint8_t *issue_level); +void encodeCardholderCredentials(uint8_t *eight_bytes, GallagherCredentials_t *creds); + +void decodeCardholderCredentials(uint8_t *eight_bytes, GallagherCredentials_t *creds); bool isValidGallagherCredentials(uint64_t region_code, uint64_t facility_code, uint64_t card_number, uint64_t issue_level);