Skip to content

Conversation

benma
Copy link
Contributor

@benma benma commented Oct 24, 2022

If a USB packet starts with a zero byte, that byte will be stripped by the C USB library (i.e. signal11 on Linux), leading to a broken package.

We only need to add it if not on Windows, as karalabel/usb prepends this zero byte already for Windows (I think this is a bug in karalabe/usb and karalabe/hid, which didn't consider that a packet could start with a zero byte right after the report ID).

Currently we don't observe a bug because the first byte in each packet is the cid (channel identifier), which is harddcoded to 0xff000000. If we start using a random CID, and it starts with a zero byte, the packet will be corrupted and the BitBox02 will not be able to parse it.

The same fix was also added to the bitbox01 in the BitBoxApp (https://github.com/digitalbitbox/bitbox-wallet-app/blob/bd70f6c400268355cee94891397447796f02fbde/backend/devices/bitbox/communication.go#L235-L241), where it was crucial because packets in the bitbox01 are padded with zero bytes to the left.

@benma
Copy link
Contributor Author

benma commented Oct 24, 2022

Issue opened upstream: karalabe/usb#30

@benma benma removed the request for review from Beerosagos October 24, 2022 14:40
@benma benma marked this pull request as draft October 24, 2022 14:41
@benma benma requested a review from Beerosagos October 24, 2022 14:47
@benma benma marked this pull request as ready for review October 24, 2022 14:47
if runtime.GOOS != "windows" {
// packets have a 0 byte report ID in front. The karalabe usb library adds it
// automatically for windows, and not for unix, as there, it is stripped by the signal11
// hid library. We have to add it (to be stripped by signal11), otherwise a a zero that
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small typo otherwise a a zero

@Beerosagos
Copy link
Collaborator

tACK on Linux! 👍

benma added 2 commits October 26, 2022 11:30
If a USB packet starts with a zero byte, that byte will be stripped by
the C USB library (i.e. signal11 on Linux), leading to a broken
package.

We only need to add it if not on Windows, as karalabel/usb prepends
this zero byte already for Windows (I think this is a bug in
karalabe/usb and karalabe/hid, which didn't consider that a packet
could start with a zero byte right after the report ID).

Currently we don't observe a bug because the first byte in each packet
is the `cid` (channel identifier), which is harddcoded to
`0xff000000`. If we start using a random CID, and it starts with a
zero byte, the packet will be corrupted and the BitBox02 will not be
able to parse it.

The same fix was also added to the bitbox01 in the
BitBoxApp (https://github.com/digitalbitbox/bitbox-wallet-app/blob/bd70f6c400268355cee94891397447796f02fbde/backend/devices/bitbox/communication.go#L235-L241),
where it was crucial because packets in the bitbox01 are padded with
zero bytes to the left.
The CID in the response is compared against the hardcoded 0xff000000
CID. It should however parse and compare it to the const `cid`. This
allows us to change the CID (i.e. make it random) and not break parsing.
@benma
Copy link
Contributor Author

benma commented Oct 26, 2022

After more consideration, I think this is the wrong location to add the fix. This library doesn't even depend on karalabe/usb, but this fix is a workaround to a bug in karalabe/usb. The fix should be in the the device io.ReadWriteCloser itself.

If the fix was merged here, I assume bitbox02-api-js via WebHID would break: https://github.com/digitalbitbox/bitbox02-api-js/blob/8205a813a58fa3effb86be6ce5c99a3c0561e551/gowrapper/main.go#L183

@benma
Copy link
Contributor Author

benma commented Oct 26, 2022

Split the first commit into a new PR, as that one is still useful: #75

@benma
Copy link
Contributor Author

benma commented Oct 26, 2022

Closing in favor of BitBoxSwiss/bitbox-wallet-app#1809

@benma benma closed this Oct 26, 2022
@benma benma deleted the prepend-zero branch October 26, 2022 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants