Skip to content

Commit 8d1f234

Browse files
jeffspel-cryptodryercashewSteveMaier-IRT
authored
Overvote (#263)
* Adding code for the encrypt method of the hashed elgamal ciphertext object. * Add some decryption code * More decrypt code * Encrypt/Decrypt for HashedElGamalCiphertext is working. * Encrypt/decrypt seem to be working * More HashedElGamalCiphertest changes. * Update to HashedElGamalCiphertext crypto_hash methods. * Revert changes to hash.hpp/cpp. * Add benchmarking test. Cleanup the HashedElGamalCiphertext crypto_hash methods. * Fix destructor. * Updated to zeroize intermediate values. * Change HashedElGamalCiphertext member from ciphertext to data. * Update the padding scheme used in HashedElGamalCiphertext. * Update to using hash_elems. Also create a get_hmac function and start using it. * Fix some issues with HashElGamalCiphertext implementation. * Add enumeration for max_len on hashedElGamalEncrypt * Remove the apply_padding parameter from hashedElGamalEncrypt since with the enumeration we no longer need it. * Update the master_key creation. * Remove inheriting from CryptoHashable until we are sure we need it. * Remove random comment. Co-authored-by: Jeff <spelmaa@wwu.edu> Co-authored-by: SteveMaier-IRT <steve.maier@infernored.com>
1 parent 9525fec commit 8d1f234

8 files changed

Lines changed: 786 additions & 5 deletions

File tree

include/electionguard/elgamal.hpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "group.hpp"
66

77
#include <memory>
8+
#include <vector>
89

910
namespace electionguard
1011
{
@@ -137,6 +138,131 @@ namespace electionguard
137138
/// </summary>
138139
EG_API std::unique_ptr<ElGamalCiphertext>
139140
elgamalAdd(const std::vector<std::reference_wrapper<ElGamalCiphertext>> &ciphertexts);
141+
142+
#define HASHED_CIPHERTEXT_BLOCK_LENGTH 32U
143+
#define _PAD_INDICATOR_SIZE sizeof(uint16_t)
144+
145+
typedef enum padded_data_size_e {
146+
NO_PADDING = 0,
147+
BYTES_32 = 32 - _PAD_INDICATOR_SIZE,
148+
BYTES_64 = 64 - _PAD_INDICATOR_SIZE,
149+
BYTES_128 = 128 - _PAD_INDICATOR_SIZE,
150+
BYTES_256 = 256 - _PAD_INDICATOR_SIZE,
151+
BYTES_512 = 512 - _PAD_INDICATOR_SIZE
152+
} padded_data_size_t;
153+
154+
/// <summary>
155+
/// A "Hashed ElGamal Ciphertext" as specified as the Auxiliary Encryption in
156+
/// the ElectionGuard specification. The tuple g ^ r mod p concatenated with
157+
/// K ^ r mod p are used to feed into a hash function to generate a main key
158+
/// from which other keys derive to perform XOR encryption and to MAC the
159+
/// result. Create one with `hashedElgamalEncrypt`. Decrypt using one the
160+
/// 'decrypt' method.
161+
/// </summary>
162+
class EG_API HashedElGamalCiphertext
163+
{
164+
public:
165+
HashedElGamalCiphertext(const HashedElGamalCiphertext &other);
166+
HashedElGamalCiphertext(HashedElGamalCiphertext &&other);
167+
HashedElGamalCiphertext(std::unique_ptr<ElementModP> pad, std::vector<uint8_t> data,
168+
std::vector<uint8_t> mac);
169+
~HashedElGamalCiphertext();
170+
171+
HashedElGamalCiphertext &operator=(HashedElGamalCiphertext rhs);
172+
HashedElGamalCiphertext &operator=(HashedElGamalCiphertext &&rhs);
173+
bool operator==(const HashedElGamalCiphertext &other);
174+
bool operator!=(const HashedElGamalCiphertext &other);
175+
176+
/// <Summary>
177+
/// The g ^r mod p value also referred to as pad in the code and
178+
/// c0 in the spec.
179+
/// </Summary>
180+
ElementModP *getPad();
181+
182+
/// <Summary>
183+
/// The g ^r mod p value also referred to as pad in the code and
184+
/// c0 in the spec.
185+
/// </Summary>
186+
ElementModP *getPad() const;
187+
188+
/// <Summary>
189+
/// The vector of encrypted ciphertext bytes. Referred to as c1
190+
/// in the spec.
191+
/// </Summary>
192+
std::vector<uint8_t> getData();
193+
194+
/// <Summary>
195+
/// The vector of encrypted ciphertext bytes. Referred to as c1
196+
/// in the spec.
197+
/// </Summary>
198+
std::vector<uint8_t> getData() const;
199+
200+
/// <Summary>
201+
/// The vector of MAC bytes. Referred to as c2 in the spec.
202+
/// </Summary>
203+
std::vector<uint8_t> getMac();
204+
205+
/// <Summary>
206+
/// The vector of MAC bytes. Referred to as c2 in the spec.
207+
/// </Summary>
208+
std::vector<uint8_t> getMac() const;
209+
210+
/// <summary>
211+
/// Decrypts ciphertext with the Auxiliary Encryption method (as specified in the
212+
/// ElectionGuard specification) given a random nonce, an ElGamal public key,
213+
/// and a description hash. The encrypt may be called to look for padding to
214+
/// verify and remove, in this case the plaintext will be smaller than
215+
/// the ciphertext, or not to look for padding in which case the
216+
/// plaintext will be the same size as the ciphertext.
217+
///
218+
/// <param name="nonce"> Randomly chosen nonce in [1,Q). </param>
219+
/// <param name="publicKey"> ElGamal public key. </param>
220+
/// <param name="descriptionHash"> Hash of the ballot description. </param>
221+
/// <param name="look_for_padding"> Indicates if padding removed. </param>
222+
/// <returns>A plaintext vector.</returns>
223+
/// </summary>
224+
std::vector<uint8_t> decrypt(const ElementModQ &secret_key,
225+
const ElementModQ &descriptionHash, bool look_for_padding);
226+
227+
/// <Summary>
228+
/// Clone the value by making a deep copy.
229+
/// </Summary>
230+
std::unique_ptr<HashedElGamalCiphertext> clone() const;
231+
232+
private:
233+
class Impl;
234+
#pragma warning(suppress : 4251)
235+
std::unique_ptr<Impl> pimpl;
236+
};
237+
238+
/// <summary>
239+
/// Encrypts a message with the Auxiliary Encryption method (as specified in the
240+
/// ElectionGuard specification) given a random nonce, an ElGamal public key,
241+
/// and a description hash. The encrypt may be called to apply padding. If
242+
/// padding is to be applied then the max_len parameter may be used with
243+
/// any of the padded_data_size_t enumeration values that is not NO_PADDING.
244+
/// This value indicates the maximum length of the plaintext that may be
245+
/// encrypted. The padding scheme applies two bytes for length of padding
246+
/// plus padding bytes. If padding is not to be applied then the
247+
/// max_len parameter must be NO_PADDING and the plaintext must
248+
/// be a multiple of the block length (32) and the ciphertext will be
249+
/// the same size.
250+
///
251+
/// <param name="plaintext"> Message to hashed elgamal encrypt. </param>
252+
/// <param name="nonce"> Randomly chosen nonce in [1,Q). </param>
253+
/// <param name="publicKey"> ElGamal public key. </param>
254+
/// <param name="descriptionHash"> Hash of the ballot description. </param>
255+
/// <param name="max_len"> If padding is to be applied then this indicates the
256+
/// maximum length of plaintext, must be one padded_data_size_t enumeration
257+
/// values. If padding is not to be applied then this parameter must use
258+
/// the NO_PADDING padded_data_size_t enumeration value.</param>
259+
/// <returns>A ciphertext triple.</returns>
260+
/// </summary>
261+
EG_API std::unique_ptr<HashedElGamalCiphertext>
262+
hashedElgamalEncrypt(std::vector<uint8_t> plaintext, const ElementModQ &nonce,
263+
const ElementModP &publicKey, const ElementModQ &descriptionHash,
264+
padded_data_size_t max_len);
265+
140266
} // namespace electionguard
141267

142268
#endif /* __ELECTIONGUARD__CPP_ELGAMAL_HPP_INCLUDED__ */

include/electionguard/hmac.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef __ELECTIONGUARD_CPP_HMAC_HPP_INCLUDED__
2+
#define __ELECTIONGUARD_CPP_HMAC_HPP_INCLUDED__
3+
#include <electionguard/crypto_hashable.hpp>
4+
#include <electionguard/export.h>
5+
#include <electionguard/group.hpp>
6+
#include <memory>
7+
#include <string>
8+
#include <variant>
9+
#include <vector>
10+
11+
namespace electionguard
12+
{
13+
/// <param name="a"> Zero or more elements of any of the accepted types.</param>
14+
/// <returns>A cryptographic hash of these elements, concatenated.</returns>
15+
/// </Summary>
16+
EG_API std::vector<uint8_t> get_hmac(std::vector<uint8_t> key,
17+
std::vector<uint8_t> message,
18+
uint32_t length, uint32_t start);
19+
} // namespace electionguard
20+
21+
#endif /* __ELECTIONGUARD_CPP_HMAC_HPP_INCLUDED__ */

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ set(PROJECT_SOURCE_FILES
5959
${PROJECT_SOURCE_DIR}/src/electionguard/encrypt.cpp
6060
${PROJECT_SOURCE_DIR}/src/electionguard/group.cpp
6161
${PROJECT_SOURCE_DIR}/src/electionguard/hash.cpp
62+
${PROJECT_SOURCE_DIR}/src/electionguard/hmac.cpp
6263
${PROJECT_SOURCE_DIR}/src/electionguard/log.hpp
6364
${PROJECT_SOURCE_DIR}/src/electionguard/log.cpp
6465
${PROJECT_SOURCE_DIR}/src/electionguard/lookup_table.hpp

0 commit comments

Comments
 (0)