Merge branch 'master' of https://github.com/RfidResearchGroup/proxmark3 into 20260819-gdmfix
This commit is contained in:
commit
dfa21be198
27 files changed
+4576
-451
No files matched your search
@@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.
|
||||
This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log...
|
||||
|
||||
## [unreleased][unreleased]
|
||||
- Added `nfc encode` and `hf mfu ndefwrite` - build NDEF URI/Text/AAR records and write them to Ultralight tags (@0x6r1an0y)
|
||||
- Fixed `hf emrtd info` - the picture viewer now scales images to the window instead of pinning them at native size (@iceman1001)
|
||||
- Added `hf emrtd` - PACE-CAM (Chip Authentication Mapping) session support. The anti-clone proof itself is not yet verified (@iceman1001)
|
||||
- Added `hf emrtd test` - offline regression tests for the PACE / secure messaging primitives (@iceman1001)
|
||||
- Added `hf emrtd info/dump --can/--pace/--bac` - PACE-ECDH-GM support with MRZ or CAN passwords, AES-CMAC-128/192/256 and 3DES-CBC-CBC secure messaging (@iceman1001)
|
||||
- Changed `hf emrtd info` - now shows EF_DG2 portraits for JPREG2000 via new dependecy lib (@iceman1001)
|
||||
- Changed `hf emrtd info`- now shows portrait, signature and other biometrics at the same time (@iceman1001)
|
||||
- Fixed `pm3_grabbed_output_get` - heap overflow (@jonyen)
|
||||
@@ -16,6 +21,8 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac
|
||||
- Fixed `hw version` / `hw status` (PM5) - now showing correct information for fpga images (@nemanjan00)
|
||||
- Fixed `hw status` [Model] section reporting "PM3 GENERIC" firmware on Proxmark5 (PM5/AT32); it now reports `PM5` (@nemanjan00)
|
||||
- Added `hw version` / `hw status` - now reports Proxmark5 (PM5/AT32) correct MCU (AT32F437) (@nemanjan00)
|
||||
- Added CMake support for armsrc and bootrom (@xianglin1998)
|
||||
- Added HAL support for ARM firmware (@xianglin1998)
|
||||
- Fixed `hf mf dump` - preserving readable sector trailer Key B data (@oSPANNERo)
|
||||
- Changed `magic_cards_notes.md` - documented the USCUID-UL helper scripts (`hf_mfu_uscuid` / `hf_mf_uscuid_prog`) (@c-barron)
|
||||
- Fixed `hf_mf_uscuid_prog.lua` - corrected the script name shown in its usage text (@c-barron)
|
||||
|
||||
+33
-26
@@ -1007,10 +1007,8 @@ void hitag_sniff(void) {
|
||||
// Set up eavesdropping mode, frequency divisor which will drive the FPGA
|
||||
// and analog mux selection.
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT | FPGA_LF_EDGE_DETECT_TOGGLE_MODE);
|
||||
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); // 125Khz
|
||||
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, LF_DIVISOR_125); // 125Khz
|
||||
SetAdcMuxFor(ADC_MUXSEL_LOPKD);
|
||||
RELAY_OFF();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1167,19 +1165,15 @@ void SniffHitag2(bool ledcontrol) {
|
||||
}
|
||||
*/
|
||||
|
||||
// Dont use the FPGA modulation output, we only want to capture the tag/reader comms
|
||||
gpio_fpga_mod_only_setup();
|
||||
Gpio_SSC_DOUT_Low();
|
||||
|
||||
// Set up eavesdropping mode, frequency divisor which will drive the FPGA
|
||||
// and analog mux selection.
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT | FPGA_LF_EDGE_DETECT_TOGGLE_MODE);
|
||||
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); // 125Khz
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT);
|
||||
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, LF_DIVISOR_125); // 125Khz
|
||||
SetAdcMuxFor(ADC_MUXSEL_LOPKD);
|
||||
RELAY_OFF();
|
||||
|
||||
// Configure output pin that is connected to the FPGA (for modulating)
|
||||
// AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
|
||||
// AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT;
|
||||
|
||||
// Disable modulation, we are going to eavesdrop, not modulate ;)
|
||||
// Gpio_SSC_DOUT_Low();
|
||||
|
||||
// Configure the input capture (TC1) via the HAL: it enables the TC1 clock, routes
|
||||
// SSC_FRAME to the timer input, resets on each falling edge and captures RB
|
||||
@@ -1198,6 +1192,10 @@ void SniffHitag2(bool ledcontrol) {
|
||||
|
||||
auth_table = (uint8_t *)BigBuf_calloc(AUTH_TABLE_LENGTH);
|
||||
|
||||
// Only for debug, if we want to see the edge timing of the captured frames
|
||||
uint8_t edges[100];
|
||||
uint8_t e_count = 0;
|
||||
|
||||
while (BUTTON_PRESS() == false) {
|
||||
|
||||
WDT_HIT();
|
||||
@@ -1219,6 +1217,7 @@ void SniffHitag2(bool ledcontrol) {
|
||||
if (ledcontrol) LED_C_OFF();
|
||||
reader_frame = true;
|
||||
rxlen = 0;
|
||||
memset(rx, 0x00, sizeof(rx));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1226,6 +1225,10 @@ void SniffHitag2(bool ledcontrol) {
|
||||
if (lo_edge == LO_EDGE_FALLING) {
|
||||
int rb = GetLoEdgeCaptureFalling() / HITAG_T0;
|
||||
|
||||
if (e_count < sizeof(edges)) {
|
||||
edges[e_count++] = rb; // Store the edge timing for debug purposes
|
||||
}
|
||||
|
||||
if (reader_frame) {
|
||||
|
||||
if (ledcontrol) LED_B_ON();
|
||||
@@ -1233,7 +1236,9 @@ void SniffHitag2(bool ledcontrol) {
|
||||
if (rb >= HITAG_T_STOP) {
|
||||
// Capture the T0 periods that have passed since last communication or field drop (reset)
|
||||
response = (rb - HITAG_T_LOW);
|
||||
if (rxlen != 0) { Dbprintf("rb - HITAG_T_LOW... %i", response); }
|
||||
if (rxlen != 0) {
|
||||
DBG Dbprintf("rb - HITAG_T_LOW... %i", response);
|
||||
}
|
||||
|
||||
} else if (rb >= HITAG_T_1_MIN) {
|
||||
// '1' bit
|
||||
@@ -1294,6 +1299,15 @@ void SniffHitag2(bool ledcontrol) {
|
||||
frame_count++;
|
||||
LogTraceBits(rx, rxlen, response, 0, reader_frame);
|
||||
|
||||
// Print only from tag response(No auth cmd from reader)
|
||||
DBG {
|
||||
if (rxlen >= 8) {
|
||||
for (size_t i = 0; i < e_count; i++) {
|
||||
Dbprintf("edges[%zu] = %u", i, edges[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we recognize a valid authentication attempt
|
||||
if (rxlen == 64) {
|
||||
// Store the authentication attempt
|
||||
@@ -1313,28 +1327,21 @@ void SniffHitag2(bool ledcontrol) {
|
||||
lastbit = 1;
|
||||
bSkip = true;
|
||||
tag_sof = 4;
|
||||
|
||||
if (ledcontrol) {
|
||||
LED_B_OFF();
|
||||
LED_C_OFF();
|
||||
}
|
||||
|
||||
e_count = 0;
|
||||
}
|
||||
|
||||
// Reset the frame length
|
||||
// Reset the frame length and clear the buffer so that '0' bits written
|
||||
// by the next frame don't inherit stale '1' bits from this one.
|
||||
rxlen = 0;
|
||||
|
||||
// Reset the capture counter to restart the while-loop that receives frames.
|
||||
ResetLoEdgeCapture();
|
||||
memset(rx, 0x00, sizeof(rx));
|
||||
}
|
||||
|
||||
if (ledcontrol) LEDsoff();
|
||||
|
||||
StopLoEdgeCapture();
|
||||
|
||||
DBG Dbprintf("frames.......... %d", frame_count);
|
||||
Dbprintf("Auth attempts... %d", (auth_table_len / 8));
|
||||
|
||||
StopLoEdgeCapture();
|
||||
switch_off();
|
||||
BigBuf_free();
|
||||
}
|
||||
|
||||
@@ -358,6 +358,8 @@ set (TARGET_SOURCES
|
||||
${PM3_ROOT}/client/src/cipurse/cipursecrypto.c
|
||||
${PM3_ROOT}/client/src/cipurse/cipursecore.c
|
||||
${PM3_ROOT}/client/src/cipurse/cipursetest.c
|
||||
${PM3_ROOT}/client/src/emrtd/emrtd_pace.c
|
||||
${PM3_ROOT}/client/src/emrtd/emrtd_pacetest.c
|
||||
${PM3_ROOT}/client/src/loclass/cipher.c
|
||||
${PM3_ROOT}/client/src/loclass/cipher_bs.c
|
||||
${PM3_ROOT}/client/src/loclass/cipher_bs_avx2.c
|
||||
|
||||
@@ -860,6 +860,8 @@ SRCS = mifare/aiddesfire.c \
|
||||
cipurse/cipursecore.c \
|
||||
cipurse/cipursecrypto.c \
|
||||
cipurse/cipursetest.c \
|
||||
emrtd/emrtd_pace.c \
|
||||
emrtd/emrtd_pacetest.c \
|
||||
fileutils.c \
|
||||
flash.c \
|
||||
frame_progress.c \
|
||||
@@ -959,6 +961,8 @@ ifeq ($(QT_FOUND),1)
|
||||
CXXSRCS = proxgui.cpp proxguiqt.cpp proxguiqt.moc.cpp
|
||||
# Qt has no JPEG2000 image handler, so we decode EF_DG2 & friends ourselves
|
||||
SRCS += imgjp2.c
|
||||
# openjpeg.a is a static lib, avoid __declspec(dllimport) on its Windows API decls
|
||||
PM3CFLAGS += -DOPJ_STATIC
|
||||
STATICLIBS += $(OPENJPEGLIB)
|
||||
LDLIBS += $(OPENJPEGLIBLD)
|
||||
else
|
||||
|
||||
@@ -28,6 +28,8 @@ add_library(pm3rrg_rdv4_openjpeg STATIC
|
||||
|
||||
target_include_directories(pm3rrg_rdv4_openjpeg INTERFACE openjpeg)
|
||||
target_include_directories(pm3rrg_rdv4_openjpeg PRIVATE openjpeg)
|
||||
# openjpeg.a is a static lib, avoid __declspec(dllimport) on its Windows API decls
|
||||
target_compile_definitions(pm3rrg_rdv4_openjpeg PUBLIC OPJ_STATIC)
|
||||
# Third party code, don't hold it to our warning settings
|
||||
target_compile_options(pm3rrg_rdv4_openjpeg PRIVATE -w -O3)
|
||||
set_property(TARGET pm3rrg_rdv4_openjpeg PROPERTY POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
@@ -2,7 +2,8 @@ MYSRCPATHS =
|
||||
MYINCLUDES =
|
||||
# Third party code, don't fail the build on upstream's warnings
|
||||
MYCFLAGS = -w -O3
|
||||
MYDEFS =
|
||||
# Avoid __declspec(dllimport/dllexport) on Windows, we always link this in statically
|
||||
MYDEFS = -DOPJ_STATIC
|
||||
MYSRCS = \
|
||||
bio.c \
|
||||
cio.c \
|
||||
|
||||
@@ -8129,14 +8129,15 @@ OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k,
|
||||
|
||||
/* initialize the multiple tiles */
|
||||
/* ---------------------------- */
|
||||
cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
|
||||
const size_t num_tiles = (size_t)cp->tw * (size_t)cp->th;
|
||||
cp->tcps = (opj_tcp_t*) opj_calloc(num_tiles, sizeof(opj_tcp_t));
|
||||
if (!cp->tcps) {
|
||||
opj_event_msg(p_manager, EVT_ERROR,
|
||||
"Not enough memory to allocate tile coding parameters\n");
|
||||
return OPJ_FALSE;
|
||||
}
|
||||
|
||||
for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
|
||||
for (tileno = 0; tileno < num_tiles; tileno++) {
|
||||
opj_tcp_t *tcp = &cp->tcps[tileno];
|
||||
const OPJ_BOOL fixed_distoratio =
|
||||
cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
|
||||
|
||||
Binary file not shown.
@@ -251,6 +251,8 @@ set (TARGET_SOURCES
|
||||
${PM3_ROOT}/client/src/crypto/duoxcrypto.c
|
||||
${PM3_ROOT}/client/src/crypto/libpcrypto.c
|
||||
${PM3_ROOT}/client/src/crypto/originality.c
|
||||
${PM3_ROOT}/client/src/emrtd/emrtd_pace.c
|
||||
${PM3_ROOT}/client/src/emrtd/emrtd_pacetest.c
|
||||
${PM3_ROOT}/client/src/emv/test/cda_test.c
|
||||
${PM3_ROOT}/client/src/emv/test/crypto_test.c
|
||||
${PM3_ROOT}/client/src/emv/test/cryptotest.c
|
||||
|
||||
+1063
-387
File diff suppressed because it is too large.
Load diff
+13
-14
@@ -47,23 +47,22 @@ typedef struct emrtd_hashalg_s {
|
||||
const uint8_t descriptor[15];
|
||||
} emrtd_hashalg_t;
|
||||
|
||||
typedef struct emrtd_pacealg_s {
|
||||
const char *name;
|
||||
int (*keygenerator)(uint8_t *datain, int datainlen, uint8_t *dataout);
|
||||
const uint8_t descriptor[10];
|
||||
} emrtd_pacealg_t;
|
||||
|
||||
// Standardized Domain Parameters
|
||||
typedef struct emrtd_pacesdp_s {
|
||||
uint8_t id;
|
||||
const char *name;
|
||||
size_t size;
|
||||
} emrtd_pacesdp_t;
|
||||
// Everything needed to authenticate to a document, either with BAC or with PACE
|
||||
typedef struct emrtd_auth_s {
|
||||
char documentnumber[10];
|
||||
char dob[7];
|
||||
char expiry[7];
|
||||
char can[15];
|
||||
bool mrz_available;
|
||||
bool can_available;
|
||||
bool force_pace; // fail rather than falling back to BAC
|
||||
bool force_bac; // skip PACE even when EF_CardAccess is present
|
||||
} emrtd_auth_t;
|
||||
|
||||
int CmdHFeMRTD(const char *Cmd);
|
||||
|
||||
int dumpHF_EMRTD(char *documentnumber, char *dob, char *expiry, bool BAC_available, const char *path);
|
||||
int infoHF_EMRTD(char *documentnumber, char *dob, char *expiry, bool BAC_available, bool only_fast);
|
||||
int dumpHF_EMRTD(const emrtd_auth_t *auth, const char *path);
|
||||
int infoHF_EMRTD(const emrtd_auth_t *auth, bool only_fast);
|
||||
int infoHF_EMRTD_offline(const char *path);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -7402,6 +7402,431 @@ int CmdHF14MfuNDEFRead(const char *Cmd) {
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
// Build a NDEF message from the CLI options and write it into the data area of a
|
||||
// MIFARE Ultralight / NTAG tag. The tag has to be NDEF formatted already, this
|
||||
// command never touches the Capability Container in block 3.
|
||||
int CmdHF14MfuNDEFWrite(const char *Cmd) {
|
||||
|
||||
int ak_len = 0;
|
||||
bool has_auth_key = false;
|
||||
bool has_pwd = false;
|
||||
bool swap_endian = false;
|
||||
|
||||
iso14a_card_select_t card;
|
||||
uint8_t data[16] = {0x00};
|
||||
uint8_t authenticationkey[16] = {0x00};
|
||||
uint8_t *auth_key_ptr = authenticationkey;
|
||||
uint8_t pack[4] = {0, 0, 0, 0};
|
||||
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "hf mfu ndefwrite",
|
||||
"Write NFC Data Exchange Format (NDEF) records to a MIFARE Ultralight / NTAG tag.\n"
|
||||
"The tag must already be NDEF formatted, ie carry a Capability Container in block 3.\n"
|
||||
"\n"
|
||||
"Combine several of --uri, --text and --aar to build a multi record message,\n"
|
||||
"records are added in that order. Alternatively supply raw NDEF bytes with -d\n"
|
||||
"or -f, those get wrapped in a TLV container automatically when they are not\n"
|
||||
"already. Use `nfc encode` to build such raw bytes offline.\n"
|
||||
"\n"
|
||||
"Note: the tag is re-selected and re-authenticated for every block written,\n"
|
||||
"so a large message takes a while.",
|
||||
"hf mfu ndefwrite --uri https://proxmark.com\n"
|
||||
"hf mfu ndefwrite --uri tel:+123456789\n"
|
||||
"hf mfu ndefwrite --text \"hello world\"\n"
|
||||
"hf mfu ndefwrite --aar com.example.app\n"
|
||||
"hf mfu ndefwrite --uri https://proxmark.com --aar com.example.app\n"
|
||||
"hf mfu ndefwrite --uri https://proxmark.com -k ffffffff\n"
|
||||
"hf mfu ndefwrite -d 0311D1010D550270726F786D61726B2E636F6DFE\n"
|
||||
"hf mfu ndefwrite -f myfilename"
|
||||
);
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_str0("k", "key", "<hex>", "Authentication key (UL-C/UL-AES 16 bytes, EV1/NTAG 4 bytes)"),
|
||||
arg_lit0("l", NULL, "Swap entered key endianness"),
|
||||
arg_str0(NULL, "uri", "<str>", "URI record. URL, tel:, mailto:, ..."),
|
||||
arg_str0(NULL, "text", "<str>", "Text record"),
|
||||
arg_str0(NULL, "lang", "<str>", "language code for the text record (default: en)"),
|
||||
arg_str0(NULL, "aar", "<str>", "Android Application Record, ie an app package name"),
|
||||
arg_str0("d", "data", "<hex>", "Raw NDEF bytes to write"),
|
||||
arg_str0("f", "file", "<fn>", "Raw NDEF file to write"),
|
||||
arg_lit0("v", "verbose", "Verbose output"),
|
||||
arg_lit0(NULL, "schann", "use secure channel. Must have key"),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
||||
|
||||
CLIGetHexWithReturn(ctx, 1, authenticationkey, &ak_len);
|
||||
swap_endian = arg_get_lit(ctx, 2);
|
||||
|
||||
// CLIParamStrToBuf copies the trailing NUL as well but only rejects lengths
|
||||
// strictly above maxdatalen, so leave room for that byte
|
||||
int urilen = 0;
|
||||
char uri[1024] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 3), (uint8_t *)uri, sizeof(uri) - 1, &urilen);
|
||||
|
||||
int textlen = 0;
|
||||
char text[1024] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 4), (uint8_t *)text, sizeof(text) - 1, &textlen);
|
||||
|
||||
int langlen = 0;
|
||||
char lang[32] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 5), (uint8_t *)lang, sizeof(lang) - 1, &langlen);
|
||||
|
||||
int aarlen = 0;
|
||||
char aar[256] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 6), (uint8_t *)aar, sizeof(aar) - 1, &aarlen);
|
||||
|
||||
int rawlen = 0;
|
||||
uint8_t raw[MFU_NDEF_MAX_BYTES] = {0};
|
||||
CLIGetHexWithReturn(ctx, 7, raw, &rawlen);
|
||||
|
||||
int fnlen = 0;
|
||||
char filename[FILE_PATH_SIZE] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 8), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen);
|
||||
|
||||
bool verbose = arg_get_lit(ctx, 9);
|
||||
bool use_schann = arg_get_lit(ctx, 10);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
bool has_records = (urilen || textlen || aarlen);
|
||||
int sources = (has_records ? 1 : 0) + (rawlen ? 1 : 0) + (fnlen ? 1 : 0);
|
||||
if (sources == 0) {
|
||||
PrintAndLogEx(ERR, "Nothing to write. See `" _YELLOW_("hf mfu ndefwrite -h") "`");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
if (sources > 1) {
|
||||
PrintAndLogEx(ERR, "Use either the record options, -d or -f, not a mix of them");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
if ((langlen != 0) && (textlen == 0)) {
|
||||
PrintAndLogEx(WARNING, "--lang only applies to a text record, ignoring");
|
||||
}
|
||||
|
||||
switch (ak_len) {
|
||||
case 0:
|
||||
break;
|
||||
case 4:
|
||||
has_pwd = true;
|
||||
break;
|
||||
case 16:
|
||||
has_auth_key = true;
|
||||
break;
|
||||
default:
|
||||
PrintAndLogEx(WARNING, "ERROR: Key is incorrect length\n");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
if (use_schann && (has_auth_key == false)) {
|
||||
PrintAndLogEx(WARNING, "Secure channel must be called with key");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// build the TLV block that goes onto the tag
|
||||
// ------------------------------------------------------------------
|
||||
uint8_t tlv[MFU_NDEF_MAX_BYTES] = {0};
|
||||
size_t tlv_len = 0;
|
||||
int res = PM3_SUCCESS;
|
||||
|
||||
if (has_records) {
|
||||
|
||||
NDEFRecordDesc_t recs[3] = {{0}};
|
||||
size_t count = 0;
|
||||
|
||||
uint8_t p_uri[sizeof(uri) + 1] = {0};
|
||||
size_t p_uri_len = 0;
|
||||
if (urilen) {
|
||||
res = NDEFEncodePayloadURI(uri, p_uri, sizeof(p_uri), &p_uri_len);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to encode URI record");
|
||||
return res;
|
||||
}
|
||||
recs[count].tnf = tnfWellKnownRecord;
|
||||
recs[count].type = (const uint8_t *)NDEF_TYPE_URI;
|
||||
recs[count].typeLen = strlen(NDEF_TYPE_URI);
|
||||
recs[count].payload = p_uri;
|
||||
recs[count].payloadLen = p_uri_len;
|
||||
count++;
|
||||
}
|
||||
|
||||
uint8_t p_text[sizeof(text) + sizeof(lang) + 1] = {0};
|
||||
size_t p_text_len = 0;
|
||||
if (textlen) {
|
||||
res = NDEFEncodePayloadText(text, lang, p_text, sizeof(p_text), &p_text_len);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to encode Text record");
|
||||
return res;
|
||||
}
|
||||
recs[count].tnf = tnfWellKnownRecord;
|
||||
recs[count].type = (const uint8_t *)NDEF_TYPE_TEXT;
|
||||
recs[count].typeLen = strlen(NDEF_TYPE_TEXT);
|
||||
recs[count].payload = p_text;
|
||||
recs[count].payloadLen = p_text_len;
|
||||
count++;
|
||||
}
|
||||
|
||||
uint8_t p_aar[sizeof(aar)] = {0};
|
||||
size_t p_aar_len = 0;
|
||||
if (aarlen) {
|
||||
res = NDEFEncodePayloadAAR(aar, p_aar, sizeof(p_aar), &p_aar_len);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to encode Android Application Record");
|
||||
return res;
|
||||
}
|
||||
recs[count].tnf = tnfExternalRecord;
|
||||
recs[count].type = (const uint8_t *)NDEF_ANDROID_AAR;
|
||||
recs[count].typeLen = strlen(NDEF_ANDROID_AAR);
|
||||
recs[count].payload = p_aar;
|
||||
recs[count].payloadLen = p_aar_len;
|
||||
count++;
|
||||
}
|
||||
|
||||
uint8_t msg[MFU_NDEF_MAX_BYTES] = {0};
|
||||
size_t msg_len = 0;
|
||||
res = NDEFEncodeMessage(recs, count, msg, sizeof(msg), &msg_len);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to encode NDEF message");
|
||||
return res;
|
||||
}
|
||||
|
||||
res = NDEFEncodeTLV(msg, msg_len, tlv, sizeof(tlv), &tlv_len);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to wrap NDEF message in a TLV container");
|
||||
return res;
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
uint8_t *src = raw;
|
||||
size_t src_len = (size_t)rawlen;
|
||||
uint8_t *dump = NULL;
|
||||
|
||||
if (fnlen) {
|
||||
size_t bytes_read = 0;
|
||||
res = pm3_load_dump(filename, (void **)&dump, &bytes_read, sizeof(raw));
|
||||
if (res != PM3_SUCCESS) {
|
||||
return res;
|
||||
}
|
||||
src = dump;
|
||||
src_len = bytes_read;
|
||||
}
|
||||
|
||||
if (src_len == 0) {
|
||||
PrintAndLogEx(ERR, "No NDEF data supplied");
|
||||
free(dump);
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
// is it a TLV block already?
|
||||
bool is_tlv = false;
|
||||
switch (src[0]) {
|
||||
case 0x00:
|
||||
case 0x01:
|
||||
case 0x02:
|
||||
case 0x03:
|
||||
case 0xFD:
|
||||
case 0xFE:
|
||||
is_tlv = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_tlv) {
|
||||
if (src_len > sizeof(tlv)) {
|
||||
PrintAndLogEx(ERR, "NDEF data too large, %zu bytes", src_len);
|
||||
free(dump);
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
memcpy(tlv, src, src_len);
|
||||
tlv_len = src_len;
|
||||
} else {
|
||||
if (verbose) {
|
||||
PrintAndLogEx(INFO, "Raw data is not TLV wrapped, adding container");
|
||||
}
|
||||
res = NDEFEncodeTLV(src, src_len, tlv, sizeof(tlv), &tlv_len);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to wrap NDEF message in a TLV container");
|
||||
free(dump);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
free(dump);
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
PrintAndLogEx(INFO, "TLV block [%zu]...", tlv_len);
|
||||
print_hex_noascii_break(tlv, tlv_len, 32);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// find the tag and check the message fits
|
||||
// ------------------------------------------------------------------
|
||||
uint64_t tagtype = GetHF14AMfU_Type();
|
||||
if (tagtype == MFU_TT_UL_ERROR) {
|
||||
PrintAndLogEx(WARNING, "No Ultralight / NTAG based tag found");
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
ul_print_type(tagtype, 0);
|
||||
|
||||
if (swap_endian) {
|
||||
if (ak_len == 16) {
|
||||
if (((tagtype & MFU_TT_UL_C) == MFU_TT_UL_C)) {
|
||||
auth_key_ptr = SwapEndian64(authenticationkey, ak_len, 8);
|
||||
} else if (((tagtype & MFU_TT_UL_AES) == MFU_TT_UL_AES)) {
|
||||
auth_key_ptr = SwapEndian64(authenticationkey, ak_len, 16);
|
||||
}
|
||||
} else if (ak_len == 4) {
|
||||
auth_key_ptr = SwapEndian64(authenticationkey, ak_len, 4);
|
||||
}
|
||||
}
|
||||
|
||||
if (ul_auth_select(&card, tagtype, (has_auth_key || has_pwd), auth_key_ptr, pack, sizeof(pack), use_schann) == PM3_ESOFT) {
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
// read blocks 0..3 to get at the Capability Container
|
||||
int status = ul_read(0, data, sizeof(data), use_schann);
|
||||
if (status <= 0) {
|
||||
DropField();
|
||||
PrintAndLogEx(ERR, "Error: tag didnt answer to READ");
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
if (status != 16) {
|
||||
DropField();
|
||||
PrintAndLogEx(ERR, "Error: tag returned %d bytes, need 16 to read the NDEF Container", status);
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
if (ndef_print_CC(data + 12) == PM3_ESOFT) {
|
||||
DropField();
|
||||
PrintAndLogEx(ERR, "Error: tag didnt contain a NDEF Container");
|
||||
PrintAndLogEx(HINT, "Hint: the tag needs to be NDEF formatted first");
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
// size of the NDEF data area
|
||||
uint16_t ndef_size = ndef_get_maxsize(data + 12);
|
||||
if (ndef_size == 0) {
|
||||
DropField();
|
||||
PrintAndLogEx(ERR, "Error: tag announces an empty NDEF data area");
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
// The data area starts at block 4, so cap the announced size to what the
|
||||
// identified tag can physically hold. UL_MEMORY_ARRAY holds the last valid
|
||||
// block number, hence the +1.
|
||||
for (uint8_t idx = 1; idx < ARRAYLEN(UL_TYPES_ARRAY); idx++) {
|
||||
if ((tagtype & UL_TYPES_ARRAY[idx]) == UL_TYPES_ARRAY[idx]) {
|
||||
|
||||
int avail = (UL_MEMORY_ARRAY[idx] + 1 - MFU_NDEF_FIRST_BLOCK) * MFU_BLOCK_SIZE;
|
||||
if (avail > 0 && ndef_size > avail) {
|
||||
PrintAndLogEx(INFO, "NDEF data area (%u bytes) is larger than the tag (%d bytes), using tag size"
|
||||
, ndef_size
|
||||
, avail
|
||||
);
|
||||
ndef_size = avail;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// MLEN can announce up to 2040 bytes but WRITE addresses blocks with a single
|
||||
// byte, so block 255 is the last one we can reach.
|
||||
if (ndef_size > MFU_NDEF_MAX_BYTES) {
|
||||
ndef_size = MFU_NDEF_MAX_BYTES;
|
||||
}
|
||||
|
||||
if (tlv_len > ndef_size) {
|
||||
DropField();
|
||||
PrintAndLogEx(ERR, "NDEF message is too large for this tag");
|
||||
PrintAndLogEx(ERR, " message..... " _RED_("%zu") " bytes", tlv_len);
|
||||
PrintAndLogEx(ERR, " tag holds... " _GREEN_("%u") " bytes", ndef_size);
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
DropField();
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// write it out, one block at a time
|
||||
// ------------------------------------------------------------------
|
||||
uint8_t keytype = 0;
|
||||
if (has_auth_key || has_pwd) {
|
||||
if ((tagtype & MFU_TT_UL_C) == MFU_TT_UL_C) {
|
||||
keytype = 1; // UL_C auth
|
||||
} else if ((tagtype & MFU_TT_UL_AES) == MFU_TT_UL_AES) {
|
||||
keytype = 3; // UL_AES auth
|
||||
} else {
|
||||
keytype = 2; // UL_EV1/NTAG auth
|
||||
}
|
||||
}
|
||||
|
||||
// pad the tail so the last block is complete
|
||||
size_t padded_len = (tlv_len + (MFU_BLOCK_SIZE - 1)) & ~((size_t)MFU_BLOCK_SIZE - 1);
|
||||
uint16_t blocks = (uint16_t)(padded_len / MFU_BLOCK_SIZE);
|
||||
|
||||
PrintAndLogEx(INFO, "Writing " _YELLOW_("%u") " blocks ( %zu bytes ) from block " _YELLOW_("%d")
|
||||
, blocks
|
||||
, padded_len
|
||||
, MFU_NDEF_FIRST_BLOCK
|
||||
);
|
||||
PrintAndLogEx(INFO, "Press " _GREEN_("<Enter>") " to abort");
|
||||
|
||||
for (uint16_t i = 0; i < blocks; i++) {
|
||||
|
||||
if (kbd_enter_pressed()) {
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
PrintAndLogEx(WARNING, "aborted via keyboard!");
|
||||
if (i > 0) {
|
||||
PrintAndLogEx(WARNING, "tag holds a partially written NDEF message, blocks %d..%u"
|
||||
, MFU_NDEF_FIRST_BLOCK
|
||||
, MFU_NDEF_FIRST_BLOCK + i - 1
|
||||
);
|
||||
}
|
||||
return PM3_EOPABORTED;
|
||||
}
|
||||
|
||||
uint8_t blockno = (uint8_t)(MFU_NDEF_FIRST_BLOCK + i);
|
||||
|
||||
res = mfu_write_block(tlv + (i * MFU_BLOCK_SIZE), MFU_BLOCK_SIZE, keytype, auth_key_ptr, blockno, use_schann);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
|
||||
// a protected tag simply stops answering, so the write times out
|
||||
// rather than coming back with an explicit error
|
||||
if (res == PM3_ETIMEOUT) {
|
||||
PrintAndLogEx(FAILED, "Write block %u ( " _RED_("timeout") " )", blockno);
|
||||
} else {
|
||||
PrintAndLogEx(FAILED, "Write block %u ( " _RED_("fail") " )", blockno);
|
||||
}
|
||||
PrintAndLogEx(HINT, "Hint: Check password / key!");
|
||||
|
||||
if (i > 0) {
|
||||
PrintAndLogEx(WARNING, "tag holds a partially written NDEF message, blocks %d..%u"
|
||||
, MFU_NDEF_FIRST_BLOCK
|
||||
, blockno - 1
|
||||
);
|
||||
}
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
|
||||
PrintAndLogEx(INPLACE, "Block %u / %u", i + 1, blocks);
|
||||
}
|
||||
|
||||
DropField();
|
||||
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
PrintAndLogEx(SUCCESS, "Write ( " _GREEN_("ok") " )");
|
||||
PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("hf mfu ndefread") "` to verify");
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
// utility function. Retrieves emulator memory
|
||||
static int GetMfuDumpFromEMul(mfu_dump_t **buf) {
|
||||
|
||||
@@ -8274,6 +8699,7 @@ static command_t CommandTable[] = {
|
||||
{"incr", CmdHF14AMfUIncr, IfPm3Iso14443a, "Increments Ev1/NTAG counter"},
|
||||
{"info", CmdHF14AMfUInfo, IfPm3Iso14443a, "Tag information"},
|
||||
{"ndefread", CmdHF14MfuNDEFRead, IfPm3Iso14443a, "Prints NDEF records from card"},
|
||||
{"ndefwrite", CmdHF14MfuNDEFWrite, IfPm3Iso14443a, "Write NDEF records to card"},
|
||||
{"rdbl", CmdHF14AMfURdBl, IfPm3Iso14443a, "Read block"},
|
||||
{"restore", CmdHF14AMfURestore, IfPm3Iso14443a, "Restore a dump file onto a tag"},
|
||||
{"tamper", CmdHF14MfUTamper, IfPm3Iso14443a, "NTAG 213TT - Configure the tamper feature"},
|
||||
|
||||
@@ -57,7 +57,8 @@ int mfu_get_version_uid(uint8_t *version, uint8_t *uid);
|
||||
int mfuc_test_authentication_support(void);
|
||||
|
||||
int CmdHFMFUltra(const char *Cmd);
|
||||
int CmdHF14MfuNDEFRead(const char *Cmd);
|
||||
int CmdHF14MfuNDEFRead(const char *Cmd); // used by cmdnfc.c
|
||||
int CmdHF14MfuNDEFWrite(const char *Cmd); // used by cmdnfc.c
|
||||
int CmdHF14MfUTamper(const char *Cmd);
|
||||
|
||||
#define MFU_TT_UNKNOWN 0x0ULL
|
||||
|
||||
@@ -673,7 +673,7 @@ static saflok_mfc_datetime_t add_offset(const saflok_mfc_datetime_t *base, const
|
||||
|
||||
// variable days per month ... loop is simplest option
|
||||
uint8_t curr_days_in_month = get_days_in_month(tm.tm_year + 1900, tm.tm_mon + 1);
|
||||
for (; ((tm.tm_mday & 0xFF) > curr_days_in_month); curr_days_in_month = get_days_in_month(tm.tm_year + 1900, tm.tm_mon + 1)) {
|
||||
for (; (tm.tm_mday > (int)curr_days_in_month); curr_days_in_month = get_days_in_month(tm.tm_year + 1900, tm.tm_mon + 1)) {
|
||||
|
||||
tm.tm_mon ++;
|
||||
tm.tm_mday -= curr_days_in_month;
|
||||
|
||||
+183
-2
@@ -178,6 +178,183 @@ static int CmdNfcDecode(const char *Cmd) {
|
||||
return res;
|
||||
}
|
||||
|
||||
static int CmdNfcEncode(const char *Cmd) {
|
||||
|
||||
#ifndef MAX_NDEF_LEN
|
||||
#define MAX_NDEF_LEN 2048
|
||||
#endif
|
||||
|
||||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "nfc encode",
|
||||
"Encode NFC Data Exchange Format (NDEF) records.\n"
|
||||
"Combine several options to build a multi record message,\n"
|
||||
"records are added in the order listed below.\n"
|
||||
"\n"
|
||||
"By default a bare NDEF message is emitted. Use `--tlv` to wrap it in a\n"
|
||||
"NFC Forum Type 2 tag container ( 03 <len> ... FE ), which is what the\n"
|
||||
"`-d` parameter of `hf mf ndefwrite` and `hf 14a ndefwrite` expects.\n"
|
||||
"`hf mfu ndefwrite` adds the container itself, so it does not need `--tlv`",
|
||||
"nfc encode --uri https://proxmark.com\n"
|
||||
"nfc encode --uri tel:+123456789\n"
|
||||
"nfc encode --text \"hello world\"\n"
|
||||
"nfc encode --aar com.example.app\n"
|
||||
"nfc encode --uri https://proxmark.com --aar com.example.app\n"
|
||||
"nfc encode --uri https://proxmark.com --tlv -f myfilename"
|
||||
);
|
||||
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_str0(NULL, "uri", "<str>", "URI record. URL, tel:, mailto:, ..."),
|
||||
arg_str0(NULL, "text", "<str>", "Text record"),
|
||||
arg_str0(NULL, "lang", "<str>", "language code for the text record (default: en)"),
|
||||
arg_str0(NULL, "aar", "<str>", "Android Application Record, ie an app package name"),
|
||||
arg_lit0(NULL, "tlv", "wrap message in a NFC Forum Type 2 tag TLV container"),
|
||||
arg_str0("f", "file", "<fn>", "save raw bytes to file"),
|
||||
arg_lit0("v", "verbose", "verbose output"),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, false);
|
||||
|
||||
// CLIParamStrToBuf copies the trailing NUL as well but only rejects lengths
|
||||
// strictly above maxdatalen, so leave room for that byte
|
||||
int urilen = 0;
|
||||
char uri[1024] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)uri, sizeof(uri) - 1, &urilen);
|
||||
|
||||
int textlen = 0;
|
||||
char text[1024] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)text, sizeof(text) - 1, &textlen);
|
||||
|
||||
int langlen = 0;
|
||||
char lang[32] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 3), (uint8_t *)lang, sizeof(lang) - 1, &langlen);
|
||||
|
||||
int aarlen = 0;
|
||||
char aar[256] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 4), (uint8_t *)aar, sizeof(aar) - 1, &aarlen);
|
||||
|
||||
bool use_tlv = arg_get_lit(ctx, 5);
|
||||
|
||||
int fnlen = 0;
|
||||
char filename[FILE_PATH_SIZE] = {0};
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 6), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen);
|
||||
|
||||
bool verbose = arg_get_lit(ctx, 7);
|
||||
CLIParserFree(ctx);
|
||||
|
||||
if ((urilen == 0) && (textlen == 0) && (aarlen == 0)) {
|
||||
PrintAndLogEx(ERR, "Must specify at least one record. See `" _YELLOW_("nfc encode -h") "`");
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
if ((langlen != 0) && (textlen == 0)) {
|
||||
PrintAndLogEx(WARNING, "`--lang` only applies to a text record, ignoring");
|
||||
}
|
||||
|
||||
NDEFRecordDesc_t recs[3] = {{0}};
|
||||
size_t count = 0;
|
||||
int res = PM3_SUCCESS;
|
||||
|
||||
uint8_t p_uri[sizeof(uri) + 1] = {0};
|
||||
size_t p_uri_len = 0;
|
||||
if (urilen) {
|
||||
res = NDEFEncodePayloadURI(uri, p_uri, sizeof(p_uri), &p_uri_len);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to encode URI record");
|
||||
return res;
|
||||
}
|
||||
recs[count].tnf = tnfWellKnownRecord;
|
||||
recs[count].type = (const uint8_t *)NDEF_TYPE_URI;
|
||||
recs[count].typeLen = strlen(NDEF_TYPE_URI);
|
||||
recs[count].payload = p_uri;
|
||||
recs[count].payloadLen = p_uri_len;
|
||||
count++;
|
||||
}
|
||||
|
||||
uint8_t p_text[sizeof(text) + sizeof(lang) + 1] = {0};
|
||||
size_t p_text_len = 0;
|
||||
if (textlen) {
|
||||
res = NDEFEncodePayloadText(text, lang, p_text, sizeof(p_text), &p_text_len);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to encode Text record");
|
||||
return res;
|
||||
}
|
||||
recs[count].tnf = tnfWellKnownRecord;
|
||||
recs[count].type = (const uint8_t *)NDEF_TYPE_TEXT;
|
||||
recs[count].typeLen = strlen(NDEF_TYPE_TEXT);
|
||||
recs[count].payload = p_text;
|
||||
recs[count].payloadLen = p_text_len;
|
||||
count++;
|
||||
}
|
||||
|
||||
uint8_t p_aar[sizeof(aar)] = {0};
|
||||
size_t p_aar_len = 0;
|
||||
if (aarlen) {
|
||||
res = NDEFEncodePayloadAAR(aar, p_aar, sizeof(p_aar), &p_aar_len);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to encode Android Application Record");
|
||||
return res;
|
||||
}
|
||||
recs[count].tnf = tnfExternalRecord;
|
||||
recs[count].type = (const uint8_t *)NDEF_ANDROID_AAR;
|
||||
recs[count].typeLen = strlen(NDEF_ANDROID_AAR);
|
||||
recs[count].payload = p_aar;
|
||||
recs[count].payloadLen = p_aar_len;
|
||||
count++;
|
||||
}
|
||||
|
||||
uint8_t msg[MAX_NDEF_LEN] = {0};
|
||||
size_t msglen = 0;
|
||||
res = NDEFEncodeMessage(recs, count, msg, sizeof(msg), &msglen);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to encode NDEF message");
|
||||
return res;
|
||||
}
|
||||
|
||||
uint8_t tlv[MAX_NDEF_LEN] = {0};
|
||||
uint8_t *out = msg;
|
||||
size_t outlen = msglen;
|
||||
|
||||
if (use_tlv) {
|
||||
size_t tlvlen = 0;
|
||||
res = NDEFEncodeTLV(msg, msglen, tlv, sizeof(tlv), &tlvlen);
|
||||
if (res != PM3_SUCCESS) {
|
||||
PrintAndLogEx(ERR, "Failed to wrap NDEF message in a TLV container");
|
||||
return res;
|
||||
}
|
||||
out = tlv;
|
||||
outlen = tlvlen;
|
||||
}
|
||||
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
PrintAndLogEx(INFO, "--- " _CYAN_("NDEF encoding") " ----------------");
|
||||
PrintAndLogEx(SUCCESS, "Records......... " _YELLOW_("%zu"), count);
|
||||
PrintAndLogEx(SUCCESS, "Message size.... " _YELLOW_("%zu") " bytes", msglen);
|
||||
if (use_tlv) {
|
||||
PrintAndLogEx(SUCCESS, "TLV size........ " _YELLOW_("%zu") " bytes", outlen);
|
||||
}
|
||||
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
if (outlen > 64) {
|
||||
print_hex_noascii_break(out, outlen, 32);
|
||||
} else {
|
||||
PrintAndLogEx(SUCCESS, _GREEN_("%s"), sprint_hex_inrow(out, outlen));
|
||||
}
|
||||
|
||||
if (fnlen) {
|
||||
saveFile(filename, ".bin", out, outlen);
|
||||
}
|
||||
|
||||
// round trip it back through the decoder, so the user can see what a reader will see
|
||||
if (use_tlv) {
|
||||
NDEFDecodeAndPrint(out, outlen, verbose);
|
||||
} else {
|
||||
NDEFRecordsDecodeAndPrint(out, outlen, verbose);
|
||||
}
|
||||
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
static int CmdNFCType1Read(const char *Cmd) {
|
||||
return CmdHFTopazInfo(Cmd);
|
||||
}
|
||||
@@ -210,6 +387,10 @@ static int CmdNFCType2Read(const char *Cmd) {
|
||||
return CmdHF14MfuNDEFRead(Cmd);
|
||||
}
|
||||
|
||||
static int CmdNFCType2Write(const char *Cmd) {
|
||||
return CmdHF14MfuNDEFWrite(Cmd);
|
||||
}
|
||||
|
||||
static int CmdNFCType2Help(const char *Cmd);
|
||||
|
||||
static command_t CommandNFCType2Table[] = {
|
||||
@@ -217,7 +398,7 @@ static command_t CommandNFCType2Table[] = {
|
||||
{"--------", CmdNFCType2Help, AlwaysAvailable, "-------------- " _CYAN_("NFC Forum Tag Type 2") " ---------------"},
|
||||
// {"format", CmdNFCType2Format, IfPm3Iso14443a, "format ISO-14443-a tag as NFC Tag"},
|
||||
{"read", CmdNFCType2Read, IfPm3Iso14443a, "read NFC Forum Tag Type 2"},
|
||||
// {"write", CmdNFCType2Write, IfPm3Iso14443a, "write NFC Forum Tag Type 2"},
|
||||
{"write", CmdNFCType2Write, IfPm3Iso14443a, "write NFC Forum Tag Type 2"},
|
||||
{"--------", CmdNFCType2Help, AlwaysAvailable, "--------------------- " _CYAN_("General") " ---------------------"},
|
||||
{"help", CmdNFCType2Help, AlwaysAvailable, "This help"},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
@@ -459,7 +640,7 @@ static command_t CommandTable[] = {
|
||||
{"--------", CmdHelp, AlwaysAvailable, "--------------------- " _CYAN_("General") " ---------------------"},
|
||||
{"help", CmdHelp, AlwaysAvailable, "This help"},
|
||||
{"decode", CmdNfcDecode, AlwaysAvailable, "Decode NDEF records"},
|
||||
// {"encode", CmdNfcEncode, AlwaysAvailable, "Encode NDEF records"},
|
||||
{"encode", CmdNfcEncode, AlwaysAvailable, "Encode NDEF records"},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,245 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// See LICENSE.txt for the text of the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// PACE (Password Authenticated Connection Establishment) primitives for eMRTD
|
||||
//
|
||||
// ICAO Doc 9303 Part 11, section 4.4 and BSI TR-03110 part 3, section 3.2.
|
||||
// Everything in here is pure crypto / parsing so that it can be regression
|
||||
// tested offline by `hf emrtd test` without a card on the reader.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EMRTD_PACE_H__
|
||||
#define EMRTD_PACE_H__
|
||||
|
||||
#include "common.h"
|
||||
#include <mbedtls/ecp.h>
|
||||
|
||||
// Longest OID body (the bytes after the 06 <len> header) we know about
|
||||
#define EMRTD_PACE_OID_MAXLEN 10
|
||||
// AES-256
|
||||
#define EMRTD_SM_MAX_KEY_LEN 32
|
||||
// AES block, and the SSC length for AES secure messaging
|
||||
#define EMRTD_SM_MAX_BLOCK_LEN 16
|
||||
// Uncompressed EC point of the largest curve we support (P-521): 04 || X || Y
|
||||
#define EMRTD_EC_POINT_MAXLEN (1 + (2 * 66))
|
||||
// Largest raw secret fed to the KDF (a P-521 x-coordinate)
|
||||
#define EMRTD_PACE_SECRET_MAXLEN 66
|
||||
// Number of SecurityInfos we keep out of EF_CardAccess
|
||||
#define EMRTD_PACE_MAX_INFOS 16
|
||||
|
||||
typedef enum {
|
||||
EMRTD_PACE_KA_DH = 0,
|
||||
EMRTD_PACE_KA_ECDH,
|
||||
} emrtd_pace_ka_t;
|
||||
|
||||
typedef enum {
|
||||
EMRTD_PACE_MAP_GM = 0, // Generic Mapping
|
||||
EMRTD_PACE_MAP_IM, // Integrated Mapping
|
||||
EMRTD_PACE_MAP_CAM, // Chip Authentication Mapping
|
||||
} emrtd_pace_map_t;
|
||||
|
||||
typedef enum {
|
||||
EMRTD_PACE_CIPHER_3DES = 0,
|
||||
EMRTD_PACE_CIPHER_AES128,
|
||||
EMRTD_PACE_CIPHER_AES192,
|
||||
EMRTD_PACE_CIPHER_AES256,
|
||||
} emrtd_pace_cipher_t;
|
||||
|
||||
typedef struct emrtd_pacealg_s {
|
||||
const char *name;
|
||||
emrtd_pace_ka_t ka;
|
||||
emrtd_pace_map_t mapping;
|
||||
emrtd_pace_cipher_t cipher;
|
||||
size_t keylen; // 16 / 24 / 32
|
||||
size_t oidlen;
|
||||
const uint8_t oid[EMRTD_PACE_OID_MAXLEN];
|
||||
} emrtd_pacealg_t;
|
||||
|
||||
// Standardized Domain Parameters, TR-03110 part 3, table A.2
|
||||
typedef struct emrtd_pacesdp_s {
|
||||
uint8_t id;
|
||||
const char *name;
|
||||
size_t size;
|
||||
bool is_ec;
|
||||
// MBEDTLS_ECP_DP_NONE when the curve is not compiled into our mbedtls
|
||||
mbedtls_ecp_group_id curve;
|
||||
} emrtd_pacesdp_t;
|
||||
|
||||
// One parsed SecurityInfo out of EF_CardAccess
|
||||
typedef struct {
|
||||
const emrtd_pacealg_t *alg; // NULL when the OID is unknown to us
|
||||
uint8_t oid[EMRTD_PACE_OID_MAXLEN];
|
||||
size_t oidlen;
|
||||
uint32_t version;
|
||||
bool has_param;
|
||||
uint8_t param_id;
|
||||
const emrtd_pacesdp_t *sdp; // NULL when param_id is unknown/absent
|
||||
bool supported;
|
||||
const char *reason; // why not supported, NULL when it is
|
||||
} emrtd_paceinfo_t;
|
||||
|
||||
typedef struct {
|
||||
emrtd_paceinfo_t infos[EMRTD_PACE_MAX_INFOS];
|
||||
size_t count;
|
||||
int best; // index into infos[], -1 when none usable
|
||||
} emrtd_cardaccess_t;
|
||||
|
||||
typedef enum {
|
||||
EMRTD_SM_NONE = 0,
|
||||
EMRTD_SM_3DES,
|
||||
EMRTD_SM_AES,
|
||||
} emrtd_sm_t;
|
||||
|
||||
// State captured during a PACE run with Chip Authentication Mapping, verified
|
||||
// once the LDS applet is readable and EF_DG14 can be fetched.
|
||||
typedef struct {
|
||||
bool negotiated; // PACE-CAM was the chosen protocol
|
||||
mbedtls_ecp_group_id curve;
|
||||
uint8_t enc_data[EMRTD_EC_POINT_MAXLEN]; // DO'8A' as returned by the chip
|
||||
size_t enc_datalen;
|
||||
// our own mapping private key. PACE-CAM reuses it as the terminal side of
|
||||
// an ECDH with the chip's static Chip Authentication key, which is what
|
||||
// makes the chip prove itself without any extra round trip.
|
||||
uint8_t sk_map_ifd[EMRTD_PACE_SECRET_MAXLEN];
|
||||
size_t sk_map_ifdlen;
|
||||
uint8_t sk_dh_ifd[EMRTD_PACE_SECRET_MAXLEN]; // the step 3 key, over G'
|
||||
size_t sk_dh_ifdlen;
|
||||
uint8_t pk_map_ic[EMRTD_EC_POINT_MAXLEN]; // the chip's step 2 mapping key
|
||||
size_t pk_map_iclen;
|
||||
uint8_t pk_dh_ic[EMRTD_EC_POINT_MAXLEN]; // the chip's step 3 agreement key
|
||||
size_t pk_dh_iclen;
|
||||
} emrtd_cam_t;
|
||||
|
||||
typedef struct {
|
||||
emrtd_sm_t type;
|
||||
uint8_t ks_enc[EMRTD_SM_MAX_KEY_LEN];
|
||||
uint8_t ks_mac[EMRTD_SM_MAX_KEY_LEN];
|
||||
size_t keylen;
|
||||
uint8_t ssc[EMRTD_SM_MAX_BLOCK_LEN];
|
||||
size_t ssclen; // 8 for 3DES, 16 for AES
|
||||
bool pace; // session was established with PACE
|
||||
const char *pace_alg; // algorithm name, for reporting
|
||||
const char *pace_curve; // domain parameter name, for reporting
|
||||
emrtd_cam_t cam;
|
||||
} emrtd_session_t;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Shared 3DES / ISO 9797-1 helpers (also used by the BAC path)
|
||||
//-----------------------------------------------------------------------------
|
||||
void emrtd_des3_encrypt_cbc(uint8_t *iv, const uint8_t *key, const uint8_t *input, size_t inputlen, uint8_t *output);
|
||||
void emrtd_des3_decrypt_cbc(uint8_t *iv, const uint8_t *key, const uint8_t *input, size_t inputlen, uint8_t *output);
|
||||
|
||||
// ISO 9797-1 padding method 2
|
||||
size_t emrtd_pad_block(const uint8_t *input, size_t inputlen, size_t blocksize, uint8_t *output);
|
||||
|
||||
// ISO 9797-1 MAC algorithm 3 with padding method 2 ("retail MAC")
|
||||
int emrtd_retail_mac(const uint8_t *key, const uint8_t *input, size_t inputlen, uint8_t *output);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Table lookups
|
||||
//-----------------------------------------------------------------------------
|
||||
const emrtd_pacealg_t *emrtd_pace_alg_by_oid(const uint8_t *oid, size_t oidlen);
|
||||
const emrtd_pacesdp_t *emrtd_pace_sdp_by_id(uint8_t id);
|
||||
const char *emrtd_pace_cipher_name(emrtd_pace_cipher_t cipher);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Secure messaging session
|
||||
//-----------------------------------------------------------------------------
|
||||
void emrtd_sm_clear(emrtd_session_t *ssn);
|
||||
int emrtd_sm_setup(emrtd_session_t *ssn, emrtd_pace_cipher_t cipher, const uint8_t *ks_enc, const uint8_t *ks_mac);
|
||||
size_t emrtd_sm_blocksize(const emrtd_session_t *ssn);
|
||||
void emrtd_sm_bump_ssc(emrtd_session_t *ssn);
|
||||
int emrtd_sm_mac(const emrtd_session_t *ssn, const uint8_t *input, size_t inputlen, uint8_t *mac);
|
||||
int emrtd_sm_encrypt(const emrtd_session_t *ssn, const uint8_t *input, size_t inputlen, uint8_t *output);
|
||||
int emrtd_sm_decrypt(const emrtd_session_t *ssn, const uint8_t *input, size_t inputlen, uint8_t *output);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// BER-TLV
|
||||
//-----------------------------------------------------------------------------
|
||||
// Reads one TLV starting at *cur. On success *cur is advanced past it.
|
||||
bool emrtd_tlv_next(const uint8_t **cur, const uint8_t *end, uint32_t *tag, const uint8_t **value, size_t *valuelen);
|
||||
// Writes tag (1 or 2 bytes, big endian) + length, returns bytes written, 0 on overflow
|
||||
size_t emrtd_tlv_write_header(uint8_t *out, size_t outlen, uint32_t tag, size_t len);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// EF_CardAccess
|
||||
//-----------------------------------------------------------------------------
|
||||
int emrtd_pace_parse_cardaccess(const uint8_t *data, size_t datalen, emrtd_cardaccess_t *out);
|
||||
// Higher is better, 0 means unusable. Used to order the algorithms we try.
|
||||
int emrtd_pace_rank(const emrtd_paceinfo_t *info);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// PACE key derivation
|
||||
//-----------------------------------------------------------------------------
|
||||
// MRZ check digit, ICAO 9303-3 4.9. Returns the digit as a value 0-9, not ASCII.
|
||||
char emrtd_calculate_check_digit(const char *data);
|
||||
|
||||
// The MRZ information string that BAC and PACE both hash: docnr||cd||dob||cd||exp||cd
|
||||
int emrtd_pace_kmrz(const char *documentnumber, const char *dob, const char *expiry, char *out, size_t outlen);
|
||||
// K for the MRZ password: SHA-1 of the above
|
||||
int emrtd_pace_password_mrz(const char *documentnumber, const char *dob, const char *expiry, uint8_t *k, size_t *klen);
|
||||
// K for the CAN password: the CAN as raw ASCII
|
||||
int emrtd_pace_password_can(const char *can, uint8_t *k, size_t *klen);
|
||||
// TR-03110 part 3, A.2.3
|
||||
int emrtd_pace_kdf(emrtd_pace_cipher_t cipher, const uint8_t *k, size_t klen, uint32_t counter, uint8_t *out, size_t *outlen);
|
||||
|
||||
// s = D(Kpi, z), zero IV
|
||||
int emrtd_pace_decrypt_nonce(emrtd_pace_cipher_t cipher, const uint8_t *kpi, const uint8_t *z, size_t zlen, uint8_t *s);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// ECDH with Generic Mapping
|
||||
//-----------------------------------------------------------------------------
|
||||
size_t emrtd_pace_ec_pointlen(mbedtls_ecp_group_id curve);
|
||||
|
||||
// Public key for a caller supplied private scalar over a caller supplied base point.
|
||||
// base may be NULL to use the nominal generator of the curve.
|
||||
int emrtd_pace_ec_pubkey(mbedtls_ecp_group_id curve, const uint8_t *base, size_t baselen,
|
||||
const uint8_t *priv, size_t privlen, uint8_t *pub, size_t *publen);
|
||||
|
||||
// Fresh ephemeral keypair over base (NULL == nominal generator)
|
||||
int emrtd_pace_ec_keygen(mbedtls_ecp_group_id curve, const uint8_t *base, size_t baselen,
|
||||
uint8_t *priv, size_t *privlen, uint8_t *pub, size_t *publen);
|
||||
|
||||
// G' = s*G + (priv * pk_ic)
|
||||
int emrtd_pace_ec_gm(mbedtls_ecp_group_id curve, const uint8_t *s, size_t slen,
|
||||
const uint8_t *priv, size_t privlen, const uint8_t *pk_ic, size_t pk_ic_len,
|
||||
uint8_t *mapped, size_t *mappedlen);
|
||||
|
||||
// x-coordinate of priv * pk_ic
|
||||
int emrtd_pace_ec_shared_x(mbedtls_ecp_group_id curve, const uint8_t *priv, size_t privlen,
|
||||
const uint8_t *pk_ic, size_t pk_ic_len, uint8_t *out, size_t *outlen);
|
||||
|
||||
// T = MAC(ks_mac, 7F49 || 06 <oid> || 86 <pubkey>) truncated to 8 bytes
|
||||
int emrtd_pace_token(const emrtd_pacealg_t *alg, const uint8_t *ks_mac,
|
||||
const uint8_t *pubkey, size_t pubkeylen, uint8_t *token);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Chip Authentication Mapping
|
||||
//-----------------------------------------------------------------------------
|
||||
// Pulls the index'th EC ChipAuthenticationPublicKey out of a SET of
|
||||
// SecurityInfos, i.e. out of the body of EF_DG14 or the eContent of
|
||||
// EF_CardSecurity. A document may publish more than one.
|
||||
int emrtd_pace_find_ca_pubkey(const uint8_t *data, size_t datalen, size_t index, uint8_t *pub, size_t *publen);
|
||||
|
||||
// Prints everything a failed CAM check depends on, so it can be reported
|
||||
void emrtd_pace_cam_dump(const emrtd_session_t *ssn, const uint8_t *pk_icc, size_t pk_icc_len);
|
||||
|
||||
// Recovers the Chip Authentication Data out of the DO'8A' the chip returns and
|
||||
// checks it against the ECDH the terminal can do for itself. Returns
|
||||
// PM3_SUCCESS when the document has proven it holds the CA private key.
|
||||
int emrtd_pace_cam_verify(const emrtd_session_t *ssn, const uint8_t *pk_icc, size_t pk_icc_len,
|
||||
const char **mode);
|
||||
|
||||
#endif /* emrtd_pace.h */
|
||||
@@ -0,0 +1,682 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// See LICENSE.txt for the text of the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Offline regression tests for the eMRTD PACE / secure messaging primitives
|
||||
//
|
||||
// Unless noted otherwise every vector below is the worked example from
|
||||
// ICAO Doc 9303 Part 11 (7th edition) Appendix G, "PACE-ECDH-GM-AES-CMAC-128
|
||||
// with BrainpoolP256r1", which is the same example as BSI TR-SAC 1.01
|
||||
// Appendix D.3. The BAC / 3DES secure messaging vectors are from
|
||||
// ICAO Doc 9303 Part 11 Appendix D.4.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "emrtd_pacetest.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "emrtd/emrtd_pace.h"
|
||||
#include "crypto/libpcrypto.h"
|
||||
#include "ui.h"
|
||||
#include "util.h"
|
||||
|
||||
static bool hexeq(const uint8_t *data, size_t datalen, const char *expected) {
|
||||
char buf[512] = { 0x00 };
|
||||
|
||||
if ((datalen * 2) + 1 > sizeof(buf)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < datalen; i++) {
|
||||
snprintf(buf + (i * 2), 3, "%02X", data[i]);
|
||||
}
|
||||
|
||||
return (strlen(expected) == (datalen * 2)) && (strcmp(buf, expected) == 0);
|
||||
}
|
||||
|
||||
static size_t unhex(const char *in, uint8_t *out, size_t outlen) {
|
||||
size_t len = strlen(in) / 2;
|
||||
if (len > outlen) {
|
||||
return 0;
|
||||
}
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
unsigned int v = 0;
|
||||
if (sscanf(in + (i * 2), "%2x", &v) != 1) {
|
||||
return 0;
|
||||
}
|
||||
out[i] = (uint8_t)v;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
// ICAO 9303-11 Appendix G, the MRZ of the sample document
|
||||
#define ICAO_DOCNR "T22000129"
|
||||
#define ICAO_DOB "640812"
|
||||
#define ICAO_EXPIRY "101031"
|
||||
#define ICAO_KMRZ "T22000129364081251010318"
|
||||
#define ICAO_K "7E2D2A41C74EA0B38CD36F863939BFA8E9032AAD"
|
||||
|
||||
static bool test_mrz_password(void) {
|
||||
char kmrz[25] = { 0x00 };
|
||||
bool res = (emrtd_pace_kmrz(ICAO_DOCNR, ICAO_DOB, ICAO_EXPIRY, kmrz, sizeof(kmrz)) == PM3_SUCCESS);
|
||||
res = res && (strcmp(kmrz, ICAO_KMRZ) == 0);
|
||||
|
||||
uint8_t k[64] = { 0x00 };
|
||||
size_t klen = 0;
|
||||
res = res && (emrtd_pace_password_mrz(ICAO_DOCNR, ICAO_DOB, ICAO_EXPIRY, k, &klen) == PM3_SUCCESS);
|
||||
res = res && (klen == 20) && hexeq(k, klen, ICAO_K);
|
||||
|
||||
// Kpi = KDF(K, 3)
|
||||
uint8_t kpi[32] = { 0x00 };
|
||||
size_t kpilen = 0;
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_AES128, k, klen, 3, kpi, &kpilen) == PM3_SUCCESS);
|
||||
res = res && (kpilen == 16) && hexeq(kpi, kpilen, "89DED1B26624EC1E634C1989302849DD");
|
||||
|
||||
PrintAndLogEx(SUCCESS, "MRZ password..... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
static bool test_can_password(void) {
|
||||
uint8_t k[64] = { 0x00 };
|
||||
size_t klen = 0;
|
||||
|
||||
// ICAO 9303-11 9.7.2, the CAN is used as its plain ASCII representation
|
||||
bool res = (emrtd_pace_password_can("123456", k, &klen) == PM3_SUCCESS);
|
||||
res = res && (klen == 6) && hexeq(k, klen, "313233343536");
|
||||
|
||||
uint8_t kpi[32] = { 0x00 };
|
||||
size_t kpilen = 0;
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_AES128, k, klen, 3, kpi, &kpilen) == PM3_SUCCESS);
|
||||
// SHA-1("123456" || 00000003), first 16 bytes
|
||||
res = res && (kpilen == 16) && hexeq(kpi, kpilen, "591468CDA83D65219CCCB8560233600F");
|
||||
|
||||
PrintAndLogEx(SUCCESS, "CAN password..... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
static bool test_kdf(void) {
|
||||
uint8_t k[20] = { 0x00 };
|
||||
unhex(ICAO_K, k, sizeof(k));
|
||||
|
||||
uint8_t out[32] = { 0x00 };
|
||||
size_t outlen = 0;
|
||||
bool res = true;
|
||||
|
||||
// SHA-1 based, 16 byte keys
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_AES128, k, sizeof(k), 1, out, &outlen) == PM3_SUCCESS);
|
||||
res = res && (outlen == 16) && hexeq(out, outlen, "3CC5F8872F8B1471B47EEFDDFFC43F46");
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_AES128, k, sizeof(k), 2, out, &outlen) == PM3_SUCCESS);
|
||||
res = res && hexeq(out, outlen, "BD651C6B2FA9B4704553332007771E84");
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_AES128, k, sizeof(k), 3, out, &outlen) == PM3_SUCCESS);
|
||||
res = res && hexeq(out, outlen, "89DED1B26624EC1E634C1989302849DD");
|
||||
|
||||
// Same hash, but 3DES adds odd parity to both halves (ICAO 9303-11 9.7.1.1)
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_3DES, k, sizeof(k), 3, out, &outlen) == PM3_SUCCESS);
|
||||
res = res && (outlen == 16) && hexeq(out, outlen, "89DFD0B36725EC1F624C1989312949DC");
|
||||
|
||||
// SHA-256 based, truncated to 24 bytes
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_AES192, k, sizeof(k), 1, out, &outlen) == PM3_SUCCESS);
|
||||
res = res && (outlen == 24) && hexeq(out, outlen, "11504483F04A10A66A8FCD6D348C75A961809C54EDAA2117");
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_AES192, k, sizeof(k), 2, out, &outlen) == PM3_SUCCESS);
|
||||
res = res && hexeq(out, outlen, "1F35D576CE54BA775A391F9749E388C48215B7D544D62D96");
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_AES192, k, sizeof(k), 3, out, &outlen) == PM3_SUCCESS);
|
||||
res = res && hexeq(out, outlen, "D79A23C126202AC9051FEBFBC0E8A03B1C6645D85752B4B7");
|
||||
|
||||
// SHA-256 based, full 32 bytes
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_AES256, k, sizeof(k), 1, out, &outlen) == PM3_SUCCESS);
|
||||
res = res && (outlen == 32) && hexeq(out, outlen, "11504483F04A10A66A8FCD6D348C75A961809C54EDAA21174726A97FB37DEF52");
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_AES256, k, sizeof(k), 2, out, &outlen) == PM3_SUCCESS);
|
||||
res = res && hexeq(out, outlen, "1F35D576CE54BA775A391F9749E388C48215B7D544D62D96470ABA7B19F051A3");
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_AES256, k, sizeof(k), 3, out, &outlen) == PM3_SUCCESS);
|
||||
res = res && hexeq(out, outlen, "D79A23C126202AC9051FEBFBC0E8A03B1C6645D85752B4B71408FA229AB6D56B");
|
||||
|
||||
PrintAndLogEx(SUCCESS, "KDF.............. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
static bool test_nonce(void) {
|
||||
uint8_t kpi[16] = { 0x00 };
|
||||
uint8_t z[16] = { 0x00 };
|
||||
uint8_t s[16] = { 0x00 };
|
||||
|
||||
unhex("89DED1B26624EC1E634C1989302849DD", kpi, sizeof(kpi));
|
||||
unhex("95A3A016522EE98D01E76CB6B98B42C3", z, sizeof(z));
|
||||
|
||||
bool res = (emrtd_pace_decrypt_nonce(EMRTD_PACE_CIPHER_AES128, kpi, z, sizeof(z), s) == PM3_SUCCESS);
|
||||
res = res && hexeq(s, sizeof(s), "3F00C4D39D153F2B2A214A078D899B22");
|
||||
|
||||
PrintAndLogEx(SUCCESS, "Nonce decrypt.... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
// ICAO 9303-11 Appendix G, PACE-ECDH-GM-AES-CMAC-128 over BrainpoolP256r1
|
||||
#define ICAO_S "3F00C4D39D153F2B2A214A078D899B22"
|
||||
#define ICAO_SK_MAP_IFD "7F4EF07B9EA82FD78AD689B38D0BC78CF21F249D953BC46F4C6E19259C010F99"
|
||||
#define ICAO_PK_MAP_IFD "047ACF3EFC982EC45565A4B155129EFBC74650DCBFA6362D896FC70262E0C2CC5E544552DCB6725218799115B55C9BAA6D9F6BC3A9618E70C25AF71777A9C4922D"
|
||||
#define ICAO_PK_MAP_IC "04824FBA91C9CBE26BEF53A0EBE7342A3BF178CEA9F45DE0B70AA601651FBA3F5730D8C879AAA9C9F73991E61B58F4D52EB87A0A0C709A49DC63719363CCD13C54"
|
||||
#define ICAO_G_MAPPED "048CED63C91426D4F0EB1435E7CB1D74A46723A0AF21C89634F65A9AE87A9265E28C879506743F8611AC33645C5B985C80B5F09A0B83407C1B6A4D857AE76FE522"
|
||||
#define ICAO_SK_DH_IFD "A73FB703AC1436A18E0CFA5ABB3F7BEC7A070E7A6788486BEE230C4A22762595"
|
||||
#define ICAO_PK_DH_IFD "042DB7A64C0355044EC9DF190514C625CBA2CEA48754887122F3A5EF0D5EDD301C3556F3B3B186DF10B857B58F6A7EB80F20BA5DC7BE1D43D9BF850149FBB36462"
|
||||
#define ICAO_PK_DH_IC "049E880F842905B8B3181F7AF7CAA9F0EFB743847F44A306D2D28C1D9EC65DF6DB7764B22277A2EDDC3C265A9F018F9CB852E111B768B326904B59A0193776F094"
|
||||
#define ICAO_SHARED_X "28768D20701247DAE81804C9E780EDE582A9996DB4A315020B2733197DB84925"
|
||||
#define ICAO_KSENC "F5F0E35C0D7161EE6724EE513A0D9A7F"
|
||||
#define ICAO_KSMAC "FE251C7858B356B24514B3BD5F4297D1"
|
||||
|
||||
static bool test_gm(void) {
|
||||
uint8_t s[16], x1[32], pk_map_ic[65], x2[32], pk_dh_ic[65];
|
||||
uint8_t pub[EMRTD_EC_POINT_MAXLEN] = { 0x00 };
|
||||
uint8_t mapped[EMRTD_EC_POINT_MAXLEN] = { 0x00 };
|
||||
uint8_t shared[EMRTD_PACE_SECRET_MAXLEN] = { 0x00 };
|
||||
size_t publen = 0, mappedlen = 0, sharedlen = 0;
|
||||
|
||||
unhex(ICAO_S, s, sizeof(s));
|
||||
unhex(ICAO_SK_MAP_IFD, x1, sizeof(x1));
|
||||
unhex(ICAO_PK_MAP_IC, pk_map_ic, sizeof(pk_map_ic));
|
||||
unhex(ICAO_SK_DH_IFD, x2, sizeof(x2));
|
||||
unhex(ICAO_PK_DH_IC, pk_dh_ic, sizeof(pk_dh_ic));
|
||||
|
||||
const mbedtls_ecp_group_id curve = MBEDTLS_ECP_DP_BP256R1;
|
||||
|
||||
bool res = (emrtd_pace_ec_pointlen(curve) == 65);
|
||||
|
||||
// PK.Map.IFD = x1 * G
|
||||
res = res && (emrtd_pace_ec_pubkey(curve, NULL, 0, x1, sizeof(x1), pub, &publen) == PM3_SUCCESS);
|
||||
res = res && hexeq(pub, publen, ICAO_PK_MAP_IFD);
|
||||
|
||||
// G' = s*G + x1 * PK.Map.IC
|
||||
res = res && (emrtd_pace_ec_gm(curve, s, sizeof(s), x1, sizeof(x1), pk_map_ic, sizeof(pk_map_ic),
|
||||
mapped, &mappedlen) == PM3_SUCCESS);
|
||||
res = res && hexeq(mapped, mappedlen, ICAO_G_MAPPED);
|
||||
|
||||
// PK.DH.IFD = x2 * G'
|
||||
res = res && (emrtd_pace_ec_pubkey(curve, mapped, mappedlen, x2, sizeof(x2), pub, &publen) == PM3_SUCCESS);
|
||||
res = res && hexeq(pub, publen, ICAO_PK_DH_IFD);
|
||||
|
||||
// K = x-coordinate of x2 * PK.DH.IC
|
||||
res = res && (emrtd_pace_ec_shared_x(curve, x2, sizeof(x2), pk_dh_ic, sizeof(pk_dh_ic),
|
||||
shared, &sharedlen) == PM3_SUCCESS);
|
||||
res = res && hexeq(shared, sharedlen, ICAO_SHARED_X);
|
||||
|
||||
// KSenc = KDF(K, 1), KSmac = KDF(K, 2)
|
||||
uint8_t ks[32] = { 0x00 };
|
||||
size_t kslen = 0;
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_AES128, shared, sharedlen, 1, ks, &kslen) == PM3_SUCCESS);
|
||||
res = res && hexeq(ks, kslen, ICAO_KSENC);
|
||||
res = res && (emrtd_pace_kdf(EMRTD_PACE_CIPHER_AES128, shared, sharedlen, 2, ks, &kslen) == PM3_SUCCESS);
|
||||
res = res && hexeq(ks, kslen, ICAO_KSMAC);
|
||||
|
||||
PrintAndLogEx(SUCCESS, "ECDH GM mapping.. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
static bool test_ec_keygen(void) {
|
||||
// Not a published vector, just a sanity check that the ephemeral key we
|
||||
// generate is a real key on the mapped generator and is not all zeroes.
|
||||
uint8_t mapped[EMRTD_EC_POINT_MAXLEN] = { 0x00 };
|
||||
size_t mappedlen = unhex(ICAO_G_MAPPED, mapped, sizeof(mapped));
|
||||
|
||||
uint8_t priv[EMRTD_PACE_SECRET_MAXLEN] = { 0x00 };
|
||||
uint8_t pub[EMRTD_EC_POINT_MAXLEN] = { 0x00 };
|
||||
uint8_t check[EMRTD_EC_POINT_MAXLEN] = { 0x00 };
|
||||
size_t privlen = 0, publen = 0, checklen = 0;
|
||||
|
||||
bool res = (emrtd_pace_ec_keygen(MBEDTLS_ECP_DP_BP256R1, mapped, mappedlen,
|
||||
priv, &privlen, pub, &publen) == PM3_SUCCESS);
|
||||
res = res && (privlen == 32) && (publen == 65) && (pub[0] == 0x04);
|
||||
|
||||
uint8_t zeroes[EMRTD_PACE_SECRET_MAXLEN] = { 0x00 };
|
||||
res = res && (memcmp(priv, zeroes, privlen) != 0);
|
||||
|
||||
// the generated public key has to be priv * G'
|
||||
res = res && (emrtd_pace_ec_pubkey(MBEDTLS_ECP_DP_BP256R1, mapped, mappedlen,
|
||||
priv, privlen, check, &checklen) == PM3_SUCCESS);
|
||||
res = res && (checklen == publen) && (memcmp(check, pub, publen) == 0);
|
||||
|
||||
PrintAndLogEx(SUCCESS, "Ephemeral keygen. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
static bool test_token(void) {
|
||||
uint8_t ks_mac[16] = { 0x00 };
|
||||
uint8_t pk_dh_ic[65] = { 0x00 };
|
||||
uint8_t pk_dh_ifd[65] = { 0x00 };
|
||||
uint8_t token[8] = { 0x00 };
|
||||
|
||||
unhex(ICAO_KSMAC, ks_mac, sizeof(ks_mac));
|
||||
unhex(ICAO_PK_DH_IC, pk_dh_ic, sizeof(pk_dh_ic));
|
||||
unhex(ICAO_PK_DH_IFD, pk_dh_ifd, sizeof(pk_dh_ifd));
|
||||
|
||||
const uint8_t oid[10] = {0x04, 0x00, 0x7F, 0x00, 0x07, 0x02, 0x02, 0x04, 0x02, 0x02};
|
||||
const emrtd_pacealg_t *alg = emrtd_pace_alg_by_oid(oid, sizeof(oid));
|
||||
|
||||
bool res = (alg != NULL);
|
||||
res = res && (alg->cipher == EMRTD_PACE_CIPHER_AES128);
|
||||
res = res && (alg->mapping == EMRTD_PACE_MAP_GM);
|
||||
res = res && (alg->ka == EMRTD_PACE_KA_ECDH);
|
||||
|
||||
// T_IFD is computed over the chip's key
|
||||
res = res && (emrtd_pace_token(alg, ks_mac, pk_dh_ic, sizeof(pk_dh_ic), token) == PM3_SUCCESS);
|
||||
res = res && hexeq(token, sizeof(token), "C2B0BD78D94BA866");
|
||||
|
||||
// T_IC is computed over our key
|
||||
res = res && (emrtd_pace_token(alg, ks_mac, pk_dh_ifd, sizeof(pk_dh_ifd), token) == PM3_SUCCESS);
|
||||
res = res && hexeq(token, sizeof(token), "3ABB9674BCE93C08");
|
||||
|
||||
PrintAndLogEx(SUCCESS, "Auth tokens...... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
static bool test_ssc(void) {
|
||||
emrtd_session_t ssn;
|
||||
uint8_t ks[32] = { 0x00 };
|
||||
|
||||
emrtd_sm_clear(&ssn);
|
||||
bool res = (emrtd_sm_setup(&ssn, EMRTD_PACE_CIPHER_AES128, ks, ks) == PM3_SUCCESS);
|
||||
res = res && (ssn.ssclen == 16) && (emrtd_sm_blocksize(&ssn) == 16);
|
||||
|
||||
emrtd_sm_bump_ssc(&ssn);
|
||||
res = res && hexeq(ssn.ssc, ssn.ssclen, "00000000000000000000000000000001");
|
||||
|
||||
// carry across the whole SSC
|
||||
memset(ssn.ssc, 0xFF, ssn.ssclen);
|
||||
ssn.ssc[ssn.ssclen - 1] = 0xFF;
|
||||
emrtd_sm_bump_ssc(&ssn);
|
||||
res = res && hexeq(ssn.ssc, ssn.ssclen, "00000000000000000000000000000000");
|
||||
|
||||
memset(ssn.ssc, 0x00, ssn.ssclen);
|
||||
ssn.ssc[ssn.ssclen - 1] = 0xFF;
|
||||
emrtd_sm_bump_ssc(&ssn);
|
||||
res = res && hexeq(ssn.ssc, ssn.ssclen, "00000000000000000000000000000100");
|
||||
|
||||
// 3DES sessions carry an 8 byte SSC
|
||||
emrtd_sm_clear(&ssn);
|
||||
res = res && (emrtd_sm_setup(&ssn, EMRTD_PACE_CIPHER_3DES, ks, ks) == PM3_SUCCESS);
|
||||
res = res && (ssn.ssclen == 8) && (emrtd_sm_blocksize(&ssn) == 8);
|
||||
unhex("887022120C06C2FF", ssn.ssc, sizeof(ssn.ssc));
|
||||
emrtd_sm_bump_ssc(&ssn);
|
||||
res = res && hexeq(ssn.ssc, ssn.ssclen, "887022120C06C300");
|
||||
|
||||
PrintAndLogEx(SUCCESS, "SSC increment.... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
static bool test_padding(void) {
|
||||
uint8_t out[32] = { 0x00 };
|
||||
|
||||
bool res = (emrtd_pad_block((const uint8_t *)"\x01\x1E", 2, 8, out) == 8);
|
||||
res = res && hexeq(out, 8, "011E800000000000");
|
||||
|
||||
res = res && (emrtd_pad_block((const uint8_t *)"\x01\x1E", 2, 16, out) == 16);
|
||||
res = res && hexeq(out, 16, "011E8000000000000000000000000000");
|
||||
|
||||
// a full block always gets a whole block of padding added
|
||||
res = res && (emrtd_pad_block((const uint8_t *)"\x0C\xA4\x02\x0C", 4, 8, out) == 8);
|
||||
res = res && hexeq(out, 8, "0CA4020C80000000");
|
||||
|
||||
uint8_t full[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
|
||||
res = res && (emrtd_pad_block(full, 8, 8, out) == 16);
|
||||
res = res && hexeq(out, 16, "11223344556677888000000000000000");
|
||||
|
||||
PrintAndLogEx(SUCCESS, "ISO 9797-1 pad... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
// ICAO 9303-11 Appendix D.4, the BAC secure messaging worked example.
|
||||
// This is the regression test for the 3DES path, it has to keep passing
|
||||
// byte for byte after any refactor of the session handling.
|
||||
static bool test_sm_3des(void) {
|
||||
emrtd_session_t ssn;
|
||||
uint8_t ks_enc[16], ks_mac[16];
|
||||
|
||||
unhex("979EC13B1CBFE9DCD01AB0FED307EAE5", ks_enc, sizeof(ks_enc));
|
||||
unhex("F1CB1F1FB5ADF208806B89DC579DC1F8", ks_mac, sizeof(ks_mac));
|
||||
|
||||
emrtd_sm_clear(&ssn);
|
||||
bool res = (emrtd_sm_setup(&ssn, EMRTD_PACE_CIPHER_3DES, ks_enc, ks_mac) == PM3_SUCCESS);
|
||||
unhex("887022120C06C226", ssn.ssc, sizeof(ssn.ssc));
|
||||
|
||||
// SELECT EF.COM, protected
|
||||
emrtd_sm_bump_ssc(&ssn);
|
||||
res = res && hexeq(ssn.ssc, ssn.ssclen, "887022120C06C227");
|
||||
|
||||
uint8_t pt[16] = { 0x00 };
|
||||
uint8_t ct[16] = { 0x00 };
|
||||
size_t ptlen = emrtd_pad_block((const uint8_t *)"\x01\x1E", 2, 8, pt);
|
||||
res = res && (emrtd_sm_encrypt(&ssn, pt, ptlen, ct) == PM3_SUCCESS);
|
||||
res = res && hexeq(ct, ptlen, "6375432908C044F6");
|
||||
|
||||
uint8_t n[64] = { 0x00 };
|
||||
size_t nlen = 0;
|
||||
memcpy(n, ssn.ssc, ssn.ssclen);
|
||||
nlen += ssn.ssclen;
|
||||
nlen += emrtd_pad_block((const uint8_t *)"\x0C\xA4\x02\x0C", 4, 8, n + nlen);
|
||||
memcpy(n + nlen, "\x87\x09\x01", 3);
|
||||
nlen += 3;
|
||||
memcpy(n + nlen, ct, ptlen);
|
||||
nlen += ptlen;
|
||||
|
||||
uint8_t cc[8] = { 0x00 };
|
||||
res = res && (emrtd_sm_mac(&ssn, n, nlen, cc) == PM3_SUCCESS);
|
||||
res = res && hexeq(cc, sizeof(cc), "BF8B92D635FF24F8");
|
||||
|
||||
// Response 99 02 90 00 8E 08 FA855A5D4C50A8ED
|
||||
emrtd_sm_bump_ssc(&ssn);
|
||||
res = res && hexeq(ssn.ssc, ssn.ssclen, "887022120C06C228");
|
||||
|
||||
nlen = 0;
|
||||
memcpy(n, ssn.ssc, ssn.ssclen);
|
||||
nlen += ssn.ssclen;
|
||||
memcpy(n + nlen, "\x99\x02\x90\x00", 4);
|
||||
nlen += 4;
|
||||
res = res && (emrtd_sm_mac(&ssn, n, nlen, cc) == PM3_SUCCESS);
|
||||
res = res && hexeq(cc, sizeof(cc), "FA855A5D4C50A8ED");
|
||||
|
||||
PrintAndLogEx(SUCCESS, "3DES SM.......... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
// AES secure messaging, ICAO 9303-11 9.8.6.
|
||||
// The session keys are the ones the PACE example above produces. ICAO publishes
|
||||
// no AES secure messaging trace, so the protected APDUs below were computed
|
||||
// independently (OpenSSL / PyCryptodome) from those keys. The one judgement
|
||||
// call they encode is that step e.2, "concatenate SSC and M and add padding",
|
||||
// applies to AES as well as to 3DES; that was confirmed on a document doing
|
||||
// PACE with AES-CMAC-256, which answers 6988 to an unpadded MAC input.
|
||||
static bool test_sm_aes(void) {
|
||||
emrtd_session_t ssn;
|
||||
uint8_t ks_enc[16], ks_mac[16];
|
||||
|
||||
unhex(ICAO_KSENC, ks_enc, sizeof(ks_enc));
|
||||
unhex(ICAO_KSMAC, ks_mac, sizeof(ks_mac));
|
||||
|
||||
emrtd_sm_clear(&ssn);
|
||||
bool res = (emrtd_sm_setup(&ssn, EMRTD_PACE_CIPHER_AES128, ks_enc, ks_mac) == PM3_SUCCESS);
|
||||
// SSC starts at zero after PACE
|
||||
res = res && hexeq(ssn.ssc, ssn.ssclen, "00000000000000000000000000000000");
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// SELECT EF.COM -> 0CA4020C1D 871101<cryptogram> 8E08<cc> 00
|
||||
//---------------------------------------------------------------------
|
||||
emrtd_sm_bump_ssc(&ssn);
|
||||
|
||||
uint8_t pt[32] = { 0x00 };
|
||||
uint8_t ct[32] = { 0x00 };
|
||||
size_t ptlen = emrtd_pad_block((const uint8_t *)"\x01\x1E", 2, 16, pt);
|
||||
res = res && (emrtd_sm_encrypt(&ssn, pt, ptlen, ct) == PM3_SUCCESS);
|
||||
res = res && hexeq(ct, ptlen, "EE0E4724F4465C1BE9C2F73ABDD73A3D");
|
||||
|
||||
uint8_t n[128] = { 0x00 };
|
||||
size_t nlen = 0;
|
||||
memcpy(n, ssn.ssc, ssn.ssclen);
|
||||
nlen += ssn.ssclen;
|
||||
nlen += emrtd_pad_block((const uint8_t *)"\x0C\xA4\x02\x0C", 4, 16, n + nlen);
|
||||
memcpy(n + nlen, "\x87\x11\x01", 3);
|
||||
nlen += 3;
|
||||
memcpy(n + nlen, ct, ptlen);
|
||||
nlen += ptlen;
|
||||
|
||||
uint8_t cc[8] = { 0x00 };
|
||||
res = res && (emrtd_sm_mac(&ssn, n, nlen, cc) == PM3_SUCCESS);
|
||||
res = res && hexeq(cc, sizeof(cc), "835D1B54575C955F");
|
||||
|
||||
// response 99 02 90 00 8E 08 <cc>
|
||||
emrtd_sm_bump_ssc(&ssn);
|
||||
nlen = 0;
|
||||
memcpy(n, ssn.ssc, ssn.ssclen);
|
||||
nlen += ssn.ssclen;
|
||||
memcpy(n + nlen, "\x99\x02\x90\x00", 4);
|
||||
nlen += 4;
|
||||
res = res && (emrtd_sm_mac(&ssn, n, nlen, cc) == PM3_SUCCESS);
|
||||
res = res && hexeq(cc, sizeof(cc), "BEA7B381C494A079");
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// READ BINARY offset 0, 4 bytes -> 0CB000000D 970104 8E08<cc> 00
|
||||
//---------------------------------------------------------------------
|
||||
emrtd_sm_bump_ssc(&ssn);
|
||||
nlen = 0;
|
||||
memcpy(n, ssn.ssc, ssn.ssclen);
|
||||
nlen += ssn.ssclen;
|
||||
nlen += emrtd_pad_block((const uint8_t *)"\x0C\xB0\x00\x00", 4, 16, n + nlen);
|
||||
memcpy(n + nlen, "\x97\x01\x04", 3);
|
||||
nlen += 3;
|
||||
res = res && (emrtd_sm_mac(&ssn, n, nlen, cc) == PM3_SUCCESS);
|
||||
res = res && hexeq(cc, sizeof(cc), "AA6BA54F44DF8364");
|
||||
|
||||
// response 87 11 01 <cryptogram of 60145F01> 99 02 90 00 8E 08 <cc>
|
||||
emrtd_sm_bump_ssc(&ssn);
|
||||
|
||||
uint8_t rct[16] = { 0x00 };
|
||||
unhex("645C0B1F998A088278D07942BBE94B60", rct, sizeof(rct));
|
||||
|
||||
nlen = 0;
|
||||
memcpy(n, ssn.ssc, ssn.ssclen);
|
||||
nlen += ssn.ssclen;
|
||||
memcpy(n + nlen, "\x87\x11\x01", 3);
|
||||
nlen += 3;
|
||||
memcpy(n + nlen, rct, sizeof(rct));
|
||||
nlen += sizeof(rct);
|
||||
memcpy(n + nlen, "\x99\x02\x90\x00", 4);
|
||||
nlen += 4;
|
||||
res = res && (emrtd_sm_mac(&ssn, n, nlen, cc) == PM3_SUCCESS);
|
||||
res = res && hexeq(cc, sizeof(cc), "94A33C6AA2D8CCC8");
|
||||
|
||||
// and the response data has to decrypt back to the start of EF_COM
|
||||
uint8_t plain[16] = { 0x00 };
|
||||
res = res && (emrtd_sm_decrypt(&ssn, rct, sizeof(rct), plain) == PM3_SUCCESS);
|
||||
res = res && hexeq(plain, sizeof(plain), "60145F01800000000000000000000000");
|
||||
res = res && hexeq(plain, 4, "60145F01");
|
||||
|
||||
PrintAndLogEx(SUCCESS, "AES SM........... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
static bool test_cardaccess(void) {
|
||||
// A SET of four PACEInfos:
|
||||
// ECDH-GM-AES-CMAC-128 / BrainpoolP256r1 (supported)
|
||||
// ECDH-GM-AES-CMAC-256 / BrainpoolP384r1 (supported)
|
||||
// DH-GM-AES-CMAC-128 / 1024-bit MODP (not supported, no DHM)
|
||||
// ECDH-CAM-AES-CMAC-256 / BrainpoolP384r1 (supported, but ranked below
|
||||
// GM while its proof cannot
|
||||
// be checked)
|
||||
const char *blob =
|
||||
"3150"
|
||||
"3012" "060A04007F0007020204020202010202010D"
|
||||
"3012" "060A04007F00070202040204020102020110"
|
||||
"3012" "060A04007F00070202040101020102020100"
|
||||
"3012" "060A04007F00070202040604020102020110";
|
||||
|
||||
uint8_t data[128] = { 0x00 };
|
||||
size_t datalen = unhex(blob, data, sizeof(data));
|
||||
|
||||
emrtd_cardaccess_t ca;
|
||||
bool res = (datalen > 0);
|
||||
res = res && (emrtd_pace_parse_cardaccess(data, datalen, &ca) == PM3_SUCCESS);
|
||||
res = res && (ca.count == 4);
|
||||
// GM wins ties over CAM for now, see emrtd_pace_rank()
|
||||
res = res && (ca.best == 1);
|
||||
res = res && (ca.infos[3].supported);
|
||||
res = res && (ca.infos[3].alg != NULL) && (ca.infos[3].alg->mapping == EMRTD_PACE_MAP_CAM);
|
||||
res = res && (emrtd_pace_rank(&ca.infos[3]) < emrtd_pace_rank(&ca.infos[1]));
|
||||
// but CAM still has to be usable when it is all a document offers
|
||||
res = res && (emrtd_pace_rank(&ca.infos[3]) > 0);
|
||||
|
||||
res = res && (ca.infos[0].supported) && (ca.infos[0].version == 2);
|
||||
res = res && (ca.infos[0].alg != NULL) && (ca.infos[0].alg->cipher == EMRTD_PACE_CIPHER_AES128);
|
||||
res = res && (ca.infos[0].sdp != NULL) && (ca.infos[0].sdp->curve == MBEDTLS_ECP_DP_BP256R1);
|
||||
|
||||
res = res && (ca.infos[1].supported);
|
||||
res = res && (ca.infos[1].alg != NULL) && (ca.infos[1].alg->cipher == EMRTD_PACE_CIPHER_AES256);
|
||||
res = res && (ca.infos[1].sdp != NULL) && (ca.infos[1].sdp->curve == MBEDTLS_ECP_DP_BP384R1);
|
||||
|
||||
res = res && (ca.infos[2].supported == false) && (ca.infos[2].reason != NULL);
|
||||
|
||||
// curves that this tree's mbedtls does not carry must never resolve
|
||||
const emrtd_pacesdp_t *sdp = emrtd_pace_sdp_by_id(14); // BrainpoolP320r1
|
||||
res = res && (sdp != NULL) && (sdp->curve == MBEDTLS_ECP_DP_NONE);
|
||||
sdp = emrtd_pace_sdp_by_id(9); // BrainpoolP192r1
|
||||
res = res && (sdp != NULL) && (sdp->curve == MBEDTLS_ECP_DP_NONE);
|
||||
sdp = emrtd_pace_sdp_by_id(12); // NIST P-256
|
||||
res = res && (sdp != NULL) && (sdp->curve == MBEDTLS_ECP_DP_SECP256R1);
|
||||
|
||||
// truncated input must be rejected, not walked off the end of the buffer
|
||||
emrtd_cardaccess_t bad;
|
||||
for (size_t cut = 1; cut < datalen; cut++) {
|
||||
if (emrtd_pace_parse_cardaccess(data, cut, &bad) == PM3_SUCCESS) {
|
||||
// a shorter but still well formed prefix is only possible if it
|
||||
// parses to fewer entries than the full blob
|
||||
if (bad.count >= ca.count) {
|
||||
res = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PrintAndLogEx(SUCCESS, "EF_CardAccess.... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
// Chip Authentication Mapping.
|
||||
//
|
||||
// The chip sends t = SK.Map.IC * SK.CA.IC^-1 mod n encrypted with KSenc, so
|
||||
// that t * PK.CA.IC reproduces the ephemeral PACE key it used. Only a chip
|
||||
// holding the static Chip Authentication private key can produce such a t.
|
||||
// See US 9215230 (Kuegler / Bender) and TR-03110 part 3.
|
||||
//
|
||||
// No published vector exists, so this one is constructed the way a genuine
|
||||
// chip would: t is derived from a chosen static key and mapping key, padded
|
||||
// and encrypted. t is 32 bytes here and DO'8A' is 48, mirroring a real
|
||||
// BrainpoolP384r1 document where a 48 byte t arrives as 64 bytes.
|
||||
#define CAM_PK_ICC "048DF71464547FC82DCF1650BBC5B72DCD2CC3D9A71283F6E374C0958F316026DB0E1F619B0FBBD54B130E9D04B5201A0522D4CC199888B6D76C9A22BDD79F2D45"
|
||||
#define CAM_PK_MAP_IC "041D840F08CE75DDA65326277A850039892B9A399614221865EDC0DCD27DE1B4EEA5E8B3CB9560F08D09DDB3B4DE4D936FF1311A9F6D20047E94B3E458BCA5778B"
|
||||
#define CAM_KSENC "0102030405060708090A0B0C0D0E0F10"
|
||||
#define CAM_DO8A "25887095A78FA691E074800EF9B097F96EBFA89ED8350BCAEDFD309DE91878ADCE249F1253D13483C441B1A08DFD74DE"
|
||||
|
||||
static bool test_cam(void) {
|
||||
uint8_t pk_icc[65], ks[16];
|
||||
|
||||
size_t pk_icclen = unhex(CAM_PK_ICC, pk_icc, sizeof(pk_icc));
|
||||
unhex(CAM_KSENC, ks, sizeof(ks));
|
||||
|
||||
emrtd_session_t ssn;
|
||||
emrtd_sm_clear(&ssn);
|
||||
bool res = (emrtd_sm_setup(&ssn, EMRTD_PACE_CIPHER_AES128, ks, ks) == PM3_SUCCESS);
|
||||
|
||||
ssn.cam.negotiated = true;
|
||||
ssn.cam.curve = MBEDTLS_ECP_DP_BP256R1;
|
||||
ssn.cam.pk_map_iclen = unhex(CAM_PK_MAP_IC, ssn.cam.pk_map_ic, sizeof(ssn.cam.pk_map_ic));
|
||||
ssn.cam.enc_datalen = unhex(CAM_DO8A, ssn.cam.enc_data, sizeof(ssn.cam.enc_data));
|
||||
res = res && (ssn.cam.enc_datalen == 48);
|
||||
|
||||
const char *mode = NULL;
|
||||
res = res && (emrtd_pace_cam_verify(&ssn, pk_icc, pk_icclen, &mode) == PM3_SUCCESS);
|
||||
res = res && (mode != NULL) && (strstr(mode, "PK.Map.IC") != NULL);
|
||||
|
||||
// a chip that returns the wrong blob must not verify under any encoding
|
||||
ssn.cam.enc_data[0] ^= 0x01;
|
||||
res = res && (emrtd_pace_cam_verify(&ssn, pk_icc, pk_icclen, &mode) != PM3_SUCCESS);
|
||||
ssn.cam.enc_data[0] ^= 0x01;
|
||||
|
||||
// nor may a different, perfectly valid, chip authentication key pass
|
||||
uint8_t other[65] = { 0x00 };
|
||||
size_t otherlen = unhex(CAM_PK_MAP_IC, other, sizeof(other));
|
||||
res = res && (emrtd_pace_cam_verify(&ssn, other, otherlen, &mode) != PM3_SUCCESS);
|
||||
|
||||
// and the chip's own public key has to come out of EF_DG14
|
||||
const char *dg14 =
|
||||
"6E5E315C305A060904007F000702020102304D300706052B8104002203420004"
|
||||
"8DF71464547FC82DCF1650BBC5B72DCD2CC3D9A71283F6E374C0958F316026DB"
|
||||
"0E1F619B0FBBD54B130E9D04B5201A0522D4CC199888B6D76C9A22BDD79F2D45";
|
||||
|
||||
uint8_t dg14bin[128] = { 0x00 };
|
||||
size_t dg14len = unhex(dg14, dg14bin, sizeof(dg14bin));
|
||||
|
||||
uint8_t found[EMRTD_EC_POINT_MAXLEN] = { 0x00 };
|
||||
size_t foundlen = 0;
|
||||
res = res && (emrtd_pace_find_ca_pubkey(dg14bin, dg14len, 0, found, &foundlen) == PM3_SUCCESS);
|
||||
res = res && (foundlen == pk_icclen) && hexeq(found, foundlen, CAM_PK_ICC);
|
||||
|
||||
// truncated EF_DG14 must be refused, not walked off the end
|
||||
for (size_t cut = 1; cut < dg14len; cut++) {
|
||||
if (emrtd_pace_find_ca_pubkey(dg14bin, cut, 0, found, &foundlen) == PM3_SUCCESS) {
|
||||
res = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
PrintAndLogEx(SUCCESS, "CA mapping....... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
static bool test_tlv(void) {
|
||||
const uint8_t data[] = {0x7F, 0x49, 0x04, 0x06, 0x02, 0xAA, 0xBB};
|
||||
const uint8_t *cur = data;
|
||||
const uint8_t *end = data + sizeof(data);
|
||||
uint32_t tag = 0;
|
||||
const uint8_t *val = NULL;
|
||||
size_t vlen = 0;
|
||||
|
||||
bool res = emrtd_tlv_next(&cur, end, &tag, &val, &vlen);
|
||||
res = res && (tag == 0x7F49) && (vlen == 4);
|
||||
|
||||
const uint8_t *icur = val;
|
||||
const uint8_t *iend = val + vlen;
|
||||
res = res && emrtd_tlv_next(&icur, iend, &tag, &val, &vlen);
|
||||
res = res && (tag == 0x06) && (vlen == 2) && (val[0] == 0xAA) && (val[1] == 0xBB);
|
||||
res = res && (emrtd_tlv_next(&icur, iend, &tag, &val, &vlen) == false);
|
||||
|
||||
// a length that runs past the buffer must be rejected
|
||||
const uint8_t bad[] = {0x30, 0x10, 0x01};
|
||||
cur = bad;
|
||||
res = res && (emrtd_tlv_next(&cur, bad + sizeof(bad), &tag, &val, &vlen) == false);
|
||||
|
||||
uint8_t out[8] = { 0x00 };
|
||||
res = res && (emrtd_tlv_write_header(out, sizeof(out), 0x7F49, 0x4F) == 3);
|
||||
res = res && hexeq(out, 3, "7F494F");
|
||||
res = res && (emrtd_tlv_write_header(out, sizeof(out), 0x86, 0x41) == 2);
|
||||
res = res && hexeq(out, 2, "8641");
|
||||
res = res && (emrtd_tlv_write_header(out, sizeof(out), 0x87, 0x81) == 3);
|
||||
res = res && hexeq(out, 3, "878181");
|
||||
res = res && (emrtd_tlv_write_header(out, 1, 0x87, 0x81) == 0);
|
||||
|
||||
PrintAndLogEx(SUCCESS, "BER-TLV.......... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
return res;
|
||||
}
|
||||
|
||||
bool emrtd_test(bool verbose) {
|
||||
(void)verbose;
|
||||
bool res = true;
|
||||
|
||||
PrintAndLogEx(INFO, "------ " _CYAN_("eMRTD PACE tests") " ------");
|
||||
|
||||
res = test_padding() && res;
|
||||
res = test_tlv() && res;
|
||||
res = test_mrz_password() && res;
|
||||
res = test_can_password() && res;
|
||||
res = test_kdf() && res;
|
||||
res = test_nonce() && res;
|
||||
res = test_cardaccess() && res;
|
||||
res = test_gm() && res;
|
||||
res = test_ec_keygen() && res;
|
||||
res = test_token() && res;
|
||||
res = test_cam() && res;
|
||||
res = test_ssc() && res;
|
||||
res = test_sm_3des() && res;
|
||||
res = test_sm_aes() && res;
|
||||
|
||||
PrintAndLogEx(INFO, "---------------------------");
|
||||
PrintAndLogEx(SUCCESS, "Tests ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
|
||||
PrintAndLogEx(NORMAL, "");
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// See LICENSE.txt for the text of the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Offline regression tests for the eMRTD PACE / secure messaging primitives
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EMRTD_PACETEST_H__
|
||||
#define EMRTD_PACETEST_H__
|
||||
|
||||
#include "common.h"
|
||||
|
||||
bool emrtd_test(bool verbose);
|
||||
|
||||
#endif /* emrtd_pacetest.h */
|
||||
@@ -998,6 +998,17 @@ static int ndefDecodeExternal_record(NDEFHeader_t *ndef) {
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
// Android Application Record
|
||||
if ((ndef->TypeLen == strlen(NDEF_ANDROID_AAR)) &&
|
||||
(strncmp((char *)ndef->Type, NDEF_ANDROID_AAR, ndef->TypeLen) == 0)) {
|
||||
PrintAndLogEx(INFO
|
||||
, " Android app... " _GREEN_("%.*s")
|
||||
, (int)ndef->PayloadLen
|
||||
, ndef->Payload
|
||||
);
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
PrintAndLogEx(INFO
|
||||
, " URN... " _GREEN_("urn:nfc:ext:%.*s")
|
||||
, (int)ndef->TypeLen
|
||||
@@ -1393,3 +1404,257 @@ int NDEFGetTotalLength(uint8_t *ndef, size_t ndeflen, size_t *outlen) {
|
||||
*outlen = idx;
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// NDEF encoding
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// Find the longest matching abbreviation from URI_s[].
|
||||
// Returns the prefix code and sets prefixLen to how many bytes of uri it covers.
|
||||
// Code 0x00 means "no abbreviation", the whole string gets stored verbatim.
|
||||
static uint8_t ndefEncodeURIPrefix(const char *uri, size_t *prefixLen) {
|
||||
|
||||
uint8_t code = 0;
|
||||
size_t best = 0;
|
||||
|
||||
for (uint8_t i = 1; i < ARRAYLEN(URI_s); i++) {
|
||||
size_t n = strlen(URI_s[i]);
|
||||
if ((n > best) && (strncmp(uri, URI_s[i], n) == 0)) {
|
||||
best = n;
|
||||
code = i;
|
||||
}
|
||||
}
|
||||
|
||||
*prefixLen = best;
|
||||
return code;
|
||||
}
|
||||
|
||||
int NDEFEncodePayloadURI(const char *uri, uint8_t *buf, size_t bufLen, size_t *outLen) {
|
||||
|
||||
if ((uri == NULL) || (buf == NULL) || (outLen == NULL)) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
size_t uri_len = strlen(uri);
|
||||
if (uri_len == 0) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
size_t prefix_len = 0;
|
||||
uint8_t code = ndefEncodeURIPrefix(uri, &prefix_len);
|
||||
|
||||
size_t n = 1 + (uri_len - prefix_len);
|
||||
if (n > bufLen) {
|
||||
return PM3_EOVFLOW;
|
||||
}
|
||||
|
||||
buf[0] = code;
|
||||
memcpy(buf + 1, uri + prefix_len, uri_len - prefix_len);
|
||||
|
||||
*outLen = n;
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
int NDEFEncodePayloadText(const char *text, const char *lang, uint8_t *buf, size_t bufLen, size_t *outLen) {
|
||||
|
||||
if ((text == NULL) || (buf == NULL) || (outLen == NULL)) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
if ((lang == NULL) || (strlen(lang) == 0)) {
|
||||
lang = "en";
|
||||
}
|
||||
|
||||
size_t lang_len = strlen(lang);
|
||||
size_t text_len = strlen(text);
|
||||
|
||||
// the status byte carries the language code length in its lower 6 bits
|
||||
if (lang_len > 0x3F) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
size_t n = 1 + lang_len + text_len;
|
||||
if (n > bufLen) {
|
||||
return PM3_EOVFLOW;
|
||||
}
|
||||
|
||||
// bit 7 clear == UTF-8
|
||||
buf[0] = (uint8_t)(lang_len & 0x3F);
|
||||
memcpy(buf + 1, lang, lang_len);
|
||||
memcpy(buf + 1 + lang_len, text, text_len);
|
||||
|
||||
*outLen = n;
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
int NDEFEncodePayloadAAR(const char *pkg, uint8_t *buf, size_t bufLen, size_t *outLen) {
|
||||
|
||||
if ((pkg == NULL) || (buf == NULL) || (outLen == NULL)) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
size_t n = strlen(pkg);
|
||||
if (n == 0) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
if (n > bufLen) {
|
||||
return PM3_EOVFLOW;
|
||||
}
|
||||
|
||||
memcpy(buf, pkg, n);
|
||||
|
||||
*outLen = n;
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
int NDEFEncodeRecord(const NDEFRecordDesc_t *rec, bool mb, bool me, uint8_t *buf, size_t bufLen, size_t *outLen) {
|
||||
|
||||
if ((rec == NULL) || (buf == NULL) || (outLen == NULL)) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
if ((rec->typeLen > 255) || (rec->idLen > 255)) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
#if SIZE_MAX > 0xFFFFFFFF
|
||||
// the long form payload length field is only four bytes wide, a larger payload
|
||||
// cannot be expressed. unreachable where size_t is no wider than the field.
|
||||
if (rec->payloadLen > 0xFFFFFFFF) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((rec->typeLen && (rec->type == NULL)) ||
|
||||
(rec->idLen && (rec->id == NULL)) ||
|
||||
(rec->payloadLen && (rec->payload == NULL))) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
bool sr = (rec->payloadLen < 256);
|
||||
bool il = (rec->idLen > 0);
|
||||
|
||||
size_t head = 1 + 1 + (sr ? 1 : 4) + (il ? 1 : 0) + rec->typeLen + rec->idLen;
|
||||
|
||||
// keep the total from wrapping before it reaches the bufLen check below
|
||||
if (rec->payloadLen > SIZE_MAX - head) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
size_t n = head + rec->payloadLen;
|
||||
if (n > bufLen) {
|
||||
return PM3_EOVFLOW;
|
||||
}
|
||||
|
||||
size_t idx = 0;
|
||||
|
||||
// MB | ME | CF | SR | IL | TNF. chunked records are never emitted
|
||||
buf[idx] = (uint8_t)(rec->tnf & 0x07);
|
||||
if (mb) {
|
||||
buf[idx] |= 0x80;
|
||||
}
|
||||
if (me) {
|
||||
buf[idx] |= 0x40;
|
||||
}
|
||||
if (sr) {
|
||||
buf[idx] |= 0x10;
|
||||
}
|
||||
if (il) {
|
||||
buf[idx] |= 0x08;
|
||||
}
|
||||
idx++;
|
||||
|
||||
buf[idx++] = (uint8_t)rec->typeLen;
|
||||
|
||||
if (sr) {
|
||||
buf[idx++] = (uint8_t)rec->payloadLen;
|
||||
} else {
|
||||
buf[idx++] = (uint8_t)(rec->payloadLen >> 24);
|
||||
buf[idx++] = (uint8_t)(rec->payloadLen >> 16);
|
||||
buf[idx++] = (uint8_t)(rec->payloadLen >> 8);
|
||||
buf[idx++] = (uint8_t)(rec->payloadLen);
|
||||
}
|
||||
|
||||
if (il) {
|
||||
buf[idx++] = (uint8_t)rec->idLen;
|
||||
}
|
||||
|
||||
if (rec->typeLen) {
|
||||
memcpy(buf + idx, rec->type, rec->typeLen);
|
||||
idx += rec->typeLen;
|
||||
}
|
||||
|
||||
if (rec->idLen) {
|
||||
memcpy(buf + idx, rec->id, rec->idLen);
|
||||
idx += rec->idLen;
|
||||
}
|
||||
|
||||
if (rec->payloadLen) {
|
||||
memcpy(buf + idx, rec->payload, rec->payloadLen);
|
||||
idx += rec->payloadLen;
|
||||
}
|
||||
|
||||
*outLen = idx;
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
int NDEFEncodeMessage(const NDEFRecordDesc_t *recs, size_t count, uint8_t *buf, size_t bufLen, size_t *outLen) {
|
||||
|
||||
if ((recs == NULL) || (buf == NULL) || (outLen == NULL) || (count == 0)) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
size_t idx = 0;
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
|
||||
size_t n = 0;
|
||||
int res = NDEFEncodeRecord(&recs[i], (i == 0), (i == (count - 1)), buf + idx, bufLen - idx, &n);
|
||||
if (res != PM3_SUCCESS) {
|
||||
return res;
|
||||
}
|
||||
idx += n;
|
||||
}
|
||||
|
||||
*outLen = idx;
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
int NDEFEncodeTLV(const uint8_t *msg, size_t msgLen, uint8_t *buf, size_t bufLen, size_t *outLen) {
|
||||
|
||||
if ((msg == NULL) || (buf == NULL) || (outLen == NULL) || (msgLen == 0)) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
// 0xFFFF is reserved, and the terminator has to fit as well
|
||||
if (msgLen > 0xFFFE) {
|
||||
return PM3_EINVARG;
|
||||
}
|
||||
|
||||
// one byte length form up to 254, three byte form above that
|
||||
size_t hdr = (msgLen < 0xFF) ? 2 : 4;
|
||||
|
||||
size_t n = hdr + msgLen + 1;
|
||||
if (n > bufLen) {
|
||||
return PM3_EOVFLOW;
|
||||
}
|
||||
|
||||
size_t idx = 0;
|
||||
buf[idx++] = 0x03;
|
||||
|
||||
if (msgLen < 0xFF) {
|
||||
buf[idx++] = (uint8_t)msgLen;
|
||||
} else {
|
||||
buf[idx++] = 0xFF;
|
||||
buf[idx++] = (uint8_t)(msgLen >> 8);
|
||||
buf[idx++] = (uint8_t)(msgLen);
|
||||
}
|
||||
|
||||
memcpy(buf + idx, msg, msgLen);
|
||||
idx += msgLen;
|
||||
|
||||
buf[idx++] = 0xFE;
|
||||
|
||||
*outLen = idx;
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
@@ -77,4 +77,35 @@ typedef struct {
|
||||
int NDEFDecodeAndPrint(uint8_t *ndef, size_t ndefLen, bool verbose);
|
||||
int NDEFRecordsDecodeAndPrint(uint8_t *ndefRecord, size_t ndefRecordLen, bool verbose);
|
||||
int NDEFGetTotalLength(uint8_t *ndef, size_t ndeflen, size_t *outlen);
|
||||
|
||||
// NFC Forum well known types
|
||||
#define NDEF_TYPE_URI "U"
|
||||
#define NDEF_TYPE_TEXT "T"
|
||||
|
||||
// Android Application Record, TNF external
|
||||
#define NDEF_ANDROID_AAR "android.com:pkg"
|
||||
|
||||
// Describes one record to be encoded.
|
||||
// Field order intentionally matches gst_ndef_record_t in cmdhfgst.c so the two
|
||||
// can eventually be folded together.
|
||||
typedef struct {
|
||||
TypeNameFormat_t tnf;
|
||||
const uint8_t *type;
|
||||
size_t typeLen;
|
||||
const uint8_t *id;
|
||||
size_t idLen;
|
||||
const uint8_t *payload;
|
||||
size_t payloadLen;
|
||||
} NDEFRecordDesc_t;
|
||||
|
||||
// payload builders
|
||||
int NDEFEncodePayloadURI(const char *uri, uint8_t *buf, size_t bufLen, size_t *outLen);
|
||||
int NDEFEncodePayloadText(const char *text, const char *lang, uint8_t *buf, size_t bufLen, size_t *outLen);
|
||||
int NDEFEncodePayloadAAR(const char *pkg, uint8_t *buf, size_t bufLen, size_t *outLen);
|
||||
|
||||
// record / message / TLV assembly
|
||||
int NDEFEncodeRecord(const NDEFRecordDesc_t *rec, bool mb, bool me, uint8_t *buf, size_t bufLen, size_t *outLen);
|
||||
int NDEFEncodeMessage(const NDEFRecordDesc_t *recs, size_t count, uint8_t *buf, size_t bufLen, size_t *outLen);
|
||||
int NDEFEncodeTLV(const uint8_t *msg, size_t msgLen, uint8_t *buf, size_t bufLen, size_t *outLen);
|
||||
|
||||
#endif // _NDEF_H_
|
||||
@@ -255,6 +255,7 @@ const static vocabulary_t vocabulary[] = {
|
||||
{ 0, "hf emrtd dump" },
|
||||
{ 1, "hf emrtd info" },
|
||||
{ 1, "hf emrtd list" },
|
||||
{ 1, "hf emrtd test" },
|
||||
{ 1, "hf felica help" },
|
||||
{ 1, "hf felica list" },
|
||||
{ 0, "hf felica info" },
|
||||
|
||||
+36
-11
@@ -270,6 +270,40 @@ const PictureItem *PictureWidget::pictureAt(int i) const {
|
||||
return &m_images.at(i);
|
||||
}
|
||||
|
||||
ScaledPictureLabel::ScaledPictureLabel(const QImage &img, QWidget *parent)
|
||||
: QLabel(parent), m_image(img) {
|
||||
|
||||
setAlignment(Qt::AlignCenter);
|
||||
setMinimumSize(1, 1);
|
||||
// let the layout hand us whatever is going, we adapt to it
|
||||
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
|
||||
rescale();
|
||||
}
|
||||
|
||||
QSize ScaledPictureLabel::sizeHint(void) const {
|
||||
return m_image.size();
|
||||
}
|
||||
|
||||
void ScaledPictureLabel::resizeEvent(QResizeEvent *event) {
|
||||
QLabel::resizeEvent(event);
|
||||
rescale();
|
||||
}
|
||||
|
||||
void ScaledPictureLabel::rescale(void) {
|
||||
|
||||
if (m_image.isNull())
|
||||
return;
|
||||
|
||||
QSize room = size();
|
||||
if (room.width() < 1 || room.height() < 1)
|
||||
return;
|
||||
|
||||
// Qt::KeepAspectRatio fits the image inside the box without distorting it.
|
||||
// Scaling up is wanted here, a 240x320 portrait in a 400x400 window should
|
||||
// fill the window rather than sit in the middle of it.
|
||||
setPixmap(QPixmap::fromImage(m_image.scaled(room, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
|
||||
}
|
||||
|
||||
// Append one image to the array and give it its own tab
|
||||
void PictureWidget::addPicture(const QString &title, const QImage &img) {
|
||||
|
||||
@@ -281,17 +315,8 @@ void PictureWidget::addPicture(const QString &title, const QImage &img) {
|
||||
QWidget *page = new QWidget();
|
||||
QVBoxLayout *layout = new QVBoxLayout(page);
|
||||
|
||||
QLabel *lbl_pm = new QLabel();
|
||||
lbl_pm->setPixmap(QPixmap::fromImage(img));
|
||||
lbl_pm->setScaledContents(false);
|
||||
lbl_pm->setAlignment(Qt::AlignCenter);
|
||||
|
||||
// large images (fingerprints, high res portraits) shouldn't blow up the window
|
||||
QScrollArea *scroll = new QScrollArea(page);
|
||||
scroll->setWidget(lbl_pm);
|
||||
scroll->setWidgetResizable(true);
|
||||
scroll->setAlignment(Qt::AlignCenter);
|
||||
layout->addWidget(scroll);
|
||||
ScaledPictureLabel *lbl_pm = new ScaledPictureLabel(img, page);
|
||||
layout->addWidget(lbl_pm, 1);
|
||||
|
||||
QLabel *lbl_sz = new QLabel(page);
|
||||
lbl_sz->setText(QString("w: %1 h: %2")
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include <QObject>
|
||||
#include <QWidget>
|
||||
#include <QPainter>
|
||||
#include <QLabel>
|
||||
#include <QtGui>
|
||||
|
||||
#include "proxgui.h"
|
||||
@@ -101,6 +102,27 @@ class PictureItem {
|
||||
QImage image;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A label that keeps its image scaled to whatever room it is given
|
||||
*
|
||||
* Portraits in EF_DG2 are small (often 240x320) and signatures in EF_DG7 are
|
||||
* wide and short. Painting either one at native size in a fixed window leaves
|
||||
* it stranded in a corner, so the pixmap is rebuilt from the original image on
|
||||
* every resize, fitted to the widget and keeping the aspect ratio.
|
||||
*/
|
||||
class ScaledPictureLabel : public QLabel {
|
||||
public:
|
||||
explicit ScaledPictureLabel(const QImage &img, QWidget *parent = nullptr);
|
||||
QSize sizeHint(void) const override;
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
|
||||
private:
|
||||
void rescale(void);
|
||||
QImage m_image;
|
||||
};
|
||||
|
||||
// Picture viewer window. Holds an array of images, one tab per image, so that
|
||||
// several pictures of the same document (portrait, signature, other biometrics)
|
||||
// can be shown side by side
|
||||
|
||||
@@ -68,7 +68,8 @@ static void at32_bpr_write_dt1(uint32_t data) {
|
||||
|
||||
// Select and enable an ERTC clock so the write-protection register is
|
||||
// writable at the full APB frequency.
|
||||
crm_ertc_clock_select(CRM_ERTC_CLOCK_HEXT_DIV_20);
|
||||
CRM->cfg_bit.ertcdiv = ((CRM_ERTC_CLOCK_HEXT_DIV_20 & 0x1F0) >> 4);
|
||||
CRM->bpdc_bit.ertcsel = (CRM_ERTC_CLOCK_HEXT_DIV_20 & 0xF);
|
||||
CRM->bpdc_bit.ertcen = TRUE;
|
||||
|
||||
ERTC->wp = SYS_SIMPLE_RESET_BPR_UNLOCK_KEY1;
|
||||
|
||||
+34
-5
@@ -2851,11 +2851,14 @@
|
||||
},
|
||||
"hf emrtd help": {
|
||||
"command": "hf emrtd help",
|
||||
"description": "help This help info Tag information list List ISO 14443A/7816 history --------------------------------------------------------------------------------------- hf emrtd dump available offline: no Dump all files on an eMRTD",
|
||||
"description": "help This help info Tag information list List ISO 14443A/7816 history test Regression tests --------------------------------------------------------------------------------------- hf emrtd dump available offline: no Dump all files on an eMRTD",
|
||||
"notes": [
|
||||
"hf emrtd dump",
|
||||
"hf emrtd dump --dir ../dump",
|
||||
"hf emrtd dump -n 123456789 -d 890101 -e 250401"
|
||||
"hf emrtd dump -n 123456789 -d 890101 -e 250401",
|
||||
"hf emrtd dump --can 123456 -> PACE with the Card Access Number",
|
||||
"hf emrtd dump --can 123456 --pace -> PACE only, no BAC fallback",
|
||||
"hf emrtd dump -n 123456789 -d 890101 -e 250401 --bac -> force BAC"
|
||||
],
|
||||
"offline": true,
|
||||
"options": [
|
||||
@@ -2864,9 +2867,12 @@
|
||||
"-d, --date <YYMMDD> date of birth in YYMMDD format",
|
||||
"-e, --expiry <YYMMDD> expiry in YYMMDD format",
|
||||
"-m, --mrz <[0-9A-Z<]> 2nd line of MRZ, 44 chars",
|
||||
"--can <digits> Card Access Number, PACE password instead of the MRZ",
|
||||
"--pace force PACE, fail instead of falling back to BAC",
|
||||
"--bac force BAC, skip PACE",
|
||||
"--dir <str> save dump to the given dirpath"
|
||||
],
|
||||
"usage": "hf emrtd dump [-h] [-n <alphanum>] [-d <YYMMDD>] [-e <YYMMDD>] [-m <[0-9A-Z<]>] [--dir <str>]"
|
||||
"usage": "hf emrtd dump [-h] [-n <alphanum>] [-d <YYMMDD>] [-e <YYMMDD>] [-m <[0-9A-Z<]>] [--can <digits>] [--pace] [--bac] [--dir <str>]"
|
||||
},
|
||||
"hf emrtd info": {
|
||||
"command": "hf emrtd info",
|
||||
@@ -2875,7 +2881,9 @@
|
||||
"hf emrtd info",
|
||||
"hf emrtd info --dir ../dumps",
|
||||
"hf emrtd info -n 123456789 -d 890101 -e 250401",
|
||||
"hf emrtd info -n 123456789 -d 890101 -e 250401 -i"
|
||||
"hf emrtd info -n 123456789 -d 890101 -e 250401 -i",
|
||||
"hf emrtd info --can 123456 -> PACE with the Card Access Number",
|
||||
"hf emrtd info --can 123456 --pace -> PACE only, no BAC fallback"
|
||||
],
|
||||
"offline": true,
|
||||
"options": [
|
||||
@@ -2884,10 +2892,13 @@
|
||||
"-d, --date <YYMMDD> date of birth in YYMMDD format",
|
||||
"-e, --expiry <YYMMDD> expiry in YYMMDD format",
|
||||
"-m, --mrz <[0-9A-Z<]> 2nd line of MRZ, 44 chars (passports only)",
|
||||
"--can <digits> Card Access Number, PACE password instead of the MRZ",
|
||||
"--pace force PACE, fail instead of falling back to BAC",
|
||||
"--bac force BAC, skip PACE",
|
||||
"--dir <str> display info from offline dump stored in dirpath",
|
||||
"-i, --images show images"
|
||||
],
|
||||
"usage": "hf emrtd info [-hi] [-n <alphanum>] [-d <YYMMDD>] [-e <YYMMDD>] [-m <[0-9A-Z<]>] [--dir <str>]"
|
||||
"usage": "hf emrtd info [-hi] [-n <alphanum>] [-d <YYMMDD>] [-e <YYMMDD>] [-m <[0-9A-Z<]>] [--can <digits>] [--pace] [--bac] [--dir <str>]"
|
||||
},
|
||||
"hf emrtd list": {
|
||||
"command": "hf emrtd list",
|
||||
@@ -2910,6 +2921,18 @@
|
||||
],
|
||||
"usage": "hf emrtd list [-h1crux] [--frame] [-f <fn>]"
|
||||
},
|
||||
"hf emrtd test": {
|
||||
"command": "hf emrtd test",
|
||||
"description": "Regression tests for the PACE and secure messaging primitives",
|
||||
"notes": [
|
||||
"hf emrtd test"
|
||||
],
|
||||
"offline": true,
|
||||
"options": [
|
||||
"-h, --help This help"
|
||||
],
|
||||
"usage": "hf emrtd test [-h]"
|
||||
},
|
||||
"hf epa help": {
|
||||
"command": "hf epa help",
|
||||
"description": "help This help --------------------------------------------------------------------------------------- hf epa cnonces available offline: no Tries to collect nonces when doing part of PACE protocol.",
|
||||
@@ -16116,8 +16139,14 @@
|
||||
}
|
||||
},
|
||||
"metadata": {
|
||||
<<<<<<< HEAD
|
||||
"commands_extracted": 901,
|
||||
"extracted_by": "PM3Help2JSON v1.00",
|
||||
"extracted_on": "2026-08-22T14:19:35+00:00"
|
||||
=======
|
||||
"commands_extracted": 902,
|
||||
"extracted_by": "PM3Help2JSON v1.00",
|
||||
"extracted_on": "2026-08-21T18:43:14+00:00"
|
||||
>>>>>>> b4c4edd7c94a989108cacaaaf73805a28a6c217c
|
||||
}
|
||||
}
|
||||
@@ -354,6 +354,7 @@ Check column "offline" for their availability.
|
||||
|`hf emrtd dump `|N |`Dump eMRTD files to binary files`
|
||||
|`hf emrtd info `|Y |`Tag information`
|
||||
|`hf emrtd list `|Y |`List ISO 14443A/7816 history`
|
||||
|`hf emrtd test `|Y |`Regression tests`
|
||||
|
||||
|
||||
### hf felica
|
||||
|
||||
Reference in new issue
Block a user