Use Sha256State directly.
GitOrigin-RevId: 4fbbafbd14c43a551e9c24de65a6feed2b44b09e
This commit is contained in:
parent
2b7511d07f
commit
4ee295a29e
@ -178,10 +178,10 @@ void ObfuscatedTransport::init(ChainBufferReader *input, ChainBufferWriter *outp
|
|||||||
auto fix_key = [&](UInt256 &key) {
|
auto fix_key = [&](UInt256 &key) {
|
||||||
if (!proxy_secret.empty()) {
|
if (!proxy_secret.empty()) {
|
||||||
Sha256State state;
|
Sha256State state;
|
||||||
sha256_init(&state);
|
state.init();
|
||||||
sha256_update(as_slice(key), &state);
|
state.feed(as_slice(key));
|
||||||
sha256_update(proxy_secret, &state);
|
state.feed(proxy_secret);
|
||||||
sha256_final(&state, as_slice(key));
|
state.extract(as_slice(key));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
fix_key(key);
|
fix_key(key);
|
||||||
|
@ -144,13 +144,13 @@ size_t Transport::calc_crypto_size(size_t data_size) {
|
|||||||
std::pair<uint32, UInt128> Transport::calc_message_key2(const AuthKey &auth_key, int X, Slice to_encrypt) {
|
std::pair<uint32, UInt128> Transport::calc_message_key2(const AuthKey &auth_key, int X, Slice to_encrypt) {
|
||||||
// msg_key_large = SHA256 (substr (auth_key, 88+x, 32) + plaintext + random_padding);
|
// msg_key_large = SHA256 (substr (auth_key, 88+x, 32) + plaintext + random_padding);
|
||||||
Sha256State state;
|
Sha256State state;
|
||||||
sha256_init(&state);
|
state.init();
|
||||||
sha256_update(Slice(auth_key.key()).substr(88 + X, 32), &state);
|
state.feed(Slice(auth_key.key()).substr(88 + X, 32));
|
||||||
sha256_update(to_encrypt, &state);
|
state.feed(to_encrypt);
|
||||||
|
|
||||||
uint8 msg_key_large_raw[32];
|
uint8 msg_key_large_raw[32];
|
||||||
MutableSlice msg_key_large(msg_key_large_raw, sizeof(msg_key_large_raw));
|
MutableSlice msg_key_large(msg_key_large_raw, sizeof(msg_key_large_raw));
|
||||||
sha256_final(&state, msg_key_large);
|
state.extract(msg_key_large);
|
||||||
|
|
||||||
// msg_key = substr (msg_key_large, 8, 16);
|
// msg_key = substr (msg_key_large, 8, 16);
|
||||||
UInt128 res;
|
UInt128 res;
|
||||||
|
@ -63,13 +63,13 @@ static Status data_view_for_each(const DataView &data, F &&f) {
|
|||||||
|
|
||||||
Result<ValueHash> calc_value_hash(const DataView &data_view) {
|
Result<ValueHash> calc_value_hash(const DataView &data_view) {
|
||||||
Sha256State state;
|
Sha256State state;
|
||||||
sha256_init(&state);
|
state.init();
|
||||||
TRY_STATUS(data_view_for_each(data_view, [&state](BufferSlice bytes) {
|
TRY_STATUS(data_view_for_each(data_view, [&state](BufferSlice bytes) {
|
||||||
sha256_update(bytes.as_slice(), &state);
|
state.feed(bytes.as_slice());
|
||||||
return Status::OK();
|
return Status::OK();
|
||||||
}));
|
}));
|
||||||
UInt256 res;
|
UInt256 res;
|
||||||
sha256_final(&state, as_slice(res));
|
state.extract(as_slice(res));
|
||||||
return ValueHash{res};
|
return ValueHash{res};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,7 +267,7 @@ EncryptedSecret::EncryptedSecret(UInt256 encrypted_secret) : encrypted_secret_(e
|
|||||||
}
|
}
|
||||||
|
|
||||||
Decryptor::Decryptor(AesCbcState aes_cbc_state) : aes_cbc_state_(std::move(aes_cbc_state)) {
|
Decryptor::Decryptor(AesCbcState aes_cbc_state) : aes_cbc_state_(std::move(aes_cbc_state)) {
|
||||||
sha256_init(&sha256_state_);
|
sha256_state_.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<BufferSlice> Decryptor::append(BufferSlice data) {
|
Result<BufferSlice> Decryptor::append(BufferSlice data) {
|
||||||
@ -278,7 +278,7 @@ Result<BufferSlice> Decryptor::append(BufferSlice data) {
|
|||||||
return Status::Error("Part size should be divisible by 16");
|
return Status::Error("Part size should be divisible by 16");
|
||||||
}
|
}
|
||||||
aes_cbc_state_.decrypt(data.as_slice(), data.as_slice());
|
aes_cbc_state_.decrypt(data.as_slice(), data.as_slice());
|
||||||
sha256_update(data.as_slice(), &sha256_state_);
|
sha256_state_.feed(data.as_slice());
|
||||||
if (!skipped_prefix_) {
|
if (!skipped_prefix_) {
|
||||||
to_skip_ = data.as_slice().ubegin()[0];
|
to_skip_ = data.as_slice().ubegin()[0];
|
||||||
size_t to_skip = min(to_skip_, data.size());
|
size_t to_skip = min(to_skip_, data.size());
|
||||||
@ -298,8 +298,9 @@ Result<ValueHash> Decryptor::finish() {
|
|||||||
if (to_skip_ < 32) {
|
if (to_skip_ < 32) {
|
||||||
return Status::Error("Too small random prefix");
|
return Status::Error("Too small random prefix");
|
||||||
}
|
}
|
||||||
|
|
||||||
UInt256 res;
|
UInt256 res;
|
||||||
sha256_final(&sha256_state_, as_slice(res));
|
sha256_state_.extract(as_slice(res));
|
||||||
return ValueHash{res};
|
return ValueHash{res};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ Status FileHashUploader::init() {
|
|||||||
return Status::Error("size mismatch");
|
return Status::Error("size mismatch");
|
||||||
}
|
}
|
||||||
fd_ = BufferedFd<FileFd>(std::move(fd));
|
fd_ = BufferedFd<FileFd>(std::move(fd));
|
||||||
sha256_init(&sha256_state_);
|
sha256_state_.init();
|
||||||
|
|
||||||
resource_state_.set_unit_size(1024);
|
resource_state_.set_unit_size(1024);
|
||||||
resource_state_.update_estimated_limit(size_);
|
resource_state_.update_estimated_limit(size_);
|
||||||
@ -69,7 +69,7 @@ Status FileHashUploader::loop_impl() {
|
|||||||
if (state_ == State::NetRequest) {
|
if (state_ == State::NetRequest) {
|
||||||
// messages.getDocumentByHash#338e2464 sha256:bytes size:int mime_type:string = Document;
|
// messages.getDocumentByHash#338e2464 sha256:bytes size:int mime_type:string = Document;
|
||||||
auto hash = BufferSlice(32);
|
auto hash = BufferSlice(32);
|
||||||
sha256_final(&sha256_state_, hash.as_slice());
|
sha256_state_.extract(hash.as_slice());
|
||||||
auto mime_type = MimeType::from_extension(PathView(local_.path_).extension(), "image/gif");
|
auto mime_type = MimeType::from_extension(PathView(local_.path_).extension(), "image/gif");
|
||||||
auto query =
|
auto query =
|
||||||
telegram_api::messages_getDocumentByHash(std::move(hash), static_cast<int32>(size_), std::move(mime_type));
|
telegram_api::messages_getDocumentByHash(std::move(hash), static_cast<int32>(size_), std::move(mime_type));
|
||||||
@ -101,7 +101,7 @@ Status FileHashUploader::loop_sha() {
|
|||||||
if (ready.empty()) {
|
if (ready.empty()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
sha256_update(ready, &sha256_state_);
|
sha256_state_.feed(ready);
|
||||||
fd_.input_buffer().confirm_read(ready.size());
|
fd_.input_buffer().confirm_read(ready.size());
|
||||||
}
|
}
|
||||||
resource_state_.stop_use(limit);
|
resource_state_.stop_use(limit);
|
||||||
|
@ -71,13 +71,13 @@ TEST(Crypto, Sha256State) {
|
|||||||
td::sha256(s, as_slice(baseline));
|
td::sha256(s, as_slice(baseline));
|
||||||
|
|
||||||
td::Sha256State state;
|
td::Sha256State state;
|
||||||
td::sha256_init(&state);
|
state.init();
|
||||||
auto v = td::rand_split(s);
|
auto v = td::rand_split(s);
|
||||||
for (auto &x : v) {
|
for (auto &x : v) {
|
||||||
td::sha256_update(x, &state);
|
state.feed(x);
|
||||||
}
|
}
|
||||||
td::UInt256 result;
|
td::UInt256 result;
|
||||||
td::sha256_final(&state, as_slice(result));
|
state.extract(as_slice(result));
|
||||||
ASSERT_TRUE(baseline == result);
|
ASSERT_TRUE(baseline == result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user