51 void AESEncrypter::Init(
const std::vector<unsigned char>& pub_key,
const std::vector<unsigned char>& input_challenge,
52 std::vector<unsigned char>& raw_shared_secret, std::vector<unsigned char>& encrypted_shared_secret,
53 std::vector<unsigned char>& encrypted_challenge)
56 const unsigned char* pub_key_ptr = pub_key.data();
58 RSA* rsa = d2i_RSA_PUBKEY(
nullptr, &pub_key_ptr,
static_cast<long>(pub_key.size()));
60 std::mt19937 random_gen(
static_cast<unsigned int>(std::chrono::steady_clock::now().time_since_epoch().count()));
61 std::uniform_int_distribution<unsigned int> random_dist(0, 255);
63 raw_shared_secret = std::vector<unsigned char>(AES_BLOCK_SIZE);
65 for (
int i = 0; i < AES_BLOCK_SIZE; ++i)
67 raw_shared_secret[i] = random_dist(random_gen);
70 int rsa_size = RSA_size(rsa);
72 encrypted_shared_secret = std::vector<unsigned char>(rsa_size);
73 RSA_public_encrypt(AES_BLOCK_SIZE, raw_shared_secret.data(), encrypted_shared_secret.data(), rsa, RSA_PKCS1_PADDING);
74#if PROTOCOL_VERSION < 759
76 encrypted_nonce = std::vector<unsigned char>(rsa_size);
77 RSA_public_encrypt(
static_cast<int>(input_nonce.size()), input_nonce.data(), encrypted_nonce.data(), rsa, RSA_PKCS1_PADDING);
78#elif PROTOCOL_VERSION < 761
81 salt = std::uniform_int_distribution<long long int>(std::numeric_limits<long long int>::min(), std::numeric_limits<long long int>::max())(random_gen);
82 std::array<unsigned char, 8> salt_bytes;
83 for (
int i = 0; i < 8; ++i)
85 salt_bytes[i] =
static_cast<unsigned char>((salt >> (8 * (7 - i))) & 0xFF);
88 std::array<unsigned char, SHA256_DIGEST_LENGTH> salted_hash;
91 SHA256_Update(&sha256, input_nonce.data(), input_nonce.size());
92 SHA256_Update(&sha256, salt_bytes.data(), salt_bytes.size());
93 SHA256_Final(salted_hash.data(), &sha256);
96 RSA* rsa_signature =
nullptr;
97 const char* c_string = private_key.c_str();
98 BIO* keybio = BIO_new_mem_buf((
void*)c_string, -1);
99 rsa_signature = PEM_read_bio_RSAPrivateKey(keybio, &rsa_signature, NULL, NULL);
103 const int rsa_signature_size = RSA_size(rsa_signature);
104 salted_nonce_signature = std::vector<unsigned char>(rsa_signature_size);
105 unsigned int salted_nonce_signature_size;
106 RSA_sign(NID_sha256, salted_hash.data(),
static_cast<unsigned int>(salted_hash.size()), salted_nonce_signature.data(), &salted_nonce_signature_size, rsa_signature);
107 RSA_free(rsa_signature);
108 salted_nonce_signature.resize(salted_nonce_signature_size);
111 encrypted_challenge = std::vector<unsigned char>(rsa_size);
112 RSA_public_encrypt(
static_cast<int>(input_challenge.size()), input_challenge.data(), encrypted_challenge.data(), rsa, RSA_PKCS1_PADDING);
116 encryption_context = EVP_CIPHER_CTX_new();
117 EVP_EncryptInit_ex(encryption_context, EVP_aes_128_cfb8(),
nullptr, raw_shared_secret.data(), raw_shared_secret.data());
119 decryption_context = EVP_CIPHER_CTX_new();
120 EVP_DecryptInit_ex(decryption_context, EVP_aes_128_cfb8(),
nullptr, raw_shared_secret.data(), raw_shared_secret.data());
122 blocksize = EVP_CIPHER_block_size(EVP_aes_128_cfb8());