core_crypto/
obfuscate.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use crate::prelude::{ClientId, ConversationId};
use derive_more::{Constructor, From};
use hex;
use log::kv::{ToValue, Value};
use openmls::framing::Sender;
use openmls::group::QueuedProposal;
use openmls::prelude::Proposal;
use sha2::{Digest, Sha256};
use std::fmt::{Debug, Formatter};

pub(crate) trait Obfuscate {
    fn obfuscate(&self, f: &mut Formatter<'_>) -> core::fmt::Result;
}

impl Obfuscate for &ConversationId {
    fn obfuscate(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_str(hex::encode(compute_hash(self)).as_str())
    }
}

impl Obfuscate for &ClientId {
    fn obfuscate(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_str(hex::encode(compute_hash(self)).as_str())
    }
}

impl Obfuscate for &Proposal {
    fn obfuscate(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_str(match self {
            Proposal::Add(_) => "Add",
            Proposal::Update(_) => "Update",
            Proposal::Remove(_) => "Remove",
            Proposal::PreSharedKey(_) => "PreSharedKey",
            Proposal::ReInit(_) => "ReInit",
            Proposal::ExternalInit(_) => "ExternalInit",
            Proposal::AppAck(_) => "AppAck",
            Proposal::GroupContextExtensions(_) => "GroupContextExtensions",
        })
    }
}

impl Obfuscate for &QueuedProposal {
    fn obfuscate(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        (&self.proposal).obfuscate(f)
    }
}

impl Obfuscate for &Sender {
    fn obfuscate(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        match self {
            Sender::Member(leaf_node_index) => write!(f, "Member{leaf_node_index}"),
            Sender::External(external_sender_index) => write!(f, "External{external_sender_index:?}"),
            Sender::NewMemberProposal => write!(f, "NewMemberProposal"),
            Sender::NewMemberCommit => write!(f, "NewMemberCommit"),
        }
    }
}

#[derive(From, Constructor)]
pub(crate) struct Obfuscated<T>(T);

impl<T> Debug for Obfuscated<T>
where
    T: Obfuscate,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        self.0.obfuscate(f)
    }
}

impl<T> ToValue for Obfuscated<T>
where
    T: Obfuscate,
{
    fn to_value(&self) -> Value {
        Value::from_debug(self)
    }
}

fn compute_hash(bytes: &[u8]) -> [u8; 10] {
    let mut hasher = Sha256::new();
    let mut output = [0; 10];
    hasher.update(bytes);
    output.copy_from_slice(&hasher.finalize().as_slice()[0..10]);
    output
}