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
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
use umbral_pre::{
Capsule, CapsuleFrag, PublicKey, SerializableToArray, Signature, Signer, VerifiedCapsuleFrag,
};
use crate::hrac::HRAC;
use crate::key_frag::EncryptedKeyFrag;
use crate::versioning::{
messagepack_deserialize, messagepack_serialize, ProtocolObject, ProtocolObjectInner,
};
#[derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct ReencryptionRequest {
pub capsules: Box<[Capsule]>,
pub hrac: HRAC,
pub encrypted_kfrag: EncryptedKeyFrag,
pub publisher_verifying_key: PublicKey,
pub bob_verifying_key: PublicKey,
}
impl ReencryptionRequest {
pub fn new(
capsules: &[Capsule],
hrac: &HRAC,
encrypted_kfrag: &EncryptedKeyFrag,
publisher_verifying_key: &PublicKey,
bob_verifying_key: &PublicKey,
) -> Self {
Self {
capsules: capsules.into(),
hrac: *hrac,
encrypted_kfrag: encrypted_kfrag.clone(),
publisher_verifying_key: *publisher_verifying_key,
bob_verifying_key: *bob_verifying_key,
}
}
}
impl<'a> ProtocolObjectInner<'a> for ReencryptionRequest {
fn brand() -> [u8; 4] {
*b"ReRq"
}
fn version() -> (u16, u16) {
(1, 0)
}
fn unversioned_to_bytes(&self) -> Box<[u8]> {
messagepack_serialize(&self)
}
fn unversioned_from_bytes(minor_version: u16, bytes: &[u8]) -> Option<Result<Self, String>> {
if minor_version == 0 {
Some(messagepack_deserialize(bytes))
} else {
None
}
}
}
impl<'a> ProtocolObject<'a> for ReencryptionRequest {}
#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
pub struct ReencryptionResponse {
cfrags: Box<[CapsuleFrag]>,
signature: Signature,
}
fn signed_message(capsules: &[Capsule], cfrags: &[CapsuleFrag]) -> Vec<u8> {
let capsule_bytes = capsules.iter().fold(Vec::<u8>::new(), |mut acc, capsule| {
acc.extend(capsule.to_array().as_ref());
acc
});
let cfrag_bytes = cfrags.iter().fold(Vec::<u8>::new(), |mut acc, cfrag| {
acc.extend(cfrag.to_array().as_ref());
acc
});
[capsule_bytes, cfrag_bytes].concat()
}
impl ReencryptionResponse {
pub fn new(
signer: &Signer,
capsules: &[Capsule],
vcfrags: impl IntoIterator<Item = VerifiedCapsuleFrag>,
) -> Self {
let cfrags: Vec<_> = vcfrags
.into_iter()
.map(|vcfrag| vcfrag.unverify())
.collect();
let signature = signer.sign(&signed_message(capsules, &cfrags));
ReencryptionResponse {
cfrags: cfrags.into_boxed_slice(),
signature,
}
}
pub fn verify(
&self,
capsules: &[Capsule],
alice_verifying_key: &PublicKey,
ursula_verifying_key: &PublicKey,
policy_encrypting_key: &PublicKey,
bob_encrypting_key: &PublicKey,
) -> Option<Box<[VerifiedCapsuleFrag]>> {
if capsules.len() != self.cfrags.len() {
return None;
}
if !self.signature.verify(
ursula_verifying_key,
&signed_message(capsules, &self.cfrags),
) {
return None;
}
let vcfrags = self
.cfrags
.iter()
.cloned()
.zip(capsules.iter())
.map(|(cfrag, capsule)| {
cfrag.verify(
capsule,
alice_verifying_key,
policy_encrypting_key,
bob_encrypting_key,
)
})
.collect::<Result<Vec<_>, _>>();
vcfrags.ok().map(|vcfrags| vcfrags.into_boxed_slice())
}
}
impl<'a> ProtocolObjectInner<'a> for ReencryptionResponse {
fn brand() -> [u8; 4] {
*b"ReRs"
}
fn version() -> (u16, u16) {
(1, 0)
}
fn unversioned_to_bytes(&self) -> Box<[u8]> {
messagepack_serialize(&self)
}
fn unversioned_from_bytes(minor_version: u16, bytes: &[u8]) -> Option<Result<Self, String>> {
if minor_version == 0 {
Some(messagepack_deserialize(bytes))
} else {
None
}
}
}
impl<'a> ProtocolObject<'a> for ReencryptionResponse {}