xref: /aosp_15_r20/system/keymint/README.md (revision 9860b7637a5f185913c70aa0caabe3ecb78441e4)
1*9860b763SAndroid Build Coastguard Worker# KeyMint Rust Reference Implementation
2*9860b763SAndroid Build Coastguard Worker
3*9860b763SAndroid Build Coastguard WorkerThis repository holds a reference implementation of the Android
4*9860b763SAndroid Build Coastguard Worker[KeyMint
5*9860b763SAndroid Build Coastguard WorkerHAL](https://cs.android.com/android/platform/superproject/main/+/main:hardware/interfaces/security/keymint/aidl/android/hardware/security/keymint/IKeyMintDevice.aidl),
6*9860b763SAndroid Build Coastguard Workerincluding closely related HAL interfaces:
7*9860b763SAndroid Build Coastguard Worker
8*9860b763SAndroid Build Coastguard Worker- [`IRemotelyProvisionedComponent`](https://cs.android.com/android/platform/superproject/main/+/main:hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.aidl)
9*9860b763SAndroid Build Coastguard Worker- [`ISharedSecret`](https://cs.android.com/android/platform/superproject/main/+/main:hardware/interfaces/security/sharedsecret/aidl/android/hardware/security/sharedsecret/ISharedSecret.aidl)
10*9860b763SAndroid Build Coastguard Worker- [`ISecureClock`](https://cs.android.com/android/platform/superproject/main/+/main:hardware/interfaces/security/secureclock/aidl/android/hardware/security/secureclock/ISecureClock.aidl)
11*9860b763SAndroid Build Coastguard Worker
12*9860b763SAndroid Build Coastguard Worker## Repository Structure
13*9860b763SAndroid Build Coastguard Worker
14*9860b763SAndroid Build Coastguard WorkerThe codebase is divided into a number of interdependent crates, as follows.
15*9860b763SAndroid Build Coastguard Worker
16*9860b763SAndroid Build Coastguard Worker- `derive/`: The `kmr-derive` crate holds [proc
17*9860b763SAndroid Build Coastguard Worker  macros](https://doc.rust-lang.org/reference/procedural-macros.html) used for deriving the
18*9860b763SAndroid Build Coastguard Worker  `kmr_wire::AsCborValue` trait that is used for message serialization. This crate uses `std`, but
19*9860b763SAndroid Build Coastguard Worker  is only required for the build process on the host, and does not produce code that runs on the
20*9860b763SAndroid Build Coastguard Worker  device.
21*9860b763SAndroid Build Coastguard Worker- `wire/`: The `kmr-wire` crate holds the types that are used for communication between the
22*9860b763SAndroid Build Coastguard Worker  userspace HAL service and the trusted application code that runs in the secure world, together
23*9860b763SAndroid Build Coastguard Worker  with code for serializing and deserializing these types as CBOR. This crate is `no_std` but uses
24*9860b763SAndroid Build Coastguard Worker  `alloc`.
25*9860b763SAndroid Build Coastguard Worker- `common/`: The `kmr-common` crate holds common code used throughout the KeyMint
26*9860b763SAndroid Build Coastguard Worker  implementation. This includes metadata processing code, keyblob manipulation code, and also the
27*9860b763SAndroid Build Coastguard Worker  abstractions used to represent access to underlying cryptographic functionality. This crate is
28*9860b763SAndroid Build Coastguard Worker  `no_std` but uses `alloc`.
29*9860b763SAndroid Build Coastguard Worker- `ta/`: The `kmr-ta` crate holds the implementation of the KeyMint trusted application (TA), which
30*9860b763SAndroid Build Coastguard Worker  is expected to run within the device's secure environment. This crate is `no_std` but uses
31*9860b763SAndroid Build Coastguard Worker  `alloc`.
32*9860b763SAndroid Build Coastguard Worker- `hal/`: The `kmr-hal` crate holds the implementation of the HAL service for KeyMint, which is
33*9860b763SAndroid Build Coastguard Worker  expected to run in the Android userspace and respond to Binder method invocations. This crate uses
34*9860b763SAndroid Build Coastguard Worker  `std` (as it runs within Android, not within the more restricted secure environment).
35*9860b763SAndroid Build Coastguard Worker- `boringssl/`: The `kmr-crypto-boring` crate holds a BoringSSL-based implementation of the
36*9860b763SAndroid Build Coastguard Worker  cryptographic abstractions from `kmr-common`. This crate is `no_std` (but using `alloc`); however,
37*9860b763SAndroid Build Coastguard Worker  it relies on the Rust [`openssl` crate](https://docs.rs/openssl) for BoringSSL support, and that
38*9860b763SAndroid Build Coastguard Worker  crate uses `std`.
39*9860b763SAndroid Build Coastguard Worker- `tests/`: The `kmr-tests` crate holds internal testing code.
40*9860b763SAndroid Build Coastguard Worker
41*9860b763SAndroid Build Coastguard Worker| Subdir           | Crate Name          | `std`?              | Description                                           |
42*9860b763SAndroid Build Coastguard Worker|------------------|---------------------|---------------------|-------------------------------------------------------|
43*9860b763SAndroid Build Coastguard Worker| **`derive`**     | `kmr-derive`        | Yes (build-only)    | Proc macros for deriving the `AsCborValue` trait      |
44*9860b763SAndroid Build Coastguard Worker| **`wire`**       | `kmr-wire`          | No                  | Types for HAL <-> TA communication                    |
45*9860b763SAndroid Build Coastguard Worker| **`common`**     | `kmr-common`        | No                  | Common code used throughout KeyMint/Rust              |
46*9860b763SAndroid Build Coastguard Worker| **`ta`**         | `kmr-ta`            | No                  | TA implementation                                     |
47*9860b763SAndroid Build Coastguard Worker| **`hal`**        | `kmr-hal`           | Yes                 | HAL service implementation                            |
48*9860b763SAndroid Build Coastguard Worker| **`boringssl`**  | `kmr-crypto-boring` | Yes (via `openssl`) | Boring/OpenSSL-based implementations of crypto traits |
49*9860b763SAndroid Build Coastguard Worker| `tests`          | `kmr-tests`         |                     | Tests and test infrastructure                         |
50*9860b763SAndroid Build Coastguard Worker
51*9860b763SAndroid Build Coastguard Worker## Porting to a Device
52*9860b763SAndroid Build Coastguard Worker
53*9860b763SAndroid Build Coastguard WorkerTo use the Rust reference implementation on an Android device, implementations of various
54*9860b763SAndroid Build Coastguard Workerabstractions must be provided.  This section describes the different areas of functionality that are
55*9860b763SAndroid Build Coastguard Workerrequired.
56*9860b763SAndroid Build Coastguard Worker
57*9860b763SAndroid Build Coastguard Worker### Rust Toolchain and Heap Allocator
58*9860b763SAndroid Build Coastguard Worker
59*9860b763SAndroid Build Coastguard WorkerUsing the reference implementation requires a Rust toolchain that can target the secure environment.
60*9860b763SAndroid Build Coastguard WorkerThis toolchain (and any associated system libraries) must also support heap allocation (or an
61*9860b763SAndroid Build Coastguard Workerapproximation thereof) via the [`alloc` sysroot crate](https://doc.rust-lang.org/alloc/).
62*9860b763SAndroid Build Coastguard Worker
63*9860b763SAndroid Build Coastguard WorkerIf the BoringSSL-based implementation of cryptographic functionality is used (see below), then some
64*9860b763SAndroid Build Coastguard Workerparts of the Rust `std` library must also be provided, in order to support the compilation of the
65*9860b763SAndroid Build Coastguard Worker[`openssl`](https://docs.rs/openssl) wrapper crate.
66*9860b763SAndroid Build Coastguard Worker
67*9860b763SAndroid Build Coastguard Worker**Checklist:**
68*9860b763SAndroid Build Coastguard Worker
69*9860b763SAndroid Build Coastguard Worker- [ ] Rust toolchain that targets secure environment.
70*9860b763SAndroid Build Coastguard Worker- [ ] Heap allocation support via `alloc`.
71*9860b763SAndroid Build Coastguard Worker
72*9860b763SAndroid Build Coastguard Worker### HAL Service
73*9860b763SAndroid Build Coastguard Worker
74*9860b763SAndroid Build Coastguard WorkerKeyMint appears as a HAL service in userspace, and so an executable that registers for and services
75*9860b763SAndroid Build Coastguard Workerthe KeyMint related HALs must be provided.
76*9860b763SAndroid Build Coastguard Worker
77*9860b763SAndroid Build Coastguard WorkerThe implementation of this service is mostly provided by the `kmr-hal` crate, but a driver program
78*9860b763SAndroid Build Coastguard Workermust be provided that:
79*9860b763SAndroid Build Coastguard Worker
80*9860b763SAndroid Build Coastguard Worker- Performs start-of-day administration (e.g. logging setup, panic handler setup)
81*9860b763SAndroid Build Coastguard Worker- Creates a communication channel to the KeyMint TA.
82*9860b763SAndroid Build Coastguard Worker- Registers for the KeyMint HAL services.
83*9860b763SAndroid Build Coastguard Worker- Starts a thread pool to service requests.
84*9860b763SAndroid Build Coastguard Worker
85*9860b763SAndroid Build Coastguard WorkerThe KeyMint HAL service (which runs in userspace) must communicate with the KeyMint TA (which runs
86*9860b763SAndroid Build Coastguard Workerin the secure environment).  The reference implementation assumes the existence of a reliable,
87*9860b763SAndroid Build Coastguard Workermessage-oriented, bi-directional communication channel for this, as encapsulated in the
88*9860b763SAndroid Build Coastguard Worker`kmr_hal::SerializedChannel` trait.
89*9860b763SAndroid Build Coastguard Worker
90*9860b763SAndroid Build Coastguard WorkerThis trait has a single method `execute()`, which takes as input a request message (as bytes), and
91*9860b763SAndroid Build Coastguard Workerreturns a response message (as bytes) or an error.
92*9860b763SAndroid Build Coastguard Worker
93*9860b763SAndroid Build Coastguard WorkerA (shared) instance of this trait must be provided to each of the `kmr_hal::<interface>::Device`
94*9860b763SAndroid Build Coastguard Workertypes, which allows them to service Binder requests for the relevant interface by forwarding the
95*9860b763SAndroid Build Coastguard Workerrequests to the TA as request/response pairs.
96*9860b763SAndroid Build Coastguard Worker
97*9860b763SAndroid Build Coastguard Worker**Checklist:**
98*9860b763SAndroid Build Coastguard Worker
99*9860b763SAndroid Build Coastguard Worker- [ ] Implementation of HAL service, which registers for all HAL services.
100*9860b763SAndroid Build Coastguard Worker- [ ] SELinux policy for the HAL service.
101*9860b763SAndroid Build Coastguard Worker- [ ] init.rc configuration for the HAL service.
102*9860b763SAndroid Build Coastguard Worker- [ ] Implementation of `SerializedChannel` trait, for reliable HAL <-> TA communication.
103*9860b763SAndroid Build Coastguard Worker- [ ] Populate userspace environment information at start of day, using `kmr_hal::send_hal_info()`.
104*9860b763SAndroid Build Coastguard Worker
105*9860b763SAndroid Build Coastguard WorkerThe Cuttlefish implementation of the [KeyMint/Rust HAL
106*9860b763SAndroid Build Coastguard Workerservice](https://cs.android.com/android/platform/superproject/main/+/main:device/google/cuttlefish/guest/hals/keymint/rust/src/keymint_hal_main.rs)
107*9860b763SAndroid Build Coastguard Workerprovides an example of all of the above.
108*9860b763SAndroid Build Coastguard Worker
109*9860b763SAndroid Build Coastguard Worker### TA Driver
110*9860b763SAndroid Build Coastguard Worker
111*9860b763SAndroid Build Coastguard WorkerThe `kmr-ta` crate provides the majority of the implementation of the KeyMint TA, but needs a driver
112*9860b763SAndroid Build Coastguard Workerprogram that:
113*9860b763SAndroid Build Coastguard Worker
114*9860b763SAndroid Build Coastguard Worker- Performs start-of-day administration (e.g. logging setup).
115*9860b763SAndroid Build Coastguard Worker- Populates initially required information (e.g. `kmr_ta::HardwareInfo`)
116*9860b763SAndroid Build Coastguard Worker- Creates a `kmr_ta::KeyMintTa` instance.
117*9860b763SAndroid Build Coastguard Worker- Configures the communication channel with the HAL service.
118*9860b763SAndroid Build Coastguard Worker- Configures the communication channel with the bootloader, which is required so that the current
119*9860b763SAndroid Build Coastguard Worker  root-of-trust boot information can be received.
120*9860b763SAndroid Build Coastguard Worker- Holds the main loop that:
121*9860b763SAndroid Build Coastguard Worker    - reads request messages from the channel(s)
122*9860b763SAndroid Build Coastguard Worker    - passes request messages to `kmr_ta::KeyMintTa::process()`, receiving a response
123*9860b763SAndroid Build Coastguard Worker    - writes response messages back to the relevant channel.
124*9860b763SAndroid Build Coastguard Worker
125*9860b763SAndroid Build Coastguard Worker**Checklist:**
126*9860b763SAndroid Build Coastguard Worker
127*9860b763SAndroid Build Coastguard Worker- [ ] Implementation of `main` equivalent for TA, handling scheduling of incoming requests.
128*9860b763SAndroid Build Coastguard Worker- [ ] Implementation of communication channel between HAL service and TA.
129*9860b763SAndroid Build Coastguard Worker- [ ] Implementation of communication channel from bootloader to TA.
130*9860b763SAndroid Build Coastguard Worker    - [ ] Trigger call to `kmr_ta::KeyMintTa::set_boot_info` on receipt of boot info.
131*9860b763SAndroid Build Coastguard Worker
132*9860b763SAndroid Build Coastguard WorkerThe Cuttlefish implementation of the [KeyMint/Rust
133*9860b763SAndroid Build Coastguard WorkerTA](https://cs.android.com/android/platform/superproject/main/+/main:device/google/cuttlefish/host/commands/secure_env/rust/lib.rs)
134*9860b763SAndroid Build Coastguard Workerprovides an example of all of the above.
135*9860b763SAndroid Build Coastguard Worker
136*9860b763SAndroid Build Coastguard Worker### Bootloader
137*9860b763SAndroid Build Coastguard Worker
138*9860b763SAndroid Build Coastguard WorkerThe bootloader is required to transmit root of trust and boot state information to the TA at start
139*9860b763SAndroid Build Coastguard Workerof day, so the TA can bind keys to the root of trust appropriately.  The bootloader should fill out
140*9860b763SAndroid Build Coastguard Workerand send a `kmr_wire::SetBootInfoRequest` message to do this.
141*9860b763SAndroid Build Coastguard Worker
142*9860b763SAndroid Build Coastguard Worker**Checklist:**
143*9860b763SAndroid Build Coastguard Worker
144*9860b763SAndroid Build Coastguard Worker- [ ] Implementation of communication channel from bootloader to TA.
145*9860b763SAndroid Build Coastguard Worker- [ ] Trigger for and population of `kmr_wire::SetBootInfoRequest` message.
146*9860b763SAndroid Build Coastguard Worker
147*9860b763SAndroid Build Coastguard Worker### Authenticators
148*9860b763SAndroid Build Coastguard Worker
149*9860b763SAndroid Build Coastguard WorkerKeyMint supports auth-bound keys that can only be used when an appropriate hardware authentication
150*9860b763SAndroid Build Coastguard Workertoken (HAT) is presented. Secure authenticators such as Gatekeeper or Fingerprint produce these
151*9860b763SAndroid Build Coastguard WorkerHATs, and validation of them requires that:
152*9860b763SAndroid Build Coastguard Worker
153*9860b763SAndroid Build Coastguard Worker- [ ] KeyMint and the authenticators share a common monotonic time source.
154*9860b763SAndroid Build Coastguard Worker- [ ] The authenticators have access to the (per-boot) HMAC signing key, via one of:
155*9860b763SAndroid Build Coastguard Worker   - [ ] The authenticator retrieves the HMAC key from KeyMint via a communication mechanism that is
156*9860b763SAndroid Build Coastguard Worker         completely internal to the secure environment, using `KeyMintTa::get_hmac_key`, or
157*9860b763SAndroid Build Coastguard Worker   - [ ] The authenticator also implements the `ISharedSecret` HAL, and joins in the HMAC key
158*9860b763SAndroid Build Coastguard Worker         derivation process.  This requires that the authenticator have access to the pre-shared key
159*9860b763SAndroid Build Coastguard Worker         that is used as the basis of the derivation process.
160*9860b763SAndroid Build Coastguard Worker
161*9860b763SAndroid Build Coastguard Worker### Cryptographic Abstractions
162*9860b763SAndroid Build Coastguard Worker
163*9860b763SAndroid Build Coastguard WorkerThe KeyMint TA requires implementations for low-level cryptographic primitives to be provided, in
164*9860b763SAndroid Build Coastguard Workerthe form of implementations of the various Rust traits held in
165*9860b763SAndroid Build Coastguard Worker[`kmr_common::crypto`](common/src/crypto/traits.rs).
166*9860b763SAndroid Build Coastguard Worker
167*9860b763SAndroid Build Coastguard WorkerNote that some of these traits include methods that have default implementations, which means that
168*9860b763SAndroid Build Coastguard Workeran external implementation is not required (but can be provided if desired).
169*9860b763SAndroid Build Coastguard Worker
170*9860b763SAndroid Build Coastguard Worker**Checklist:**
171*9860b763SAndroid Build Coastguard Worker
172*9860b763SAndroid Build Coastguard Worker- [ ] RNG implementation: `Rng`.
173*9860b763SAndroid Build Coastguard Worker- [ ] Constant time comparison implementation: `ConstTimeEq`.
174*9860b763SAndroid Build Coastguard Worker- [ ] AES implementation: `Aes`.
175*9860b763SAndroid Build Coastguard Worker- [ ] 3-DES implementation: `Des`.
176*9860b763SAndroid Build Coastguard Worker- [ ] HMAC implementation: `Hmac`.
177*9860b763SAndroid Build Coastguard Worker- [ ] RSA implementation: `Rsa`.
178*9860b763SAndroid Build Coastguard Worker- [ ] EC implementation, including curve 25519 support: `Ec`.
179*9860b763SAndroid Build Coastguard Worker- [ ] AES-CMAC or CKDF implementation: `AesCmac`, `Ckdf`.
180*9860b763SAndroid Build Coastguard Worker
181*9860b763SAndroid Build Coastguard WorkerBoringSSL-based implementations are available for all of the above.
182*9860b763SAndroid Build Coastguard Worker
183*9860b763SAndroid Build Coastguard Worker### Device Abstractions
184*9860b763SAndroid Build Coastguard Worker
185*9860b763SAndroid Build Coastguard WorkerThe KeyMint TA requires implementations of traits that involve interaction with device-specific
186*9860b763SAndroid Build Coastguard Workerfeatures or provisioned information, in the form of implementations of the various Rust traits held
187*9860b763SAndroid Build Coastguard Worker(mostly) in [`kmr_ta::device`](ta/src/device.rs).
188*9860b763SAndroid Build Coastguard Worker
189*9860b763SAndroid Build Coastguard Worker**Checklist:**
190*9860b763SAndroid Build Coastguard Worker
191*9860b763SAndroid Build Coastguard Worker- [ ] Secure time implementation (monotonic, shared with authenticators): `kmr_common::crypto::MonotonicClock`.
192*9860b763SAndroid Build Coastguard Worker- [ ] Root key(s) retrieval implementation: `RetrieveKeyMaterial`.
193*9860b763SAndroid Build Coastguard Worker- [ ] Attestation key / chain retrieval implementation (optional): `RetrieveCertSigningInfo`.
194*9860b763SAndroid Build Coastguard Worker- [ ] Attestation device ID retrieval implementation: `RetrieveAttestationIds`.
195*9860b763SAndroid Build Coastguard Worker- [ ] Retrieval of BCC and DICE artefacts: `RetrieveRpcArtefacts`.
196*9860b763SAndroid Build Coastguard Worker- [ ] Secure secret storage (for rollback-resistant keys) implementation (optional): `SecureDeletionSecretManager`.
197*9860b763SAndroid Build Coastguard Worker- [ ] Bootloader status retrieval (optional): `BootloaderStatus`.
198*9860b763SAndroid Build Coastguard Worker- [ ] Storage key wrapping integration (optional): `StorageKeyWrapper`.
199*9860b763SAndroid Build Coastguard Worker- [ ] Trusted user presence indication (optional): `TrustedUserPresence`.
200*9860b763SAndroid Build Coastguard Worker- [ ] Legacy keyblob format converter (optional): `LegacyKeyHandler`.
201*9860b763SAndroid Build Coastguard Worker
202*9860b763SAndroid Build Coastguard Worker## Supporting Older Versions of the KeyMint HAL
203*9860b763SAndroid Build Coastguard Worker
204*9860b763SAndroid Build Coastguard WorkerThe reference implementation has the ability to behave like an earlier version of the KeyMint
205*9860b763SAndroid Build Coastguard WorkerHAL. To enable emulation of (say) KeyMint v1, link the HAL service against the `libkmr_hal_v1` and
206*9860b763SAndroid Build Coastguard Worker`libkmr_wire_hal_v1` targets rather than `libkmr_hal` / `libkmr_wire`.
207