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
use super::Error;
use ibe::kem::IBKEM;
use ibe::Derive;
use serde::{Deserialize, Serialize};
use tiny_keccak::{Hasher, Sha3};
const IDENTITY_UNSET: u64 = u64::MAX;
const MAX_CON: usize = (IDENTITY_UNSET as usize - 1) >> 1;
#[derive(Serialize, Deserialize, Debug, Ord, PartialOrd, PartialEq, Eq, Clone, Default)]
pub struct Attribute {
#[serde(rename = "t")]
pub atype: String,
#[serde(rename = "v")]
pub value: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Default)]
pub struct Policy {
#[serde(rename = "ts")]
pub timestamp: u64,
pub con: Vec<Attribute>,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Default)]
pub struct HiddenAttribute {
#[serde(rename = "t")]
pub atype: String,
#[serde(rename = "v")]
pub hidden_value: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Default)]
pub struct HiddenPolicy {
#[serde(rename = "ts")]
pub timestamp: u64,
pub con: Vec<HiddenAttribute>,
}
impl Policy {
pub fn to_hidden(&self) -> HiddenPolicy {
HiddenPolicy {
timestamp: self.timestamp,
con: self
.con
.iter()
.map(|a| HiddenAttribute {
atype: a.atype.clone(),
hidden_value: Some("".to_string()),
})
.collect(),
}
}
}
impl Policy {
pub fn derive<K: IBKEM>(&self) -> Result<<K as IBKEM>::Id, Error> {
if self.con.len() > MAX_CON {
return Err(Error::ConstraintViolation);
}
let mut tmp = [0u8; 64];
let mut pre_h = Sha3::v512();
pre_h.update(&[0x00]);
let mut copy = self.con.clone();
copy.sort();
for (i, ar) in copy.iter().enumerate() {
let mut f = Sha3::v512();
f.update(&((2 * i + 1) as u64).to_be_bytes());
let at_bytes = ar.atype.as_bytes();
f.update(&(at_bytes.len() as u64).to_be_bytes());
f.update(at_bytes);
f.finalize(&mut tmp);
pre_h.update(&tmp);
f = Sha3::v512();
f.update(&((2 * i + 2) as u64).to_be_bytes());
match &ar.value {
None => f.update(&IDENTITY_UNSET.to_be_bytes()),
Some(val) => {
let val_bytes = val.as_bytes();
f.update(&(val_bytes.len() as u64).to_be_bytes());
f.update(val_bytes);
}
}
f.finalize(&mut tmp);
pre_h.update(&tmp);
}
pre_h.update(&self.timestamp.to_be_bytes());
pre_h.finalize(&mut tmp);
Ok(<K as IBKEM>::Id::derive(&tmp))
}
}
impl Attribute {
pub fn new(atype: &str, value: Option<&str>) -> Self {
let atype = atype.to_owned();
let value = value.map(|s| s.to_owned());
Attribute { atype, value }
}
}
#[cfg(test)]
mod tests {
use crate::test_common::TestSetup;
use crate::Policy;
use ibe::kem::cgw_kv::CGWKV;
#[test]
fn test_ordering() {
let setup = TestSetup::default();
let policies: Vec<Policy> = setup.policies.into_values().collect();
let p1_derived = policies[1].derive::<CGWKV>().unwrap();
let mut reversed = policies[1].clone();
reversed.con.reverse();
assert_eq!(&p1_derived, &reversed.derive::<CGWKV>().unwrap());
reversed.timestamp += 1;
assert_ne!(&p1_derived, &reversed.derive::<CGWKV>().unwrap());
}
}