-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
314 lines (261 loc) · 10.6 KB
/
example.py
File metadata and controls
314 lines (261 loc) · 10.6 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# -*- coding: utf-8 -*-
import asyncio
from cloudproof_py.cover_crypt import Attribute
from cloudproof_py.cover_crypt import CoverCrypt
from cloudproof_py.cover_crypt import Policy
from cloudproof_py.cover_crypt import PolicyAxis
from cloudproof_py.cover_crypt import UserSecretKey
async def main(use_kms: bool = True):
"""Usage example of Cover Crypt
Keys generation, encryption and decryption are done locally."""
# Creating Policy
policy = Policy()
policy.add_axis(
PolicyAxis(
"Security Level",
[
("Protected", False),
("Confidential", False),
# the following attribute is hybridized allowing post-quantum resistance
("Top Secret", True),
],
hierarchical=True, # this is a hierarchical axis
)
)
policy.add_axis(
PolicyAxis(
"Department",
[("FIN", False), ("MKG", False), ("HR", False)],
hierarchical=False, # this is NOT a hierarchical axis
)
)
# Generating master keys
cover_crypt = CoverCrypt()
master_private_key, public_key = cover_crypt.generate_master_keys(policy)
# Messages encryption
protected_mkg_data = b"protected_mkg_message"
protected_mkg_ciphertext = cover_crypt.encrypt(
policy,
"Department::MKG && Security Level::Protected",
public_key,
protected_mkg_data,
)
top_secret_mkg_data = b"top_secret_mkg_message"
top_secret_mkg_ciphertext = cover_crypt.encrypt(
policy,
"Department::MKG && Security Level::Top Secret",
public_key,
top_secret_mkg_data,
)
protected_fin_data = b"protected_fin_message"
protected_fin_ciphertext = cover_crypt.encrypt(
policy,
"Department::FIN && Security Level::Protected",
public_key,
protected_fin_data,
)
# Generating user keys
confidential_mkg_user_key = cover_crypt.generate_user_secret_key(
master_private_key,
"Department::MKG && Security Level::Confidential",
policy,
)
topSecret_mkg_fin_user_key = cover_crypt.generate_user_secret_key(
master_private_key,
"(Department::MKG || Department::FIN) && Security Level::Top Secret",
policy,
)
# Decryption with the right access policy
protected_mkg_plaintext, _ = cover_crypt.decrypt(
confidential_mkg_user_key, protected_mkg_ciphertext
)
assert protected_mkg_plaintext == protected_mkg_data
# Decryption without the right access will fail
try:
# will throw
cover_crypt.decrypt(confidential_mkg_user_key, top_secret_mkg_ciphertext)
except Exception as e:
# ==> the user is not be able to decrypt
print("Expected error:", e)
try:
# will throw
cover_crypt.decrypt(confidential_mkg_user_key, protected_fin_ciphertext)
except Exception as e:
# ==> the user is not be able to decrypt
print("Expected error:", e)
# User with Top Secret access can decrypt messages
# of all Security Level within the right Department
protected_mkg_plaintext2, _ = cover_crypt.decrypt(
topSecret_mkg_fin_user_key, protected_mkg_ciphertext
)
assert protected_mkg_plaintext2 == protected_mkg_data
topSecret_mkg_plaintext, _ = cover_crypt.decrypt(
topSecret_mkg_fin_user_key, top_secret_mkg_ciphertext
)
assert topSecret_mkg_plaintext == top_secret_mkg_data
protected_fin_plaintext, _ = cover_crypt.decrypt(
topSecret_mkg_fin_user_key, protected_fin_ciphertext
)
assert protected_fin_plaintext == protected_fin_data
# Rekey
# make a copy of the current user key
old_confidential_mkg_user_key = UserSecretKey.from_bytes(
confidential_mkg_user_key.to_bytes()
)
# Rekey MKG attribute
cover_crypt.rekey_master_keys(
"Department::MKG", policy, master_private_key, public_key
)
# update user key
cover_crypt.refresh_user_secret_key(
confidential_mkg_user_key,
master_private_key,
keep_old_accesses=True,
)
# New confidential marketing message
confidential_mkg_data = b"confidential_secret_mkg_message"
confidential_mkg_ciphertext = cover_crypt.encrypt(
policy,
"Department::MKG && Security Level::Confidential",
public_key,
confidential_mkg_data,
)
# Decrypting the messages with the rekeyed key
# decrypting the "old" `protected marketing` message
old_protected_mkg_plaintext, _ = cover_crypt.decrypt(
confidential_mkg_user_key, protected_mkg_ciphertext
)
assert old_protected_mkg_plaintext == protected_mkg_data
# decrypting the "new" `confidential marketing` message
new_confidential_mkg_plaintext, _ = cover_crypt.decrypt(
confidential_mkg_user_key, confidential_mkg_ciphertext
)
assert new_confidential_mkg_plaintext == confidential_mkg_data
# Decrypting the messages with the NON rekeyed key
# decrypting the "old" `protected marketing` message with the old key works
old_protected_mkg_plaintext, _ = cover_crypt.decrypt(
old_confidential_mkg_user_key, protected_mkg_ciphertext
)
assert old_protected_mkg_plaintext == protected_mkg_data
# decrypting the "new" `confidential marketing` message with the old key fails
try:
cover_crypt.decrypt(old_confidential_mkg_user_key, confidential_mkg_ciphertext)
except Exception as e:
# ==> the user is not be able to decrypt
print("Expected error:", e)
# Prune: remove old keys for the MKG attribute
cover_crypt.prune_master_secret_key("Department::MKG", policy, master_private_key)
# update user key
cover_crypt.refresh_user_secret_key(
confidential_mkg_user_key,
master_private_key,
keep_old_accesses=True, # will not keep removed rotations
)
# decrypting the "old" `protected marketing` message will fail
try:
cover_crypt.decrypt(confidential_mkg_user_key, protected_mkg_ciphertext)
except Exception as e:
# ==> the user is not be able to decrypt
print("Expected error:", e)
# decrypting the "new" `confidential marketing` message will still work
new_confidential_mkg_plaintext, _ = cover_crypt.decrypt(
confidential_mkg_user_key, confidential_mkg_ciphertext
)
assert new_confidential_mkg_plaintext == confidential_mkg_data
# Edit policy
# Addition
policy.add_attribute(Attribute("Department", "R&D"), is_hybridized=False)
# hierarchical axis are immutable (no addition nor deletion allowed)
try:
policy.add_attribute(Attribute("Security Level", "Classified"), False)
except Exception as e:
print("Expected error:", e)
# new attributes can be used after updating the master keys
cover_crypt.update_master_keys(policy, master_private_key, public_key)
protected_rd_data = b"top_secret_mkg_message"
protected_rd_ciphertext = cover_crypt.encrypt(
policy,
"Department::R&D && Security Level::Protected",
public_key,
protected_rd_data,
)
confidential_rd_fin_user_key = cover_crypt.generate_user_secret_key(
master_private_key,
"(Department::R&D || Department::FIN) && Security Level::Confidential",
policy,
)
protected_rd_plaintext, _ = cover_crypt.decrypt(
confidential_rd_fin_user_key, protected_rd_ciphertext
)
assert protected_rd_plaintext == protected_rd_data
# Rename attribute "Department::MKG" to "Department::Marketing"
policy.rename_attribute(Attribute("Department", "MKG"), "Marketing")
# Encryption and decryption work the same even with previously generated keys and ciphers
confidential_mkg_plaintext, _ = cover_crypt.decrypt(
confidential_mkg_user_key, confidential_mkg_ciphertext
)
assert confidential_mkg_plaintext == confidential_mkg_data
# Removing access to an attribute
# 1 - Keep decryption access to ciphertext from old attributes but remove the right to encrypt new data
policy.disable_attribute(Attribute("Department", "R&D"))
# this method can also be used on hierarchical axis
policy.disable_attribute(Attribute("Security Level", "Protected"))
# after updating the keys, disabled attributes can no longer be used to encrypt data
cover_crypt.update_master_keys(policy, master_private_key, public_key)
cover_crypt.refresh_user_secret_key(
confidential_rd_fin_user_key,
master_private_key,
keep_old_accesses=True,
)
# New data encryption for `Department::R&D` will fail
try:
cover_crypt.encrypt(
policy,
"Department::R&D && Security Level::Protected",
public_key,
protected_rd_data,
)
except Exception as e:
print("Expected error:", e)
# Decryption of old ciphertext is still possible
new_protected_rd_plaintext, _ = cover_crypt.decrypt(
confidential_rd_fin_user_key, protected_rd_ciphertext
)
assert new_protected_rd_plaintext == protected_rd_data
# 2 - Complete removing of an attribute
# /!\ this operation is irreversible and may cause data loss
policy.remove_attribute(Attribute("Department", "R&D"))
# removing attribute from hierarchical axis is prohibited
try:
policy.remove_attribute(Attribute("Security Level", "Protected"))
except Exception as e:
print("Expected error:", e)
# after updating the keys, removed attributes can no longer be used to encrypt or decrypt
cover_crypt.update_master_keys(policy, master_private_key, public_key)
cover_crypt.refresh_user_secret_key(
confidential_rd_fin_user_key,
master_private_key,
keep_old_accesses=True,
)
try:
cover_crypt.decrypt(confidential_rd_fin_user_key, protected_rd_ciphertext)
except Exception as e:
print("Expected error:", e)
# 3 - Removing an entire axis
# /!\ this operation is irreversible and may cause data loss
# any type of axis can be removed
policy.remove_axis("Security Level")
# updating the keys will remove all access to previous ciphertext encrypted for `Security Level`
cover_crypt.update_master_keys(policy, master_private_key, public_key)
try:
cover_crypt.generate_user_secret_key(
master_private_key,
"Department::FIN && Security Level::Confidential", # `Security Level` can no longer be used here
policy,
)
except Exception as e:
print("Expected error:", e)
if __name__ == "__main__":
loop = asyncio.new_event_loop()
loop.run_until_complete(main())
loop.close()