-
Notifications
You must be signed in to change notification settings - Fork 96
/
decrypt_with_secrets.py
353 lines (305 loc) · 13.8 KB
/
decrypt_with_secrets.py
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from typing import List, Optional
from .ballot import (
CiphertextBallot,
CiphertextBallotContest,
CiphertextBallotSelection,
PlaintextBallot,
PlaintextBallotContest,
PlaintextBallotSelection,
)
from .elgamal import ElGamalPublicKey, ElGamalSecretKey
from .group import ElementModQ
from .logs import log_warning
from .manifest import (
InternalManifest,
ContestDescriptionWithPlaceholders,
SelectionDescription,
)
from .nonces import Nonces
from .utils import get_optional
# The Methods in this file can be used to decrypt values if private keys or nonces are known
def decrypt_selection_with_secret(
selection: CiphertextBallotSelection,
description: SelectionDescription,
public_key: ElGamalPublicKey,
secret_key: ElGamalSecretKey,
crypto_extended_base_hash: ElementModQ,
suppress_validity_check: bool = False,
) -> Optional[PlaintextBallotSelection]:
"""
Decrypt the specified `CiphertextBallotSelection` within the context of the specified selection.
:param selection: the selection to decrypt
:param description: the qualified selection metadata
:param public_key: the public key for the election (K)
:param secret_key: the known secret key used to generate the public key for this election
:param crypto_extended_base_hash: the extended base hash code (𝑄') for the election
:param suppress_validity_check: do not validate the encryption prior to decrypting (useful for tests)
"""
if not suppress_validity_check and not selection.is_valid_encryption(
description.crypto_hash(), public_key, crypto_extended_base_hash
):
log_warning(f"selection: {selection.object_id} failed validity check")
return None
plaintext_vote = selection.ciphertext.decrypt(secret_key)
# TODO: ISSUE #47: handle decryption of the extradata field if needed
return PlaintextBallotSelection(
selection.object_id,
plaintext_vote,
selection.is_placeholder_selection,
)
def decrypt_selection_with_nonce(
selection: CiphertextBallotSelection,
description: SelectionDescription,
public_key: ElGamalPublicKey,
crypto_extended_base_hash: ElementModQ,
nonce_seed: Optional[ElementModQ] = None,
suppress_validity_check: bool = False,
) -> Optional[PlaintextBallotSelection]:
"""
Decrypt the specified `CiphertextBallotSelection` within the context of the specified selection.
:param selection: the contest selection to decrypt
:param description: the qualified selection metadata that may be a placeholder selection
:param public_key: the public key for the election (K)
:param crypto_extended_base_hash: the extended base hash code (𝑄') for the election
:param nonce_seed: the optional nonce that was seeded to the encryption function.
if no value is provided, the nonce field from the selection is used
:param suppress_validity_check: do not validate the encryption prior to decrypting (useful for tests)
"""
if not suppress_validity_check and not selection.is_valid_encryption(
description.crypto_hash(), public_key, crypto_extended_base_hash
):
log_warning(f"selection: {selection.object_id} failed validity check")
return None
if nonce_seed is None:
nonce = selection.nonce
else:
nonce_sequence = Nonces(description.crypto_hash(), nonce_seed)
nonce = nonce_sequence[description.sequence_order]
if nonce is None:
log_warning(
f"missing nonce value. decrypt could not derive a nonce value for selection {selection.object_id}"
)
return None
if selection.nonce is not None and nonce != selection.nonce:
log_warning(
f"decrypt could not verify a nonce value for selection {selection.object_id}"
)
return None
plaintext_vote = selection.ciphertext.decrypt_known_nonce(public_key, nonce)
# TODO: ISSUE #35: encrypt/decrypt: handle decryption of the extradata field if needed
return PlaintextBallotSelection(
selection.object_id,
plaintext_vote,
selection.is_placeholder_selection,
)
def decrypt_contest_with_secret(
contest: CiphertextBallotContest,
description: ContestDescriptionWithPlaceholders,
public_key: ElGamalPublicKey,
secret_key: ElGamalSecretKey,
crypto_extended_base_hash: ElementModQ,
suppress_validity_check: bool = False,
remove_placeholders: bool = True,
) -> Optional[PlaintextBallotContest]:
"""
Decrypt the specified `CiphertextBallotContest` within the context of the specified contest.
:param contest: the contest to decrypt
:param description: the qualified contest metadata that includes placeholder selections
:param public_key: the public key for the election (K)
:param secret_key: the known secret key used to generate the public key for this election
:param crypto_extended_base_hash: the extended base hash code (𝑄') for the election
:param suppress_validity_check: do not validate the encryption prior to decrypting (useful for tests)
:param remove_placeholders: filter out placeholder ciphertext selections after decryption
"""
if not suppress_validity_check and not contest.is_valid_encryption(
description.crypto_hash(), public_key, crypto_extended_base_hash
):
log_warning(f"contest: {contest.object_id} failed validity check")
return None
plaintext_selections: List[PlaintextBallotSelection] = []
for selection in contest.ballot_selections:
selection_description = description.selection_for(selection.object_id)
plaintext_selection = decrypt_selection_with_secret(
selection,
get_optional(selection_description),
public_key,
secret_key,
crypto_extended_base_hash,
suppress_validity_check,
)
if plaintext_selection is not None:
if (
not remove_placeholders
or not plaintext_selection.is_placeholder_selection
):
plaintext_selections.append(plaintext_selection)
else:
log_warning(
f"decryption with secret failed for contest: {contest.object_id} selection: {selection.object_id}"
)
return None
return PlaintextBallotContest(contest.object_id, plaintext_selections)
def decrypt_contest_with_nonce(
contest: CiphertextBallotContest,
description: ContestDescriptionWithPlaceholders,
public_key: ElGamalPublicKey,
crypto_extended_base_hash: ElementModQ,
nonce_seed: Optional[ElementModQ] = None,
suppress_validity_check: bool = False,
remove_placeholders: bool = True,
) -> Optional[PlaintextBallotContest]:
"""
Decrypt the specified `CiphertextBallotContest` within the context of the specified contest.
:param contest: the contest to decrypt
:param description: the qualified contest metadata that includes placeholder selections
:param public_key: the public key for the election (K)
:param crypto_extended_base_hash: the extended base hash code (𝑄') for the election
:param nonce_seed: the optional nonce that was seeded to the encryption function
if no value is provided, the nonce field from the contest is used
:param suppress_validity_check: do not validate the encryption prior to decrypting (useful for tests)
:param remove_placeholders: filter out placeholder ciphertext selections after decryption
"""
if not suppress_validity_check and not contest.is_valid_encryption(
description.crypto_hash(), public_key, crypto_extended_base_hash
):
log_warning(f"contest: {contest.object_id} failed validity check")
return None
if nonce_seed is None:
nonce_seed = contest.nonce
else:
nonce_sequence = Nonces(description.crypto_hash(), nonce_seed)
nonce_seed = nonce_sequence[description.sequence_order]
if nonce_seed is None:
log_warning(
f"missing nonce_seed value. decrypt could not dewrive a nonce value for contest {contest.object_id}"
)
return None
if contest.nonce is not None and nonce_seed != contest.nonce:
log_warning(
f"decrypt could not verify a nonce_seed value for contest {contest.object_id}"
)
return None
plaintext_selections: List[PlaintextBallotSelection] = []
for selection in contest.ballot_selections:
selection_description = description.selection_for(selection.object_id)
plaintext_selection = decrypt_selection_with_nonce(
selection,
get_optional(selection_description),
public_key,
crypto_extended_base_hash,
nonce_seed,
suppress_validity_check,
)
if plaintext_selection is not None:
if (
not remove_placeholders
or not plaintext_selection.is_placeholder_selection
):
plaintext_selections.append(plaintext_selection)
else:
log_warning(
f"decryption with nonce failed for contest: {contest.object_id} selection: {selection.object_id}"
)
return None
return PlaintextBallotContest(contest.object_id, plaintext_selections)
def decrypt_ballot_with_secret(
ballot: CiphertextBallot,
internal_manifest: InternalManifest,
crypto_extended_base_hash: ElementModQ,
public_key: ElGamalPublicKey,
secret_key: ElGamalSecretKey,
suppress_validity_check: bool = False,
remove_placeholders: bool = True,
) -> Optional[PlaintextBallot]:
"""
Decrypt the specified `CiphertextBallot` within the context of the specified election.
:param ballot: the ballot to decrypt
:param internal_manifest: the qualified election metadata that includes placeholder selections
:param crypto_extended_base_hash: the extended base hash code (𝑄') for the election
:param public_key: the public key for the election (K)
:param secret_key: the known secret key used to generate the public key for this election
:param suppress_validity_check: do not validate the encryption prior to decrypting (useful for tests)
:param remove_placeholders: filter out placeholder ciphertext selections after decryption
"""
if not suppress_validity_check and not ballot.is_valid_encryption(
internal_manifest.manifest_hash, public_key, crypto_extended_base_hash
):
log_warning(f"ballot: {ballot.object_id} failed validity check")
return None
plaintext_contests: List[PlaintextBallotContest] = []
for contest in ballot.contests:
description = internal_manifest.contest_for(contest.object_id)
plaintext_contest = decrypt_contest_with_secret(
contest,
get_optional(description),
public_key,
secret_key,
crypto_extended_base_hash,
suppress_validity_check,
remove_placeholders,
)
if plaintext_contest is not None:
plaintext_contests.append(plaintext_contest)
else:
log_warning(
f"decryption with nonce failed for ballot: {ballot.object_id} selection: {contest.object_id}"
)
return None
return PlaintextBallot(ballot.object_id, ballot.style_id, plaintext_contests)
def decrypt_ballot_with_nonce(
ballot: CiphertextBallot,
internal_manifest: InternalManifest,
crypto_extended_base_hash: ElementModQ,
public_key: ElGamalPublicKey,
nonce: Optional[ElementModQ] = None,
suppress_validity_check: bool = False,
remove_placeholders: bool = True,
) -> Optional[PlaintextBallot]:
"""
Decrypt the specified `CiphertextBallot` within the context of the specified election.
:param ballot: the ballot to decrypt
:param internal_manifest: the qualified election metadata that includes placeholder selections
:param crypto_extended_base_hash: the extended base hash code (𝑄') for the election
:param public_key: the public key for the election (K)
:param nonce: the optional master ballot nonce that was either seeded to, or gernated by the encryption function
:param suppress_validity_check: do not validate the encryption prior to decrypting (useful for tests)
:param remove_placeholders: filter out placeholder ciphertext selections after decryption
"""
if not suppress_validity_check and not ballot.is_valid_encryption(
internal_manifest.manifest_hash, public_key, crypto_extended_base_hash
):
log_warning(f"ballot: {ballot.object_id} failed validity check")
return None
# Use the hashed representation included in the ballot
# or override with the provided values
if nonce is None:
nonce_seed = ballot.hashed_ballot_nonce()
else:
nonce_seed = CiphertextBallot.nonce_seed(
internal_manifest.manifest_hash, ballot.object_id, nonce
)
if nonce_seed is None:
log_warning(
f"missing nonce_seed value. decrypt could not derive a nonce value for ballot {ballot.object_id}"
)
return None
plaintext_contests: List[PlaintextBallotContest] = []
for contest in ballot.contests:
description = internal_manifest.contest_for(contest.object_id)
plaintext_contest = decrypt_contest_with_nonce(
contest,
get_optional(description),
public_key,
crypto_extended_base_hash,
nonce_seed,
suppress_validity_check,
remove_placeholders,
)
if plaintext_contest is not None:
plaintext_contests.append(plaintext_contest)
else:
log_warning(
f"decryption with nonce failed for ballot: {ballot.object_id} selection: {contest.object_id}"
)
return None
return PlaintextBallot(ballot.object_id, ballot.style_id, plaintext_contests)