Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLE Protocol Reverse Engineering #1

Open
nccchirag opened this issue Jul 31, 2019 · 98 comments
Open

BLE Protocol Reverse Engineering #1

nccchirag opened this issue Jul 31, 2019 · 98 comments
Labels
help wanted Extra attention is needed

Comments

@nccchirag
Copy link
Owner

nccchirag commented Jul 31, 2019

Inputs from @matthias-schulz

yee-rc detected as F8:24:41:C1:D1:1F (Yeelink) -67 dBm.
 

 │   Handles    │ Service > Characteristics │  Properties   │         Data         │
├──────────────┼───────────────────────────┼───────────────┼──────────────────────┤
│ 0001 -> 001a │ fe95                      │               │                      │
│ 0003         │     0001                  │ WRITE, NOTIFY │                      │
│ 0007         │     0002                  │ READ          │ 0000                 │
│ 000a         │     0004                  │ READ          │ O993yDåo8f04X        │
│ 000d         │     0005                  │ WRITE, NOTIFY │                      │
│ 0010         │     0007                  │ WRITE         │                      │
│ 0013         │     0010                  │ WRITE         │                      │
│ 0016         │     0013                  │ READ, WRITE   │ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ │
│ 0019         │     0014                  │ READ, WRITE   │ L8e"ëad<01cúýéû 

LL Data: 05 22 ea 7f 8e f9 d1 c2 e0 ab df 41 24 f8 bb 3d e9 b1 d9 16 42 08 06 00 43 00 00 00 d0 07 ff ff ff ff 1f 10
[i] Got CONNECT_REQ packet from c2:d1:f9:8e:7f:ea to f8:24:41:df:ab:e0
 |-- Access Address: 0xb1e93dbb
 |-- CRC Init value: 0x4216d9
 |-- Hop interval: 67
 |-- Hop increment: 16
 |-- Channel Map: 1fffffffff
 |-- Timeout: 20000 ms

LL Data: 13 09 08 e1 00 00 00 00 00 00 00
LL Data: 0b 09 09 01 00 00 00 00 00 00 00
LL Data: 06 10 0c 00 05 00 12 01 08 00 10 00 20 00 00 00 c8 00
LL Data: 0a 0c 08 00 04 00 11 06 01 00 1a 00 95 fe
LL Data: 13 0c 00 08 06 00 24 00 00 00 c8 00 08 00
LL Data: 1e 0a 06 00 05 00 13 01 02 00 00 00
LL Data: 12 0b 07 00 04 00 10 1b 00 ff ff 00 28
LL Data: 0a 09 05 00 04 00 01 10 1b 00 00
LL Data: 12 0d 09 00 04 00 06 01 00 ff ff 00 28 95 fe
LL Data: 0a 09 05 00 04 00 07 01 00 1a 00
LL Data: 12 0d 09 00 04 00 06 1b 00 ff ff 00 28 95 fe
LL Data: 0a 09 05 00 04 00 01 06 1b 00 0a
LL Data: 12 0b 07 00 04 00 08 01 00 1a 00 02 28
LL Data: 0a 09 05 00 04 00 01 08 01 00 00
LL Data: 12 0b 07 00 04 00 08 01 00 1a 00 03 28
LL Data: 06 1b 17 00 04 00 09 07 02 00 18 03 00 01 00 06 00 02 07 00 02 00 09 00 02 0a 00 04 00
LL Data: 1e 0b 07 00 04 00 08 0b 00 1a 00 03 28
LL Data: 06 1b 17 00 04 00 09 07 0c 00 18 0d 00 05 00 0f 00 08 10 00 07 00 12 00 08 13 00 10 00


LL Data: 05 22 08 e4 ad a2 ac c8 1f d1 c1 41 24 f8 60 58 ac 0b 72 86 a0 08 06 00 43 00 00 00 d0 07 ff ff ff ff 1f 10
[i] Got CONNECT_REQ packet from c8:ac:a2:ad:e4:08 to f8:24:41:c1:d1:1f
 |-- Access Address: 0x0bac5860
 |-- CRC Init value: 0xa08672
 |-- Hop interval: 67
 |-- Hop increment: 16
 |-- Channel Map: 1fffffffff
 |-- Timeout: 20000 ms

LL Data: 13 09 08 e1 00 00 00 00 00 00 00
LL Data: 0b 09 09 01 00 00 00 00 00 00 00
LL Data: 12 0b 07 00 04 00 10 01 00 ff ff 00 28
LL Data: 0a 0c 08 00 04 00 11 06 01 00 1a 00 95 fe
LL Data: 13 0c 00 08 06 00 24 00 00 00 c8 00 08 00
LL Data: 1e 0a 06 00 05 00 13 01 02 00 00 00
LL Data: 12 0b 07 00 04 00 10 1b 00 ff ff 00 28
LL Data: 0a 09 05 00 04 00 01 10 1b 00 00
LL Data: 12 0d 09 00 04 00 06 01 00 ff ff 00 28 95 fe
LL Data: 12 0d 09 00 04 00 06 1b 00 ff ff 00 28 95 fe
LL Data: 0a 09 05 00 04 00 01 06 1b 00 0a
LL Data: 12 0b 07 00 04 00 08 01 00 1a 00 02 28
LL Data: 0a 09 05 00 04 00 01 08 01 00 00
LL Data: 12 0b 07 00 04 00 08 01 00 1a 00 03 28
LL Data: 0a 1b 17 00 04 00 09 07 02 00 18 03 00 01 00 06 00 02 07 00 02 00 09 00 02 0a 00 04 00
LL Data: 12 0b 07 00 04 00 08 0b 00 1a 00 03 28
LL Data: 0a 1b 17 00 04 00 09 07 0c 00 18 0d 00 05 00 0f 00 08 10 00 07 00 12 00 08 13 00 10 00


LL Data: 05 22 40 9f ce 64 21 c3 a3 d5 c1 41 24 f8 12 8c e2 7b eb 6e 0f 08 06 00 43 00 00 00 d0 07 ff ff ff ff 1f 05
[i] Got CONNECT_REQ packet from c3:21:64:ce:9f:40 to f8:24:41:c1:d5:a3
 |-- Access Address: 0x7be28c12
 |-- CRC Init value: 0x0f6eeb
 |-- Hop interval: 67
 |-- Hop increment: 5
 |-- Channel Map: 1fffffffff
 |-- Timeout: 20000 ms

LL Data: 13 09 08 e1 00 00 00 00 00 00 00
LL Data: 0b 09 09 01 00 00 00 00 00 00 00
LL Data: 12 0b 07 00 04 00 10 01 00 ff ff 00 28
LL Data: 0a 0c 08 00 04 00 11 06 01 00 1a 00 95 fe
LL Data: 0a 09 05 00 04 00 01 10 1b 00 00
LL Data: 12 0d 09 00 04 00 06 01 00 ff ff 00 28 95 fe
LL Data: 0a 09 05 00 04 00 07 01 00 1a 00
LL Data: 12 0d 09 00 04 00 06 1b 00 ff ff 00 28 95 fe
LL Data: 0a 09 05 00 04 00 01 06 1b 00 0a
LL Data: 12 0b 07 00 04 00 08 01 00 1a 00 02 28
LL Data: 0a 09 05 00 04 00 01 08 01 00 00
LL Data: 0a 1b 17 00 04 00 09 07 02 00 18 03 00 01 00 06 00 02 07 00 02 00 09 00 02 0a 00 04 00
LL Data: 12 0b 07 00 04 00 08 0b 00 1a 00 03 28
LL Data: 0a 1b 17 00 04 00 09 07 0c 00 18 0d 00 05 00 0f 00 08 10 00 07 00 12 00 08 13 00 10 00
LL Data: 12 0b 07 00 04 00 08 14 00 1a 00 03 28
LL Data: 0a 14 10 00 04 00 09 07 15 00 0a 16 00 13 00 18 00 0a 19 00 14 00
LL Data: 12 09 05 00 04 00 04 04 00 05 00
LL Data: 0a 0e 0a 00 04 00 05 01 04 00 02 29 05 00 01 29
LL Data: 12 09 05 00 04 00 04 08 00 08 00
LL Data: 0a 0a 06 00 04 00 05 01 08 00 01 29
LL Data: 12 09 05 00 04 00 04 0b 00 0b 00
LL Data: 0a 0a 06 00 04 00 05 01 0b 00 01 29
LL Data: 0a 0a 06 00 04 00 05 01 0e 00 01 29
LL Data: 0a 0a 06 00 04 00 05 01 11 00 01 29
LL Data: 12 09 05 00 04 00 04 14 00 14 00
LL Data: 0a 0a 06 00 04 00 05 01 14 00 01 29
LL Data: 12 09 05 00 04 00 04 17 00 17 00
LL Data: 0a 0a 06 00 04 00 05 01 17 00 01 29
LL Data: 12 09 05 00 04 00 04 1a 00 1a 00
LL Data: 0a 0a 06 00 04 00 05 01 1a 00 01 29
LL Data: 12 0b 07 00 04 00 12 13 00 90 ca 85 de
LL Data: 0a 05 01 00 04 00 13
LL Data: 12 09 05 00 04 00 12 04 00 01 00
LL Data: 0a 05 01 00 04 00 13
LL Data: 12 13 0f 00 04 00 12 03 00 8c d1 cf 62 43 fb b1 d3 f8 2a f2 b9
LL Data: 1a 05 01 00 04 00 13
LL Data: 06 13 0f 00 04 00 1b 03 00 5e 6a 72 c9 52 b1 95 a9 2c 0f 1f 51
LL Data: 1e 0b 07 00 04 00 12 03 00 99 7b 30 c5
LL Data: 06 05 01 00 04 00 13
LL Data: 1e 07 03 00 04 00 0a 19 00
LL Data: 06 11 0d 00 04 00 0b 4c 0a 2a 21 a8 c9 4a 69 63 4c e7 31
LL Data: 1f 02 02 13
@nccchirag
Copy link
Owner Author

nccchirag commented Aug 2, 2019

BLEScanner Connection Data

Custom Service
0000FE95-0000-1000-8000-00805F9B34FB
	Custom Characteristic
		UUID: 00000001-0000-1000-8000-00805F9B34FB
		Properties: WRITE,NOTIFY
		Write Type:WRITE REQUEST

		Descriptors:
		Client Characteristic Configuration
		UUID: 0x2902
		Notifications or indications disabled
		Characteristic User Description
		UUID: 0x2901
		token
	Custom Characteristic
		UUID: 00000002-0000-1000-8000-00805F9B34FB
		Properties: READ
		Value:
		Hex: 0x0000

		Descriptors:
		Characteristic User Description
		UUID: 0x2901
		productid
	Custom Characteristic
		UUID: 00000004-0000-1000-8000-00805F9B34FB
		Properties: READ
		Value:\ ?
		A *
		Hex: 0x5C0B1F0A410C2AB0394B

		Descriptors:
		Characteristic User Description
		UUID: 0x2901
		ver
	Custom Characteristic
		UUID: 00000005-0000-1000-8000-00805F9B34FB
		Properties: WRITE,NOTIFY
		Write Type:WRITE REQUEST

		Descriptors:
		Characteristic User Description
		UUID: 0x2901
		wificfg
	Custom Characteristic
		UUID: 00000007-0000-1000-8000-00805F9B34FB
		Properties: WRITE
		Write Type:WRITE REQUEST

		Descriptors:
		Characteristic User Description
		UUID: 0x2901
		eventrule
	Custom Characteristic
		UUID: 00000010-0000-1000-8000-00805F9B34FB
		Properties: WRITE
		Write Type:WRITE REQUEST

		Descriptors:
		Characteristic User Description
		UUID: 0x2901
		authentication
	Custom Characteristic
		UUID: 00000013-0000-1000-8000-00805F9B34FB
		Properties: READ,WRITE
		Write Type:WRITE REQUEST

		Descriptors:
		Characteristic User Description
		UUID: 0x2901
		sn
	Custom Characteristic
		UUID: 00000014-0000-1000-8000-00805F9B34FB
		Properties: READ,WRITE
		Write Type:WRITE REQUEST

		Descriptors:
		Characteristic User Description
		UUID: 0x2901
		beaconkey

@nccchirag nccchirag pinned this issue Aug 2, 2019
@saeugetier
Copy link

When looking at the names of the services, the name "authentication" is noticeable. Hopefully the dimmer has not to be activated by using some cryptographic keys. Due to the fact that the device is very cheap, it might be a very weak authentication.

Maybe a look on other Xiaomi reverse engineering projects might help. I found a project about the Mijia temperature sensor. I'm not sure if this sensor has some authentication implemented: https://github.com/mspider65/Xiaomi-Mijia-Bluetooth-Temperature-and-Humidity-Sensor

But there is a demo project for the Mijia (from a chinese guy), which implements some authentication: https://github.com/MiEcosystem/mijia_ble

@nccchirag
Copy link
Owner Author

nccchirag commented Aug 12, 2019

thank you @saeugetier for the assistance
yes, it seems authentication might be weak and the dimmer supports mi Home app so, mostly it would be implementing the same authentication as the one mentioned in the link you shared. Let me have a look

@0xabadc0fe 你能在这里协助吗? 谢谢

@saeugetier
Copy link

I'm interested in the encoder as well. So I ordered a sample as well. The microcontroller might have an external flash which hides some secrets.

An interesting article about how to reverse BLE can be found here.
https://medium.com/@yogeshojha/i-hacked-xiaomi-miband-3-and-here-is-how-i-did-it-43d68c272391
Maybe the authentication process is the same as for the Mi Band 3. The IOT gadgets and the fitness tracker are developed by different teams, but with some luck it might be the same.

@nccchirag
Copy link
Owner Author

there isn't any external flash inside, just one BLE module (TLSR8267 controller). All those article have one thing in common which is, (Device) <----> (Mi Home App), hence one can snoop BLE packets using developer mode settings in an Android phone but with this dimmer/encoder the command is sent to ceiling light and not to the app hence snooping is difficult. Here's the link to the image that details how communication happens. I'm stuck as I don't have the ceiling light to verify the same.

The only way I can think getting around this is to emulate esp32 as a rotary encoder by advertising the same characteristics and inspect packets sent to it. Authentication happens just once when the dimmer is paired to the light. At this time all 3 devices are required - Mi Home App, Dimmer & Light
Check this operation demo. During the demo the phone Bluetooth seems Off (unless he has hide that icon from status bar, which is rare). The dimmer is enumerated in the app via the ceiling light (which has both BLE & Wifi), and I think the pairing process is complete before that

@nikko82
Copy link

nikko82 commented Aug 14, 2019

I've been able to authenticate to the dimmer (I think) using a ESP32 and a ported version of the authentication logic from here: https://github.com/aprosvetova/xiaomi-kettle
The productId seems to be 950.
I'm stuck at what to do after authenticating. I've tried to write 0x92, 0xAB, 0x54, 0xFA to authCharacteristic, and writing 0x99, 0x7b, 0x30, 0xc5 instead which is what I can see from your log in the first post. I then read verCharacteristics (UUID 0004), or UUID 0014 which it seems like is read in the log. I've then tried to update the list of characteristics, but it doesn't change (maybe I need to run some command to update it?).

Could you post a log showing what happens after the authentication is done, with some commands going from the dimmer? If you could also upload the pcap-log that would be nice. (I suppose the log is from BtleJack?)

@nccchirag
Copy link
Owner Author

sorry @nikko82 I haven't been able to take out time for this, thank you very much for your input. will give it a shot when time permits. The previous logs were shared by @matthias-schulz so can't comment on it.

@nikko82
Copy link

nikko82 commented Aug 19, 2019

I found out, thanks to https://github.com/drndos/mi-kettle-poc/blob/master/mi-kettle.py that the last thing that is send to authCharacteristic must be calculated with cipher(token, [0x92, 0xAB, 0x54, 0xFA]).

I've found the product key both by automating a brute force attack where I tried all keys until authentication was successful and also by reversing the cipher algorithm and brute forcing based on the data in the log from @matthias-schulz. Both ways gave the productId of 950.

Using that productId and the token i found from the response I was able to calculate the same authentication values that was sent by the lamp in the log. This makes me very confident that all the data I'm sending to the dimmer is correct. But still I can't figure out how to subscribe to commands from the dimmer.

@saeugetier
Copy link

@nikko82 sounds good. I wish you good luck. As soon as my device arrives, I will take a look, if i can help with reversing.

@nikko82
Copy link

nikko82 commented Aug 21, 2019

@saeugetier do you have a micro:bit or any other equipment that you can use to sniff the traffic?

@saeugetier
Copy link

@nikko82 I think I have several devices which can be used for sniffing. But I don't have a Yeelight light. I only own the dimmer.

@saeugetier
Copy link

For your information: currently I'm working on an esp32 implementation of the dimmer knob. It is not fully working for now. But you can stay tuned:

https://github.com/saeugetier/LowPowerDimmerKnop

Reversing the protocoll doesn't seem easy. Building an own firmware seems to be the way with less effort. But the hardware has to be modified. The new implementation will use wifi and mqtt.

Current state: the rotary encoder and push button are evaluated via ULP. Next step is to remove all debug variables and write the wifi code. Very next step: enable OTA and wifi setting dialog via button on top.

@thedayu
Copy link

thedayu commented Jan 1, 2020

Any progress here?
I tried YLKG08YL, and found that THIS device sends BLE advertise at clicking or rotating, no matter it is connected or not.

My guess is that It sends command thru BLE adv data. Here is the data i got
[clockwise, counter clockwise, click, double click, long press]

Type: 0x16 Value: 95FE5830B603E6381FC34124F81962647279260400004A
Type: 0x16 Value: 95FE5830B603E7381FC34124F881FE75CD2DBE040000D0
Type: 0x16 Value: 95FE5830B603E8381FC34124F81E9180BCC248040000B4
Type: 0x16 Value: 95FE5830B603E9381FC34124F8A2FB15DDC53C040000A6
Type: 0x16 Value: 95FE5830B603EA381FC34124F8BF414286576E040000A5

My read:
95FE5830B603 - EA - 381FC34124F8 - BF414286576E - 04 - 0000A5
[service id] [Command SN] [device id] [command content] [time] [checksum]

Command SN is increased by 1 and goes to 0 after 0xff.
[[[Need help]]] command content is different every time, should be encoded with something.

@thedayu
Copy link

thedayu commented Jan 1, 2020

More log for clockwise rotation:

 Type: 0x16 Value: 95FE5830B6037B381FC34124F8837E33ED9CB50800005C
 Type: 0x16 Value: 95FE5830B6037C381FC34124F8353FBAFAED2508000013
 Type: 0x16 Value: 95FE5830B6037D381FC34124F8FD9E018C46FE08000009
 Type: 0x16 Value: 95FE5830B6037E381FC34124F897645E49EAB2080000F3
 Type: 0x16 Value: 95FE5830B6037F381FC34124F87898DA62791E0800005C
 Type: 0x16 Value: 95FE5830B6037F381FC34124F87898DA62791E0800005C
 Type: 0x16 Value: 95FE5830B60380381FC34124F89E27ABB24B6F08000040
 Type: 0x16 Value: 95FE5830B60381381FC34124F8D91603561B7E080000CA
 Type: 0x16 Value: 95FE5830B60382381FC34124F8886E32BAA798080000F1
 Type: 0x16 Value: 95FE5830B60382381FC34124F8886E32BAA798080000F1
 Type: 0x16 Value: 95FE5830B60383381FC34124F86EA7D1F540CF08000052
 Type: 0x16 Value: 95FE5830B60384381FC34124F83A706F766DA208000085
 Type: 0x16 Value: 95FE5830B60385381FC34124F869A53577F37708000015
 Type: 0x16 Value: 95FE5830B60386381FC34124F8F14A85463CBE0800004F
 Type: 0x16 Value: 95FE5830B60387381FC34124F85474F4AB55E808000013
 Type: 0x16 Value: 95FE5830B60388381FC34124F89B2AD0B7E7C4080000BD
 Type: 0x16 Value: 95FE5830B60389381FC34124F8893D613033A50800008A
 Type: 0x16 Value: 95FE5830B6038A381FC34124F8298A4AD061C508000045
 Type: 0x16 Value: 95FE5830B6038A381FC34124F8298A4AD061C508000045
 Type: 0x16 Value: 95FE5830B6038B381FC34124F8E0B0EA70E35608000059

@nccchirag
Copy link
Owner Author

Update
Happy new to year to all!

sorry, I haven't got the time to work on this, I also have ordered corresponding light which may help in reverse engineering, but I haven't received it yet .. will try extracting time .. thank you all for the help

@thedayu
Copy link

thedayu commented Jan 2, 2020

MORE INFO:
90% sure it is using Mibeacon protocol, described as follow:

	ServiceID	[2Byte]	95FE
	FrameControl	[2B]	5830 --> Encyrpted, contains Obj, version 3, no auth.
	Product ID	[2B]	B603
	Sequence	[1B]	8B
	MAC		[6B]	381FC34124F8 <- My device.
	OBJ Encyrpted	[6B]	E0B0EA70E356  <<<<<<-----------
	Ext seq		[3B]	080000
	No idea		[1B]	59    <---might be message integrity check, but according to spec,   MIC should be 4bytes long.

Digged into mi ble code:
payload object is encrypted with aes_ccm_encrypt_and_tag, which takes 16 bytes beacon key.

Hold the small button on the top for 3 seconds, and connect, there is a BLE Char described as "beaconkey", and can read out 12bytes content. I think it should be part of AES key. (which changes every connection, explains how "bonding").
I have put up code for AES decryption and tried with Product id/Service id and some other things, add up with that 12bytes beaconkey to get 16 bytes as key, so far, no luck.

Custom Characteristic
	UUID: 00000014-0000-1000-8000-00805F9B34FB
	Properties: READ,WRITE
	Write Type:WRITE REQUEST

	Descriptors:
	Characteristic User Description
	UUID: 0x2901
	beaconkey

@Busyrev
Copy link

Busyrev commented Jan 15, 2020

@syu-source as @Staars mensioned, there is a strange repo with no description and some code around mibeacon protocol with ccm_auth_crypt aes_ccm_encrypt_and_tag inside. https://github.com/MiEcosystem/mijia_ble_common May be it will help you

@thedayu
Copy link

thedayu commented Jan 25, 2020

Yes, i am using code from this repo.
For now the issue is, what is the 16bytes key to decrypt.

@ufanders
Copy link

ufanders commented Jan 28, 2020

Maybe this can help?:

https://medium.com/@yogeshojha/i-hacked-xiaomi-miband-3-and-here-is-how-i-did-it-43d68c272391

I just got a Yeelight dimmer switch too, I'd love to use it as-is without replacing the BLE platform it uses!

@kueblc
Copy link

kueblc commented Jan 28, 2020

@nikko82 do you have a proof of concept for authentication? Trying to see if I can adapt the mi-kettle code you linked but I'm not that familiar with BLE. Not sure what handle to use for auth and authInit.

EDIT: I believe I've authenticated, using authInit = 19 and auth = 3. I noticed that these handles had the UUIDs of 0010 and 0001 respectively, which corresponds with what I've read on aprosvetova/xiaomi-kettle. I'm curious as to how @aprosvetova found this mapping. Using these handles and the PoC for mi-kettle from @drndos I received a token that matched the "should be token". If I'm understanding this correctly it looks like next I need to find the UUID for status.

@syu-source how did you capture the data from the advertise packet?

@kueblc
Copy link

kueblc commented Jan 29, 2020

MORE INFO:
90% sure it is using Mibeacon protocol, described as follow:

	ServiceID	[2Byte]	95FE
	FrameControl	[2B]	5830 --> Encyrpted, contains Obj, version 3, no auth.
	Product ID	[2B]	B603
	Sequence	[1B]	8B
	MAC		[6B]	381FC34124F8 <- My device.
	OBJ Encyrpted	[6B]	E0B0EA70E356  <<<<<<-----------
	Ext seq		[3B]	080000
	No idea		[1B]	59    <---might be message integrity check, but according to spec,   MIC should be 4bytes long.

@syu-source I've reached pretty much the same conclusion as you here, except what concerns me is that the "encrypted obj" is a very strange length for it to be AES128. I would expect it to match the block size of 128 bits / 16 bytes. What could you do with 6 bytes?

The product ID makes total sense, if we take B603 as a LE decimal, I see we get 950, the previous found product ID.

@thedayu
Copy link

thedayu commented Jan 29, 2020

I didn't look into details but, these tow functions take any length of data, and as I tested, they work with 6 bytes.
aes_ccm_encrypt_and_tag
aes_ccm_auth_decrypt

	uint8_t pi[32] = { 0xF6,0xA7,0x5E,0x39,0x7A,0x72 };
	uint8_t po[32];
	uint8_t pt[32];
	dumphex(pi, 6);
	aes_ccm_encrypt_and_tag(key, c, sizeof(beacon_nonce), &aad, 1, pi, 6, po, mic, sizeof(mic));
	dumphex(po, 6);
	aes_ccm_auth_decrypt(key, c, sizeof(beacon_nonce), &aad, 1, po, 6, pt, mic, 4);
	dumphex(pt, 6);

pi equals to pt.

@kueblc
Copy link

kueblc commented Feb 3, 2020

@syu-source I found this document which shows it is legal to have a 1-byte MIC.

@aheadlead
Copy link

thank you @saeugetier for the assistance
yes, it seems authentication might be weak and the dimmer supports mi Home app so, mostly it would be implementing the same authentication as the one mentioned in the link you shared. Let me have a look

@0xabadc0fe 你能在这里协助吗? 谢谢

@0xabadc0fe seems like a Xiaomi employee since his email address ends with xiaomi.com and he will not provide us with any help obviously.

@joeschny
Copy link

joeschny commented Apr 7, 2020

Hello,
is this thread dead ?
thx

@Busyrev
Copy link

Busyrev commented May 22, 2021

Works. posted results here custom-components/ble_monitor#353 (comment)

@Ernst79
Copy link

Ernst79 commented May 22, 2021

Script to get the beaconkey is added in the BLE monitor documentation (including updated script for more devices).

@Busyrev
Copy link

Busyrev commented May 22, 2021

Confirming total success. Got encryption key using get_beacon_key, installed HomeAssistant with https://github.com/custom-components/ble_monitor and cofigured device using key. It works.

@wouterf11
Copy link

@Ernst79 could you elaborate on how the BLE Monitor works for the YLKG08YL dimmer? How should the data on https://github.com/archaron/docs/blob/master/BLE/ylkg08y.md be combined into decoding a message, say:
Click once
58 30 b6 03 | c1 | 69 44 c2 41 24 f8 | 17 4a bb 49 78 d3 02 | 00 00 | 63

with the beaconkey: 10 d8 99 8c 8e cd 72 9d e4 21 02 8d

From what I gather from xiaomi.py:
key = "10 d8 99 8c 8e cd 8d 3d 3c 97 72 9d e4 21 02 8d" (beacon(1:6), 8d3d3c97, beacon(6:end))
nonce (iv) = "58 30 b6 03 c1 02 00 00 69 44 c2 41 24" (frame, dev, count, reverse mac(:-1) where count = (packet id, msg(-4:-1))
cipherdata = "17 4a bb 49 78"
authdata = "11"

Is (any of) that correct? Many thanks in advance!

@rezmus
Copy link

rezmus commented May 22, 2021

@wouterf11 beaconkey is encrypted in this example (was not decrypted with token). use this sample data for dimmer to check your code.

custom-components/ble_monitor#289 (comment)

@Ernst79
Copy link

Ernst79 commented May 22, 2021

Here is a little python script that shows the decryption.
Your message starts at frame ctrl and stops before rssi.

from Cryptodome.Cipher import AES

data_string = "043e25020103008b98c54124f819181695fe5830b603368b98c54124f88bb8f2661351000000d6ef"
aeskey = "b853075158487ca39a5b5ea9"

#                                       frame dev ct ---mac------ ----encrypted payload- rssi
#                                       ctrl  id                  cipherpayld- -cnt-- tk 
#  043e25020103008b98c54124f819181695fe 5830 b603 36 8b98c54124f8 8bb8f2661351 000000 d6   ef


data = bytes(bytearray.fromhex(data_string))
key = bytes.fromhex(aeskey)

key_1 = key[0:6]
key_2 = bytes.fromhex("8d3d3c97")
key_3 = key[6:]
key = b"".join([key_1, key_2, key_3])
print("key: ", key.hex())

xiaomi_index = data.find(b'\x16\x95\xFE')
xiaomi_mac_reversed = data[xiaomi_index + 8:xiaomi_index + 14]
print("reversed mac: ", xiaomi_mac_reversed.hex())
# reversed mac: 8b98c54124f8

framectrl_data = data[xiaomi_index + 3:xiaomi_index + 5]
print("frame ctrl: ", framectrl_data.hex())
# frame ctrl: 5830

device_type = data[xiaomi_index + 5:xiaomi_index + 7]
print("device type (product id): ", device_type.hex())
# device type (product id): b603

encrypted_payload = data[xiaomi_index + 14:-1]
print("encrypted payload: ", encrypted_payload.hex())
# encrypted payload: 8bb8f2661351000000d6

packet_id = data[xiaomi_index + 7:xiaomi_index + 8]
payload_counter = b"".join([packet_id,  encrypted_payload[-4:-1]])
print("payload counter: ", payload_counter.hex())
# payload_counter: 36000000

nonce = b"".join([framectrl_data, device_type, payload_counter, xiaomi_mac_reversed[:-1]])
print("nonce: ", nonce.hex())
# nonce: 5830b603360000008b98c54124

aad = b"\x11"

token = encrypted_payload[-1:]
print("token: ", token.hex())
# token: d6

cipherpayload = encrypted_payload[:-4]
print("cipher payload: ", cipherpayload.hex())
# cipher payload: 8bb8f2661351

cipher = AES.new(key, AES.MODE_CCM, nonce=nonce, mac_len=4)
cipher.update(aad)

decrypted_payload = cipher.decrypt(cipherpayload)
print("decrypted payload: ", decrypted_payload.hex())
# decrypted payload:  01100300ff04

The decrypted payload can be read as follows.
0110 = Button (= type of message according to the MiBeacon protocol)
03 = length of data
00 = button
ff = value
04 = press

button, value and press are the names I use in BLE monitor, depending on the device type, they are translated to a message. See the def obj0110(xobj): function in https://github.com/custom-components/ble_monitor/blob/master/custom_components/ble_monitor/ble_parser/xiaomi.py. In this example, press 04 + button = 0 means "rotate left" with (256 - 255(= ff) = 1 steps.

I also tried your BLE advertisement + beaconkey, but it doesn't seem to be right, I get this as result.

decrypted payload: ab330e5cbc82

@rezmus
Copy link

rezmus commented May 22, 2021

I also tried your BLE advertisement + beaconkey, but it doesn't seem to be right, I get this as result.

he used sample data from old github, where beaconkey was not decrypted using token.

beacon_key = cipher(token, peripheral.readCharacteristic(HANDLE_BEACON_KEY)).hex()

@wouterf11
Copy link

Thanks a lot!! I will have a look.

@kirilldobr
Copy link

I successfully got dimmer beakonKey using method 6 from FAQ,
but after adding the integration to HA I am getting weird errors:

2021-05-23 13:30:03 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] Data binary sensor received: {'rssi': -55, 'mac': 'F82441C371CD', 'type': 'YLKG07YL/YLKG08YL', 'packet': 1, 'firmware': 'Xiaomi (MiBeacon)', 'data': True}
2021-05-23 13:30:03 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Data measuring sensor received: {'rssi': -55, 'mac': 'F82441C371CD', 'type': 'YLKG07YL/YLKG08YL', 'packet': 1, 'firmware': 'Xiaomi (MiBeacon)', 'data': True}
2021-05-23 13:30:16 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] 5 MiBeacon BLE ADV messages processed for 0 binary sensor device(s) total. Priority queue = 0
2021-05-23 13:30:16 DEBUG (MainThread) [custom_components.ble_monitor.sensor] 5 BLE ADV messages processed for 1 measuring device(s).
2021-05-23 13:30:16 DEBUG (Thread-4) [custom_components.ble_monitor] HCIdump thread: main event_loop stopped, finishing
2021-05-23 13:30:16 DEBUG (Thread-4) [custom_components.ble_monitor] HCIdump thread: Scanning will be restarted
2021-05-23 13:30:16 DEBUG (Thread-4) [custom_components.ble_monitor] 3121 HCI events processed for previous period.
2021-05-23 13:30:16 DEBUG (Thread-4) [custom_components.ble_monitor] HCIdump thread: Run
2021-05-23 13:30:16 DEBUG (Thread-4) [custom_components.ble_monitor] HCIdump thread: connected to hci0
2021-05-23 13:30:16 DEBUG (Thread-4) [custom_components.ble_monitor] HCIdump thread: start main event_loop
2021-05-23 13:30:31 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Decryption MiBeacon V2/V3 advertisement failed: No encryption key found
2021-05-23 13:30:31 DEBUG (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Invalid data: Data decryption failed
2021-05-23 13:30:32 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Decryption MiBeacon V2/V3 advertisement failed: No encryption key found
2021-05-23 13:30:32 DEBUG (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Invalid data: Data decryption failed
2021-05-23 13:30:33 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] Data binary sensor received: {'rssi': -58, 'mac': 'F82441C371CD', 'type': 'YLKG07YL/YLKG08YL', 'packet': 1, 'firmware': 'Xiaomi (MiBeacon)', 'data': True}
2021-05-23 13:30:33 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Data measuring sensor received: {'rssi': -58, 'mac': 'F82441C371CD', 'type': 'YLKG07YL/YLKG08YL', 'packet': 1, 'firmware': 'Xiaomi (MiBeacon)', 'data': True}
2021-05-23 13:30:33 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Decryption MiBeacon V2/V3 advertisement failed: No encryption key found
2021-05-23 13:30:33 DEBUG (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Invalid data: Data decryption failed
2021-05-23 13:30:35 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Decryption MiBeacon V2/V3 advertisement failed: No encryption key found
2021-05-23 13:30:35 DEBUG (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Invalid data: Data decryption failed
2021-05-23 13:30:35 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Decryption MiBeacon V2/V3 advertisement failed: No encryption key found
2021-05-23 13:30:35 DEBUG (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Invalid data: Data decryption failed
2021-05-23 13:30:37 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Decryption MiBeacon V2/V3 advertisement failed: No encryption key found
2021-05-23 13:30:37 DEBUG (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Invalid data: Data decryption failed
2021-05-23 13:30:37 ERROR (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Decryption MiBeacon V2/V3 advertisement failed: No encryption key found
2021-05-23 13:30:37 DEBUG (Thread-4) [custom_components.ble_monitor.ble_parser.xiaomi] Invalid data: Data decryption failed

My configuration.yaml:

ble_monitor:
  devices:
    - mac: 'CD:71:C3:41:24:F8'
      name: 'myDimmer'
      encryption_key: '346B958D1E040ED240AB84B7'

They seem to happen every time dimmer action is sent...
Does anyone know what can cause such a behaviour?

@Ernst79
Copy link

Ernst79 commented May 23, 2021

Did you restart HA?

@kirilldobr
Copy link

Yes I did, but the result is the same.
It’s state changes between unknown (dark gray) and unavailable (light gray, during events).
67ED1DE6-FA9C-4C69-A824-0FE16F1E642A

@Ernst79
Copy link

Ernst79 commented May 23, 2021

Can you show the log from right after the start of HA?

@kirilldobr
Copy link

That's debug log enabled, of course.

[s6-finish] sending all processes the TERM signal.
[s6-finish] sending all processes the KILL signal and exiting.
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] udev.sh: executing... 
[cont-init.d] udev.sh: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
2021-05-23 16:38:36 WARNING (MainThread) [homeassistant.loader] You are using a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2021-05-23 16:38:36 WARNING (MainThread) [homeassistant.loader] You are using a custom integration ble_monitor which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2021-05-23 16:38:36 WARNING (MainThread) [homeassistant.loader] You are using a custom integration yandex_smart_home which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2021-05-23 16:38:42 DEBUG (MainThread) [custom_components.ble_monitor] Initializing BLE Monitor integration (YAML): {'devices': [{'mac': 'CD:71:C3:41:24:F8', 'name': 'Крутилочка', 'encryption_key': '346B958D1E040ED240AB84B7', 'reset_timer': 35, 'restore_state': 'default', 'decimals': 'default', 'use_median': 'default'}], 'bt_interface': ['DC:A6:32:F7:CD:74'], 'log_spikes': False, 'restore_state': False, 'active_scan': False, 'hci_interface': [], 'discovery': True, 'report_unknown': False, 'period': 60, 'decimals': 1, 'rounding': 1, 'batt_entities': True, 'use_median': False, 'is_flow': False, 'ids_from_name': True}
2021-05-23 16:38:42 DEBUG (MainThread) [custom_components.ble_monitor.config_flow] async_step_import: {'devices': [{'mac': 'CD:71:C3:41:24:F8', 'name': 'Крутилочка', 'encryption_key': '346B958D1E040ED240AB84B7', 'reset_timer': 35, 'restore_state': 'default', 'decimals': 'default', 'use_median': 'default'}], 'bt_interface': ['DC:A6:32:F7:CD:74'], 'log_spikes': False, 'restore_state': False, 'active_scan': False, 'hci_interface': [], 'discovery': True, 'report_unknown': False, 'period': 60, 'decimals': 1, 'rounding': 1, 'batt_entities': True, 'use_median': False, 'is_flow': False, 'ids_from_name': True}
2021-05-23 16:38:42 DEBUG (MainThread) [custom_components.ble_monitor.config_flow] async_step_user: {'devices': '--Devices--', 'bt_interface': ['DC:A6:32:F7:CD:74'], 'log_spikes': False, 'restore_state': False, 'active_scan': False, 'hci_interface': [], 'discovery': True, 'report_unknown': False, 'period': 60, 'decimals': 1, 'rounding': 1, 'batt_entities': True, 'use_median': False, 'is_flow': False, 'ids_from_name': True}
2021-05-23 16:38:46 DEBUG (MainThread) [custom_components.ble_monitor] Initializing BLE Monitor entry (config entry): <homeassistant.config_entries.ConfigEntry object at 0x7fa942d460>
2021-05-23 16:38:46 DEBUG (MainThread) [custom_components.ble_monitor] async_setup_entry: domain {'devices': [{'mac': 'CD:71:C3:41:24:F8', 'name': 'Крутилочка', 'encryption_key': '346B958D1E040ED240AB84B7', 'reset_timer': 35, 'restore_state': 'default', 'decimals': 'default', 'use_median': 'default'}], 'bt_interface': ['DC:A6:32:F7:CD:74'], 'log_spikes': False, 'restore_state': False, 'active_scan': False, 'hci_interface': [], 'discovery': True, 'report_unknown': False, 'period': 60, 'decimals': 1, 'rounding': 1, 'batt_entities': True, 'use_median': False, 'is_flow': False, 'ids_from_name': True}
2021-05-23 16:38:46 WARNING (MainThread) [custom_components.ble_monitor] Available Bluetooth interfaces for BLE monitor: ['DC:A6:32:F7:CD:74']
2021-05-23 16:38:46 DEBUG (MainThread) [custom_components.ble_monitor] async_setup_entry: {'devices': [{'mac': 'CD:71:C3:41:24:F8', 'name': 'Крутилочка', 'encryption_key': '346B958D1E040ED240AB84B7', 'reset_timer': 35, 'restore_state': 'default', 'decimals': 'default', 'use_median': 'default'}], 'bt_interface': ['DC:A6:32:F7:CD:74'], 'log_spikes': False, 'restore_state': False, 'active_scan': False, 'hci_interface': [0], 'discovery': True, 'report_unknown': False, 'period': 60, 'decimals': 1, 'rounding': 1, 'batt_entities': True, 'use_median': False, 'is_flow': False, 'ids_from_name': True}
2021-05-23 16:38:46 DEBUG (MainThread) [custom_components.ble_monitor] HCI interface is [0]
2021-05-23 16:38:46 DEBUG (MainThread) [custom_components.ble_monitor] Spawning HCIdump thread
2021-05-23 16:38:46 DEBUG (MainThread) [custom_components.ble_monitor] HCIdump thread: Init
2021-05-23 16:38:46 DEBUG (MainThread) [custom_components.ble_monitor] 1 encryptors mac:key pairs loaded.
2021-05-23 16:38:46 DEBUG (MainThread) [custom_components.ble_monitor] whitelist: []
2021-05-23 16:38:46 DEBUG (MainThread) [custom_components.ble_monitor] 0 whitelist item(s) loaded.
2021-05-23 16:38:46 DEBUG (Thread-5) [custom_components.ble_monitor] HCIdump thread: Run
2021-05-23 16:38:46 DEBUG (Thread-5) [custom_components.ble_monitor] HCIdump thread: connected to hci0
2021-05-23 16:38:46 DEBUG (Thread-5) [custom_components.ble_monitor] HCIdump thread: start main event_loop
2021-05-23 16:38:47 WARNING (MainThread) [homeassistant.config_entries] Config entry 'MFC-J2510 E71354E3F408895' for brother integration not ready yet: Bad IPv4/UDP transport address BRW1C3E84A7D7C3.local@161: [Errno -2] Name does not resolvecaused by <class 'socket.gaierror'>: [Errno -2] Name does not resolve; Retrying in background
2021-05-23 16:38:48 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] Starting binary sensor entry startup
2021-05-23 16:38:48 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] BLE binary sensors updater initialization
2021-05-23 16:38:48 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] BLE binary sensors updater initialized
2021-05-23 16:38:48 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] Binary sensor entry setup finished
2021-05-23 16:38:48 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Starting measuring sensor entry startup
2021-05-23 16:38:48 DEBUG (MainThread) [custom_components.ble_monitor.sensor] BLE sensors updater initialization
2021-05-23 16:38:48 DEBUG (MainThread) [custom_components.ble_monitor.sensor] BLE sensors updater initialized
2021-05-23 16:38:48 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Measuring sensor entry setup finished
2021-05-23 16:38:48 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] Binary entities updater loop started!
2021-05-23 16:38:48 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Entities updater loop started!
2021-05-23 16:38:50 WARNING (MainThread) [homeassistant.helpers.entity] Updating state for remote.xiaomi_miio_192_168_1_82 (<class 'homeassistant.components.xiaomi_miio.remote.XiaomiMiioRemote'>) took 0.639 seconds. Please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+xiaomi_miio%22
2021-05-23 16:39:09 ERROR (MainThread) [pyhap.characteristic] SecuritySystemCurrentState: value=0 is an invalid value.
2021-05-23 16:39:09 ERROR (MainThread) [pyhap.characteristic] SecuritySystemTargetState: value=0 is an invalid value.
2021-05-23 16:39:40 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'click' when rendering '{{ value_json.click }}'
2021-05-23 16:39:41 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'click' when rendering '{{ value_json.click }}'
2021-05-23 16:39:41 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'click' when rendering '{{ value_json.click }}'
2021-05-23 16:39:48 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] 0 MiBeacon BLE ADV messages processed for 0 binary sensor device(s) total. Priority queue = 0
2021-05-23 16:39:48 DEBUG (MainThread) [custom_components.ble_monitor.sensor] 0 BLE ADV messages processed for 0 measuring device(s).
2021-05-23 16:39:48 DEBUG (Thread-5) [custom_components.ble_monitor] HCIdump thread: main event_loop stopped, finishing
2021-05-23 16:39:48 DEBUG (Thread-5) [custom_components.ble_monitor] HCIdump thread: Scanning will be restarted
2021-05-23 16:39:48 DEBUG (Thread-5) [custom_components.ble_monitor] 3085 HCI events processed for previous period.
2021-05-23 16:39:48 DEBUG (Thread-5) [custom_components.ble_monitor] HCIdump thread: Run
2021-05-23 16:39:48 DEBUG (Thread-5) [custom_components.ble_monitor] HCIdump thread: connected to hci0
2021-05-23 16:39:48 DEBUG (Thread-5) [custom_components.ble_monitor] HCIdump thread: start main event_loop
2021-05-23 16:39:51 WARNING (MainThread) [homeassistant.components.media_player] Updating samsungtv media_player took longer than the scheduled update interval 0:00:10
2021-05-23 16:40:15 ERROR (Thread-5) [custom_components.ble_monitor.ble_parser.xiaomi] Decryption MiBeacon V2/V3 advertisement failed: No encryption key found
2021-05-23 16:40:15 DEBUG (Thread-5) [custom_components.ble_monitor.ble_parser.xiaomi] Invalid data: Data decryption failed
2021-05-23 16:40:16 ERROR (Thread-5) [custom_components.ble_monitor.ble_parser.xiaomi] Decryption MiBeacon V2/V3 advertisement failed: No encryption key found
2021-05-23 16:40:16 DEBUG (Thread-5) [custom_components.ble_monitor.ble_parser.xiaomi] Invalid data: Data decryption failed
2021-05-23 16:40:16 DEBUG (MainThread) [custom_components.ble_monitor.binary_sensor] Data binary sensor received: {'rssi': -44, 'mac': 'F82441C371CD', 'type': 'YLKG07YL/YLKG08YL', 'packet': 1, 'firmware': 'Xiaomi (MiBeacon)', 'data': True}
2021-05-23 16:40:16 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Data measuring sensor received: {'rssi': -44, 'mac': 'F82441C371CD', 'type': 'YLKG07YL/YLKG08YL', 'packet': 1, 'firmware': 'Xiaomi (MiBeacon)', 'data': True}
2021-05-23 16:40:16 DEBUG (MainThread) [custom_components.ble_monitor.sensor] Sensor device with mac address F8:24:41:C3:71:CD has the following settings. Name: F82441C371CD. Temperature unit: °C. Decimals: 1. Use Median: False. Restore state: False. Reset Timer: 35.
2021-05-23 16:40:16 DEBUG (MainThread) [custom_components.ble_monitor.sensor] async_added_to_hass called for ble dimmer F82441C371CD
2021-05-23 16:40:17 ERROR (Thread-5) [custom_components.ble_monitor.ble_parser.xiaomi] Decryption MiBeacon V2/V3 advertisement failed: No encryption key found
2021-05-23 16:40:17 DEBUG (Thread-5) [custom_components.ble_monitor.ble_parser.xiaomi] Invalid data: Data decryption failed
2021-05-23 16:40:19 ERROR (Thread-5) [custom_components.ble_monitor.ble_parser.xiaomi] Decryption MiBeacon V2/V3 advertisement failed: No encryption key found
2021-05-23 16:40:19 DEBUG (Thread-5) [custom_components.ble_monitor.ble_parser.xiaomi] Invalid data: Data decryption failed

@kirilldobr
Copy link

Btw, i'm running on a RPI B4, Home Assistant OS 5.13

@Ernst79
Copy link

Ernst79 commented May 23, 2021

You have entered the MAC address reversed (per two characters in your config

@kirilldobr
Copy link

kirilldobr commented May 23, 2021

Script told me that mac address is

{'mac': 'cd71c34124f8', 'evtid': 4097, 'pid': 950, 'beaconkey': '346b958d1e040ed240ab84b7'}

I double checked mac address in the config, it's the exact same.
I am not seeing something obvious, aren't I (

@kirilldobr
Copy link

I got it!
The script returns MAC address in the reversed order!
Everything works now.

Thank you for your help and your amazing work, I've been following the YLKg08YL thread closely, it inspired me to create a proper home automation ✨

@Ernst79
Copy link

Ernst79 commented May 23, 2021

I will change the script to return the real MAC. I can understand its confusing.

@rezmus
Copy link

rezmus commented May 23, 2021

@Ernst79 mac is reversed in miio cmd response (method 6 he used). just bold in faq to reverse it during remote/dimmer setup in HA.

afaik mac for dimmer/remote (not from bundle) always starts with F8:24:41.

@Ernst79
Copy link

Ernst79 commented May 23, 2021

Ah, you’re right. I’ll highlight it in the FAQ

@psylity
Copy link

psylity commented Dec 1, 2021

I've made a full python code that pairs and reads Yeelight dimmer data.
For those who want the working code - check it out https://github.com/psylity/yeelight-dimmer-python

@psylity
Copy link

psylity commented Dec 6, 2021

Also I'm glad to present ESP-IDF component that adds Yeelight YLKG08Y support to your ESP32 projects - yeelight-dimmer-esp32

@Busyrev
Copy link

Busyrev commented Jan 6, 2022

@psylity @AlexxIT do you know any way to read battery level for this dimmers?

@onlysunjun
Copy link

kill app, restart bluetooth, use different phone. just paired mine, no issues.

https://ibb.co/M6x38KM https://ibb.co/3mcbttj https://ibb.co/z80S9WV

Dose the xiaomihome app need the old version? the web url is cannot work,https://github.com/custom-components/ble_monitor/issues/353 ,could you share the xiaomihome app? thx

@AlexxIT
Copy link

AlexxIT commented Apr 9, 2022

@Busyrev Does any Xiaomi software show the battery value?

@Busyrev
Copy link

Busyrev commented Apr 10, 2022

AlexxIT

Does any Xiaomi software show the battery value?

No idea, I`m not using xiaomi software at all.

@psylity
Copy link

psylity commented Apr 10, 2022

@psylity @AlexxIT do you know any way to read battery level for this dimmers?

There is no place left in standard ble packet to store the battery level information.

@Artoria2e5
Copy link

Artoria2e5 commented Feb 18, 2024

https://stackoverflow.com/questions/70426330/decrypt-aes-128-ccm-on-nodejs-without-auth-tag reports that the mac_len=4 is actually incorrect: the message contains no MAC at all. I believe this means that the encryption scheme is not regular CCM, but CCM* in ZigBee 802.15.4 Annex B. The 13-byte nonce seems to match.

As a result:

  • The correct mac_len should be 0. Of course, this is not allowed in regular CCM, so the library won't accept it; you have to use a wrong one. (It is a mistake to decrypt without verify in CCM too, but sometimes two wrongs make a right haha)
  • The update with AAD b'\x11' is unnecessary. An update is only needed to make the MAC match. If there is no MAC to check against, no update is needed.
  • Some libraries do not allow you to make the mistake of decrypting without verifying in CCM. For that, the StackOverflow question has an answer: use a different library.

(Also, I don't think you should bother libraries to support a mac_len=0. It's reckless and weird -- it's overall hard to justify.)

The C code with aes_ccm_auth_decrypt works because MBEDTLS allows you to make the same mistake. It gives a return value MBEDTLS_ERR_CCM_AUTH_FAILED, but the decrypted data is already there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests