|
5 | 5 | #include "group.hpp" |
6 | 6 |
|
7 | 7 | #include <memory> |
| 8 | +#include <vector> |
8 | 9 |
|
9 | 10 | namespace electionguard |
10 | 11 | { |
@@ -137,6 +138,131 @@ namespace electionguard |
137 | 138 | /// </summary> |
138 | 139 | EG_API std::unique_ptr<ElGamalCiphertext> |
139 | 140 | 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 | + |
140 | 266 | } // namespace electionguard |
141 | 267 |
|
142 | 268 | #endif /* __ELECTIONGUARD__CPP_ELGAMAL_HPP_INCLUDED__ */ |
0 commit comments