2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/ByteFlow.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/crypto.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
2018-12-19 22:18:53 +01:00
|
|
|
#include "td/utils/UInt.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
#if TD_HAVE_OPENSSL
|
2021-07-04 04:58:54 +02:00
|
|
|
class AesCtrByteFlow final : public ByteFlowInplaceBase {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
void init(const UInt256 &key, const UInt128 &iv) {
|
2019-08-12 15:53:17 +02:00
|
|
|
state_.init(as_slice(key), as_slice(iv));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
void init(AesCtrState &&state) {
|
|
|
|
state_ = std::move(state);
|
|
|
|
}
|
|
|
|
AesCtrState move_aes_ctr_state() {
|
|
|
|
return std::move(state_);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool loop() final {
|
2020-07-22 20:52:00 +02:00
|
|
|
bool result = false;
|
|
|
|
auto ready = input_->prepare_read();
|
|
|
|
if (!ready.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
state_.encrypt(ready, MutableSlice(const_cast<char *>(ready.data()), ready.size()));
|
|
|
|
input_->confirm_read(ready.size());
|
|
|
|
output_.advance_end(ready.size());
|
2020-07-22 20:52:00 +02:00
|
|
|
result = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-07-22 20:52:00 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!is_input_active_) {
|
|
|
|
finish(Status::OK()); // End of input stream.
|
|
|
|
}
|
2020-07-22 20:52:00 +02:00
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AesCtrState state_;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace td
|