From 722a8aac8c9dec5cdaa2a1a272ba17763a64012f Mon Sep 17 00:00:00 2001 From: "Aaron Tulino (Aaronjamt)" Date: Mon, 22 Dec 2025 11:39:50 -0700 Subject: [PATCH 1/8] [hf seos] Simulate client support --- client/src/cmdhfseos.c | 114 +++++++++++++++++++++++++++++++++++++++++ client/src/cmdhfseos.h | 7 +-- client/src/cmdparser.c | 6 +++ client/src/cmdparser.h | 1 + 4 files changed, 122 insertions(+), 6 deletions(-) diff --git a/client/src/cmdhfseos.c b/client/src/cmdhfseos.c index 915b131ae..ee9200c8b 100644 --- a/client/src/cmdhfseos.c +++ b/client/src/cmdhfseos.c @@ -1676,6 +1676,118 @@ static int CmdHfSeosADF(const char *Cmd) { return seos_pacs((char *)oid, oid_len, data_tag, data_tag_len, key_index); } +static int CmdHfSeosSim(const char *Cmd) { + CLIParserContext *ctx; + CLIParserInit(&ctx, "hf seos sim", + "Simulate a SEOS card with the provided keys and data\n\n" + "By default:\n" + " - ADF OID : 2B0601040181E438010102011801010202\n" + " - Diversifier: 01020304050607\n" + " - Key Index : 2\n" + " - Data Tag : FF00\n" + " - Encryption : AES128\n" + " - Hashing : SHA256\n", + "hf seos sim -d 12345678\n" + "hf seos sim --ki 1\n" + "hf seos sim -o 2B0601040181E438010102011801010202 -u 01020304050607 --ki 2 -d 12345678\n" + "hf seos sim -o 2B0601040181E438010102011801010202 --legacy -t FF41 -d 12345678\n" + ); + + void *argtable[] = { + arg_param_begin, + arg_str0("t", "tag", "", "<0-100> hex bytes for tag to simulate (Default: FF00)"), + arg_str0("o", "oid", "", "<0-100> hex bytes for OID (Default: 2B0601040181E438010102011801010202)"), + arg_int0(NULL, "ki", "", "Specify key index to set key in memory"), + arg_str0("u", "diversifier", "", "<0-16> hex bytes for diversifier (Equivalent of UID)"), + arg_str0("d", "data", "", "<0-128> hex bytes for data (Must be valid BER-TLV)"), + arg_lit0("l", "legacy", "Use legacy algorithms (3DES/SHA1)"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + + int data_tag_len = 0; + uint8_t data_tag[16] = {0xff, 0x00}; + CLIGetHexWithReturn(ctx, 1, data_tag, &data_tag_len); + + int oid_len = 0; + uint8_t oid[256] = {0x2B, 0x06, 0x01, 0x04, 0x01, 0x81, 0xE4, 0x38, 0x01, 0x01, 0x02, 0x01, 0x18, 0x01, 0x01, 0x02, 0x02}; + CLIGetHexWithReturn(ctx, 2, oid, &oid_len); + + int key_index = arg_get_int_def(ctx, 3, 2); + + int diversifier_len = 0; + uint8_t diversifier[256] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; + CLIGetHexWithReturn(ctx, 4, diversifier, &diversifier_len); + + int data_len = 0; + uint8_t data[256] = {}; + CLIGetHexWithReturn(ctx, 5, data, &data_len); + + uint8_t encryption_algorithm = SEOS_ENCRYPTION_AES; + uint8_t hashing_algorithm = SEOS_HASHING_SHA256; + if (arg_get_lit(ctx, 6)) { // legacy algorithms + encryption_algorithm = SEOS_ENCRYPTION_2K3DES, + hashing_algorithm = SEOS_HASHING_SHA1; + } + + CLIParserFree(ctx); + + // Fall back to default values + if (data_tag_len == 0) { + data_tag_len = 2; + } + if (oid_len == 0) { + oid_len = 17; + } + if (diversifier_len == 0) { + diversifier_len = 7; + } + + if (data_len == 0) { + PrintAndLogEx(ERR, "Data to simulate must be supplied"); + return PM3_ESOFT; + } + + srand(time(NULL)); + seos_emulate_req_t request = { + .encr_alg = encryption_algorithm, + .hash_alg = hashing_algorithm, + + .uid = {0x08, rand()%255, rand()%255, rand()%255}, + .uid_len = 4, + .diversifier_len = diversifier_len, + .data_tag_len = data_tag_len, + .data_len = data_len, + .oid_len = oid_len, + }; + + // Copy all the provided values into the request object + memcpy(request.privenc, keys[key_index].privEncKey, 16); + memcpy(request.privmac, keys[key_index].privMacKey, 16); + memcpy(request.authkey, keys[key_index].readKey, 16); + + memcpy(request.diversifier, diversifier, diversifier_len); + memcpy(request.data_tag, data_tag, data_tag_len); + memcpy(request.data, data, data_len); + memcpy(request.oid, oid, oid_len); + + PacketResponseNG resp; + clearCommandBuffer(); + SendCommandNG(CMD_HF_SEOS_SIMULATE, (uint8_t*)&request, sizeof(request)); + + PrintAndLogEx(INFO, "Press " _GREEN_("pm3 button") " or " _GREEN_("") " to abort simulation"); + while (WaitForResponseTimeout(CMD_HF_SEOS_SIMULATE, &resp, 1000) == false) { + if (kbd_enter_pressed()) { + PrintAndLogEx(WARNING, "\naborted via keyboard."); + // inform device to break the sim loop since client has exited + SendCommandNG(CMD_BREAK_LOOP, NULL, 0); + return PM3_EOPABORTED; + } + } + + return PM3_SUCCESS; +} + static int CmdHfSeosManageKeys(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf seos managekeys", @@ -1988,6 +2100,8 @@ static command_t CommandTable[] = { {"write", CmdHfSeosWrite, IfPm3Iso14443a, "Write an ADF to the card"}, {"adf", CmdHfSeosADF, IfPm3Iso14443a, "Read an ADF from the card"}, {"gdf", CmdHfSeosGDF, IfPm3Iso14443a, "Read an GDF from card"}, + {"-----------", CmdHelp, IfPm3Seos, "---------------------- " _CYAN_("Simulation") " ---------------------"}, + {"sim", CmdHfSeosSim, IfPm3Seos, "Simulate Seos tag"}, {"-----------", CmdHelp, AlwaysAvailable, "------------------------ " _CYAN_("Utils") " ------------------------"}, {"managekeys", CmdHfSeosManageKeys, AlwaysAvailable, "Manage keys to use with SEOS commands"}, {NULL, NULL, NULL, NULL} diff --git a/client/src/cmdhfseos.h b/client/src/cmdhfseos.h index 46ce3d32e..abc7f7239 100644 --- a/client/src/cmdhfseos.h +++ b/client/src/cmdhfseos.h @@ -20,13 +20,8 @@ #define CMDHFSEOS_H__ #include "common.h" +#include "seos_cmd.h" -#define SEOS_ENCRYPTION_2K3DES 0x02 -#define SEOS_ENCRYPTION_3K3DES 0x03 -#define SEOS_ENCRYPTION_AES 0x09 - -#define SEOS_HASHING_SHA1 0x06 -#define SEOS_HASHING_SHA256 0x07 int infoSeos(bool verbose); int CmdHFSeos(const char *Cmd); int seos_kdf(bool encryption, uint8_t *masterKey, uint8_t keyslot, diff --git a/client/src/cmdparser.c b/client/src/cmdparser.c index fc628b947..16fbeba6c 100644 --- a/client/src/cmdparser.c +++ b/client/src/cmdparser.c @@ -182,6 +182,12 @@ bool IfPm3Iclass(void) { return g_pm3_capabilities.compiled_with_iclass; } +bool IfPm3Seos(void) { + if (IfPm3Present() == false) + return false; + return g_pm3_capabilities.compiled_with_seos; +} + bool IfPm3NfcBarcode(void) { if (IfPm3Present() == false) return false; diff --git a/client/src/cmdparser.h b/client/src/cmdparser.h index 34eee8bd5..4bfe71394 100644 --- a/client/src/cmdparser.h +++ b/client/src/cmdparser.h @@ -54,6 +54,7 @@ bool IfPm3Iso15693(void); bool IfPm3Felica(void); bool IfPm3Legicrf(void); bool IfPm3Iclass(void); +bool IfPm3Seos(void); bool IfPm3NfcBarcode(void); bool IfPm3Lcd(void); bool IfPm3Zx8211(void); From 17dfd50907ab8946312b5b6a9763089002bc33aa Mon Sep 17 00:00:00 2001 From: "Aaron Tulino (Aaronjamt)" Date: Mon, 22 Dec 2025 14:24:54 -0700 Subject: [PATCH 2/8] Update `CHANGELOG.md` --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 486a173a3..13aac3dbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac - Added ATR fingerprinting to `hf 14a/14b info` (@doegox) - Added `Verkada 40-bit` format (@aaronmaxlevy) - Added `hf seos write` command (@aaronjamt) +- Added `hf seos sim` command (@aaronjamt) ## [Phrack.4.20728][2025-09-11] - Added `unofficial desfire bible` document (@mistial-dev) From f1e6e49484725950f822d3d3b425ac301e0fd533 Mon Sep 17 00:00:00 2001 From: "Aaron Tulino (Aaronjamt)" Date: Mon, 22 Dec 2025 15:15:39 -0700 Subject: [PATCH 3/8] No more `rand()` --- client/src/cmdhfseos.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/client/src/cmdhfseos.c b/client/src/cmdhfseos.c index ee9200c8b..5f538b763 100644 --- a/client/src/cmdhfseos.c +++ b/client/src/cmdhfseos.c @@ -1698,8 +1698,9 @@ static int CmdHfSeosSim(const char *Cmd) { arg_str0("t", "tag", "", "<0-100> hex bytes for tag to simulate (Default: FF00)"), arg_str0("o", "oid", "", "<0-100> hex bytes for OID (Default: 2B0601040181E438010102011801010202)"), arg_int0(NULL, "ki", "", "Specify key index to set key in memory"), - arg_str0("u", "diversifier", "", "<0-16> hex bytes for diversifier (Equivalent of UID)"), + arg_str0(NULL, "div", "", "<0-16> hex bytes for diversifier (Equivalent of UID)"), arg_str0("d", "data", "", "<0-128> hex bytes for data (Must be valid BER-TLV)"), + arg_str0("u", "uid", "", "<0-10> hex bytes for UID (Must be a RID i.e. [0]=0x08)"), arg_lit0("l", "legacy", "Use legacy algorithms (3DES/SHA1)"), arg_param_end }; @@ -1723,9 +1724,13 @@ static int CmdHfSeosSim(const char *Cmd) { uint8_t data[256] = {}; CLIGetHexWithReturn(ctx, 5, data, &data_len); + int uid_len = 0; + uint8_t uid[10] = {0x08, 0x01, 0x02, 0x03}; + CLIGetHexWithReturn(ctx, 6, uid, &uid_len); + uint8_t encryption_algorithm = SEOS_ENCRYPTION_AES; uint8_t hashing_algorithm = SEOS_HASHING_SHA256; - if (arg_get_lit(ctx, 6)) { // legacy algorithms + if (arg_get_lit(ctx, 7)) { // legacy algorithms encryption_algorithm = SEOS_ENCRYPTION_2K3DES, hashing_algorithm = SEOS_HASHING_SHA1; } @@ -1742,19 +1747,20 @@ static int CmdHfSeosSim(const char *Cmd) { if (diversifier_len == 0) { diversifier_len = 7; } + if (uid_len == 0) { + uid_len = 4; + } if (data_len == 0) { PrintAndLogEx(ERR, "Data to simulate must be supplied"); return PM3_ESOFT; } - srand(time(NULL)); seos_emulate_req_t request = { .encr_alg = encryption_algorithm, .hash_alg = hashing_algorithm, - .uid = {0x08, rand()%255, rand()%255, rand()%255}, - .uid_len = 4, + .uid_len = uid_len, .diversifier_len = diversifier_len, .data_tag_len = data_tag_len, .data_len = data_len, @@ -1766,6 +1772,7 @@ static int CmdHfSeosSim(const char *Cmd) { memcpy(request.privmac, keys[key_index].privMacKey, 16); memcpy(request.authkey, keys[key_index].readKey, 16); + memcpy(request.uid, uid, uid_len); memcpy(request.diversifier, diversifier, diversifier_len); memcpy(request.data_tag, data_tag, data_tag_len); memcpy(request.data, data, data_len); From 53306abd5e1a5fd5d76212878e200cb5bd864b68 Mon Sep 17 00:00:00 2001 From: "Aaron Tulino (Aaronjamt)" Date: Mon, 22 Dec 2025 15:55:37 -0700 Subject: [PATCH 4/8] Reduce array usages in Seos simulation --- armsrc/seos.c | 161 ++++++++++++++++++++++---------------------------- 1 file changed, 72 insertions(+), 89 deletions(-) diff --git a/armsrc/seos.c b/armsrc/seos.c index 1e37605b5..e19d2dfc3 100644 --- a/armsrc/seos.c +++ b/armsrc/seos.c @@ -63,97 +63,66 @@ static uint8_t round_to_next(uint8_t value, uint8_t step) { } } -static bool generate_cryptogram(const uint8_t *key, const uint8_t *opt_iv, const uint8_t *input, size_t length, uint8_t *output, uint8_t algorithm) { - // IV is optional, only add if provided. Zeros by default. - uint8_t iv[16] = {0x00}; - if (opt_iv != NULL) { - memcpy(iv, opt_iv, 16); - } - - uint8_t bs = block_size(algorithm); - uint8_t padded_length = round_to_next(length, bs); - - // Allocate enough room to store any additional padding - uint8_t cleartext[padded_length]; - uint8_t cryptogram[padded_length]; - - memset(cleartext, 0, padded_length); - memcpy(cleartext, input, length); - // ISO7816 padding: add 0x80 after data, then follow with all zeros - if (padded_length != length) { - cleartext[length] = 0x80; +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) { + memset(cryptogram_iv, 0x00, 16); } if (algorithm == SEOS_ENCRYPTION_AES) { mbedtls_aes_context ctx; mbedtls_aes_setkey_enc(&ctx, key, 128); - mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, length, iv, input, cryptogram); + mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, length, cryptogram_iv, input, output); mbedtls_aes_free(&ctx); } else if (algorithm == SEOS_ENCRYPTION_2K3DES) { mbedtls_des3_context ctx; mbedtls_des3_set2key_enc(&ctx, key); - mbedtls_des3_crypt_cbc(&ctx, MBEDTLS_DES_ENCRYPT, length, iv, input, cryptogram); + mbedtls_des3_crypt_cbc(&ctx, MBEDTLS_DES_ENCRYPT, length, cryptogram_iv, input, output); mbedtls_des3_free(&ctx); } - // Add generated cryptogram to output buffer - memcpy(output, cryptogram, length); - return true; } static bool decrypt_cryptogram(const uint8_t *key, const uint8_t *input, size_t length, uint8_t *output, uint8_t algorithm) { - uint8_t iv[16] = {0x00}; - - // Allocate enough room to store any additional padding - uint8_t cleartext[length + block_size(algorithm)]; + memset(cryptogram_iv, 0x00, 16); if (algorithm == SEOS_ENCRYPTION_AES) { mbedtls_aes_context ctx; mbedtls_aes_init(&ctx); mbedtls_aes_setkey_dec(&ctx, key, 128); - mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, length, iv, input, cleartext); + mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, length, cryptogram_iv, input, output); mbedtls_aes_free(&ctx); } else if (algorithm == SEOS_ENCRYPTION_2K3DES) { mbedtls_des3_context ctx; mbedtls_des3_set2key_dec(&ctx, key); - mbedtls_des3_crypt_cbc(&ctx, MBEDTLS_DES_DECRYPT, length, iv, input, cleartext); + mbedtls_des3_crypt_cbc(&ctx, MBEDTLS_DES_DECRYPT, length, cryptogram_iv, input, output); mbedtls_des3_free(&ctx); } else { Dbprintf(_RED_("Unknown Encryption Algorithm")); return false; } - // Add decrypted cleartext to output buffer - memcpy(output, cleartext, length); - return true; } // Returns length of generated CMAC -static uint8_t generate_cmac(const uint8_t *key, const uint8_t *input, size_t length, uint8_t *output, uint8_t max_output_len, uint8_t encryption_algorithm) { - uint8_t size = block_size(encryption_algorithm); - uint8_t mac[size]; - +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, mac); + ulaes_cmac(key, 16, input, length, output); } else if (encryption_algorithm == SEOS_ENCRYPTION_2K3DES || encryption_algorithm == SEOS_ENCRYPTION_3K3DES) { uint8_t keylen = 16; if (encryption_algorithm == SEOS_ENCRYPTION_3K3DES) keylen = 24; - des3_cmac(key, keylen, input, length, mac); + des3_cmac(key, keylen, input, length, output); } else { Dbprintf(_RED_("Unknown Encryption Algorithm")); return false; } - // Add generated CMAC to output buffer - size = MIN(size, max_output_len); - memcpy(output, mac, size); - - return size; + return true; } -static void seos_kdf(bool forEncryption, uint8_t *masterKey, uint8_t keyslot, +static void seos_kdf(bool forEncryption, uint8_t *masterKey, uint8_t keyslot, uint8_t *work_buffer, uint8_t *adfOid, size_t adfoid_len, uint8_t *diversifier, uint8_t diversifier_len, uint8_t *out, int encryption_algorithm, int hash_algorithm) { // Encryption key = 04 @@ -166,28 +135,18 @@ static void seos_kdf(bool forEncryption, uint8_t *masterKey, uint8_t keyslot, typeOfKey = 0x04; } - uint8_t inputPre[] = { - // Padding - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, typeOfKey, 0x00, 0x00, 0x80, 0x01, - encryption_algorithm, hash_algorithm, keyslot - }; - - // 00000000000000000000000600008001 09 07 00 06112B0601040181E438010102011801010202 EFB08A28B0529F - // 00000000000000000000000400008001 09 07 00 06112B0601040181E438010102011801010202 EFB08A28B0529F - // 06112B0601040181E438010102011801010202 CF 07 EFB08A28B0529F DBA240413B0969B7111F4B6133A3DEFAD934B6DC - - - uint8_t input[sizeof(inputPre) + adfoid_len + diversifier_len]; - - memset(input, 0, sizeof(input)); - - memcpy(input, inputPre, sizeof(inputPre)); - memcpy(input + sizeof(inputPre), adfOid, adfoid_len); - memcpy(input + sizeof(inputPre) + adfoid_len, diversifier, diversifier_len); + memset(work_buffer, 0x00, 16); + work_buffer[11] = typeOfKey; + work_buffer[14] = 0x80; + work_buffer[15] = 0x01; + work_buffer[16] = encryption_algorithm; + work_buffer[17] = hash_algorithm; + work_buffer[18] = keyslot; + memcpy(work_buffer+19, adfOid, adfoid_len); + 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, input, sizeof(input), out, 16, SEOS_ENCRYPTION_AES); + generate_cmac(masterKey, work_buffer, 19 + adfoid_len + diversifier_len, out, SEOS_ENCRYPTION_AES); } // turn off afterwards @@ -208,7 +167,6 @@ void SimulateSeos(seos_emulate_req_t *msg) { uint8_t diver_cmac_key[16]; // Calculated block size - const uint8_t max_bs = 16; const uint8_t bs = block_size(msg->encr_alg); const uint8_t half_bs = bs >> 1; if (bs == 0) { @@ -220,9 +178,9 @@ void SimulateSeos(seos_emulate_req_t *msg) { // free eventually allocated BigBuf memory but keep Emulator Memory BigBuf_free_keep_EM(); - // Allocate 512 bytes for the dynamic modulation, created when the reader queries for it + // Allocate 1024 bytes for the dynamic modulation, created when the reader queries for it // Such a response is less time critical, so we can prepare them on the fly -#define DYNAMIC_RESPONSE_BUFFER_SIZE 64 +#define DYNAMIC_RESPONSE_BUFFER_SIZE 192 #define DYNAMIC_MODULATION_BUFFER_SIZE 1024 uint8_t *dynamic_response_buffer = BigBuf_calloc(DYNAMIC_RESPONSE_BUFFER_SIZE); @@ -259,6 +217,14 @@ void SimulateSeos(seos_emulate_req_t *msg) { return; } + // The RND_* counter is exactly one block size + uint8_t *rndCounter = BigBuf_calloc(bs); + if (rndCounter == NULL) { + BigBuf_free_keep_EM(); + reply_ng(CMD_HF_MIFARE_SIMULATE, PM3_EMALLOC, NULL, 0); + return; + } + uint16_t flags = 0; uint8_t data[PM3_CMD_DATA_SIZE] = { 0 }; memcpy(data, msg->uid, msg->uid_len); @@ -416,9 +382,11 @@ void SimulateSeos(seos_emulate_req_t *msg) { if (selected_oid) { // Synthesized IV: half a block of random data followed by half of the CMAC of that data - uint8_t synthesized_iv[max_bs]; - memset(synthesized_iv, 0, bs>>1); // TODO: Maybe actually use random data? - generate_cmac(msg->privmac, synthesized_iv, bs>>1, synthesized_iv+(bs>>1), bs>>1, msg->encr_alg); + memset(cryptogram_iv, 0, half_bs); // TODO: Maybe actually use random data? + if (!generate_cmac(msg->privmac, cryptogram_iv, half_bs, cryptogram_iv+half_bs, msg->encr_alg)) { + Dbprintf(_RED_("Select ADF failed") ": Failed to create IV CMAC."); + break; + } // Always exactly 0x30 bytes in length const uint8_t reply_len = 0x30; @@ -436,12 +404,6 @@ void SimulateSeos(seos_emulate_req_t *msg) { memcpy(reply+reply_idx, msg->diversifier, msg->diversifier_len); reply_idx += msg->diversifier_len; - uint8_t cryptogram[reply_len]; - if (!generate_cryptogram(msg->privenc, synthesized_iv, reply, reply_len, cryptogram, msg->encr_alg)) { - Dbprintf(_RED_("Select ADF failed") ": Failed to create reply cryptogram."); - break; - } - uint8_t tlv_base = 1 + offset; uint8_t tlv_idx = tlv_base; @@ -452,14 +414,23 @@ void SimulateSeos(seos_emulate_req_t *msg) { dynamic_response_info.response[tlv_idx++] = 0x85; // Tag: cryptogram dynamic_response_info.response[tlv_idx++] = reply_len + bs; // Length - memcpy(dynamic_response_info.response+tlv_idx, synthesized_iv, bs); + memcpy(dynamic_response_info.response+tlv_idx, cryptogram_iv, bs); tlv_idx += bs; - memcpy(dynamic_response_info.response+tlv_idx, cryptogram, reply_len); + + // Generate cryptogram directly into response buffer + if (!generate_cryptogram(msg->privenc, true, reply, reply_len, dynamic_response_info.response+tlv_idx, msg->encr_alg)) { + Dbprintf(_RED_("Select ADF failed") ": Failed to create reply cryptogram."); + break; + } tlv_idx += reply_len; // Always an 8-byte CMAC + const uint8_t cmac_size = 8; uint8_t *cmac = work_buffer_a; - uint8_t cmac_size = generate_cmac(msg->privmac, dynamic_response_info.response+tlv_base, tlv_idx-tlv_base, cmac, 8, msg->encr_alg); + if (!generate_cmac(msg->privmac, dynamic_response_info.response+tlv_base, tlv_idx-tlv_base, cmac, msg->encr_alg)) { + Dbprintf(_RED_("Select ADF failed") ": Failed to create reply CMAC."); + break; + } dynamic_response_info.response[tlv_idx++] = 0x8E; // Tag: CMAC dynamic_response_info.response[tlv_idx++] = cmac_size; // Length @@ -520,13 +491,16 @@ void SimulateSeos(seos_emulate_req_t *msg) { uint8_t keyslot = receivedCmd[4 + offset]; // APDU P2 byte - seos_kdf(true, msg->authkey, keyslot, msg->oid, msg->oid_len, msg->diversifier, msg->diversifier_len, diver_encr_key, msg->encr_alg, msg->hash_alg); - seos_kdf(false, msg->authkey, keyslot, msg->oid, msg->oid_len, msg->diversifier, msg->diversifier_len, diver_cmac_key, msg->encr_alg, msg->hash_alg); + seos_kdf(true, msg->authkey, keyslot, work_buffer_a, msg->oid, msg->oid_len, msg->diversifier, msg->diversifier_len, diver_encr_key, msg->encr_alg, msg->hash_alg); + seos_kdf(false, msg->authkey, keyslot, work_buffer_a, msg->oid, msg->oid_len, msg->diversifier, msg->diversifier_len, diver_cmac_key, msg->encr_alg, msg->hash_alg); // Verify CMAC (last 8 bytes) uint8_t request_len = received_tlv_len - 8; uint8_t *cmac = work_buffer_a; - generate_cmac(diver_cmac_key, received_tlv, request_len, cmac, 8, msg->encr_alg); + if (!generate_cmac(diver_cmac_key, received_tlv, request_len, cmac, msg->encr_alg)) { + Dbprintf(_RED_("Mutual auth failed") ": Failed to create CMAC."); + break; + } if (memcmp(cmac, received_tlv + request_len, 8) != 0) { Dbprintf(_RED_("Mutual auth failed") ": Invalid CMAC:"); Dbhexdump(8, received_tlv + request_len, false); @@ -559,8 +533,11 @@ void SimulateSeos(seos_emulate_req_t *msg) { // Generate cryptogram + 8-byte CMAC const uint8_t reply_len = reply_plain_len + 8; uint8_t *reply = work_buffer_b; - generate_cryptogram(diver_encr_key, NULL, reply_plain, reply_plain_len, reply, msg->encr_alg); - generate_cmac(diver_cmac_key, reply, reply_plain_len, reply+reply_plain_len, 8, msg->encr_alg); + generate_cryptogram(diver_encr_key, false, reply_plain, reply_plain_len, reply, msg->encr_alg); + if (!generate_cmac(diver_cmac_key, reply, reply_plain_len, reply+reply_plain_len, msg->encr_alg)) { + Dbprintf(_RED_("Mutual auth failed") ": Failed to create reply CMAC."); + break; + } uint8_t tlv_idx = 1 + offset; @@ -662,7 +639,6 @@ void SimulateSeos(seos_emulate_req_t *msg) { // Combine the first half_bs each of RND_ICC and RND_IFD, // then increment as a single counter - uint8_t rndCounter[bs]; memcpy(rndCounter, RND_ICC, half_bs); memcpy(rndCounter + half_bs, RND_IFD, half_bs); @@ -697,7 +673,10 @@ void SimulateSeos(seos_emulate_req_t *msg) { } uint8_t *cmac = work_buffer_b; - generate_cmac(diver_cmac_key, mac_input, mac_input_idx, cmac, recvd_cmac_length, msg->encr_alg); + if (!generate_cmac(diver_cmac_key, mac_input, mac_input_idx, cmac, msg->encr_alg)) { + Dbprintf(_RED_("Get Data failed") ": Failed to create CMAC."); + break; + } if (memcmp(cmac, recvd_cmac, recvd_cmac_length) != 0) { Dbprintf( _RED_("Get Data failed") ": Invalid CMAC."); break; @@ -748,7 +727,7 @@ void SimulateSeos(seos_emulate_req_t *msg) { } uint8_t *reply_cryptogram = work_buffer_b; - if (!generate_cryptogram(diver_encr_key, NULL, reply, reply_len, reply_cryptogram, msg->encr_alg)) { + if (!generate_cryptogram(diver_encr_key, false, reply, reply_len, reply_cryptogram, msg->encr_alg)) { Dbprintf(_RED_("Get Data failed") ": Failed to create reply cryptogram."); break; } @@ -787,7 +766,11 @@ void SimulateSeos(seos_emulate_req_t *msg) { mac_input_idx += bs - (mac_input_idx % bs); } - uint8_t cmac_size = generate_cmac(diver_cmac_key, mac_input, mac_input_idx, cmac, recvd_cmac_length, msg->encr_alg); + uint8_t cmac_size = recvd_cmac_length; + if (!generate_cmac(diver_cmac_key, mac_input, mac_input_idx, cmac, msg->encr_alg)) { + Dbprintf(_RED_("Get Data failed") ": Failed to create reply CMAC."); + break; + } dynamic_response_info.response[tlv_idx++] = 0x8E; // Tag: CMAC dynamic_response_info.response[tlv_idx++] = cmac_size; // Length From 3a931fb32004171a22ee978f866ee2abd694ea9e Mon Sep 17 00:00:00 2001 From: "Aaron Tulino (Aaronjamt)" Date: Tue, 23 Dec 2025 10:52:27 -0700 Subject: [PATCH 5/8] Fix #3063 --- client/src/cmdhfseos.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/client/src/cmdhfseos.c b/client/src/cmdhfseos.c index 4c32c8e1c..003b6f3f8 100644 --- a/client/src/cmdhfseos.c +++ b/client/src/cmdhfseos.c @@ -233,7 +233,11 @@ static void generate_command_wrapping(uint8_t *command_Header, int command_heade // 0181e43801010201 + 0000000000000001 + 0CCB3FFF800000000000000000000000 + 8510EB54DA90CB43AEE7FBFE816ECA25A10D + 9700 + 800000000000000000000000 uint8_t mac[8]; - create_cmac(diversified_mac_key, padded_toEncrypt, mac, padded_toEncrypt_len, sizeof(mac), encryption_algorithm); + memset(mac, 0x00, 8); + if (create_cmac(diversified_mac_key, padded_toEncrypt, mac, padded_toEncrypt_len, sizeof(mac), encryption_algorithm) != PM3_SUCCESS) { + PrintAndLogEx(ERR, _RED_("Failed to create MAC")); + return; + } // PrintAndLogEx(SUCCESS, "Encryption Key................... " _YELLOW_("%s"), sprint_hex_inrow(diversified_enc_key, 24)); // PrintAndLogEx(SUCCESS, "MAC Key.......................... " _YELLOW_("%s"), sprint_hex_inrow(diversified_mac_key, 24)); From 6da3dc379897d3ff991faa09449fa2ee077d3bf6 Mon Sep 17 00:00:00 2001 From: "Aaron Tulino (Aaronjamt)" Date: Tue, 23 Dec 2025 11:02:09 -0700 Subject: [PATCH 6/8] [hf seos] Minor cleanup --- client/src/cmdhfseos.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/src/cmdhfseos.c b/client/src/cmdhfseos.c index 4c32c8e1c..26118d972 100644 --- a/client/src/cmdhfseos.c +++ b/client/src/cmdhfseos.c @@ -511,8 +511,8 @@ static void create_mutual_auth_key(uint8_t *KEYIFD, uint8_t *KEYICC, uint8_t *RN // PrintAndLogEx(SUCCESS, "RandomIFD........................ " _YELLOW_("%s"), sprint_hex_inrow(RNDIFD, 8)); // PrintAndLogEx(SUCCESS, "hash Input....................... " _YELLOW_("%s"), sprint_hex_inrow(hash_in,ARRAYLEN(hash_in))); - uint8_t output[128]; // Buffer to store the two 32-byte keys - uint8_t hashedOutput[128]; + uint8_t output[32]; // Buffer to store the two 16-byte keys + uint8_t hashedOutput[32]; uint32_t counter = 1; // Generate the first key @@ -526,7 +526,7 @@ static void create_mutual_auth_key(uint8_t *KEYIFD, uint8_t *KEYICC, uint8_t *RN counter++; set_counter_big_endian(hash_in, counter); sha1hash(hash_in, sizeof(hash_in), hashedOutput); - memcpy(output + 20, hashedOutput, 20); + memcpy(output + 20, hashedOutput, 12); //PrintAndLogEx(SUCCESS, "key_out_temp..................... " _YELLOW_("%s"), sprint_hex_inrow(hash_in,ARRAYLEN(hash_in))); } else if (HashingAlgorithm == 0x07) { sha256hash(hash_in, sizeof(hash_in), hashedOutput); From e2bd1444fbc05bb9ed1d933cac21b52f2933dc89 Mon Sep 17 00:00:00 2001 From: "Aaron Tulino (Aaronjamt)" Date: Tue, 23 Dec 2025 14:43:14 -0700 Subject: [PATCH 7/8] Further optimization Make SHA256 output directly into the final output buffer and make the first SHA1 do the same. The second SHA1 needs an intermediate buffer as it outputs 20 bytes but we only need the first 12. --- client/src/cmdhfseos.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/client/src/cmdhfseos.c b/client/src/cmdhfseos.c index 26118d972..59d9d0380 100644 --- a/client/src/cmdhfseos.c +++ b/client/src/cmdhfseos.c @@ -512,7 +512,6 @@ static void create_mutual_auth_key(uint8_t *KEYIFD, uint8_t *KEYICC, uint8_t *RN // PrintAndLogEx(SUCCESS, "hash Input....................... " _YELLOW_("%s"), sprint_hex_inrow(hash_in,ARRAYLEN(hash_in))); uint8_t output[32]; // Buffer to store the two 16-byte keys - uint8_t hashedOutput[32]; uint32_t counter = 1; // Generate the first key @@ -520,17 +519,16 @@ static void create_mutual_auth_key(uint8_t *KEYIFD, uint8_t *KEYICC, uint8_t *RN // PrintAndLogEx(SUCCESS, "key_out_temp..................... " _YELLOW_("%s"), sprint_hex_inrow(hash_in,ARRAYLEN(hash_in))); if (HashingAlgorithm == 0x06) { - sha1hash(hash_in, sizeof(hash_in), hashedOutput); + sha1hash(hash_in, sizeof(hash_in), output); //PrintAndLogEx(SUCCESS, "key_out_temp..................... " _YELLOW_("%s"), sprint_hex_inrow(hash_in,ARRAYLEN(hash_in))); - memcpy(output, hashedOutput, 20); counter++; set_counter_big_endian(hash_in, counter); + uint8_t hashedOutput[20]; sha1hash(hash_in, sizeof(hash_in), hashedOutput); memcpy(output + 20, hashedOutput, 12); //PrintAndLogEx(SUCCESS, "key_out_temp..................... " _YELLOW_("%s"), sprint_hex_inrow(hash_in,ARRAYLEN(hash_in))); } else if (HashingAlgorithm == 0x07) { - sha256hash(hash_in, sizeof(hash_in), hashedOutput); - memcpy(output, hashedOutput, 32); + sha256hash(hash_in, sizeof(hash_in), output); } else { // Yes they generate their encryption keys and mac keys in a weird way for no fucking reason, the 2nd cycle isn't required. PrintAndLogEx(ERR, _RED_("Unknown Hashing Algorithm")); From 08f7e0d45a1c7c9137e0b54b0ae071c53a301e66 Mon Sep 17 00:00:00 2001 From: "Aaron Tulino (Aaronjamt)" Date: Tue, 23 Dec 2025 22:51:29 -0700 Subject: [PATCH 8/8] Another minor bugfix --- client/src/cmdhfseos.c | 1 + 1 file changed, 1 insertion(+) diff --git a/client/src/cmdhfseos.c b/client/src/cmdhfseos.c index 13a684430..c3ecb7618 100644 --- a/client/src/cmdhfseos.c +++ b/client/src/cmdhfseos.c @@ -926,6 +926,7 @@ static int seos_mutual_auth(uint8_t *adfOID, size_t adfoid_len, uint8_t *randomI // ----------------- Encryption and MAC Generation ----------------- uint8_t mac[8]; + memset(mac, 0x00, 8); uint8_t mutual_auth_enc[32]; create_cryptogram(AES_key, mutual_auth_plain, mutual_auth_enc, sizeof(mutual_auth_plain), encryption_algorithm); create_cmac(MAC_key, mutual_auth_enc, mac, sizeof(mutual_auth_enc), sizeof(mac), encryption_algorithm);