YPC  0.2.0
crypto_pack.h
1 #pragma once
2 #include "ypc/core/byte.h"
3 #include "ypc/corecommon/crypto/crypto_pack.h"
4 #include "ypc/corecommon/crypto/stdeth.h"
5 #include "ypc/corecommon/crypto/gmssl.h"
6 
7 
8 namespace ypc {
9 namespace terminus {
10 class crypto_pack {
11 public:
12  virtual bytes gen_ecc_private_key() = 0;
13  virtual bytes
14  gen_ecc_public_key_from_private_key(const bytes &private_key) = 0;
15 
16  virtual bytes hash_256(const bytes &msg) = 0;
17  virtual bytes sign_message(const bytes &msg, const bytes &private_key) = 0;
18 
19  virtual bool verify_message_signature(const bytes &sig, const bytes &message,
20  const bytes &pubkey) = 0;
21 
22  virtual bytes ecc_encrypt(const bytes &msg, const bytes &pubic_key,
23  uint32_t prefix) = 0;
24  virtual bytes ecc_decrypt(const bytes &cipher, const bytes &private_key,
25  uint32_t prefix) = 0;
26 };
27 
28 template <typename Crypto> class crypto_pack_t : public crypto_pack {
29 public:
30  typedef Crypto crypto_t;
31 
32  virtual bytes gen_ecc_private_key() {
33  bytes ret;
34  auto status = crypto_t::gen_private_key(ret);
35  if (status) {
36  std::cerr << "gen_ecc_private_key return " << std::hex << status
37  << std::endl;
38  throw std::runtime_error("terminus lib gen_ecc_private_key fail");
39  }
40  return ret;
41  }
42  virtual bytes gen_ecc_public_key_from_private_key(const bytes &private_key) {
43  bytes ret;
44  auto status = crypto_t::generate_pkey_from_skey(private_key, ret);
45  if (status) {
46  std::cerr << "gen_ecc_public_key_from_private_key return " << std::hex
47  << status << std::endl;
48  throw std::runtime_error(
49  "terminus lib gen_ecc_public_key_from_private_key fail");
50  }
51  return ret;
52  }
53  virtual bytes hash_256(const bytes &msg) {
54  bytes sig;
55  auto status = crypto_t::hash_256(msg, sig);
56  if (status) {
57  std::cerr << "hash_256 return " << std::hex << status << std::endl;
58  throw std::runtime_error("terminus lib hash_256 fail");
59  }
60  return sig;
61  }
62 
63  virtual bytes sign_message(const bytes &msg, const bytes &private_key) {
64  bytes ret;
65  auto status = crypto_t::sign_message(private_key, msg, ret);
66  if (status) {
67  std::cerr << "sign_message return " << std::hex << status << std::endl;
68  throw std::runtime_error("terminus lib sig_message fail");
69  }
70  return ret;
71  }
72 
73  virtual bool verify_message_signature(const bytes &sig, const bytes &message,
74  const bytes &pubkey) {
75  return stbox::stx_status::success ==
76  crypto_t::verify_signature(message, sig, pubkey);
77  }
78 
79  virtual bytes ecc_encrypt(const bytes &msg, const bytes &public_key,
80  uint32_t prefix) {
81  bytes ret;
82  uint32_t status =
83  crypto_t::encrypt_message_with_prefix(public_key, msg, prefix, ret);
84  if (status) {
85  std::cerr << "ecc_encrypt return " << std::hex << status << std::endl;
86  throw std::runtime_error("terminus lib ecc_encrypt fail");
87  }
88  return ret;
89  }
90 
91  virtual bytes ecc_decrypt(const bytes &cipher, const bytes &private_key,
92  uint32_t prefix) {
93  bytes ret;
94  uint32_t status =
95  crypto_t::decrypt_message_with_prefix(private_key, cipher, ret, prefix);
96  if (status) {
97  std::cerr << "ecc_decrypt return " << std::hex << status << std::endl;
98  throw std::runtime_error("terminus lib ecc_decrypt fail");
99  }
100  return ret;
101  }
102 };
103 
104 std::unique_ptr<crypto_pack> intel_sgx_and_eth_compatible();
105 std::unique_ptr<crypto_pack> sm_compatible();
106 
107 } // namespace terminus
108 } // namespace ypc
ypc::terminus::crypto_pack
Definition: crypto_pack.h:10
ypc::terminus::crypto_pack_t
Definition: crypto_pack.h:28
byte.h
ypc::utc::bytes< byte_t, ::ypc::utc::byte_encode::raw_bytes >
ypc::crypto::crypto_pack
Definition: crypto_pack.h:14