//----------------------------------------------------------------------------- // 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. //----------------------------------------------------------------------------- #ifndef __SAM_COMMON_H #define __SAM_COMMON_H #include "common.h" #include "mifare.h" static const uint8_t SAM_TX_APDU_PREFIX_LENGTH = 5; static const uint8_t SAM_TX_ASN1_PREFIX_LENGTH = 6; static const uint8_t SAM_RX_ASN1_PREFIX_LENGTH = 5; // Offset of the 0xBD response node - 5 or 6 depending on the SAM. 0 if absent. uint16_t sam_bd_offset(const uint8_t *response, uint16_t response_len); // Offset of the response node plus how much of it to forward. See the .c file. uint16_t sam_rx_prefix_len(const uint8_t *rx, uint16_t rx_len); bool sam_relay_pending(const uint8_t *rx, uint16_t rx_len); bool sam_relay_complete(const uint8_t *rx, uint16_t rx_len); uint16_t sam_response_payload(const uint8_t *rx, uint16_t rx_len, uint16_t *payload_len); int sam_rxtx(const uint8_t *data, uint16_t n, uint8_t *resp, uint16_t *resplen); // Backwards-compatible wrapper that calls sam_send_payload_ex with scFlag=0x00. int sam_send_payload( const uint8_t addr_src, const uint8_t addr_dest, const uint8_t addr_reply, const uint8_t *const payload, const uint16_t *payload_len, uint8_t *response, uint16_t *response_len ); // Extended variant that lets the caller set the Grace routing scFlag byte. // // The Grace routing header is 6 bytes: FROM, TO, REPLY-TO, 0x00, 0x00, scFlag. // During InitAuth the scFlag is 0x00; after the SAM authenticates the host it // returns a session-bound scFlag (typically 0x81) that must be echoed in the // routing header of every subsequent wrapped APDU (ContinueAuth, wrap/unwrap). // The original sam_send_payload hardcoded scFlag=0x00 which made it impossible // to drive a real secure-channel session. New SC code paths must use this _ex // variant instead. int sam_send_payload_ex( const uint8_t addr_src, const uint8_t addr_dest, const uint8_t addr_reply, const uint8_t scFlag, const uint8_t *const payload, const uint16_t *payload_len, uint8_t *response, uint16_t *response_len ); int sam_get_version(bool info); int sam_get_serial_number(void); 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_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, uint16_t sam_rx_len); // NOTE: derived from sam_picopass.c::sam_send_request_iso15's relay loop. int sam_relay_iso15_loop( uint8_t *sam_tx_buf, uint8_t *sam_rx_buf, uint16_t *sam_rx_len, bool shallow_mod, bool break_on_nr_mac, bool prevent_epurse_update, uint8_t *nr_mac_out, uint16_t *nr_mac_len_out, bool *got_nr_mac ); int sam_set_card_detected_iso14a(const iso14a_card_select_t *card_select, bool with_atqa); uint16_t sam_copy_payload_nfc2sam_status(uint8_t *sam_tx, const uint8_t *nfc_rx, uint8_t nfc_len, uint16_t rf_status); bool sam_nfc_send_format(const uint8_t *sam_rx_buf, uint16_t sam_rx_len, uint8_t *format); // nfcRx rfStatus values, as Seader reports them #define SAM_RF_STATUS_OK 0x0000 #define SAM_RF_STATUS_TIMEOUT 0x0020 #define SAM_RF_STATUS_PROTOCOL 0x0004 // Capture of UID and plaintext SIO for the extended PACS reply (sam_ext_reply_hdr_t) void sam_capture_reset(void); void sam_capture_uid(const uint8_t *uid, uint8_t uid_len); void sam_capture_iclass(const uint8_t *tx, uint16_t tx_len, const uint8_t *rx, uint16_t rx_len); void sam_capture_desfire(const uint8_t *tx, uint16_t tx_len, const uint8_t *rx, uint16_t rx_len); void sam_reply_pacs(uint16_t cmd, bool ext, const uint8_t *sam, uint8_t sam_len); #endif