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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use super::{
system::{KeyedSig, SigShare},
Error, Result,
};
use crate::{
messaging::signature_aggregator::{Error as AggregatorError, SignatureAggregator},
routing::SectionKeyShare,
types::{PublicKey, Signature},
};
use bls::PublicKey as BlsPublicKey;
use ed25519_dalek::{
Keypair as EdKeypair, PublicKey as EdPublicKey, Signature as EdSignature, Signer as _,
Verifier as _,
};
use serde::{Deserialize, Serialize};
use xor_name::XorName;
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct ServiceAuth {
pub public_key: PublicKey,
pub signature: Signature,
}
#[derive(Clone, Eq, PartialEq, custom_debug::Debug, serde::Deserialize, serde::Serialize)]
pub struct NodeAuth {
pub section_pk: BlsPublicKey,
#[debug(with = "PublicKey::fmt_ed25519")]
pub public_key: EdPublicKey,
#[debug(with = "Signature::fmt_ed25519")]
#[serde(with = "serde_bytes")]
pub signature: EdSignature,
}
impl NodeAuth {
pub(crate) fn authorize(
section_pk: BlsPublicKey,
keypair: &EdKeypair,
payload: impl AsRef<[u8]>,
) -> AuthorityProof<Self> {
AuthorityProof(NodeAuth {
section_pk,
public_key: keypair.public,
signature: keypair.sign(payload.as_ref()),
})
}
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct BlsShareAuth {
pub section_pk: BlsPublicKey,
pub src_name: XorName,
pub sig_share: SigShare,
}
impl BlsShareAuth {
pub(crate) fn authorize(
section_pk: BlsPublicKey,
src_name: XorName,
key_share: &SectionKeyShare,
payload: impl AsRef<[u8]>,
) -> AuthorityProof<Self> {
AuthorityProof(BlsShareAuth {
section_pk,
src_name,
sig_share: SigShare {
public_key_set: key_share.public_key_set.clone(),
index: key_share.index,
signature_share: key_share.secret_key_share.sign(payload),
},
})
}
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct SectionAuth {
pub src_name: XorName,
pub sig: KeyedSig,
}
impl SectionAuth {
pub(crate) async fn try_authorize(
aggregator: SignatureAggregator,
share: BlsShareAuth,
payload: impl AsRef<[u8]>,
) -> Result<AuthorityProof<Self>, AggregatorError> {
let sig = aggregator
.add(payload.as_ref(), share.sig_share.clone())
.await?;
if share.sig_share.public_key_set.public_key() != sig.public_key {
return Err(AggregatorError::InvalidShare);
}
if sig.public_key != share.section_pk {
return Err(AggregatorError::InvalidShare);
}
Ok(AuthorityProof(SectionAuth {
src_name: share.src_name,
sig,
}))
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct AuthorityProof<T>(T);
impl<T: VerifyAuthority> AuthorityProof<T> {
pub fn verify(inner: T, payload: impl AsRef<[u8]>) -> Result<Self> {
inner.verify_authority(payload).map(Self)
}
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> core::ops::Deref for AuthorityProof<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub trait VerifyAuthority: Sized + sealed::Sealed {
fn verify_authority(self, payload: impl AsRef<[u8]>) -> Result<Self>;
}
impl VerifyAuthority for ServiceAuth {
fn verify_authority(self, payload: impl AsRef<[u8]>) -> Result<Self> {
self.public_key
.verify(&self.signature, payload)
.map_err(|_| Error::InvalidSignature)?;
Ok(self)
}
}
impl sealed::Sealed for ServiceAuth {}
impl VerifyAuthority for NodeAuth {
fn verify_authority(self, payload: impl AsRef<[u8]>) -> Result<Self> {
self.public_key
.verify(payload.as_ref(), &self.signature)
.map_err(|_| Error::InvalidSignature)?;
Ok(self)
}
}
impl sealed::Sealed for NodeAuth {}
impl VerifyAuthority for BlsShareAuth {
fn verify_authority(self, payload: impl AsRef<[u8]>) -> Result<Self> {
if self.sig_share.public_key_set.public_key() != self.section_pk {
return Err(Error::InvalidSignature);
}
if !self.sig_share.verify(payload.as_ref()) {
return Err(Error::InvalidSignature);
}
Ok(self)
}
}
impl sealed::Sealed for BlsShareAuth {}
impl VerifyAuthority for SectionAuth {
fn verify_authority(self, payload: impl AsRef<[u8]>) -> Result<Self> {
if !self.sig.public_key.verify(&self.sig.signature, payload) {
return Err(Error::InvalidSignature);
}
Ok(self)
}
}
impl sealed::Sealed for SectionAuth {}
mod sealed {
#[allow(missing_docs, unreachable_pub)]
pub trait Sealed {}
}