Kleidos API Search

In development. It changes without notice.

Topic

Change of Owner & Resets

What a factory reset, a vault wipe and a settings reset erase, one sequence for every way of asking for each.

A factory reset leaves nothing of the previous owner readable on the flash. The tenth wrong secret is one, and so are the reset in settings, the portal's, and the one a board with an unreadable vault offers. It has two halves with a restart between them:

  1. a mark says that a wipe is pending; the vault's partition is erased and read back blank; the bonds, the host names and the access point's identity are erased; the failure count is reset once the vault is gone; and a tag on the blank partition says that the second half is owed;
  2. first thing in the next boot, before anything opens NVS, the whole NVS partition is erased and read back, a fresh one is made with one entry in its log that says a wipe happened, and the tag is cleared.

A cut in power at any point finishes the wipe at the next boot, before anything mounts. What holds nothing of the owner's stays: the eFuses, the firmware and which copy of it runs.

A vault wipe empties the vault under a new salt and a new vault key and keeps the secret, which must be proven in the same call, with the bonds, the access point and the preferences. A wrong secret counts as a failure.

A settings reset erases the preferences and nothing of the vault.

See also
Security, Vault Storage, Application States

Files

file src/reset/change_of_owner.h
The destructive effects every wipe and reset surface shares.
file src/reset/change_of_owner_sequence.h
The first half of a factory reset, as one sequence over platform steps.
file src/reset/factory_reset_finish.h
The second half of a factory reset, as one sequence over platform steps.
file src/reset/settings_reset.h
The Reset-Settings preference scope (kPreferenceKeys) and the NVS erase helpers.
file src/reset/vault_only_wipe.h
The vault-only wipe: empty the vault, keep the owner.

Classes

struct kleidos::reset::VaultEraseResult
What the vault erase reported, as the sequence weighs it. More about kleidos::reset::VaultEraseResult
struct kleidos::reset::ChangeOfOwnerSteps
The first half's steps, as a platform performs them. More about kleidos::reset::ChangeOfOwnerSteps
struct kleidos::reset::ChangeOfOwnerErased
What the first half took. More about kleidos::reset::ChangeOfOwnerErased
struct kleidos::reset::TagRead
The tag as the second half reads it. More about kleidos::reset::TagRead
struct kleidos::reset::FactoryResetFinishSteps
The second half's steps, as a platform performs them. More about kleidos::reset::FactoryResetFinishSteps
struct kleidos::reset::OwnerLinkSteps
The three erases, as a platform performs them. More about kleidos::reset::OwnerLinkSteps
struct kleidos::reset::OwnerLinksErased
What the erase outside the vault left behind. More about kleidos::reset::OwnerLinksErased
struct kleidos::reset::PrefKey
A single persisted preference: its NVS namespace and key. More about kleidos::reset::PrefKey
struct kleidos::reset::VaultOnlyResult
The verdict and, for a wrong secret, the lockout it started. More about kleidos::reset::VaultOnlyResult

Enumerations

enum class kleidos::reset::WipeFinish : uint8_t { kleidos::reset::WipeFinish::Nothing, kleidos::reset::WipeFinish::FinishedFull, kleidos::reset::WipeFinish::FinishedVaultOnly, kleidos::reset::WipeFinish::StorageFault, kleidos::reset::WipeFinish::Unfinished, kleidos::reset::WipeFinish::Unreadable }
What finishInterruptedWipe did at boot. More about kleidos::reset::WipeFinish
enum class kleidos::reset::TagFound : uint8_t { kleidos::reset::TagFound::Absent , kleidos::reset::TagFound::Present , kleidos::reset::TagFound::Unreadable }
What reading the tag found. More about kleidos::reset::TagFound
enum class kleidos::reset::FactoryResetFinish : uint8_t { kleidos::reset::FactoryResetFinish::NotPending, kleidos::reset::FactoryResetFinish::Finished, kleidos::reset::FactoryResetFinish::Unreadable, kleidos::reset::FactoryResetFinish::NvsFault, kleidos::reset::FactoryResetFinish::CarryFault, kleidos::reset::FactoryResetFinish::TagStuck }
How the second half ended. More about kleidos::reset::FactoryResetFinish
enum class kleidos::reset::VaultOnlyVerdict : uint8_t { kleidos::reset::VaultOnlyVerdict::Emptied, kleidos::reset::VaultOnlyVerdict::ErasedSecretLost, kleidos::reset::VaultOnlyVerdict::Incomplete, kleidos::reset::VaultOnlyVerdict::WrongSecret, kleidos::reset::VaultOnlyVerdict::WipeDue, kleidos::reset::VaultOnlyVerdict::LockedOut, kleidos::reset::VaultOnlyVerdict::KeyfileMissing, kleidos::reset::VaultOnlyVerdict::Unreadable }
What the vault-only wipe did, and what a surface tells the owner. More about kleidos::reset::VaultOnlyVerdict

Functions

boolkleidos::reset::eraseOwnerLinks ()
Erase what ties the device to its owner outside the vault.
boolkleidos::reset::eraseForChangeOfOwner (void(*onProgress)(void *progressUser)=nullptr, void *progressUser=nullptr)
Erase everything a change of owner must not hand over: a factory reset.
boolkleidos::reset::performChangeOfOwnerWipe (void(*onProgress)(void *progressUser)=nullptr, void *progressUser=nullptr)
Record the wipe in the audit log, then eraseForChangeOfOwner.
boolkleidos::reset::performSettingsReset ()
Restore every preference to its default. The vault is untouched.
FactoryResetFinishkleidos::reset::finishFactoryReset ()
Finish a factory reset: the second half, first thing in setup().
boolkleidos::reset::timeFactoryResetEntry ()
Give the factory reset's VAULT_WIPE entry its time, once the HAL is up.
WipeFinishkleidos::reset::finishInterruptedWipe ()
Finish a wipe a power cut or a refused erase left pending.
boolkleidos::reset::acknowledgeResumedWipe ()
Clear the marker of a vault-only wipe the boot finished, once the owner has been told.
boolkleidos::reset::askMarkerArm (bool(*arm)())
Ask arm for the wipe-pending marker up to kMarkerArmAsks times.
template<typename EraseVault>
ChangeOfOwnerErasedkleidos::reset::runChangeOfOwner (const ChangeOfOwnerSteps &steps, EraseVault &&eraseVault)
Run the first half of a factory reset: arm the marker, erase the vault and prove it blank, erase the owner links, reset the counter, erase the core dump and write the tag.
FactoryResetFinishkleidos::reset::runFactoryResetFinish (const FactoryResetFinishSteps &steps)
Run the second half of a factory reset: erase NVS, bring it up fresh, write the audit entry, forget the crash ring, and clear the tag under the wipe-pending marker.
constexpr OwnerLinksErasedkleidos::reset::eraseOwnerLinks (const OwnerLinkSteps &steps)
Erase the bonds, the access-point identity and the pending-admin flag.
constexpr boolkleidos::reset::isPreferenceKey (const char *ns, const char *key)
Whether ns / key is in the Reset-Settings preference scope.
boolkleidos::reset::resetPreferencesToDefaults ()
Erase every preference key so the next boot reloads defaults.
boolkleidos::reset::clearOnboardingState ()
Clear the first-run onboarding state.
VaultOnlyResultkleidos::reset::emptyVaultKeepingSecret (const kleidos::vault::DerivationInput &input, void(*onProgress)(void *progressUser)=nullptr, void *progressUser=nullptr)
Empty the vault under the secret in input, keeping that secret, the BLE bonds, the access point and the preferences.
constexpr VaultOnlyResultkleidos::reset::wrongSecretVerdict (const BruteForceGuard::FailureCount &preFail)
The verdict for the counter's answer to a wrong secret.

Variables

constexpr intkleidos::reset::kMarkerArmAsks
constexpr PrefKeykleidos::reset::kPreferenceKeys []
Every device-preference NVS entry that Reset Settings restores.
constexpr std::size_tkleidos::reset::kPreferenceKeyCount
Number of preference entries in kPreferenceKeys.

Back to the top

Enumeration Type Documentation

FactoryResetFinish #

enum class kleidos::reset::FactoryResetFinish : uint8_t

How the second half ended.

Enumerator
NotPending 

No tag: an ordinary boot.

Finished 

NVS is blank and fresh, the carry-overs written, the tag gone.

Unreadable 

The tag could not be read; nothing was touched.

NvsFault 

NVS would not erase, read back blank or come up; the tag stays.

CarryFault 

A carry-over or the marker was not written; the tag stays, the next boot repeats.

TagStuck 

Everything else took, but the tag's sector would not erase.

TagFound #

enum class kleidos::reset::TagFound : uint8_t

What reading the tag found.

Enumerator
Absent 

No factory reset is pending.

Present 

One is: NVS still has to go.

Unreadable 

The read failed; nothing is erased on a guess.

VaultOnlyVerdict #

enum class kleidos::reset::VaultOnlyVerdict : uint8_t

What the vault-only wipe did, and what a surface tells the owner.

Enumerator
Emptied 

The vault is empty under the same secret.

ErasedSecretLost 

The vault is gone and blank; a new secret must be set.

Incomplete 

The partition did not erase; the next boot finishes it.

WrongSecret 

A wrong secret, counted; lockoutSeconds says what it cost.

WipeDue 

A wrong secret that reached the wipe threshold: the FULL wipe is due.

LockedOut 

A lockout is in force: nothing was derived.

KeyfileMissing 

The vault's key file could not be read: not counted.

Unreadable 

Nothing was weighed (record, layout, backend): not counted.

WipeFinish #

enum class kleidos::reset::WipeFinish : uint8_t

What finishInterruptedWipe did at boot.

Enumerator
Nothing 

No wipe was pending.

FinishedFull 

A pending FULL wipe's first half ran again: the partition is blank and the tag down; a restart finishes it.

FinishedVaultOnly 

A pending vault-only wipe was finished: the partition was erased and read back blank; the secret it meant to keep is gone with it. The marker stays armed until acknowledgeResumedWipe.

StorageFault 

The partition would not erase, or did not read back blank: no vault is created over it.

Unfinished 

The partition is blank and the tag down, but NVS would not erase at the top of this boot: no vault is created, and nothing here starts another wipe.

Unreadable 

The marker or the tag could not be read: nothing was erased on a guess, and no vault is created until a read answers.

Back to the top

Function Documentation

acknowledgeResumedWipe() #

bool kleidos::reset::acknowledgeResumedWipe()

Clear the marker of a vault-only wipe the boot finished, once the owner has been told.

finishInterruptedWipe leaves it armed after a FinishedVaultOnly, so that a power cut before the boot has said the secret went has the next boot erase the (blank) partition again and say it then. BOOT_CHECK calls this once the alert has been dismissed or has timed out, before anything can provision.

Return values
ValueMeaning
trueThe marker reads None.
falseIt could not be written: no provision runs this boot, and the next boot finishes and says it again.

askMarkerArm() #

bool kleidos::reset::askMarkerArm(bool(* arm )())
inlinenodiscard

Ask arm for the wipe-pending marker up to kMarkerArmAsks times.

Parameters
NameDirectionMeaning
arminThe platform's arm step (not null).
Return values
ValueMeaning
trueOne ask armed the marker.
falseEvery ask was refused.

clearOnboardingState() #

bool kleidos::reset::clearOnboardingState()
nodiscard

Clear the first-run onboarding state.

Erases the onboarding namespace so the next boot runs first-time setup. Only the debug console calls it, which leaves a device as a lost flag would: its vault, bonds and access point still stored. A factory reset takes the namespace with the rest of NVS instead (reset/change_of_owner.h).

Return values
ValueMeaning
trueThe onboarding namespace is empty.
falseIt would not open or would not clear; the next boot skips first-run setup on a device reported as factory-reset.

emptyVaultKeepingSecret() #

VaultOnlyResult kleidos::reset::emptyVaultKeepingSecret(const kleidos::vault::DerivationInput & input, void(* onProgress )(void *progressUser) = nullptr, void * progressUser = nullptr)
nodiscard

Empty the vault under the secret in input, keeping that secret, the BLE bonds, the access point and the preferences.

The caller holds the vault mounted (VaultStore::begin) for the input's assembly; this ends the mount.

Parameters
NameDirectionMeaning
inputinThe typed secret, assembled against the stored vault.
onProgressinProgress hook for the erase's logical fallback, or null.
progressUserinCookie for onProgress.
Returns
The verdict.

eraseForChangeOfOwner() #

bool kleidos::reset::eraseForChangeOfOwner(void(* onProgress )(void *progressUser) = nullptr, void * progressUser = nullptr)
nodiscard

Erase everything a change of owner must not hand over: a factory reset.

Every change-of-owner wipe is a factory reset, so nothing of the previous owner's stays recoverable from the device's flash. It runs in two halves with a restart between them, and the caller owes that restart: this is the first half, and finishFactoryReset, at the top of the next boot, is the second.

This half, in order (runChangeOfOwner):

  1. the wipe-pending marker is armed (kleidos::vault::wipe_marker);
  2. the whole vault partition is erased raw and walked back blank, formatting nothing (the unlock secret goes with it);
  3. the owner links go (eraseOwnerLinks(), which takes the BLE radio down and empties the in-RAM bond list);
  4. the brute-force counter is reset once the vault is gone;
  5. the core dump partition of a debug build's partition table is erased;
  6. the factory-reset tag (kleidos::vault::factory_reset_tag) is written over the blank partition.

The second half erases the whole NVS partition at the sector level (every preference, the language, the audit history, the bond keys, the host names, the access-point password, the classic ESP32's pepper, the marker) and the boot lands in first-run setup on the compile-time defaults. A cut or a refused step before the tag leaves the marker armed, so the next boot runs this half again before anything mounts (finishInterruptedWipe) and no vault is provisioned meanwhile.

Whichever task calls it, boot recovery included, every step but the owner links (the marker, the vault erase, the blank walk, the counter, the core dump erase, the tag) runs on the vault worker (Core 0), and the caller blocks feeding its own task watchdog meanwhile; a step whose dispatch could not be set up counts as not done. The owner links run on the caller. A full worker queue, or a worker that never started or has stopped, runs a step inline on the caller instead.

Writes no audit entry: use it only where the caller has already recorded the decision, as the unlock screen does when it arms its countdown. Every other surface calls performChangeOfOwnerWipe.

Note
De-bonding needs the BLE radio off, which the bond erase guarantees itself: it stops the radio before touching the store, so the call is safe whether or not a pairing window or a typing session left it up.

A failed raw erase falls back to removing every record-log segment, credential and one-time-code file and index segment one by one, which takes minutes on a full vault. A caller whose own clock expires over that (the admin portal tears its access point down after an unstamped idle window) passes onProgress and is called once per file.

Parameters
NameDirectionMeaning
onProgressinProgress hook, or null when the caller needs no heartbeat (the on-device flows hold no session).
progressUserinCookie handed to onProgress.
Return values
ValueMeaning
trueThe vault partition reads blank and the tag is down: the restart the caller owes finishes the reset.
falseThe partition would not erase or read back blank, or the tag did not write. The marker stays armed; the next boot tries again. Do not tell the owner the device was reset.

eraseOwnerLinks() [1/2] #

bool kleidos::reset::eraseOwnerLinks()
nodiscard

Erase what ties the device to its owner outside the vault.

Every BLE bond, every admin access-point identity (kleidos::net::apidentity::forgetAll) and the RTC pending-admin flag, through eraseOwnerLinks(const OwnerLinkSteps&), each attempted whatever the others report. It is the part of eraseForChangeOfOwner that does not touch the vault, and that function runs it through here, so the two cannot drift apart.

Every provision in a production build runs it on its own before it writes the vault (only a debug build's console provisions without it): the unlock screen over an absent vault on a device whose first-run setup was done (what a wipe leaves, and a partition erased from outside the firmware), and first-run setup when the repeated secret matches (it runs whenever the onboarding flag reads unset, so it cannot assume a new device). The new vault must not inherit the hosts and the access point of the one before it. On a new device, and after a wipe, it finds nothing left and changes nothing.

Note
De-bonding needs the BLE radio off, which the bond erase guarantees itself: it stops the radio before touching the store.
Return values
ValueMeaning
trueNo bond and no admin-AP identity remain.
falseA bond or an access-point identity survived. Do not provision a vault over it.

eraseOwnerLinks() [2/2] #

OwnerLinksErased kleidos::reset::eraseOwnerLinks(const OwnerLinkSteps & steps)
nodiscardconstexpr

Erase the bonds, the access-point identity and the pending-admin flag.

Every step runs whatever the others report, so one that refuses never leaves the next one standing. A step a platform does not provide (a null pointer) counts as not done, so a missing hook fails closed.

Parameters
NameDirectionMeaning
stepsinThe platform's erases.
Returns
What was erased.

finishFactoryReset() #

FactoryResetFinish kleidos::reset::finishFactoryReset()
nodiscard

Finish a factory reset: the second half, first thing in setup().

Runs runFactoryResetFinish over the device: when the vault partition carries the factory-reset tag, the whole NVS partition is erased at the sector level and read back blank, a fresh NVS comes up with one VAULT_WIPE audit entry, the crash ring RTC memory holds is forgotten, and the tag's sector is erased under the wipe-pending marker, which a cut inside that erase leaves for finishInterruptedWipe. Every other boot reads 32 bytes and returns.

It must run before anything opens NVS (the crash record's mirror is the first reader): with nothing open and no radio up, no task can hold a handle the erase would pull from under it. A test asserts the order in setup().

Returns
How it ended; NotPending on every ordinary boot.

finishInterruptedWipe() #

WipeFinish kleidos::reset::finishInterruptedWipe()
nodiscard

Finish a wipe a power cut or a refused erase left pending.

BOOT_CHECK calls it before anything mounts the vault, in this order: a factory-reset tag still present (the second half failed at the top of this boot) is Unfinished; an unreadable marker or tag is Unreadable; a pending FULL wipe runs eraseForChangeOfOwner again, and the caller restarts so the second half runs; a pending vault-only wipe erases the partition again, since the secret it meant to keep has nothing left to be proven against. Without this, a partly erased partition could mount from its second superblock, or a vault be minted over un-erased blocks, and the old records would open with the old secret. A marker it cannot read erases nothing: a moment's NVS fault must not cost an owner a vault, and the provision gate keeps refusing until a read answers.

Returns
What it found and did.

isPreferenceKey() #

bool kleidos::reset::isPreferenceKey(const char * ns, const char * key)
constexpr

Whether ns / key is in the Reset-Settings preference scope.

Pure predicate over kPreferenceKeys (host-testable). Both strings are compared by value.

Parameters
NameDirectionMeaning
nsinNVS namespace to test.
keyinNVS key to test.
Return values
ValueMeaning
trueif the entry is a device preference Reset Settings restores.

performChangeOfOwnerWipe() #

bool kleidos::reset::performChangeOfOwnerWipe(void(* onProgress )(void *progressUser) = nullptr, void * progressUser = nullptr)
nodiscard

Record the wipe in the audit log, then eraseForChangeOfOwner.

The audit entry is written BEFORE acting, so a vault destroyed this way leaves a trail even when the erase does not complete (a completed reset takes it with the rest of NVS and writes one fresh VAULT_WIPE after). This is the factory reset every surface but the unlock screen runs: the admin portal's two brute-force branches and its factory reset, boot recovery, Settings' factory reset and a debug build's serial console, so none of them can drift into skipping a step. The settings menu's and the portal's "erase the vault" are the vault-only wipe instead (emptyVaultKeepingSecret), which keeps the owner's secret, links and preferences. The caller restarts afterwards, whatever it reports.

Parameters
NameDirectionMeaning
onProgressinHanded to eraseForChangeOfOwner.
progressUserinCookie handed to onProgress.
Returns
What eraseForChangeOfOwner reports.

performSettingsReset() #

bool kleidos::reset::performSettingsReset()
nodiscard

Restore every preference to its default. The vault is untouched.

Non-destructive to credentials by construction: kPreferenceKeys excludes onboarding, the brute-force counter and the stored secret length.

Return values
ValueMeaning
trueEvery preference key was erased.
falseAt least one survived; those settings come back at the next boot, so the surface must not report a reset.

resetPreferencesToDefaults() #

bool kleidos::reset::resetPreferencesToDefaults()
nodiscard

Erase every preference key so the next boot reloads defaults.

Restores device preferences to their defaults by deleting each kPreferenceKeys entry from NVS (the loaders re-seed the default when the key is absent). Leaves the vault, BLE bonds, the onboarding flag, and the brute-force counter untouched — Reset Settings keeps the vault fully usable. The caller restarts afterwards so every subsystem re-reads its default.

Return values
ValueMeaning
trueEvery preference key is gone from NVS.
falseAt least one namespace would not open or one key would not erase; the settings the owner was told were reset are still stored and come back at the next boot.

runChangeOfOwner() #

template<typename EraseVault>
ChangeOfOwnerErased kleidos::reset::runChangeOfOwner(const ChangeOfOwnerSteps & steps, EraseVault && eraseVault)
nodiscard

Run the first half of a factory reset: arm the marker, erase the vault and prove it blank, erase the owner links, reset the counter, erase the core dump and write the tag.

A step a platform does not provide (a null pointer) counts as not done, so a missing hook fails closed: no blank proof, no tag, and the marker stays armed. The vault erase is a callable of its own, so a platform hands it the progress hook it was given in the call that names it.

Template Parameters
NameMeaning
EraseVaultA callable returning VaultEraseResult.
Parameters
NameDirectionMeaning
stepsinThe platform's other steps.
eraseVaultinThe vault erase.
Returns
What was erased.

runFactoryResetFinish() #

FactoryResetFinish kleidos::reset::runFactoryResetFinish(const FactoryResetFinishSteps & steps)
inlinenodiscard

Run the second half of a factory reset: erase NVS, bring it up fresh, write the audit entry, forget the crash ring, and clear the tag under the wipe-pending marker.

A step a platform does not provide counts as failed, so a missing hook leaves the tag for the next boot.

Parameters
NameDirectionMeaning
stepsinThe platform's steps.
Returns
How it ended.

timeFactoryResetEntry() #

bool kleidos::reset::timeFactoryResetEntry()

Give the factory reset's VAULT_WIPE entry its time, once the HAL is up.

finishFactoryReset runs before hal::begin(), when a battery-backed clock's bus does not exist yet, so it writes its audit entry with no time (AuditTime::Later) instead of reading the clock there. Called after hal::begin() in the boot whose finishFactoryReset returned FactoryResetFinish::Finished, this stamps that entry with the clock's time; without a trusted clock it keeps its explicit none.

Return values
ValueMeaning
trueThe entry carries the clock's time.
falseIt keeps no time (no trusted clock, or NVS refused).

wrongSecretVerdict() #

VaultOnlyResult kleidos::reset::wrongSecretVerdict(const BruteForceGuard::FailureCount & preFail)
nodiscardconstexpr

The verdict for the counter's answer to a wrong secret.

Parameters
NameDirectionMeaning
preFailinBruteForceGuard::registerFailure's answer, counted.
Returns
WipeDue at the threshold, else WrongSecret with its lockout.

Back to the top

Variable Documentation

kMarkerArmAsks #

int kleidos::reset::kMarkerArmAsks
inlineconstexpr

How many times the first half asks for the marker: a refused NVS write is asked once more before the erase goes on without it.

kPreferenceKeys #

PrefKey kleidos::reset::kPreferenceKeys[]
inlineconstexpr

Every device-preference NVS entry that Reset Settings restores.

Reset Settings erases each of these so the next boot re-seeds its compile-time default. The list is every value-backed settings-model option that is a real NVS key (all of them but the pinned access-point identity, which kleidos::net::apidentity owns), plus the dim time derived from the screen-off timeout and the two debug hold toggles, and nothing that is vault, security, or identity state (see the file header for the exclusions). A host test keeps this list and the model in step.

Back to the top