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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use digest::Digest;
use tor_llcrypto as ll;
use tor_netdoc::doc::{
authcert::{AuthCert, AuthCertKeyIds},
netstatus::{Lifetime, MdConsensus, UnvalidatedMdConsensus},
};
use std::time::SystemTime;
#[derive(Debug, Clone)]
pub(crate) struct ConsensusMeta {
lifetime: Lifetime,
sha3_256_of_signed: [u8; 32],
sha3_256_of_whole: [u8; 32],
}
impl ConsensusMeta {
pub(crate) fn new(
lifetime: Lifetime,
sha3_256_of_signed: [u8; 32],
sha3_256_of_whole: [u8; 32],
) -> Self {
ConsensusMeta {
lifetime,
sha3_256_of_signed,
sha3_256_of_whole,
}
}
pub(crate) fn from_unvalidated(
signed_part: &str,
remainder: &str,
con: &UnvalidatedMdConsensus,
) -> Self {
let lifetime = con.peek_lifetime().clone();
let (sd, wd) = sha3_dual(signed_part, remainder);
ConsensusMeta::new(lifetime, sd, wd)
}
#[allow(unused)]
pub(crate) fn from_consensus(signed_part: &str, remainder: &str, con: &MdConsensus) -> Self {
let lifetime = con.lifetime().clone();
let (sd, wd) = sha3_dual(signed_part, remainder);
ConsensusMeta::new(lifetime, sd, wd)
}
pub(crate) fn lifetime(&self) -> &Lifetime {
&self.lifetime
}
pub(crate) fn sha3_256_of_signed(&self) -> &[u8; 32] {
&self.sha3_256_of_signed
}
pub(crate) fn sha3_256_of_whole(&self) -> &[u8; 32] {
&self.sha3_256_of_whole
}
}
fn sha3_dual(signed_part: impl AsRef<[u8]>, remainder: impl AsRef<[u8]>) -> ([u8; 32], [u8; 32]) {
let mut d = ll::d::Sha3_256::new();
d.update(signed_part.as_ref());
let sha3_of_signed = d.clone().finalize().into();
d.update(remainder.as_ref());
let sha3_of_whole = d.finalize().into();
(sha3_of_signed, sha3_of_whole)
}
#[derive(Clone, Debug)]
pub(crate) struct AuthCertMeta {
ids: AuthCertKeyIds,
published: SystemTime,
expires: SystemTime,
}
impl AuthCertMeta {
pub(crate) fn new(ids: AuthCertKeyIds, published: SystemTime, expires: SystemTime) -> Self {
AuthCertMeta {
ids,
published,
expires,
}
}
pub(crate) fn from_authcert(cert: &AuthCert) -> Self {
AuthCertMeta::new(*cert.key_ids(), cert.published(), cert.expires())
}
pub(crate) fn key_ids(&self) -> &AuthCertKeyIds {
&self.ids
}
pub(crate) fn published(&self) -> SystemTime {
self.published
}
pub(crate) fn expires(&self) -> SystemTime {
self.expires
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn t_sha3_dual() {
let s = b"Loarax ipsum gruvvulus thneed amet, snergelly once-ler lerkim, sed do barbaloot tempor gluppitus ut labore et truffula magna aliqua. Ut enim ad grickle-grass veniam, quis miff-muffered ga-zumpco laboris nisi ut cruffulus ex ea schloppity consequat. Duis aute snarggle in swomeeswans in voluptate axe-hacker esse rippulus crummii eu moof nulla snuvv.";
let sha3_of_whole: [u8; 32] = ll::d::Sha3_256::digest(s).into();
for idx in 0..s.len() {
let sha3_of_part: [u8; 32] = ll::d::Sha3_256::digest(&s[..idx]).into();
let (a, b) = sha3_dual(&s[..idx], &s[idx..]);
assert_eq!(a, sha3_of_part);
assert_eq!(b, sha3_of_whole);
}
}
}