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

Method CtrDecrypt

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

Source from the content-addressed store, hash-verified

627}
628
629int32_t AesDecryptor::AesDecryptorImpl::CtrDecrypt(std::span<const uint8_t> ciphertext,
630 std::span<const uint8_t> key,
631 std::span<uint8_t> plaintext) {
632 int len;
633 int32_t plaintext_len;
634
635 std::array<uint8_t, kCtrIvLength> iv{};
636
637 int32_t ciphertext_len = GetCiphertextLength(ciphertext);
638
639 if (plaintext.size() < static_cast<size_t>(ciphertext_len) - ciphertext_size_delta_) {
640 std::stringstream ss;
641 ss << "Plaintext buffer length " << plaintext.size() << " is insufficient "
642 << "for ciphertext length " << ciphertext_len;
643 throw ParquetException(ss.str());
644 }
645
646 if (ciphertext_len < length_buffer_length_ + kNonceLength) {
647 std::stringstream ss;
648 ss << "Invalid ciphertext length " << ciphertext_len << ". Expected at least "
649 << length_buffer_length_ + kNonceLength << "\n";
650 throw ParquetException(ss.str());
651 }
652
653 // Extracting nonce
654 std::copy(ciphertext.begin() + length_buffer_length_,
655 ciphertext.begin() + length_buffer_length_ + kNonceLength, iv.begin());
656 // Parquet CTR IVs are comprised of a 12-byte nonce and a 4-byte initial
657 // counter field.
658 // The first 31 bits of the initial counter field are set to 0, the last bit
659 // is set to 1.
660 iv[kCtrIvLength - 1] = 1;
661
662 auto ctx = MakeCipherContext();
663
664 // Setting key and IV
665 if (1 != EVP_DecryptInit_ex(ctx.get(), nullptr, nullptr, key.data(), iv.data())) {
666 throw ParquetException("Couldn't set key and IV");
667 }
668
669 // Decryption
670 int decryption_length = ciphertext_len - length_buffer_length_ - kNonceLength;
671 if (!EVP_DecryptUpdate(ctx.get(), plaintext.data(), &len,
672 ciphertext.data() + length_buffer_length_ + kNonceLength,
673 decryption_length)) {
674 throw ParquetException("Failed decryption update");
675 }
676
677 plaintext_len = len;
678
679 // Finalization
680 if (1 != EVP_DecryptFinal_ex(ctx.get(), plaintext.data() + len, &len)) {
681 throw ParquetException("Failed decryption finalization");
682 }
683
684 plaintext_len += len;
685 return plaintext_len;
686}

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected