MCPcopy Create free account
hub / github.com/apache/arrow / GcmDecrypt

Method GcmDecrypt

cpp/src/parquet/encryption/encryption_internal.cc:554–627  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

552}
553
554int32_t AesDecryptor::AesDecryptorImpl::GcmDecrypt(std::span<const uint8_t> ciphertext,
555 std::span<const uint8_t> key,
556 std::span<const uint8_t> aad,
557 std::span<uint8_t> plaintext) {
558 int len;
559 int32_t plaintext_len;
560
561 std::array<uint8_t, kGcmTagLength> tag{};
562 std::array<uint8_t, kNonceLength> nonce{};
563
564 int32_t ciphertext_len = GetCiphertextLength(ciphertext);
565
566 if (plaintext.size() < static_cast<size_t>(ciphertext_len) - ciphertext_size_delta_) {
567 std::stringstream ss;
568 ss << "Plaintext buffer length " << plaintext.size() << " is insufficient "
569 << "for ciphertext length " << ciphertext_len;
570 throw ParquetException(ss.str());
571 }
572
573 if (ciphertext_len < length_buffer_length_ + kNonceLength + kGcmTagLength) {
574 std::stringstream ss;
575 ss << "Invalid ciphertext length " << ciphertext_len << ". Expected at least "
576 << length_buffer_length_ + kNonceLength + kGcmTagLength << "\n";
577 throw ParquetException(ss.str());
578 }
579
580 // Extracting IV and tag
581 std::copy(ciphertext.begin() + length_buffer_length_,
582 ciphertext.begin() + length_buffer_length_ + kNonceLength, nonce.begin());
583 std::copy(ciphertext.begin() + ciphertext_len - kGcmTagLength,
584 ciphertext.begin() + ciphertext_len, tag.begin());
585
586 auto ctx = MakeCipherContext();
587
588 // Setting key and IV
589 if (1 != EVP_DecryptInit_ex(ctx.get(), nullptr, nullptr, key.data(), nonce.data())) {
590 throw ParquetException("Couldn't set key and IV");
591 }
592
593 // Setting additional authenticated data
594 if (aad.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
595 std::stringstream ss;
596 ss << "AAD size " << aad.size() << " overflows int";
597 throw ParquetException(ss.str());
598 }
599 if ((!aad.empty()) && (1 != EVP_DecryptUpdate(ctx.get(), nullptr, &len, aad.data(),
600 static_cast<int>(aad.size())))) {
601 throw ParquetException("Couldn't set AAD");
602 }
603
604 // Decryption
605 int decryption_length =
606 ciphertext_len - length_buffer_length_ - kNonceLength - kGcmTagLength;
607 if (!EVP_DecryptUpdate(ctx.get(), plaintext.data(), &len,
608 ciphertext.data() + length_buffer_length_ + kNonceLength,
609 decryption_length)) {
610 throw ParquetException("Failed decryption update");
611 }

Callers

nothing calls this directly

Calls 7

ParquetExceptionFunction · 0.85
strMethod · 0.80
sizeMethod · 0.45
beginMethod · 0.45
getMethod · 0.45
dataMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected