Hi All,
I am trying to write a Mifare Ultralight C encryption sketch. Basically everything is working, but while running the sketch the arrays seem to get corrupted and the sketch might even crash and restart.
I use the RC522 board and an Uno to read the Mifare Ultralight C Tags. The RC522 board is modified (L1 and L2 replaced) in order to detect an read the Ultralight C.
With commands over the serial monitor I am able to dump the data or to authenticate via an 3DES encrypted key.
the command for encryption used in the serial monitor is:
auth 49454D4B41455242214E4143554F5946
The perfect output looks like this (running the code on a Mega2560):
Firmware Version: 0x92 = v2.0
Read/Write Mifare Ultrlight C
Mifare Ultralight compatible PICC found
command: auth
data: 49454D4B41455242214E4143554F5946
IV: 00 00 00 00 00 00 00 00
enc(RndB): 92 37 31 3C 6D 3E 9D C2
RndB: A1 B5 EC 99 5E 37 2C B7
RndA: D4 6A AD E2 D8 5A 48 A0
rRndB: B5 EC 99 5E 37 2C B7 A1
RndA+RndB’: D4 6A AD E2 D8 5A 48 A0 B5 EC 99 5E 37 2C B7 A1
new IV: 92 37 31 3C 6D 3E 9D C2
0xAF + enk(RndA+RndB’): AF 33 DD F7 8F 32 2F 43 C9 0D C1 68 27 79 F3 DA CF
dRndA': 6A AD E2 D8 5A 48 A0 D4
RndA': 6A AD E2 D8 5A 48 A0 D4
Keys are correct :-)
When trying to run this code on an Uno / Nano the result is this:
Firmware Version: 0x92 = v2.0
Read/Write Mifare Ultrlight C
Mifare Ultralight compatible PICC found
command: auth
data: 49454D4B41455242214E4143554F5946
IV: 00 00 00 00 00 00 00 00
enc(RndB): 31Firmware Version: 0x92 = v2.0
Read/Write Mifare Ultrlight C
Mifare Ultralight compatible PICC found
when repeating the command I get somthing like
command: auth
data: 49454D4B41455242214E414355⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮auth
IV: 00 00 00 00 00 00 00 00
enc(RndB): 4Firmware Version: 0x92 = v2.0
Read/Write Mifare Ultrlight C
Mifare Ultralight compatible PICC found
When printing the content of enc(RndB) the code crashes and restarts.
The intersting thing is that the code is running differently on a Mega2560 and Uno. Furthermore the output for the authentication depents on what routine I define to read the data from the tag.
To read the data from the tag, I tried to use the given MFRC522 command
mfrc522.PICC_DumpMifareUltralightToSerial();
When using this option, the authentication works.
What puzzles me is that after encrypted authentication this command seems to be corrupted as well, as the counter, limiting this function to only 16 pages in the MFRC522 library does not work anymore and the pages just keep counting up to thousends.
So I wrote my own dump-routine whitch totally messes up the authentification part of the program.
if(!strcmp(Scmd,"dump")){ // dump data
DumpDataUltralight(); // this messes up the Authentification
//mfrc522.PICC_DumpMifareUltralightToSerial(); // this messes the 16 page counter of the routine
}
I tried my best to define and initialize the arrays required for encryption.
There are no warnings (setting to: "All")
Memory usage is OK
14926 Bytes (48%) of 30720 Bytes.
globale variables use 995 Bytes (48%) of 2048 Bytes
Full sketch is attached.
Where do I mess up the arrays/the code?
Arduino 1.8.13 on Windows
MFRC522 version 1.4.7
DES.h from Github
Many thanks in advance.
Ultralight_C_03.ino (14.9 KB)