| 500 | } |
| 501 | |
| 502 | UInt32 CompressionCodecEncrypted::doCompressData(const char * source, UInt32 source_size, char * dest) const |
| 503 | { |
| 504 | // Nonce, key and plaintext will be used to generate authentication tag |
| 505 | // and message encryption key. AES-GCM-SIV authenticates the encoded additional data and plaintext. |
| 506 | // For this purpose message_authentication_key is used. |
| 507 | // Algorithm is completely deterministic, but does not leak any |
| 508 | // information about the data block except for equivalence of |
| 509 | // identical blocks (under the same key). |
| 510 | |
| 511 | const std::string_view plaintext = std::string_view(source, source_size); |
| 512 | |
| 513 | /// Get key and nonce for encryption |
| 514 | UInt64 current_key_id = 0; |
| 515 | String current_key; |
| 516 | String nonce; |
| 517 | Configuration::instance().getCurrentKeyAndNonce(encryption_method, current_key_id, current_key, nonce); |
| 518 | |
| 519 | /// Write current key id to support multiple keys. |
| 520 | /// (key id in the beginning will help to decrypt data after changing current key) |
| 521 | char* ciphertext_with_nonce = writeVarUInt(current_key_id, dest); |
| 522 | size_t keyid_size = ciphertext_with_nonce - dest; |
| 523 | |
| 524 | /// write nonce in data. This will help to read data even after changing nonce in config |
| 525 | /// If there were no nonce in data, one zero byte will be written |
| 526 | char* ciphertext = writeNonce(nonce, ciphertext_with_nonce); |
| 527 | UInt64 nonce_size = ciphertext - ciphertext_with_nonce; |
| 528 | |
| 529 | // The ciphertext and the authentication tag will be written directly in the dest buffer. |
| 530 | size_t out_len = encrypt(plaintext, ciphertext, encryption_method, current_key, nonce); |
| 531 | |
| 532 | /// Length of encrypted text should be equal to text length plus tag_size (which was added by algorithm). |
| 533 | if (out_len != source_size + tag_size) |
| 534 | throw Exception(ErrorCodes::LOGICAL_ERROR, |
| 535 | "Can't encrypt data, length after encryption {} is wrong, expected {}", |
| 536 | out_len, source_size + tag_size); |
| 537 | |
| 538 | size_t out_size = out_len + keyid_size + nonce_size; |
| 539 | return safe_cast<UInt32>(out_size); |
| 540 | } |
| 541 | |
| 542 | UInt32 CompressionCodecEncrypted::doDecompressData(const char * source, UInt32 source_size, char * dest, UInt32 uncompressed_size) const |
| 543 | { |
nothing calls this directly
no test coverage detected