Extract gallagher encoding/decoding

Change input parameters to be `arg_u640` so that we can check they are the right lengths (and not have them silently truncated)
This commit is contained in:
Matt Moran 2021-12-29 20:54:03 +13:00
commit f0b82c6311
7 changed files with 176 additions and 122 deletions

View file

@ -234,6 +234,7 @@ set (TARGET_SOURCES
${PM3_ROOT}/client/src/mifare/desfiresecurechan.c ${PM3_ROOT}/client/src/mifare/desfiresecurechan.c
${PM3_ROOT}/client/src/mifare/desfirecore.c ${PM3_ROOT}/client/src/mifare/desfirecore.c
${PM3_ROOT}/client/src/mifare/desfiretest.c ${PM3_ROOT}/client/src/mifare/desfiretest.c
${PM3_ROOT}/client/src/mifare/gallaghercore.c
${PM3_ROOT}/client/src/uart/uart_posix.c ${PM3_ROOT}/client/src/uart/uart_posix.c
${PM3_ROOT}/client/src/uart/uart_win32.c ${PM3_ROOT}/client/src/uart/uart_win32.c
${PM3_ROOT}/client/src/ui/overlays.ui ${PM3_ROOT}/client/src/ui/overlays.ui

View file

@ -612,6 +612,7 @@ SRCS = mifare/aiddesfire.c \
mifare/desfirecore.c \ mifare/desfirecore.c \
mifare/desfiresecurechan.c \ mifare/desfiresecurechan.c \
mifare/desfiretest.c \ mifare/desfiretest.c \
mifare/gallaghercore.c \
mifare/mad.c \ mifare/mad.c \
mifare/mfkey.c \ mifare/mfkey.c \
mifare/mifare4.c \ mifare/mifare4.c \

View file

@ -99,6 +99,7 @@ add_library(pm3rrg_rdv4 SHARED
${PM3_ROOT}/client/src/mifare/desfiresecurechan.c ${PM3_ROOT}/client/src/mifare/desfiresecurechan.c
${PM3_ROOT}/client/src/mifare/desfirecore.c ${PM3_ROOT}/client/src/mifare/desfirecore.c
${PM3_ROOT}/client/src/mifare/desfiretest.c ${PM3_ROOT}/client/src/mifare/desfiretest.c
${PM3_ROOT}/client/src/mifare/gallaghercore.c
${PM3_ROOT}/client/src/uart/uart_posix.c ${PM3_ROOT}/client/src/uart/uart_posix.c
${PM3_ROOT}/client/src/uart/uart_win32.c ${PM3_ROOT}/client/src/uart/uart_win32.c
${PM3_ROOT}/client/src/ui/overlays.ui ${PM3_ROOT}/client/src/ui/overlays.ui

View file

@ -233,6 +233,7 @@ set (TARGET_SOURCES
${PM3_ROOT}/client/src/mifare/desfiresecurechan.c ${PM3_ROOT}/client/src/mifare/desfiresecurechan.c
${PM3_ROOT}/client/src/mifare/desfirecore.c ${PM3_ROOT}/client/src/mifare/desfirecore.c
${PM3_ROOT}/client/src/mifare/desfiretest.c ${PM3_ROOT}/client/src/mifare/desfiretest.c
${PM3_ROOT}/client/src/mifare/gallaghercore.c
${PM3_ROOT}/client/src/uart/uart_posix.c ${PM3_ROOT}/client/src/uart/uart_posix.c
${PM3_ROOT}/client/src/uart/uart_win32.c ${PM3_ROOT}/client/src/uart/uart_win32.c
${PM3_ROOT}/client/src/ui/overlays.ui ${PM3_ROOT}/client/src/ui/overlays.ui

View file

@ -9,7 +9,10 @@
// ASK/MAN, RF/32, 96 bits long (unknown cs) (0x00088060) // ASK/MAN, RF/32, 96 bits long (unknown cs) (0x00088060)
// sample Q5 , ASK RF/32, STT, 96 bits (3blocks) ( 0x9000F006) // sample Q5 , ASK RF/32, STT, 96 bits (3blocks) ( 0x9000F006)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Modified by: Matt Moran (@DarkMatterMatt), 2021
#include "cmdlfgallagher.h" #include "cmdlfgallagher.h"
#include "mifare/gallaghercore.h"
#include <string.h> // memcpy #include <string.h> // memcpy
#include <ctype.h> // tolower #include <ctype.h> // tolower
#include <stdio.h> #include <stdio.h>
@ -29,56 +32,6 @@
static int CmdHelp(const char *Cmd); static int CmdHelp(const char *Cmd);
static void scramble(uint8_t *arr, uint8_t len) {
uint8_t lut[] = {
0xa3, 0xb0, 0x80, 0xc6, 0xb2, 0xf4, 0x5c, 0x6c, 0x81, 0xf1, 0xbb, 0xeb, 0x55, 0x67, 0x3c, 0x05,
0x1a, 0x0e, 0x61, 0xf6, 0x22, 0xce, 0xaa, 0x8f, 0xbd, 0x3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, 0x23, 0xa1, 0xcd, 0xb5, 0x7d, 0xc7, 0xa9, 0xc8, 0xef, 0xdf, 0x02, 0xb8, 0x03,
0x6b, 0x35, 0x3e, 0x2c, 0x76, 0xc9, 0xde, 0x1c, 0x4b, 0xd1, 0xed, 0x14, 0xc5, 0xad, 0xe9, 0x64,
0x4a, 0xec, 0x8d, 0xf7, 0x10, 0x43, 0x78, 0x15, 0x87, 0xe4, 0xd7, 0x92, 0xe1, 0xee, 0xe3, 0x90
};
for (int i = 0; i < len; i++) {
arr[i] = lut[arr[i]];
}
}
static void descramble(uint8_t *arr, uint8_t len) {
uint8_t lut[] = {
0x2f, 0x6e, 0xdd, 0xdf, 0x1d, 0x0f, 0xb0, 0x76, 0xad, 0xaf, 0x7f, 0xbb, 0x77, 0x85, 0x11, 0x6d,
0xf4, 0xd2, 0x84, 0x42, 0xeb, 0xf7, 0x34, 0x55, 0x4a, 0x3a, 0x10, 0x71, 0xe7, 0xa1, 0x62, 0x1a,
0x3e, 0x4c, 0x14, 0xd3, 0x5e, 0xb2, 0x7d, 0x56, 0xbc, 0x27, 0x82, 0x60, 0xe3, 0xae, 0x1f, 0x9b,
0xaa, 0x2b, 0x95, 0x49, 0x73, 0xe1, 0x92, 0x79, 0x91, 0x38, 0x6c, 0x19, 0x0e, 0xa9, 0xe2, 0x8d,
0x66, 0xc7, 0x5a, 0xf5, 0x1c, 0x80, 0x99, 0xbe, 0x4e, 0x41, 0xf0, 0xe8, 0xa6, 0x20, 0xab, 0x87,
0xc8, 0x1e, 0xa0, 0x59, 0x7b, 0x0c, 0xc3, 0x3c, 0x61, 0xcc, 0x40, 0x9e, 0x06, 0x52, 0x1b, 0x32,
0x8c, 0x12, 0x93, 0xbf, 0xef, 0x3b, 0x25, 0x0d, 0xc2, 0x88, 0xd1, 0xe0, 0x07, 0x2d, 0x70, 0xc6,
0x29, 0x6a, 0x4d, 0x47, 0x26, 0xa3, 0xe4, 0x8b, 0xf6, 0x97, 0x2c, 0x5d, 0x3d, 0xd7, 0x96, 0x28,
0x02, 0x08, 0x30, 0xa7, 0x22, 0xc9, 0x65, 0xf8, 0xb7, 0xb4, 0x8a, 0xca, 0xb9, 0xf2, 0xd0, 0x17,
0xff, 0x46, 0xfb, 0x9a, 0xba, 0x8f, 0xb6, 0x69, 0x68, 0x8e, 0x21, 0x6f, 0xc4, 0xcb, 0xb3, 0xce,
0x51, 0xd4, 0x81, 0x00, 0x2e, 0x9c, 0x74, 0x63, 0x45, 0xd9, 0x16, 0x35, 0x5f, 0xed, 0x78, 0x9f,
0x01, 0x48, 0x04, 0xc1, 0x33, 0xd6, 0x4f, 0x94, 0xde, 0x31, 0x9d, 0x0a, 0xac, 0x18, 0x4b, 0xcd,
0x98, 0xb8, 0x37, 0xa2, 0x83, 0xec, 0x03, 0xd8, 0xda, 0xe5, 0x7a, 0x6b, 0x53, 0xd5, 0x15, 0xa4,
0x43, 0xe9, 0x90, 0x67, 0x58, 0xc0, 0xa5, 0xfa, 0x2a, 0xb1, 0x75, 0x50, 0x39, 0x5c, 0xe6, 0xdc,
0x89, 0xfc, 0xcf, 0xfe, 0xf9, 0x57, 0x54, 0x64, 0xa8, 0xee, 0x23, 0x0b, 0xf1, 0xea, 0xfd, 0xdb,
0xbd, 0x09, 0xb5, 0x5b, 0x05, 0x86, 0x13, 0xf3, 0x24, 0xc5, 0x3f, 0x44, 0x72, 0x7c, 0x7e, 0x36
};
for (int i = 0; i < len; i++) {
arr[i] = lut[arr[i]];
}
}
//see ASK/MAN Demod for what args are accepted //see ASK/MAN Demod for what args are accepted
int demodGallagher(bool verbose) { int demodGallagher(bool verbose) {
(void) verbose; // unused so far (void) verbose; // unused so far
@ -113,6 +66,7 @@ int demodGallagher(bool verbose) {
// bytes // bytes
uint8_t arr[8] = {0}; uint8_t arr[8] = {0};
for (int i = 0, pos = 0; i < ARRAYLEN(arr); i++) { for (int i = 0, pos = 0; i < ARRAYLEN(arr); i++) {
// first 16 bits are the 7FEA prefix, then every 9th bit is a checksum-bit for the preceding byte
pos = 16 + (9 * i); pos = 16 + (9 * i);
arr[i] = bytebits_to_byte(g_DemodBuffer + pos, 8); arr[i] = bytebits_to_byte(g_DemodBuffer + pos, 8);
} }
@ -121,22 +75,15 @@ int demodGallagher(bool verbose) {
uint8_t crc = bytebits_to_byte(g_DemodBuffer + 16 + (9 * 8), 8); uint8_t crc = bytebits_to_byte(g_DemodBuffer + 16 + (9 * 8), 8);
uint8_t calc_crc = CRC8Cardx(arr, ARRAYLEN(arr)); uint8_t calc_crc = CRC8Cardx(arr, ARRAYLEN(arr));
descramble(arr, ARRAYLEN(arr)); uint8_t region_code, issue_level;
uint16_t facility_code;
uint32_t card_number;
// 4bit region code decodeCardholderCredentials(arr, &region_code, &facility_code, &card_number, &issue_level);
uint8_t rc = (arr[3] & 0x1E) >> 1;
// 16bit FC PrintAndLogEx(SUCCESS, "GALLAGHER - Region: " _GREEN_("%u") " Facility: " _GREEN_("%u") " Card No.: " _GREEN_("%u") " Issue Level: " _GREEN_("%u"),
uint16_t fc = (arr[5] & 0x0F) << 12 | arr[1] << 4 | ((arr[7] >> 4) & 0x0F); region_code, facility_code, card_number, issue_level);
PrintAndLogEx(SUCCESS, " Displayed: " _GREEN_("%C%u"), region_code + 'A', facility_code);
// 24bit CN
uint32_t cn = arr[0] << 16 | (arr[4] & 0x1F) << 11 | arr[2] << 3 | (arr[3] & 0xE0) >> 5;
// 4bit issue level
uint8_t il = arr[7] & 0x0F;
PrintAndLogEx(SUCCESS, "GALLAGHER - Region: " _GREEN_("%u") " FC: " _GREEN_("%u") " CN: " _GREEN_("%u") " Issue Level: " _GREEN_("%u"), rc, fc, cn, il);
PrintAndLogEx(SUCCESS, " Displayed: " _GREEN_("%C%u"), rc + 'A', fc);
PrintAndLogEx(SUCCESS, " Raw: %08X%08X%08X", raw1, raw2, raw3); PrintAndLogEx(SUCCESS, " Raw: %08X%08X%08X", raw1, raw2, raw3);
PrintAndLogEx(SUCCESS, " CRC: %02X - %02X (%s)", crc, calc_crc, (crc == calc_crc) ? "ok" : "fail"); PrintAndLogEx(SUCCESS, " CRC: %02X - %02X (%s)", crc, calc_crc, (crc == calc_crc) ? "ok" : "fail");
return PM3_SUCCESS; return PM3_SUCCESS;
@ -185,34 +132,6 @@ static int CmdGallagherReader(const char *Cmd) {
return PM3_SUCCESS; return PM3_SUCCESS;
} }
static bool isValidGallagherParams(int8_t rc, int32_t fc, int32_t cn, int8_t il) {
bool isValid = true;
// if one is set, all must be set
if (rc < 0 || fc < 0 || cn < 0 || il < 0) {
PrintAndLogEx(FAILED, "If rc/fc/cn/il is specified, all must be set");
isValid = false;
}
// validate input
if (rc > 0x0f) {
PrintAndLogEx(FAILED, "Region code must be less than 16 (4 bits)");
isValid = false;
}
if (fc > 0xffff) {
PrintAndLogEx(FAILED, "Facility code must be less than 65536 (2 bytes)");
isValid = false;
}
if (cn > 0xffffff) {
PrintAndLogEx(FAILED, "Card number must be less than 16777216 (3 bytes)");
isValid = false;
}
if (il > 0x0f) {
PrintAndLogEx(FAILED, "Issue level must be less than 16 (4 bits)");
isValid = false;
}
return isValid;
}
static void setBitsInBlocks(uint32_t *blocks, uint8_t *pos, uint32_t data, uint8_t data_len) { static void setBitsInBlocks(uint32_t *blocks, uint8_t *pos, uint32_t data, uint8_t data_len) {
for (int i = data_len - 1; i >= 0; i--) { for (int i = data_len - 1; i >= 0; i--) {
uint8_t blk = *pos / 32; uint8_t blk = *pos / 32;
@ -225,18 +144,8 @@ 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, uint8_t rc, uint16_t fc, uint32_t cn, uint8_t il) {
// put data into the correct places (Gallagher obfuscation) // put data into the correct places (Gallagher obfuscation)
uint8_t arr[8] = {0}; uint8_t arr[8] = { 0x00 };
arr[0] = (cn & 0xffffff) >> 16; encodeCardholderCredentials(arr, rc, fc, cn, il);
arr[1] = (fc & 0xfff) >> 4;
arr[2] = (cn & 0x7ff) >> 3;
arr[3] = (cn & 0x7) << 5 | (rc & 0xf) << 1;
arr[4] = (cn & 0xffff) >> 11;
arr[5] = (fc & 0xffff) >> 12;
arr[6] = 0;
arr[7] = (fc & 0xf) << 4 | (il & 0xf);
// more obfuscation
scramble(arr, ARRAYLEN(arr));
blocks[0] = blocks[1] = blocks[2] = 0; blocks[0] = blocks[1] = blocks[2] = 0;
uint8_t pos = 0; uint8_t pos = 0;
@ -273,10 +182,10 @@ static int CmdGallagherClone(const char *Cmd) {
arg_str0("r", "raw", "<hex>", "raw hex data. 12 bytes max"), arg_str0("r", "raw", "<hex>", "raw hex data. 12 bytes max"),
arg_lit0(NULL, "q5", "optional - specify writing to Q5/T5555 tag"), arg_lit0(NULL, "q5", "optional - specify writing to Q5/T5555 tag"),
arg_lit0(NULL, "em", "optional - specify writing to EM4305/4469 tag"), arg_lit0(NULL, "em", "optional - specify writing to EM4305/4469 tag"),
arg_int0(NULL, "rc", "<decimal>", "Region code. 4 bits max"), arg_u64_0(NULL, "rc", "<decimal>", "Region code. 4 bits max"),
arg_int0(NULL, "fc", "<decimal>", "Facility code. 2 bytes max"), arg_u64_0(NULL, "fc", "<decimal>", "Facility code. 2 bytes max"),
arg_int0(NULL, "cn", "<decimal>", "Card number. 3 bytes max"), arg_u64_0(NULL, "cn", "<decimal>", "Card number. 3 bytes max"),
arg_int0(NULL, "il", "<decimal>", "Issue level. 4 bits max"), arg_u64_0(NULL, "il", "<decimal>", "Issue level. 4 bits max"),
arg_param_end arg_param_end
}; };
CLIExecWithReturn(ctx, Cmd, argtable, false); CLIExecWithReturn(ctx, Cmd, argtable, false);
@ -292,10 +201,10 @@ static int CmdGallagherClone(const char *Cmd) {
bool q5 = arg_get_lit(ctx, 2); bool q5 = arg_get_lit(ctx, 2);
bool em = arg_get_lit(ctx, 3); bool em = arg_get_lit(ctx, 3);
int16_t region_code = arg_get_int_def(ctx, 4, -1); uint64_t region_code = arg_get_u64_def(ctx, 4, -1); // uint16, will be validated later
int32_t facility_code = arg_get_int_def(ctx, 5, -1); uint64_t facility_code = arg_get_u64_def(ctx, 5, -1); // uint32, will be validated later
uint64_t card_number = arg_get_int_def(ctx, 6, -1); uint64_t card_number = arg_get_u64_def(ctx, 6, -1); // uint64
uint32_t issue_level = arg_get_int_def(ctx, 7, -1); uint64_t issue_level = arg_get_u64_def(ctx, 7, -1); // uint32, will be validated later
CLIParserFree(ctx); CLIParserFree(ctx);
bool use_raw = raw_len > 0; bool use_raw = raw_len > 0;
@ -316,7 +225,7 @@ static int CmdGallagherClone(const char *Cmd) {
PrintAndLogEx(FAILED, "Can't specify both raw and rc/fc/cn/il at the same time"); PrintAndLogEx(FAILED, "Can't specify both raw and rc/fc/cn/il at the same time");
return PM3_EINVARG; return PM3_EINVARG;
} }
if (!isValidGallagherParams(region_code, facility_code, card_number, issue_level)) { if (!isValidGallagherCredentials(region_code, facility_code, card_number, issue_level)) {
return PM3_EINVARG; return PM3_EINVARG;
} }
} }
@ -373,10 +282,10 @@ static int CmdGallagherSim(const char *Cmd) {
void *argtable[] = { void *argtable[] = {
arg_param_begin, arg_param_begin,
arg_str0("r", "raw", "<hex>", "raw hex data. 12 bytes max"), arg_str0("r", "raw", "<hex>", "raw hex data. 12 bytes max"),
arg_int0(NULL, "rc", "<decimal>", "Region code. 4 bits max"), arg_u64_0(NULL, "rc", "<decimal>", "Region code. 4 bits max"),
arg_int0(NULL, "fc", "<decimal>", "Facility code. 2 bytes max"), arg_u64_0(NULL, "fc", "<decimal>", "Facility code. 2 bytes max"),
arg_int0(NULL, "cn", "<decimal>", "Card number. 3 bytes max"), arg_u64_0(NULL, "cn", "<decimal>", "Card number. 3 bytes max"),
arg_int0(NULL, "il", "<decimal>", "Issue level. 4 bits max"), arg_u64_0(NULL, "il", "<decimal>", "Issue level. 4 bits max"),
arg_param_end arg_param_end
}; };
CLIExecWithReturn(ctx, Cmd, argtable, false); CLIExecWithReturn(ctx, Cmd, argtable, false);
@ -391,10 +300,10 @@ static int CmdGallagherSim(const char *Cmd) {
return PM3_EINVARG; return PM3_EINVARG;
} }
int16_t region_code = arg_get_int_def(ctx, 2, -1); uint64_t region_code = arg_get_u64_def(ctx, 2, -1); // uint16, will be validated later
int32_t facility_code = arg_get_int_def(ctx, 3, -1); uint64_t facility_code = arg_get_u64_def(ctx, 3, -1); // uint32, will be validated later
uint64_t card_number = arg_get_int_def(ctx, 4, -1); uint64_t card_number = arg_get_u64_def(ctx, 4, -1); // uint64
uint32_t issue_level = arg_get_int_def(ctx, 5, -1); uint64_t issue_level = arg_get_u64_def(ctx, 5, -1); // uint32, will be validated later
CLIParserFree(ctx); CLIParserFree(ctx);
bool use_raw = raw_len > 0; bool use_raw = raw_len > 0;
@ -410,7 +319,7 @@ static int CmdGallagherSim(const char *Cmd) {
PrintAndLogEx(FAILED, "Can't specify both raw and rc/fc/cn/il at the same time"); PrintAndLogEx(FAILED, "Can't specify both raw and rc/fc/cn/il at the same time");
return PM3_EINVARG; return PM3_EINVARG;
} }
if (!isValidGallagherParams(region_code, facility_code, card_number, issue_level)) { if (!isValidGallagherCredentials(region_code, facility_code, card_number, issue_level)) {
return PM3_EINVARG; return PM3_EINVARG;
} }
} }

View file

@ -0,0 +1,119 @@
/**
* Matt Moran (@DarkMatterMatt), 2021
* -----------------------------------------------------------------------------
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
* -----------------------------------------------------------------------------
* Common functionality for low/high-frequency GALLAGHER tag encoding & decoding.
*/
#include "gallaghercore.h"
#include "common.h"
#include "ui.h"
static void scramble(uint8_t *arr, uint8_t len) {
uint8_t lut[] = {
0xa3, 0xb0, 0x80, 0xc6, 0xb2, 0xf4, 0x5c, 0x6c, 0x81, 0xf1, 0xbb, 0xeb, 0x55, 0x67, 0x3c, 0x05,
0x1a, 0x0e, 0x61, 0xf6, 0x22, 0xce, 0xaa, 0x8f, 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0x6a, 0x11, 0x23, 0xa1, 0xcd, 0xb5, 0x7d, 0xc7, 0xa9, 0xc8, 0xef, 0xdf, 0x02, 0xb8, 0x03,
0x6b, 0x35, 0x3e, 0x2c, 0x76, 0xc9, 0xde, 0x1c, 0x4b, 0xd1, 0xed, 0x14, 0xc5, 0xad, 0xe9, 0x64,
0x4a, 0xec, 0x8d, 0xf7, 0x10, 0x43, 0x78, 0x15, 0x87, 0xe4, 0xd7, 0x92, 0xe1, 0xee, 0xe3, 0x90
};
for (int i = 0; i < len; i++) {
arr[i] = lut[arr[i]];
}
}
static void descramble(uint8_t *arr, uint8_t len) {
uint8_t lut[] = {
0x2f, 0x6e, 0xdd, 0xdf, 0x1d, 0x0f, 0xb0, 0x76, 0xad, 0xaf, 0x7f, 0xbb, 0x77, 0x85, 0x11, 0x6d,
0xf4, 0xd2, 0x84, 0x42, 0xeb, 0xf7, 0x34, 0x55, 0x4a, 0x3a, 0x10, 0x71, 0xe7, 0xa1, 0x62, 0x1a,
0x3e, 0x4c, 0x14, 0xd3, 0x5e, 0xb2, 0x7d, 0x56, 0xbc, 0x27, 0x82, 0x60, 0xe3, 0xae, 0x1f, 0x9b,
0xaa, 0x2b, 0x95, 0x49, 0x73, 0xe1, 0x92, 0x79, 0x91, 0x38, 0x6c, 0x19, 0x0e, 0xa9, 0xe2, 0x8d,
0x66, 0xc7, 0x5a, 0xf5, 0x1c, 0x80, 0x99, 0xbe, 0x4e, 0x41, 0xf0, 0xe8, 0xa6, 0x20, 0xab, 0x87,
0xc8, 0x1e, 0xa0, 0x59, 0x7b, 0x0c, 0xc3, 0x3c, 0x61, 0xcc, 0x40, 0x9e, 0x06, 0x52, 0x1b, 0x32,
0x8c, 0x12, 0x93, 0xbf, 0xef, 0x3b, 0x25, 0x0d, 0xc2, 0x88, 0xd1, 0xe0, 0x07, 0x2d, 0x70, 0xc6,
0x29, 0x6a, 0x4d, 0x47, 0x26, 0xa3, 0xe4, 0x8b, 0xf6, 0x97, 0x2c, 0x5d, 0x3d, 0xd7, 0x96, 0x28,
0x02, 0x08, 0x30, 0xa7, 0x22, 0xc9, 0x65, 0xf8, 0xb7, 0xb4, 0x8a, 0xca, 0xb9, 0xf2, 0xd0, 0x17,
0xff, 0x46, 0xfb, 0x9a, 0xba, 0x8f, 0xb6, 0x69, 0x68, 0x8e, 0x21, 0x6f, 0xc4, 0xcb, 0xb3, 0xce,
0x51, 0xd4, 0x81, 0x00, 0x2e, 0x9c, 0x74, 0x63, 0x45, 0xd9, 0x16, 0x35, 0x5f, 0xed, 0x78, 0x9f,
0x01, 0x48, 0x04, 0xc1, 0x33, 0xd6, 0x4f, 0x94, 0xde, 0x31, 0x9d, 0x0a, 0xac, 0x18, 0x4b, 0xcd,
0x98, 0xb8, 0x37, 0xa2, 0x83, 0xec, 0x03, 0xd8, 0xda, 0xe5, 0x7a, 0x6b, 0x53, 0xd5, 0x15, 0xa4,
0x43, 0xe9, 0x90, 0x67, 0x58, 0xc0, 0xa5, 0xfa, 0x2a, 0xb1, 0x75, 0x50, 0x39, 0x5c, 0xe6, 0xdc,
0x89, 0xfc, 0xcf, 0xfe, 0xf9, 0x57, 0x54, 0x64, 0xa8, 0xee, 0x23, 0x0b, 0xf1, 0xea, 0xfd, 0xdb,
0xbd, 0x09, 0xb5, 0x5b, 0x05, 0x86, 0x13, 0xf3, 0x24, 0xc5, 0x3f, 0x44, 0x72, 0x7c, 0x7e, 0x36
};
for (int i = 0; i < len; i++) {
arr[i] = lut[arr[i]];
}
}
void decodeCardholderCredentials(uint8_t *eight_bytes, uint8_t *region_code, uint16_t *facility_code, uint32_t *card_number, uint8_t *issue_level) {
uint8_t *arr = eight_bytes;
descramble(arr, 8);
// 4bit region code
*region_code = (arr[3] & 0x1E) >> 1;
// 16bit facility code
*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;
// 4bit issue level
*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) {
// 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[6] = 0;
eight_bytes[7] = (facility_code & 0xf) << 4 | (issue_level & 0xf);
// more obfuscation
scramble(eight_bytes, 8);
}
bool isValidGallagherCredentials(uint64_t region_code, uint64_t facility_code, uint64_t card_number, uint64_t issue_level) {
bool isValid = true;
// validate input
if (region_code > 0x0f) {
PrintAndLogEx(FAILED, "Region code must be 0 <= rc <= 15 (4 bits), received: %d", region_code);
isValid = false;
}
if (facility_code > 0xffff) {
PrintAndLogEx(FAILED, "Facility code must be 0 <= fc <= 65535 (2 bytes), received: %d", facility_code);
isValid = false;
}
if (card_number > 0xffffff) {
PrintAndLogEx(FAILED, "Card number must be 0 <= cn <= 16777215 (3 bytes), received: %d", card_number);
isValid = false;
}
if (issue_level > 0x0f) {
PrintAndLogEx(FAILED, "Issue level must be 0 <= il <= 15 (4 bits), received: %d", issue_level);
isValid = false;
}
return isValid;
}

View file

@ -0,0 +1,22 @@
/**
* Matt Moran (@DarkMatterMatt), 2021
* -----------------------------------------------------------------------------
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
* -----------------------------------------------------------------------------
* Common functionality for low/high-frequency GALLAGHER tag encoding & decoding.
*/
#ifndef MIFARE_GALLAGHERCORE_H__
#define MIFARE_GALLAGHERCORE_H__
#include "common.h"
void encodeCardholderCredentials(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, uint8_t *region_code, uint16_t *facility_code, uint32_t *card_number, uint8_t *issue_level);
bool isValidGallagherCredentials(uint64_t region_code, uint64_t facility_code, uint64_t card_number, uint64_t issue_level);
#endif