Reference key not useable when authenticating to MIFARE Classic card - authentication

I'm trying to authenticate to a MIFARE Classic card using PC/SC based APDU. I'm using a SCL3711 reader and the key A 00 00 00 00 00 03.
>> FF 82 00 60 06 00 00 00 00 00 03
<< 90 00
>> FF 86 00 00 05 01 00 08 60 00
<< 69 84
(Check error : referenced data invalidated)
I'm sure the data stored in the corresponding sector trailer is ok, as using a native reader I'm able to read/write the value. Actually this is the way I was able to restore the key once this happened.
When using other keys like FF FF FF FF FF FF, or 01 02 03 04 05 06, or whatever "normal", everything is working as expected.
a) With valid key FF FF FF FF FF FF
>> FF 82 00 60 06 FF FF FF FF FF FF
<< 90 00
>> FF 86 00 00 05 01 00 08 60 00
<< 90 00
b) With invalid key 01 02 03 04 05 06
>> FF 82 00 60 06 01 02 03 04 05 06
<< 90 00
>> FF 86 00 00 05 01 00 08 60 00
<< 69 82
I have gone through the PC/SC specs (Part 3) and haven't found any place where they include any reference to valid or invalid keys. It is the first time I'm not able to use a key.
Any idea why this is happening?

Related

Accessing an external SFP module via I2C not working

I hope this is the correct location for a question like I am asking here (I am new to asking questions on stackoverflow)
I am working on a project with a processor to access the registers of an SFP (connected to the I2C-bus of the processor).
Obviously it is not working (at least not always), but to clarify I want to mention what is working ... The setup fo the processor and the I2C bus is working properly. Other devices on the bus work flawlessly.
At the moment I am using a Raspberry Pi and its I2C capabilities to troubleshoote my project.
The hardware to connect the SFP-interface to the I2C pins of the Raspberry Pi I built myself (and there may be some error in it), but as I am trying different SFPs I see some are working properly.
These are working:
GLC-T, GLC-SX-MM
The following did not work (with an i2detect command from the RPi it does not see any responding device):
GLC-SX-MMD, SFP-10G-SR, PAN-SFP-PLUS-SR-CMT (The last one is from PaloAlto, all others are from Cisco)
I tried to follow the specifications from INF-8074 (https://www.10gtek.com/templates/wzten/pdf/INF-8074.pdf) to make the proper connections and for some SFPs it seems to be working. Even though I cannot verify all the registers' accuracy, there are parts (like the description of the SFP) which show up clearly ... below is the output of the GCL-T:
mordin#ubuntu:~$ sudo i2cdump -y 1 0x50
No size specified (using byte-data access)
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 03 04 00 00 00 00 08 00 00 00 00 01 0d 00 00 00 ??....?....??...
10: 00 00 64 00 43 49 53 43 4f 2d 4d 45 54 48 4f 44 ..d.CISCO-METHOD
20: 45 20 20 20 01 00 00 00 53 50 37 30 34 31 5f 52 E ?...SP7041_R
30: 65 76 5f 46 20 20 20 20 46 20 20 20 41 0c c1 5a ev_F F A??Z
40: 00 10 00 00 30 30 30 30 30 4d 54 43 31 35 33 39 .?..00000MTC1539
50: 30 31 45 51 31 31 30 39 32 31 30 31 00 00 00 3c 01EQ11092101...<
60: 00 00 0e fa 99 1c 0a 21 57 89 b6 80 c0 8a 20 a0 ..?????!W????? ?
70: f7 c2 a9 00 00 00 00 00 00 00 00 00 46 a1 08 57 ???.........F??W
80: 43 4e 53 38 54 55 54 41 41 42 33 30 2d 31 34 31 CNS8TUTAAB30-141
90: 30 2d 30 33 00 00 00 00 00 00 00 00 00 00 00 c3 0-03...........?
a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
c0: 47 4c 43 2d 54 20 20 20 20 20 20 20 20 20 20 20 GLC-T
d0: 20 20 20 20 ff ff ff ff ff ff ff ff ff ff ff ff ............
e0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
f0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................
There are multiple projects online ... and they all describe their workflow, and it sounds like as long as you make proper physical connections (power, I2C-pins) it should "just work" to have access to the registers of the connected SFP.
Now I am not sure ... did I miss something when I made my physical connections and some SFPs are just working accidently?
Just for "fun" I tried to pin the "TX Disable" to GND (I was thinking maybe the line disables the whole SFP-module and not only the TX-part), but it did not make any difference.
If someone has a suggestion (or a hint what I could try) why I am not able to access many SFPs via I2C while some work just fine, I would be grateful.
Thanks,
Mordin
PS.: apparently I am not allowed to use "sfp" as a tag ... weird

Why do I get 2 interface descriptors before the endpoint descriptor after a GET_DESCRIPTOR USB request on QEMU?

I am writing a small x86-64 hobby OS I boot with UEFI. I am currently writing a driver for the Intel's xHC. I am at a point where I can address USB devices and have a Transfer Ring allocated for Endpoint 0 of each device. I then use a GET_DESCRIPTOR request to get the configuration descriptor of each device. I ask QEMU to emulate a USB keyboard and a USB mouse. I thus get 2 different descriptors which are the following:
user#user-System-Product-Name:~$ hexdump -C result.bin
00000000 09 02 22 00 01 01 06 a0 32 09 04 00 00 01 03 01 |..".....2.......|
00000010 02 00 09 21 01 00 00 01 22 34 00 07 05 81 03 04 |...!...."4......|
00000020 00 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000 09 02 22 00 01 01 08 a0 32 09 04 00 00 01 03 01 |..".....2.......|
00001010 01 00 09 21 11 01 00 01 22 3f 00 07 05 81 03 08 |...!...."?......|
00001020 00 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00001030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00002000
Basically, I ask GDB to output the content of RAM where I placed the descriptors in the file result.bin. Then I hexdump the content of result.bin in the console. Here you can see the configuration of the USB mouse first. Then, 1 page later, the configuration of the USB keyboard.
The configuration descriptor of the mouse is 09 02 22 00 01 01 06 a0 32. It is followed with 2 interface descriptors: 09 04 00 00 01 03 01 02 00 and 09 21 01 00 00 01 22 34 00. Those are followed by one endpoint descriptor: 07 05 81 03 08 00 07.
In the first interface descriptor for both the mouse and the keyboard, it is indicated that there is one endpoint descriptor (indicated by the bNumEndpoints field of the descriptor which is byte number 4 indexed from 0). I would expect that the following descriptor is the endpoint descriptor. Instead, I get a second interface descriptor (indicated by the fact that it has a length of 9 bytes instead of 7 and by the values of the different fields).
As stated on https://wiki.osdev.org/Universal_Serial_Bus:
Each CONFIGURATION descriptor has at least one INTERFACE descriptor, and each INTERFACE descriptor may have up to 15 ENDPOINT descriptors. When the host requests a certain CONFIGURATION descriptor, the device returns the CONFIGURATION descriptor followed immediately by the first INTERFACE descriptor, followed immediately by all of the ENDPOINT descriptors for endpoints that the interface defines (which may be none). This is followed immediately by the next INTERFACE descriptor if one exists, and then by its ENDPOINT descriptors if applicable. This pattern continues until all the information within the scope of the specific configuration is transfered.
Why do I get 2 interface descriptors followed by the endpoint descriptor in my case? Is it a QEMU bug or is it something I should expect?
You're not accurately describing the binary data that I see in your shell output.
The dump starts with 9-byte descriptor of type 2 so that is your device descriptor:
09 02 22 00 01 01 06 a0 32
Then there is a 9-byte descriptor of type 4, so that is an interface, and it has bNumEndpoints set to 1:
09 04 00 00 01 03 01 02 00
Then there is another 9-byte descriptor of type 0x21. I don't recognize that code off the top of my head, but it probably is something standard:
09 21 01 00 00 01 22 34 00
Then we have a 7-byte descriptor of type 5, so that is an endpoint descriptor:
07 05 81 03 04 00 07

MIFARE desfire EV1 communication using winscard

I need some help communicating with Desfire EV1 card. The library (winscard.dll) seems to be automatically handling all responses from the card that have an ADDITIONAL_FRAME (AF) byte so that the application receives already processed data. For example, I send the GetVersion command as follows:
[out] 90 60 00 00 00, and the response is:
[ in ] 04 01 01 01 00 18 05 04 01 01 01 04 18 05 04 83 71 2A 9F 43 80 BA 64 17 8F A0 07 15 91 00
Note: I wrapped the command data in ISO 7816 headers.
I expected the communication to be something like this:
[out] 90 60 00 00 00
[ in ] 04 01 01 01 00 18 05 91 AF
[out] 90 AF 00 00 00
[ in ] 04 01 01 01 04 18 05 91 AF
[out] 90 AF 00 00 00
[ in ] 04 83 71 2A 9F 43 80 BA 64 17 8F A0 07 15 91 00.
The same thing happens during card authentication so when I send
[out] 90 0A 00 00 01 00 00, I get back
[ in ] 91 00
instead of a card challenge.
Is there a way to disable this behaviour?
Thanks.

How can I check INITIALIZE UPDATE and EXTERNAL AUTHENTICATE correctness?

I sent 80 50 00 00 08 00 00 00 00 00 00 00 00 [INITILIZE UPDATE Command] via opensc-tool to my java card and received 00 00 11 60 01 00 8A 79 0A F9 FF 02 00 11 79 11 36 5D 71 00 A5 A5 EC 63 BB DC 05 CC [Init Response] as its response from the card.
As you see:
In the command,I send 00 00 00 00 00 00 00 00 as Host Challenge, And in the response :
00 00 11 60 01 00 8A 79 0A F9 = Key diversification data
FF 02 = Key information
00 11 79 11 36 5D 71 00 = Card challenge
A5 A5 EC 63 BB DC 05 CC = Card cryptogram
Now I want to check myself,if the card cryptogram is OK or not. How I can do it? for example I encrypt 00 00 00 00 00 00 00 00 in this site under a 3DES cryptography algorithm [with keys of my card = 4041...4F], but the output is not equal with card cryptogram that I wrote above. Why?
And the next question is, if I want to send EXTERNAL AUTHENTICATION command to the card, what is its data field (after the above INITILIZE UPDATE)?
Update:
This is GPJ output :
C:\Users\ghasemi\Desktop\gpj-20120310>GPJ
C:\Users\ghasemi\Desktop\gpj-20120310>java -jar gpj.jar
Found terminals: [PC/SC terminal ACS CCID USB Reader 0]
Found card in terminal: ACS CCID USB Reader 0
ATR: 3B 68 00 00 00 73 C8 40 12 00 90 00
.
.
.
DEBUG: Command APDU: 00 A4 04 00 08 A0 00 00 00 03 00 00 00
DEBUG: Response APDU: 6F 10 84 08 A0 00 00 00 03 00 00 00 A5 04 9F 65 01 FF 90 00
Successfully selected Security Domain OP201a A0 00 00 00 03 00 00 00
DEBUG: Command APDU: 80 50 00 00 08 7F 41 A9 E7 19 37 83 FA
DEBUG: Response APDU: 00 00 11 60 01 00 8A 79 0A F9 FF 02 00 1B 9B 95 B9 5E 5E BC BA 51 34 84 D9 C1 B9 6E 90 00
DEBUG: Command APDU: 84 82 00 00 10 13 3B 4E C5 2C 9E D8 24 50 71 83 3A 78 AE 75 23
DEBUG: Response APDU: 90 00
DEBUG: Command APDU: 84 82 00 00 08 13 3B 4E C5 2C 9E D8 24
DEBUG: Response APDU: 90 00
C:\Users\ghasemi\Desktop\gpj-20120310>
So :
Host_Challenge :: 7F41A9E7193783FA
Diversification_Data :: 0000116001008A790AF9
Key_Information :: FF02
Sequence_Counter :: 001B
Card_Challenge :: 9B95B95E5EBC
Card_Cryptogram :: BA513484D9C1B96E
Host_Cryptogram[16,24] = 13 3B 4E C5 2C 9E D8 24
Now,lets make our Host_Cryptogram Manually :
Derivation_data=derivation_const_ENC|sequence_counter|0000 0000 0000 0000 0000 0000
Derivation_Data = 0182001B000000000000000000000000
k_ENC :: 404142434445464748494A4B4C4D4E4F
IV = 00 00 00 00 00 00 00 00
S_ENC = encrypt(TDES_CBC, K_ENC, IV, derivation_data)
So :
I used http://tripledes.online-domain-tools.com/ and its output for above values was :
S_ENC = 448b0a5967ca246d058703ff0c694f15
And :
Padding_DES = 80 00 00 00 00 00 00 00
Host_auth_data = sequence_counter | card_challenge | host_challenge | padding_DES
IV = Card_Cryptogram :: BA513484D9C1B96E
host_cryptogram = encrypt(TDES_CBC, S_ENC, IV, host_auth_data)
So :
Host_Authentication_Data : 001B9B95B95E5EBC7F41A9E7193783FA8000000000000000
Again, I used http://tripledes.online-domain-tools.com/
and :
Host_Cryptogram : 3587b531db71ac52392493c08cff189ce7b9061029c63b62
So :
Host_Cryptogram[16,24] = e7b9061029c63b62
Why these two way [manually and GPJ output] give us two host cryptogram?
From the INITIALIZE UPDATE command you send, you get
host_challenge = 00 00 00 00 00 00 00 00
In response to the INITIALIZE UPDATE command, you get
diversification_data = 00 00 11 60 01 00 8A 79 0A F9
key_information = FF 02
sequence_counter = 00 11
card_challenge = 79 11 36 5D 71 00
card_cryptogram = A5 A5 EC 63 BB DC 05 CC
The key information indicates SCP02 (02). The key diversification data may be used to derive the card-specific K_ENC. Lets assume we have a K_ENC like this
K_ENC = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
We can then derive the session encryption key like this
derivation_const_ENC = 01 82
derivation_data = derivation_const_ENC | sequence_counter | 00 00 00 00 00 00 00 00 00 00 00 00
IV = 00 00 00 00 00 00 00 00
S_ENC = encrypt(TDES_CBC, K_ENC, IV, derivation_data)
Next, we can assemble the authentication data used to calculate the host cryptogram:
padding_DES = 80 00 00 00 00 00 00 00
host_auth_data = sequence_counter | card_challenge | host_challenge | padding_DES
Then we can use the session encryption key to encrypt the authentication data:
IV = 00 00 00 00 00 00 00 00
host_cryptogram = encrypt(TDES_CBC, S_ENC, IV, host_auth_data)
The last 8 bytes of the encrypted authentication data are the actual host cryptogram that we would send to the card:
EXTERNAL_AUTHENTICATE_data = host_cryptogram[16, 24]
Now we can assemble the EXTERNAL AUTHENTICATE command:
EXTERNAL_AUTHENTICATE = 84 82 03 00 08 | EXTERNAL_AUTHENTICATE_data
We can then calculate the S_MAC key (analoguous to getting the S_ENC above) and the MAC over that command and append it to the command data to get the full EXTERNAL AUTHENTICATE command that can be sent to the card:
EXTERNAL_AUTHENTICATE = 84 82 03 00 10 | EXTERNAL_AUTHENTICATE_data | MAC
Update
Using http://tripledes.online-domain-tools.com/ to reproduce the results of GPJ
Your K_ENC is 404142434445464748494A4B4C4D4E4F. The online tools does not properly support 2-key-3DES, so you have to convert the key into its 3-key form first:
K_ENC = 404142434445464748494A4B4C4D4E4F4041424344454647
Use this key and a zero IV to encrypt the derivation data (0182001B000000000000000000000000). You get
S_ENC = fb063cc2e17b979b10e22f82110234b4
In 3-key notation, this is
S_ENC = fb063cc2e17b979b10e22f82110234b4fb063cc2e17b979b
Use this key and a zero IV to encrypt the host authentication data (001b9b95b95e5ebc7f41a9e7193783fa8000000000000000):
HOST_CRYPTOGRAM = 773e790c91acce3167d99f92c60e2afd133b4ec52c9ed824

understand hexedit of an elf

Consider the following hexedit display of an ELF file.
00000000 7F 45 4C 46 01 01 01 00 00 00 00 00 .ELF........
0000000C 00 00 00 00 02 00 03 00 01 00 00 00 ............
00000018 30 83 04 08 34 00 00 00 50 14 00 00 0...4...P...
00000024 00 00 00 00 34 00 20 00 08 00 28 00 ....4. ...(.
00000030 24 00 21 00 06 00 00 00 34 00 00 00 $.!.....4...
0000003C 34 80 04 08 34 80 04 08 00 01 00 00 4...4.......
00000048 00 01 00 00 05 00 00 00 04 00 00 00 ............
How many section headers does it have?
Is it an object file or an executable file?
How many program headers does it have?
If there are any program headers, what does the first program header do?
If there are any section headers, at what offset is the section header table?
Strange, this hexdump looks like your homework to me...
There are 36 section headers.
It is an executable.
It has 8 program headers.
As you can tell by the first word (offset 0x34: 0x0006) in the first program header, it is of type PT_PHDR, which just informs about the characteristics of the program header table itself.
The section header table begins at byte 5200 (which is 0x1450 in hex).
How do I know this stuff? By dumping the hex into a binary and reading it with readelf -a (because I am lazy). Except for question no. 4, which I had to figure out manually by reading man 5 elf.