Skip to content

Commit

Permalink
Merge pull request #2731 from jkramarz/feature/sam_picopass
Browse files Browse the repository at this point in the history
support for HID SAM communication with Picopass cards
  • Loading branch information
iceman1001 authored Jan 27, 2025
2 parents d5004fc + 550af14 commit 50b66a6
Show file tree
Hide file tree
Showing 13 changed files with 545 additions and 491 deletions.
2 changes: 1 addition & 1 deletion armsrc/appmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -2258,7 +2258,7 @@ static void PacketReceived(PacketCommandNG *packet) {
}

case CMD_HF_SAM_PICOPASS: {
sam_picopass_get_pacs();
sam_picopass_get_pacs(packet);
break;
}
case CMD_HF_SAM_SEOS: {
Expand Down
73 changes: 73 additions & 0 deletions armsrc/sam_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,76 @@ void sam_send_ack(void) {

BigBuf_free();
}

/**
* @brief Copies the payload from an NFC buffer to a SAM buffer.
*
* Wraps received data from NFC into an ASN1 tree, so it can be transmitted to the SAM .
*
* @param sam_tx Pointer to the SAM transmit buffer.
* @param nfc_rx Pointer to the NFC receive buffer.
* @param nfc_len Length of the data to be copied from the NFC buffer.
*
* @return Length of SAM APDU to be sent.
*/
uint16_t sam_copy_payload_nfc2sam(uint8_t *sam_tx, uint8_t *nfc_rx, uint8_t nfc_len) {
// NFC resp:
// 6f 0c 84 0a a0 00 00 04 40 00 01 01 00 01 90 00 fb e3

// SAM req:
// bd 1c
// a0 1a
// a0 18
// 80 12
// 6f 0c 84 0a a0 00 00 04 40 00 01 01 00 01 90 00 fb e3
// 81 02
// 00 00

const uint8_t payload[] = {
0xbd, 4,
0xa0, 2,
0xa0, 0
};

const uint8_t tag81[] = {
0x00, 0x00
};

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, 0x81, tag81, sizeof(tag81));

return sam_tx[1] + 2; // length of the ASN1 tree
}

/**
* @brief Copies the payload from the SAM receive buffer to the NFC transmit buffer.
*
* Unpacks data to be transmitted from ASN1 tree in APDU received from SAM.
*
* @param nfc_tx_buf Pointer to the buffer where the NFC transmit data will be stored.
* @param sam_rx_buf Pointer to the buffer containing the data received from the SAM.
* @return Length of NFC APDU to be sent.
*/
uint16_t sam_copy_payload_sam2nfc(uint8_t *nfc_tx_buf, uint8_t *sam_rx_buf) {
// SAM resp:
// c1 61 c1 00 00
// a1 10 <- nfc command
// a1 0e <- nfc send
// 80 10 <- data
// 00 a4 04 00 0a a0 00 00 04 40 00 01 01 00 01 00
// 81 02 <- protocol
// 00 04
// 82 02 <- timeout
// 01 F4
// 90 00

// NFC req:
// 0C 05 DE 64

// copy data out of c1->a1>->a1->80 node
uint16_t nfc_tx_len = (uint8_t) * (sam_rx_buf + 10);
memcpy(nfc_tx_buf, sam_rx_buf + 11, nfc_tx_len);
return nfc_tx_len;
}
3 changes: 3 additions & 0 deletions armsrc/sam_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ void sam_append_asn1_node(const uint8_t *root, const uint8_t *node, uint8_t type

void sam_send_ack(void);

uint16_t sam_copy_payload_nfc2sam(uint8_t *sam_tx, uint8_t *nfc_rx, uint8_t nfc_len);
uint16_t sam_copy_payload_sam2nfc(uint8_t *nfc_tx_buf, uint8_t *sam_rx_buf);

#endif
Loading

0 comments on commit 50b66a6

Please sign in to comment.