YPC  0.2.0
sign_impl.h
1 #pragma once
2 #include "ypc/common/byte.h"
3 
4 namespace ypc {
5 namespace crypto {
6 namespace internal {
7 template <typename Hash, typename EC> struct sign_impl {
8  typedef Hash hash_t;
9  typedef EC ecc_t;
10  typedef ::ypc::utc::bytes<uint8_t, ::ypc::utc::byte_encode::raw_bytes> bytes;
11 
12 public:
13  static uint32_t get_signature_size() { return ecc_t::get_signature_size(); }
14 
15  static uint32_t sign_message(const uint8_t *skey, uint32_t skey_size,
16  const uint8_t *data, uint32_t data_size,
17  uint8_t *sig, uint32_t sig_size) {
18  bytes hash(hash_t::get_msg_hash_size());
19  uint32_t ret = hash_t::msg_hash(data, data_size, hash.data(), hash.size());
20  if (ret) {
21  return ret;
22  }
23  ret = ecc_t::sign_message(skey, skey_size, hash.data(), hash.size(), sig,
24  sig_size);
25  return ret;
26  }
27 
28  static uint32_t verify_signature(const uint8_t *data, uint32_t data_size,
29  const uint8_t *sig, uint32_t sig_size,
30  const uint8_t *public_key,
31  uint32_t pkey_size) {
32  uint32_t hash_size = hash_t::get_msg_hash_size();
33  bytes hash(hash_size);
34  uint32_t ret = hash_t::msg_hash(data, data_size, hash.data(), hash_size);
35  if (ret) {
36  //iris example didn't enter this condition
37  return ret;
38  }
39  ret = ecc_t::verify_signature(hash.data(), hash_size, sig, sig_size,
40  public_key, pkey_size);
41  return ret;
42  }
43 
44  template <typename BytesType>
45  static uint32_t sign_message(const BytesType &skey, const BytesType &data,
46  BytesType &sig) {
47  sig = BytesType(ecc_t::get_signature_size());
48  return sign_message(skey.data(), skey.size(), data.data(), data.size(),
49  sig.data(), sig.size());
50  }
51 
52  template <typename BytesType>
53  static uint32_t verify_signature(const BytesType &data, const BytesType &sig,
54  const BytesType &public_key) {
55  //return 0;
56  //verify_signature(msg, allowance, pkey);
57 
58  return verify_signature(data.data(), data.size(), sig.data(), sig.size(),
59  public_key.data(), public_key.size());
60 
61  }
62 };
63 } // namespace internal
64 } // namespace crypto
65 } // namespace ypc
ypc::crypto::internal::sign_impl
Definition: sign_impl.h:7
ypc::utc::bytes
Definition: bytes.h:143