Skip to main content

oxabl_cache_crypto/
dictionary_pointer.rs

1//! The signed pointer to a cache's dictionary segment.
2
3use oxabl_cache_format::{CacheId, DictionaryPointer, SegmentId};
4
5use crate::domain::{SIG_DICTIONARY, prefixed};
6use crate::error::{Error, Result};
7use crate::identity::Identity;
8use crate::roster_verify::VerifiedRoster;
9use crate::signing;
10
11/// Signs a pointer to `dict_segment_id` as `signer`.
12pub fn sign_pointer(
13    signer: &Identity,
14    cache_id: CacheId,
15    epoch: u32,
16    version: u64,
17    dict_segment_id: SegmentId,
18) -> DictionaryPointer {
19    let mut pointer = DictionaryPointer {
20        cache_id,
21        epoch,
22        version,
23        dict_segment_id,
24        signer: signer.signing_public(),
25        signature: [0; 64],
26    };
27    pointer.signature = signer.sign(&prefixed(SIG_DICTIONARY, &pointer.signed_bytes()));
28    pointer
29}
30
31/// Verifies a pointer's signature and that its signer is a `ci` or `owner` key of the
32/// current roster, for this cache and epoch.
33pub fn verify_pointer(bytes: &[u8], roster: &VerifiedRoster) -> Result<DictionaryPointer> {
34    let pointer = DictionaryPointer::decode(bytes)?;
35    if pointer.cache_id != roster.cache_id() || pointer.epoch != roster.epoch() {
36        return Err(Error::Mismatch("dictionary pointer cache or epoch"));
37    }
38    signing::verify(
39        &pointer.signer,
40        &prefixed(SIG_DICTIONARY, &pointer.signed_bytes()),
41        &pointer.signature,
42    )?;
43    if !roster.is_ci_or_owner_key(&pointer.signer) {
44        return Err(Error::SignerNotAccepted);
45    }
46    Ok(pointer)
47}