Seal new backups with Argon2id and ask for longer passwords

This commit is contained in:
waozi committed 2026-11-01 18:50:20 -03:00
1 parent 2f33b69d56
commit d838cbeefb
6 files changed
+267 -28

No files matched your search

+1
View File
@@ -2,6 +2,7 @@
## [Unreleased]
- Seal new backups with Argon2id in a storage-independent format and ask for a password of at least 12 characters. Older backup files keep opening.
- Restore Shovey Island purchases across devices and confirm the current Dew price before unlocking.
- Sign in like any other app: choose your backup file, then enter its password, and try again without finding the file again.
+1 -1
View File
@@ -762,7 +762,7 @@ BackupPage :: (x: float32, y: *float32, width: float32) {
for index: 0..password.count - 1 { password[index] = account_passphrase[index] }
{
value := TextUntilNul(password[:])
if !ManagementPasswordAccepted(value) { notice = Label("entity.password_rule", "Use a password of at least 8 characters") }
if !ManagementPasswordAccepted(value) { notice = Label("entity.password_rule", "Use a password of at least 12 characters") }
else if value != TextUntilNul(account_passphrase_repeat[:]) { notice = Label("entity.password_mismatch", "The passwords do not match") }
else { Manage("backup", value) }
}
+1 -1
View File
@@ -100,7 +100,7 @@ ManagementOperation :: (operation: string) -> s32 {
// New backups always have a password: there is no reset, and a plain file
// would be the account itself. Older files of any kind still open.
MANAGEMENT_PASSWORD_MINIMUM :: 8
MANAGEMENT_PASSWORD_MINIMUM :: 12
ManagementPasswordAccepted :: (password: string) -> bool {
characters: s64 = 0
+1 -1
View File
@@ -209,7 +209,7 @@ ProviderImportHeld :: (provider: *ManagementProvider, passphrase: string) {
ProviderBackup :: (provider: *ManagementProvider, passphrase: string) {
if !ManagementPasswordAccepted(passphrase) {
ProviderFinish(provider, "error", "Use a password of at least 8 characters")
ProviderFinish(provider, "error", "Use a password of at least 12 characters")
return
}
recovery: Recovery
+202 -21
View File
@@ -1,10 +1,118 @@
// Native key recovery uses Daochi's existing plain and passphrase-protected
// formats. The optional, authenticated context keeps app recovery material
// (such as a record vault) with the key without exposing it to the UI bundle.
// Native key recovery. New backups use the sealed format below; older plain
// and PBKDF2 files keep opening. The optional, authenticated context keeps app
// recovery material (such as a record vault) with the key without exposing it
// to the UI bundle.
//
// The sealed format is plain text and does not depend on where it is stored:
//
// waozi-account-backup-v3
// algorithm=ML-DSA-44
// kdf=Argon2id
// memory_kib=65536
// passes=3
// lanes=1
// salt=<16 bytes hex>
// nonce=<12 bytes hex>
// ciphertext=<ChaCha20-Poly1305 output hex>
//
// Everything before "ciphertext=" is authenticated. Argon2id gives 64 bytes:
// the first 32 seal the backup, the last 32 make the login proof a node checks
// before it releases a stored copy, so a node can never open what it holds.
// The work factors are fixed: anything else is refused, so a hostile node or
// file can neither weaken the proof it receives nor exhaust this device.
#import "daochi_client/keys"
#import "daochi_client/crypto"
#import "std/c_string"
#import "std/text_buffer"
#import "std/byte_text_linux"
recovery_c :: #system_library "c";
RecoveryCalloc :: (count: usize, size: usize) -> *u8 #foreign recovery_c "calloc";
RecoveryFree :: (memory: *u8) #foreign recovery_c "free";
RecoveryArgonConfig :: struct {
algorithm: u32
blocks: u32
passes: u32
lanes: u32
}
RecoveryArgonInputs :: struct {
pass: *u8
salt: *u8
pass_size: u32
salt_size: u32
}
RecoveryArgonExtras :: struct {
key: *u8
ad: *u8
key_size: u32
ad_size: u32
}
RecoveryArgon :: (hash: *u8, hash_size: u32, work_area: *u8, config: RecoveryArgonConfig,
inputs: RecoveryArgonInputs, extras: RecoveryArgonExtras) #foreign recovery_c "crypto_argon2";
RECOVERY_SEALED_HEADER :: "waozi-account-backup-v3\n"
RECOVERY_SEALED_PREFIX :: "waozi-account-backup-v3\nalgorithm=ML-DSA-44\nkdf=Argon2id\nmemory_kib=65536\npasses=3\nlanes=1\nsalt="
RECOVERY_SEALED_NONCE :: "\nnonce="
RECOVERY_SEALED_CIPHERTEXT :: "\nciphertext="
RECOVERY_ARGON_ID :: 2
RECOVERY_ARGON_BLOCKS :: 65536
RECOVERY_ARGON_BLOCK_BYTES :: 1024
RECOVERY_ARGON_PASSES :: 3
RECOVERY_ARGON_LANES :: 1
RECOVERY_SALT_BYTES :: 16
RECOVERY_NONCE_BYTES :: 12
RECOVERY_KEY_BYTES :: 32
RECOVERY_PROOF_LABEL :: "waozi-account-backup-login-v3\n"
// What a password gives for one salt. The proof goes to a node once to release
// a stored copy; only the verifier stays there.
RecoveryKeys :: struct {
seal: [RECOVERY_KEY_BYTES]u8
proof: [RECOVERY_KEY_BYTES]u8
verifier: [RECOVERY_KEY_BYTES]u8
}
RecoveryBytes :: (value: string) -> *u8 {
descriptor := cast(*TextDescriptor)(*value)
return descriptor.data
}
RecoveryKeysClear :: (keys: *RecoveryKeys) {
SecureWipe(*keys.seal[0], keys.seal.count)
SecureWipe(*keys.proof[0], keys.proof.count)
SecureWipe(*keys.verifier[0], keys.verifier.count)
}
// Fails only when the password is empty or the memory is not available.
RecoveryDerive :: (passphrase: string, salt: *u8, keys: *RecoveryKeys) -> bool {
if salt == null || keys == null || passphrase.count == 0 || passphrase.count > 511 { return false }
work := RecoveryCalloc(cast(usize)RECOVERY_ARGON_BLOCKS, cast(usize)RECOVERY_ARGON_BLOCK_BYTES)
if work == null { return false }
output: [64]u8
defer { SecureWipe(*output[0], output.count) }
config := RecoveryArgonConfig.{cast(u32)RECOVERY_ARGON_ID, cast(u32)RECOVERY_ARGON_BLOCKS,
cast(u32)RECOVERY_ARGON_PASSES, cast(u32)RECOVERY_ARGON_LANES}
inputs := RecoveryArgonInputs.{RecoveryBytes(passphrase), salt, cast(u32)passphrase.count, cast(u32)RECOVERY_SALT_BYTES}
extras := RecoveryArgonExtras.{null, null, cast(u32)0, cast(u32)0}
RecoveryArgon(*output[0], cast(u32)64, work, config, inputs, extras)
SecureWipe(work, cast(s64)RECOVERY_ARGON_BLOCKS * cast(s64)RECOVERY_ARGON_BLOCK_BYTES)
RecoveryFree(work)
for index: 0..RECOVERY_KEY_BYTES - 1 { keys.seal[index] = output[index] }
label := RECOVERY_PROOF_LABEL
input: [64]u8
defer { SecureWipe(*input[0], input.count) }
for byte, index: label { input[index] = byte }
for index: 0..RECOVERY_KEY_BYTES - 1 { input[label.count + index] = output[RECOVERY_KEY_BYTES + index] }
Sha256(*input[0], label.count + RECOVERY_KEY_BYTES, *keys.proof[0])
Sha256(*keys.proof[0], RECOVERY_KEY_BYTES, *keys.verifier[0])
return true
}
RecoveryDeriveFromHex :: (passphrase: string, salt_hex: string, keys: *RecoveryKeys) -> bool {
salt: [RECOVERY_SALT_BYTES]u8
if salt_hex.count != RECOVERY_SALT_BYTES * 2 || HexToBytes(salt_hex, *salt[0], salt.count) == 0 { return false }
return RecoveryDerive(passphrase, *salt[0], keys)
}
// Android providers identify .key as PGP keys or generic binary data. Recovery
// backups can also be JSON or plain text; keep all of those visible to import.
@@ -69,7 +177,7 @@ RecoveryFileName :: (username: string, public_id: string, output: []u8) -> strin
RecoveryNeedsPassword :: (input: string) -> bool {
start: s64 = 0
if input.count >= 3 && input[0] == cast(u8)239 && input[1] == cast(u8)187 && input[2] == cast(u8)191 { start = 3 }
return AccountStartsWith(input[start:], AccountEncryptedHeader)
return AccountStartsWith(input[start:], AccountEncryptedHeader) || AccountStartsWith(input[start:], RECOVERY_SEALED_HEADER)
}
RecoveryPlain :: (value: *Recovery, output: []u8) -> bool {
@@ -83,35 +191,102 @@ RecoveryPlain :: (value: *Recovery, output: []u8) -> bool {
return true
}
RecoveryEncode :: (value: *Recovery, passphrase: string, output: []u8) -> bool {
if value == null || output.count < ACCOUNT_EXPORT_ENCRYPTED_TEXT_SIZE || passphrase.count > 511 { return false }
// Seals with a fresh salt. keys holds what a node needs to release this copy;
// the caller wipes it.
RecoverySeal :: (value: *Recovery, passphrase: string, output: []u8, keys: *RecoveryKeys) -> bool {
if value == null || keys == null || output.count < ACCOUNT_EXPORT_ENCRYPTED_TEXT_SIZE || passphrase == "" || passphrase.count > 511 { return false }
plaintext: [ACCOUNT_EXPORT_TEXT_SIZE]u8
salt: [AccountEncryptedSaltBytes]u8
nonce: [AccountEncryptedNonceBytes]u8
key: [32]u8
salt: [RECOVERY_SALT_BYTES]u8
nonce: [RECOVERY_NONCE_BYTES]u8
sealed: [ACCOUNT_EXPORT_TEXT_SIZE]u8
defer {
SecureWipe(*plaintext[0], plaintext.count)
SecureWipe(*key[0], key.count)
SecureWipe(*sealed[0], sealed.count)
}
if !RecoveryPlain(value, plaintext[:]) { return false }
length := TextUntilNul(plaintext[:]).count
if passphrase == "" { return CopyCString(TextUntilNul(plaintext[:]), output) }
if length + AccountEncryptedTagBytes >= sealed.count ||
SecureRandom(*salt[0], salt.count) == 0 || SecureRandom(*nonce[0], nonce.count) == 0 ||
!AccountDerivePassphraseKey(passphrase, *salt[0], *key[0], ACCOUNT_PASSPHRASE_ITERATIONS) ||
ChaCha20Poly1305Seal(*key[0], *nonce[0], *plaintext[0], length, null, 0, *sealed[0], sealed.count) == 0 { return false }
if length + AccountEncryptedTagBytes >= sealed.count || SecureRandom(*salt[0], salt.count) == 0 ||
SecureRandom(*nonce[0], nonce.count) == 0 || !RecoveryDerive(passphrase, *salt[0], keys) { return false }
cursor: s64 = 0
return AccountTextAppend(*output[0], output.count, *cursor,
"ksync-account-key-v2\nalgorithm=ML-DSA-44\nkdf=PBKDF2-SHA256\niterations=600000\nsalt=") &&
AccountAppendHex(*output[0], output.count, *cursor, *salt[0], salt.count) &&
AccountTextAppend(*output[0], output.count, *cursor, "\nnonce=") &&
AccountAppendHex(*output[0], output.count, *cursor, *nonce[0], nonce.count) &&
AccountTextAppend(*output[0], output.count, *cursor, "\nciphertext=") &&
if !AccountTextAppend(*output[0], output.count, *cursor, RECOVERY_SEALED_PREFIX) ||
!AccountAppendHex(*output[0], output.count, *cursor, *salt[0], salt.count) ||
!AccountTextAppend(*output[0], output.count, *cursor, RECOVERY_SEALED_NONCE) ||
!AccountAppendHex(*output[0], output.count, *cursor, *nonce[0], nonce.count) { return false }
if ChaCha20Poly1305Seal(*keys.seal[0], *nonce[0], *plaintext[0], length, *output[0], cursor, *sealed[0], sealed.count) == 0 { return false }
return AccountTextAppend(*output[0], output.count, *cursor, RECOVERY_SEALED_CIPHERTEXT) &&
AccountAppendHex(*output[0], output.count, *cursor, *sealed[0], length + AccountEncryptedTagBytes)
}
RecoveryEncode :: (value: *Recovery, passphrase: string, output: []u8) -> bool {
if value == null || output.count < ACCOUNT_EXPORT_ENCRYPTED_TEXT_SIZE || passphrase.count > 511 { return false }
if passphrase == "" {
plaintext: [ACCOUNT_EXPORT_TEXT_SIZE]u8
defer { SecureWipe(*plaintext[0], plaintext.count) }
if !RecoveryPlain(value, plaintext[:]) { return false }
return CopyCString(TextUntilNul(plaintext[:]), output)
}
keys: RecoveryKeys
defer { RecoveryKeysClear(*keys) }
return RecoverySeal(value, passphrase, output, *keys)
}
// The sealed format's fixed-layout fields. Anything else is not this format.
RecoverySealedParts :: struct {
valid: bool
salt: string
nonce: string
ciphertext: string
// Everything before the ciphertext, which it authenticates.
authenticated: string
}
RecoverySealedParse :: (input: string) -> RecoverySealedParts {
parts: RecoverySealedParts
start: s64 = 0
if input.count >= 3 && input[0] == cast(u8)239 && input[1] == cast(u8)187 && input[2] == cast(u8)191 { start = 3 }
end := input.count
while end > start && input[end - 1] <= cast(u8)32 { end -= 1 }
text := input[start:end]
prefix := RECOVERY_SEALED_PREFIX
nonce_label := RECOVERY_SEALED_NONCE
cipher_label := RECOVERY_SEALED_CIPHERTEXT
salt_at := prefix.count
nonce_label_at := salt_at + RECOVERY_SALT_BYTES * 2
nonce_at := nonce_label_at + nonce_label.count
cipher_label_at := nonce_at + RECOVERY_NONCE_BYTES * 2
cipher_at := cipher_label_at + cipher_label.count
if text.count <= cipher_at || !AccountStartsWith(text, prefix) || text[nonce_label_at:nonce_at] != nonce_label ||
text[cipher_label_at:cipher_at] != cipher_label { return parts }
parts.salt = text[salt_at:nonce_label_at]
parts.nonce = text[nonce_at:cipher_label_at]
parts.ciphertext = text[cipher_at:]
parts.authenticated = text[:cipher_label_at]
length := parts.ciphertext.count
parts.valid = length % 2 == 0 && length / 2 > AccountEncryptedTagBytes && length / 2 < ACCOUNT_EXPORT_TEXT_SIZE
return parts
}
// Opens with keys already derived, so a stored copy costs one derivation.
RecoveryOpenSealed :: (input: string, keys: *RecoveryKeys, output: *Recovery) -> bool {
if output == null || keys == null { return false }
output.* = Recovery.{}
parts := RecoverySealedParse(input)
if !parts.valid { return false }
nonce: [RECOVERY_NONCE_BYTES]u8
sealed: [ACCOUNT_EXPORT_TEXT_SIZE]u8
plaintext: [ACCOUNT_EXPORT_TEXT_SIZE]u8
defer {
SecureWipe(*plaintext[0], plaintext.count)
SecureWipe(*sealed[0], sealed.count)
}
sealed_length := parts.ciphertext.count / 2
if HexToBytes(parts.nonce, *nonce[0], nonce.count) == 0 || HexToBytes(parts.ciphertext, *sealed[0], sealed_length) == 0 ||
ChaCha20Poly1305Open(*keys.seal[0], *nonce[0], *sealed[0], sealed_length, RecoveryBytes(parts.authenticated), parts.authenticated.count,
*plaintext[0], plaintext.count) == 0 { return false }
plaintext[sealed_length - AccountEncryptedTagBytes] = cast(u8)0
return RecoveryReadPlain(TextUntilNul(plaintext[:]), output)
}
RecoveryReadPlain :: (text: string, output: *Recovery) -> bool {
candidate: Recovery
defer { RecoveryClear(*candidate) }
@@ -135,6 +310,12 @@ RecoveryDecode :: (input: string, passphrase: string, output: *Recovery) -> bool
start: s64 = 0
if input.count >= 3 && input[0] == cast(u8)239 && input[1] == cast(u8)187 && input[2] == cast(u8)191 { start = 3 }
text := input[start:]
if AccountStartsWith(text, RECOVERY_SEALED_HEADER) {
parts := RecoverySealedParse(text)
keys: RecoveryKeys
defer { RecoveryKeysClear(*keys) }
return parts.valid && RecoveryDeriveFromHex(passphrase, parts.salt, *keys) && RecoveryOpenSealed(text, *keys, output)
}
if !AccountStartsWith(text, AccountEncryptedHeader) { return RecoveryReadPlain(text, output) }
if passphrase == "" { return false }
iterations := AccountParseIterations(AccountEncryptedLineValue(text, "iterations="))
+61 -4
View File
@@ -76,8 +76,8 @@ CheckBackupAndSignIn :: () -> s32 {
if RecoveryFileName("", "", name[:]) != "account.key" { return 107 }
tiny: [6]u8
if RecoveryFileName("username", id, tiny[:]) != "" { return 108 }
if ManagementPasswordAccepted("") || ManagementPasswordAccepted("1234567") || !ManagementPasswordAccepted("12345678") { return 109 }
if ManagementPasswordAccepted("ééééééé") || !ManagementPasswordAccepted("éééééééé") { return 110 }
if ManagementPasswordAccepted("") || ManagementPasswordAccepted("12345678901") || !ManagementPasswordAccepted("123456789012") ||
ManagementPasswordAccepted("ééééééééééé") || !ManagementPasswordAccepted("éééééééééééé") { return 109 }
root: [4096]u8
if !NativeDirectory(NativeRoot(root[:])) || NativeJoin(backup_path[:], TextUntilNul(root[:]), "fixture-backup.key") == "" { return 111 }
@@ -91,7 +91,7 @@ CheckBackupAndSignIn :: () -> s32 {
// A backup never goes out without a password.
unused RemoveFile(TextUntilNul(backup_path[:]))
if !Finished(provider, "backup", "", "error") || !Finished(provider, "backup", "short", "error") ||
if !Finished(provider, "backup", "", "error") || !Finished(provider, "backup", "short", "error") || !Finished(provider, "backup", "elevenchars", "error") ||
PathExists(TextUntilNul(backup_path[:])) { return 114 }
if !Finished(provider, "backup", "fixture password", "ok") { return 115 }
file: [ACCOUNT_EXPORT_ENCRYPTED_TEXT_SIZE]u8
@@ -145,6 +145,58 @@ Request :: (provider: *ManagementProvider, operation: string, value: string = ""
return ProviderEnqueue(provider, encoded)
}
// The sealed backup: its KDF, its stored layout and every way it must refuse.
RecoverySealedCheck :: (fixture: *Recovery, text: string) -> s32 {
if !RecoveryNeedsPassword(text) { return 1 }
prefix := "waozi-account-backup-v3\nalgorithm=ML-DSA-44\nkdf=Argon2id\nmemory_kib=65536\npasses=3\nlanes=1\nsalt="
if text.count <= prefix.count || text[:prefix.count] != prefix { return 2 }
// Independent vector: openssl kdf ARGON2ID, 64 MiB, 3 passes, 1 lane, 64 bytes.
salt: [16]u8
for index: 0..15 { salt[index] = cast(u8)index }
keys: RecoveryKeys
defer { RecoveryKeysClear(*keys) }
if !RecoveryDerive("correct horse battery", *salt[0], *keys) { return 3 }
seal_hex: [65]u8
if BytesToHex(*keys.seal[0], 32, *seal_hex[0], seal_hex.count) == 0 ||
TextUntilNul(seal_hex[:]) != "91b1d756d2e5d66cfc48e89292d2a4f2b64c5a8e84cffe10a1d4cdc7fc7dd5cc" { return 4 }
// The proof and the sealing key are separate; the node keeps only the verifier.
expected: [32]u8
Sha256(*keys.proof[0], 32, *expected[0])
same := true
for index: 0..31 {
if keys.proof[index] != keys.seal[index] { same = false }
if expected[index] != keys.verifier[index] { return 5 }
}
if same { return 6 }
// A stored copy opens with its password's keys, and only with them.
parts := RecoverySealedParse(text)
if !parts.valid { return 7 }
opened: Recovery
defer { RecoveryClear(*opened) }
right: RecoveryKeys
defer { RecoveryKeysClear(*right) }
if !RecoveryDeriveFromHex("fixture passphrase", parts.salt, *right) ||
!RecoveryOpenSealed(text, *right, *opened) ||
TextUntilNul(opened.keys.public_id[:]) != TextUntilNul(fixture.keys.public_id[:]) { return 8 }
if RecoveryOpenSealed(text, *keys, *opened) || opened.keys.private_key_hex[0] != cast(u8)0 { return 9 }
// A trailing newline from storage is harmless; other edits are refused.
copy: [ACCOUNT_EXPORT_ENCRYPTED_TEXT_SIZE + 2]u8
if !CopyCString(text, copy[:]) { return 10 }
copy[text.count] = cast(u8)10
if !RecoveryOpenSealed(TextUntilNul(copy[:]), *right, *opened) { return 11 }
copy[text.count] = cast(u8)0
// A weaker work factor is refused before any key is derived.
weakened := "waozi-account-backup-v3\nalgorithm=ML-DSA-44\nkdf=Argon2id\nmemory_kib=8\npasses=1\nlanes=1\nsalt=00000000000000000000000000000000\nnonce=000000000000000000000000\nciphertext=00000000000000000000000000000000000000"
if RecoverySealedParse(weakened).valid || RecoveryDecode(weakened, "fixture passphrase", *opened) { return 12 }
// Changing an authenticated field breaks the seal, even with the right keys.
copy[prefix.count + 32 + 7] = ifx copy[prefix.count + 32 + 7] == cast(u8)48 then cast(u8)49 else cast(u8)48
if RecoveryOpenSealed(TextUntilNul(copy[:]), *right, *opened) { return 13 }
return 0
}
#program_export
Check :: () -> s32 {
if CreateAccountKeys(*fixture.keys) == 0 { return 1 }
@@ -162,7 +214,12 @@ Check :: () -> s32 {
if RecoveryDecode(TextUntilNul(backup[:]), "wrong", *opened) { return 6 }
if !RecoveryDecode(TextUntilNul(backup[:]), "fixture passphrase", *opened) ||
TextUntilNul(opened.context[:]) != TextUntilNul(fixture.context[:]) { return 7 }
if ParseEncryptedAccountKeys(TextUntilNul(backup[:]), "fixture passphrase", *legacy) == 0 { return 8 }
// Backups made before the sealed format still open.
older: [ACCOUNT_EXPORT_ENCRYPTED_TEXT_SIZE]u8
if AccountKeysEncryptedText(*fixture.keys, "fixture passphrase", *older[0], older.count) == 0 ||
!RecoveryNeedsPassword(TextUntilNul(older[:])) || !RecoveryDecode(TextUntilNul(older[:]), "fixture passphrase", *opened) ||
TextUntilNul(opened.keys.public_id[:]) != TextUntilNul(fixture.keys.public_id[:]) { return 8 }
if RecoverySealedCheck(*fixture, TextUntilNul(backup[:])) != 0 { return 300 + RecoverySealedCheck(*fixture, TextUntilNul(backup[:])) }
length := TextUntilNul(backup[:]).count
backup[length - 2] = ifx backup[length - 2] == cast(u8)48 then cast(u8)49 else cast(u8)48
if RecoveryDecode(TextUntilNul(backup[:]), "fixture passphrase", *opened) { return 9 }