scooterhacking / NinebotCrypto

An implementation of the NinebotCrypto protocol by majsi.
GNU Affero General Public License v3.0
41 stars 15 forks source link

Crypto iterator bug results in communication problems/slowness #13

Open nopbxlr opened 8 months ago

nopbxlr commented 8 months ago

There is a bug in the initial C# implementation that causes the counter to be over-incremented after the first message. After the 0x5B initial crypto message, the counter goes from 0 to 2. This wasn't disturbing the communication on earlier devices/firmware versions but on newer scooters like the G2, it causes you to encrypt and send every message twice to keep up and thus slows communication down.

This bug was jointly discovered by @ArchGryphon9362 and myself today while I was working on G2 support for the Utility app and while he was writing his improved Swift version as well.

It has been fixed in his version of the library (BetterNinebotCrypto.swift) and I also issued a fix for the Kotlin version. If you are using either of those two, then we'd recommend using the latest version. If you are using other versions, you might encounter this bug and its side effects. Please, take the time to send a pull request if you do fix it.

I will update this issue as other versions get updated.

nopbxlr commented 8 months ago

Update: the bug doesn't seem to be from the C# implementation. It does manipulate the iterator in a correct manner and increments upon decryption. This (for now) seems like it was only happening for the Kotlin version where the conditions to increase the counter on decryption were never met.