Skip to content

Commit 6c599e9

Browse files
thamht4190ggershinsky
authored andcommitted
fix code style
1 parent 9fa9ef6 commit 6c599e9

2 files changed

Lines changed: 97 additions & 103 deletions

File tree

cpp/src/parquet/util/crypto.cc

Lines changed: 79 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ constexpr int BufferSizeLength = 4;
4545
}
4646

4747
AesEncryptor::AesEncryptor(ParquetCipher::type alg_id, int key_len, bool metadata) {
48-
4948
ctx_ = nullptr;
5049

5150
if (ParquetCipher::AES_GCM_V1 != alg_id && ParquetCipher::AES_GCM_CTR_V1 != alg_id) {
@@ -96,13 +95,11 @@ AesEncryptor::AesEncryptor(ParquetCipher::type alg_id, int key_len, bool metadat
9695
}
9796
}
9897

99-
int AesEncryptor::CiphertextSizeDelta() {
100-
return ciphertext_size_delta_;
101-
}
98+
int AesEncryptor::CiphertextSizeDelta() { return ciphertext_size_delta_; }
10299

103-
int AesEncryptor::gcm_encrypt(const uint8_t* plaintext, int plaintext_len,
104-
uint8_t* key, int key_len, uint8_t* nonce, uint8_t* aad,
105-
int aad_len, uint8_t* ciphertext) {
100+
int AesEncryptor::gcm_encrypt(const uint8_t* plaintext, int plaintext_len, uint8_t* key,
101+
int key_len, uint8_t* nonce, uint8_t* aad, int aad_len,
102+
uint8_t* ciphertext) {
106103
int len;
107104
int ciphertext_len;
108105

@@ -115,21 +112,21 @@ int AesEncryptor::gcm_encrypt(const uint8_t* plaintext, int plaintext_len,
115112
}
116113

117114
// Setting additional authenticated data
118-
if ((nullptr != aad) &&
119-
(1 != EVP_EncryptUpdate(ctx_, nullptr, &len, aad, aad_len))) {
115+
if ((nullptr != aad) && (1 != EVP_EncryptUpdate(ctx_, nullptr, &len, aad, aad_len))) {
120116
throw ParquetException("Couldn't set AAD");
121117
}
122118

123119
// Encryption
124-
if (1 != EVP_EncryptUpdate(ctx_, ciphertext + BufferSizeLength + NonceLength,
125-
&len, plaintext, plaintext_len)) {
120+
if (1 != EVP_EncryptUpdate(ctx_, ciphertext + BufferSizeLength + NonceLength, &len,
121+
plaintext, plaintext_len)) {
126122
throw ParquetException("Failed encryption update");
127123
}
128124

129125
ciphertext_len = len;
130126

131127
// Finalization
132-
if (1 != EVP_EncryptFinal_ex(ctx_, ciphertext + BufferSizeLength + NonceLength + len, &len)) {
128+
if (1 != EVP_EncryptFinal_ex(ctx_, ciphertext + BufferSizeLength + NonceLength + len,
129+
&len)) {
133130
throw ParquetException("Failed encryption finalization");
134131
}
135132

@@ -147,20 +144,20 @@ int AesEncryptor::gcm_encrypt(const uint8_t* plaintext, int plaintext_len,
147144
ciphertext[1] = (uint8_t)(0xff & (bufferSize >> 8));
148145
ciphertext[0] = (uint8_t)(0xff & (bufferSize));
149146
std::copy(nonce, nonce + NonceLength, ciphertext + BufferSizeLength);
150-
std::copy(tag, tag + GCMTagLength, ciphertext + BufferSizeLength + NonceLength + ciphertext_len);
147+
std::copy(tag, tag + GCMTagLength,
148+
ciphertext + BufferSizeLength + NonceLength + ciphertext_len);
151149

152150
return BufferSizeLength + bufferSize;
153151
}
154152

155-
156-
int AesEncryptor::ctr_encrypt(const uint8_t* plaintext, int plaintext_len,
157-
uint8_t* key, int key_len, uint8_t* nonce,
158-
uint8_t* ciphertext) {
153+
int AesEncryptor::ctr_encrypt(const uint8_t* plaintext, int plaintext_len, uint8_t* key,
154+
int key_len, uint8_t* nonce, uint8_t* ciphertext) {
159155
int len;
160156
int ciphertext_len;
161157

162-
// Parquet CTR IVs are comprised of a 12-byte nonce and a 4-byte initial counter field.
163-
// The first 31 bits of the initial counter field are set to 0, the last bit is set to 1.
158+
// Parquet CTR IVs are comprised of a 12-byte nonce and a 4-byte initial counter field.
159+
// The first 31 bits of the initial counter field are set to 0, the last bit is set
160+
// to 1.
164161
uint8_t iv[CTRIvLength];
165162
memset(iv, 0, CTRIvLength);
166163
std::copy(nonce, nonce + NonceLength, iv);
@@ -172,15 +169,16 @@ int AesEncryptor::ctr_encrypt(const uint8_t* plaintext, int plaintext_len,
172169
}
173170

174171
// Encryption
175-
if (1 != EVP_EncryptUpdate(ctx_, ciphertext + BufferSizeLength + CTRIvLength, &len, plaintext,
176-
plaintext_len)) {
172+
if (1 != EVP_EncryptUpdate(ctx_, ciphertext + BufferSizeLength + CTRIvLength, &len,
173+
plaintext, plaintext_len)) {
177174
throw ParquetException("Failed encryption update");
178175
}
179176

180177
ciphertext_len = len;
181178

182179
// Finalization
183-
if (1 != EVP_EncryptFinal_ex(ctx_, ciphertext + BufferSizeLength + CTRIvLength + len, &len)) {
180+
if (1 != EVP_EncryptFinal_ex(ctx_, ciphertext + BufferSizeLength + CTRIvLength + len,
181+
&len)) {
184182
throw ParquetException("Failed encryption finalization");
185183
}
186184

@@ -197,10 +195,9 @@ int AesEncryptor::ctr_encrypt(const uint8_t* plaintext, int plaintext_len,
197195
return BufferSizeLength + bufferSize;
198196
}
199197

200-
int AesEncryptor::SignedFooterEncrypt(const uint8_t* footer, int footer_len,
201-
uint8_t* key, int key_len, uint8_t* aad, int aad_len,
202-
uint8_t* nonce, uint8_t* encrypted_footer) {
203-
198+
int AesEncryptor::SignedFooterEncrypt(const uint8_t* footer, int footer_len, uint8_t* key,
199+
int key_len, uint8_t* aad, int aad_len,
200+
uint8_t* nonce, uint8_t* encrypted_footer) {
204201
if (key_length_ != key_len) {
205202
std::stringstream ss;
206203
ss << "Wrong key length " << key_len << ". Should be " << key_length_;
@@ -211,12 +208,12 @@ int AesEncryptor::SignedFooterEncrypt(const uint8_t* footer, int footer_len,
211208
throw ParquetException("Must use AES GCM (metadata) encryptor");
212209
}
213210

214-
return gcm_encrypt(footer, footer_len, key, key_len, nonce, aad, aad_len, encrypted_footer);
211+
return gcm_encrypt(footer, footer_len, key, key_len, nonce, aad, aad_len,
212+
encrypted_footer);
215213
}
216214

217-
int AesEncryptor::Encrypt(const uint8_t* plaintext, int plaintext_len, uint8_t* key, int key_len,
218-
uint8_t* aad, int aad_len, uint8_t* ciphertext) {
219-
215+
int AesEncryptor::Encrypt(const uint8_t* plaintext, int plaintext_len, uint8_t* key,
216+
int key_len, uint8_t* aad, int aad_len, uint8_t* ciphertext) {
220217
if (key_length_ != key_len) {
221218
std::stringstream ss;
222219
ss << "Wrong key length " << key_len << ". Should be " << key_length_;
@@ -229,15 +226,14 @@ int AesEncryptor::Encrypt(const uint8_t* plaintext, int plaintext_len, uint8_t*
229226
RAND_bytes(nonce, sizeof(nonce));
230227

231228
if (GCM_MODE == aes_mode_) {
232-
return gcm_encrypt(plaintext, plaintext_len, key, key_len, nonce,
233-
aad, aad_len, ciphertext);
229+
return gcm_encrypt(plaintext, plaintext_len, key, key_len, nonce, aad, aad_len,
230+
ciphertext);
234231
}
235232

236233
return ctr_encrypt(plaintext, plaintext_len, key, key_len, nonce, ciphertext);
237234
}
238235

239236
AesDecryptor::AesDecryptor(ParquetCipher::type alg_id, int key_len, bool metadata) {
240-
241237
ctx_ = nullptr;
242238

243239
if (ParquetCipher::AES_GCM_V1 != alg_id && ParquetCipher::AES_GCM_CTR_V1 != alg_id) {
@@ -251,7 +247,7 @@ AesDecryptor::AesDecryptor(ParquetCipher::type alg_id, int key_len, bool metadat
251247
aes_mode_ = GCM_MODE;
252248
ciphertext_size_delta_ += GCMTagLength;
253249
} else {
254-
aes_mode_ = CTR_MODE;
250+
aes_mode_ = CTR_MODE;
255251
}
256252

257253
if (16 != key_len && 24 != key_len && 32 != key_len) {
@@ -288,12 +284,10 @@ AesDecryptor::AesDecryptor(ParquetCipher::type alg_id, int key_len, bool metadat
288284
}
289285
}
290286

291-
int AesDecryptor::CiphertextSizeDelta() {
292-
return ciphertext_size_delta_;
293-
}
287+
int AesDecryptor::CiphertextSizeDelta() { return ciphertext_size_delta_; }
294288

295-
int AesDecryptor::gcm_decrypt(const uint8_t* ciphertext, int ciphertext_len,
296-
uint8_t* key, int key_len, uint8_t* aad, int aad_len,
289+
int AesDecryptor::gcm_decrypt(const uint8_t* ciphertext, int ciphertext_len, uint8_t* key,
290+
int key_len, uint8_t* aad, int aad_len,
297291
uint8_t* plaintext) {
298292
int len;
299293
int plaintext_len;
@@ -302,38 +296,37 @@ int AesDecryptor::gcm_decrypt(const uint8_t* ciphertext, int ciphertext_len,
302296
memset(tag, 0, GCMTagLength);
303297
uint8_t nonce[NonceLength];
304298
memset(nonce, 0, NonceLength);
305-
299+
306300
// Extract ciphertext length
307-
int written_ciphertext_len =
308-
((ciphertext[3] & 0xff) << 24) |
309-
((ciphertext[2] & 0xff) << 16) |
310-
((ciphertext[1] & 0xff) << 8) |
311-
((ciphertext[0] & 0xff));
312-
313-
if (ciphertext_len > 0 && ciphertext_len != (written_ciphertext_len + BufferSizeLength)) {
301+
int written_ciphertext_len = ((ciphertext[3] & 0xff) << 24) |
302+
((ciphertext[2] & 0xff) << 16) |
303+
((ciphertext[1] & 0xff) << 8) | ((ciphertext[0] & 0xff));
304+
305+
if (ciphertext_len > 0 &&
306+
ciphertext_len != (written_ciphertext_len + BufferSizeLength)) {
314307
throw ParquetException("Wrong ciphertext length");
315308
}
316309
ciphertext_len = written_ciphertext_len + BufferSizeLength;
317310

318311
// Extracting IV and tag
319-
std::copy(ciphertext + BufferSizeLength, ciphertext + BufferSizeLength + NonceLength, nonce);
312+
std::copy(ciphertext + BufferSizeLength, ciphertext + BufferSizeLength + NonceLength,
313+
nonce);
320314
std::copy(ciphertext + ciphertext_len - GCMTagLength, ciphertext + ciphertext_len, tag);
321315

322-
323316
// Setting key and IV
324317
if (1 != EVP_DecryptInit_ex(ctx_, nullptr, nullptr, key, nonce)) {
325318
throw ParquetException("Couldn't set key and IV");
326319
}
327320

328321
// Setting additional authenticated data
329-
if ((nullptr != aad) &&
330-
(1 != EVP_DecryptUpdate(ctx_, nullptr, &len, aad, aad_len))) {
322+
if ((nullptr != aad) && (1 != EVP_DecryptUpdate(ctx_, nullptr, &len, aad, aad_len))) {
331323
throw ParquetException("Couldn't set AAD");
332324
}
333325

334326
// Decryption
335-
if (!EVP_DecryptUpdate(ctx_, plaintext, &len, ciphertext + BufferSizeLength + NonceLength,
336-
ciphertext_len - BufferSizeLength - NonceLength - GCMTagLength)) {
327+
if (!EVP_DecryptUpdate(
328+
ctx_, plaintext, &len, ciphertext + BufferSizeLength + NonceLength,
329+
ciphertext_len - BufferSizeLength - NonceLength - GCMTagLength)) {
337330
throw ParquetException("Failed decryption update");
338331
}
339332

@@ -353,40 +346,41 @@ int AesDecryptor::gcm_decrypt(const uint8_t* ciphertext, int ciphertext_len,
353346
return plaintext_len;
354347
}
355348

356-
int AesDecryptor::ctr_decrypt(const uint8_t* ciphertext, int ciphertext_len,
357-
uint8_t* key, int key_len, uint8_t* plaintext) {
349+
int AesDecryptor::ctr_decrypt(const uint8_t* ciphertext, int ciphertext_len, uint8_t* key,
350+
int key_len, uint8_t* plaintext) {
358351
int len;
359352
int plaintext_len;
360353

361354
uint8_t iv[CTRIvLength];
362355
memset(iv, 0, CTRIvLength);
363-
356+
364357
// Extract ciphertext length
365-
int written_ciphertext_len =
366-
((ciphertext[3] & 0xff) << 24) |
367-
((ciphertext[2] & 0xff) << 16) |
368-
((ciphertext[1] & 0xff) << 8) |
369-
((ciphertext[0] & 0xff));
370-
371-
if (ciphertext_len > 0 && ciphertext_len != (written_ciphertext_len + BufferSizeLength)) {
358+
int written_ciphertext_len = ((ciphertext[3] & 0xff) << 24) |
359+
((ciphertext[2] & 0xff) << 16) |
360+
((ciphertext[1] & 0xff) << 8) | ((ciphertext[0] & 0xff));
361+
362+
if (ciphertext_len > 0 &&
363+
ciphertext_len != (written_ciphertext_len + BufferSizeLength)) {
372364
throw ParquetException("Wrong ciphertext length");
373365
}
374366
ciphertext_len = written_ciphertext_len;
375367

376368
// Extracting nonce
377-
std::copy(ciphertext + BufferSizeLength, ciphertext + BufferSizeLength + NonceLength, iv);
378-
// Parquet CTR IVs are comprised of a 12-byte nonce and a 4-byte initial counter field.
379-
// The first 31 bits of the initial counter field are set to 0, the last bit is set to 1.
369+
std::copy(ciphertext + BufferSizeLength, ciphertext + BufferSizeLength + NonceLength,
370+
iv);
371+
// Parquet CTR IVs are comprised of a 12-byte nonce and a 4-byte initial counter field.
372+
// The first 31 bits of the initial counter field are set to 0, the last bit is set
373+
// to 1.
380374
iv[CTRIvLength - 1] = 1;
381375

382-
383376
// Setting key and IV
384377
if (1 != EVP_DecryptInit_ex(ctx_, nullptr, nullptr, key, iv)) {
385378
throw ParquetException("Couldn't set key and IV");
386379
}
387380

388381
// Decryption
389-
if (!EVP_DecryptUpdate(ctx_, plaintext, &len, ciphertext + BufferSizeLength + CTRIvLength,
382+
if (!EVP_DecryptUpdate(ctx_, plaintext, &len,
383+
ciphertext + BufferSizeLength + CTRIvLength,
390384
ciphertext_len - CTRIvLength)) {
391385
throw ParquetException("Failed decryption update");
392386
}
@@ -402,10 +396,8 @@ int AesDecryptor::ctr_decrypt(const uint8_t* ciphertext, int ciphertext_len,
402396
return plaintext_len;
403397
}
404398

405-
int AesDecryptor::Decrypt(const uint8_t* ciphertext, int ciphertext_len,
406-
uint8_t* key, int key_len, uint8_t* aad, int aad_len,
407-
uint8_t* plaintext) {
408-
399+
int AesDecryptor::Decrypt(const uint8_t* ciphertext, int ciphertext_len, uint8_t* key,
400+
int key_len, uint8_t* aad, int aad_len, uint8_t* plaintext) {
409401
if (key_length_ != key_len) {
410402
std::stringstream ss;
411403
ss << "Wrong key length " << key_len << ". Should be " << key_length_;
@@ -425,44 +417,46 @@ static std::string shortToBytesLE(int16_t input) {
425417
memset(output, 0, 2);
426418
output[1] = (int8_t)(0xff & (input >> 8));
427419
output[0] = (int8_t)(0xff & (input));
428-
std::string output_str(reinterpret_cast<char const*>(output), 2) ;
420+
std::string output_str(reinterpret_cast<char const*>(output), 2);
429421

430422
return output_str;
431423
}
432424

433425
std::string createModuleAAD(const std::string& fileAAD, int8_t module_type,
434-
int16_t row_group_ordinal, int16_t column_ordinal,
435-
int16_t page_ordinal) {
436-
426+
int16_t row_group_ordinal, int16_t column_ordinal,
427+
int16_t page_ordinal) {
437428
int8_t type_ordinal_bytes[1];
438429
type_ordinal_bytes[0] = module_type;
439-
std::string type_ordinal_bytes_str(reinterpret_cast<char const*>(type_ordinal_bytes), 1) ;
430+
std::string type_ordinal_bytes_str(reinterpret_cast<char const*>(type_ordinal_bytes),
431+
1);
440432
if (Footer == module_type) {
441433
std::string result = fileAAD + type_ordinal_bytes_str;
442434
return result;
443435
}
444436
std::string row_group_ordinal_bytes = shortToBytesLE(row_group_ordinal);
445437
std::string column_ordinal_bytes = shortToBytesLE(column_ordinal);
446438
if (DataPage != module_type && DataPageHeader != module_type) {
447-
std::string result = fileAAD + type_ordinal_bytes_str + row_group_ordinal_bytes
448-
+ column_ordinal_bytes;
439+
std::string result =
440+
fileAAD + type_ordinal_bytes_str + row_group_ordinal_bytes + column_ordinal_bytes;
449441
return result;
450442
}
451443
std::string page_ordinal_bytes = shortToBytesLE(page_ordinal);
452-
std::string result = fileAAD + type_ordinal_bytes_str + row_group_ordinal_bytes
453-
+ column_ordinal_bytes + page_ordinal_bytes;;
444+
std::string result = fileAAD + type_ordinal_bytes_str + row_group_ordinal_bytes +
445+
column_ordinal_bytes + page_ordinal_bytes;
454446
return result;
455447
}
456448

457449
std::string createFooterAAD(const std::string& aad_prefix_bytes) {
458-
return createModuleAAD(aad_prefix_bytes, Footer, (int16_t) -1, (int16_t) -1, (int16_t) -1);
450+
return createModuleAAD(aad_prefix_bytes, Footer, (int16_t)-1, (int16_t)-1, (int16_t)-1);
459451
}
460452

461-
// Update last two bytes with new page ordinal (instead of creating new page AAD from scratch)
462-
void quickUpdatePageAAD(const std::string &AAD, int16_t new_page_ordinal) {
453+
// Update last two bytes with new page ordinal (instead of creating new page AAD from
454+
// scratch)
455+
void quickUpdatePageAAD(const std::string& AAD, int16_t new_page_ordinal) {
463456
std::string page_ordinal_bytes = shortToBytesLE(new_page_ordinal);
464457
int length = (int)AAD.size();
465-
std::memcpy((int16_t*)(AAD.c_str()+length-2), (int16_t*)(page_ordinal_bytes.c_str()), 2);
458+
std::memcpy((int16_t*)(AAD.c_str() + length - 2),
459+
(int16_t*)(page_ordinal_bytes.c_str()), 2);
466460
}
467461

468462
} // namespace parquet_encryption

0 commit comments

Comments
 (0)