55#include " electionguard/ballot_code.hpp"
66#include " electionguard/elgamal.hpp"
77#include " electionguard/hash.hpp"
8+ #include " electionguard/precompute_buffers.hpp"
89#include " log.hpp"
910#include " nonces.hpp"
1011#include " serialize.hpp"
1112#include " utils.hpp"
12- #include " electionguard/precompute_buffers.hpp"
13- #include < nlohmann/json.hpp>
1413
1514#include < algorithm>
1615#include < future>
1716#include < iostream>
17+ #include < nlohmann/json.hpp>
1818
1919extern " C" {
2020#include " ../karamel/Hacl_Bignum4096.h"
@@ -90,18 +90,10 @@ namespace electionguard
9090 return DeviceSerializer::fromBson (move (data));
9191 }
9292
93- uint64_t EncryptionDevice::getDeviceUuid () const {
94- return pimpl->deviceUuid ;
95- }
96- uint64_t EncryptionDevice::getSessionUuid () const {
97- return pimpl->sessionUuid ;
98- }
99- uint64_t EncryptionDevice::getLaunchCode () const {
100- return pimpl->launchCode ;
101- }
102- std::string EncryptionDevice::getLocation () const {
103- return pimpl->location ;
104- }
93+ uint64_t EncryptionDevice::getDeviceUuid () const { return pimpl->deviceUuid ; }
94+ uint64_t EncryptionDevice::getSessionUuid () const { return pimpl->sessionUuid ; }
95+ uint64_t EncryptionDevice::getLaunchCode () const { return pimpl->launchCode ; }
96+ std::string EncryptionDevice::getLocation () const { return pimpl->location ; }
10597
10698#pragma endregion
10799
@@ -127,6 +119,11 @@ namespace electionguard
127119 const EncryptionDevice &encryptionDevice)
128120 : pimpl(new Impl(internalManifest, context, encryptionDevice))
129121 {
122+ if (internalManifest.getManifestHash ()->toHex () != context.getManifestHash ()->toHex ()) {
123+ throw invalid_argument (" manifest and context do not match hashes manifest:" +
124+ internalManifest.getManifestHash ()->toHex () +
125+ " context:" + context.getManifestHash ()->toHex ());
126+ }
130127 }
131128
132129 EncryptionMediator::~EncryptionMediator () = default ;
@@ -296,8 +293,8 @@ namespace electionguard
296293 auto pubkey_to_exp = triple1->get_pubkey_to_exp ();
297294
298295 // Generate the encryption using precomputed values
299- ciphertext = elgamalEncrypt_with_precomputed (selection. getVote (),
300- *g_to_exp, *pubkey_to_exp);
296+ ciphertext =
297+ elgamalEncrypt_with_precomputed (selection. getVote (), *g_to_exp, *pubkey_to_exp);
301298 if (ciphertext == nullptr ) {
302299 throw runtime_error (" encryptSelection:: Error generating ciphertext" );
303300 }
@@ -309,12 +306,10 @@ namespace electionguard
309306 encrypted = CiphertextBallotSelection::make_with_precomputed (
310307 selection.getObjectId (), description.getSequenceOrder (), *descriptionHash,
311308 move (ciphertext), cryptoExtendedBaseHash, selection.getVote (),
312- move (precomputedTwoTriplesAndAQuad),
313- isPlaceholder, true );
309+ move (precomputedTwoTriplesAndAQuad), isPlaceholder, true );
314310 } else {
315311 // Generate the encryption
316- ciphertext =
317- elgamalEncrypt (selection.getVote (), *selectionNonce, elgamalPublicKey);
312+ ciphertext = elgamalEncrypt (selection.getVote (), *selectionNonce, elgamalPublicKey);
318313 if (ciphertext == nullptr ) {
319314 throw runtime_error (" encryptSelection:: Error generating ciphertext" );
320315 }
@@ -336,17 +331,17 @@ namespace electionguard
336331
337332 // verify the selection.
338333 if (encrypted->isValidEncryption (*descriptionHash, elgamalPublicKey,
339- cryptoExtendedBaseHash)) {
334+ cryptoExtendedBaseHash)) {
340335 return encrypted;
341336 }
342- throw runtime_error (" encryptSelection failed validity check" );
337+ throw runtime_error (" encryptSelection failed validity check" );
343338 }
344339
345340 string getOvervoteAndWriteIns (const PlaintextBallotContest &contest,
346341 const InternalManifest &internalManifest,
347342 eg_valid_contest_return_type_t is_overvote)
348343 {
349- json overvoteAndWriteIns;
344+ json overvoteAndWriteIns;
350345 auto selections = contest.getSelections ();
351346
352347 // if an overvote is detected then put the selections into json
@@ -403,24 +398,23 @@ namespace electionguard
403398
404399 string overvoteAndWriteIns_string (" " );
405400 if (overvoteAndWriteIns.dump () != string (" null" )) {
406- overvoteAndWriteIns_string = overvoteAndWriteIns.dump ();
401+ overvoteAndWriteIns_string = overvoteAndWriteIns.dump ();
407402 }
408403
409404 return overvoteAndWriteIns_string;
410405 }
411406
412407 unique_ptr<CiphertextBallotContest>
413- encryptContest (const PlaintextBallotContest &contest,
414- const InternalManifest &internalManifest,
408+ encryptContest (const PlaintextBallotContest &contest, const InternalManifest &internalManifest,
415409 const ContestDescriptionWithPlaceholders &description,
416410 const ElementModP &elgamalPublicKey, const ElementModQ &cryptoExtendedBaseHash,
417411 const ElementModQ &nonceSeed, bool shouldVerifyProofs /* = true */ )
418412
419413 {
420414 // Validate Input
421415 bool supportOvervotes = true ;
422- eg_valid_contest_return_type_t is_valid_contest =
423- contest. isValid ( description.getObjectId (), description.getSelections ().size (),
416+ eg_valid_contest_return_type_t is_valid_contest = contest. isValid (
417+ description.getObjectId (), description.getSelections ().size (),
424418 description.getNumberElected (), description.getVotesAllowed (), supportOvervotes);
425419 if ((is_valid_contest != SUCCESS) && (is_valid_contest != OVERVOTE)) {
426420 throw invalid_argument (" the plaintext contest was invalid" );
@@ -471,16 +465,16 @@ namespace electionguard
471465
472466 // if the is an overvote then we need to make all the selection votes 0
473467 if (is_valid_contest == OVERVOTE) {
474- duplicate_selection = make_unique<PlaintextBallotSelection>(
475- selection_ptr->getObjectId (), 0 , false );
468+ duplicate_selection =
469+ make_unique<PlaintextBallotSelection>( selection_ptr->getObjectId (), 0 , false );
476470 selection_ptr = duplicate_selection.get ();
477471 }
478472
479473 selectionCount += selection_ptr->getVote ();
480474
481475 encryptedSelections.push_back (encryptSelection (
482476 *selection_ptr, selectionDescription.get (), *elgamalPublicKey_ptr,
483- *cryptoExtendedBaseHash_ptr, *sharedNonce.get (), false , shouldVerifyProofs));
477+ *cryptoExtendedBaseHash_ptr, *sharedNonce.get (), false , shouldVerifyProofs));
484478 } else {
485479 // Should never happen since the contest is normalized by emplaceMissingValues
486480 throw runtime_error (" encryptedContest:: Error constructing encrypted selection" );
@@ -507,18 +501,17 @@ namespace electionguard
507501
508502 auto placeholderSelection = selectionFrom (placeholder, true , selectPlaceholder);
509503 encryptedSelections.push_back (encryptSelection (
510- *placeholderSelection, placeholder, *elgamalPublicKey_ptr,
511- *cryptoExtendedBaseHash_ptr, *sharedNonce.get (), false , shouldVerifyProofs));
504+ *placeholderSelection, placeholder, *elgamalPublicKey_ptr,
505+ *cryptoExtendedBaseHash_ptr, *sharedNonce.get (), false , shouldVerifyProofs));
512506 }
513507
514508 // Derive the extendedDataNonce from the selection nonce and a constant
515509 auto noncesForExtendedData =
516- make_unique<Nonces>(*sharedNonce->clone (), " constant-extended-data" );
510+ make_unique<Nonces>(*sharedNonce->clone (), " constant-extended-data" );
517511 auto extendedDataNonce = noncesForExtendedData->get (0 );
518512
519- vector<uint8_t > extendedData_plaintext ((uint8_t *)&extendedData.front (),
520- (uint8_t *)&extendedData.front () +
521- extendedData.size ());
513+ vector<uint8_t > extendedData_plaintext (
514+ (uint8_t *)&extendedData.front (), (uint8_t *)&extendedData.front () + extendedData.size ());
522515
523516 // Perform HashedElGamalCiphertext calculation
524517 unique_ptr<HashedElGamalCiphertext> hashedElGamal =
@@ -536,8 +529,8 @@ namespace electionguard
536529 auto encryptedContest = CiphertextBallotContest::make (
537530 contest.getObjectId (), description.getSequenceOrder (), *descriptionHash,
538531 move (encryptedSelections), elgamalPublicKey, cryptoExtendedBaseHash, *chaumPedersenNonce,
539- description.getNumberElected (), sharedNonce->clone (), nullptr ,
540- nullptr , move (hashedElGamal));
532+ description.getNumberElected (), sharedNonce->clone (), nullptr , nullptr ,
533+ move (hashedElGamal));
541534
542535 if (encryptedContest == nullptr || encryptedContest->getProof () == nullptr ) {
543536 throw runtime_error (" encryptedContest:: Error constructing encrypted constest" );
@@ -574,8 +567,8 @@ namespace electionguard
574567 hasContest = true ;
575568 auto encrypted = encryptContest (
576569 contest.get (), internalManifest, description.get (),
577- *context.getElGamalPublicKey (),
578- *context. getCryptoExtendedBaseHash (), nonceSeed, shouldVerifyProofs);
570+ *context.getElGamalPublicKey (), *context. getCryptoExtendedBaseHash (),
571+ nonceSeed, shouldVerifyProofs);
579572
580573 encryptedContests.push_back (move (encrypted));
581574 break ;
0 commit comments