steam-min-cpp
Loading...
Searching...
No Matches
helpers.hpp
1
3#pragma once
4#include <cryptopp/aes.h>
5#include <cryptopp/crc.h>
6#include <cryptopp/files.h>
7#include <cryptopp/filters.h>
8#include <cryptopp/modes.h>
9#include <cryptopp/osrng.h>
10#include <cryptopp/rsa.h>
11#include <cryptopp/sha.h>
12
13#include <boost/endian/conversion.hpp>
14#include <cstdint>
15#include <stdexcept>
16#include <vector>
17
18
19namespace Steam::Crypto::Helpers {
20inline constexpr static size_t IV_LENGTH = 16;
21inline constexpr static size_t IV_RAND_LENGTH = 3;
22inline constexpr static size_t BLOCK_SIZE = 16;
23inline constexpr static size_t HASH_LENGTH = IV_LENGTH - IV_RAND_LENGTH;
24
25inline static std::vector<uint8_t> crc_to_vector(uint32_t crc) {
26 uint32_t le_crc = boost::endian::native_to_little(crc);
27 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&le_crc);
28
29 return std::vector<uint8_t>(bytes, bytes + sizeof(uint32_t));
30}
31
32inline std::vector<uint8_t> generate_random_bytes(
33 size_t length, CryptoPP::AutoSeededRandomPool& rng) {
34 std::vector<uint8_t> buffer(length);
35 rng.GenerateBlock(buffer.data(), buffer.size());
36 return buffer;
37}
38
39std::vector<uint8_t> rsa_encrypt_oaep_sha1(
40 const std::vector<uint8_t>& pubKeyDer,
41 const std::vector<uint8_t>& plaintext, CryptoPP::AutoSeededRandomPool& rng);
42uint32_t crc32_hash(const std::vector<uint8_t>& data);
43
44std::vector<uint8_t> symmetric_encrypt_hmac_iv(
45 const std::vector<uint8_t>& plaintext,
46 const std::vector<uint8_t>& session_key,
47 const std::vector<uint8_t>& hmac_secret,
48 CryptoPP::RandomNumberGenerator& rng);
49
50std::vector<uint8_t> symmetric_decrypt_hmac_iv(
51 const std::vector<uint8_t>& input, const std::vector<uint8_t>& session_key,
52 const std::vector<uint8_t>& hmac_secret);
53
54void generate_iv(const std::vector<uint8_t>& plaintext,
55 std::vector<uint8_t>& iv,
56 const std::vector<uint8_t>& hmac_secret,
57 CryptoPP::RandomNumberGenerator& rng);
58
59bool validate_iv(const std::vector<uint8_t>& plaintext,
60 const std::vector<uint8_t>& iv,
61 const std::vector<uint8_t>& hmac_secret);
62
63inline size_t const calculate_max_encrypted_length(size_t plaintext_len) {
64 return IV_LENGTH + (((plaintext_len + BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE);
65}
66
67} // namespace Steam::Crypto::Helpers