Use Sha256State directly.

GitOrigin-RevId: 4fbbafbd14c43a551e9c24de65a6feed2b44b09e
This commit is contained in:
levlam 2019-07-23 03:20:11 +03:00
parent 2b7511d07f
commit 4ee295a29e
5 changed files with 21 additions and 20 deletions

View File

@ -178,10 +178,10 @@ void ObfuscatedTransport::init(ChainBufferReader *input, ChainBufferWriter *outp
auto fix_key = [&](UInt256 &key) {
if (!proxy_secret.empty()) {
Sha256State state;
sha256_init(&state);
sha256_update(as_slice(key), &state);
sha256_update(proxy_secret, &state);
sha256_final(&state, as_slice(key));
state.init();
state.feed(as_slice(key));
state.feed(proxy_secret);
state.extract(as_slice(key));
}
};
fix_key(key);

View File

@ -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) {
// msg_key_large = SHA256 (substr (auth_key, 88+x, 32) + plaintext + random_padding);
Sha256State state;
sha256_init(&state);
sha256_update(Slice(auth_key.key()).substr(88 + X, 32), &state);
sha256_update(to_encrypt, &state);
state.init();
state.feed(Slice(auth_key.key()).substr(88 + X, 32));
state.feed(to_encrypt);
uint8 msg_key_large_raw[32];
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);
UInt128 res;

View File

@ -63,13 +63,13 @@ static Status data_view_for_each(const DataView &data, F &&f) {
Result<ValueHash> calc_value_hash(const DataView &data_view) {
Sha256State state;
sha256_init(&state);
state.init();
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();
}));
UInt256 res;
sha256_final(&state, as_slice(res));
state.extract(as_slice(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)) {
sha256_init(&sha256_state_);
sha256_state_.init();
}
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");
}
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_) {
to_skip_ = data.as_slice().ubegin()[0];
size_t to_skip = min(to_skip_, data.size());
@ -298,8 +298,9 @@ Result<ValueHash> Decryptor::finish() {
if (to_skip_ < 32) {
return Status::Error("Too small random prefix");
}
UInt256 res;
sha256_final(&sha256_state_, as_slice(res));
sha256_state_.extract(as_slice(res));
return ValueHash{res};
}

View File

@ -42,7 +42,7 @@ Status FileHashUploader::init() {
return Status::Error("size mismatch");
}
fd_ = BufferedFd<FileFd>(std::move(fd));
sha256_init(&sha256_state_);
sha256_state_.init();
resource_state_.set_unit_size(1024);
resource_state_.update_estimated_limit(size_);
@ -69,7 +69,7 @@ Status FileHashUploader::loop_impl() {
if (state_ == State::NetRequest) {
// messages.getDocumentByHash#338e2464 sha256:bytes size:int mime_type:string = Document;
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 query =
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()) {
break;
}
sha256_update(ready, &sha256_state_);
sha256_state_.feed(ready);
fd_.input_buffer().confirm_read(ready.size());
}
resource_state_.stop_use(limit);

View File

@ -71,13 +71,13 @@ TEST(Crypto, Sha256State) {
td::sha256(s, as_slice(baseline));
td::Sha256State state;
td::sha256_init(&state);
state.init();
auto v = td::rand_split(s);
for (auto &x : v) {
td::sha256_update(x, &state);
state.feed(x);
}
td::UInt256 result;
td::sha256_final(&state, as_slice(result));
state.extract(as_slice(result));
ASSERT_TRUE(baseline == result);
}
}