Skip to main content

hyprstream_rpc/crypto/
broadcast_primitives.rs

1//! Broadcast crypto primitives preserved from the removed NotificationService.
2//!
3//! These are reused by the WASM client (`wasm_api.rs`) and remain public so the
4//! browser binding is unaffected by the NotificationService removal (EV5/#605).
5//! The blind-relay broadcast service itself is gone; only the reusable crypto
6//! helpers (pubkey fingerprint, length-prefixed AAD, one-shot MAC, attestation
7//! message) remain here.
8
9use subtle::ConstantTimeEq;
10
11use super::backend::keyed_mac;
12use crate::error::{EnvelopeError, EnvelopeResult};
13
14/// 128-bit pubkey fingerprint (Blake3 truncated).
15pub type PubkeyFingerprint = [u8; 16];
16
17/// Compute a 128-bit fingerprint of a public key: `Blake3(pubkey)[..16]`.
18pub fn pubkey_fingerprint(pubkey_bytes: &[u8; 32]) -> PubkeyFingerprint {
19    let hash = blake3::hash(pubkey_bytes);
20    let mut fp = [0u8; 16];
21    fp.copy_from_slice(&hash.as_bytes()[..16]);
22    fp
23}
24
25/// Build length-prefixed AAD for payload encryption.
26///
27/// Format: `u32_le(len(intent_id)) || intent_id || u32_le(len(scope)) || scope`.
28/// Length prefixing prevents ambiguity where different intent_id/scope splits
29/// produce the same concatenated bytes.
30pub fn build_payload_aad(intent_id: &str, scope: &str) -> Vec<u8> {
31    let mut aad = Vec::with_capacity(8 + intent_id.len() + scope.len());
32    aad.extend_from_slice(&(intent_id.len() as u32).to_le_bytes());
33    aad.extend_from_slice(intent_id.as_bytes());
34    aad.extend_from_slice(&(scope.len() as u32).to_le_bytes());
35    aad.extend_from_slice(scope.as_bytes());
36    aad
37}
38
39/// Compute a one-shot MAC: `keyed_mac(mac_key, ciphertext)`.
40pub fn notification_mac(mac_key: &[u8; 32], ciphertext: &[u8]) -> [u8; 32] {
41    keyed_mac(mac_key, ciphertext)
42}
43
44/// Verify a one-shot MAC in constant time.
45pub fn notification_mac_verify(
46    mac_key: &[u8; 32],
47    ciphertext: &[u8],
48    expected_mac: &[u8; 32],
49) -> EnvelopeResult<()> {
50    let computed = keyed_mac(mac_key, ciphertext);
51    if bool::from(computed.ct_eq(expected_mac)) {
52        Ok(())
53    } else {
54        Err(EnvelopeError::MacVerification)
55    }
56}
57
58/// Build the message signed by the publisher's Ed25519 key for attestation.
59///
60/// Format: `ephemeral_pubkey || blinded_sub_pubkey || u32_le(len(scope)) ||
61/// scope || u32_le(len(intent_id)) || intent_id`. Fixed-length pubkeys are not
62/// length-prefixed; variable-length fields are, to prevent concatenation
63/// ambiguity.
64pub fn build_attestation_message(
65    ephemeral_pubkey: &[u8; 32],
66    blinded_sub_pubkey: &[u8; 32],
67    scope: &str,
68    intent_id: &str,
69) -> Vec<u8> {
70    let mut msg = Vec::with_capacity(64 + 8 + scope.len() + intent_id.len());
71    msg.extend_from_slice(ephemeral_pubkey);
72    msg.extend_from_slice(blinded_sub_pubkey);
73    msg.extend_from_slice(&(scope.len() as u32).to_le_bytes());
74    msg.extend_from_slice(scope.as_bytes());
75    msg.extend_from_slice(&(intent_id.len() as u32).to_le_bytes());
76    msg.extend_from_slice(intent_id.as_bytes());
77    msg
78}