This repository has been archived by the owner on Jun 27, 2022. It is now read-only.
forked from telamon/mfkeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfc-helper.c
184 lines (147 loc) · 4.51 KB
/
nfc-helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
This file is a part of mfkeys
Copyright (c) 2010 Christian Panton <christian@panton.org>
mfkeys is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
mfkeys is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with mfkeys. If not, see <http://www.gnu.org/licenses/>.
*/
#include <nfc/nfc.h>
#include <nfc/nfc-types.h>
#include <string.h>
#include <stdlib.h>
#include "mifare.h"
#include "nfc-helper.h"
// from libnfc: utils/nfc-mfultralight.c
static const nfc_modulation nmMifare = {
.nmt = NMT_ISO14443A,
.nbr = NBR_106,
};
bool mf_configure(nfc_device *reader)
{
bool nfc_status;
nfc_status = (nfc_initiator_init(reader) == 0);
nfc_status &= (nfc_device_set_property_bool(reader, NP_ACTIVATE_FIELD, false) == 0);
nfc_status &= (nfc_device_set_property_bool(reader, NP_INFINITE_SELECT, false) == 0);
nfc_status &= (nfc_device_set_property_bool(reader, NP_HANDLE_CRC, true) == 0);
nfc_status &= (nfc_device_set_property_bool(reader, NP_HANDLE_PARITY, true) == 0);
nfc_status &= (nfc_device_set_property_bool(reader, NP_ACTIVATE_FIELD, true) == 0);
nfc_status &= (nfc_device_set_property_bool(reader, NP_AUTO_ISO14443_4, false) == 0);
return nfc_status;
}
bool mf_anticol(nfc_device *reader, nfc_target *target)
{
if(nfc_initiator_select_passive_target(reader, nmMifare, NULL, 0, target))
{
if(target == NULL)
return true;
if ((target->nti.nai.btSak & 0x08) == 0) {
fprintf (stderr, "Error: tag is not a MIFARE Classic card\n");
return false;
}
else
{
return true;
}
}
fprintf(stderr, "Error: No tag was found\n");
return false;
}
bool mf_checkkey(nfc_device *reader, byte_t *uid, uint8_t sector, uint8_t keytype, byte_t *key)
{
mifare_param param;
mifare_cmd mc;
uint8_t block;
memcpy(param.mpa.abtUid, uid, sizeof(param.mpa.abtUid));
memcpy(param.mpa.abtKey, key, sizeof(param.mpa.abtKey));
mc = keytype == 0 ? MC_AUTH_A : MC_AUTH_B;
block = sector * 4;
if(sector > 15)
block = 64 + (sector-16)*16;
if(nfc_initiator_mifare_cmd(reader, mc, block, ¶m))
{
return true;
}
else{
mf_anticol(reader, NULL);
return false;
}
}
int mf_check_card(nfc_device *reader, byte_t *uid, uint8_t numsector, byte_t *key, byte_t *amap, byte_t *bmap, byte_t *akeys, byte_t *bkeys)
{
int i;
int c = 0;
for(i = 0; i < numsector; i++)
{
if(!amap[i])
{
if(mf_checkkey(reader, uid, i, 0, key))
{
amap[i] = true;
memcpy(&akeys[i], key, 6);
c++;
}
}
if(!bmap[i])
{
if(mf_checkkey(reader, uid, i, 1, key))
{
bmap[i] = true;
memcpy(&bkeys[i], key, 6);
c++;
}
}
}
return c;
}
bool mf_dumpsector(nfc_device *reader, uint8_t sector, byte_t **data, uint8_t *datalen)
{
mifare_param mf_param;
int i, j;
int block = sector * 4;
if(sector > 15)
block = 64 + (sector-16)*16;
int numblocks = sector > 16 ? 16 : 4;
*datalen = numblocks*16;
*data = (byte_t *) malloc(sizeof(byte_t) * numblocks * 16);
for(i = 0; i < *datalen; i++)
{
(*data)[i] = 0; // ensure nulls
}
for(i = 0; i < numblocks; i++)
{
if(nfc_initiator_mifare_cmd(reader, MC_READ, block + i, &mf_param))
{
memcpy(*data + i * sizeof(byte_t) * 16, mf_param.mpd.abtData, 16);
}
else
{
return false;
}
}
return true;
}
long long unsigned int bytes_to_num(byte_t* src, uint32_t len) {
uint64_t num = 0;
while (len--)
{
num = (num << 8) | (*src);
src++;
}
return num;
}
void hexprint(byte_t* data, uint8_t len) {
int i;
for(i = 0; i < len; i++)
{
if(i % 8 == 0 && i > 0) printf(" ");
printf("%02X", data[i]);
}
printf("\n");
}