hf seos - added the skeleton for future use
[RRG-proxmark3.git] / armsrc / desfire.h
blob507bda42454ad69ed9bee6c75960e6724b2337da
1 #ifndef __DESFIRE_H
2 #define __DESFIRE_H
4 #include "common.h"
5 #include "mifare.h"
7 #define MAX_CRYPTO_BLOCK_SIZE 16
8 /* Mifare DESFire EV1 Application crypto operations */
9 #define APPLICATION_CRYPTO_DES 0x00
10 #define APPLICATION_CRYPTO_3K3DES 0x40
11 #define APPLICATION_CRYPTO_AES 0x80
13 #define MAC_LENGTH 4
14 #define CMAC_LENGTH 8
16 typedef enum {
17 MCD_SEND,
18 MCD_RECEIVE
19 } MifareCryptoDirection;
21 typedef enum {
22 MCO_ENCYPHER,
23 MCO_DECYPHER
24 } MifareCryptoOperation;
26 #define MDCM_MASK 0x000F
28 #define CMAC_NONE 0
30 // Data send to the PICC is used to update the CMAC
31 #define CMAC_COMMAND 0x010
32 // Data received from the PICC is used to update the CMAC
33 #define CMAC_VERIFY 0x020
35 // MAC the command (when MDCM_MACED)
36 #define MAC_COMMAND 0x100
37 // The command returns a MAC to verify (when MDCM_MACED)
38 #define MAC_VERIFY 0x200
40 #define ENC_COMMAND 0x1000
41 #define NO_CRC 0x2000
43 #define MAC_MASK 0x0F0
44 #define CMAC_MACK 0xF00
46 /* Communication mode */
47 #define MDCM_PLAIN 0x00
48 #define MDCM_MACED 0x01
49 #define MDCM_ENCIPHERED 0x03
51 /* Error code managed by the library */
52 #define CRYPTO_ERROR 0x01
55 enum DESFIRE_AUTH_SCHEME {
56 AS_LEGACY,
57 AS_NEW
60 enum DESFIRE_CRYPTOALGO {
61 T_DES = 0x00,
62 T_3DES = 0x01,
63 T_3K3DES = 0x02,
64 T_AES = 0x03,
65 T_2K3DES = 0x04
69 #define DESFIRE_KEY(key) ((struct desfire_key *) key)
70 struct desfire_key {
71 enum DESFIRE_CRYPTOALGO type;
72 uint8_t data[24];
73 uint8_t cmac_sk1[24];
74 uint8_t cmac_sk2[24];
75 uint8_t aes_version;
77 typedef struct desfire_key *desfirekey_t;
79 #define DESFIRE(tag) ((struct desfire_tag *) tag)
80 struct desfire_tag {
81 iso14a_card_select_t info;
82 int active;
83 uint8_t last_picc_error;
84 uint8_t last_internal_error;
85 uint8_t last_pcd_error;
86 desfirekey_t session_key;
87 enum DESFIRE_AUTH_SCHEME authentication_scheme;
88 uint8_t authenticated_key_no;
90 uint8_t ivect[MAX_CRYPTO_BLOCK_SIZE];
91 uint8_t cmac[16];
92 uint8_t *crypto_buffer;
93 size_t crypto_buffer_size;
94 uint32_t selected_application;
96 typedef struct desfire_tag *desfiretag_t;
99 /* File types */
100 enum DESFIRE_FILE_TYPES {
101 MDFT_STANDARD_DATA_FILE = 0x00,
102 MDFT_BACKUP_DATA_FILE = 0x01,
103 MDFT_VALUE_FILE_WITH_BACKUP = 0x02,
104 MDFT_LINEAR_RECORD_FILE_WITH_BACKUP = 0x03,
105 MDFT_CYCLIC_RECORD_FILE_WITH_BACKUP = 0x04
108 enum DESFIRE_STATUS {
109 OPERATION_OK = 0x00,
110 NO_CHANGES = 0x0c,
111 OUT_OF_EEPROM_ERROR = 0x0e,
112 ILLEGAL_COMMAND_CODE = 0x1c,
113 INTEGRITY_ERROR = 0x1e,
114 NO_SUCH_KEY = 0x40,
115 LENGTH_ERROR = 0x7e,
116 PERMISSION_DENIED = 0x9d,
117 PARAMETER_ERROR = 0x9e,
118 APPLICATION_NOT_FOUND = 0xa0,
119 APPL_INTEGRITY_ERROR = 0xa1,
120 AUTHENTICATION_ERROR = 0xae,
121 ADDITIONAL_FRAME = 0xaf,
122 BOUNDARY_ERROR = 0xbe,
123 PICC_INTEGRITY_ERROR = 0xc1,
124 COMMAND_ABORTED = 0xca,
125 PICC_DISABLED_ERROR = 0xcd,
126 COUNT_ERROR = 0xce,
127 DUPLICATE_ERROR = 0xde,
128 EEPROM_ERROR = 0xee,
129 FILE_NOT_FOUND = 0xf0,
130 FILE_INTEGRITY_ERROR = 0xf1
133 enum DESFIRE_CMD {
134 CREATE_APPLICATION = 0xca,
135 DELETE_APPLICATION = 0xda,
136 GET_APPLICATION_IDS = 0x6a,
137 SELECT_APPLICATION = 0x5a,
138 FORMAT_PICC = 0xfc,
139 GET_VERSION = 0x60,
140 READ_DATA = 0xbd,
141 WRITE_DATA = 0x3d,
142 GET_VALUE = 0x6c,
143 CREDIT = 0x0c,
144 DEBIT = 0xdc,
145 LIMITED_CREDIT = 0x1c,
146 WRITE_RECORD = 0x3b,
147 READ_RECORDS = 0xbb,
148 CLEAR_RECORD_FILE = 0xeb,
149 COMMIT_TRANSACTION = 0xc7,
150 ABORT_TRANSACTION = 0xa7,
151 GET_FREE_MEMORY = 0x6e,
152 GET_FILE_IDS = 0x6f,
153 GET_FILE_SETTINGS = 0xf5,
154 GET_DF_NAMES = 0x6d,
155 CHANGE_FILE_SETTINGS = 0x5f,
156 CREATE_STD_DATA_FILE = 0xcd,
157 CREATE_BACKUP_DATA_FILE = 0xcb,
158 CREATE_VALUE_FILE = 0xcc,
159 CREATE_LINEAR_RECORD_FILE = 0xc1,
160 CREATE_CYCLIC_RECORD_FILE = 0xc0,
161 DELETE_FILE = 0xdf,
162 AUTHENTICATE = 0x0a, // AUTHENTICATE_NATIVE
163 AUTHENTICATE_ISO = 0x1a, // AUTHENTICATE_STANDARD
164 AUTHENTICATE_AES = 0xaa,
165 CHANGE_KEY_SETTINGS = 0x54,
166 GET_KEY_SETTINGS = 0x45,
167 CHANGE_KEY = 0xc4,
168 GET_KEY_VERSION = 0x64,
169 AUTHENTICATION_FRAME = 0xAF
172 #endif