C API for BALTECH SDK
mif.h File Reference

With this command group, you can access MIFARE cards in low-level mode. More...

#include "../typedefs.h"

Go to the source code of this file.

Macros

#define BRP_Mif_ErrNoTag   BRP_ERR_STATUS(0x1000, 0x01)
 There's no card in the HF field, or the card doesn't respond. More...
 
#define BRP_Mif_ErrCrc   BRP_ERR_STATUS(0x1000, 0x02)
 The response frame is invalid, e.g. More...
 
#define BRP_Mif_ErrAuth   BRP_ERR_STATUS(0x1000, 0x04)
 Card authentication has failed. More...
 
#define BRP_Mif_ErrParity   BRP_ERR_STATUS(0x1000, 0x05)
 Legacy error code: The parity bits don't match the transmitted data. More...
 
#define BRP_Mif_ErrCode   BRP_ERR_STATUS(0x1000, 0x06)
 The card behaves in an unspecified way. More...
 
#define BRP_Mif_ErrSnr   BRP_ERR_STATUS(0x1000, 0x08)
 Legacy error code: The serial number is wrong. More...
 
#define BRP_Mif_ErrKey   BRP_ERR_STATUS(0x1000, 0x09)
 The key in the SAM/crypto memory is invalid or missing. More...
 
#define BRP_Mif_ErrNotauth   BRP_ERR_STATUS(0x1000, 0x0A)
 Card authentication has failed. More...
 
#define BRP_Mif_ErrBitcount   BRP_ERR_STATUS(0x1000, 0x0B)
 Legacy error code: HF data transition error. More...
 
#define BRP_Mif_ErrBytecount   BRP_ERR_STATUS(0x1000, 0x0C)
 Legacy error code: HF data transition error. More...
 
#define BRP_Mif_VcsAndProxCheckError   BRP_ERR_STATUS(0x1000, 0x0E)
 The proximity check has timed out. More...
 
#define BRP_Mif_ErrWrite   BRP_ERR_STATUS(0x1000, 0x0F)
 Writing to the card has failed. More...
 
#define BRP_Mif_ErrInc   BRP_ERR_STATUS(0x1000, 0x10)
 Legacy error code: Increment couldn't be performed. More...
 
#define BRP_Mif_ErrDecr   BRP_ERR_STATUS(0x1000, 0x11)
 Legacy error code: Decrement couldn't be performed. More...
 
#define BRP_Mif_ErrRead   BRP_ERR_STATUS(0x1000, 0x12)
 Reading data from the card has failed. More...
 
#define BRP_Mif_ErrOvfl   BRP_ERR_STATUS(0x1000, 0x13)
 Legacy error code: An overflow occurred during decrement or increment. More...
 
#define BRP_Mif_ErrFraming   BRP_ERR_STATUS(0x1000, 0x15)
 The response frame is invalid, e.g. More...
 
#define BRP_Mif_ErrBreak   BRP_ERR_STATUS(0x1000, 0x16)
 The command has been aborted because the HF interface has been requested by another task or command. More...
 
#define BRP_Mif_ErrCmd   BRP_ERR_STATUS(0x1000, 0x17)
 The specified command or parameters are unknown. More...
 
#define BRP_Mif_ErrColl   BRP_ERR_STATUS(0x1000, 0x18)
 An error occurred in the anti-collision sequence. More...
 
#define BRP_Mif_ErrReaderChipCommunication   BRP_ERR_STATUS(0x1000, 0x1A)
 Communication with the reader's HF interface has failed. More...
 
#define BRP_Mif_ErrFirmwareNotSupported   BRP_ERR_STATUS(0x1000, 0x1D)
 This command isn't supported by the reader firmware. More...
 
#define BRP_Mif_ErrVal   BRP_ERR_STATUS(0x1000, 0x1E)
 A value operation, e.g. More...
 
#define BRP_Mif_ErrIntegrity   BRP_ERR_STATUS(0x1000, 0x1F)
 Secure messaging error: The CRC or MAC checksum doesn't match the transmitted data. More...
 
#define BRP_Mif_CondNotvalid   BRP_ERR_STATUS(0x1000, 0x20)
 Card error as per MIFARE specification: Condition of use not satisfied. More...
 
#define BRP_Mif_ErrHwNotSupported   BRP_ERR_STATUS(0x1000, 0x21)
 This command isn't supported by the reader hardware, i.e. More...
 
#define BRP_Mif_ErrSamUnlock   BRP_ERR_STATUS(0x1000, 0x22)
 Unlocking/authenticating with the SAM has failed. More...
 
#define BRP_Mif_ErrSamCommunication   BRP_ERR_STATUS(0x1000, 0x23)
 Communication with the SAM has failed. More...
 

Functions

brp_errcode brp_Mif_LoadKey (brp_protocol protocol, unsigned KeyIdx, brp_buf Key)
 This command writes a MIFARE Classic key to the reader's secure key memory. More...
 
brp_errcode brp_Mif_Request (brp_protocol protocol, unsigned ReqAll, unsigned *ATQA)
 Request labels in the field of the antenna. More...
 
brp_errcode brp_Mif_Anticoll (brp_protocol protocol, unsigned BitCount, brp_buf PreSelSer, brp_buf *Snr, brp_mempool *mempool)
 This command performs an anti-collision sequence. More...
 
brp_errcode brp_Mif_Select (brp_protocol protocol, brp_buf Snr, unsigned *SAK)
 This command selects a card with a 4 Byte serial number specified in the Snr parameter. More...
 
brp_errcode brp_Mif_AuthE2 (brp_protocol protocol, unsigned AuthMode, unsigned Block, unsigned KeyIdx)
 This command authenticates a certain sector of a card using a key from the secure EEPROM of the Baltech reader chip. More...
 
brp_errcode brp_Mif_AuthUser (brp_protocol protocol, unsigned AuthMode, unsigned Block, brp_buf Key)
 This command authenticates a certain sector of a card using the key specified in the Key variable. More...
 
brp_errcode brp_Mif_Read (brp_protocol protocol, unsigned Block, brp_buf *BlockData, brp_mempool *mempool)
 This command reads data from a specified block of the currently selected card, providing authentication has been performed beforehand. More...
 
brp_errcode brp_Mif_Write (brp_protocol protocol, unsigned Block, brp_buf BlockData)
 This command write data to a specified block of the currently selected card, providing authentication has been performed beforehand. More...
 
brp_errcode brp_Mif_ChangeValue (brp_protocol protocol, unsigned Mode, unsigned Block, unsigned Value)
 This command uses the value block specified by the Block parameter and performs an operation given by the Mode parameter. More...
 
brp_errcode brp_Mif_ChangeValueBackup (brp_protocol protocol, unsigned Mode, unsigned Block, unsigned Value)
 This command is identical to brp_Mif_ChangeValue(), but can only be used for MIFARE cards which support automatic transfer, such as Pro or Light MIFARE variants. More...
 
brp_errcode brp_Mif_TransferBlock (brp_protocol protocol, unsigned Block)
 This command transfers data from the card's internal Transfer Buffer to a specified block. More...
 
brp_errcode brp_Mif_Halt (brp_protocol protocol)
 Switch card to halt state. More...
 
brp_errcode brp_Mif_AuthE2Extended (brp_protocol protocol, unsigned AuthLevel, bool KeyHasExtIdx, unsigned EV1Mode, bool IsKeyB, unsigned Block, unsigned KeyIdx, unsigned KeyExtIdx, brp_buf DivData, size_t DivData_len)
 This command is identical to the brp_Mif_AuthE2() command with the exception that it supports stronger authentication methods (MIFARE and AES), supported by MIFARE Pro cards. More...
 
brp_errcode brp_Mif_AuthUserExtended (brp_protocol protocol, unsigned AuthLevel, unsigned EV1Mode, unsigned KeyB, unsigned Block, brp_buf Key, size_t Key_len)
 This command is identical to the brp_Mif_AuthUser() command with the exception that it supports stronger authentication methods (MIFARE and AES), supported by MIFARE Pro cards. More...
 
brp_errcode brp_Mif_ResetAuth (brp_protocol protocol)
 This command resets the reader's authentication state (used for MIFARE Pro specific Read/Write counters. More...
 
brp_errcode brp_Mif_ReadSL3 (brp_protocol protocol, unsigned NoMacOnCmd, unsigned PlainData, unsigned NoMacOnResp, unsigned Block, unsigned BlockNr, brp_buf *BlockData, size_t *BlockData_len, brp_mempool *mempool)
 This command reads blocks from an SL3-authenticated MIFARE Pro card. More...
 
brp_errcode brp_Mif_WriteSL3 (brp_protocol protocol, unsigned PlainData, unsigned NoMacOnResp, unsigned Block, brp_buf BlockData, size_t BlockData_len)
 This command writes blocks to an SL3-authenticated MIFARE Pro card. More...
 
brp_errcode brp_Mif_ChangeAESKey (brp_protocol protocol, bool KeyHasExtIdx, unsigned NoMacOnResp, unsigned Block, unsigned KeyIdx, unsigned KeyExtIdx, brp_buf DivData, size_t DivData_len)
 This command changes an AES key on a MIFARE Plus card. More...
 
brp_errcode brp_Mif_ValueSL3 (brp_protocol protocol, unsigned NoMacOnResp, unsigned Cmd, unsigned Block, unsigned DestBlock, unsigned Value, brp_buf *TMCounterTMValue, size_t *TMCounterTMValue_len, brp_mempool *mempool)
 This command performs an operation on a value block. More...
 
brp_errcode brp_Mif_ProxCheck (brp_protocol protocol, unsigned M, unsigned DisableIsoWrapping, bool UseExtProxKey, bool DiversifyProxKey, bool UseProxKey, unsigned ProxKeyIdx, brp_buf DivData, size_t DivData_len, brp_buf ProxKey, size_t ProxKey_len)
 This command performs a proximity check. More...
 
brp_errcode brp_Mif_GetCardVersion (brp_protocol protocol, brp_buf *CardVersion, size_t *CardVersion_len, brp_mempool *mempool)
 This command returns HW- / SW- / Production-Information. More...
 
brp_errcode brp_Mif_ReadSig (brp_protocol protocol, brp_buf *NxpSignature, size_t *NxpSignature_len, brp_mempool *mempool)
 
brp_errcode brp_Mif_VirtualCardSelect (brp_protocol protocol, bool ForceVcsAuthentication, bool UseExtVcSelectKeys, unsigned DiversifyMacKey, bool DiversifyEncKey, bool UseVcSelectKeys, brp_buf IID, size_t IID_len, unsigned EncKeyIdx, unsigned MacKeyIdx, brp_buf DivData, size_t DivData_len, brp_buf EncKey, size_t EncKey_len, brp_buf MacKey, size_t MacKey_len, unsigned *FciType, brp_buf *Fci, size_t *Fci_len, brp_mempool *mempool)
 Command is only supported by MIFARE Plus EV1 cards. More...
 
brp_errcode brp_Mif_SectorSwitch (brp_protocol protocol, bool L3SectorSwitch, unsigned SectorSwitchKeyIdx, brp_buf SectorSwitchKeyDivData, size_t SectorSwitchKeyDivData_len, brp_Mif_SectorSwitch_SectorSpec_Entry *SectorSpec, size_t SectorSpec_len, brp_buf SectorKeysDivData, size_t SectorKeysDivData_len)
 This command performs a sector switch command (only for EV1 cards). More...
 
brp_errcode brp_Mif_CommitReaderID (brp_protocol protocol, unsigned Block, brp_buf *EncTRI, size_t *EncTRI_len, brp_mempool *mempool)
 This commands commits a reader ID from a card and returns the encrypted TMRI to the host. More...
 
brp_errcode brp_Mif_SetFraming (brp_protocol protocol, brp_Mif_SetFraming_CommMode CommMode)
 This command switches the communication protocol mode for MIFARE Plus EV1 cards. More...
 

Detailed Description

With this command group, you can access MIFARE cards in low-level mode.

Whenever a card enters the HF field, it will be in idle state. From the idle state, a card switches to the requested state as soon as the brp_Mif_Request() command is executed. Cards in requested state participate in the anti-collision sequence, controlled by the brp_Mif_Anticoll() command, resulting in an unambiguous serial number belonging to one of the cards in the HF field. The card with this serial number may now be selected with the brp_Mif_Select() command. All other cards will fall back to idle state again and wait for the next call of the brp_Mif_Request() command. The selected card, however, changes from requested state to selected state.

The brp_Mif_Anticoll() and brp_Mif_Select() commands can only process serial numbers with a length of 4 bytes. Since 2010, however, MIFARE cards with a 7-byte serial number are available. Please use theiso14a.h to select and process cards with 7-byte serial numbers.

When a card is in selected state, communication with the card is possible. Nevertheless, it is necessary to be authenticated before a sector of the card can be accessed. Authentication can be performed either with the brp_Mif_AuthE2() command or with the brp_Mif_AuthUser() command. Both commands send a key to the currently selected card along with information regarding whether this key should be compared to key A or key B on the card. If the given key matches the corresponding sector's key (either key A or key B, as specified in the command's parameters), then access according to the access conditions of the desired sector will be granted.

Finally, a card that is in select state can be set to halt state. A card in halt state will not respond to any command unless a specially parameterized brp_Mif_Request() command is called, or when it leaves and then re- enters the HF field of the reader's antenna. The halt state is helpful when it is necessary to deal with multiple cards present in the reader's antenna HF field.

The brp_Mif_Read() and brp_Mif_Write() commands can be use to read/write data from/to a certain block. Additionally, a data sector may be turned into a special value sector (also called value block), which is initialized with a specific starting value and can then only be changed by means of increment and decrement operations using the ChangeValue or ChangeValueBackup commands. Value sectors are very useful in some scenarios, such as for cash-based applications. To create a value sector, it is simply required to write specially formatted data to it:

  • The first 4 bytes (bytes 0-3) represent the initially stored value.
  • The following 4 bytes (bytes 4-7) have to be the two's complement of the first 4 bytes.
  • The next 4 bytes (bytes 8-11) are a copy of the first 4 bytes.
  • The last 4 bytes (bytes 12-15) contain the 1-byte address of a block which can be used for backup management. This address is stored on both byte 12 and byte 14, while its two's complement is stored on both byte 13 and byte 15. These 4 bytes are not affected by calls of the brp_Mif_ChangeValue() and brp_Mif_ChangeValueBackup() commands.

Each increment and decrement operation has to be followed by a transfer operation, using the brp_Mif_TransferBlock() command, in order to make the increment/decrement persistent. This feature guarantees that no data will be lost during a recalculation of the value on the card.

Definition in file mif.h.