YPC  0.2.0
aes_impl.h
1 #pragma once
2 #include "ypc/common/byte.h"
3 #include "ypc/stbox/stx_status.h"
4 
5 namespace ypc {
6 namespace crypto {
7 namespace internal {
8 template <typename EC, typename AES, typename ECDH> class aes_gcm_impl {
9  typedef EC ecc_t;
10  typedef AES aes_t;
11  typedef ECDH ecdh_t;
12  typedef ::ypc::utc::bytes<uint8_t, ::ypc::utc::byte_encode::raw_bytes> bytes;
13 
14 public:
15  static uint32_t get_encrypt_message_size_with_prefix(uint32_t data_size) {
16  return aes_t::get_cipher_size(data_size) + ecc_t::get_public_key_size() +
17  aes_t::get_mac_code_size();
18  }
19 
20  static uint32_t encrypt_message_with_prefix(const uint8_t *public_key,
21  uint32_t pkey_size,
22  const uint8_t *data,
23  uint32_t data_size,
24  uint32_t prefix, uint8_t *cipher,
25  uint32_t cipher_size) {
26  if (aes_t::get_cipher_size(data_size) + ecc_t::get_public_key_size() +
27  aes_t::get_mac_code_size() !=
28  cipher_size) {
29  return stbox::stx_status::aes_invalid_cipher_size;
30  }
31  bytes gskey(ecc_t::get_private_key_size());
32 
33  // we may cache this for performance
34  uint32_t ret = ecc_t::gen_private_key(gskey.size(), gskey.data());
35  if (ret) {
36  return ret;
37  }
38 
39  uint8_t *gpkey = cipher + aes_t::get_cipher_size(data_size);
40  ret = ecc_t::generate_pkey_from_skey(gskey.data(), gskey.size(), gpkey,
41  ecc_t::get_public_key_size());
42  if (ret) {
43  return ret;
44  }
45  bytes shared_key(ecdh_t::get_ecdh_shared_key_size());
46  ret = ecdh_t::ecdh_shared_key(gskey.data(), gskey.size(), public_key,
47  pkey_size, shared_key.data(),
48  shared_key.size());
49  if (ret) {
50  return ret;
51  }
52 
53  uint8_t *p_out_mac = cipher + aes_t::get_cipher_size(data_size) +
54  ecc_t::get_public_key_size();
55 
56  ret = aes_t::encrypt_with_prefix(
57  shared_key.data(), shared_key.size(), data, data_size, prefix, cipher,
58  aes_t::get_cipher_size(data_size), p_out_mac);
59  return ret;
60  }
61 
62  template <typename BytesType>
63  static uint32_t encrypt_message_with_prefix(const BytesType &public_key,
64  const BytesType &data,
65  uint32_t prefix,
66  BytesType &cipher) {
67  cipher = BytesType(get_encrypt_message_size_with_prefix(data.size()));
68 
69  return encrypt_message_with_prefix(public_key.data(), public_key.size(),
70  data.data(), data.size(), prefix,
71  cipher.data(), cipher.size());
72  }
73 
74  static uint32_t get_decrypt_message_size_with_prefix(uint32_t cipher_size) {
75  return aes_t::get_data_size(cipher_size) - ecc_t::get_public_key_size() -
76  aes_t::get_mac_code_size();
77  }
78 
79  static uint32_t decrypt_message_with_prefix(const uint8_t *private_key,
80  uint32_t private_key_size,
81  const uint8_t *cipher,
82  uint32_t cipher_size,
83  uint8_t *data, uint32_t data_size,
84  uint32_t prefix) {
85  if (aes_t::get_cipher_size(data_size) + ecc_t::get_public_key_size() +
86  aes_t::get_mac_code_size() !=
87  cipher_size) {
88  return stbox::stx_status::aes_invalid_data_size;
89  }
90 
91  bytes shared_key(ecdh_t::get_ecdh_shared_key_size());
92 
93  uint32_t ret = ecdh_t::ecdh_shared_key(
94  private_key, private_key_size,
95  cipher + aes_t::get_cipher_size(data_size),
96  ecc_t::get_public_key_size(), shared_key.data(), shared_key.size());
97  if (ret) {
98  return ret;
99  }
100 
101  const uint8_t *p_in_mac = cipher + aes_t::get_cipher_size(data_size) +
102  ecc_t::get_public_key_size();
103 
104  ret = aes_t::decrypt_with_prefix(shared_key.data(), shared_key.size(),
105  cipher, aes_t::get_cipher_size(data_size),
106  prefix, data, data_size, p_in_mac);
107 
108  return ret;
109  }
110  template <typename BytesType>
111  static uint32_t decrypt_message_with_prefix(const BytesType &private_key,
112  const BytesType &cipher,
113  BytesType &data,
114  uint32_t prefix) {
115  data = BytesType(get_decrypt_message_size_with_prefix(cipher.size()));
116  return decrypt_message_with_prefix(private_key.data(), private_key.size(),
117  cipher.data(), cipher.size(),
118  data.data(), data.size(), prefix);
119  }
120 };
121 
122 template <typename Curve, typename AES, typename ECDH> class aes_impl {
123  // todo
124 };
125 } // namespace internal
126 } // namespace crypto
127 } // namespace ypc
ypc::crypto::internal::aes_impl
Definition: aes_impl.h:122
ypc::crypto::internal::aes_gcm_impl
Definition: aes_impl.h:8
ypc::utc::bytes
Definition: bytes.h:143