Skip to main content

rusty_esp_mid_core/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![forbid(unsafe_code)]
3//! `rusty_esp_mid-core` — MATA mID on the chip, the pure core.
4//!
5//! A Janus device is its own `did:mata`: a P-256 key generated on the chip,
6//! whose compressed public key *is* the identifier. This crate holds
7//! everything about that identity that needs no driver:
8//!
9//! | Module | Holds | Interoperates with |
10//! |---|---|---|
11//! | [`did`] | [`Did`] — `did:mata:<base58btc(33-byte SEC1)>`, no-alloc encode/parse | `mid-verify::did_from_pubkey` |
12//! | [`key`] | [`DeviceKey`] — generate from a TRNG, persist through the `Kv` seam, ECDH | |
13//! | [`signer`] | [`DeviceSigner`] — **byte-for-byte** the trait `mid`'s `kms-client` defines | `kms-client::DeviceSigner` |
14//! | [`kms`] | the `NonceEnvelope` canonical form and the `SignedAssertion` a gateway verifies | `kms-types`, `kms-verifier` |
15//! | [`jws`] · [`roster`] · [`token`] | JWS ES256, the self-signed genesis roster, the self-issued mID token | `mid-issuer`, `mid-verify` |
16//! | [`cap`] | `component:action@scope` matching | `mata-cap` |
17//! | [`adoption`] | the owner-signed **Adoption** grant: who this device answers to | (Janus-defined) |
18//! | [`nonce`] | a bounded single-use nonce window | |
19//! | [`manifest`] | signing the `rusty_esp_core` capability manifest | |
20//! | [`setup`] | the setup session's Reply signature and stored records (enc-ble) | `rusty_esp_signal-core::setup` |
21//!
22//! Every canonical byte form that the `mid` repository defines is reproduced
23//! here exactly and gated by tests against the real `mid` crates on the host
24//! (`tests/oracle.rs`). Formats this crate *defines* (adoption, manifest
25//! signature) use the same discipline: a domain separator, length prefixes,
26//! big-endian integers, SHA-256, low-s ECDSA.
27//!
28//! Feature ladder: `std` ⊃ `alloc` ⊃ core-only. The core-only rung is what a
29//! `no_std` firmware without a heap uses: keys, signatures, the borrowed kms
30//! canonical form, adoption, the nonce window, manifest signing. `alloc` adds
31//! the JSON envelopes, JWS and the self-issued token.
32//!
33//! What is deliberately not here: any driver, NVS encryption (the `-esp`
34//! backend's guarantee to state), a roster-chain verifier (the upstream `mid`
35//! no_std refactor's job), ed25519 (the family has none).
36
37#[cfg(feature = "alloc")]
38extern crate alloc;
39
40pub mod adoption;
41pub mod cap;
42pub mod did;
43pub mod key;
44pub mod kms;
45pub mod manifest;
46pub mod nonce;
47pub mod seal;
48#[cfg(feature = "alloc")]
49pub mod settings_adoption;
50pub mod setup;
51pub mod signer;
52pub mod status;
53
54#[cfg(feature = "alloc")]
55pub mod jws;
56#[cfg(feature = "alloc")]
57pub mod roster;
58#[cfg(feature = "alloc")]
59pub mod token;
60
61pub use adoption::{Adoption, AdoptionFields, OwnerPin};
62pub use did::Did;
63pub use key::{DeviceId, DeviceKey};
64pub use nonce::NonceWindow;
65pub use rusty_esp_core as esp_core;
66pub use signer::{DeviceSigner, verify_prehash};
67
68/// The names a firmware or a backend wants in scope.
69pub mod prelude {
70    pub use crate::adoption::{Adoption, AdoptionFields, OwnerPin};
71    pub use crate::did::Did;
72    pub use crate::key::{DeviceId, DeviceKey};
73    pub use crate::nonce::NonceWindow;
74    pub use crate::signer::DeviceSigner;
75    pub use rusty_esp_core::prelude::*;
76}
77
78/// Crate version, for capability manifests and logs.
79pub const VERSION: &str = env!("CARGO_PKG_VERSION");
80
81/// The verification-method type string mID uses for every P-256 key.
82pub const VM_TYPE_ECDSA_P256: &str = "EcdsaSecp256r1VerificationKey2019";
83
84/// SHA-256 of `data`, the prehash every signature in this crate is over.
85#[must_use]
86pub fn sha256(data: &[u8]) -> [u8; 32] {
87    use sha2::Digest;
88    sha2::Sha256::digest(data).into()
89}