Skip to content

Commit

Permalink
cryptography draft fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arseny30 committed Feb 24, 2021
1 parent 9c478e2 commit 143846f
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions test/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "td/mtproto/KDF.h"
#include "td/mtproto/Transport.h"


class Handshake {
public:
struct KeyPair {
Expand Down Expand Up @@ -184,18 +183,24 @@ void KDF2(Slice auth_key, const UInt128 &msg_key, int X, UInt256 *aes_key, UInt1
aes_iv_slice.substr(4).copy_from(sha256_a.substr(8, 8));
aes_iv_slice.substr(12).copy_from(sha256_b.substr(24, 4));
}
}
} // namespace td

td::SecureString encrypt(td::Slice key, td::Slice data, td::int32 seqno, int X) {
td::SecureString encrypt(td::Slice key, td::Slice data, td::int32 seqno, int X) {
td::SecureString res(data.size() + 4 + 16);
res.as_mutable_slice().substr(20).copy_from(data);
td::TlStorerUnsafe storer(res.as_mutable_slice().substr(16).ubegin());
storer.store_int(seqno);

// big endian
td::uint8 *ptr = res.as_mutable_slice().substr(16).ubegin();
ptr[0] = (seqno >> 24) & 255;
ptr[1] = (seqno >> 16) & 255;
ptr[2] = (seqno >> 8) & 255;
ptr[3] = (seqno)&255;

td::mtproto::AuthKey auth_key(0, key.str());
auto payload = res.as_mutable_slice().substr(16);
td::UInt128 msg_key = td::mtproto::Transport::calc_message_key2(auth_key, X, payload).second;
td::UInt256 aes_key;
td::UInt128 aes_iv;
td::UInt128 msg_key = td::mtproto::Transport::calc_message_key2(auth_key, X, payload).second;
td::UInt256 aes_key;
td::UInt128 aes_iv;
td::KDF2(key, msg_key, X, &aes_key, &aes_iv);
td::AesCtrState aes;
aes.init(aes_key.as_slice(), aes_iv.as_slice());
Expand All @@ -214,7 +219,6 @@ HandshakeTest gen_test() {
return res;
}


void run_test(const HandshakeTest &test) {
auto alice_secret = Handshake::calc_shared_secret(test.alice.private_key, test.bob.public_key).move_as_ok();
auto bob_secret = Handshake::calc_shared_secret(test.bob.private_key, test.alice.public_key).move_as_ok();
Expand Down Expand Up @@ -255,7 +259,6 @@ HandshakeTest pregenerated_test() {
"HYe8tJdEwHcmlnXq7LtprigzExaNJS7skfOo2irClj-7EL06-jMrhfwngSJFsak8JFSw8s6R4fwCsr50")
.move_as_ok();


return test;
}

Expand Down

0 comments on commit 143846f

Please sign in to comment.