diff --git a/armsrc/seos.c b/armsrc/seos.c index 1f01be720..6e2b40798 100644 --- a/armsrc/seos.c +++ b/armsrc/seos.c @@ -64,8 +64,9 @@ static uint8_t round_to_next(uint8_t value, uint8_t step) { } static uint8_t cryptogram_iv[16] = {0x00}; + static bool generate_cryptogram(const uint8_t *key, bool use_iv, const uint8_t *input, size_t length, uint8_t *output, uint8_t algorithm) { - if (!use_iv) { + if (use_iv == false) { memset(cryptogram_iv, 0x00, 16); } @@ -85,6 +86,7 @@ static bool generate_cryptogram(const uint8_t *key, bool use_iv, const uint8_t * } static bool decrypt_cryptogram(const uint8_t *key, const uint8_t *input, size_t length, uint8_t *output, uint8_t algorithm) { + memset(cryptogram_iv, 0x00, 16); if (algorithm == SEOS_ENCRYPTION_AES) { @@ -99,7 +101,7 @@ static bool decrypt_cryptogram(const uint8_t *key, const uint8_t *input, size_t mbedtls_des3_crypt_cbc(&ctx, MBEDTLS_DES_DECRYPT, length, cryptogram_iv, input, output); mbedtls_des3_free(&ctx); } else { - Dbprintf(_RED_("Unknown Encryption Algorithm")); + Dbprintf(_RED_("Unknown encryption algorithm")); return false; } @@ -108,6 +110,7 @@ static bool decrypt_cryptogram(const uint8_t *key, const uint8_t *input, size_t // Returns length of generated CMAC static bool generate_cmac(const uint8_t *key, const uint8_t *input, size_t length, uint8_t *output, uint8_t encryption_algorithm) { + if (encryption_algorithm == SEOS_ENCRYPTION_AES) { ulaes_cmac(key, 16, input, length, output); } else if (encryption_algorithm == SEOS_ENCRYPTION_2K3DES || encryption_algorithm == SEOS_ENCRYPTION_3K3DES) { @@ -115,7 +118,7 @@ static bool generate_cmac(const uint8_t *key, const uint8_t *input, size_t lengt if (encryption_algorithm == SEOS_ENCRYPTION_3K3DES) keylen = 24; des3_cmac(key, keylen, input, length, output); } else { - Dbprintf(_RED_("Unknown Encryption Algorithm")); + Dbprintf(_RED_("Unknown encryption algorithm")); return false; } @@ -146,7 +149,8 @@ static void seos_kdf(bool forEncryption, uint8_t *masterKey, uint8_t keyslot, ui memcpy(work_buffer + 19 + adfoid_len, diversifier, diversifier_len); // This CMAC always uses AES, regardless of the main encryption algorithm in use. - generate_cmac(masterKey, work_buffer, 19 + adfoid_len + diversifier_len, out, SEOS_ENCRYPTION_AES); + bool res = generate_cmac(masterKey, work_buffer, 19 + adfoid_len + diversifier_len, out, SEOS_ENCRYPTION_AES); + (void)res; } // turn off afterwards @@ -160,15 +164,15 @@ void SimulateSeos(seos_emulate_req_t *msg) { // These values are determined at runtime uint8_t RND_ICC[8] = { 0x00 }; - uint8_t RND_IFD[8]; + uint8_t RND_IFD[8] = { 0x00 }; uint8_t KEY_ICC[16] = { 0x00 }; - uint8_t KEY_IFD[16]; - uint8_t diver_encr_key[16]; - uint8_t diver_cmac_key[16]; + uint8_t KEY_IFD[16] = { 0x00 }; + uint8_t diver_encr_key[16] = { 0x00 }; + uint8_t diver_cmac_key[16] = { 0x00 }; // Calculated block size const uint8_t bs = block_size(msg->encr_alg); - const uint8_t half_bs = bs >> 1; + const uint8_t half_bs = (bs >> 1); if (bs == 0) { // Can't continue, invalid encryption algorithm reply_ng(CMD_HF_SEOS_SIMULATE, PM3_EINVARG, NULL, 0); @@ -189,12 +193,14 @@ void SimulateSeos(seos_emulate_req_t *msg) { reply_ng(CMD_HF_MIFARE_SIMULATE, PM3_EMALLOC, NULL, 0); return; } + uint8_t *dynamic_modulation_buffer = BigBuf_calloc(DYNAMIC_MODULATION_BUFFER_SIZE); if (dynamic_modulation_buffer == NULL) { BigBuf_free_keep_EM(); reply_ng(CMD_HF_MIFARE_SIMULATE, PM3_EMALLOC, NULL, 0); return; } + tag_response_info_t dynamic_response_info = { .response = dynamic_response_buffer, .response_n = 0, @@ -210,6 +216,7 @@ void SimulateSeos(seos_emulate_req_t *msg) { reply_ng(CMD_HF_MIFARE_SIMULATE, PM3_EMALLOC, NULL, 0); return; } + uint8_t *work_buffer_b = BigBuf_calloc(WORK_BUFFER_SIZE); if (work_buffer_b == NULL) { BigBuf_free_keep_EM(); diff --git a/client/src/cmdhfjooki.c b/client/src/cmdhfjooki.c index 04ef9b94f..c7a3db1ee 100644 --- a/client/src/cmdhfjooki.c +++ b/client/src/cmdhfjooki.c @@ -172,10 +172,11 @@ static int jooki_decode(uint8_t *b64, uint8_t *result) { PrintAndLogEx(DEBUG, "(decode_jooki) raw encoded... " _GREEN_("%s"), sprint_hex(ndef, sizeof(ndef))); for (uint8_t i = 0; i < JOOKI_PLAIN_LEN; i++) { - if (i < 3) + if (i < 3) { result[i] = ndef[i] ^ nfc_secret[i]; - else + } else { result[i] = ndef[i] ^ nfc_secret[i] ^ ndef[i % 3] ^ nfc_secret[i % 3]; + } } PrintAndLogEx(DEBUG, "(decode_jooki) plain......... %s", sprint_hex(result, sizeof(ndef))); return PM3_SUCCESS; @@ -239,9 +240,12 @@ static void jooki_print(uint8_t *b64, uint8_t *result, bool verbose) { static int jooki_selftest(void) { PrintAndLogEx(INFO, "======== " _CYAN_("self test") " ==========================================="); + for (int i = 0; i < ARRAYLEN(jooks); i++) { - if (strlen(jooks[i].b64) == 0) + + if (strlen(jooks[i].b64) == 0) { continue; + } uint8_t iv[JOOKI_IV_LEN] = {0}; uint8_t uid[JOOKI_UID_LEN] = {0}; @@ -409,11 +413,14 @@ static int CmdHF14AJookiEncode(const char *Cmd) { fid = 0x0d; uint8_t iv[JOOKI_IV_LEN] = {0x80, 0x77, 0x51}; + if (use_tag) { + res = ul_read_uid(uid); if (res != PM3_SUCCESS) { return res; } + } else { if (ulen != JOOKI_UID_LEN) { PrintAndLogEx(ERR, "Wrong length of UID, expect %u, got %d", JOOKI_UID_LEN, ulen); @@ -442,14 +449,16 @@ static int CmdHF14AJookiDecode(const char *Cmd) { arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); + uint8_t b64[JOOKI_B64_LEN] = {0x00}; int dlen = sizeof(b64) - 1; // CLIGetStrWithReturn does not guarantee string to be null-terminated - memset(b64, 0x0, sizeof(b64)); CLIGetStrWithReturn(ctx, 1, b64, &dlen); + bool verbose = arg_get_lit(ctx, 2); CLIParserFree(ctx); uint8_t result[JOOKI_PLAIN_LEN] = {0}; + int res = jooki_decode(b64, result); if (res == PM3_SUCCESS) { jooki_print(b64, result, verbose); @@ -471,9 +480,9 @@ static int CmdHF14AJookiSim(const char *Cmd) { arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); + uint8_t b64[JOOKI_B64_LEN] = {0x00}; int dlen = sizeof(b64) - 1; // CLIGetStrWithReturn does not guarantee string to be null-terminated - memset(b64, 0x0, sizeof(b64)); CLIGetStrWithReturn(ctx, 1, b64, &dlen); CLIParserFree(ctx); @@ -533,7 +542,7 @@ static int CmdHF14AJookiSim(const char *Cmd) { // fast push mode g_conn.block_after_ACK = true; - uint8_t blockwidth = 4 + uint8_t blockwidth = 4; uint8_t counter = 0; uint8_t blockno = 0; @@ -595,11 +604,13 @@ static int CmdHF14AJookiSim(const char *Cmd) { break; } - if (WaitForResponseTimeout(CMD_HF_MIFARE_SIMULATE, &resp, 1500) == false) + if (WaitForResponseTimeout(CMD_HF_MIFARE_SIMULATE, &resp, 1500) == false) { continue; + } - if (resp.status != PM3_SUCCESS) + if (resp.status != PM3_SUCCESS) { break; + } } free(data); PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("hf 14a list") "` to view trace log"); @@ -623,26 +634,26 @@ static int CmdHF14AJookiClone(const char *Cmd) { arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); + uint8_t b64[JOOKI_B64_LEN] = {0x00}; int blen = sizeof(b64) - 1; // CLIGetStrWithReturn does not guarantee string to be null-terminated - memset(b64, 0x0, sizeof(b64)); CLIGetStrWithReturn(ctx, 1, b64, &blen); int dlen = 0; uint8_t data[52] = {0x00}; - memset(data, 0x0, sizeof(data)); int res = CLIParamHexToBuf(arg_get_str(ctx, 2), data, sizeof(data), &dlen); - if (res) { - CLIParserFree(ctx); - PrintAndLogEx(FAILED, "Error parsing bytes"); - return PM3_EINVARG; - } int plen = 0; uint8_t pwd[4] = {0x00}; CLIGetHexWithReturn(ctx, 3, pwd, &plen); CLIParserFree(ctx); + // sanity checks + if (res) { + PrintAndLogEx(FAILED, "Error parsing bytes"); + return PM3_EINVARG; + } + if (dlen != 52) { PrintAndLogEx(ERR, "Wrong data length. Expected 52 got %d", dlen); return PM3_EINVARG;