Commit graph

104 commits

Author SHA1 Message Date
bunnie
9093bb4ed7 simplify keystore setup
The TL;DR is this now treats dabao exactly the same as
baosec in terms of keystore setup from the security flow perspective.

baosec & dabao were different originally because baosec would
rely upon the Avalanche generator TRNG as supplemental randomness
and dabao would not. This special case had to be handled in the
bootloader and propagated through.

However, in a previous commit, the keystore setup was moved to
the OS *anyways* - thus making this difference moot.

This patch backs out the one-way counters that would track the
dabao initialization state.

It also doesn't fail the boot on the erase key proof check
failing if the keystores aren't initialized yet. This creates
yet another condition that could be glitched over to bypass
the erase check but it is checked twice indepedently.
2025-12-18 02:16:50 +08:00
bunnie
d874c3e5c2 commit for version bump 2025-12-16 02:02:37 +08:00
bunnie
679ece9b19 major rework of the clock-set routines
the routines provided in the reference crossbar libraries are
not great and the recco of their team is to just use hard-coded
table lookups. Replaced this with a computational method that can
find arbitrary clocks; however, the computation *is* somewhat expensive
because you have to iterate through a fairly large solution space
to find an "optimal" solution.

That being said, these solutions can be memoized if they are
frequently re-used. I rather prefer this method than just an undocumented
table lookup.

More testing is needed but the initial results look pretty good.

Also peripheral clock setting is reworked to be more clear as to what
is going on. I think I finally have some clarity on why a mysterious
"divide by 2" was ending up in everything; other dependencies have
been reworked to clean that up. I think actually I may have been
overclocking the peripheral bus by a factor of 2 prior to now :-/
2025-12-16 00:58:37 +08:00
bunnie
021cf609e4 remove wfi apis from hal
this is now in the susres crate
2025-12-16 00:58:10 +08:00
bunnie
261ea7a880 update apis in hal-service 2025-12-11 02:18:44 +08:00
bunnie
eee9cd62c9
enable secure erase confirmation in loader (leaks one chaff key)
A problem was discovered while implementing the secure boot
hardening. Namely, an attacker could trick the loader into
loading a devkey-signed kernel by incrementing the
developer mode counter without actually triggering the
secure erase of the keys, and finding a way to glitch
over the check of the developer mode counter check.

Two countermeasuers are implemented:

1. if the developer mode flag is set, trigger a secure erase
even if the image signatures aren't developer mode

2. leak one of the chaff keys into the loader, so there are
now at least two indpendent checks of being in developer
mode in the loader. One is based on the counter, which
is only weakly correlated with the erased state. The
other is checking for the erase value in the leaked key,
which is a strong indicator of being in developer mode.

The leaked key is, of course, erased before entering the
kernel (and the key is leaked into a region of memory
that is, in theory, securely claimed by the keystore, so
it's not like anyone can just read it; however, it is
in a less secure location than the protected key area).

The impact of an adversary obtaining the key is only
a slight weakening of security because the chaff keys
are numerous, they still have thousands of bits to
guess even if they got a couple hundred of them.

The next series of patches implements the scheme outlined
above, starting with defining the key to be leaked. The
leaked key is intentionally the last key in the slice
of keys used in the erasure algorithm, so that presumably
if the leaked key is showing an erased value, all other
keys have been erased.
2025-12-06 18:24:40 +08:00
bunnie
9ddf6beae6
move the kernel start address
this correctly gives the loader 256k of space, which is what
its link file specifies. The loader was previously under 128k
by chance, but with glitch hardening it's grown past that
boundary.
2025-12-06 18:23:10 +08:00
bunnie
32ffba0931
introduce security configurations
The management of different security configurations was getting
a bit out of hand, so all the security configurations used by
the system and their transitions are now consolidated into a single
file, This will make auditing and checking the transitions a lot
easier.
2025-12-05 21:37:21 +08:00
bunnie
362124127e
add redundant counters, paranoid mode, attack counters
This allocates some additional one way counters dedicated
to hardening:

- Paranoid mode is duplicated in slots 65 & 67
- An attack counter is in slot 66
- Redundant revocation counters are seated in slot 68

The primary revocation counters are in slot 116. This means
that cutting out the revocation counters would probably require
at least two edits to the memory array - simply cutting the
word addressing line for those columns might be enough to
defeat a counter.

Paranoid mode duplicate is stored somewhat close to the
primary counter, which means it could be subject to a single
line cut to fully defeat the counter, but the space is getting
pretty crowded so I'll leave it like that; I really want to
leave the bottom 64 slots available for application use.

A "flag" key is allocated, plus a dummy flag is committed
to the repo. This is a value that, if retrieved, would
ostensibly bring the retriever glory and bounty, to the
extent that any glory or bounty is to be had (ps: I'm poor
and this project is obscure). The actual flag, of course,
is not in the repo...I hope!

The flag value is different from all other values in that
this is the only shared, common secret across all devices.

All other device secrets are generated on a per-device
basis from the TRNGs. So the flag needs some special treatment
such that it's not overwritten during device keying.

The flag exists because I'm hoping that anyone who manages
to extract it will also provide a helpful explanation of
the procedure to get it out, so that the system could be
patched (if the error is patchable) or rev'd in future
silicon. That being said, a flag retrieval is not necessarily
equal to a full break of security across all devices because
as mentioned above, most device secrets are on a per-device
basis. The flag represents then, the amount of effort necessary
to apply to every device to retrieve the secrets within.
2025-12-04 14:56:07 +08:00
bunnie
31385a5599
start moving clock routines into an API library
clock setting calls, for legacy reasons, are duplicated in all the
stages of the bootloaders. This is a start toward moving these routines
into a common set of calls that improves maintainability.
2025-12-04 14:54:26 +08:00
bunnie
bd08328530
add bollard macro and HardenedBool
These macros are added to the platform-agnostic API crate.

bollard is a macro that can respond to the following argument
formats:

bollard!(): inserts 4 illegal instructions and a jump over them
bollard!(#): inserts # of illegal instructions and a jump over them
bollard!(f, #): inserts # of an interleave of illegal instructions
and a jump to f

the last form is intended to "up the ante" by including a call
to a routine that can shut down the chip and/or wipe secrets
should the defender choose to do that. The other ones just
trampoline into an illegal instruction vector which is a fairly
weak defense because the CPU could once again get glitched out
of that.

HardenedBool is a boolean represented by a high-hamming distance
numeric value so that single-bit flips just lead to an invalid
bool, instead of a change in the meaning of the bool.
2025-12-04 14:50:33 +08:00
bunnie
1ca144f47f remove stale constants; add an irq handler storage class
constants made obsolete in the latest utra update are removed,
and a storage class for multi-purpose interrupt handlers is added.
2025-11-25 19:08:53 +08:00
bunnie
960ba350cb add bu_mem locations
these failed to extract automatically from the RTL
2025-11-23 08:48:24 +08:00
bunnie
a7477ec53b
Remove key slot implementation; migrate keys to data slots
The -A1 stepping is going to tie the IFR access control
bits for data and key slots together. This is due to an RTL
bug where the key slot maps were typo'd and they ended
up mapping to the same IFR ACL.

We "could" continue to use key slots with a rule that
key and data slots would have to coordinate with each
other and not use the same slot number.

However, because of this linkage, and because there are
other bugs suspected in the keyslot implementation, an
executive decision was made to disregard the key slot
feature in the chip.

The main downsides of this are:

- fewer slots (2048 vs 4096)
- no key access linkage to HMAC auth on SCE

I *think* that's it.

Fewer slots - 2048 slots is still a lot of slots, about half are
used now, I think this is plenty.

Key access linkage to HMAC auth on SCE - I have reasons to
suspect this feature is broken anyways for subtle reasons
related to malleability of the HMAC round constants. Even
without that brokenness, it's hard to use HMAC auth to manage
the system because it requires per-device unique keys: so,
we're using a PKI scheme and no symmetric secrets to handle
access controls.

Removing the key slots also greatly simplifies the code
base analytically. This culling removes the key slot
abstraction the "simple" way, but without the weirdness
of the keyslots (in particular the key ID chaining which
makes them different from data slots) I could remove even
more abstractions and simplify the code.

One upside is now all elements can enjoy the write mode
behavior which can force the slot to be only 0->1 writeable,
which could be useful for other things down the road.

This was tested to not interfere with Dabao-Alpha 0
bootloaders.
2025-11-13 18:21:52 +08:00
bunnie
f88c801e01
update key ACL to r/w
this allows the OS to initialize the keys

note that including write access only gives it to the fw0 user,
which is the OS's secure user. thus any write primitive created
by this expansion of access should transitively also be a read
primitive (which is actually what the adversary desires).

In exchange the OS is allowed to initialize the keys, which
I think is the right place to do it given that it's too hard
at this point to nail down the exact key layout, etc.

Note that it is impossible for the OS to change ACLs - that is
a feature that can only be updated by the boot user - which
is why the boot user can change the ACL to gain access to
erase keys, but the OS user can't modify its own ACL.
2025-11-13 11:06:16 +08:00
bunnie
10bd1502d0 fix ACL to match actual usage of data slots 2025-11-12 22:08:12 +08:00
bunnie
d91c2e7671 fix API to reflect that the 0-state is the most permissive state 2025-11-06 14:10:00 +08:00
bunnie
aaf1cbd2f9 add base routines for deriving & controlling serial numbers 2025-11-04 13:13:52 +08:00
bunnie
d901c61846 modify boot sequence to generate keys for a dabao environment
also update the audit to check the respective flags
2025-11-01 23:56:47 +08:00
bunnie
26078dcc7d add keystore to dabao configuration
After some contemplation, we should include the keystore in the
core service set featured by the dabao-xous configuration.

The pros are: any compatibility/sharing of resources
that require access to RRAM, one-way counters, UUIDs, etc.
will either require a wholly separate implementation that is
"insecure" or it could just reuse the baosec work. Furthermore,
as cryptographic accelerators are laid into the keystore,
the APIs become available to the dabao environment

The cons are: more space is used in a space-constrained
dabao environment (roughly 140k "deadweight" at the start, but
as cryptographic APIs get layered in this can grow substantially);
the key slots are "used up" in part by the scheme, which leaves
less key slots available for user-defined purposes.

This is mitigated by the fact that there are a couple thousand
key slots, and so even though we use a couple hundred of them
it's still not the whole range.

Also, the baosec/dabao feature flag can be used to feature-out
cryptographic APIs/accelerators that are too obscure or not
useful for the dabao environment, to "lighten up" the
firmware load.
2025-11-01 22:59:07 +08:00
bunnie
94e1f6044a add spim flash autodetect routine and partial support for xtx devices
xtx devices still not fully working, but at least able to switch
in and out of spim modes in the face of uncertainty over which
device type is present.
2025-10-30 04:13:37 +08:00
bunnie
c827a0ed12
add swap key slot 2025-10-28 18:50:09 +08:00
bunnie
d10274b9ec
fixup pio->bio typos
also fixup a return-type bug in the std implementation for
bio pin settings

much more bio abstractions will be needed, but for now,
this branch is just focused on the keystore.
2025-10-28 17:57:33 +08:00
bunnie
290a662bc3
add pubkey block to the data sets, fix nuisance key range
just learned of another ECO incoming that will tie the key
and data areas together in terms of access control from
the IFR region, so the nuisance key range has to have
its access control redefined to steer clear of the data
slots allocated that have write-protects on them.

pubkey block is added to the data ranges

also a oneway counter to keep track of pubkey validation
failures in boot0 is added
2025-10-28 03:33:15 +08:00
bunnie
e6bef77b5f
change boot1 default behavior
The motivation for this is to make the tester configuration
exactly identical to that used in production. Right now a
"cheater" step is used to set the board type to dabao and
enable boot waiting because in the test environment there
isn't a physical switch anyone can press to invoke that
behavior.

So, in order to get the test environment to work just like
production, the behavior goes from insta-die if no valid
image is found on the next stage, to "wait for new image".
This is slightly less secure of a default, but only slightly
so. Basically the previous scheme required someone to be
physically present to press the "give me a new image" button,
this one causes the system to automatically accept a new image.
I suppose the main downside is this does allow the possibility
of say, a fully remote, automated exploit to be delivered
if an adversary can damage the loader image such that it
doesn't pass signature check and then it will automatically
go into a mode looking for a new image, at which point
an arbitrary image could be potentially loaded (although
because the image would need to be signed, and there is
a trap that destroys secrets if a developer image is
presented, it wouldn't instantly lead to secret disclosure,
but it would be a denial of service).

The flip side is that with the paranoid default, developers
are "confused" by the silent failure of a brand new chip that
has no code loaded in it (because it goes catatonic until
the developer knows to press the load button).

The loader image is protected from writes after boot, so
in theory there isn't a direct path to "spoil" the loader
image and trigger this path, so it's not like we're just
hanging in the wind. It just means that there isn't a
full human-presence requirement as a defense in depth against
a potential denial of service if an adversary could find
a way to spoil the loader image.

If the DoS path is a concern, one option is to configure
the system such that the developer key is revoked. This
would disallow that from being recognized as a valid
image, and would also prevent the DoS from happening,
as it would require the adversary to present a valid,
signed image which presumably they can't as long as
the signing keys are kept safe.
2025-10-27 18:22:00 +08:00
bunnie
775726af23 standardize BAUD_RATE location
also slow down pre-emption timer a bit, on a slower system
like ours 100Hz is less overhead
2025-10-26 19:30:49 +08:00
bunnie
4e522873f0 add preemptionstate opcode to the hal api 2025-10-26 13:38:28 +08:00
bunnie
1c435909a2
fix the sense of the access permission bits
I had read the docs as saying 1 is denied access, but in fact,
1 is allowed access, at least empirically.
2025-10-26 01:55:27 +08:00
bunnie
04d46b9549 update location of system timer interval definition 2025-10-24 21:12:00 +08:00
bunnie
caf6fc3a61
Implement erase_secrets() on entry into developer mode 2025-10-21 23:46:06 +08:00
bunnie
e3140609e4
designate the eraseable keys
only some keys are marked as writeable to harden against
potential oracle attacks, i.e., if we made all keys
erasable and an oracle attack was found, then the entire
key region can be exposed by the oracle attack.

Instead, some key material always persists, but a sufficiently
large amount of seed material is erased such that the key is not
recoverable by guessing the key.
2025-10-21 19:08:35 +08:00
bunnie
3e4b0b6571
Add bao2 signing keys 2025-10-21 02:39:11 +08:00
bunnie
2095acfe13
slot manager debug.
add read/write specs as well to the slot definitions.

Not terribly happy with how this all worked out. The subtle
differences between Data and Keys is really irritating.
it's the sort of thing you would say "let's make a trait"
for it but then the two behave just differently enough
that I worry I can't make a trait generic enough to encompass
all the weird stuff that exists in the hardware.

The existing implementation definitely looks like trait
territory, but it currently hides the subtleties around
key/data chaining and secure crypto engine handling of
the two regions as distinct entities. so long as we don't
plan to use those things, traits are a good idea, but
i don't feel like I can reject using the crypto engine
features out of hand this early on just to make the
APIs nice and square.
2025-10-20 23:32:29 +08:00
bunnie
a64337679f
Add API for defining key/data slots, and initial ranges for the same
Data and key slots are hardware access-controlled areas that
can store sensitive data. Slot definitions come with a "spec"
field that defines what the access controls *should* be,
so that we can check them later.

In practice no user code should be manipulating these, but
there is an initial stage where a totally blank device needs
to be provisioned, hence the focus on correct specification
and checking of state, rather than convenient runtime manipulation
and update of the same.
2025-10-20 02:07:19 +08:00
bunnie
a40bb7c244 other half of the adjustment 2025-10-18 01:16:16 +08:00
bunnie
3b4e035a4f
revise auth_data handling for key header block
auth_data actually changes with each signing operation (ostensibly
to prevent replay of signatures coming from tokens) so it has
to be presented as data parallel to the signature, instead of
as a static filed baked into the key header.

This commit revises the header record to reflect this organizatio
of data and also revises the signature check to be in-line
with the protocol.
2025-10-06 23:31:02 +08:00
bunnie
03ea3c9d0c changes to the apis to accommodate the public key record format 2025-10-06 15:20:34 +08:00
bunnie
a1a5ff1592 add public key templates
Beta/developer keys are fully filled out.

Placeholders for production signing keys are also provided.
2025-10-06 15:19:19 +08:00
bunnie
3c68eea714
Merge branch 'main' into signing 2025-10-06 13:45:29 +08:00
bunnie
dbb0a75118 bump versions
have to absorb a version change due to xous-semver change

might as well also update the utra/svd generator dependencies
since that's been held off for a long time now.
2025-10-06 13:23:18 +08:00
bunnie
69d81042b9 upgrade signature check to handle domain separation data
this is needed to handle the fido2/webauthn style signatures
2025-10-06 03:49:11 +08:00
bunnie
01a78e4474 fixup app region start and add freq/voltage configs for board targets
this is the agnostic version of all of the board targets, which
allows the boot1 to lookup based on the current configuration
which configurations to apply.

I think I've talked myself into giving away $0.20 of parts with
every dabao board so that everyone can run at full frequency.
Originally I thought I'd limit to the minimal configuration with
low frequency operation but since this is the main developer
platform all the benchmarks and first impressions will come
through this board. Crippling it with a slower clock frequency
by omitting the supplementary digital regulator seems
short-sighted, even if it means up slightly subsidizing things
with this extra cost.
2025-10-05 17:12:40 +08:00
bunnie
2f5af89547 make the swap header length explicitly controlled
previously it was just "4096" in the image writer
2025-10-04 18:52:30 +08:00
bunnie
c86844a39a make signatureinflash structure data
this cleans up one unsafe step where we have to assume an
alignment of the data at the cost of the structure being copied
to stack. I think this is a fine trade-off.
2025-10-04 18:51:34 +08:00
bunnie
869da85ef7 add swap descriptors as structures; add offsets for boards
In the API layer, the boards aren't setup with convenience flags.
they are all laid out and the caller needs to know which board
it's trying to resolve. This is because the API layer is agnostic
to board and assumes the tool working with it knows what board
it's working with already.

The HAL layer tries to hide the board-specific stuff, and so
API defines are re-imported there in a board-transparent fashion.

Also add swap descriptors to the API crate, so that the image
generation tools can refer to the exact same structures as
are used inside the code, simplifying image creation maintenance.
2025-10-04 00:51:25 +08:00
bunnie
ec9aed29dd fix kernel offset so signature doesn't shift the image
the image needs to be page-aligned so it can do direct-map
to memory.
2025-10-01 23:35:24 +08:00
bunnie
cdb60eed0f update the static table definition 2025-09-30 21:52:45 +08:00
bunnie
15562abf00 add functional code slice
this puts a domain separator in every signature block
a domain separator meaning, even though the same private key
maybe be used to sign multiple blocks, because a functional code
is embedded in the block that is tied to the block's function, you
can't take a pre-signed block and shove it into another block's slot
and expect it to work.
2025-09-29 22:17:56 +08:00
bunnie
f8e124f6f9 limit writeable area to the active data area
the top of memory is reserved for various security structures

the security structures do have some hardware protection

but life is better when we also help programmers avoid triggering
the protections by accident and then just being confused.
2025-09-29 18:41:29 +08:00
bunnie
ba817cc1a9 add uf2 identifier for baochip-1x
just pulled it out of a uuid generator, checked it has no
collisions. At some point, maybe some official repo will allow
us to be "real" but ain't nobody got time for those politics
today.
2025-09-29 16:20:43 +08:00
bunnie
c6445c9a9b add an API for accessing one-way counter values
A macro is added, a bit like this one:

```
encode_oneway! {
    #[offset = 80]
    pub enum BootWaitCoding {
        Disable,
        Enable,
    }
}
```

Which encodes possible one-way counter states along with their
offset in the one-way counter array.

The states in the enum correspond to the evolution of the counter,
modulo the number of elements in the enume.

The macro also creates a getter for the states, and an incrementer
to try and make this whole process less error-prone.
2025-09-29 13:40:14 +08:00
bunnie
094207fe40 add signatures API
this allows us to generate signatures using the same API
that we use to check 'em.
2025-09-26 22:56:19 +08:00
bunnie
a3a7bf9f3a add a format for passing static initialized data to baremetal
a small, 256-byte record is added to the top of baremetal records
that is a "poke table" that incorporates what values to put where
for baremetal targets, as well as a zeroization region.

This is generally more efficient because most values are set to
zero, and the only things that generally have a non-zero value
are 'static' variables, which are used extremely sparingly.
2025-09-21 21:50:48 +08:00
bunnie
1255bf12d9 FLAG DAY: fix all names to harmonize around 'bao1x'
The code base had evolved through several code names, interim
names, company names and chip names that alternatively did or did
not exist.

Baochip is the definitive name of the company, and the Baochip1x
(full part number BAO1X2S4F-WA) is the definitivey part number of
the SoC. For convenience, the part number is shortened to 'bao1x'
in most feature flags and variable references.

This commit fixes all of the odd names and removes some unmaintained
early targets.
2025-09-18 21:25:51 +08:00