-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.cpp
More file actions
380 lines (318 loc) · 12 KB
/
Copy pathcrypto.cpp
File metadata and controls
380 lines (318 loc) · 12 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include "crypto.hpp"
AES::AES(const int& key_length){
// Compute the byte length.
const int byte_length = key_length / 8;
// Get the cipher object.
cipher_ = get_cipher(byte_length);
// Sample a random key.
key_.resize(byte_length);
RAND_bytes(key_.data(), byte_length);
// Get the cipher ctx.
ctx_ = EVP_CIPHER_CTX_new();
}
AES::AES(const CharVec& key){
// Get the cipher object.
cipher_ = get_cipher(key.size());
// Set the key.
key_ = key;
// Get the cipher ctx.
ctx_ = EVP_CIPHER_CTX_new();
}
AES::~AES(){
EVP_CIPHER_CTX_free(ctx_);
}
CharVec AES::get_key(){ return key_; }
void AES::set_key(const CharVec& key){ key_ = key; }
CharVec AES::encrypt(const CharVec& plaintext) const{
// Sample the random IV.
CharVec iv(EVP_MAX_IV_LENGTH);
RAND_bytes(iv.data(), EVP_MAX_IV_LENGTH);
// Declare length variable and get space for ciphertext.
int len;
CharVec ciphertext(plaintext.size() + EVP_CIPHER_block_size(cipher_));
// Init the encryption scheme and update with ciphertext.
EVP_EncryptInit(ctx_, cipher_, key_.data(), iv.data());
EVP_EncryptUpdate(ctx_, ciphertext.data(), &len, plaintext.data(), static_cast<int>(plaintext.size()));
// Record the ciphertext length and finalize.
int ciphertext_len = len;
EVP_EncryptFinal_ex(ctx_, ciphertext.data() + len, &len);
// Insert the IV to the beginning.
ciphertext_len += len;
ciphertext.resize(ciphertext_len);
ciphertext.insert(ciphertext.begin(), iv.begin(), iv.end());
return ciphertext;
}
CharVec AES::decrypt(const CharVec& ciphertext) const{
// Split IV and the actual ciphertext.
const CharVec iv(ciphertext.begin(), ciphertext.begin() + EVP_MAX_IV_LENGTH);
const CharVec actual_ciphertext(ciphertext.begin() + EVP_MAX_IV_LENGTH, ciphertext.end());
// Declare length variable and get space for plaintext.
int len;
CharVec plaintext(actual_ciphertext.size() + EVP_CIPHER_block_size(cipher_));
// Init the decryption scheme and update with ciphertext.
EVP_DecryptInit(ctx_, cipher_, key_.data(), iv.data());
EVP_DecryptUpdate(
ctx_, plaintext.data(), &len, actual_ciphertext.data(), static_cast<int>(actual_ciphertext.size())
);
// Record the plaintext length and finalize.
int plaintext_len = len;
EVP_DecryptFinal_ex(ctx_, plaintext.data() + len, &len);
// Resize the plaintext and output.
plaintext_len += len;
plaintext.resize(plaintext_len);
return plaintext;
}
const EVP_CIPHER* AES::get_cipher(const size_t byte_length){
switch (byte_length){
case 16:
return EVP_aes_128_cbc();
case 24:
return EVP_aes_192_cbc();
case 32:
return EVP_aes_256_cbc();
default:
throw std::invalid_argument("Key length must be 128, 192, or 256 bits.");
}
}
PRF::PRF(const CharVec& key){
// If key is not provided, sample a random key, we use AES-256 and manually set the key size to 32.
if (key.empty()){
key_.resize(32);
RAND_bytes(key_.data(), 32);
}
else{
if (key.size() != 32) throw std::invalid_argument("Key length must be 32 bytes.");
key_ = key;
}
// Fetch the desired algorithm to use.
mac_ = EVP_MAC_fetch(nullptr, "CMAC", nullptr);
// Get the ctx object.
ctx_ = EVP_MAC_CTX_new(mac_);
}
PRF::~PRF(){
EVP_MAC_CTX_free(ctx_);
EVP_MAC_free(mac_);
}
CharVec PRF::get_key(){
return key_;
}
void PRF::set_key(const CharVec& key){
key_ = key;
}
CharVec PRF::digest(const CharVec& data) const{
// Set the parameters.
const OSSL_PARAM params[] = {
OSSL_PARAM_construct_utf8_string(
OSSL_MAC_PARAM_CIPHER,
const_cast<char*>(cipher_.data()),
cipher_.size()
),
OSSL_PARAM_construct_end()
};
// Initialize the CMAC.
EVP_MAC_init(ctx_, key_.data(), key_.size(), params);
// Update HMAC with input data.
EVP_MAC_update(ctx_, data.data(), data.size());
// Create the holder for output.
CharVec out(32);
// Finalize the CMAC.
EVP_MAC_final(ctx_, out.data(), nullptr, 32);
return out;
}
HMAC::HMAC(const CharVec& key){
// If key is not provided, sample a random key, we use BLAKE2B and manually set the key size to 64.
if (key.empty()){
key_.resize(64);
RAND_bytes(key_.data(), 64);
}
else{
if (key.size() != 64) throw std::invalid_argument("Key length must be 64 bytes.");
key_ = key;
}
// Fetch the desired algorithm to use.
mac_ = EVP_MAC_fetch(nullptr, "HMAC", nullptr);
// Get the ctx object.
ctx_ = EVP_MAC_CTX_new(mac_);
}
HMAC::~HMAC(){
EVP_MAC_CTX_free(ctx_);
EVP_MAC_free(mac_);
}
CharVec HMAC::get_key(){ return key_; }
void HMAC::set_key(const CharVec& key){ key_ = key; }
CharVec HMAC::digest(const CharVec& data) const{
// Set the parameters.
const OSSL_PARAM params[] = {
OSSL_PARAM_construct_utf8_string(
OSSL_MAC_PARAM_DIGEST,
const_cast<char*>(digest_.data()),
digest_.size()
),
OSSL_PARAM_construct_end()
};
// Initialize the HMAC.
EVP_MAC_init(ctx_, key_.data(), key_.size(), params);
// Update HMAC with input data.
EVP_MAC_update(ctx_, data.data(), data.size());
// Create the holder for output.
CharVec out(64);
// Finalize the HMAC.
EVP_MAC_final(ctx_, out.data(), nullptr, 64);
return out;
}
FpVec HMAC::digest_vec_to_fp(const Vec& x, const IntVec& sel) const{
// Create holder for the hash result.
FpVec r;
// Depending on the input type, hash the input x vector.
std::visit([&r, sel, this](auto&& vec_x){
// Get the type of the input.
using T = std::decay_t<decltype(vec_x)>;
// For integer vectors.
if constexpr (std::is_same_v<T, IntVec>){
// If the selective is empty, we hash x_i + i.
if (sel.empty())
for (int i = 0; i < vec_x.size(); ++i){
r.push_back(Helper::char_vec_to_fp(
digest(Helper::str_to_char_vec(std::to_string(vec_x[i]) + std::to_string(i)))
));
}
else
// Otherwise we hash x_i + sel[i].
for (int i = 0; i < vec_x.size(); ++i)
r.push_back(Helper::char_vec_to_fp(
digest(Helper::str_to_char_vec(std::to_string(vec_x[i]) + std::to_string(sel[i])))
));
}
// For string vectors.
else if constexpr (std::is_same_v<T, StrVec>){
if (sel.empty())
// If the selective is empty, we hash x_i || i.
for (int i = 0; i < vec_x.size(); ++i)
r.push_back(Helper::char_vec_to_fp(
digest(Helper::str_to_char_vec(vec_x[i] + std::to_string(i)))
));
else
// Otherwise we hash x_i || sel[i].
for (int i = 0; i < vec_x.size(); ++i)
r.push_back(Helper::char_vec_to_fp(
digest(Helper::str_to_char_vec(vec_x[i] + std::to_string(sel[i])))
));
}
// Otherwise the type is not supported.
else{
throw std::runtime_error("Unsupported hash type");
}
}, x);
return r;
}
FpMat HMAC::digest_mat_to_fp(const Mat& x, const IntVec& sel) const{
// Create holder for the hash result.
FpMat r;
// Depending on the input type, hash the input x matrix.
std::visit([&r, sel, this](auto&& mat_x){
// Get the type of the input.
using T = std::decay_t<decltype(mat_x)>;
// For integer matrix.
if constexpr (std::is_same_v<T, IntMat>){
// If we do not need to select values.
if (sel.empty()){
for (int i = 0; i < mat_x.size(); ++i){
if (mat_x[i].empty()){
r.push_back({Fp(0)});
}
else{
// Create holder for hash result of this row.
FpVec row_hash;
// This row is hashed with row + i.
for (const auto& each_x : mat_x[i])
row_hash.push_back(Helper::char_vec_to_fp(
digest(Helper::str_to_char_vec(std::to_string(each_x) + std::to_string(i)))
));
// Add the hashed value back to r.
r.push_back(row_hash);
}
}
}
else{
for (int i = 0; i < mat_x.size(); ++i){
// Create holder for hash result of this row.
FpVec row_hash;
// This row is hashed with row + sel[i].
for (const auto& each_x : mat_x[i])
row_hash.push_back(Helper::char_vec_to_fp(
digest(Helper::str_to_char_vec(std::to_string(each_x) + std::to_string(sel[i])))
));
// Add the hashed value back to r.
r.push_back(row_hash);
}
}
}
else if constexpr (std::is_same_v<T, StrMat>){
// If we do not need to select values.
if (sel.empty()){
for (int i = 0; i < mat_x.size(); ++i){
if (mat_x[i].empty()){
r.push_back({Fp(0)});
}
else{
// Create holder for hash result of this row.
FpVec row_hash;
// This row is hashed with row + i.
for (const auto& each_x : mat_x[i])
row_hash.push_back(Helper::char_vec_to_fp(
digest(Helper::str_to_char_vec(each_x + std::to_string(i)))
));
// Add the hashed value back to r.
r.push_back(row_hash);
}
}
}
else{
for (int i = 0; i < mat_x.size(); ++i){
// Create holder for hash result of this row.
FpVec row_hash;
// This row is hashed with row + sel[i].
for (const auto& each_x : mat_x[i])
row_hash.push_back(Helper::char_vec_to_fp(
digest(Helper::str_to_char_vec(each_x + std::to_string(sel[i])))
));
// Add the hashed value back to r.
r.push_back(row_hash);
}
}
}
else{
throw std::runtime_error("Unsupported hash type");
}
}, x);
return r;
}
FpVec HMAC::digest_int_to_fp_vec(const int& x, const int& len) const{
// Get the hash of the input integer first.
CharMat r;
r.push_back(digest(Helper::int_to_char_vec(x)));
// Compute the hash based on prior hash values.
for (int i = 1; i < len; ++i) r.push_back(digest(r[i - 1]));
// Convert hashes to field point values.
FpVec fp_r;
for (auto& each_r : r) fp_r.push_back(Helper::char_vec_to_fp(each_r));
return fp_r;
}
FpVec HMAC::digest_vec_to_fp_mod(const BP& pairing_group, const Vec& x, const IntVec& sel) const{
// Get the hash result and perform modulo.
FpVec r = digest_vec_to_fp(x, sel);
pairing_group.Zp->mod(r);
return r;
}
FpMat HMAC::digest_mat_to_fp_mod(const BP& pairing_group, const Mat& x, const IntVec& sel) const{
// Get the hash result and perform modulo.
FpMat r = digest_mat_to_fp(x, sel);
pairing_group.Zp->mod(r);
return r;
}
FpVec HMAC::digest_int_to_fp_vec_mod(const BP& pairing_group, const int& x, const int& len) const{
// Get the hash result and perform modulo.
FpVec r = digest_int_to_fp_vec(x, len);
pairing_group.Zp->mod(r);
return r;
}