]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmfdesfire.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2014 Andy Davies
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE commands
9 //-----------------------------------------------------------------------------
13 #include <openssl/des.h>
14 #include <openssl/aes.h>
16 static int CmdHelp(const char *Cmd
);
19 // Reader 2 Card : 020A, key (1 byte), CRC1 CRC2 ; auth (020a00)
20 // Card 2 Reader : 02AF, 8 Bytes(b0), CRC1 CRC2
21 // Reader 2 Card : 03AF, 8 Bytes(b1),8 bytes(b2), CRC1 CRC2
22 // Card 2 Reader : 0300, 8 bytes(b3), CRC1 CRC2 ; success
24 //send 020A00, receive enc(nc)
32 int CmdHF14AMfDESAuth(const char *Cmd
){
39 uint8_t b1
[8]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
40 uint8_t b2
[8]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
41 DES_cblock nr
, b0
, r1
, r0
;
44 uint8_t key
[8]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
45 //DES_cblock iv={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
50 PrintAndLog("Usage: hf desfire des-auth k <key number>");
51 PrintAndLog(" sample: hf desfire des-auth k 0");
55 //Change key to user defined one
58 //memcpy(key2,key+8,8);
59 DES_set_key((DES_cblock
*)key1
,&ks1
);
60 //DES_set_key((DES_cblock *)key2,&ks2);
63 UsbCommand c
= {CMD_MIFARE_DES_AUTH1
, {blockNo
}};
66 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
67 uint8_t isOK
= resp
.arg
[0] & 0xff;
69 uint8_t * data
= resp
.d
.asBytes
;
72 PrintAndLog("enc(nc)/b0:%s", sprint_hex(data
+2,8));
76 PrintAndLog("Command execute timeout");
83 DES_ecb_encrypt(&nr
,&b1
,&ks1
,0);
84 DES_ecb_encrypt(&b0
,&r0
,&ks1
,0);
85 //PrintAndLog("b1:%s",sprint_hex(b1, 8));
86 PrintAndLog("r0:%s",sprint_hex(r0
, 8));
90 PrintAndLog("r1:%s",sprint_hex(r1
, 8));
92 b2
[i
]=(r1
[i
] ^ b1
[i
]);
94 DES_ecb_encrypt(&b2
,&b2
,&ks1
,0);
95 //PrintAndLog("b1:%s",sprint_hex(b1, 8));
96 PrintAndLog("b2:%s",sprint_hex(b2
, 8));
99 UsbCommand d
= {CMD_MIFARE_DES_AUTH2
, {cuid
}};
101 memcpy(reply
+8,b2
,8);
102 memcpy(d
.d
.asBytes
,reply
, 16);
106 if (WaitForResponseTimeout(CMD_ACK
,&respb
,1500)) {
107 uint8_t isOK
= respb
.arg
[0] & 0xff;
108 uint8_t * data2
= respb
.d
.asBytes
;
111 PrintAndLog("b3:%s", sprint_hex(data2
+2, 8));
115 PrintAndLog("Command execute timeout");
121 // Reader 2 Card : 02AA, key (1 byte), CRC1 CRC2 ; auth
122 // Card 2 Reader : 02AF, 16 Bytes(b0), CRC1 CRC2
123 // Reader 2 Card : 03AF, 16 Bytes(b1),16Bytes(b2) CRC1 CRC2
124 // Card 2 Reader : 0300, 16 bytes(b3), CRC1 CRC2 ; success
125 int CmdHF14AMfAESAuth(const char *Cmd
){
132 //unsigned char * b1, b2, nr, b0, r0, r1;
134 uint8_t b1
[16]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
135 uint8_t b2
[16]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
136 uint8_t nr
[16]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
137 uint8_t b0
[16]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
138 uint8_t r0
[16]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
139 uint8_t r1
[16]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
141 uint8_t key
[16]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
142 uint8_t iv
[16]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
147 PrintAndLog("Usage: hf desfire aes-auth k <key number>");
148 PrintAndLog(" sample: hf desfire aes-auth k 0");
152 //Change key to user defined one
154 // int private_AES_set_encrypt_key(const unsigned char *userKey, const int bits,AES_KEY *key);
155 //int private_AES_set_decrypt_key(const unsigned char *userKey, const int bits,AES_KEY *key);
157 //memcpy(key1,key,16);
158 //memcpy(key2,key+8,8);
159 AES_set_encrypt_key(key
,128,&key_e
);
160 AES_set_decrypt_key(key
,128,&key_d
);
163 UsbCommand c
= {CMD_MIFARE_DES_AUTH1
, {blockNo
}};
166 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
167 uint8_t isOK
= resp
.arg
[0] & 0xff;
169 uint8_t * data
= resp
.d
.asBytes
;
172 PrintAndLog("enc(nc)/b0:%s", sprint_hex(data
+2,16));
173 memcpy(b0
,data
+2,16);
176 PrintAndLog("Command execute timeout");
179 // void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
180 //size_t length, const AES_KEY *key,
181 //unsigned char *ivec, const int enc);
184 //DES_random_key(&nr);
187 //AES_cbc_encrypt(&nr,&b1,16,&key,0);
188 AES_cbc_encrypt(&b0
,&r0
,16,&key_d
,iv
,0);
189 //PrintAndLog("b1:%s",sprint_hex(b1, 8));
190 PrintAndLog("r0:%s",sprint_hex(r0
, 16));
194 PrintAndLog("r1:%s",sprint_hex(r1
, 16));
195 for(int i
=0;i
<16;i
++){
196 b1
[i
]=(nr
[i
] ^ b0
[i
]);
197 b2
[i
]=(r1
[i
] ^ b1
[i
]);
199 PrintAndLog("nr:%s",sprint_hex(nr
, 16));
200 AES_cbc_encrypt(&b1
,&b1
,16,&key_e
,iv
,1);
201 AES_cbc_encrypt(&b2
,&b2
,16,&key_e
,iv
,1);
202 PrintAndLog("b1:%s",sprint_hex(b1
, 16));
203 PrintAndLog("b2:%s",sprint_hex(b2
, 16));
206 UsbCommand d
= {CMD_MIFARE_DES_AUTH2
, {cuid
}};
208 memcpy(reply
+16,b2
,16);
209 memcpy(d
.d
.asBytes
,reply
, 32);
213 if (WaitForResponseTimeout(CMD_ACK
,&respb
,1500)) {
214 uint8_t isOK
= respb
.arg
[0] & 0xff;
215 uint8_t * data2
= respb
.d
.asBytes
;
218 PrintAndLog("b3:%s", sprint_hex(data2
+2, 16));
222 PrintAndLog("Command execute timeout");
228 //------------------------------------
230 //------------------------------------
231 static command_t CommandTable
[] =
233 {"help", CmdHelp
, 1,"This help"},
234 {"dbg", CmdHF14AMfDbg
, 0,"Set default debug mode"},
235 {"des-auth",CmdHF14AMfDESAuth
, 0,"Desfire Authentication"},
236 {"ev1-auth",CmdHF14AMfAESAuth
, 0,"EV1 Authentication"},
237 {NULL
, NULL
, 0, NULL
}
240 int CmdHFMFDesfire(const char *Cmd
){
242 clearCommandBuffer();
243 //WaitForResponseTimeout(CMD_ACK,NULL,100);
244 CmdsParse(CommandTable
, Cmd
);
248 int CmdHelp(const char *Cmd
){
249 CmdsHelp(CommandTable
);