roughtime 0.1.0

A no_std-capable Roughtime secure time-sync client with pluggable crypto backends
Documentation
//! Demonstrates the crate's `no_std`-capable core surface: building a request and verifying a
//! response with **zero networking and zero OS dependency** — the exact API a bootloader, UEFI
//! app, or kernel would use, supplying its own transport (e.g. a UEFI network stack, a
//! hypervisor virtio-net queue) and entropy (e.g. RDRAND, a hardware TRNG) in place of `std`.
//!
//! This example binary itself still runs as an ordinary host process (so it can `println!` and
//! use `std::vec`/heap for the canned bytes below) — but the `roughtime` *library* dependency is
//! compiled genuinely `no_std`: `--no-default-features --features rustcrypto` disables every
//! std-only module (networking clients, `DNSCrypt` resolution, the Linux `os-clock` backend). Only
//! [`roughtime::request::build_request`] and [`roughtime::verify_response`] are used below, and
//! neither one touches `std`.
//!
//! Since there's no real server to round-trip with here, `SERVER_RESPONSE_BYTES` is a canned,
//! validly Ed25519-signed Roughtime response (for `NONCE`) — in a real bootloader this byte
//! buffer is instead whatever its transport received back.
//!
//! Run with: `cargo run --example no_std --no-default-features --features rustcrypto`

use roughtime::request::{Nonce, build_request};
use roughtime::verify_response;

/// Stand-in for hardware entropy (e.g. RDRAND) — a bare-metal caller supplies its own bytes via
/// [`Nonce::new`] rather than [`Nonce::random`], which requires the `std` feature.
const NONCE_BYTES: [u8; 64] = [0x2A; 64];

/// The root public key that signed `SERVER_RESPONSE_BYTES`, matching a real deployment's
/// hardcoded/pinned server key (see [`roughtime::servers::ROUGHTIME_SERVERS`]).
const SERVER_ROOT_PUBKEY_BASE64: &str = "iojj3XQJ8ZX9UtstPLpdcspnCb8dlBIb83SIAbQPb1w=";

/// A canned, validly-signed Roughtime response for `NONCE_BYTES`, standing in for whatever bytes
/// a bare-metal transport received back over the wire.
#[rustfmt::skip]
const SERVER_RESPONSE_BYTES: [u8; 328] = [
    0x05, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0xd8, 0x00, 0x00, 0x00,
    0xdc, 0x00, 0x00, 0x00, 0x53, 0x49, 0x47, 0x00, 0x50, 0x41, 0x54, 0x48, 0x43, 0x45, 0x52, 0x54,
    0x49, 0x4e, 0x44, 0x58, 0x53, 0x52, 0x45, 0x50, 0xf0, 0xd9, 0xd1, 0xc6, 0xd7, 0xe2, 0xe0, 0x36,
    0x56, 0x5e, 0xf6, 0x2d, 0x6a, 0x8c, 0x8c, 0xc5, 0x86, 0x1a, 0xbe, 0x31, 0x3a, 0x24, 0xd5, 0xc7,
    0xcd, 0x67, 0xc8, 0xc5, 0xf9, 0x78, 0xb3, 0x50, 0x63, 0x11, 0xce, 0x53, 0x79, 0xe0, 0x9c, 0x22,
    0x89, 0xf3, 0x87, 0xeb, 0x5c, 0x56, 0x09, 0x18, 0x8b, 0xb2, 0xfd, 0xe1, 0xd5, 0x00, 0x8e, 0x43,
    0xa5, 0xae, 0x6e, 0xd7, 0x5d, 0x89, 0xb4, 0x08, 0x02, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00,
    0x44, 0x45, 0x4c, 0x45, 0x53, 0x49, 0x47, 0x00, 0x03, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
    0x28, 0x00, 0x00, 0x00, 0x50, 0x55, 0x42, 0x4b, 0x4d, 0x49, 0x4e, 0x54, 0x4d, 0x41, 0x58, 0x54,
    0x81, 0x39, 0x77, 0x0e, 0xa8, 0x7d, 0x17, 0x5f, 0x56, 0xa3, 0x54, 0x66, 0xc3, 0x4c, 0x7e, 0xcc,
    0xcb, 0x8d, 0x8a, 0x91, 0xb4, 0xee, 0x37, 0xa2, 0x5d, 0xf6, 0x0f, 0x5b, 0x8f, 0xc9, 0xb3, 0x94,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0x50, 0xe4, 0xb1, 0x48, 0x80, 0x4c, 0x31, 0x3c, 0x37, 0x42, 0xcd, 0x98, 0x8b, 0xb2, 0xd4, 0x9f,
    0x8c, 0xfa, 0xaa, 0x38, 0x13, 0xa5, 0xe6, 0xa2, 0xf2, 0xca, 0x26, 0xb3, 0x97, 0xfe, 0x8a, 0xf7,
    0x3b, 0x65, 0x74, 0xbb, 0x4d, 0x8c, 0x0f, 0x49, 0x52, 0x52, 0x69, 0x0d, 0x21, 0x26, 0x82, 0x20,
    0xbd, 0x6f, 0xfe, 0x31, 0xef, 0x91, 0x17, 0x86, 0x3f, 0xb6, 0xf0, 0xaf, 0xeb, 0x8b, 0x0d, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00,
    0x52, 0x4f, 0x4f, 0x54, 0x4d, 0x49, 0x44, 0x50, 0x52, 0x41, 0x44, 0x49, 0x0f, 0x90, 0x55, 0x65,
    0xed, 0x29, 0x73, 0x63, 0x41, 0xa1, 0xcf, 0x6e, 0x9e, 0x03, 0x72, 0x96, 0xff, 0x32, 0xe0, 0xc0,
    0x33, 0xcf, 0x58, 0x12, 0x01, 0xe3, 0x80, 0x9a, 0x3f, 0xa1, 0x8f, 0xaf, 0x00, 0xc4, 0xfa, 0x35,
    0x82, 0x55, 0x06, 0x00, 0x40, 0x42, 0x0f, 0x00,
];

fn main() {
    // 1. Build a request. In a real bare-metal caller, `NONCE_BYTES` comes from a hardware RNG
    //    and the returned bytes are handed to whatever transport is available (UDP, a serial
    //    line to a co-processor, ...) — nothing here touches the network or the OS.
    let nonce = Nonce::new(NONCE_BYTES);
    let request_bytes = build_request(&nonce);
    println!("Built a {}-byte Roughtime request.", request_bytes.len());

    // 2. ... send `request_bytes` over your own transport and receive a response back ...
    //    (simulated here by the canned `SERVER_RESPONSE_BYTES`.)

    // 3. Verify the response: Ed25519 signatures, Merkle inclusion proof, and the anti-rollback
    //    floor are all checked with no I/O.
    match verify_response(&SERVER_RESPONSE_BYTES, &nonce, SERVER_ROOT_PUBKEY_BASE64) {
        Ok(verified) => {
            println!(
                "Verified Unix time: {}s (radius {}us)",
                verified.as_unix_seconds(),
                verified.radius_micros
            );
        }
        Err(err) => eprintln!("Verification failed: {err}"),
    }
}