Skip to content

Protect locally stored Acount Code on disk with system api - #3508

Open
Mabeeck wants to merge 31 commits into
PixelGuys:masterfrom
Mabeeck:windows-CryptProtectData
Open

Protect locally stored Acount Code on disk with system api#3508
Mabeeck wants to merge 31 commits into
PixelGuys:masterfrom
Mabeeck:windows-CryptProtectData

Conversation

@Mabeeck

@Mabeeck Mabeeck commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

This PR adds the main.protect.protect and main.protect.unprotect functions and integrates them into the PasswordEncodedAccountCode class.

protect Takes an allocator and a slice of bytes as arguments. The function returns a different slice of bytes that has been allocated with the provided allocator and can be passed to unprotect to get back the original slice of bytes. The function can fail, if the platform Cubyz is currently running on does not have an implementation yet (in which case the error will be error.Unsupported) or the syscall fails for some undisclosed reason (Windows), in which case the error will be error.syserr.
unprotect Takes an allocator and a slice of bytes that has been previously generated by protect as arguments. The function returns a different slice of bytes that has been allocated with the provided allocator and is equivalent in value to the slice of bytes that was passed to protect in order to produce the provided slice. The function can return error.Invalid if the provided input was protected on a different device; can no longer be unprotected for some reason; the current platform does not have an implementation. If something unexpected happened the function will fail with error.syserr.
canProtect Takes no arguments and returns a boolean indicating weather the protection functions have an implementation on the current platform.

Currently the protection functions only support Windows. They can later be easily expanded to support Linux as well.

Adds a protected attribute to PasswordEncodedAccountCode that indicates weather a call to unprotect is needed, before the AccountCode can be decrypted.
Functions to initialize PasswordEncodedAccountCode now take a shouldProtect boolean argument that when set to true will protect the function with the native system api, if available. Setting it to false will prevent usage of the protection api.

Contributes to #2551

@Mabeeck Mabeeck changed the title Add a protect() function Protect locally stored Acount Code on disk with system api Aug 12, 2026
@Mabeeck
Mabeeck force-pushed the windows-CryptProtectData branch 2 times, most recently from 60f2598 to 92890da Compare August 12, 2026 19:19
@Mabeeck
Mabeeck marked this pull request as ready for review August 12, 2026 19:56
@Wunka Wunka moved this to High Priority in PRs to review Aug 14, 2026
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/network/authentication.zig Outdated
Comment thread src/gui/windows/authentication/encrypt_with_password.zig Outdated
Comment thread src/protect.zig Outdated
@IntegratedQuantum IntegratedQuantum moved this from High Priority to In review in PRs to review Aug 16, 2026
@Mabeeck

Mabeeck commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

Done

Comment thread src/network/authentication.zig Outdated
Comment thread src/network/authentication.zig Outdated
Comment thread src/network/authentication.zig Outdated
@Mabeeck
Mabeeck force-pushed the windows-CryptProtectData branch from 026d0ee to 419ec15 Compare August 23, 2026 12:28
@Mabeeck

Mabeeck commented Aug 23, 2026

Copy link
Copy Markdown
Contributor Author

Done

Comment thread src/gui/windows/authentication/encrypt_with_password.zig Outdated
Comment thread src/network/authentication.zig Outdated
Comment thread src/network/authentication.zig Outdated
Comment thread src/network/authentication.zig Outdated
Comment thread src/network/protection.zig
@Mabeeck

Mabeeck commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

Done

@IntegratedQuantum IntegratedQuantum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please also include a screenshot (since I cannot test this locally), also please adjust the window height to avoid a scrollbar.

Comment thread src/network/authentication.zig Outdated
Comment thread src/network/protection.zig Outdated
list.add(Label.init(.{0, 0}, width, "Your Account Code will be stored in your settings to allow you to stay logged in. Please decide how we should store it:", .left));
innerList = VerticalList.init(.{0, 0}, 100, 16);
if (main.network.authentication.protection.canProtect) {
protectCheckbox = CheckBox.init(.{0, 0}, width, "Protect from theft (recommended)\nForces re-authentication when device changes", protectAccountCode, &protectAccountCodeCallback);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is too generic. Protection from theft can mean a lot of things.

I'd suggest to find a non-dev on the discord server to work together on this. Or maybe @ikabod-kee could help here.

@Mabeeck Mabeeck Aug 30, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

image

Does this work? Or maybe "Prevent loading on different devices"? I have also asked on discord. Will change once I have further improvement ideas.

Mabeeck added 11 commits August 30, 2026 15:12
Due to line 411 in authentication.zig and some other reasons I have decided to rework this implementation to make use of a `protected` boolean attribute instead of creating tons of extra EncodingTypes.
Obey linter and migrate to protected attribute.
protect() and unprotect() now error on unsupported platforms. I felt that it would be bad, if someone assumed that a call to unprotect would always error on bad input or that a call to protect would always encrypt the data.
These previously false assumptions are now true.
Yes, I blame microslop for using an uppercase in their docs.
Apparently errorcode 13 is for strings too short and errorcode 87 is for otherwise gibberish.

The real reason I made this commit is that some github service was down when my last test ran and the only way to rerun it is to make another commit.
@Mabeeck
Mabeeck force-pushed the windows-CryptProtectData branch from 9646ce5 to 728c5a7 Compare August 30, 2026 14:41
As I see it, this is the simplest way to have the defer directly below the recource creation. That makes one more allocation than necessary when shouldProtect is false, but if it improves readability, then that should be fine, since this is not performance critical code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

4 participants