-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathencryption_internal.cc
More file actions
787 lines (660 loc) · 29.7 KB
/
encryption_internal.cc
File metadata and controls
787 lines (660 loc) · 29.7 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "parquet/encryption/encryption_internal.h"
#include <openssl/aes.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <algorithm>
#include <array>
#include <iostream>
#include <memory>
#include <span>
#include <sstream>
#include <string>
#include <vector>
#include "parquet/encryption/openssl_internal.h"
#include "parquet/exception.h"
using parquet::ParquetException;
namespace parquet::encryption {
constexpr int32_t kGcmMode = 0;
constexpr int32_t kCtrMode = 1;
constexpr int32_t kCtrIvLength = 16;
constexpr int32_t kBufferSizeLength = 4;
#define ENCRYPT_INIT(CTX, ALG) \
if (1 != EVP_EncryptInit_ex(CTX, ALG, nullptr, nullptr, nullptr)) { \
throw ParquetException("Couldn't init ALG encryption"); \
}
#define DECRYPT_INIT(CTX, ALG) \
if (1 != EVP_DecryptInit_ex(CTX, ALG, nullptr, nullptr, nullptr)) { \
throw ParquetException("Couldn't init ALG decryption"); \
}
class AesCryptoContext {
public:
AesCryptoContext(ParquetCipher::type alg_id, int32_t key_len, bool metadata,
bool include_length) {
openssl::EnsureInitialized();
length_buffer_length_ = include_length ? kBufferSizeLength : 0;
ciphertext_size_delta_ = length_buffer_length_ + kNonceLength;
if (ParquetCipher::AES_GCM_V1 != alg_id && ParquetCipher::AES_GCM_CTR_V1 != alg_id) {
std::stringstream ss;
ss << "Crypto algorithm " << alg_id << " is not supported";
throw ParquetException(ss.str());
}
if (16 != key_len && 24 != key_len && 32 != key_len) {
std::stringstream ss;
ss << "Wrong key length: " << key_len;
throw ParquetException(ss.str());
}
if (metadata || (ParquetCipher::AES_GCM_V1 == alg_id)) {
aes_mode_ = kGcmMode;
ciphertext_size_delta_ += kGcmTagLength;
} else {
aes_mode_ = kCtrMode;
}
key_length_ = key_len;
}
virtual ~AesCryptoContext() = default;
protected:
static void DeleteCipherContext(EVP_CIPHER_CTX* ctx) { EVP_CIPHER_CTX_free(ctx); }
using CipherContext = std::unique_ptr<EVP_CIPHER_CTX, decltype(&DeleteCipherContext)>;
static CipherContext NewCipherContext() {
auto ctx = CipherContext(EVP_CIPHER_CTX_new(), DeleteCipherContext);
if (!ctx) {
throw ParquetException("Couldn't init cipher context");
}
return ctx;
}
int32_t aes_mode_;
int32_t key_length_;
int32_t ciphertext_size_delta_;
int32_t length_buffer_length_;
};
class AesEncryptor::AesEncryptorImpl : public AesCryptoContext {
public:
explicit AesEncryptorImpl(ParquetCipher::type alg_id, int32_t key_len, bool metadata,
bool write_length);
int32_t Encrypt(std::span<const uint8_t> plaintext, std::span<const uint8_t> key,
std::span<const uint8_t> aad, std::span<uint8_t> ciphertext);
int32_t SignedFooterEncrypt(std::span<const uint8_t> footer,
std::span<const uint8_t> key, std::span<const uint8_t> aad,
std::span<const uint8_t> nonce,
std::span<uint8_t> encrypted_footer);
[[nodiscard]] int32_t CiphertextLength(int64_t plaintext_len) const {
if (plaintext_len < 0) {
std::stringstream ss;
ss << "Negative plaintext length " << plaintext_len;
throw ParquetException(ss.str());
} else if (plaintext_len >
std::numeric_limits<int32_t>::max() - ciphertext_size_delta_) {
std::stringstream ss;
ss << "Plaintext length " << plaintext_len << " plus ciphertext size delta "
<< ciphertext_size_delta_ << " overflows int32";
throw ParquetException(ss.str());
}
return static_cast<int32_t>(plaintext_len + ciphertext_size_delta_);
}
private:
[[nodiscard]] CipherContext MakeCipherContext() const;
int32_t GcmEncrypt(std::span<const uint8_t> plaintext, std::span<const uint8_t> key,
std::span<const uint8_t> nonce, std::span<const uint8_t> aad,
std::span<uint8_t> ciphertext);
int32_t CtrEncrypt(std::span<const uint8_t> plaintext, std::span<const uint8_t> key,
std::span<const uint8_t> nonce, std::span<uint8_t> ciphertext);
};
AesEncryptor::AesEncryptorImpl::AesEncryptorImpl(ParquetCipher::type alg_id,
int32_t key_len, bool metadata,
bool write_length)
: AesCryptoContext(alg_id, key_len, metadata, write_length) {}
AesCryptoContext::CipherContext AesEncryptor::AesEncryptorImpl::MakeCipherContext()
const {
auto ctx = NewCipherContext();
if (kGcmMode == aes_mode_) {
// Init AES-GCM with specified key length
if (16 == key_length_) {
ENCRYPT_INIT(ctx.get(), EVP_aes_128_gcm());
} else if (24 == key_length_) {
ENCRYPT_INIT(ctx.get(), EVP_aes_192_gcm());
} else if (32 == key_length_) {
ENCRYPT_INIT(ctx.get(), EVP_aes_256_gcm());
}
} else {
// Init AES-CTR with specified key length
if (16 == key_length_) {
ENCRYPT_INIT(ctx.get(), EVP_aes_128_ctr());
} else if (24 == key_length_) {
ENCRYPT_INIT(ctx.get(), EVP_aes_192_ctr());
} else if (32 == key_length_) {
ENCRYPT_INIT(ctx.get(), EVP_aes_256_ctr());
}
}
return ctx;
}
int32_t AesEncryptor::AesEncryptorImpl::SignedFooterEncrypt(
std::span<const uint8_t> footer, std::span<const uint8_t> key,
std::span<const uint8_t> aad, std::span<const uint8_t> nonce,
std::span<uint8_t> encrypted_footer) {
if (static_cast<size_t>(key_length_) != key.size()) {
std::stringstream ss;
ss << "Wrong key length " << key.size() << ". Should be " << key_length_;
throw ParquetException(ss.str());
}
if (encrypted_footer.size() != footer.size() + ciphertext_size_delta_) {
std::stringstream ss;
ss << "Encrypted footer buffer length " << encrypted_footer.size()
<< " does not match expected length " << (footer.size() + ciphertext_size_delta_);
throw ParquetException(ss.str());
}
if (kGcmMode != aes_mode_) {
throw ParquetException("Must use AES GCM (metadata) encryptor");
}
return GcmEncrypt(footer, key, nonce, aad, encrypted_footer);
}
int32_t AesEncryptor::AesEncryptorImpl::Encrypt(std::span<const uint8_t> plaintext,
std::span<const uint8_t> key,
std::span<const uint8_t> aad,
std::span<uint8_t> ciphertext) {
if (static_cast<size_t>(key_length_) != key.size()) {
std::stringstream ss;
ss << "Wrong key length " << key.size() << ". Should be " << key_length_;
throw ParquetException(ss.str());
}
if (ciphertext.size() != plaintext.size() + ciphertext_size_delta_) {
std::stringstream ss;
ss << "Ciphertext buffer length " << ciphertext.size()
<< " does not match expected length "
<< (plaintext.size() + ciphertext_size_delta_);
throw ParquetException(ss.str());
}
std::array<uint8_t, kNonceLength> nonce{};
// Random nonce
RAND_bytes(nonce.data(), kNonceLength);
if (kGcmMode == aes_mode_) {
return GcmEncrypt(plaintext, key, nonce, aad, ciphertext);
}
return CtrEncrypt(plaintext, key, nonce, ciphertext);
}
int32_t AesEncryptor::AesEncryptorImpl::GcmEncrypt(std::span<const uint8_t> plaintext,
std::span<const uint8_t> key,
std::span<const uint8_t> nonce,
std::span<const uint8_t> aad,
std::span<uint8_t> ciphertext) {
int len;
int32_t ciphertext_len;
std::array<uint8_t, kGcmTagLength> tag{};
if (nonce.size() != static_cast<size_t>(kNonceLength)) {
std::stringstream ss;
ss << "Invalid nonce size " << nonce.size() << ", expected " << kNonceLength;
throw ParquetException(ss.str());
}
auto ctx = MakeCipherContext();
// Setting key and IV (nonce)
if (1 != EVP_EncryptInit_ex(ctx.get(), nullptr, nullptr, key.data(), nonce.data())) {
throw ParquetException("Couldn't set key and nonce");
}
// Setting additional authenticated data
if (aad.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
std::stringstream ss;
ss << "AAD size " << aad.size() << " overflows int";
throw ParquetException(ss.str());
}
if ((!aad.empty()) && (1 != EVP_EncryptUpdate(ctx.get(), nullptr, &len, aad.data(),
static_cast<int>(aad.size())))) {
throw ParquetException("Couldn't set AAD");
}
// Encryption
if (plaintext.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
std::stringstream ss;
ss << "Plaintext size " << plaintext.size() << " overflows int";
throw ParquetException(ss.str());
}
if (1 != EVP_EncryptUpdate(
ctx.get(), ciphertext.data() + length_buffer_length_ + kNonceLength, &len,
plaintext.data(), static_cast<int>(plaintext.size()))) {
throw ParquetException("Failed encryption update");
}
ciphertext_len = len;
// Finalization
if (1 != EVP_EncryptFinal_ex(
ctx.get(), ciphertext.data() + length_buffer_length_ + kNonceLength + len,
&len)) {
throw ParquetException("Failed encryption finalization");
}
ciphertext_len += len;
// Getting the tag
if (1 !=
EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_GET_TAG, kGcmTagLength, tag.data())) {
throw ParquetException("Couldn't get AES-GCM tag");
}
// Copying the buffer size, nonce and tag to ciphertext
int32_t buffer_size = kNonceLength + ciphertext_len + kGcmTagLength;
if (length_buffer_length_ > 0) {
ciphertext[3] = static_cast<uint8_t>(0xff & (buffer_size >> 24));
ciphertext[2] = static_cast<uint8_t>(0xff & (buffer_size >> 16));
ciphertext[1] = static_cast<uint8_t>(0xff & (buffer_size >> 8));
ciphertext[0] = static_cast<uint8_t>(0xff & (buffer_size));
}
std::copy(nonce.begin(), nonce.begin() + kNonceLength,
ciphertext.begin() + length_buffer_length_);
std::copy(tag.begin(), tag.end(),
ciphertext.begin() + length_buffer_length_ + kNonceLength + ciphertext_len);
return length_buffer_length_ + buffer_size;
}
int32_t AesEncryptor::AesEncryptorImpl::CtrEncrypt(std::span<const uint8_t> plaintext,
std::span<const uint8_t> key,
std::span<const uint8_t> nonce,
std::span<uint8_t> ciphertext) {
int len;
int32_t ciphertext_len;
if (nonce.size() != static_cast<size_t>(kNonceLength)) {
std::stringstream ss;
ss << "Invalid nonce size " << nonce.size() << ", expected " << kNonceLength;
throw ParquetException(ss.str());
}
// Parquet CTR IVs are comprised of a 12-byte nonce and a 4-byte initial
// counter field.
// The first 31 bits of the initial counter field are set to 0, the last bit
// is set to 1.
std::array<uint8_t, kCtrIvLength> iv{};
std::copy(nonce.begin(), nonce.begin() + kNonceLength, iv.begin());
iv[kCtrIvLength - 1] = 1;
auto ctx = MakeCipherContext();
// Setting key and IV
if (1 != EVP_EncryptInit_ex(ctx.get(), nullptr, nullptr, key.data(), iv.data())) {
throw ParquetException("Couldn't set key and IV");
}
// Encryption
if (plaintext.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
std::stringstream ss;
ss << "Plaintext size " << plaintext.size() << " overflows int";
throw ParquetException(ss.str());
}
if (1 != EVP_EncryptUpdate(
ctx.get(), ciphertext.data() + length_buffer_length_ + kNonceLength, &len,
plaintext.data(), static_cast<int>(plaintext.size()))) {
throw ParquetException("Failed encryption update");
}
ciphertext_len = len;
// Finalization
if (1 != EVP_EncryptFinal_ex(
ctx.get(), ciphertext.data() + length_buffer_length_ + kNonceLength + len,
&len)) {
throw ParquetException("Failed encryption finalization");
}
ciphertext_len += len;
// Copying the buffer size and nonce to ciphertext
int32_t buffer_size = kNonceLength + ciphertext_len;
if (length_buffer_length_ > 0) {
ciphertext[3] = static_cast<uint8_t>(0xff & (buffer_size >> 24));
ciphertext[2] = static_cast<uint8_t>(0xff & (buffer_size >> 16));
ciphertext[1] = static_cast<uint8_t>(0xff & (buffer_size >> 8));
ciphertext[0] = static_cast<uint8_t>(0xff & (buffer_size));
}
std::copy(nonce.begin(), nonce.begin() + kNonceLength,
ciphertext.begin() + length_buffer_length_);
return length_buffer_length_ + buffer_size;
}
AesEncryptor::~AesEncryptor() = default;
int32_t AesEncryptor::SignedFooterEncrypt(std::span<const uint8_t> footer,
std::span<const uint8_t> key,
std::span<const uint8_t> aad,
std::span<const uint8_t> nonce,
std::span<uint8_t> encrypted_footer) {
return impl_->SignedFooterEncrypt(footer, key, aad, nonce, encrypted_footer);
}
int32_t AesEncryptor::CiphertextLength(int64_t plaintext_len) const {
return impl_->CiphertextLength(plaintext_len);
}
int32_t AesEncryptor::Encrypt(std::span<const uint8_t> plaintext,
std::span<const uint8_t> key, std::span<const uint8_t> aad,
std::span<uint8_t> ciphertext) {
return impl_->Encrypt(plaintext, key, aad, ciphertext);
}
AesEncryptor::AesEncryptor(ParquetCipher::type alg_id, int32_t key_len, bool metadata,
bool write_length)
: impl_{std::unique_ptr<AesEncryptorImpl>(
new AesEncryptorImpl(alg_id, key_len, metadata, write_length))} {}
class AesDecryptor::AesDecryptorImpl : AesCryptoContext {
public:
explicit AesDecryptorImpl(ParquetCipher::type alg_id, int32_t key_len, bool metadata,
bool contains_length);
int32_t Decrypt(std::span<const uint8_t> ciphertext, std::span<const uint8_t> key,
std::span<const uint8_t> aad, std::span<uint8_t> plaintext);
[[nodiscard]] int32_t PlaintextLength(int32_t ciphertext_len) const {
if (ciphertext_len < ciphertext_size_delta_) {
std::stringstream ss;
ss << "Ciphertext length " << ciphertext_len << " is invalid, expected at least "
<< ciphertext_size_delta_;
throw ParquetException(ss.str());
}
return ciphertext_len - ciphertext_size_delta_;
}
[[nodiscard]] int32_t CiphertextLength(int32_t plaintext_len) const {
if (plaintext_len < 0) {
std::stringstream ss;
ss << "Negative plaintext length " << plaintext_len;
throw ParquetException(ss.str());
} else if (plaintext_len >
std::numeric_limits<int32_t>::max() - ciphertext_size_delta_) {
std::stringstream ss;
ss << "Plaintext length " << plaintext_len << " plus ciphertext size delta "
<< ciphertext_size_delta_ << " overflows int32";
throw ParquetException(ss.str());
}
return plaintext_len + ciphertext_size_delta_;
}
private:
[[nodiscard]] CipherContext MakeCipherContext() const;
/// Get the actual ciphertext length, inclusive of the length buffer length,
/// and validate that the provided buffer size is large enough.
[[nodiscard]] int32_t GetCiphertextLength(std::span<const uint8_t> ciphertext) const;
int32_t GcmDecrypt(std::span<const uint8_t> ciphertext, std::span<const uint8_t> key,
std::span<const uint8_t> aad, std::span<uint8_t> plaintext);
int32_t CtrDecrypt(std::span<const uint8_t> ciphertext, std::span<const uint8_t> key,
std::span<uint8_t> plaintext);
};
int32_t AesDecryptor::Decrypt(std::span<const uint8_t> ciphertext,
std::span<const uint8_t> key, std::span<const uint8_t> aad,
std::span<uint8_t> plaintext) {
return impl_->Decrypt(ciphertext, key, aad, plaintext);
}
AesDecryptor::~AesDecryptor() {}
AesDecryptor::AesDecryptorImpl::AesDecryptorImpl(ParquetCipher::type alg_id,
int32_t key_len, bool metadata,
bool contains_length)
: AesCryptoContext(alg_id, key_len, metadata, contains_length) {}
AesCryptoContext::CipherContext AesDecryptor::AesDecryptorImpl::MakeCipherContext()
const {
auto ctx = NewCipherContext();
if (kGcmMode == aes_mode_) {
// Init AES-GCM with specified key length
if (16 == key_length_) {
DECRYPT_INIT(ctx.get(), EVP_aes_128_gcm());
} else if (24 == key_length_) {
DECRYPT_INIT(ctx.get(), EVP_aes_192_gcm());
} else if (32 == key_length_) {
DECRYPT_INIT(ctx.get(), EVP_aes_256_gcm());
}
} else {
// Init AES-CTR with specified key length
if (16 == key_length_) {
DECRYPT_INIT(ctx.get(), EVP_aes_128_ctr());
} else if (24 == key_length_) {
DECRYPT_INIT(ctx.get(), EVP_aes_192_ctr());
} else if (32 == key_length_) {
DECRYPT_INIT(ctx.get(), EVP_aes_256_ctr());
}
}
return ctx;
}
std::unique_ptr<AesEncryptor> AesEncryptor::Make(ParquetCipher::type alg_id,
int32_t key_len, bool metadata,
bool write_length) {
return std::make_unique<AesEncryptor>(alg_id, key_len, metadata, write_length);
}
AesDecryptor::AesDecryptor(ParquetCipher::type alg_id, int32_t key_len, bool metadata,
bool contains_length)
: impl_{std::make_unique<AesDecryptorImpl>(alg_id, key_len, metadata,
contains_length)} {}
std::unique_ptr<AesDecryptor> AesDecryptor::Make(ParquetCipher::type alg_id,
int32_t key_len, bool metadata) {
return std::make_unique<AesDecryptor>(alg_id, key_len, metadata);
}
int32_t AesDecryptor::PlaintextLength(int32_t ciphertext_len) const {
return impl_->PlaintextLength(ciphertext_len);
}
int32_t AesDecryptor::CiphertextLength(int32_t plaintext_len) const {
return impl_->CiphertextLength(plaintext_len);
}
int32_t AesDecryptor::AesDecryptorImpl::GetCiphertextLength(
std::span<const uint8_t> ciphertext) const {
if (length_buffer_length_ > 0) {
// Note: length_buffer_length_ must be either 0 or kBufferSizeLength
if (ciphertext.size() < static_cast<size_t>(kBufferSizeLength)) {
std::stringstream ss;
ss << "Ciphertext buffer length " << ciphertext.size()
<< " is insufficient to read the ciphertext length."
<< " At least " << kBufferSizeLength << " bytes are required.";
throw ParquetException(ss.str());
}
// Extract ciphertext length
uint32_t written_ciphertext_len = (static_cast<uint32_t>(ciphertext[3]) << 24) |
(static_cast<uint32_t>(ciphertext[2]) << 16) |
(static_cast<uint32_t>(ciphertext[1]) << 8) |
(static_cast<uint32_t>(ciphertext[0]));
if (written_ciphertext_len >
static_cast<uint32_t>(std::numeric_limits<int32_t>::max() -
length_buffer_length_)) {
std::stringstream ss;
ss << "Written ciphertext length " << written_ciphertext_len
<< " plus length buffer length " << length_buffer_length_ << " overflows int32";
throw ParquetException(ss.str());
} else if (ciphertext.size() <
static_cast<size_t>(written_ciphertext_len) + length_buffer_length_) {
std::stringstream ss;
ss << "Serialized ciphertext length "
<< (written_ciphertext_len + length_buffer_length_)
<< " is greater than the provided ciphertext buffer length "
<< ciphertext.size();
throw ParquetException(ss.str());
}
return static_cast<int32_t>(written_ciphertext_len) + length_buffer_length_;
} else {
if (ciphertext.size() > static_cast<size_t>(std::numeric_limits<int32_t>::max())) {
std::stringstream ss;
ss << "Ciphertext buffer length " << ciphertext.size() << " overflows int32";
throw ParquetException(ss.str());
}
return static_cast<int32_t>(ciphertext.size());
}
}
int32_t AesDecryptor::AesDecryptorImpl::GcmDecrypt(std::span<const uint8_t> ciphertext,
std::span<const uint8_t> key,
std::span<const uint8_t> aad,
std::span<uint8_t> plaintext) {
int len;
int32_t plaintext_len;
std::array<uint8_t, kGcmTagLength> tag{};
std::array<uint8_t, kNonceLength> nonce{};
int32_t ciphertext_len = GetCiphertextLength(ciphertext);
if (plaintext.size() < static_cast<size_t>(ciphertext_len) - ciphertext_size_delta_) {
std::stringstream ss;
ss << "Plaintext buffer length " << plaintext.size() << " is insufficient "
<< "for ciphertext length " << ciphertext_len;
throw ParquetException(ss.str());
}
if (ciphertext_len < length_buffer_length_ + kNonceLength + kGcmTagLength) {
std::stringstream ss;
ss << "Invalid ciphertext length " << ciphertext_len << ". Expected at least "
<< length_buffer_length_ + kNonceLength + kGcmTagLength << "\n";
throw ParquetException(ss.str());
}
// Extracting IV and tag
std::copy(ciphertext.begin() + length_buffer_length_,
ciphertext.begin() + length_buffer_length_ + kNonceLength, nonce.begin());
std::copy(ciphertext.begin() + ciphertext_len - kGcmTagLength,
ciphertext.begin() + ciphertext_len, tag.begin());
auto ctx = MakeCipherContext();
// Setting key and IV
if (1 != EVP_DecryptInit_ex(ctx.get(), nullptr, nullptr, key.data(), nonce.data())) {
throw ParquetException("Couldn't set key and IV");
}
// Setting additional authenticated data
if (aad.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
std::stringstream ss;
ss << "AAD size " << aad.size() << " overflows int";
throw ParquetException(ss.str());
}
if ((!aad.empty()) && (1 != EVP_DecryptUpdate(ctx.get(), nullptr, &len, aad.data(),
static_cast<int>(aad.size())))) {
throw ParquetException("Couldn't set AAD");
}
// Decryption
int decryption_length =
ciphertext_len - length_buffer_length_ - kNonceLength - kGcmTagLength;
if (!EVP_DecryptUpdate(ctx.get(), plaintext.data(), &len,
ciphertext.data() + length_buffer_length_ + kNonceLength,
decryption_length)) {
throw ParquetException("Failed decryption update");
}
plaintext_len = len;
// Checking the tag (authentication)
if (!EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_TAG, kGcmTagLength, tag.data())) {
throw ParquetException("Failed authentication");
}
// Finalization
if (1 != EVP_DecryptFinal_ex(ctx.get(), plaintext.data() + len, &len)) {
throw ParquetException("Failed decryption finalization");
}
plaintext_len += len;
return plaintext_len;
}
int32_t AesDecryptor::AesDecryptorImpl::CtrDecrypt(std::span<const uint8_t> ciphertext,
std::span<const uint8_t> key,
std::span<uint8_t> plaintext) {
int len;
int32_t plaintext_len;
std::array<uint8_t, kCtrIvLength> iv{};
int32_t ciphertext_len = GetCiphertextLength(ciphertext);
if (plaintext.size() < static_cast<size_t>(ciphertext_len) - ciphertext_size_delta_) {
std::stringstream ss;
ss << "Plaintext buffer length " << plaintext.size() << " is insufficient "
<< "for ciphertext length " << ciphertext_len;
throw ParquetException(ss.str());
}
if (ciphertext_len < length_buffer_length_ + kNonceLength) {
std::stringstream ss;
ss << "Invalid ciphertext length " << ciphertext_len << ". Expected at least "
<< length_buffer_length_ + kNonceLength << "\n";
throw ParquetException(ss.str());
}
// Extracting nonce
std::copy(ciphertext.begin() + length_buffer_length_,
ciphertext.begin() + length_buffer_length_ + kNonceLength, iv.begin());
// Parquet CTR IVs are comprised of a 12-byte nonce and a 4-byte initial
// counter field.
// The first 31 bits of the initial counter field are set to 0, the last bit
// is set to 1.
iv[kCtrIvLength - 1] = 1;
auto ctx = MakeCipherContext();
// Setting key and IV
if (1 != EVP_DecryptInit_ex(ctx.get(), nullptr, nullptr, key.data(), iv.data())) {
throw ParquetException("Couldn't set key and IV");
}
// Decryption
int decryption_length = ciphertext_len - length_buffer_length_ - kNonceLength;
if (!EVP_DecryptUpdate(ctx.get(), plaintext.data(), &len,
ciphertext.data() + length_buffer_length_ + kNonceLength,
decryption_length)) {
throw ParquetException("Failed decryption update");
}
plaintext_len = len;
// Finalization
if (1 != EVP_DecryptFinal_ex(ctx.get(), plaintext.data() + len, &len)) {
throw ParquetException("Failed decryption finalization");
}
plaintext_len += len;
return plaintext_len;
}
int32_t AesDecryptor::AesDecryptorImpl::Decrypt(std::span<const uint8_t> ciphertext,
std::span<const uint8_t> key,
std::span<const uint8_t> aad,
std::span<uint8_t> plaintext) {
if (static_cast<size_t>(key_length_) != key.size()) {
std::stringstream ss;
ss << "Wrong key length " << key.size() << ". Should be " << key_length_;
throw ParquetException(ss.str());
}
if (kGcmMode == aes_mode_) {
return GcmDecrypt(ciphertext, key, aad, plaintext);
}
return CtrDecrypt(ciphertext, key, plaintext);
}
static std::string ShortToBytesLe(int16_t input) {
int8_t output[2];
memset(output, 0, 2);
output[1] = static_cast<int8_t>(0xff & (input >> 8));
output[0] = static_cast<int8_t>(0xff & (input));
return std::string(reinterpret_cast<const char*>(output), 2);
}
static void CheckPageOrdinal(int32_t page_ordinal) {
if (ARROW_PREDICT_FALSE(page_ordinal > std::numeric_limits<int16_t>::max())) {
throw ParquetException("Encrypted Parquet files can't have more than " +
std::to_string(std::numeric_limits<int16_t>::max()) +
" pages per chunk: got " + std::to_string(page_ordinal));
}
}
std::string CreateModuleAad(const std::string& file_aad, int8_t module_type,
int16_t row_group_ordinal, int16_t column_ordinal,
int32_t page_ordinal) {
CheckPageOrdinal(page_ordinal);
const int16_t page_ordinal_short = static_cast<int16_t>(page_ordinal);
int8_t type_ordinal_bytes[1];
type_ordinal_bytes[0] = module_type;
std::string type_ordinal_bytes_str(reinterpret_cast<const char*>(type_ordinal_bytes),
1);
if (kFooter == module_type) {
std::string result = file_aad + type_ordinal_bytes_str;
return result;
}
std::string row_group_ordinal_bytes = ShortToBytesLe(row_group_ordinal);
std::string column_ordinal_bytes = ShortToBytesLe(column_ordinal);
if (kDataPage != module_type && kDataPageHeader != module_type) {
std::ostringstream out;
out << file_aad << type_ordinal_bytes_str << row_group_ordinal_bytes
<< column_ordinal_bytes;
return out.str();
}
std::string page_ordinal_bytes = ShortToBytesLe(page_ordinal_short);
std::ostringstream out;
out << file_aad << type_ordinal_bytes_str << row_group_ordinal_bytes
<< column_ordinal_bytes << page_ordinal_bytes;
return out.str();
}
std::string CreateFooterAad(const std::string& aad_prefix_bytes) {
return CreateModuleAad(aad_prefix_bytes, kFooter, static_cast<int16_t>(-1),
static_cast<int16_t>(-1), static_cast<int16_t>(-1));
}
// Update last two bytes with new page ordinal (instead of creating new page AAD
// from scratch)
void QuickUpdatePageAad(int32_t new_page_ordinal, std::string* AAD) {
CheckPageOrdinal(new_page_ordinal);
const std::string page_ordinal_bytes =
ShortToBytesLe(static_cast<int16_t>(new_page_ordinal));
std::memcpy(AAD->data() + AAD->length() - 2, page_ordinal_bytes.data(), 2);
}
void RandBytes(unsigned char* buf, size_t num) {
if (num > static_cast<size_t>(std::numeric_limits<int>::max())) {
std::stringstream ss;
ss << "Length " << num << " for RandBytes overflows int";
throw ParquetException(ss.str());
}
openssl::EnsureInitialized();
int status = RAND_bytes(buf, static_cast<int>(num));
if (status != 1) {
const auto error_code = ERR_get_error();
char buffer[256];
ERR_error_string_n(error_code, buffer, sizeof(buffer));
std::stringstream ss;
ss << "Failed to generate random bytes: " << buffer;
throw ParquetException(ss.str());
}
}
void EnsureBackendInitialized() { openssl::EnsureInitialized(); }
#undef ENCRYPT_INIT
#undef DECRYPT_INIT
} // namespace parquet::encryption