diff --git a/client/src/cmdhfgallagher.c b/client/src/cmdhfgallagher.c index 8e089e23c..e8ff0554d 100644 --- a/client/src/cmdhfgallagher.c +++ b/client/src/cmdhfgallagher.c @@ -36,7 +36,7 @@ static const uint8_t DEFAULT_SITE_KEY[] = { * @brief Reverses the bytes in AID. Used when parsing CLI args * (because Proxmark displays AIDs in reverse byte order). */ -static void reverseAid(uint8_t *aid) { +static void reverse_aid(uint8_t *aid) { uint8_t tmp = aid[0]; aid[0] = aid[2]; aid[2] = tmp; @@ -46,7 +46,7 @@ static void reverseAid(uint8_t *aid) { * @brief Converts a Card Application Directory format application ID to an integer. * Note that the CAD stores AIDs in reverse order, so this function is different to DesfireAIDByteToUint(). */ -static uint32_t cadAidByteToUint(uint8_t *data) { +static uint32_t cad_aid_byte_to_uint(uint8_t *data) { return data[2] + (data[1] << 8) + (data[0] << 16); } @@ -54,7 +54,7 @@ static uint32_t cadAidByteToUint(uint8_t *data) { * @brief Converts an integer application ID to Card Application Directory format. * Note that the CAD stores AIDs in reverse order, so this function is different to DesfireAIDUintToByte(). */ -static void cadAidUintToByte(uint32_t aid, uint8_t *data) { +static void cad_aid_uint_to_byte(uint32_t aid, uint8_t *data) { data[2] = aid & 0xff; data[1] = (aid >> 8) & 0xff; data[0] = (aid >> 16) & 0xff; @@ -64,42 +64,42 @@ static void cadAidUintToByte(uint32_t aid, uint8_t *data) { * @brief Returns true if the Card Application Directory entry * is for the specified region & facility, false otherwise. */ -static bool cadFacilityMatch(uint8_t *entry, uint8_t regionCode, uint16_t facilityCode) { - return entry[0] == regionCode && (entry[1] << 8) + entry[2] == facilityCode; +static bool cad_facility_match(uint8_t *entry, uint8_t region_code, uint16_t facility_code) { + return entry[0] == region_code && (entry[1] << 8) + entry[2] == facility_code; } /** * @brief Create Gallagher Application Master Key by diversifying * the MIFARE Site Key with card UID, key number, and application ID. * - * @param sitekey MIFARE Site Key (16 bytes). + * @param site_key MIFARE Site Key (16 bytes). * @param uid Card unique ID (4 or 7 bytes). - * @param uidLen Length of UID. - * @param keyNum Key number (0 <= keyNum <= 2). - * @param aid Application ID (0x2?81F4 where 0 <= ? <= B). - * @param keyOut Buffer to copy the diversified key into (must be 16 bytes). + * @param uid_len Length of UID. + * @param key_num Key number (0 <= key_num <= 2). + * @param aid Application ID (0x2?81F4 where 0 <= ? <= 0xB). + * @param key_output Buffer to copy the diversified key into (must be 16 bytes). * @return PM3_SUCCESS if successful, PM3_EINVARG if an argument is invalid. */ -int GallagherDiversifyKey(uint8_t *sitekey, uint8_t *uid, uint8_t uidLen, - uint8_t keyNo, uint32_t aid, uint8_t *keyOut) { +int hfgal_diversify_key(uint8_t *site_key, uint8_t *uid, uint8_t uid_len, + uint8_t key_num, uint32_t aid, uint8_t *key_output) { // Generate diversification input - uint8_t kdfInputLen = 11; - int res = mfdes_kdf_input_gallagher(uid, uidLen, keyNo, aid, keyOut, &kdfInputLen); + uint8_t kdf_input_len = 11; + int res = mfdes_kdf_input_gallagher(uid, uid_len, key_num, aid, key_output, &kdf_input_len); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed generating Gallagher key diversification input"); - if (sitekey == NULL) { - PrintAndLogEx(INFO, "GallagherDiversifyKey is using default site key: %s", + if (site_key == NULL) { + PrintAndLogEx(INFO, "hfgal_diversify_key is using default site key: %s", sprint_hex_inrow(DEFAULT_SITE_KEY, ARRAYLEN(DEFAULT_SITE_KEY))); - sitekey = (uint8_t *) &DEFAULT_SITE_KEY; + site_key = (uint8_t *) &DEFAULT_SITE_KEY; } // Make temporary DesfireContext DesfireContext_t dctx = {0}; - DesfireSetKey(&dctx, 0, T_AES, sitekey); + DesfireSetKey(&dctx, 0, T_AES, site_key); // Diversify input & copy to output buffer - MifareKdfAn10922(&dctx, DCOMasterKey, keyOut, kdfInputLen); - memcpy(keyOut, dctx.key, CRYPTO_AES128_KEY_SIZE); + MifareKdfAn10922(&dctx, DCOMasterKey, key_output, kdf_input_len); + memcpy(key_output, dctx.key, CRYPTO_AES128_KEY_SIZE); return PM3_SUCCESS; } @@ -107,7 +107,7 @@ int GallagherDiversifyKey(uint8_t *sitekey, uint8_t *uid, uint8_t uidLen, /** * @brief Select application ID. */ -static int selectAid(DesfireContext_t *ctx, uint32_t aid, bool verbose) { +static int select_aid(DesfireContext_t *ctx, uint32_t aid, bool verbose) { // TODO: do these both need to be set? DesfireSetCommMode(ctx, DCMPlain); DesfireSetCommandSet(ctx, DCCNativeISO); @@ -125,7 +125,7 @@ static int selectAid(DesfireContext_t *ctx, uint32_t aid, bool verbose) { } /** - * @brief Authenticate to application. + * @brief Authenticate to application. Uses existing authentication keys in context. */ static int authenticate(DesfireContext_t *ctx, bool verbose) { // TODO: do these both need to be set? @@ -153,8 +153,8 @@ static int authenticate(DesfireContext_t *ctx, bool verbose) { * @brief Select application ID & authenticate. * Uses existing authentication keys in context. */ -static int selectAidAndAuthenticate(DesfireContext_t *ctx, uint32_t aid, bool verbose) { - int res = selectAid(ctx, aid, verbose); +static int select_aid_and_authenticate(DesfireContext_t *ctx, uint32_t aid, bool verbose) { + int res = select_aid(ctx, aid, verbose); HFGAL_RET_IF_ERR(res); res = authenticate(ctx, verbose); @@ -166,7 +166,7 @@ static int selectAidAndAuthenticate(DesfireContext_t *ctx, uint32_t aid, bool ve /** * @brief Returns true if the specified application exists, false otherwise. */ -static bool aidExists(DesfireContext_t *ctx, uint32_t aid, bool verbose) { +static bool aid_exists(DesfireContext_t *ctx, uint32_t aid, bool verbose) { // TODO: do these both need to be set? DesfireSetCommMode(ctx, DCMPlain); DesfireSetCommandSet(ctx, DCCNativeISO); @@ -185,10 +185,10 @@ static bool aidExists(DesfireContext_t *ctx, uint32_t aid, bool verbose) { * @brief Returns the lowest available Gallagher application ID. * @return 0 if no AID is available, or an AID in the range 0x2?81F4, where 0 <= ? <= 0xB. */ -static uint32_t findAvailableGallagherAid(DesfireContext_t *ctx, bool verbose) { +static uint32_t find_available_gallagher_aid(DesfireContext_t *ctx, bool verbose) { for (uint8_t i = 0x0; i <= 0xB; i++) { uint32_t aid = 0x2081F4 | (i << 16); - if (!aidExists(ctx, aid, verbose)) + if (!aid_exists(ctx, aid, verbose)) return aid; } return 0; @@ -197,44 +197,46 @@ static uint32_t findAvailableGallagherAid(DesfireContext_t *ctx, bool verbose) { /** * @brief Read Gallagher Card Application Directory from card. * - * @param destBuf Buffer to copy Card Application Directory into. - * @param destBufLen Size of destBuf. Must be at least 108 bytes. - * @param numEntries Will be set to the number of entries in the Card Application Directory. + * @param dest_buf Buffer to copy Card Application Directory into. + * @param dest_buf_len Size of dest_buf. Must be at least 108 bytes. + * @param num_entries Will be set to the number of entries in the Card Application Directory. */ -static int readCardApplicationDirectory(DesfireContext_t *ctx, uint8_t *destBuf, uint8_t destBufLen, uint8_t *numEntries, bool verbose) { - if (destBufLen < 3 * 36) { - PrintAndLogEx(ERR, "readCardApplicationDirectory destination buffer is incorrectly sized. " - "Received length %d, must be at least %d", destBufLen, 3 * 36); +static int hfgal_read_cad(DesfireContext_t *ctx, uint8_t *dest_buf, + uint8_t dest_buf_len, uint8_t *num_entries, bool verbose) { + if (dest_buf_len < 3 * 36) { + PrintAndLogEx(ERR, "hfgal_read_cad destination buffer is incorrectly sized. " + "Received length %d, must be at least %d", dest_buf_len, 3 * 36); return PM3_EINVARG; } // Get card AIDs from Card Application Directory (which contains 1 to 3 files) - int res = selectAid(ctx, CAD_AID, verbose); + int res = select_aid(ctx, CAD_AID, verbose); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed selecting Card Application Directory, does AID %06X exist?", CAD_AID); // Read up to 3 files with 6x 6-byte entries each for (uint8_t i = 0; i < 3; i++) { - size_t readLen; - res = DesfireReadFile(ctx, i, 0, 36, &destBuf[i * 36], &readLen); + size_t read_len; + res = DesfireReadFile(ctx, i, 0, 36, &dest_buf[i * 36], &read_len); if (res != PM3_SUCCESS && res != PM3_EAPDU_FAIL) HFGAL_RET_ERR(res, "Failed reading file %d in Card Application Directory (AID %06X)", i, CAD_AID); // end if the last entry is NULL - if (memcmp(&destBuf[36 * i + 30], "\0\0\0\0\0\0", 6) == 0) break; + if (memcmp(&dest_buf[36 * i + 30], "\0\0\0\0\0\0", 6) == 0) break; } // Count number of entries (i.e. count until we hit a NULL entry) - *numEntries = 0; - for (uint8_t i = 0; i < destBufLen; i += 6) { - if (memcmp(&destBuf[i], "\0\0\0\0\0\0", 6) == 0) break; - *numEntries += 1; + *num_entries = 0; + for (uint8_t i = 0; i < dest_buf_len; i += 6) { + if (memcmp(&dest_buf[i], "\0\0\0\0\0\0", 6) == 0) break; + *num_entries += 1; } if (verbose) { // Print what we found PrintAndLogEx(SUCCESS, "Card Application Directory contains:" NOLF); - for (int i = 0; i < *numEntries; i++) - PrintAndLogEx(NORMAL, "%s %06X" NOLF, (i == 0) ? "" : ",", cadAidByteToUint(&destBuf[i * 6 + 3])); + for (int i = 0; i < *num_entries; i++) + PrintAndLogEx(NORMAL, "%s %06X" NOLF, (i == 0) ? "" : ",", + cad_aid_byte_to_uint(&dest_buf[i * 6 + 3])); PrintAndLogEx(NORMAL, ""); } @@ -245,40 +247,39 @@ static int readCardApplicationDirectory(DesfireContext_t *ctx, uint8_t *destBuf, * @brief Read credentials from a single AID. * * @param aid Application ID to read. - * @param sitekey MIFARE site key. + * @param site_key MIFARE site key. * @param creds Decoded credentials will be stored in this structure. */ -static int readCardApplicationCredentials(DesfireContext_t *ctx, uint32_t aid, uint8_t *sitekey, GallagherCredentials_t *creds, bool verbose) { +static int hfgal_read_app_creds(DesfireContext_t *ctx, uint32_t aid, uint8_t *site_key, + GallagherCredentials_t *creds, bool verbose) { // Check that card UID has been set if (ctx->uidlen == 0) HFGAL_RET_ERR(PM3_EINVARG, "Card UID must be set in DesfireContext (required for key diversification)"); // Select application & authenticate - DesfireSetKeyNoClear(ctx, 2, T_AES, sitekey); + DesfireSetKeyNoClear(ctx, 2, T_AES, site_key); DesfireSetKdf(ctx, MFDES_KDF_ALGO_GALLAGHER, NULL, 0); - int res = selectAidAndAuthenticate(ctx, aid, verbose); + int res = select_aid_and_authenticate(ctx, aid, verbose); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed selecting/authenticating to AID %06X", aid); // Read file 0 (contains credentials) uint8_t buf[16] = {0}; - size_t readLen = 0; + size_t read_len = 0; DesfireSetCommMode(ctx, DCMEncrypted); - res = DesfireReadFile(ctx, 0, 0, 16, buf, &readLen); + res = DesfireReadFile(ctx, 0, 0, 16, buf, &read_len); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed reading file 0 in AID %06X", aid); // Check file contained 16 bytes of data - if (readLen != 16) { - HFGAL_RET_ERR(PM3_EFAILED, "Failed reading file 0 in AID %06X, expected 16 bytes but received %d bytes", aid, readLen); - } + if (read_len != 16) + HFGAL_RET_ERR(PM3_EFAILED, "Failed reading file 0 in AID %06X, expected 16 bytes but received %d bytes", aid, read_len); // Check second half of file is the bitwise inverse of the first half for (uint8_t i = 8; i < 16; i++) buf[i] ^= 0xFF; - if (memcmp(buf, &buf[8], 8) != 0) { + if (memcmp(buf, &buf[8], 8) != 0) HFGAL_RET_ERR(PM3_EFAILED, "Invalid cardholder data in file 0 in AID %06X. Received %s", sprint_hex_inrow(buf, 16)); - } - decodeCardholderCredentials(buf, creds); + gallagher_decode_creds(buf, creds); // TODO: read MIFARE Enhanced Security file // https://github.com/megabug/gallagher-research/blob/master/formats/mes.md @@ -290,10 +291,10 @@ static int readCardApplicationCredentials(DesfireContext_t *ctx, uint32_t aid, u * @brief Read credentials from a Gallagher card. * * @param aid Application ID to read. If 0, then the Card Application Directory will be queried and all entries will be read. - * @param sitekey MIFARE site key. + * @param site_key MIFARE site key. * @param quiet Suppress error messages. Used when in continuous reader mode. */ -static int readCard(uint32_t aid, uint8_t *sitekey, bool verbose, bool quiet) { +static int hfgal_read_card(uint32_t aid, uint8_t *site_key, bool verbose, bool quiet) { DropField(); clearCommandBuffer(); @@ -307,42 +308,42 @@ static int readCard(uint32_t aid, uint8_t *sitekey, bool verbose, bool quiet) { // Find AIDs to process (from CLI args or the Card Application Directory) uint8_t cad[36 * 3] = {0}; - uint8_t numEntries = 0; + uint8_t num_entries = 0; if (aid != 0) { - cadAidUintToByte(aid, &cad[3]); - numEntries = 1; + cad_aid_uint_to_byte(aid, &cad[3]); + num_entries = 1; } else { - res = readCardApplicationDirectory(&dctx, cad, ARRAYLEN(cad), &numEntries, verbose); + res = hfgal_read_cad(&dctx, cad, ARRAYLEN(cad), &num_entries, verbose); HFGAL_RET_IF_ERR_MAYBE_MSG(res, !quiet, "Failed reading Card Application Directory"); } // Loop through each application in the CAD - for (uint8_t i = 0; i < numEntries * 6; i += 6) { - uint16_t regionCode = cad[i + 0]; - uint16_t facilityCode = (cad[i + 1] << 8) + cad[i + 2]; - uint32_t currentAid = cadAidByteToUint(&cad[i + 3]); + for (uint8_t i = 0; i < num_entries * 6; i += 6) { + uint16_t region_code = cad[i + 0]; + uint16_t facility_code = (cad[i + 1] << 8) + cad[i + 2]; + uint32_t current_aid = cad_aid_byte_to_uint(&cad[i + 3]); if (verbose) { - if (regionCode > 0 || facilityCode > 0) - PrintAndLogEx(INFO, "Reading AID: %06X, region: %u, facility: %u", currentAid, regionCode, facilityCode); + if (region_code > 0 || facility_code > 0) + PrintAndLogEx(INFO, "Reading AID: %06X, region: %u, facility: %u", current_aid, region_code, facility_code); else - PrintAndLogEx(INFO, "Reading AID: %06X", currentAid); + PrintAndLogEx(INFO, "Reading AID: %06X", current_aid); } // Read & decode credentials GallagherCredentials_t creds = {0}; - res = readCardApplicationCredentials(&dctx, currentAid, sitekey, &creds, verbose); + res = hfgal_read_app_creds(&dctx, current_aid, site_key, &creds, verbose); HFGAL_RET_IF_ERR_MAYBE_MSG(res, !quiet, "Failed reading card application credentials"); PrintAndLogEx(SUCCESS, "GALLAGHER (AID %06X) - Region: " _GREEN_("%u") ", Facility: " _GREEN_("%u") - ", Card No.: " _GREEN_("%u") ", Issue Level: " _GREEN_("%u"), currentAid, + ", Card No.: " _GREEN_("%u") ", Issue Level: " _GREEN_("%u"), current_aid, creds.region_code, creds.facility_code, creds.card_number, creds.issue_level); } return PM3_SUCCESS; } -static int CmdGallagherReader(const char *Cmd) { +static int CmdGallagherReader(const char *cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf gallagher reader", "Read a GALLAGHER tag", @@ -360,51 +361,52 @@ static int CmdGallagherReader(const char *Cmd) { arg_lit0("@", "continuous", "Continuous reader mode"), arg_param_end }; - CLIExecWithReturn(ctx, Cmd, argtable, true); + CLIExecWithReturn(ctx, cmd, argtable, true); - int aidLen = 0; - uint8_t aidBuf[3] = {0}; - CLIGetHexWithReturn(ctx, 1, aidBuf, &aidLen); - if (aidLen > 0 && aidLen != 3) + int aid_len = 0; + uint8_t aid_buf[3] = {0}; + CLIGetHexWithReturn(ctx, 1, aid_buf, &aid_len); + if (aid_len > 0 && aid_len != 3) HFGAL_RET_ERR(PM3_EINVARG, "--aid must be 3 bytes"); - reverseAid(aidBuf); // PM3 displays AIDs backwards - uint32_t aid = DesfireAIDByteToUint(aidBuf); + reverse_aid(aid_buf); // PM3 displays AIDs backwards + uint32_t aid = DesfireAIDByteToUint(aid_buf); - int sitekeyLen = 0; - uint8_t sitekey[16] = {0}; - memcpy(sitekey, DEFAULT_SITE_KEY, ARRAYLEN(sitekey)); - CLIGetHexWithReturn(ctx, 2, sitekey, &sitekeyLen); - if (sitekeyLen > 0 && sitekeyLen != 16) + int site_key_len = 0; + uint8_t site_key[16] = {0}; + memcpy(site_key, DEFAULT_SITE_KEY, ARRAYLEN(site_key)); + CLIGetHexWithReturn(ctx, 2, site_key, &site_key_len); + if (site_key_len > 0 && site_key_len != 16) HFGAL_RET_ERR(PM3_EINVARG, "--sitekey must be 16 bytes"); SetAPDULogging(arg_get_lit(ctx, 3)); bool verbose = arg_get_lit(ctx, 4); - bool continuousMode = arg_get_lit(ctx, 5); + bool continuous_mode = arg_get_lit(ctx, 5); CLIParserFree(ctx); - if (!continuousMode) + if (!continuous_mode) // Read single card - return readCard(aid, sitekey, verbose, false); + return hfgal_read_card(aid, site_key, verbose, false); // Loop until is pressed PrintAndLogEx(INFO, "Press " _GREEN_("") " to exit"); while (!kbd_enter_pressed()) - readCard(aid, sitekey, verbose, !verbose); + hfgal_read_card(aid, site_key, verbose, !verbose); return PM3_SUCCESS; } /** * @brief Delete the CAD or an application that contains cardholder credentials. * - * @param sitekey MIFARE site key. + * @param site_key MIFARE site key. * @param aid Application ID to remove. */ -static int deleteGallagherApplication(DesfireContext_t *ctx, uint8_t *sitekey, uint32_t aid, bool verbose) { +static int hfgal_delete_app(DesfireContext_t *ctx, uint8_t *site_key, + uint32_t aid, bool verbose) { // Select application & authenticate - DesfireSetKeyNoClear(ctx, 0, T_AES, sitekey); + DesfireSetKeyNoClear(ctx, 0, T_AES, site_key); DesfireSetKdf(ctx, MFDES_KDF_ALGO_GALLAGHER, NULL, 0); - int res = selectAidAndAuthenticate(ctx, aid, verbose); + int res = select_aid_and_authenticate(ctx, aid, verbose); HFGAL_RET_IF_ERR(res); // Delete application @@ -419,23 +421,23 @@ static int deleteGallagherApplication(DesfireContext_t *ctx, uint8_t *sitekey, u /** * @brief Create a new application to store Gallagher cardholder credentials. * - * @param sitekey MIFARE site key. + * @param site_key MIFARE site key. * @param aid New application ID. Should be 0x2?81F4, where 0 <= ? <= 0xB. */ -static int createGallagherCredentialsApplication(DesfireContext_t *ctx, uint8_t *sitekey, uint32_t aid, bool verbose) { +static int hfgal_create_creds_app(DesfireContext_t *ctx, uint8_t *site_key, uint32_t aid, bool verbose) { // Select application & authenticate - int res = selectAidAndAuthenticate(ctx, 0x000000, verbose); + int res = select_aid_and_authenticate(ctx, 0x000000, verbose); HFGAL_RET_IF_ERR(res); // UID is required for key diversification if (ctx->uidlen == 0) - HFGAL_RET_ERR(PM3_EINVARG, "UID is required for key diversification. Please fetch it before calling `createGallagherCredentialsApplication`"); + HFGAL_RET_ERR(PM3_EINVARG, "UID is required for key diversification. Please fetch it before calling `hfgal_create_creds_app`"); // Create application - DesfireCryptoAlgorithm dstalgo = T_AES; - uint8_t keycount = 3; + DesfireCryptoAlgorithm app_algo = T_AES; + uint8_t num_keys = 3; uint8_t ks1 = 0x0B; - uint8_t ks2 = (DesfireKeyAlgoToType(dstalgo) << 6) | keycount;; + uint8_t ks2 = (DesfireKeyAlgoToType(app_algo) << 6) | num_keys;; uint8_t data[5] = {0}; DesfireAIDUintToByte(aid, &data[0]); @@ -450,28 +452,28 @@ static int createGallagherCredentialsApplication(DesfireContext_t *ctx, uint8_t PrintAndLogEx(INFO, "Created application %06X (currently has empty contents & blank keys)", aid); // Select the new application - res = selectAid(ctx, aid, verbose); + res = select_aid(ctx, aid, verbose); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed selecting application %06X", aid); // Add key 2, then key 0 (we must authenticate with key 0 in order to make changes) for (int i = 2; i >= 0; i -= 2) { // Diversify key uint8_t buf[CRYPTO_AES128_KEY_SIZE] = {0}; - res = GallagherDiversifyKey(sitekey, ctx->uid, ctx->uidlen, i, aid, buf); + res = hfgal_diversify_key(site_key, ctx->uid, ctx->uidlen, i, aid, buf); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed diversifying key %d for AID %06X", i, aid); PrintAndLogEx(INFO, "Diversified key %d for AID %06X: " _GREEN_("%s"), i, aid, sprint_hex_inrow(buf, ARRAYLEN(buf))); // Authenticate - uint8_t blankKey[CRYPTO_AES128_KEY_SIZE] = {0}; - DesfireSetKeyNoClear(ctx, 0, T_AES, blankKey); + uint8_t blank_key[CRYPTO_AES128_KEY_SIZE] = {0}; + DesfireSetKeyNoClear(ctx, 0, T_AES, blank_key); DesfireSetKdf(ctx, MFDES_KDF_ALGO_NONE, NULL, 0); res = authenticate(ctx, verbose); HFGAL_RET_IF_ERR_WITH_MSG(res, "Desfire authenticate error. Result: [%d] %s", res, DesfireAuthErrorToStr(res)); // Change key DesfireSetCommMode(ctx, DCMEncryptedPlain); - res = DesfireChangeKey(ctx, false, i, dstalgo, 1, buf, dstalgo, blankKey, verbose); + res = DesfireChangeKey(ctx, false, i, app_algo, 1, buf, app_algo, blank_key, verbose); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed setting key %d for AID %06X", i, aid); if (verbose) @@ -485,33 +487,34 @@ static int createGallagherCredentialsApplication(DesfireContext_t *ctx, uint8_t /** * @brief Create a new file containing Gallagher cardholder credentials. * - * @param sitekey MIFARE site key. + * @param site_key MIFARE site key. * @param aid Application ID to put the new file in. * @param creds Gallagher cardholder credentials. */ -static int createGallagherCredentialsFile(DesfireContext_t *ctx, uint8_t *sitekey, uint32_t aid, GallagherCredentials_t *creds, bool verbose) { +static int hfgal_create_creds_file(DesfireContext_t *ctx, uint8_t *site_key, uint32_t aid, + GallagherCredentials_t *creds, bool verbose) { // Select application & authenticate - DesfireSetKeyNoClear(ctx, 0, T_AES, sitekey); + DesfireSetKeyNoClear(ctx, 0, T_AES, site_key); DesfireSetKdf(ctx, MFDES_KDF_ALGO_GALLAGHER, NULL, 0); - int res = selectAidAndAuthenticate(ctx, aid, verbose); + int res = select_aid_and_authenticate(ctx, aid, verbose); HFGAL_RET_IF_ERR(res); // Prepare create file command - uint8_t fileType = 0; // standard data file - uint8_t fileId = 0x00; - uint8_t fileSize = 16; - uint8_t fileAccessMode = 0x03; // encrypted - uint32_t fileRights = 0x2000; // key 0 has God mode, key 2 can read + uint8_t file_type = 0; // standard data file + uint8_t file_id = 0x00; + uint8_t file_size = 16; + uint8_t file_access_mode = 0x03; // encrypted + uint32_t file_rights = 0x2000; // key 0 has God mode, key 2 can read uint8_t data[7] = {0}; - data[0] = fileId; - data[1] = fileAccessMode; - data[2] = fileRights & 0xff; - data[3] = (fileRights >> 8) & 0xff; - Uint3byteToMemLe(&data[4], fileSize); + data[0] = file_id; + data[1] = file_access_mode; + data[2] = file_rights & 0xff; + data[3] = (file_rights >> 8) & 0xff; + Uint3byteToMemLe(&data[4], file_size); // Create file - res = DesfireCreateFile(ctx, fileType, data, ARRAYLEN(data), false); + res = DesfireCreateFile(ctx, file_type, data, ARRAYLEN(data), false); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed creating file 0 in AID %06X", aid); if (verbose) @@ -519,13 +522,13 @@ static int createGallagherCredentialsFile(DesfireContext_t *ctx, uint8_t *siteke // Create file contents (2nd half is the bitwise inverse of the encoded creds) uint8_t contents[16] = {0}; - encodeCardholderCredentials(contents, creds); + gallagher_encode_creds(contents, creds); for (int i = 0; i < 8; i++) contents[i + 8] = contents[i] ^ 0xFF; // Write file DesfireSetCommMode(ctx, DCMEncrypted); - res = DesfireWriteFile(ctx, fileId, 0, ARRAYLEN(contents), contents); + res = DesfireWriteFile(ctx, file_id, 0, ARRAYLEN(contents), contents); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed writing data to file 0 in AID %06X"); PrintAndLogEx(INFO, "Successfully wrote cardholder credentials to file 0 in AID %06X", aid); @@ -535,22 +538,22 @@ static int createGallagherCredentialsFile(DesfireContext_t *ctx, uint8_t *siteke /** * @brief Create the Gallagher Card Application Directory. * - * @param sitekey MIFARE site key. + * @param site_key MIFARE site key. */ -static int createGallagherCAD(DesfireContext_t *ctx, uint8_t *sitekey, bool verbose) { +static int hfgal_create_cad(DesfireContext_t *ctx, uint8_t *site_key, bool verbose) { // Check that card UID has been set if (ctx->uidlen == 0) HFGAL_RET_ERR(PM3_EINVARG, "Card UID must be set in DesfireContext (required for key diversification)"); // Select application & authenticate - int res = selectAidAndAuthenticate(ctx, 0x000000, verbose); + int res = select_aid_and_authenticate(ctx, 0x000000, verbose); HFGAL_RET_IF_ERR(res); // Create application - DesfireCryptoAlgorithm dstalgo = T_AES; - uint8_t keycount = 1; + DesfireCryptoAlgorithm app_algo = T_AES; + uint8_t num_keys = 1; uint8_t ks1 = 0x0B; - uint8_t ks2 = (DesfireKeyAlgoToType(dstalgo) << 6) | keycount;; + uint8_t ks2 = (DesfireKeyAlgoToType(app_algo) << 6) | num_keys;; uint8_t data[5] = {0}; DesfireAIDUintToByte(CAD_AID, &data[0]); @@ -565,22 +568,22 @@ static int createGallagherCAD(DesfireContext_t *ctx, uint8_t *sitekey, bool verb PrintAndLogEx(INFO, "Created Card Application Directory (AID %06X, currently has empty contents & blank keys)", CAD_AID); // Select application & authenticate - uint8_t blankKey[DESFIRE_MAX_KEY_SIZE] = {0}; - DesfireSetKeyNoClear(ctx, 0, T_AES, blankKey); + uint8_t blank_key[DESFIRE_MAX_KEY_SIZE] = {0}; + DesfireSetKeyNoClear(ctx, 0, T_AES, blank_key); DesfireSetKdf(ctx, MFDES_KDF_ALGO_NONE, NULL, 0); - res = selectAidAndAuthenticate(ctx, CAD_AID, verbose); + res = select_aid_and_authenticate(ctx, CAD_AID, verbose); HFGAL_RET_IF_ERR(res); // Diversify key uint8_t buf[CRYPTO_AES128_KEY_SIZE] = {0}; - res = GallagherDiversifyKey(sitekey, ctx->uid, ctx->uidlen, 0, CAD_AID, buf); + res = hfgal_diversify_key(site_key, ctx->uid, ctx->uidlen, 0, CAD_AID, buf); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed diversifying key 0 for AID %06X", CAD_AID); PrintAndLogEx(INFO, "Diversified key 0 for CAD (AID %06X): " _GREEN_("%s"), CAD_AID, sprint_hex_inrow(buf, ARRAYLEN(buf))); // Change key DesfireSetCommMode(ctx, DCMEncryptedPlain); - res = DesfireChangeKey(ctx, false, 0, dstalgo, 1, buf, dstalgo, blankKey, verbose); + res = DesfireChangeKey(ctx, false, 0, app_algo, 1, buf, app_algo, blank_key, verbose); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed setting key 0 for CAD"); if (verbose) @@ -593,91 +596,91 @@ static int createGallagherCAD(DesfireContext_t *ctx, uint8_t *sitekey, bool verb /** * @brief Update the Gallagher Card Application Directory with a new entry. * - * @param sitekey MIFARE site key. + * @param site_key MIFARE site key. * @param aid Application ID to add to the CAD. * @param creds Gallagher cardholder credentials (region_code & facility_code are required). */ -static int addToGallagherCAD(DesfireContext_t *ctx, uint8_t *sitekey, uint32_t aid, GallagherCredentials_t *creds, bool verbose) { +static int hfgal_add_aid_to_cad(DesfireContext_t *ctx, uint8_t *site_key, uint32_t aid, + GallagherCredentials_t *creds, bool verbose) { // Check if CAD exists uint8_t cad[36 * 3] = {0}; - uint8_t numEntries = 0; - if (aidExists(ctx, CAD_AID, false)) { + uint8_t num_entries = 0; + if (aid_exists(ctx, CAD_AID, false)) { if (verbose) PrintAndLogEx(INFO, "Card Application Directory exists, reading entries..."); - int res = readCardApplicationDirectory( - ctx, cad, ARRAYLEN(cad), &numEntries, verbose); + int res = hfgal_read_cad(ctx, cad, ARRAYLEN(cad), &num_entries, verbose); HFGAL_RET_IF_ERR(res); // Check that there is space for the new entry - if (numEntries >= 18) + if (num_entries >= 18) HFGAL_RET_ERR(PM3_EFATAL, "Card application directory is full"); } else { - // CAD doesn't exist, we need to create it. + // CAD doesn't exist, we need to create it if (verbose) PrintAndLogEx(INFO, "Card Application Directory does not exist, creating it now..."); - int res = createGallagherCAD(ctx, sitekey, verbose); + int res = hfgal_create_cad(ctx, site_key, verbose); HFGAL_RET_IF_ERR(res); } - uint8_t fileId = numEntries / 6; // 6 entries per file - uint8_t entryNum = numEntries % 6; + uint8_t file_id = num_entries / 6; // 6 entries per file + uint8_t entry_num = num_entries % 6; // Check if facility already exists in CAD. for (uint8_t i = 0; i < ARRAYLEN(cad); i += 6) { - if (cadFacilityMatch(&cad[i], creds->region_code, creds->facility_code)) + if (cad_facility_match(&cad[i], creds->region_code, creds->facility_code)) HFGAL_RET_ERR(PM3_EFATAL, "Facility already exists in CAD, delete or " - "update AID %06X instead", cadAidByteToUint(&cad[i + 3])); + "update AID %06X instead", cad_aid_byte_to_uint(&cad[i + 3])); } // Create entry - uint8_t *entry = &cad[numEntries * 6]; + uint8_t *entry = &cad[num_entries * 6]; entry[0] = creds->region_code; entry[1] = (creds->facility_code >> 8) & 0xFF; entry[2] = creds->facility_code & 0xFF; - cadAidUintToByte(aid, &entry[3]); + cad_aid_uint_to_byte(aid, &entry[3]); if (verbose) - PrintAndLogEx(INFO, "Adding entry to CAD (position %d in file %d): %s", entryNum, fileId, sprint_hex_inrow(entry, 6)); + PrintAndLogEx(INFO, "Adding entry to CAD (position %d in file %d): %s", entry_num, file_id, sprint_hex_inrow(entry, 6)); // Select application & authenticate - DesfireSetKeyNoClear(ctx, 0, T_AES, sitekey); + DesfireSetKeyNoClear(ctx, 0, T_AES, site_key); DesfireSetKdf(ctx, MFDES_KDF_ALGO_GALLAGHER, NULL, 0); - int res = selectAidAndAuthenticate(ctx, CAD_AID, verbose); + int res = select_aid_and_authenticate(ctx, CAD_AID, verbose); HFGAL_RET_IF_ERR(res); // Create file if necessary - if (entryNum == 0) { + if (entry_num == 0) { if (verbose) PrintAndLogEx(INFO, "Creating new file in CAD"); // Prepare create file command - uint8_t fileType = 0; // standard data file - uint8_t fileSize = 36; - uint8_t fileAccessMode = 0x00; // plain - uint32_t fileRights = 0xE000; // key 0 has God mode, everyone can read + uint8_t file_type = 0; // standard data file + uint8_t file_size = 36; + uint8_t file_access_mode = 0x00; // plain + uint32_t file_rights = 0xE000; // key 0 has God mode, everyone can read uint8_t data[7] = {0}; - data[0] = fileId; - data[1] = fileAccessMode; - data[2] = fileRights & 0xff; - data[3] = (fileRights >> 8) & 0xff; - Uint3byteToMemLe(&data[4], fileSize); + data[0] = file_id; + data[1] = file_access_mode; + data[2] = file_rights & 0xff; + data[3] = (file_rights >> 8) & 0xff; + Uint3byteToMemLe(&data[4], file_size); // Create file - res = DesfireCreateFile(ctx, fileType, data, ARRAYLEN(data), false); - HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed creating file %d in CAD (AID %06X)", fileId, CAD_AID); + res = DesfireCreateFile(ctx, file_type, data, ARRAYLEN(data), false); + HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed creating file %d in CAD (AID %06X)", file_id, CAD_AID); if (verbose) - PrintAndLogEx(INFO, "Created file %d in CAD (currently has empty contents)", fileId); + PrintAndLogEx(INFO, "Created file %d in CAD (currently has empty contents)", file_id); // Write file - res = DesfireWriteFile(ctx, fileId, 0, 36, &cad[fileId * 36]); + res = DesfireWriteFile(ctx, file_id, 0, 36, &cad[file_id * 36]); } else // Write file - res = DesfireWriteFile(ctx, fileId, entryNum * 6, 6, entry); - HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed writing data to file %d in CAD (AID %06X)", fileId, CAD_AID); + res = DesfireWriteFile(ctx, file_id, entry_num * 6, 6, entry); + HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed writing data to file %d in CAD (AID %06X)", file_id, CAD_AID); PrintAndLogEx(INFO, "Successfully added new entry for %06X to the Card Application Directory", aid); return PM3_SUCCESS; @@ -686,70 +689,71 @@ static int addToGallagherCAD(DesfireContext_t *ctx, uint8_t *sitekey, uint32_t a /** * @brief Remove an entry from the Gallagher Card Application Directory. * - * @param sitekey MIFARE site key. + * @param site_key MIFARE site key. * @param aid Application ID to add to the CAD. */ -static int removeFromGallagherCAD(DesfireContext_t *ctx, uint8_t *sitekey, uint32_t aid, bool verbose) { +static int hfgal_remove_aid_from_cad(DesfireContext_t *ctx, uint8_t *site_key, + uint32_t aid, bool verbose) { // Check if CAD exists uint8_t cad[36 * 3] = {0}; - uint8_t numEntries = 0; + uint8_t num_entries = 0; - int res = readCardApplicationDirectory( - ctx, cad, ARRAYLEN(cad), &numEntries, verbose); + int res = hfgal_read_cad( + ctx, cad, ARRAYLEN(cad), &num_entries, verbose); HFGAL_RET_IF_ERR(res); // Check if facility already exists in CAD - uint8_t entryNum = 0; - for (; entryNum < numEntries; entryNum++) { - if (aid > 0 && aid == cadAidByteToUint(&cad[entryNum * 6 + 3])) + uint8_t entry_num = 0; + for (; entry_num < num_entries; entry_num++) { + if (aid > 0 && aid == cad_aid_byte_to_uint(&cad[entry_num * 6 + 3])) break; } - if (entryNum >= numEntries) + if (entry_num >= num_entries) HFGAL_RET_ERR(PM3_EINVARG, "Specified facility or AID does not exist in the Card Application Directory"); // Remove entry (shift all entries left, then clear the last entry) memmove( - &cad[entryNum * 6], - &cad[(entryNum + 1) * 6], - ARRAYLEN(cad) - (entryNum + 1) * 6 + &cad[entry_num * 6], + &cad[(entry_num + 1) * 6], + ARRAYLEN(cad) - (entry_num + 1) * 6 ); memset(&cad[ARRAYLEN(cad) - 6], 0, 6); // Select application & authenticate - DesfireSetKeyNoClear(ctx, 0, T_AES, sitekey); + DesfireSetKeyNoClear(ctx, 0, T_AES, site_key); DesfireSetKdf(ctx, MFDES_KDF_ALGO_GALLAGHER, NULL, 0); - res = selectAidAndAuthenticate(ctx, CAD_AID, verbose); + res = select_aid_and_authenticate(ctx, CAD_AID, verbose); HFGAL_RET_IF_ERR(res); // Determine what files we need to update - uint8_t fileIdStart = (entryNum - 1) / 6; - uint8_t fileIdStop = (numEntries - 1) / 6; + uint8_t file_id_start = (entry_num - 1) / 6; + uint8_t file_id_stop = (num_entries - 1) / 6; - for (uint8_t fileId = fileIdStart; fileId <= fileIdStop; fileId++) { + for (uint8_t file_id = file_id_start; file_id <= file_id_stop; file_id++) { // Write file - res = DesfireWriteFile(ctx, fileId, 0, 36, &cad[fileId * 36]); - HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed writing data to file %d in CAD (AID %06X)", fileId, CAD_AID); + res = DesfireWriteFile(ctx, file_id, 0, 36, &cad[file_id * 36]); + HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed writing data to file %d in CAD (AID %06X)", file_id, CAD_AID); if (verbose) - PrintAndLogEx(INFO, "Updated file %d in CAD", fileId); + PrintAndLogEx(INFO, "Updated file %d in CAD", file_id); } // Delete empty files if necessary - if (fileIdStart != fileIdStop) { - uint8_t fileId = fileIdStop; + if (file_id_start != file_id_stop) { + uint8_t file_id = file_id_stop; DesfireSetCommMode(ctx, DCMMACed); - res = DesfireDeleteFile(ctx, fileId); - HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed deleting file %d from CAD (AID %06X)", fileId, CAD_AID); + res = DesfireDeleteFile(ctx, file_id); + HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed deleting file %d from CAD (AID %06X)", file_id, CAD_AID); if (verbose) - PrintAndLogEx(INFO, "Deleted unnecessary file %d from CAD (AID %06X)", fileId, CAD_AID); + PrintAndLogEx(INFO, "Deleted unnecessary file %d from CAD (AID %06X)", file_id, CAD_AID); // Delete the Card Application Directory if necessary // (if we just deleted the last file in it) - if (fileId == 0) { - res = deleteGallagherApplication(ctx, sitekey, CAD_AID, verbose); - HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed deleting file %d from CAD (AID %06X)", fileId, CAD_AID); + if (file_id == 0) { + res = hfgal_delete_app(ctx, site_key, CAD_AID, verbose); + HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed deleting file %d from CAD (AID %06X)", file_id, CAD_AID); if (verbose) PrintAndLogEx(INFO, "Removed CAD because it was empty"); @@ -760,7 +764,7 @@ static int removeFromGallagherCAD(DesfireContext_t *ctx, uint8_t *sitekey, uint3 return PM3_SUCCESS; } -static int CmdGallagherClone(const char *Cmd) { +static int CmdGallagherClone(const char *cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf gallagher clone", "Clone a GALLAGHER card to a blank DESFire card", @@ -783,54 +787,54 @@ static int CmdGallagherClone(const char *Cmd) { arg_str0(NULL, "sitekey", "", "Master site key to compute diversified keys (16 bytes) [default=3112B738D8862CCD34302EB299AAB456]"), arg_param_end }; - CLIExecWithReturn(ctx, Cmd, argtable, false); + CLIExecWithReturn(ctx, cmd, argtable, false); SetAPDULogging(arg_get_lit(ctx, 1)); bool verbose = arg_get_lit(ctx, 2); - int keyNum = arg_get_int_def(ctx, 3, 0); + int key_num = arg_get_int_def(ctx, 3, 0); - int algo = T_DES; - if (CLIGetOptionList(arg_get_str(ctx, 4), DesfireAlgoOpts, &algo)) return PM3_ESOFT; + int key_algo = T_DES; + if (CLIGetOptionList(arg_get_str(ctx, 4), DesfireAlgoOpts, &key_algo)) return PM3_ESOFT; - int keyLen = 0; + int key_len = 0; uint8_t key[DESFIRE_MAX_KEY_SIZE] = {0}; - CLIGetHexWithReturn(ctx, 5, key, &keyLen); - if (keyLen && keyLen != desfire_get_key_length(algo)) - HFGAL_RET_ERR(PM3_EINVARG, "%s key must have %d bytes length instead of %d", CLIGetOptionListStr(DesfireAlgoOpts, algo), desfire_get_key_length(algo), keyLen); - if (keyLen == 0) + CLIGetHexWithReturn(ctx, 5, key, &key_len); + if (key_len && key_len != desfire_get_key_length(key_algo)) + HFGAL_RET_ERR(PM3_EINVARG, "%s key must have %d bytes length instead of %d", CLIGetOptionListStr(DesfireAlgoOpts, key_algo), desfire_get_key_length(key_algo), key_len); + if (key_len == 0) // Default to a key of all zeros - keyLen = desfire_get_key_length(algo); + key_len = desfire_get_key_length(key_algo); uint64_t region_code = arg_get_u64(ctx, 6); // uint4, input will be validated later uint64_t facility_code = arg_get_u64(ctx, 7); // uint16 uint64_t card_number = arg_get_u64(ctx, 8); // uint24 uint64_t issue_level = arg_get_u64(ctx, 9); // uint4 - int aidLen = 0; - uint8_t aidBuf[3] = {0}; + int aid_len = 0; + uint8_t aid_buf[3] = {0}; uint32_t aid = 0; - CLIGetHexWithReturn(ctx, 10, aidBuf, &aidLen); - if (aidLen > 0) { - if (aidLen != 3) + CLIGetHexWithReturn(ctx, 10, aid_buf, &aid_len); + if (aid_len > 0) { + if (aid_len != 3) HFGAL_RET_ERR(PM3_EINVARG, "--aid must be 3 bytes"); - reverseAid(aidBuf); // PM3 displays AIDs backwards - aid = DesfireAIDByteToUint(aidBuf); + reverse_aid(aid_buf); // PM3 displays AIDs backwards + aid = DesfireAIDByteToUint(aid_buf); // Check that the AID is in the expected range - if (memcmp(aidBuf, "\xF4\x81", 2) != 0 || aidBuf[2] < 0x20 || aidBuf[2] > 0x2B) + if (memcmp(aid_buf, "\xF4\x81", 2) != 0 || aid_buf[2] < 0x20 || aid_buf[2] > 0x2B) // TODO: this should probably be a warning, but key diversification will throw an error later even if we don't HFGAL_RET_ERR(PM3_EINVARG, "Invalid Gallagher AID %06X, expected 2?81F4, where 0 <= ? <= 0xB", aid); } - int sitekeyLen = 0; - uint8_t sitekey[16] = {0}; - memcpy(sitekey, DEFAULT_SITE_KEY, ARRAYLEN(sitekey)); - CLIGetHexWithReturn(ctx, 11, sitekey, &sitekeyLen); - if (sitekeyLen > 0 && sitekeyLen != 16) + int site_key_len = 0; + uint8_t site_key[16] = {0}; + memcpy(site_key, DEFAULT_SITE_KEY, ARRAYLEN(site_key)); + CLIGetHexWithReturn(ctx, 11, site_key, &site_key_len); + if (site_key_len > 0 && site_key_len != 16) HFGAL_RET_ERR(PM3_EINVARG, "--sitekey must be 16 bytes"); CLIParserFree(ctx); - if (!isValidGallagherCredentials(region_code, facility_code, card_number, issue_level)) + if (!gallagher_is_valid_creds(region_code, facility_code, card_number, issue_level)) return PM3_EINVARG; GallagherCredentials_t creds = { @@ -850,27 +854,27 @@ static int CmdGallagherClone(const char *Cmd) { HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed retrieving card UID"); // Find available Gallagher AID if the user did not specify one - if (aidLen == 0) { - aid = findAvailableGallagherAid(&dctx, verbose); + if (aid_len == 0) { + aid = find_available_gallagher_aid(&dctx, verbose); if (aid == 0) HFGAL_RET_ERR(PM3_EFATAL, "Could not find an available AID, card is full"); } // Update Card Application Directory - DesfireSetKeyNoClear(&dctx, keyNum, algo, key); + DesfireSetKeyNoClear(&dctx, key_num, key_algo, key); DesfireSetKdf(&dctx, MFDES_KDF_ALGO_NONE, NULL, 0); - res = addToGallagherCAD(&dctx, sitekey, aid, &creds, verbose); + res = hfgal_add_aid_to_cad(&dctx, site_key, aid, &creds, verbose); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed updating Gallagher Card Application Directory"); // Create application - DesfireSetKeyNoClear(&dctx, keyNum, algo, key); + DesfireSetKeyNoClear(&dctx, key_num, key_algo, key); DesfireSetKdf(&dctx, MFDES_KDF_ALGO_NONE, NULL, 0); - res = createGallagherCredentialsApplication(&dctx, sitekey, aid, verbose); + res = hfgal_create_creds_app(&dctx, site_key, aid, verbose); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed creating Gallagher application"); // Create credential files // Don't need to set keys here, they're generated automatically - res = createGallagherCredentialsFile(&dctx, sitekey, aid, &creds, verbose); + res = hfgal_create_creds_file(&dctx, site_key, aid, &creds, verbose); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed creating Gallagher credential file"); PrintAndLogEx(SUCCESS, "Done"); @@ -878,7 +882,7 @@ static int CmdGallagherClone(const char *Cmd) { return PM3_SUCCESS; } -static int CmdGallagherDelete(const char *Cmd) { +static int CmdGallagherDelete(const char *cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf gallagher delete", "Delete Gallagher application from a DESFire card", @@ -894,31 +898,31 @@ static int CmdGallagherDelete(const char *Cmd) { arg_str0(NULL, "sitekey", "", "MIFARE site key to compute diversified keys (16 bytes) [default=3112B738D8862CCD34302EB299AAB456]"), arg_param_end }; - CLIExecWithReturn(ctx, Cmd, argtable, false); + CLIExecWithReturn(ctx, cmd, argtable, false); SetAPDULogging(arg_get_lit(ctx, 1)); bool verbose = arg_get_lit(ctx, 2); - int aidLen = 0; - uint8_t aidBuf[3] = {0}; + int aid_len = 0; + uint8_t aid_buf[3] = {0}; uint32_t aid = 0; - CLIGetHexWithReturn(ctx, 3, aidBuf, &aidLen); + CLIGetHexWithReturn(ctx, 3, aid_buf, &aid_len); - if (aidLen != 3) + if (aid_len != 3) HFGAL_RET_ERR(PM3_EINVARG, "--aid must be 3 bytes"); - reverseAid(aidBuf); // PM3 displays AIDs backwards - aid = DesfireAIDByteToUint(aidBuf); + reverse_aid(aid_buf); // PM3 displays AIDs backwards + aid = DesfireAIDByteToUint(aid_buf); // Check that the AID is in the expected range - if (memcmp(aidBuf, "\xF4\x81", 2) != 0 || aidBuf[2] < 0x20 || aidBuf[2] > 0x2B) + if (memcmp(aid_buf, "\xF4\x81", 2) != 0 || aid_buf[2] < 0x20 || aid_buf[2] > 0x2B) // TODO: this should probably be a warning, but key diversification will throw an error later even if we don't HFGAL_RET_ERR(PM3_EINVARG, "Invalid Gallagher AID %06X, expected 2?81F4, where 0 <= ? <= 0xB", aid); - int sitekeyLen = 0; - uint8_t sitekey[16] = {0}; - memcpy(sitekey, DEFAULT_SITE_KEY, ARRAYLEN(sitekey)); - CLIGetHexWithReturn(ctx, 4, sitekey, &sitekeyLen); - if (sitekeyLen > 0 && sitekeyLen != 16) + int site_key_len = 0; + uint8_t site_key[16] = {0}; + memcpy(site_key, DEFAULT_SITE_KEY, ARRAYLEN(site_key)); + CLIGetHexWithReturn(ctx, 4, site_key, &site_key_len); + if (site_key_len > 0 && site_key_len != 16) HFGAL_RET_ERR(PM3_EINVARG, "--sitekey must be 16 bytes"); CLIParserFree(ctx); @@ -932,11 +936,11 @@ static int CmdGallagherDelete(const char *Cmd) { HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed retrieving card UID"); // Update Card Application Directory - res = removeFromGallagherCAD(&dctx, sitekey, aid, verbose); + res = hfgal_remove_aid_from_cad(&dctx, site_key, aid, verbose); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed removing %06X from the Card Application Directory"); // Delete application - res = deleteGallagherApplication(&dctx, sitekey, aid, verbose); + res = hfgal_delete_app(&dctx, site_key, aid, verbose); HFGAL_RET_IF_ERR_WITH_MSG(res, "Failed deleting Gallagher application"); PrintAndLogEx(SUCCESS, "Done"); @@ -944,23 +948,23 @@ static int CmdGallagherDelete(const char *Cmd) { return PM3_SUCCESS; } -static int CmdHelp(const char *Cmd); +static int CmdHelp(const char *cmd); static command_t CommandTable[] = { {"help", CmdHelp, AlwaysAvailable, "This help"}, - {"reader", CmdGallagherReader, IfPm3Iso14443, "Attempt to read and extract tag data"}, + {"reader", CmdGallagherReader, IfPm3Iso14443, "Read & decode all Gallagher credentials on the DESFire card"}, {"clone", CmdGallagherClone, IfPm3Iso14443, "Add Gallagher credentials to a DESFire card"}, - {"delete", CmdGallagherDelete, IfPm3Iso14443, "Delete Gallagher application from a DESFire card"}, + {"delete", CmdGallagherDelete, IfPm3Iso14443, "Delete Gallagher credentials from a DESFire card"}, {NULL, NULL, NULL, NULL} }; -static int CmdHelp(const char *Cmd) { - (void) Cmd; // Cmd is not used so far +static int CmdHelp(const char *cmd) { + (void) cmd; // cmd is not used so far CmdsHelp(CommandTable); return PM3_SUCCESS; } -int CmdHFGallagher(const char *Cmd) { +int CmdHFGallagher(const char *cmd) { clearCommandBuffer(); - return CmdsParse(CommandTable, Cmd); + return CmdsParse(CommandTable, cmd); } diff --git a/client/src/cmdhfgallagher.h b/client/src/cmdhfgallagher.h index 08d5d2150..3c4024114 100644 --- a/client/src/cmdhfgallagher.h +++ b/client/src/cmdhfgallagher.h @@ -29,7 +29,7 @@ int CmdHFGallagher(const char *Cmd); * @param keyOut Buffer to copy the diversified key into (must be 16 bytes). * @return PM3_SUCCESS if successful, PM3_EINVARG if an argument is invalid. */ -int GallagherDiversifyKey(uint8_t *sitekey, uint8_t *uid, uint8_t uidLen, uint8_t keyNum, uint32_t aid, uint8_t *keyOut); +int hfgal_diversify_key(uint8_t *sitekey, uint8_t *uid, uint8_t uidLen, uint8_t keyNum, uint32_t aid, uint8_t *keyOut); // Return error #define HFGAL_RET_ERR(err, ...) { PrintAndLogEx(ERR, __VA_ARGS__); return err; } diff --git a/client/src/cmdlfgallagher.c b/client/src/cmdlfgallagher.c index b09f93b60..e88175f89 100644 --- a/client/src/cmdlfgallagher.c +++ b/client/src/cmdlfgallagher.c @@ -76,7 +76,7 @@ int demodGallagher(bool verbose) { uint8_t calc_crc = CRC8Cardx(arr, ARRAYLEN(arr)); GallagherCredentials_t creds = {0}; - decodeCardholderCredentials(arr, &creds); + gallagher_decode_creds(arr, &creds); PrintAndLogEx(SUCCESS, "GALLAGHER - Region: " _GREEN_("%u") " Facility: " _GREEN_("%u") " Card No.: " _GREEN_("%u") " Issue Level: " _GREEN_("%u"), creds.region_code, creds.facility_code, creds.card_number, creds.issue_level); @@ -142,7 +142,7 @@ static void setBitsInBlocks(uint32_t *blocks, uint8_t *pos, uint32_t data, uint8 static void createBlocks(uint32_t *blocks, GallagherCredentials_t *creds) { // put data into the correct places (Gallagher obfuscation) uint8_t arr[8] = {0}; - encodeCardholderCredentials(arr, creds); + gallagher_encode_creds(arr, creds); blocks[0] = blocks[1] = blocks[2] = 0; uint8_t pos = 0; @@ -222,7 +222,7 @@ static int CmdGallagherClone(const char *Cmd) { PrintAndLogEx(FAILED, "Can't specify both raw and rc/fc/cn/il at the same time"); return PM3_EINVARG; } - if (!isValidGallagherCredentials(region_code, facility_code, card_number, issue_level)) { + if (!gallagher_is_valid_creds(region_code, facility_code, card_number, issue_level)) { return PM3_EINVARG; } } @@ -322,7 +322,7 @@ static int CmdGallagherSim(const char *Cmd) { PrintAndLogEx(FAILED, "Can't specify both raw and rc/fc/cn/il at the same time"); return PM3_EINVARG; } - if (!isValidGallagherCredentials(region_code, facility_code, card_number, issue_level)) { + if (!gallagher_is_valid_creds(region_code, facility_code, card_number, issue_level)) { return PM3_EINVARG; } } diff --git a/client/src/mifare/gallaghercore.c b/client/src/mifare/gallaghercore.c index fb220eeb1..6bff623f8 100644 --- a/client/src/mifare/gallaghercore.c +++ b/client/src/mifare/gallaghercore.c @@ -62,7 +62,7 @@ static void descramble(uint8_t *arr, uint8_t len) { } } -void decodeCardholderCredentials(uint8_t *eight_bytes, GallagherCredentials_t *creds) { +void gallagher_decode_creds(uint8_t *eight_bytes, GallagherCredentials_t *creds) { uint8_t *arr = eight_bytes; descramble(arr, 8); @@ -80,7 +80,7 @@ void decodeCardholderCredentials(uint8_t *eight_bytes, GallagherCredentials_t *c creds->issue_level = arr[7] & 0x0F; } -void encodeCardholderCredentials(uint8_t *eight_bytes, GallagherCredentials_t *creds) { +void gallagher_encode_creds(uint8_t *eight_bytes, GallagherCredentials_t *creds) { uint8_t rc = creds->region_code; uint16_t fc = creds->facility_code; uint32_t cn = creds->card_number; @@ -100,25 +100,25 @@ void encodeCardholderCredentials(uint8_t *eight_bytes, GallagherCredentials_t *c 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; +bool gallagher_is_valid_creds(uint64_t region_code, uint64_t facility_code, uint64_t card_number, uint64_t issue_level) { + bool is_valid = true; // validate input if (region_code > 0x0f) { PrintAndLogEx(ERR, "Region code must be 0 <= rc <= 15 (4 bits), received: %d", region_code); - isValid = false; + is_valid = false; } if (facility_code > 0xffff) { PrintAndLogEx(ERR, "Facility code must be 0 <= fc <= 65535 (2 bytes), received: %d", facility_code); - isValid = false; + is_valid = false; } if (card_number > 0xffffff) { PrintAndLogEx(ERR, "Card number must be 0 <= cn <= 16777215 (3 bytes), received: %d", card_number); - isValid = false; + is_valid = false; } if (issue_level > 0x0f) { PrintAndLogEx(ERR, "Issue level must be 0 <= il <= 15 (4 bits), received: %d", issue_level); - isValid = false; + is_valid = false; } - return isValid; + return is_valid; } diff --git a/client/src/mifare/gallaghercore.h b/client/src/mifare/gallaghercore.h index 37164f92e..bc9ee0c67 100644 --- a/client/src/mifare/gallaghercore.h +++ b/client/src/mifare/gallaghercore.h @@ -21,10 +21,10 @@ typedef struct { uint8_t issue_level; } GallagherCredentials_t; -void encodeCardholderCredentials(uint8_t *eight_bytes, GallagherCredentials_t *creds); +void gallagher_encode_creds(uint8_t *eight_bytes, GallagherCredentials_t *creds); -void decodeCardholderCredentials(uint8_t *eight_bytes, GallagherCredentials_t *creds); +void gallagher_decode_creds(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); +bool gallagher_is_valid_creds(uint64_t region_code, uint64_t facility_code, uint64_t card_number, uint64_t issue_level); #endif diff --git a/doc/commands.md b/doc/commands.md index 3237a4c9d..ec92dcf2c 100644 --- a/doc/commands.md +++ b/doc/commands.md @@ -324,9 +324,9 @@ Check column "offline" for their availability. |command |offline |description |------- |------- |----------- |`hf gallagher help `|Y |`This help` -|`hf gallagher reader `|N |`Attempt to read and extract tag data` +|`hf gallagher reader `|N |`Read & decode all Gallagher credentials on the DESFire card` |`hf gallagher clone `|N |`Add Gallagher credentials to a DESFire card` -|`hf gallagher delete `|N |`Delete Gallagher application from a DESFire card` +|`hf gallagher delete `|N |`Delete Gallagher credentials from a DESFire card` ### hf ksx6924