Skip to content

Commit bc1b560

Browse files
nffqdoegox
authored andcommitted
Allow debug prints when isNested == AUTH_NESTED and ntencptr == NULL
1 parent 83779ea commit bc1b560

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

armsrc/mifareutil.c

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -212,35 +212,24 @@ int mifare_classic_authex_cmd(struct Crypto1State *pcs, uint32_t uid, uint8_t bl
212212
}
213213

214214
// Save the tag nonce (nt)
215-
uint32_t nt = bytes_to_num(receivedAnswer, 4);
216-
if (ntencptr) {
217-
*ntencptr = nt;
218-
}
219-
220-
if (ntencparptr) {
221-
*ntencparptr = receivedAnswerPar[0];
222-
}
223-
224-
// ----------------------------- crypto1 create
225-
if (isNested) {
226-
crypto1_deinit(pcs);
227-
}
215+
uint32_t nt = bytes_to_num(receivedAnswer, 4),
216+
ntenc = nt;
228217

229218
// Init cipher with key
230219
crypto1_init(pcs, ui64Key);
231220

232221
if (isNested == AUTH_NESTED) {
233-
// decrypt nt with help of new key
234-
nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;
222+
// decrypt ntenc with help of new key
223+
nt = crypto1_word(pcs, ntenc ^ uid, 1) ^ ntenc;
235224
} else {
236225
// Load (plain) uid^nt into the cipher
237226
crypto1_word(pcs, nt ^ uid, 0);
238227
}
239228

240229
// some statistic
241-
// if (!ntptr && (g_dbglevel >= DBG_EXTENDED))
242-
uint32_t nr32 = nr[0] << 24 | nr[1] << 16 | nr[2] << 8 | nr[3];
243230
if (g_dbglevel >= DBG_EXTENDED) {
231+
uint32_t nr32 = nr[0] << 24 | nr[1] << 16 | nr[2] << 8 | nr[3];
232+
244233
if (isNested == AUTH_FIRST) {
245234
Dbprintf("auth cmd: %02x %02x | uid: %08x | nr: %08x %s| nt: %08x %s %5i| par: %i%i%i%i %s",
246235
cmd, blockNo, uid,
@@ -253,9 +242,7 @@ int mifare_classic_authex_cmd(struct Crypto1State *pcs, uint32_t uid, uint8_t bl
253242
(receivedAnswerPar[0] >> 4) & 1,
254243
validate_parity_nonce(nt, receivedAnswerPar[0], nt) ? "ok " : "bad");
255244
} else {
256-
257-
if (ntencptr) {
258-
Dbprintf("auth nested cmd: %02x %02x | uid: %08x | nr: %08x %s| nt: %08x %s %5i| par: %i%i%i%i %s| ntenc: %08x %s| parerr: %i%i%i%i",
245+
Dbprintf("auth nested cmd: %02x %02x | uid: %08x | nr: %08x %s| nt: %08x %s %5i| par: %i%i%i%i %s| ntenc: %08x %s| parerr: %i%i%i%i",
259246
cmd, blockNo, uid,
260247
nr32, validate_prng_nonce(nr32) ? "@" : " ",
261248
nt, validate_prng_nonce(nt) ? "@idx" : " idx",
@@ -264,20 +251,27 @@ int mifare_classic_authex_cmd(struct Crypto1State *pcs, uint32_t uid, uint8_t bl
264251
(receivedAnswerPar[0] >> 6) & 1,
265252
(receivedAnswerPar[0] >> 5) & 1,
266253
(receivedAnswerPar[0] >> 4) & 1,
267-
validate_parity_nonce(*ntencptr, receivedAnswerPar[0], nt) ? "ok " : "bad",
268-
*ntencptr, validate_prng_nonce(*ntencptr) ? "@" : " ",
269-
((receivedAnswerPar[0] >> 7) & 1) ^ oddparity8((*ntencptr >> 24) & 0xFF),
270-
((receivedAnswerPar[0] >> 6) & 1) ^ oddparity8((*ntencptr >> 16) & 0xFF),
271-
((receivedAnswerPar[0] >> 5) & 1) ^ oddparity8((*ntencptr >> 8) & 0xFF),
272-
((receivedAnswerPar[0] >> 4) & 1) ^ oddparity8((*ntencptr >> 0) & 0xFF)
273-
);
274-
}
254+
validate_parity_nonce(ntenc, receivedAnswerPar[0], nt) ? "ok " : "bad",
255+
ntenc, validate_prng_nonce(ntenc) ? "@" : " ",
256+
((receivedAnswerPar[0] >> 7) & 1) ^ oddparity8((ntenc >> 24) & 0xFF),
257+
((receivedAnswerPar[0] >> 6) & 1) ^ oddparity8((ntenc >> 16) & 0xFF),
258+
((receivedAnswerPar[0] >> 5) & 1) ^ oddparity8((ntenc >> 8) & 0xFF),
259+
((receivedAnswerPar[0] >> 4) & 1) ^ oddparity8((ntenc >> 0) & 0xFF));
275260
}
276261
}
262+
277263
// save Nt
278264
if (ntptr) {
279265
*ntptr = nt;
280266
}
267+
// save encrypted Nt
268+
if (ntencptr) {
269+
*ntencptr = ntenc;
270+
}
271+
// save encrypted Nt parity
272+
if (ntencparptr) {
273+
*ntencparptr = receivedAnswerPar[0];
274+
}
281275

282276
// Generate (encrypted) nr+parity by loading it into the cipher (Nr)
283277
uint32_t pos;

0 commit comments

Comments
 (0)