kormax / apple-home-key-reader

Apple Home Key Reader Implementation
Apache License 2.0
557 stars 45 forks source link

Error homekey iPhone #2

Closed juaurodri closed 10 months ago

juaurodri commented 10 months ago

Hi @kormax,

I installed this project on a raspberry pi 3b+, via uart.

it’s working fine when I use my Apple Watch, but my iPhone and my gf iPhone doesn’t work.

I tried flow standard and fast, same problem for both.

Logs: ( 1st success with watch, 2nd iPhone)

[2023-11-30 17:17:06,025] [ INFO] init :148 searching for reader on path tty:ttyAMA0:pn532 [2023-11-30 17:17:06,144] [ INFO] init :151 using PN532v1.6 at /dev/ttyAMA0 [2023-11-30 17:17:06,157] [ INFO] accessory_driver :483 Loading Accessory state from hap.state [2023-11-30 17:17:06,161] [ INFO] accessory_driver :320 Starting the event loop [2023-11-30 17:17:06,165] [ INFO] accessory_driver :372 Starting accessory NFC Lock on addresses ['192.168.5.74'], port 51926. [2023-11-30 17:17:06,464] [ INFO] hap_protocol :82 ('192.168.4.27', 50714): Connection made to NFC Lock [2023-11-30 17:17:06,507] [ INFO] accessory :138 get_hardware_finish [2023-11-30 17:17:06,508] [ INFO] service :254 get_hardware_finish=AQTj4+MA [2023-11-30 17:17:06,509] [ INFO] accessory :115 get_lock_current_state [2023-11-30 17:17:06,510] [ INFO] accessory :119 get_lock_target_state [2023-11-30 17:17:06,511] [ INFO] accessory :129 get_lock_version [2023-11-30 17:17:06,511] [ INFO] accessory :158 get_configuration_state [2023-11-30 17:17:06,511] [ INFO] service :309 get_configuration_state [2023-11-30 17:17:06,512] [ INFO] accessory :148 get_nfc_access_control_point [2023-11-30 17:17:06,512] [ INFO] service :267 get_nfc_access_control_point [2023-11-30 17:17:06,512] [ INFO] accessory :143 get_nfc_access_supported_configuration [2023-11-30 17:17:06,513] [ INFO] service :263 TODO get_nfc_access_supported_configuration=AQEQAgEQ [2023-11-30 17:17:18,599] [ INFO] service :110 Got NFC tag Type4ATag MIU=253 FWT=0.038664 [2023-11-30 17:17:18,606] [ INFO] homekey :529 SELECT CMD = ISO7816Command(cla=0x00; ins=0xa4; p1=0x04; p2=0x00; lc=0x07(7); data=a0000008580101; le=0x00) [2023-11-30 17:17:18,626] [ INFO] homekey :535 SELECT RES = ISO7816Response(sw1=0x90; sw2=0x00; data=5c0402000100(6)) [2023-11-30 17:17:18,628] [ INFO] homekey :708 reader_identifier.hex()='700ec626a71584e3ca53a7dfb6eb559c' [2023-11-30 17:17:18,629] [ INFO] homekey :721 Choosing preferred version b'\x02\x00' [2023-11-30 17:17:18,635] [ INFO] homekey :569 Reader public key: x=df2f764981a8adc2d9b24acf08207938e18e0fd2dba28cdc3ffab4ec6a208242 y=02fa3a1f8e524be9d2aafd3d9bb372a264abcc0b86459b5c345da72ea140dfc0 [2023-11-30 17:17:18,637] [ INFO] homekey :575 protocol_version.hex()='0200' [2023-11-30 17:17:18,642] [ INFO] homekey :172 AUTH0 CMD = ISO7816Command(cla=0x80; ins=0x80; p1=0x00; p2=0x01; lc=0x6b(107); data=5c0202008741046b263a435845dbd50c49b3c98713c6ddcd21316eb4506958b38caac0f9148bd05b5d58d5e5760972263684a2e7d1ba341a1e3d2b33f44265c5e3b61bab439b744c103f059b89fe06b32cba2567251a8fe0464d10700ec626a71584e3ca53a7dfb6eb559c) [2023-11-30 17:17:18,718] [ INFO] homekey :176 AUTH0 RES = ISO7816Response(sw1=0x90; sw2=0x00; data=864104d26772e1bb627b55fc5fb2e412222159431f6b3316f9a2ee6c47cb0a04de5f0f3d59c74894e90b24b88d567642287fae9241337de322091a9211dba6545f9385(67)) [2023-11-30 17:17:18,722] [ INFO] homekey :275 endpoint_ephemeral_public_key_x.hex()='d26772e1bb627b55fc5fb2e412222159431f6b3316f9a2ee6c47cb0a04de5f0f' reader_ephemeral_public_key_x.hex()='6b263a435845dbd50c49b3c98713c6ddcd21316eb4506958b38caac0f9148bd0' [2023-11-30 17:17:18,724] [ INFO] homekey :287 authentication_hash_input.hex()='4d10700ec626a71584e3ca53a7dfb6eb559c8620d26772e1bb627b55fc5fb2e412222159431f6b3316f9a2ee6c47cb0a04de5f0f87206b263a435845dbd50c49b3c98713c6ddcd21316eb4506958b38caac0f9148bd04c103f059b89fe06b32cba2567251a8fe0469304415d9569' [2023-11-30 17:17:18,727] [ INFO] homekey :292 signature.hex()='3045022006d5a67eb1aa019548c40a1bc333a154a3307fb516cdd5330f5c25d351370a25022100b0ab8df4fd3b06ed012a88deac84ba7c2044175a32cee7e7fa6e13213e3d2723' (0x47) [2023-11-30 17:17:18,728] [ INFO] homekey :295 signature_point_form.hex()='06d5a67eb1aa019548c40a1bc333a154a3307fb516cdd5330f5c25d351370a25b0ab8df4fd3b06ed012a88deac84ba7c2044175a32cee7e7fa6e13213e3d2723' (0x40) [2023-11-30 17:17:18,730] [ INFO] homekey :300 AUTH1 COMMAND ISO7816Command(cla=0x80; ins=0x81; p1=0x00; p2=0x00; lc=0x42(66); data=9e4006d5a67eb1aa019548c40a1bc333a154a3307fb516cdd5330f5c25d351370a25b0ab8df4fd3b06ed012a88deac84ba7c2044175a32cee7e7fa6e13213e3d2723) [2023-11-30 17:17:18,837] [ INFO] homekey :302 AUTH1 RESPONSE: ISO7816Response(sw1=0x90; sw2=0x00; data=d08136d37d5e087bf45e3af6317606fdfcd2842e504bb9699a2f679cad95133f3825437086393490f49ce75a623810a256ed88fa222f7b3847b5969b38de9b43e72f2d3309344a53a66b81eb136da812aaedbb96dd97732a(88)) [2023-11-30 17:17:18,842] [ INFO] homekey :102 shared_key.hex()='19328297d181b3d4e666ef41506a16397da0d062f11c087c21139b161855af38' [2023-11-30 17:17:18,844] [ INFO] homekey :109 derived_key.hex()='04a672a819a5215c6cae0a9dd901190bc756aa72e5f287498e4cdcb44c8a531c' [2023-11-30 17:17:18,846] [ INFO] homekey :124 info.hex()='6b263a435845dbd50c49b3c98713c6ddcd21316eb4506958b38caac0f9148bd0d26772e1bb627b55fc5fb2e412222159431f6b3316f9a2ee6c47cb0a04de5f0f3f059b89fe06b32cba2567251a8fe0465e000150657273697374656e745c0202005c0402000100' [2023-11-30 17:17:18,848] [ INFO] homekey :317 k_persistent.hex()='dbf920c1bfa87fb1999d9c0dbc9a38d6af45a89443d172cd5829388b70c21476' [2023-11-30 17:17:18,850] [ INFO] homekey :124 info.hex()='6b263a435845dbd50c49b3c98713c6ddcd21316eb4506958b38caac0f9148bd0d26772e1bb627b55fc5fb2e412222159431f6b3316f9a2ee6c47cb0a04de5f0f3f059b89fe06b32cba2567251a8fe0465e0001566f6c6174696c655c0202005c0402000100' [2023-11-30 17:17:18,853] [ INFO] homekey :320 hkdf.hex()='bf2592fcefbbf7fc368da0bc10d6c67601a14dd31e1eb40f8148088b08264e4290661305f487d92b066b5151b6f86350' [2023-11-30 17:17:18,853] [ INFO] homekey :324 kenc.hex()='bf2592fcefbbf7fc368da0bc10d6c676' kmac.hex()='01a14dd31e1eb40f8148088b08264e42' krmac.hex()='90661305f487d92b066b5151b6f86350' [2023-11-30 17:17:18,857] [ INFO] homekey :334 AUTH1 DECRYPTED RESPONSE: ISO7816Response(sw1=0x90; sw2=0x00; data=4e066c95af82b0739e402ce07313e81257c19819fca821ac34b7c7340fb719afc3e1b3a6ae0142ee591dd739be9975cc1cc216a63f844845a3f251f82a81ff592671ba9575ba35812266(74)) [2023-11-30 17:17:18,859] [ INFO] homekey :346 device_identifier.hex()='6c95af82b073' [2023-11-30 17:17:18,861] [ INFO] homekey :357 signature.hex()='2ce07313e81257c19819fca821ac34b7c7340fb719afc3e1b3a6ae0142ee591dd739be9975cc1cc216a63f844845a3f251f82a81ff592671ba9575ba35812266' [2023-11-30 17:17:18,862] [ INFO] homekey :370 verification_hash_input.hex()='4d10700ec626a71584e3ca53a7dfb6eb559c8620d26772e1bb627b55fc5fb2e412222159431f6b3316f9a2ee6c47cb0a04de5f0f87206b263a435845dbd50c49b3c98713c6ddcd21316eb4506958b38caac0f9148bd04c103f059b89fe06b32cba2567251a8fe04693044e887b4c' [2023-11-30 17:17:18,865] [ INFO] homekey :541 OP_CONTROL_FLOW CMD = ISO7816Command(cla=0x80; ins=0x3c; p1=0x01; p2=0x00) [2023-11-30 17:17:18,889] [ INFO] homekey :543 OP_CONTROL_FLOW RES = ISO7816Response(sw1=0x90; sw2=0x00) [2023-11-30 17:17:18,898] [ INFO] service :127 Authenticated endpoint via <DigitalKeyFlow.STANDARD: 1>: Endpoint(last_used_at=1701364638, counter=3, key_type=SECP256R1(0x02), public_key=044ba24aac55330184b95e237de272ef2890c9d2bc5ab1a8720320ec607250f724ba2190b0f9a7fc7f42aea69e13769aec2bdfe6e40a32790ec4e5f7923493c453; persistent_key=dbf920c1bfa87fb1999d9c0dbc9a38d6af45a89443d172cd5829388b70c21476, enrollments=Enrollments(hap, )) [2023-11-30 17:17:18,899] [ INFO] service :130 Transaction took 343.12944600014816 ms [2023-11-30 17:17:18,900] [ INFO] accessory :29 Toggling lock state due to endpoint authentication event 1 -> 0 Endpoint(last_used_at=1701364638, counter=3, key_type=SECP256R1(0x02), public_key=044ba24aac55330184b95e237de272ef2890c9d2bc5ab1a8720320ec607250f724ba2190b0f9a7fc7f42aea69e13769aec2bdfe6e40a32790ec4e5f7923493c453; persistent_key=dbf920c1bfa87fb1999d9c0dbc9a38d6af45a89443d172cd5829388b70c21476, enrollments=Enrollments(hap, )) [2023-11-30 17:17:18,911] [ INFO] service :137 Waiting for device to leave the field... [2023-11-30 17:17:19,474] [ INFO] service :139 Device left the field. Continuing in 2 seconds... [2023-11-30 17:17:21,475] [ INFO] service :147 Waiting for next device... [2023-11-30 17:17:28,700] [ INFO] accessory :119 get_lock_target_state [2023-11-30 17:17:28,700] [ INFO] accessory :115 get_lock_current_state [2023-11-30 17:17:31,879] [ INFO] accessory :115 get_lock_current_state [2023-11-30 17:17:38,773] [ INFO] service :110 Got NFC tag Type4ATag MIU=253 FWT=0.038664 [2023-11-30 17:17:38,776] [ INFO] homekey :529 SELECT CMD = ISO7816Command(cla=0x00; ins=0xa4; p1=0x04; p2=0x00; lc=0x07(7); data=a0000008580101; le=0x00) [2023-11-30 17:17:38,798] [ INFO] homekey :535 SELECT RES = ISO7816Response(sw1=0x90; sw2=0x00; data=5c0402000100(6)) [2023-11-30 17:17:38,799] [ INFO] homekey :708 reader_identifier.hex()='700ec626a71584e3ca53a7dfb6eb559c' [2023-11-30 17:17:38,799] [ INFO] homekey :721 Choosing preferred version b'\x02\x00' [2023-11-30 17:17:38,803] [ INFO] homekey :569 Reader public key: x=df2f764981a8adc2d9b24acf08207938e18e0fd2dba28cdc3ffab4ec6a208242 y=02fa3a1f8e524be9d2aafd3d9bb372a264abcc0b86459b5c345da72ea140dfc0 [2023-11-30 17:17:38,805] [ INFO] homekey :575 protocol_version.hex()='0200' [2023-11-30 17:17:38,807] [ INFO] homekey :172 AUTH0 CMD = ISO7816Command(cla=0x80; ins=0x80; p1=0x00; p2=0x01; lc=0x6b(107); data=5c020200874104c20cc6b5d72d020d5b2d3d1c022db0fa57c5a2e78f3deb886abb5d8fac126a82c64137a8c6e27dd676ddefc38de9ed0f7bf99392fc49b8aeaa077c189d330e854c10fe3dec88ed35bf69b43cf8712ed619974d10700ec626a71584e3ca53a7dfb6eb559c) [2023-11-30 17:17:38,884] [ INFO] homekey :176 AUTH0 RES = ISO7816Response(sw1=0x90; sw2=0x00; data=864104ff99f76dde0aa59e3d95ef32ebc124e69baa4fe2f319dc598f642e7df3fd42367695625a8a9125e744648504055155cce6683e15d33fe771b65351933754bf5d(67)) [2023-11-30 17:17:38,887] [ INFO] homekey :275 endpoint_ephemeral_public_key_x.hex()='ff99f76dde0aa59e3d95ef32ebc124e69baa4fe2f319dc598f642e7df3fd4236' reader_ephemeral_public_key_x.hex()='c20cc6b5d72d020d5b2d3d1c022db0fa57c5a2e78f3deb886abb5d8fac126a82' [2023-11-30 17:17:38,888] [ INFO] homekey :287 authentication_hash_input.hex()='4d10700ec626a71584e3ca53a7dfb6eb559c8620ff99f76dde0aa59e3d95ef32ebc124e69baa4fe2f319dc598f642e7df3fd42368720c20cc6b5d72d020d5b2d3d1c022db0fa57c5a2e78f3deb886abb5d8fac126a824c10fe3dec88ed35bf69b43cf8712ed619979304415d9569' [2023-11-30 17:17:38,889] [ INFO] homekey :292 signature.hex()='3045022012cd625588cc1619af195d58fb30701c7144e0e023f11f8da4cd874e075adb7a022100fabc06ce0b5e7510343f9e0c9dee3fd8834b232db372ce0f050f4fe7b11806ab' (0x47) [2023-11-30 17:17:38,890] [ INFO] homekey :295 signature_point_form.hex()='12cd625588cc1619af195d58fb30701c7144e0e023f11f8da4cd874e075adb7afabc06ce0b5e7510343f9e0c9dee3fd8834b232db372ce0f050f4fe7b11806ab' (0x40) [2023-11-30 17:17:38,891] [ INFO] homekey :300 AUTH1 COMMAND ISO7816Command(cla=0x80; ins=0x81; p1=0x00; p2=0x00; lc=0x42(66); data=9e4012cd625588cc1619af195d58fb30701c7144e0e023f11f8da4cd874e075adb7afabc06ce0b5e7510343f9e0c9dee3fd8834b232db372ce0f050f4fe7b11806ab) [2023-11-30 17:17:38,941] [ WARNING] tt4 :105 ISO-DEP transmission error (#1) [2023-11-30 17:17:38,951] [ WARNING] tt4 :105 ISO-DEP transmission error (#2) [2023-11-30 17:17:38,961] [ WARNING] tt4 :105 ISO-DEP transmission error (#3) [2023-11-30 17:17:38,971] [ WARNING] tt4 :105 ISO-DEP transmission error (#4) [2023-11-30 17:17:38,981] [ WARNING] tt4 :105 ISO-DEP transmission error (#5) [2023-11-30 17:17:38,992] [ ERROR] tt4 :108 ISO-DEP unrecoverable transmission error [2023-11-30 17:17:38,992] [ ERROR] service :142 unrecoverable transmission error Traceback (most recent call last): File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 667, in send_cmd_recv_rsp return self.chipset.in_communicate_thru(data, timeout+0.1) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 421, in in_communicate_thru self.chipset_error(data) File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 124, in chipset_error raise Chipset.Error(errno, strerr) nfc.clf.pn53x.Chipset.Error: Error 0x0B: Protocol error during RF communication

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/tag/tt4.py", line 95, in exchange data = self.clf.exchange(data, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/init.py", line 1055, in exchange rcvd_data = exchange(self.target, send_data, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 673, in send_cmd_recv_rsp raise nfc.clf.TransmissionError(str(error)) nfc.clf.TransmissionError: Error 0x0B: Protocol error during RF communication

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/juaurodri/apple-home-key-reader-main/service.py", line 112, in _process_nfc result_flow, new_issuers_state, endpoint = read_homekey( ^^^^^^^^^^^^^ File "/home/juaurodri/apple-home-key-reader-main/homekey.py", line 733, in read_homekey result_flow, issuer, endpoint = perform_authentication_flow( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/juaurodri/apple-home-key-reader-main/homekey.py", line 594, in perform_authentication_flow k_persistent, endpoint, secure = standard_auth( ^^^^^^^^^^^^^^ File "/home/juaurodri/apple-home-key-reader-main/homekey.py", line 301, in standard_auth response = tag.transceive(command) ^^^^^^^^^^^^^^^^^^^^^^^ File "/home/juaurodri/apple-home-key-reader-main/util/iso7816.py", line 199, in transceive self._implementation.transceive( File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/tag/tt4.py", line 437, in transceive data = self._dep.exchange(data, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/tag/tt4.py", line 109, in exchange raise Type4TagCommandError(nfc.tag.RECEIVE_ERROR) nfc.tag.tt4.Type4TagCommandError: unrecoverable transmission error [2023-11-30 17:17:39,017] [ WARNING] service :143 Encountered an exception. Waiting for 5 seconds before continuing... [2023-11-30 17:17:44,018] [ INFO] service :147 Waiting for next device...

kormax commented 10 months ago

According to the logs the error is with NFC communication on lower protocol levels (RF/Transport). Taking a guess, I assume that the problem is with NFC antenna quality resulting in bad reception.

The Chinese PN532 modules are a lottery (if that's what you have), as the ones I have also exhibit vastly different performance, with some being able to read all of my devices in reasonable positions, while the others have weird "sweet spots". The only anomaly is that in your case it works on a watch and not on a phone, as from my experience it is usually the exact opposite (phone has a much more powerful antenna, so should be less prone to bad connection). Could be that your module is "too loud" (AKA clipping), so a phone with a powerful antenna fails, while watch with relatively weak one feeling OK with it.

In conclusion, if my assumptions about your set-up are correct, I would try using/buying another sample of this NFC module.

juaurodri commented 10 months ago

Thanks @kormax, glad I bought 4 :)

I will let you know if it works with other reader

thanks for the feedback

juaurodri commented 10 months ago

Hey @kormax,

So I think I found a sweet spot with this reader

If I tap 3cm from the reader, the iPhone and Watch works, lower than that the iPhone doesn't work. Sometimes the watch fails but not so regularly.

Nevertheless I will try with other readers.

Thanks

kormax commented 10 months ago

Yeah, just as I thought.

Basically, the issue comes from a fact that some knock-off module manufacturers, in order to save fractions of a penny on each module, constitute proper matching circuit components with some incredibly bad (totally wrong) alternatives that don't match the spec at all (but enable it to work at least somehow). The culprit is usually an inductor, and sometimes a resistor, I've managed to fix one module by finding a mismatched component via cross referencing and replacing it.

But it's usually not worth the trouble to figure out what exactly is causing the issue. It's easier to just take a couple of shots (better from different suppliers), usually at least one of them is responsible and makes everything up to spec.

If you're not into taking chances, you can also buy an original module from Elechouse, Adafruit, Sparkfun, etc. They cost as much as a couple of cheap ones, but should work much better.

juaurodri commented 10 months ago

Haa, makes totally sense. Thanks for the explanation

I will look for a shop in Europe that have this original module.

Thanks

juaurodri commented 10 months ago

To close this topic, nfc reader fixed in the door frame (metal) work flawless. Thanks