]>
cvs.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 //-----------------------------------------------------------------------------
8 // High frequency ISO14443A commands
9 //-----------------------------------------------------------------------------
13 #include "mifarehost.h"
16 int compar_int(const void * a
, const void * b
) {
17 return (*(uint64_t*)b
- *(uint64_t*)a
);
20 // Compare countKeys structure
21 int compar_special_int(const void * a
, const void * b
) {
22 return (((countKeys
*)b
)->count
- ((countKeys
*)a
)->count
);
25 countKeys
* uniqsort(uint64_t * possibleKeys
, uint32_t size
) {
28 countKeys
*our_counts
;
30 qsort(possibleKeys
, size
, sizeof (uint64_t), compar_int
);
32 our_counts
= calloc(size
, sizeof(countKeys
));
33 if (our_counts
== NULL
) {
34 PrintAndLog("Memory allocation error for our_counts");
38 for (i
= 0; i
< size
; i
++) {
39 if (possibleKeys
[i
+1] == possibleKeys
[i
]) {
42 our_counts
[j
].key
= possibleKeys
[i
];
43 our_counts
[j
].count
= count
;
48 qsort(our_counts
, j
, sizeof(countKeys
), compar_special_int
);
52 int mfnested(uint8_t blockNo
, uint8_t keyType
, uint8_t * key
, uint8_t trgBlockNo
, uint8_t trgKeyType
, uint8_t * resultKeys
)
57 fnVector
* vector
= NULL
;
60 UsbCommand
* resp
= NULL
;
62 memset(resultKeys
, 0x00, 16 * 6);
65 while (WaitForResponseTimeout(CMD_ACK
, 500) != NULL
) ;
67 UsbCommand c
= {CMD_MIFARE_NESTED
, {blockNo
, keyType
, trgBlockNo
+ trgKeyType
* 0x100}};
68 memcpy(c
.d
.asBytes
, key
, 6);
78 printf("\naborted via keyboard!\n");
82 resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
85 isEOF
= resp
->arg
[0] & 0xff;
89 len
= resp
->arg
[1] & 0xff;
90 if (len
== 0) continue;
92 memcpy(&uid
, resp
->d
.asBytes
, 4);
93 PrintAndLog("uid:%08x len=%d trgbl=%d trgkey=%x", uid
, len
, resp
->arg
[2] & 0xff, (resp
->arg
[2] >> 8) & 0xff);
94 vector
= (fnVector
*) realloc((void *)vector
, (lenVector
+ len
) * sizeof(fnVector
) + 200);
96 PrintAndLog("Memory allocation error for fnVector. len: %d bytes: %d", lenVector
+ len
, (lenVector
+ len
) * sizeof(fnVector
));
100 for (i
= 0; i
< len
; i
++) {
101 vector
[lenVector
+ i
].blockNo
= resp
->arg
[2] & 0xff;
102 vector
[lenVector
+ i
].keyType
= (resp
->arg
[2] >> 8) & 0xff;
103 vector
[lenVector
+ i
].uid
= uid
;
105 memcpy(&vector
[lenVector
+ i
].nt
, (void *)(resp
->d
.asBytes
+ 8 + i
* 8 + 0), 4);
106 memcpy(&vector
[lenVector
+ i
].ks1
, (void *)(resp
->d
.asBytes
+ 8 + i
* 8 + 4), 4);
114 PrintAndLog("Got 0 keys from proxmark.");
117 printf("------------------------------------------------------------------\n");
120 struct Crypto1State
* revstate
= NULL
;
121 struct Crypto1State
* revstate_start
= NULL
;
126 if ((pk
= (void *) malloc(sizeof(pKeys
))) == NULL
) return 1;
127 memset(pk
, 0x00, sizeof(pKeys
));
129 for (m
= 0; m
< lenVector
; m
++) {
130 // And finally recover the first 32 bits of the key
131 revstate
= lfsr_recovery32(vector
[m
].ks1
, vector
[m
].nt
^ vector
[m
].uid
);
132 if (revstate_start
== NULL
) revstate_start
= revstate
;
134 while ((revstate
->odd
!= 0x0) || (revstate
->even
!= 0x0)) {
135 lfsr_rollback_word(revstate
, vector
[m
].nt
^ vector
[m
].uid
, 0);
136 crypto1_get_lfsr(revstate
, &lfsr
);
138 // Allocate a new space for keys
139 if (((kcount
% MEM_CHUNK
) == 0) || (kcount
>= pk
->size
)) {
140 pk
->size
+= MEM_CHUNK
;
141 //fprintf(stdout, "New chunk by %d, sizeof %d\n", kcount, pk->size * sizeof(uint64_t));
142 pk
->possibleKeys
= (uint64_t *) realloc((void *)pk
->possibleKeys
, pk
->size
* sizeof(uint64_t));
143 if (pk
->possibleKeys
== NULL
) {
144 PrintAndLog("Memory allocation error for pk->possibleKeys");
148 pk
->possibleKeys
[kcount
] = lfsr
;
152 free(revstate_start
);
153 revstate_start
= NULL
;
160 if ((pk
->possibleKeys
= (uint64_t *) realloc((void *)pk
->possibleKeys
, pk
->size
* sizeof(uint64_t))) == NULL
) {
161 PrintAndLog("Memory allocation error for pk->possibleKeys");
166 PrintAndLog("Total keys count:%d", kcount
);
167 ck
= uniqsort(pk
->possibleKeys
, pk
->size
);
170 for (i
= 0; i
< 16 ; i
++) {
171 num_to_bytes(ck
[i
].key
, 6, (uint8_t*)(resultKeys
+ i
* 6));
175 free(pk
->possibleKeys
);
183 int mfCheckKeys (uint8_t blockNo
, uint8_t keyType
, uint8_t keycnt
, uint8_t * keyBlock
, uint64_t * key
){
186 UsbCommand c
= {CMD_MIFARE_CHKKEYS
, {blockNo
, keyType
, keycnt
}};
187 memcpy(c
.d
.asBytes
, keyBlock
, 6 * keycnt
);
191 UsbCommand
* resp
= WaitForResponseTimeout(CMD_ACK
, 3000);
193 if (resp
== NULL
) return 1;
194 if ((resp
->arg
[0] & 0xff) != 0x01) return 2;
195 *key
= bytes_to_num(resp
->d
.asBytes
, 6);
199 int mfEmlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
200 UsbCommand c
= {CMD_MIFARE_EML_MEMGET
, {blockNum
, blocksCount
, 0}};
204 UsbCommand
* resp
= WaitForResponseTimeout(CMD_ACK
, 1500);
206 if (resp
== NULL
) return 1;
207 memcpy(data
, resp
->d
.asBytes
, blocksCount
* 16);
211 int mfEmlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
212 UsbCommand c
= {CMD_MIFARE_EML_MEMSET
, {blockNum
, blocksCount
, 0}};
213 memcpy(c
.d
.asBytes
, data
, blocksCount
* 16);