YPC  0.2.0
model_interface.h
1 #pragma once
2 #include "ypc/core_t/analyzer/internal/keymgr_session.h"
3 #include "ypc/core_t/analyzer/var/enclave_hash_var.h"
4 #include "ypc/core_t/analyzer/var/keymgr_var.h"
5 #include "ypc/core_t/analyzer/var/model_var.h"
6 #include "ypc/core_t/analyzer/var/request_key_var.h"
7 #include "ypc/corecommon/nt_cols.h"
8 #include "ypc/corecommon/package.h"
9 #include "ypc/stbox/ebyte.h"
10 #include "ypc/stbox/stx_status.h"
11 
12 namespace ypc {
13 namespace internal {
14 typedef nt<stbox::bytes> ntt;
15 
16 template <typename Crypto, typename ModelT,
17  bool has_model = !std::is_same<ModelT, void>::value>
18 class model_interface {};
19 
20 template <typename Crypto, typename ModelT>
21 class model_interface<Crypto, ModelT, true>
22  : virtual public model_var<ModelT>,
23  virtual public enclave_hash_var,
24  virtual public internal::keymgr_session,
25  virtual public keymgr_interface<Crypto>,
26  virtual public request_key_var<true> {
27  typedef Crypto ecc;
31 
32 public:
33  uint32_t init_model(const uint8_t *model, uint32_t len) {
34  auto ret = internal::keymgr_session::init_keymgr_session();
35  if (ret) {
36  LOG(ERROR) << "init_keymgr_session failed: " << stbox::status_string(ret);
37  return ret;
38  }
39  ntt::model_t mod =
41  len);
42 
43  stbox::bytes private_key, dian_pkey;
44  ret = keymgr_interface_t::request_private_key_for_public_key(
45  mod.get<ntt::pkey>(), private_key, dian_pkey);
46  if (ret) {
47  LOG(ERROR) << "request_private_key failed: " << stbox::status_string(ret);
48  return ret;
49  }
50 
51 
52  auto model_data = mod.get<ntt::model_data>();
53  stbox::bytes decrypted_model(
54  ecc::get_decrypt_message_size_with_prefix(model_data.size()));
55 
56  ret = ecc::decrypt_message_with_prefix(
57  private_key.data(), private_key.size(), model_data.data(),
58  model_data.size(), decrypted_model.data(), decrypted_model.size(),
59  ypc::utc::crypto_prefix_arbitrary);
60  if (ret) {
61  LOG(ERROR) << "decrypt_message_with_prefix failed: "
62  << stbox::status_string(ret);
63  return ret;
64  }
65 
66  model_var_t::m_model =
68  decrypted_model);
69  model_var_t::m_model_pkey = mod.get<ntt::pkey>();
70  ret = ecc::hash_256(decrypted_model, model_var_t::m_model_hash);
71  if (ret) {
72  LOG(ERROR) << "hash_256 failed: " << stbox::status_string(ret);
73  return ret;
74  }
75 
76  return stbox::stx_status::success;
77  }
78 };
79 } // namespace internal
80 } // namespace ypc
ypc::internal::model_var
Definition: model_var.h:8
ypc::nt
Definition: nt_cols.h:6
ypc::internal::enclave_hash_var
Definition: enclave_hash_var.h:7
ypc::internal::model_interface
Definition: model_interface.h:18
ypc::internal::keymgr_session
Definition: keymgr_session.h:17
ypc::internal::request_key_var
Definition: request_key_var.h:7
ypc::internal::keymgr_interface
Definition: keymgr_interface.h:18
ypc::make_package
Definition: package.h:48
ypc::crypto::crypto_pack
Definition: crypto_pack.h:14
ypc::internal::request_key_var< true >
Definition: request_key_var.h:9