seos_sam: ran make style

This commit is contained in:
Jakub Kramarz 2025-01-07 00:30:49 +01:00
commit 15a37ef9df
10 changed files with 176 additions and 176 deletions

View file

@ -96,12 +96,12 @@ int sam_rxtx(const uint8_t *data, uint16_t n, uint8_t *resp, uint16_t *resplen)
*resplen += more_len; *resplen += more_len;
out: out:
return res; return res;
} }
static inline void swap_clock_counters(volatile unsigned int * a, unsigned int * b){ static inline void swap_clock_counters(volatile unsigned int *a, unsigned int *b) {
unsigned int c = *a; unsigned int c = *a;
*a = *b; *a = *b;
*b = c; *b = c;
@ -113,9 +113,9 @@ static inline void swap_clock_counters(volatile unsigned int * a, unsigned int *
* AT91SAM7S512 has a single Timer-Counter, that is reused in clocks Ticks * AT91SAM7S512 has a single Timer-Counter, that is reused in clocks Ticks
* and CountSspClk. This function stops the current clock and restores previous * and CountSspClk. This function stops the current clock and restores previous
* values. It is used to switch between different clock sources. * values. It is used to switch between different clock sources.
* It probably makes communication timing off, but at least makes it work. * It probably makes communication timing off, but at least makes it work.
*/ */
static void swap_clocks(void){ static void swap_clocks(void) {
static unsigned int tc0, tc1, tc2 = 0; static unsigned int tc0, tc1, tc2 = 0;
StopTicks(); StopTicks();
swap_clock_counters(&(AT91C_BASE_TC0->TC_CV), &tc0); swap_clock_counters(&(AT91C_BASE_TC0->TC_CV), &tc0);
@ -123,12 +123,12 @@ static void swap_clocks(void){
swap_clock_counters(&(AT91C_BASE_TC2->TC_CV), &tc2); swap_clock_counters(&(AT91C_BASE_TC2->TC_CV), &tc2);
} }
void switch_clock_to_ticks(void){ void switch_clock_to_ticks(void) {
swap_clocks(); swap_clocks();
StartTicks(); StartTicks();
} }
void switch_clock_to_countsspclk(void){ void switch_clock_to_countsspclk(void) {
swap_clocks(); swap_clocks();
StartCountSspClk(); StartCountSspClk();
} }
@ -155,21 +155,21 @@ int sam_send_payload(
const uint8_t addr_dest, const uint8_t addr_dest,
const uint8_t addr_reply, const uint8_t addr_reply,
const uint8_t * const payload, const uint8_t *const payload,
const uint16_t *payload_len, const uint16_t *payload_len,
uint8_t *response, uint8_t *response,
uint16_t *response_len uint16_t *response_len
){ ) {
int res = PM3_SUCCESS; int res = PM3_SUCCESS;
uint8_t * buf = response; uint8_t *buf = response;
buf[0] = 0xA0; // CLA buf[0] = 0xA0; // CLA
buf[1] = 0xDA; // INS (PUT DATA) buf[1] = 0xDA; // INS (PUT DATA)
buf[2] = 0x02; // P1 (TLV format?) buf[2] = 0x02; // P1 (TLV format?)
buf[3] = 0x63; // P2 buf[3] = 0x63; // P2
buf[4] = SAM_TX_ASN1_PREFIX_LENGTH + (uint8_t) *payload_len; // LEN buf[4] = SAM_TX_ASN1_PREFIX_LENGTH + (uint8_t) * payload_len; // LEN
buf[5] = addr_src; buf[5] = addr_src;
buf[6] = addr_dest; buf[6] = addr_dest;
@ -185,10 +185,10 @@ int sam_send_payload(
*payload_len *payload_len
); );
uint16_t length = SAM_TX_ASN1_PREFIX_LENGTH + SAM_TX_APDU_PREFIX_LENGTH + (uint8_t) *payload_len; uint16_t length = SAM_TX_ASN1_PREFIX_LENGTH + SAM_TX_APDU_PREFIX_LENGTH + (uint8_t) * payload_len;
LogTrace(buf, length, 0, 0, NULL, true); LogTrace(buf, length, 0, 0, NULL, true);
if (g_dbglevel >= DBG_INFO){ if (g_dbglevel >= DBG_INFO) {
DbpString("SAM REQUEST APDU: "); DbpString("SAM REQUEST APDU: ");
Dbhexdump(length, buf, false); Dbhexdump(length, buf, false);
} }
@ -201,12 +201,12 @@ int sam_send_payload(
} }
LogTrace(response, *response_len, 0, 0, NULL, false); LogTrace(response, *response_len, 0, 0, NULL, false);
if (g_dbglevel >= DBG_INFO){ if (g_dbglevel >= DBG_INFO) {
DbpString("SAM RESPONSE APDU: "); DbpString("SAM RESPONSE APDU: ");
Dbhexdump(*response_len, response, false); Dbhexdump(*response_len, response, false);
} }
out: out:
return res; return res;
} }
@ -218,18 +218,18 @@ int sam_send_payload(
* *
* @return Status code indicating success or failure of the operation. * @return Status code indicating success or failure of the operation.
*/ */
int sam_get_version(void){ int sam_get_version(void) {
int res = PM3_SUCCESS; int res = PM3_SUCCESS;
if (g_dbglevel >= DBG_DEBUG) if (g_dbglevel >= DBG_DEBUG)
DbpString("start sam_get_version"); DbpString("start sam_get_version");
uint8_t * response = BigBuf_malloc(ISO7816_MAX_FRAME); uint8_t *response = BigBuf_malloc(ISO7816_MAX_FRAME);
uint16_t response_len = ISO7816_MAX_FRAME; uint16_t response_len = ISO7816_MAX_FRAME;
uint8_t payload[] = { uint8_t payload[] = {
0xa0, 0x02, // <- SAM command 0xa0, 0x02, // <- SAM command
0x82, 0x00 // <- get version 0x82, 0x00 // <- get version
}; };
uint16_t payload_len = sizeof(payload); uint16_t payload_len = sizeof(payload);
@ -255,46 +255,46 @@ int sam_get_version(void){
if (g_dbglevel >= DBG_DEBUG) if (g_dbglevel >= DBG_DEBUG)
DbpString("end sam_get_version"); DbpString("end sam_get_version");
if(response[5] != 0xbd){ if (response[5] != 0xbd) {
Dbprintf("Invalid SAM response"); Dbprintf("Invalid SAM response");
goto error; goto error;
}else{ } else {
uint8_t * sam_response_an = sam_find_asn1_node(response + 5, 0x8a); uint8_t *sam_response_an = sam_find_asn1_node(response + 5, 0x8a);
if(sam_response_an == NULL){ if (sam_response_an == NULL) {
if (g_dbglevel >= DBG_ERROR) if (g_dbglevel >= DBG_ERROR)
DbpString("SAM get response failed"); DbpString("SAM get response failed");
goto error; goto error;
} }
uint8_t * sam_version_an = sam_find_asn1_node(sam_response_an, 0x80); uint8_t *sam_version_an = sam_find_asn1_node(sam_response_an, 0x80);
if(sam_version_an == NULL){ if (sam_version_an == NULL) {
if (g_dbglevel >= DBG_ERROR) if (g_dbglevel >= DBG_ERROR)
DbpString("SAM get version failed"); DbpString("SAM get version failed");
goto error; goto error;
} }
uint8_t * sam_build_an = sam_find_asn1_node(sam_response_an, 0x81); uint8_t *sam_build_an = sam_find_asn1_node(sam_response_an, 0x81);
if(sam_build_an == NULL){ if (sam_build_an == NULL) {
if (g_dbglevel >= DBG_ERROR) if (g_dbglevel >= DBG_ERROR)
DbpString("SAM get firmware ID failed"); DbpString("SAM get firmware ID failed");
goto error; goto error;
} }
if (g_dbglevel >= DBG_INFO){ if (g_dbglevel >= DBG_INFO) {
DbpString("SAM get version successful"); DbpString("SAM get version successful");
Dbprintf("Firmware version: %X.%X", sam_version_an[2], sam_version_an[3]); Dbprintf("Firmware version: %X.%X", sam_version_an[2], sam_version_an[3]);
Dbprintf("Firmware ID: "); Dbprintf("Firmware ID: ");
Dbhexdump(sam_build_an[1], sam_build_an+2, false); Dbhexdump(sam_build_an[1], sam_build_an + 2, false);
} }
goto out; goto out;
} }
error: error:
res = PM3_ESOFT; res = PM3_ESOFT;
out: out:
BigBuf_free(); BigBuf_free();
if (g_dbglevel >= DBG_DEBUG) if (g_dbglevel >= DBG_DEBUG)
DbpString("end sam_get_version"); DbpString("end sam_get_version");
return res; return res;
} }
@ -310,14 +310,14 @@ int sam_get_version(void){
* @param type The type of the ASN.1 node to find. * @param type The type of the ASN.1 node to find.
* @return Pointer to the ASN.1 node of the specified type if found, otherwise NULL. * @return Pointer to the ASN.1 node of the specified type if found, otherwise NULL.
*/ */
uint8_t * sam_find_asn1_node(const uint8_t * root, const uint8_t type){ uint8_t *sam_find_asn1_node(const uint8_t *root, const uint8_t type) {
const uint8_t * end = (uint8_t *) root + *(root+1); const uint8_t *end = (uint8_t *) root + *(root + 1);
uint8_t * current = (uint8_t *) root + 2; uint8_t *current = (uint8_t *) root + 2;
while(current < end){ while (current < end) {
if(*current == type){ if (*current == type) {
return current; return current;
}else{ } else {
current += 2 + *(current+1); current += 2 + *(current + 1);
} }
} }
return NULL; return NULL;
@ -328,7 +328,7 @@ uint8_t * sam_find_asn1_node(const uint8_t * root, const uint8_t type){
* *
* This function appends an ASN.1 node of a specified type and length to the end of * This function appends an ASN.1 node of a specified type and length to the end of
* the ASN.1 structure at specified node level. * the ASN.1 structure at specified node level.
* *
* It is the most naive solution that does not handle the case where the node to append is * It is the most naive solution that does not handle the case where the node to append is
* not the last node at the same level. It also does not also care about proper * not the last node at the same level. It also does not also care about proper
* order of the nodes. * order of the nodes.
@ -339,21 +339,21 @@ uint8_t * sam_find_asn1_node(const uint8_t * root, const uint8_t type){
* @param data Pointer to the data to be appended. * @param data Pointer to the data to be appended.
* @param len The length of the data to be appended. * @param len The length of the data to be appended.
*/ */
void sam_append_asn1_node(const uint8_t * root, const uint8_t * node, uint8_t type, const uint8_t * const data, uint8_t len){ void sam_append_asn1_node(const uint8_t *root, const uint8_t *node, uint8_t type, const uint8_t *const data, uint8_t len) {
uint8_t * end = (uint8_t *) root + *(root+1) + 2; uint8_t *end = (uint8_t *) root + *(root + 1) + 2;
*(end) = type; *(end) = type;
*(end+1) = len; *(end + 1) = len;
memcpy(end+2, data, len); memcpy(end + 2, data, len);
for(uint8_t * current = (uint8_t *) root; current <= node; current += 2){ for (uint8_t *current = (uint8_t *) root; current <= node; current += 2) {
*(current+1) += 2 + len; *(current + 1) += 2 + len;
}; };
return; return;
} }
void sam_send_ack(void){ void sam_send_ack(void) {
uint8_t * response = BigBuf_malloc(ISO7816_MAX_FRAME); uint8_t *response = BigBuf_malloc(ISO7816_MAX_FRAME);
uint16_t response_len = ISO7816_MAX_FRAME; uint16_t response_len = ISO7816_MAX_FRAME;
uint8_t payload[] = { uint8_t payload[] = {

View file

@ -32,7 +32,7 @@ int sam_send_payload(
const uint8_t addr_dest, const uint8_t addr_dest,
const uint8_t addr_reply, const uint8_t addr_reply,
const uint8_t * const payload, const uint8_t *const payload,
const uint16_t *payload_len, const uint16_t *payload_len,
uint8_t *response, uint8_t *response,
@ -41,8 +41,8 @@ int sam_send_payload(
int sam_get_version(void); int sam_get_version(void);
uint8_t * sam_find_asn1_node(const uint8_t * root, const uint8_t type); uint8_t *sam_find_asn1_node(const uint8_t *root, const uint8_t type);
void sam_append_asn1_node(const uint8_t * root, const uint8_t * node, uint8_t type, const uint8_t * const data, uint8_t len); void sam_append_asn1_node(const uint8_t *root, const uint8_t *node, uint8_t type, const uint8_t *const data, uint8_t len);
void sam_send_ack(void); void sam_send_ack(void);

View file

@ -41,12 +41,12 @@
* @param card_select Pointer to the descriptor of the detected card. * @param card_select Pointer to the descriptor of the detected card.
* @return Status code indicating success or failure of the operation. * @return Status code indicating success or failure of the operation.
*/ */
static int sam_set_card_detected(picopass_hdr_t * card_select){ static int sam_set_card_detected(picopass_hdr_t *card_select) {
int res = PM3_SUCCESS; int res = PM3_SUCCESS;
if (g_dbglevel >= DBG_DEBUG) if (g_dbglevel >= DBG_DEBUG)
DbpString("start sam_set_card_detected"); DbpString("start sam_set_card_detected");
uint8_t * response = BigBuf_malloc(ISO7816_MAX_FRAME); uint8_t *response = BigBuf_malloc(ISO7816_MAX_FRAME);
uint16_t response_len = ISO7816_MAX_FRAME; uint16_t response_len = ISO7816_MAX_FRAME;
// a0 12 // a0 12
@ -59,13 +59,13 @@ static int sam_set_card_detected(picopass_hdr_t * card_select){
uint8_t payload[] = { uint8_t payload[] = {
0xa0, 18, // <- SAM command 0xa0, 18, // <- SAM command
0xad, 16, // <- set detected card 0xad, 16, // <- set detected card
0xa0, 4+10, 0xa0, 4 + 10,
0x80, 2, // <- protocol 0x80, 2, // <- protocol
0x00, 0x04, // <- Picopass 0x00, 0x04, // <- Picopass
0x81, 8, // <- CSN 0x81, 8, // <- CSN
card_select->csn[0], card_select->csn[1], card_select->csn[2], card_select->csn[3], card_select->csn[0], card_select->csn[1], card_select->csn[2], card_select->csn[3],
card_select->csn[4], card_select->csn[5], card_select->csn[6], card_select->csn[7] card_select->csn[4], card_select->csn[5], card_select->csn[6], card_select->csn[7]
}; };
uint16_t payload_len = sizeof(payload); uint16_t payload_len = sizeof(payload);
@ -82,12 +82,12 @@ static int sam_set_card_detected(picopass_hdr_t * card_select){
// bd 02 <- response // bd 02 <- response
// 8a 00 <- empty response (accepted) // 8a 00 <- empty response (accepted)
// 90 00 // 90 00
if(response[5] != 0xbd){ if (response[5] != 0xbd) {
if (g_dbglevel >= DBG_ERROR) if (g_dbglevel >= DBG_ERROR)
Dbprintf("Invalid SAM response"); Dbprintf("Invalid SAM response");
goto error; goto error;
}else{ } else {
// uint8_t * sam_response_an = sam_find_asn1_node(response + 5, 0x8a); // uint8_t * sam_response_an = sam_find_asn1_node(response + 5, 0x8a);
// if(sam_response_an == NULL){ // if(sam_response_an == NULL){
// if (g_dbglevel >= DBG_ERROR) // if (g_dbglevel >= DBG_ERROR)
@ -96,10 +96,10 @@ static int sam_set_card_detected(picopass_hdr_t * card_select){
// } // }
goto out; goto out;
} }
error: error:
res = PM3_ESOFT; res = PM3_ESOFT;
out: out:
BigBuf_free(); BigBuf_free();
if (g_dbglevel >= DBG_DEBUG) if (g_dbglevel >= DBG_DEBUG)
@ -240,11 +240,11 @@ int sam_picopass_get_pacs(void) {
// second - get PACS (0xA1) // second - get PACS (0xA1)
// a0 05 // a0 05
// a1 03 // a1 03
// 80 01 // 80 01
// 04 // 04
hexstr_to_byte_array("a005a103800104", sam_apdu, &sam_len); hexstr_to_byte_array("a005a103800104", sam_apdu, &sam_len);
if(sam_send_payload(0x44, 0x0a, 0x44, sam_apdu, (uint16_t *) &sam_len, resp, &resp_len) != PM3_SUCCESS) { if (sam_send_payload(0x44, 0x0a, 0x44, sam_apdu, (uint16_t *) &sam_len, resp, &resp_len) != PM3_SUCCESS) {
res = PM3_ECARDEXCHANGE; res = PM3_ECARDEXCHANGE;
goto out; goto out;
} }

View file

@ -49,29 +49,29 @@
* @param card_select Pointer to the descriptor of the detected card. * @param card_select Pointer to the descriptor of the detected card.
* @return Status code indicating success or failure of the operation. * @return Status code indicating success or failure of the operation.
*/ */
static int sam_set_card_detected(iso14a_card_select_t * card_select){ static int sam_set_card_detected(iso14a_card_select_t *card_select) {
int res = PM3_SUCCESS; int res = PM3_SUCCESS;
if (g_dbglevel >= DBG_DEBUG) if (g_dbglevel >= DBG_DEBUG)
DbpString("start sam_set_card_detected"); DbpString("start sam_set_card_detected");
uint8_t * request = BigBuf_malloc(ISO7816_MAX_FRAME); uint8_t *request = BigBuf_malloc(ISO7816_MAX_FRAME);
uint16_t request_len = ISO7816_MAX_FRAME; uint16_t request_len = ISO7816_MAX_FRAME;
uint8_t * response = BigBuf_malloc(ISO7816_MAX_FRAME); uint8_t *response = BigBuf_malloc(ISO7816_MAX_FRAME);
uint16_t response_len = ISO7816_MAX_FRAME; uint16_t response_len = ISO7816_MAX_FRAME;
const uint8_t payload[] = { const uint8_t payload[] = {
0xa0, 8, // <- SAM command 0xa0, 8, // <- SAM command
0xad, 6, // <- set detected card 0xad, 6, // <- set detected card
0xa0, 4, // <- detected card details 0xa0, 4, // <- detected card details
0x80, 2, // <- protocol 0x80, 2, // <- protocol
0x00, 0x02 // <- ISO14443A 0x00, 0x02 // <- ISO14443A
}; };
memcpy(request, payload, sizeof(payload)); memcpy(request, payload, sizeof(payload));
sam_append_asn1_node(request, request+4, 0x81, card_select->uid, card_select->uidlen); sam_append_asn1_node(request, request + 4, 0x81, card_select->uid, card_select->uidlen);
sam_append_asn1_node(request, request+4, 0x82, card_select->atqa, 2); sam_append_asn1_node(request, request + 4, 0x82, card_select->atqa, 2);
sam_append_asn1_node(request, request+4, 0x83, &card_select->sak, 1); sam_append_asn1_node(request, request + 4, 0x83, &card_select->sak, 1);
request_len = request[1] + 2; request_len = request[1] + 2;
sam_send_payload( sam_send_payload(
@ -87,12 +87,12 @@ static int sam_set_card_detected(iso14a_card_select_t * card_select){
// bd 02 <- response // bd 02 <- response
// 8a 00 <- empty response (accepted) // 8a 00 <- empty response (accepted)
// 90 00 // 90 00
if(response[5] != 0xbd){ if (response[5] != 0xbd) {
if (g_dbglevel >= DBG_ERROR) if (g_dbglevel >= DBG_ERROR)
Dbprintf("Invalid SAM response"); Dbprintf("Invalid SAM response");
goto error; goto error;
}else{ } else {
// uint8_t * sam_response_an = sam_find_asn1_node(response + 5, 0x8a); // uint8_t * sam_response_an = sam_find_asn1_node(response + 5, 0x8a);
// if(sam_response_an == NULL){ // if(sam_response_an == NULL){
// if (g_dbglevel >= DBG_ERROR) // if (g_dbglevel >= DBG_ERROR)
@ -101,10 +101,10 @@ static int sam_set_card_detected(iso14a_card_select_t * card_select){
// } // }
goto out; goto out;
} }
error: error:
res = PM3_ESOFT; res = PM3_ESOFT;
out: out:
BigBuf_free(); BigBuf_free();
if (g_dbglevel >= DBG_DEBUG) if (g_dbglevel >= DBG_DEBUG)
@ -123,7 +123,7 @@ static int sam_set_card_detected(iso14a_card_select_t * card_select){
* *
* @return Length of SAM APDU to be sent. * @return Length of SAM APDU to be sent.
*/ */
inline static uint16_t sam_seos_copy_payload_nfc2sam(uint8_t *sam_tx, uint8_t * nfc_rx, uint8_t nfc_len){ inline static uint16_t sam_seos_copy_payload_nfc2sam(uint8_t *sam_tx, uint8_t *nfc_rx, uint8_t nfc_len) {
// NFC resp: // NFC resp:
// 6f 0c 84 0a a0 00 00 04 40 00 01 01 00 01 90 00 fb e3 // 6f 0c 84 0a a0 00 00 04 40 00 01 01 00 01 90 00 fb e3
@ -138,18 +138,18 @@ inline static uint16_t sam_seos_copy_payload_nfc2sam(uint8_t *sam_tx, uint8_t *
const uint8_t payload[] = { const uint8_t payload[] = {
0xbd, 4, 0xbd, 4,
0xa0, 2, 0xa0, 2,
0xa0, 0 0xa0, 0
}; };
const uint8_t tag81[] = { const uint8_t tag81[] = {
0x00, 0x00 0x00, 0x00
}; };
memcpy(sam_tx, payload, sizeof(payload)); memcpy(sam_tx, payload, sizeof(payload));
sam_append_asn1_node(sam_tx, sam_tx+4, 0x80, nfc_rx, nfc_len); sam_append_asn1_node(sam_tx, sam_tx + 4, 0x80, nfc_rx, nfc_len);
sam_append_asn1_node(sam_tx, sam_tx+4, 0x81, tag81, sizeof(tag81)); sam_append_asn1_node(sam_tx, sam_tx + 4, 0x81, tag81, sizeof(tag81));
return sam_tx[1] + 2; // length of the ASN1 tree return sam_tx[1] + 2; // length of the ASN1 tree
} }
@ -163,7 +163,7 @@ inline static uint16_t sam_seos_copy_payload_nfc2sam(uint8_t *sam_tx, uint8_t *
* @param sam_rx_buf Pointer to the buffer containing the data received from the SAM. * @param sam_rx_buf Pointer to the buffer containing the data received from the SAM.
* @return Length of NFC APDU to be sent. * @return Length of NFC APDU to be sent.
*/ */
inline static uint16_t sam_seos_copy_payload_sam2nfc(uint8_t * nfc_tx_buf, uint8_t * sam_rx_buf){ inline static uint16_t sam_seos_copy_payload_sam2nfc(uint8_t *nfc_tx_buf, uint8_t *sam_rx_buf) {
// SAM resp: // SAM resp:
// c1 61 c1 00 00 // c1 61 c1 00 00
// a1 21 <- nfc command // a1 21 <- nfc command
@ -182,8 +182,8 @@ inline static uint16_t sam_seos_copy_payload_sam2nfc(uint8_t * nfc_tx_buf, uint8
// 00 a4 04 00 0a a0 00 00 04 40 00 01 01 00 01 00 // 00 a4 04 00 0a a0 00 00 04 40 00 01 01 00 01 00
// copy data out of c1->a1>->a1->80 node // copy data out of c1->a1>->a1->80 node
uint16_t nfc_tx_len = (uint8_t) *(sam_rx_buf + 10); uint16_t nfc_tx_len = (uint8_t) * (sam_rx_buf + 10);
memcpy(nfc_tx_buf, sam_rx_buf+11, nfc_tx_len); memcpy(nfc_tx_buf, sam_rx_buf + 11, nfc_tx_len);
return nfc_tx_len; return nfc_tx_len;
} }
@ -193,13 +193,13 @@ inline static uint16_t sam_seos_copy_payload_sam2nfc(uint8_t * nfc_tx_buf, uint8
* Unpacks request to the SAM and relays ISO14A traffic to the card. * Unpacks request to the SAM and relays ISO14A traffic to the card.
* If no request data provided, sends a request to get PACS data. * If no request data provided, sends a request to get PACS data.
* *
* @param request Pointer to the buffer containing the request to be sent to the SAM. * @param request Pointer to the buffer containing the request to be sent to the SAM.
* @param request_len Length of the request to be sent to the SAM. * @param request_len Length of the request to be sent to the SAM.
* @param response Pointer to the buffer where the retreived data will be stored. * @param response Pointer to the buffer where the retreived data will be stored.
* @param response_len Pointer to the variable where the length of the retreived data will be stored. * @param response_len Pointer to the variable where the length of the retreived data will be stored.
* @return Status code indicating success or failure of the operation. * @return Status code indicating success or failure of the operation.
*/ */
static int sam_send_request_iso14a(const uint8_t * const request, const uint8_t request_len, uint8_t * response, uint8_t * response_len){ static int sam_send_request_iso14a(const uint8_t *const request, const uint8_t request_len, uint8_t *response, uint8_t *response_len) {
int res = PM3_SUCCESS; int res = PM3_SUCCESS;
if (g_dbglevel >= DBG_DEBUG) if (g_dbglevel >= DBG_DEBUG)
DbpString("start sam_send_request_iso14a"); DbpString("start sam_send_request_iso14a");
@ -207,22 +207,22 @@ static int sam_send_request_iso14a(const uint8_t * const request, const uint8_t
uint8_t buf1[ISO7816_MAX_FRAME] = {0}; uint8_t buf1[ISO7816_MAX_FRAME] = {0};
uint8_t buf2[ISO7816_MAX_FRAME] = {0}; uint8_t buf2[ISO7816_MAX_FRAME] = {0};
uint8_t * sam_tx_buf = buf1; uint8_t *sam_tx_buf = buf1;
uint16_t sam_tx_len; uint16_t sam_tx_len;
uint8_t * sam_rx_buf = buf2; uint8_t *sam_rx_buf = buf2;
uint16_t sam_rx_len; uint16_t sam_rx_len;
uint8_t * nfc_tx_buf = buf1; uint8_t *nfc_tx_buf = buf1;
uint16_t nfc_tx_len; uint16_t nfc_tx_len;
uint8_t * nfc_rx_buf = buf2; uint8_t *nfc_rx_buf = buf2;
uint16_t nfc_rx_len; uint16_t nfc_rx_len;
if(request_len > 0){ if (request_len > 0) {
sam_tx_len = request_len; sam_tx_len = request_len;
memcpy(sam_tx_buf, request, sam_tx_len); memcpy(sam_tx_buf, request, sam_tx_len);
}else{ } else {
// send get pacs // send get pacs
static const uint8_t payload[] = { static const uint8_t payload[] = {
0xa0, 19, // <- SAM command 0xa0, 19, // <- SAM command
@ -243,23 +243,23 @@ static int sam_send_request_iso14a(const uint8_t * const request, const uint8_t
sam_rx_buf, &sam_rx_len sam_rx_buf, &sam_rx_len
); );
if(sam_rx_buf[1] == 0x61){ // commands to be relayed to card starts with 0x61 if (sam_rx_buf[1] == 0x61) { // commands to be relayed to card starts with 0x61
// tag <-> SAM exchange starts here // tag <-> SAM exchange starts here
while(sam_rx_buf[1] == 0x61){ while (sam_rx_buf[1] == 0x61) {
switch_clock_to_countsspclk(); switch_clock_to_countsspclk();
nfc_tx_len = sam_seos_copy_payload_sam2nfc(nfc_tx_buf, sam_rx_buf); nfc_tx_len = sam_seos_copy_payload_sam2nfc(nfc_tx_buf, sam_rx_buf);
nfc_rx_len = iso14_apdu( nfc_rx_len = iso14_apdu(
nfc_tx_buf, nfc_tx_buf,
nfc_tx_len, nfc_tx_len,
false, false,
nfc_rx_buf, nfc_rx_buf,
ISO7816_MAX_FRAME, ISO7816_MAX_FRAME,
NULL NULL
); );
switch_clock_to_ticks(); switch_clock_to_ticks();
sam_tx_len = sam_seos_copy_payload_nfc2sam(sam_tx_buf, nfc_rx_buf, nfc_rx_len-2); sam_tx_len = sam_seos_copy_payload_nfc2sam(sam_tx_buf, nfc_rx_buf, nfc_rx_len - 2);
sam_send_payload( sam_send_payload(
0x14, 0x0a, 0x14, 0x14, 0x0a, 0x14,
@ -267,13 +267,13 @@ static int sam_send_request_iso14a(const uint8_t * const request, const uint8_t
sam_rx_buf, &sam_rx_len sam_rx_buf, &sam_rx_len
); );
// last SAM->TAG // last SAM->TAG
// c1 61 c1 00 00 a1 02 >>82<< 00 90 00 // c1 61 c1 00 00 a1 02 >>82<< 00 90 00
if(sam_rx_buf[7] == 0x82){ if (sam_rx_buf[7] == 0x82) {
// tag <-> SAM exchange ends here // tag <-> SAM exchange ends here
break; break;
} }
} }
static const uint8_t hfack[] = { static const uint8_t hfack[] = {
@ -310,25 +310,25 @@ static int sam_send_request_iso14a(const uint8_t * const request, const uint8_t
// 82 01 // 82 01
// 07 // 07
// 90 00 // 90 00
if(request_len == 0){ if (request_len == 0) {
if( if (
!(sam_rx_buf[5] == 0xbd && sam_rx_buf[5+2] == 0x8a && sam_rx_buf[5+4] == 0x03) !(sam_rx_buf[5] == 0xbd && sam_rx_buf[5 + 2] == 0x8a && sam_rx_buf[5 + 4] == 0x03)
&& &&
!(sam_rx_buf[5] == 0xbd && sam_rx_buf[5+2] == 0xb3 && sam_rx_buf[5+4] == 0xa0) !(sam_rx_buf[5] == 0xbd && sam_rx_buf[5 + 2] == 0xb3 && sam_rx_buf[5 + 4] == 0xa0)
){ ) {
if (g_dbglevel >= DBG_ERROR) if (g_dbglevel >= DBG_ERROR)
Dbprintf("No PACS data in SAM response"); Dbprintf("No PACS data in SAM response");
res=PM3_ESOFT; res = PM3_ESOFT;
} }
} }
*response_len = sam_rx_buf[5+1] +2; *response_len = sam_rx_buf[5 + 1] + 2;
memcpy(response, sam_rx_buf+5, *response_len); memcpy(response, sam_rx_buf + 5, *response_len);
goto out; goto out;
out: out:
return res; return res;
} }
/** /**
@ -337,7 +337,7 @@ static int sam_send_request_iso14a(const uint8_t * const request, const uint8_t
* This function is called by appmain.c * This function is called by appmain.c
* It sends a request to the SAM to get the PACS data from the SEOS card. * It sends a request to the SAM to get the PACS data from the SEOS card.
* The PACS data is then returned to the PM3 client. * The PACS data is then returned to the PM3 client.
* *
* @return Status code indicating success or failure of the operation. * @return Status code indicating success or failure of the operation.
*/ */
int sam_seos_get_pacs(PacketCommandNG *c) { int sam_seos_get_pacs(PacketCommandNG *c) {
@ -346,7 +346,7 @@ int sam_seos_get_pacs(PacketCommandNG *c) {
uint8_t *cmd = c->data.asBytes; uint8_t *cmd = c->data.asBytes;
uint16_t cmd_len = (uint16_t) c->oldarg[2]; uint16_t cmd_len = (uint16_t) c->oldarg[2];
int res = PM3_EFAILED; int res = PM3_EFAILED;
clear_trace(); clear_trace();
@ -358,13 +358,13 @@ int sam_seos_get_pacs(PacketCommandNG *c) {
// step 1: ping SAM // step 1: ping SAM
sam_get_version(); sam_get_version();
if(!skipDetect){ if (!skipDetect) {
// step 2: get card information // step 2: get card information
iso14a_card_select_t card_a_info; iso14a_card_select_t card_a_info;
// implicit StartSspClk() happens here // implicit StartSspClk() happens here
iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD); iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD);
if (!iso14443a_select_card(NULL, &card_a_info, NULL, true, 0, false)){ if (!iso14443a_select_card(NULL, &card_a_info, NULL, true, 0, false)) {
goto err; goto err;
} }
@ -378,7 +378,7 @@ int sam_seos_get_pacs(PacketCommandNG *c) {
uint8_t sam_response[ISO7816_MAX_FRAME] = { 0x00 }; uint8_t sam_response[ISO7816_MAX_FRAME] = { 0x00 };
uint8_t sam_response_len = 0; uint8_t sam_response_len = 0;
res = sam_send_request_iso14a(cmd, cmd_len, sam_response, &sam_response_len); res = sam_send_request_iso14a(cmd, cmd_len, sam_response, &sam_response_len);
if(res != PM3_SUCCESS){ if (res != PM3_SUCCESS) {
goto err; goto err;
} }
if (g_dbglevel >= DBG_INFO) if (g_dbglevel >= DBG_INFO)
@ -387,19 +387,19 @@ int sam_seos_get_pacs(PacketCommandNG *c) {
goto out; goto out;
goto off; goto off;
err: err:
res = PM3_ENOPACS; res = PM3_ENOPACS;
reply_ng(CMD_HF_SAM_SEOS, res, NULL, 0); reply_ng(CMD_HF_SAM_SEOS, res, NULL, 0);
goto off; goto off;
out: out:
reply_ng(CMD_HF_SAM_SEOS, PM3_SUCCESS, sam_response, sam_response_len); reply_ng(CMD_HF_SAM_SEOS, PM3_SUCCESS, sam_response, sam_response_len);
goto off; goto off;
off: off:
if(disconnectAfter){ if (disconnectAfter) {
switch_off(); switch_off();
} }
set_tracing(false); set_tracing(false);
StopTicks(); StopTicks();
BigBuf_free(); BigBuf_free();
return res; return res;
} }

View file

@ -5434,15 +5434,15 @@ static int CmdHFiClassSAM(const char *Cmd) {
// CSN, config, epurse, NR/MAC, AIA // CSN, config, epurse, NR/MAC, AIA
// PACS // PACS
// 03 05 // 03 05
// 06 85 80 6d c0 // 06 85 80 6d c0
// first byte skip // first byte skip
// second byte length // second byte length
// third padded // third padded
// fourth .. // fourth ..
uint8_t *d = resp.data.asBytes; uint8_t *d = resp.data.asBytes;
HIDDumpPACSBits(d+2, d[1], verbose); HIDDumpPACSBits(d + 2, d[1], verbose);
return PM3_SUCCESS; return PM3_SUCCESS;
} }

View file

@ -1688,15 +1688,15 @@ static int CmdHfSeosSAM(const char *Cmd) {
verbose = true; verbose = true;
} }
bool disconnectAfter = true; bool disconnectAfter = true;
if(arg_get_lit(ctx, 2)){ if (arg_get_lit(ctx, 2)) {
disconnectAfter = false; disconnectAfter = false;
} }
bool skipDetect = false; bool skipDetect = false;
if(arg_get_lit(ctx, 3)){ if (arg_get_lit(ctx, 3)) {
skipDetect = true; skipDetect = true;
} }
bool decodeTLV = false; bool decodeTLV = false;
if(arg_get_lit(ctx, 4)){ if (arg_get_lit(ctx, 4)) {
decodeTLV = true; decodeTLV = true;
} }
@ -1728,45 +1728,45 @@ static int CmdHfSeosSAM(const char *Cmd) {
PrintAndLogEx(WARNING, "SAM select failed"); PrintAndLogEx(WARNING, "SAM select failed");
return resp.status; return resp.status;
} }
uint8_t *d = resp.data.asBytes; uint8_t *d = resp.data.asBytes;
// check for standard SamCommandGetContentElement response // check for standard SamCommandGetContentElement response
// bd 09 // bd 09
// 8a 07 // 8a 07
// 03 05 <- tag + length // 03 05 <- tag + length
// 06 85 80 6d c0 <- decoded PACS data // 06 85 80 6d c0 <- decoded PACS data
if(d[0] == 0xbd && d[2] == 0x8a && d[4] == 0x03){ if (d[0] == 0xbd && d[2] == 0x8a && d[4] == 0x03) {
uint8_t pacs_length = d[5]; uint8_t pacs_length = d[5];
uint8_t * pacs_data = d + 6; uint8_t *pacs_data = d + 6;
int res = HIDDumpPACSBits(pacs_data, pacs_length, verbose); int res = HIDDumpPACSBits(pacs_data, pacs_length, verbose);
if(res != PM3_SUCCESS){ if (res != PM3_SUCCESS) {
return res; return res;
} }
// check for standard samCommandGetContentElement2: // check for standard samCommandGetContentElement2:
// bd 1e // bd 1e
// b3 1c // b3 1c
// a0 1a // a0 1a
// 80 05 // 80 05
// 06 85 80 6d c0 // 06 85 80 6d c0
// 81 0e // 81 0e
// 2b 06 01 04 01 81 e4 38 01 01 02 04 3c ff // 2b 06 01 04 01 81 e4 38 01 01 02 04 3c ff
// 82 01 // 82 01
// 07 // 07
} else if(d[0]==0xbd && d[2]==0xb3 && d[4]==0xa0){ } else if (d[0] == 0xbd && d[2] == 0xb3 && d[4] == 0xa0) {
const uint8_t * pacs = d + 6; const uint8_t *pacs = d + 6;
const uint8_t pacs_length = pacs[1]; const uint8_t pacs_length = pacs[1];
const uint8_t * pacs_data = pacs + 2; const uint8_t *pacs_data = pacs + 2;
int res = HIDDumpPACSBits(pacs_data, pacs_length, verbose); int res = HIDDumpPACSBits(pacs_data, pacs_length, verbose);
if(res != PM3_SUCCESS){ if (res != PM3_SUCCESS) {
return res; return res;
} }
const uint8_t * oid = pacs + 2 + pacs_length; const uint8_t *oid = pacs + 2 + pacs_length;
const uint8_t oid_length = oid[1]; const uint8_t oid_length = oid[1];
const uint8_t * oid_data = oid + 2; const uint8_t *oid_data = oid + 2;
PrintAndLogEx(SUCCESS, "SIO OID.......: " _GREEN_("%s"), sprint_hex_inrow(oid_data, oid_length)); PrintAndLogEx(SUCCESS, "SIO OID.......: " _GREEN_("%s"), sprint_hex_inrow(oid_data, oid_length));
const uint8_t * mediaType = oid + 2 + oid_length; const uint8_t *mediaType = oid + 2 + oid_length;
const uint8_t mediaType_data = mediaType[2]; const uint8_t mediaType_data = mediaType[2];
PrintAndLogEx(SUCCESS, "SIO Media Type: " _GREEN_("%s"), getSioMediaTypeInfo(mediaType_data)); PrintAndLogEx(SUCCESS, "SIO Media Type: " _GREEN_("%s"), getSioMediaTypeInfo(mediaType_data));

View file

@ -29,6 +29,6 @@ typedef struct {
int infoSeos(bool verbose); int infoSeos(bool verbose);
int CmdHFSeos(const char *Cmd); int CmdHFSeos(const char *Cmd);
int seos_kdf(bool encryption, uint8_t* masterKey, uint8_t keyslot, int seos_kdf(bool encryption, uint8_t *masterKey, uint8_t keyslot,
uint8_t* adfOid, size_t adfoid_len, uint8_t* diversifier, uint8_t diversifier_len, uint8_t* out, int encryption_algorithm, int hash_algorithm); uint8_t *adfOid, size_t adfoid_len, uint8_t *diversifier, uint8_t diversifier_len, uint8_t *out, int encryption_algorithm, int hash_algorithm);
#endif #endif

View file

@ -1664,7 +1664,7 @@ void HIDUnpack(int idx, wiegand_message_t *packed) {
} }
} }
int HIDDumpPACSBits(const uint8_t * const data, const uint8_t length, bool verbose){ int HIDDumpPACSBits(const uint8_t *const data, const uint8_t length, bool verbose) {
uint8_t n = length - 1; uint8_t n = length - 1;
uint8_t pad = data[0]; uint8_t pad = data[0];
char *binstr = (char *)calloc((length * 8) + 1, sizeof(uint8_t)); char *binstr = (char *)calloc((length * 8) + 1, sizeof(uint8_t));
@ -1733,5 +1733,5 @@ int HIDDumpPACSBits(const uint8_t * const data, const uint8_t length, bool verbo
PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "");
} }
free(binstr); free(binstr);
return PM3_SUCCESS; return PM3_SUCCESS;
} }

View file

@ -54,7 +54,7 @@ bool HIDPack(int format_idx, wiegand_card_t *card, wiegand_message_t *packed, bo
bool HIDTryUnpack(wiegand_message_t *packed); bool HIDTryUnpack(wiegand_message_t *packed);
void HIDPackTryAll(wiegand_card_t *card, bool preamble); void HIDPackTryAll(wiegand_card_t *card, bool preamble);
void HIDUnpack(int idx, wiegand_message_t *packed); void HIDUnpack(int idx, wiegand_message_t *packed);
int HIDDumpPACSBits(const uint8_t * const data, const uint8_t length, bool verbose); int HIDDumpPACSBits(const uint8_t *const data, const uint8_t length, bool verbose);
void print_wiegand_code(wiegand_message_t *packed); void print_wiegand_code(wiegand_message_t *packed);
void print_desc_wiegand(cardformat_t *fmt, wiegand_message_t *packed); void print_desc_wiegand(cardformat_t *fmt, wiegand_message_t *packed);
#endif #endif

View file

@ -305,7 +305,7 @@ uint32_t GetTicks(void) {
do { do {
hi = AT91C_BASE_TC1->TC_CV; hi = AT91C_BASE_TC1->TC_CV;
lo = AT91C_BASE_TC0->TC_CV; lo = AT91C_BASE_TC0->TC_CV;
} while (hi != AT91C_BASE_TC1->TC_CV); } while (hi != AT91C_BASE_TC1->TC_CV);
return (hi << 16) | lo; return (hi << 16) | lo;
} }