sam_mfc.c was empty and CMD_HF_SAM_MFC was a no-op. Implement the relay the way Seader's plugin_hf_mfc_transmit does it. The SAM runs Crypto1 itself and tags each nfcSend with a format: - 00 C0 00: plain bytes, CRC already included, parity generated - 00 00 40 / 24 / 44: air order bit stream carrying the SAM's own parity Answers to the latter go back in the same bit form, len + len / 8 + 1 bytes like Seader. Unlike Seader, every byte gets its real parity bit; Seader leaves bytes 8 and on at zero. A silent card is reported to the SAM as rfStatus 00 20, an unknown format as 00 04, Seader's codes, and the conversation continues. Card detected follows Seader for MIFARE Classic: protocol, CSN and SAK, no ATQA. The card is selected without RATS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
115 lines
4.4 KiB
C
115 lines
4.4 KiB
C
//-----------------------------------------------------------------------------
|
|
// 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
|