SuperTuxKart
crypto_cryptokit.hpp
1 #ifdef APPLE_NETWORK_LIBRARIES
2 
3 #ifndef HEADER_CRYPTO_CRYPTOKIT_HPP
4 #define HEADER_CRYPTO_CRYPTOKIT_HPP
5 
6 #include <enet/enet.h>
7 
8 #include <algorithm>
9 #include <array>
10 #include <atomic>
11 #include <cassert>
12 #include <cstdint>
13 #include <memory>
14 #include <string>
15 #include <vector>
16 
17 class BareNetworkString;
18 class NetworkString;
19 
20 class Crypto
21 {
22 private:
23  static std::string m_client_key;
24 
25  static std::string m_client_iv;
26 
27  std::array<uint8_t, 12> m_iv;
28 
29  std::atomic_uint m_packet_counter;
30 
31  std::vector<uint8_t> m_key;
32 
33 public:
34  // ------------------------------------------------------------------------
35  static std::string base64(const std::vector<uint8_t>& input);
36  // ------------------------------------------------------------------------
37  static std::vector<uint8_t> decode64(std::string input);
38  // ------------------------------------------------------------------------
39  static std::array<uint8_t, 32> sha256(const std::string& input);
40  // ------------------------------------------------------------------------
41  static std::unique_ptr<Crypto> getClientCrypto(size_t tag_size)
42  {
43  assert(!m_client_key.empty());
44  assert(!m_client_iv.empty());
45  assert(tag_size == 16);
46  auto c = std::unique_ptr<Crypto>(new Crypto(decode64(m_client_key),
47  decode64(m_client_iv), tag_size));
48  c->m_packet_counter = 0;
49  return c;
50  }
51  // ------------------------------------------------------------------------
52  static void initClientAES();
53  // ------------------------------------------------------------------------
54  static void resetClientAES()
55  {
56  m_client_key = "";
57  m_client_iv = "";
58  }
59  // ------------------------------------------------------------------------
60  static const std::string& getClientKey() { return m_client_key; }
61  // ------------------------------------------------------------------------
62  static const std::string& getClientIV() { return m_client_iv; }
63  // ------------------------------------------------------------------------
64  Crypto(const std::vector<uint8_t>& key,
65  const std::vector<uint8_t>& iv,
66  size_t tag_size = 16)
67  {
68  assert(key.size() == 16);
69  assert(iv.size() == 12);
70  assert(tag_size == 16);
71  std::copy_n(iv.begin(), 12, m_iv.begin());
72  m_key = key;
73  m_packet_counter = 0;
74  }
75  // ------------------------------------------------------------------------
76  ~Crypto() {}
77  // ------------------------------------------------------------------------
78  bool encryptConnectionRequest(BareNetworkString& ns);
79  // ------------------------------------------------------------------------
80  bool decryptConnectionRequest(BareNetworkString& ns);
81  // ------------------------------------------------------------------------
82  ENetPacket* encryptSend(BareNetworkString& ns, bool reliable);
83  // ------------------------------------------------------------------------
84  NetworkString* decryptRecieve(ENetPacket* p);
85 };
86 
87 #endif // HEADER_CRYPTO_CRYPTOKIT_HPP
88 
89 #endif
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
A new implementation of NetworkString, which has a fixed format: Byte 0: The type of the message,...
Definition: network_string.hpp:422
CScriptArray * sha256(std::string *input)
Return a sha256 checksum of string in an array of integers of size 32.
Definition: script_utils.cpp:163