| 540 | } |
| 541 | |
| 542 | UInt32 CompressionCodecEncrypted::doDecompressData(const char * source, UInt32 source_size, char * dest, UInt32 uncompressed_size) const |
| 543 | { |
| 544 | /// The key is needed for decrypting. That's why it is read at the beginning of process. |
| 545 | UInt64 key_id = 0; |
| 546 | const char * ciphertext_with_nonce = readVarUInt(key_id, source, source_size); |
| 547 | |
| 548 | /// Size of text should be decreased by key_size, because key_size bytes were not participating in encryption process. |
| 549 | size_t keyid_size = ciphertext_with_nonce - source; |
| 550 | String nonce; |
| 551 | String key = Configuration::instance().getKey(encryption_method, key_id); |
| 552 | |
| 553 | /// try to read nonce from file (if it was set while encrypting) |
| 554 | const char * ciphertext = readNonce(nonce, ciphertext_with_nonce); |
| 555 | |
| 556 | /// Size of text should be decreased by nonce_size, because nonce_size bytes were not participating in encryption process. |
| 557 | UInt64 nonce_size = ciphertext - ciphertext_with_nonce; |
| 558 | |
| 559 | /// Count text size (nonce and key_id was read from source) |
| 560 | size_t ciphertext_size = source_size - keyid_size - nonce_size; |
| 561 | if (ciphertext_size != uncompressed_size + tag_size) |
| 562 | throw Exception(ErrorCodes::LOGICAL_ERROR, "Can't decrypt data, uncompressed_size {} is wrong, expected {}", |
| 563 | uncompressed_size, ciphertext_size - tag_size); |
| 564 | |
| 565 | |
| 566 | size_t out_len = decrypt({ciphertext, ciphertext_size}, dest, encryption_method, key, nonce); |
| 567 | if (out_len != ciphertext_size - tag_size) |
| 568 | throw Exception(ErrorCodes::LOGICAL_ERROR, |
| 569 | "Can't decrypt data, out length after decryption {} is wrong, expected {}", |
| 570 | out_len, ciphertext_size - tag_size); |
| 571 | |
| 572 | return static_cast<UInt32>(out_len); |
| 573 | } |
| 574 | |
| 575 | } |
| 576 | |