YPC  0.2.0
raw_data_provider.h
1 #pragma once
2 #include "eparser_t.h"
3 #include "hpda/extractor/extractor_base.h"
4 #include "ypc/common/limits.h"
5 #include "ypc/core_t/analyzer/data_source.h"
6 #include "ypc/core_t/ecommon/package.h"
7 #include "ypc/corecommon/package.h"
8 #include "ypc/stbox/ebyte.h"
9 #include "ypc/stbox/stx_common.h"
10 #include "ypc/stbox/tsgx/channel/dh_session_initiator.h"
11 #include "ypc/stbox/tsgx/log.h"
12 #include <ff/util/ntobject.h>
13 
14 #include "ypc/corecommon/crypto/stdeth.h"
15 
16 namespace ypc {
18 public:
19  typedef ::ypc::crypto::eth_sgx_crypto crypto;
20 
21  inline raw_data_provider(const ::stbox::bytes &hash)
22  : data_source_with_dhash(hash) {
23  crypto::hash_256(stbox::bytes("Fidelius"), m_actual_data_hash);
24  m_data_reach_end = false;
25  }
26 
27  virtual ~raw_data_provider() {}
28 
29  virtual bool process() {
30  if (m_data_reach_end) {
31  return false;
32  }
33  if (m_item_index + 1 < m_items.size()) {
34  m_item_index++;
35  return true;
36  } else {
37  uint8_t *t_sealed_data;
38  uint32_t t_sealed_data_len;
39  stbox::stx_status ret = static_cast<stbox::stx_status>(
40  stbox::ocall_cast<uint32_t>(next_data_batch)(
41  m_expect_data_hash.data(), m_expect_data_hash.size(),
42  &t_sealed_data, &t_sealed_data_len));
43 
44  if (ret != stbox::stx_status::success) {
45  m_data_reach_end = true;
46  return false;
47  }
48  // We need move the sealed data from untrusted memory to trusted memory
49  stbox::bytes sealed_data(t_sealed_data_len);
50  memcpy(sealed_data.data(), t_sealed_data, t_sealed_data_len);
53  stbox::ocall_cast<void>(free_data_batch)(t_sealed_data);
54 
55  typedef nt<stbox::bytes> ntt;
56  try {
58 
59  m_items = pkg.get<ntt::batch_data>();
60  if (m_items.size() == 0) {
61  m_data_reach_end = true;
62  return false;
63  }
64 
65  for (auto b : m_items) {
66  stbox::bytes k = m_actual_data_hash + b;
67  crypto::hash_256(k, m_actual_data_hash);
68  }
69 
70  m_item_index = 0;
71  return true;
72  } catch (const std::exception &e) {
73  LOG(ERROR) << "make_package got: " << e.what();
74  m_data_reach_end = true;
75  return false;
76  }
77 
78  }
79  }
80 
81  virtual data_source_output_t output_value() {
82  data_source_output_t ret;
83  ret.set<nt<bytes>::data>(m_items[m_item_index]);
84  return ret;
85  }
86 
87  virtual const bytes &data_hash() const { return m_actual_data_hash; }
88 
89 protected:
90  bytes m_actual_data_hash;
91  std::vector<stbox::bytes> m_items;
92  size_t m_item_index;
93 };
94 } // namespace ypc
ypc::raw_data_provider
Definition: raw_data_provider.h:17
ypc::data_source_with_dhash
Definition: data_source.h:15
ypc::nt
Definition: nt_cols.h:6
ypc::raw_data_provider::process
virtual bool process()
Definition: raw_data_provider.h:29
ypc::make_package
Definition: package.h:48
ypc::crypto::crypto_pack
Definition: crypto_pack.h:14