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