]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/mifarehost.c
2 // people from mifare@nethemba.com, 2010
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 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
14 #include "mifarehost.h"
18 int compar_int(const void * a
, const void * b
) {
19 return (*(uint64_t*)b
- *(uint64_t*)a
);
22 // Compare countKeys structure
23 int compar_special_int(const void * a
, const void * b
) {
24 return (((countKeys
*)b
)->count
- ((countKeys
*)a
)->count
);
27 countKeys
* uniqsort(uint64_t * possibleKeys
, uint32_t size
) {
30 countKeys
*our_counts
;
32 qsort(possibleKeys
, size
, sizeof (uint64_t), compar_int
);
34 our_counts
= calloc(size
, sizeof(countKeys
));
35 if (our_counts
== NULL
) {
36 PrintAndLog("Memory allocation error for our_counts");
40 for (i
= 0; i
< size
; i
++) {
41 if (possibleKeys
[i
+1] == possibleKeys
[i
]) {
44 our_counts
[j
].key
= possibleKeys
[i
];
45 our_counts
[j
].count
= count
;
50 qsort(our_counts
, j
, sizeof(countKeys
), compar_special_int
);
54 int mfnested(uint8_t blockNo
, uint8_t keyType
, uint8_t * key
, uint8_t trgBlockNo
, uint8_t trgKeyType
, uint8_t * resultKeys
)
59 fnVector
* vector
= NULL
;
62 UsbCommand
* resp
= NULL
;
64 memset(resultKeys
, 0x00, 16 * 6);
67 while (WaitForResponseTimeout(CMD_ACK
, 500) != NULL
) ;
69 UsbCommand c
= {CMD_MIFARE_NESTED
, {blockNo
, keyType
, trgBlockNo
+ trgKeyType
* 0x100}};
70 memcpy(c
.d
.asBytes
, key
, 6);
80 printf("\naborted via keyboard!\n");
84 resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
87 isEOF
= resp
->arg
[0] & 0xff;
91 len
= resp
->arg
[1] & 0xff;
92 if (len
== 0) continue;
94 memcpy(&uid
, resp
->d
.asBytes
, 4);
95 PrintAndLog("uid:%08x len=%d trgbl=%d trgkey=%x", uid
, len
, resp
->arg
[2] & 0xff, (resp
->arg
[2] >> 8) & 0xff);
96 vector
= (fnVector
*) realloc((void *)vector
, (lenVector
+ len
) * sizeof(fnVector
) + 200);
98 PrintAndLog("Memory allocation error for fnVector. len: %d bytes: %d", lenVector
+ len
, (lenVector
+ len
) * sizeof(fnVector
));
102 for (i
= 0; i
< len
; i
++) {
103 vector
[lenVector
+ i
].blockNo
= resp
->arg
[2] & 0xff;
104 vector
[lenVector
+ i
].keyType
= (resp
->arg
[2] >> 8) & 0xff;
105 vector
[lenVector
+ i
].uid
= uid
;
107 memcpy(&vector
[lenVector
+ i
].nt
, (void *)(resp
->d
.asBytes
+ 8 + i
* 8 + 0), 4);
108 memcpy(&vector
[lenVector
+ i
].ks1
, (void *)(resp
->d
.asBytes
+ 8 + i
* 8 + 4), 4);
116 PrintAndLog("Got 0 keys from proxmark.");
119 printf("------------------------------------------------------------------\n");
122 struct Crypto1State
* revstate
= NULL
;
123 struct Crypto1State
* revstate_start
= NULL
;
128 if ((pk
= (void *) malloc(sizeof(pKeys
))) == NULL
) return 1;
129 memset(pk
, 0x00, sizeof(pKeys
));
131 for (m
= 0; m
< lenVector
; m
++) {
132 // And finally recover the first 32 bits of the key
133 revstate
= lfsr_recovery32(vector
[m
].ks1
, vector
[m
].nt
^ vector
[m
].uid
);
134 if (revstate_start
== NULL
) revstate_start
= revstate
;
136 while ((revstate
->odd
!= 0x0) || (revstate
->even
!= 0x0)) {
137 lfsr_rollback_word(revstate
, vector
[m
].nt
^ vector
[m
].uid
, 0);
138 crypto1_get_lfsr(revstate
, &lfsr
);
140 // Allocate a new space for keys
141 if (((kcount
% MEM_CHUNK
) == 0) || (kcount
>= pk
->size
)) {
142 pk
->size
+= MEM_CHUNK
;
143 //fprintf(stdout, "New chunk by %d, sizeof %d\n", kcount, pk->size * sizeof(uint64_t));
144 pk
->possibleKeys
= (uint64_t *) realloc((void *)pk
->possibleKeys
, pk
->size
* sizeof(uint64_t));
145 if (pk
->possibleKeys
== NULL
) {
146 PrintAndLog("Memory allocation error for pk->possibleKeys");
150 pk
->possibleKeys
[kcount
] = lfsr
;
154 free(revstate_start
);
155 revstate_start
= NULL
;
162 if ((pk
->possibleKeys
= (uint64_t *) realloc((void *)pk
->possibleKeys
, pk
->size
* sizeof(uint64_t))) == NULL
) {
163 PrintAndLog("Memory allocation error for pk->possibleKeys");
168 PrintAndLog("Total keys count:%d", kcount
);
169 ck
= uniqsort(pk
->possibleKeys
, pk
->size
);
172 for (i
= 0; i
< 16 ; i
++) {
173 num_to_bytes(ck
[i
].key
, 6, (uint8_t*)(resultKeys
+ i
* 6));
177 free(pk
->possibleKeys
);
185 int mfCheckKeys (uint8_t blockNo
, uint8_t keyType
, uint8_t keycnt
, uint8_t * keyBlock
, uint64_t * key
){
188 UsbCommand c
= {CMD_MIFARE_CHKKEYS
, {blockNo
, keyType
, keycnt
}};
189 memcpy(c
.d
.asBytes
, keyBlock
, 6 * keycnt
);
193 UsbCommand
* resp
= WaitForResponseTimeout(CMD_ACK
, 3000);
195 if (resp
== NULL
) return 1;
196 if ((resp
->arg
[0] & 0xff) != 0x01) return 2;
197 *key
= bytes_to_num(resp
->d
.asBytes
, 6);
203 int mfEmlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
204 UsbCommand c
= {CMD_MIFARE_EML_MEMGET
, {blockNum
, blocksCount
, 0}};
208 UsbCommand
* resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
210 if (resp
== NULL
) return 1;
211 memcpy(data
, resp
->d
.asBytes
, blocksCount
* 16);
215 int mfEmlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
216 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNum
, blocksCount
, 0}};
217 memcpy(c
.d
.asBytes
, data
, blocksCount
* 16);
224 int mfCSetUID(uint8_t *uid
, uint8_t *oldUID
, int wantWipe
) {
226 memset(block0
, 0, 16);
227 memcpy(block0
, uid
, 4);
228 block0
[4] = block0
[0]^block0
[1]^block0
[2]^block0
[3]; // Mifare UID BCC
229 // mifare classic SAK(byte 5) and ATQA(byte 6 and 7)
234 return mfCSetBlock(0, block0
, oldUID
, wantWipe
, CSETBLOCK_SINGLE_OPER
);
237 int mfCSetBlock(uint8_t blockNo
, uint8_t *data
, uint8_t *uid
, int wantWipe
, uint8_t params
) {
240 UsbCommand c
= {CMD_MIFARE_EML_CSETBLOCK
, {wantWipe
, params
& (0xFE | (uid
== NULL
? 0:1)), blockNo
}};
241 memcpy(c
.d
.asBytes
, data
, 16);
244 UsbCommand
* resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
247 isOK
= resp
->arg
[0] & 0xff;
248 if (uid
!= NULL
) memcpy(uid
, resp
->d
.asBytes
, 4);
251 PrintAndLog("Command execute timeout");
257 int mfCGetBlock(uint8_t blockNo
, uint8_t *data
, uint8_t params
) {
260 UsbCommand c
= {CMD_MIFARE_EML_CGETBLOCK
, {params
, 0, blockNo
}};
263 UsbCommand
* resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
266 isOK
= resp
->arg
[0] & 0xff;
267 memcpy(data
, resp
->d
.asBytes
, 16);
270 PrintAndLog("Command execute timeout");
279 char logHexFileName
[200] = {0x00};
280 static uint8_t traceCard
[4096];
281 static int traceState
= TRACE_IDLE
;
282 static uint8_t traceCurBlock
= 0;
283 static uint8_t traceCurKey
= 0;
285 struct Crypto1State
*traceCrypto1
= NULL
;
287 struct Crypto1State
*revstate
;
292 uint32_t uid
; // serial number
293 uint32_t nt
; // tag challenge
294 uint32_t nr_enc
; // encrypted reader challenge
295 uint32_t ar_enc
; // encrypted reader response
296 uint32_t at_enc
; // encrypted tag response
298 int mfTraceInit(uint8_t *tuid
, uint8_t *atqa
, uint8_t sak
) {
300 if (traceCrypto1
) crypto1_destroy(traceCrypto1
);
303 memset(traceCard
, 0x00, 4096);
304 memcpy(traceCard
, tuid
+ 3, 4);
305 traceCard
[4] = traceCard
[0] ^ traceCard
[1] ^ traceCard
[2] ^ traceCard
[3];
307 memcpy(&traceCard
[6], atqa
, 2);
309 uid
= bytes_to_num(tuid
+ 3, 4);
311 traceState
= TRACE_IDLE
;
316 void mf_crypto1_decrypt(struct Crypto1State
*pcs
, uint8_t *data
, int len
, bool isEncrypted
){
321 for (i
= 0; i
< len
; i
++)
322 data
[i
] = crypto1_byte(pcs
, 0x00, isEncrypted
) ^ data
[i
];
325 for (i
= 0; i
< 4; i
++)
326 bt
|= (crypto1_bit(pcs
, 0, isEncrypted
) ^ BIT(data
[0], i
)) << i
;
334 int mfTraceDecode(uint8_t *data_src
, int len
) {
337 if (traceState
== TRACE_ERROR
) return 1;
339 traceState
= TRACE_ERROR
;
343 memcpy(data
, data_src
, len
);
344 if ((traceCrypto1
) && ((traceState
== TRACE_IDLE
) || (traceState
> TRACE_AUTH_OK
))) {
345 mf_crypto1_decrypt(traceCrypto1
, data
, len
, 0);
346 PrintAndLog("dec> %s", sprint_hex(data
, len
));
347 AddLogHex(logHexFileName
, "dec> ", data
, len
);
350 switch (traceState
) {
352 // TODO: check packet crc16!
355 if ((len
==4) && ((data
[0] == 0x60) || (data
[0] == 0x61))) {
356 traceState
= TRACE_AUTH1
;
357 traceCurBlock
= data
[1];
358 traceCurKey
= data
[0] == 60 ? 1:0;
363 if ((len
==4) && ((data
[0] == 0x30))) {
364 traceState
= TRACE_READ_DATA
;
365 traceCurBlock
= data
[1];
370 if ((len
==4) && ((data
[0] == 0xA0))) {
371 traceState
= TRACE_WRITE_OK
;
372 traceCurBlock
= data
[1];
377 if ((len
==4) && ((data
[0] == 0x50) && (data
[1] == 0x00))) {
378 traceState
= TRACE_ERROR
; // do not decrypt the next commands
385 case TRACE_READ_DATA
:
387 traceState
= TRACE_IDLE
;
389 memcpy(traceCard
+ traceCurBlock
* 16, data
, 16);
392 traceState
= TRACE_ERROR
;
398 if ((len
== 1) && (data
[0] = 0x0a)) {
399 traceState
= TRACE_WRITE_DATA
;
403 traceState
= TRACE_ERROR
;
408 case TRACE_WRITE_DATA
:
410 traceState
= TRACE_IDLE
;
412 memcpy(traceCard
+ traceCurBlock
* 16, data
, 16);
415 traceState
= TRACE_ERROR
;
422 traceState
= TRACE_AUTH2
;
424 nt
= bytes_to_num(data
, 4);
427 traceState
= TRACE_ERROR
;
434 traceState
= TRACE_AUTH_OK
;
436 nr_enc
= bytes_to_num(data
, 4);
437 ar_enc
= bytes_to_num(data
+ 4, 4);
440 traceState
= TRACE_ERROR
;
447 traceState
= TRACE_IDLE
;
449 at_enc
= bytes_to_num(data
, 4);
453 ks2
= ar_enc
^ prng_successor(nt
, 64);
454 ks3
= at_enc
^ prng_successor(nt
, 96);
455 revstate
= lfsr_recovery64(ks2
, ks3
);
456 lfsr_rollback_word(revstate
, 0, 0);
457 lfsr_rollback_word(revstate
, 0, 0);
458 lfsr_rollback_word(revstate
, nr_enc
, 1);
459 lfsr_rollback_word(revstate
, uid
^ nt
, 0);
461 ks2
= ar_enc
^ prng_successor(nt
, 64);
462 ks3
= at_enc
^ prng_successor(nt
, 96);
463 revstate
= lfsr_recovery64(ks2
, ks3
);
464 lfsr_rollback_word(revstate
, 0, 0);
465 lfsr_rollback_word(revstate
, 0, 0);
466 lfsr_rollback_word(revstate
, nr_enc
, 1);
467 lfsr_rollback_word(revstate
, uid
^ nt
, 0);
469 crypto1_get_lfsr(revstate
, &lfsr
);
470 printf("key> %x%x\n", (unsigned int)((lfsr
& 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr
& 0xFFFFFFFF));
471 AddLogUint64(logHexFileName
, "key> ", lfsr
);
474 num_to_bytes(lfsr
, 6, traceCard
+ traceCurBlock
* 16 + 10);
476 num_to_bytes(lfsr
, 6, traceCard
+ traceCurBlock
* 16);
480 crypto1_destroy(traceCrypto1
);
483 // set cryptosystem state
484 traceCrypto1
= lfsr_recovery64(ks2
, ks3
);
486 // nt = crypto1_word(traceCrypto1, nt ^ uid, 1) ^ nt;
488 /* traceCrypto1 = crypto1_create(lfsr); // key in lfsr
489 crypto1_word(traceCrypto1, nt ^ uid, 0);
490 crypto1_word(traceCrypto1, ar, 1);
491 crypto1_word(traceCrypto1, 0, 0);
492 crypto1_word(traceCrypto1, 0, 0);*/
496 traceState
= TRACE_ERROR
;
502 traceState
= TRACE_ERROR
;