plebble
ec.h
Go to the documentation of this file.
1 /*
2 -------------------------------------------------------------------------------
3  PLEBBLE
4 -------------------------------------------------------------------------------
5  Copyright (C) 2019-2020 KATLAS Technology. (http://katlastechnology.com)
6  Copyright (C) 2017-2020 Marcos Mayorga. (mm@mm-studios.com)
7 
8  This file is part of our Plebble(R) Platform.
9 
10  The code below cannot be copied, used for any purpose other than the one
11  agreed and/or distributed without the express permission of
12  KATLAS Technology.
13 -------------------------------------------------------------------------------
14 
15 
16 */
17 #ifndef USGOV_075dfff39e95d4a4a7a088fe2ea0fc252d6fa47e6b62b043b4babd2e2b511780
18 #define USGOV_075dfff39e95d4a4a7a088fe2ea0fc252d6fa47e6b62b043b4babd2e2b511780
19 
20 #include <secp256k1.h>
21 #include <iostream>
22 #include <vector>
23 #include <array>
24 #include "double_sha256.h"
25 #include "ripemd160.h"
26 #include <us/gov/likely.h>
27 #include "sha256.h"
28 #include <us/gov/config.h>
29 
30 namespace us { namespace gov {
31 namespace crypto {
32 using namespace std;
33 
34 struct ec {
35  typedef secp256k1_ecdsa_signature signature;
37 
38  ec();
39  ec(const ec&)=delete;
40  ec& operator = (const ec&)=delete;
41  ~ec();
42 
43  struct keys {
44  struct pub_t:secp256k1_pubkey {
47  static constexpr uint32_t ser_size = 33;
48 
49  pub_t();
50  pub_t(const pub_t& other);
51  pub_t(const string& b58);
52  pub_t(const vector<unsigned char>& ser);
53  const hash_t& hash() const;
54  pub_t& operator =(const string&);
55  hash_t compute_hash() const;
56  string to_b58() const;
57  bool to_vector(vector<unsigned char>& out) const;
58 
59  bool set_b58(const string&);
60  static pub_t from_b58(const string&);
61  static pub_t from_hex(const string&);
62  void read_b58(const string&);
63  void read_ser(const vector<unsigned char>&);
64 
65  bool operator == (const pub_t& other) const;
66  bool operator != (const pub_t& other) const;
67  bool operator == (const string& b58) const;
68  bool operator != (const string& b58) const;
69  pub_t& operator = (const pub_t& other);
70  inline void zero() { valid=false; }
71  bool valid{false};
72  private:
73  mutable bool h{false};
75  };
76 
77  struct priv_t: array<unsigned char,32> {
78  priv_t();
79  priv_t(const string& b58);
80  priv_t(const char* b58);
81  string to_b58() const;
82  static priv_t from_b58(const string&);
83  bool set_b58(const string&);
84  inline void zero() { memset(this,0,32); }
85  bool is_zero() const;
86  };
87 
88  keys() {}
89  keys(const priv_t&);
90  keys(const keys& other);
91  keys(keys&& other);
92  void reset(const priv_t&);
93  int reset_if_distinct(const priv_t&); // 0 OK/KO equal; 1 OK distinct-valid; -1 KO distinct invalid input
94  static keys generate();
95  static bool verify(const priv_t& privkey);
96  static pub_t get_pubkey(const priv_t& privkey);
97 
100  };
101 
102  #if CFG_COUNTERS==1
103  struct counters_t {
104  uint32_t unable_serialize_pubkey{0};
105  uint32_t invalid_b58_pubkey{0};
106  uint32_t invalid_ser_pubkey{0};
107  uint32_t error_signing{0};
108  uint32_t error_serializing_signature{0};
109  uint32_t signatures_created{0};
110  uint32_t error_creating_shared_secret{0};
111  //uint32_t signature_verification_b58_decode_error{0};
112  uint32_t signature_verification_der_parse_error{0};
113  uint32_t signature_verification_pass{0};
114  uint32_t signature_verification_fail{0};
115  uint32_t privkey_verification_pass{0};
116  uint32_t privkey_verification_fail{0};
117  uint32_t error_priv_2_pub{0};
118  void dump(ostream&os) const;
119  };
120  static counters_t counters;
121  #endif
122 
123  static constexpr uint32_t bit_size() { return 256; }
124 // static string to_b58(const vector<unsigned char>& data);
125  typedef vector<unsigned char> sig_t;
126  static vector<unsigned char> from_b58(const string& hex);
127  ko sign(const keys::priv_t& pk, const string& text, sig_t& signature) const;
128  ko sign(const keys::priv_t& pk, const vector<unsigned char>& msg, sig_t& signature) const;
130  //vector<unsigned char> sign(const keys::priv_t& pk, const ripemd160::value_type&) const;
131 // string sign_encode(const keys::priv_t& pk, const string& text) const;
132 // string sign_encode(const keys::priv_t& pk, const vector<unsigned char>& msg) const;
133 // string sign_encode(const keys::priv_t& pk, const sigmsg_hasher_t::value_type&) const;
134  bool verify(const keys::pub_t& pk, const string& text, const sig_t& signature_der) const;
135  bool verify(const keys::pub_t& pk, const vector<unsigned char>& msg, const sig_t& signature_der) const;
136  bool verify(const keys::pub_t& pk, const sigmsg_hasher_t::value_type& msgh, const sig_t& signature_der) const;
137  bool verify(const keys::pub_t& pk, const sigmsg_hasher_t::value_type& msgh, const sig_t& signature_der, ostream& err) const;
138  bool verify_not_normalized(const keys::pub_t& pk, const string& text, const sig_t& signature_der) const;
139  bool verify_not_normalized(const keys::pub_t& pk, const vector<unsigned char>& msg, const sig_t& signature_der) const;
140  bool verify_not_normalized(const keys::pub_t& pk, const sigmsg_hasher_t::value_type&, const sig_t& signature_der) const;
141  ko generate_shared_key(unsigned char *, size_t, const keys::priv_t&, const keys::pub_t&);
142 
143  static ec instance;
144  secp256k1_context *ctx;
145 };
146 
147 inline ostream& operator << (ostream& os, const ec::keys::pub_t& k) {
148  os << k.to_b58();
149  return os;
150 }
151 
152 inline istream& operator >> (istream& is, ec::keys::pub_t& k) {
153  string s;
154  is >> s;
155  if (!k.set_b58(s)) {
156  is.setstate(ios_base::failbit);
157  k.zero();
158  }
159  return is;
160 }
161 
162 inline ostream& operator << (ostream& os, const ec::keys::priv_t& k) {
163  os << k.to_b58();
164  return os;
165 }
166 
167 inline istream& operator >> (istream& is, ec::keys::priv_t& k) {
168  string s;
169  is >> s;
170  if (!k.set_b58(s)) {
171  is.setstate(ios_base::failbit);
172  k.zero();
173  }
174  return is;
175 }
176 
177 }
178 }}
179 
180 namespace std {
181  template <>
183  size_t operator()(const us::gov::crypto::ec::keys::pub_t&) const;
184  };
185 }
186 
187 #endif
188 
us::gov::crypto::ec::keys::pub_t::operator!=
bool operator!=(const pub_t &other) const
Definition: ec.cpp:252
us::gov::crypto::ec::keys::reset
void reset(const priv_t &)
us::gov::crypto::ec::keys::keys
keys(const priv_t &)
us::gov::crypto::ec::keys::priv_t::set_b58
bool set_b58(const string &)
us::gov::crypto::ec::keys::priv_t::to_b58
string to_b58() const
us::gov::crypto::ec::keys::keys
keys(const keys &other)
c
us::gov::crypto::ec c
Definition: ec.cpp:30
us::gov::crypto::ec::verify
bool verify(const keys::pub_t &pk, const sigmsg_hasher_t::value_type &msgh, const sig_t &signature_der, ostream &err) const
us::gov::crypto::ec::keys::pub_t::hash_t
hasher_t::value_type hash_t
Definition: ec.h:46
us::gov::crypto::ec::keys::pub_t::to_vector
bool to_vector(vector< unsigned char > &out) const
Definition: ec.cpp:152
us::gov::crypto::ec::bit_size
static constexpr uint32_t bit_size()
Definition: ec.h:123
us::gov::crypto::ec::verify_not_normalized
bool verify_not_normalized(const keys::pub_t &pk, const string &text, const sig_t &signature_der) const
us.ko
Definition: ko.java:20
us::gov::crypto::ripemd160::finalize
void finalize(unsigned char hash[output_size])
us::gov::crypto::ec::keys::pub_t::operator=
pub_t & operator=(const string &)
Definition: ec.cpp:236
us::gov::crypto::sha256::write
void write(const unsigned char *data, size_t len)
us::gov::crypto::ec::keys::priv_t::priv_t
priv_t(const char *b58)
double_sha256.h
us
Definition: daemon.h:22
us::gov::crypto::ec::verify
bool verify(const keys::pub_t &pk, const sigmsg_hasher_t::value_type &msgh, const sig_t &signature_der) const
us::gov::crypto::ripemd160::value_type
Definition: ripemd160.h:38
unlikely
#define unlikely(x)
Definition: likely.h:30
us::gov::crypto::operator<<
ostream & operator<<(ostream &os, const ec::keys::pub_t &k)
Definition: ec.h:147
std::hash< us::gov::crypto::ec::keys::pub_t >::operator()
size_t operator()(const us::gov::crypto::ec::keys::pub_t &) const
Definition: ec.cpp:636
us::gov::crypto::ec::keys::pub_t::operator==
bool operator==(const pub_t &other) const
Definition: ec.cpp:246
us::gov::crypto::sha256::value_type
Definition: sha256.h:38
likely.h
us::gov::crypto::ec::signature
secp256k1_ecdsa_signature signature
Definition: ec.h:35
us::gov::crypto::ec::keys::pub_t::from_b58
static pub_t from_b58(const string &)
Definition: ec.cpp:232
us::gov::crypto::ec::keys::priv_t::from_b58
static priv_t from_b58(const string &)
us::gov::crypto::ec::keys::pub_t::valid
bool valid
Definition: ec.h:71
us::gov::crypto::ec::keys::priv_t::priv_t
priv_t()
us::gov::crypto::ec::keys::pub_t::read_b58
void read_b58(const string &)
Definition: ec.cpp:212
us::gov::crypto::ec::keys::pub_t::h
bool h
Definition: ec.h:73
us::gov::crypto::ec::keys::pub_t
Definition: ec.h:44
us::gov::crypto::ec::ec
ec(const ec &)=delete
us::gov::crypto::ec::keys::get_pubkey
static pub_t get_pubkey(const priv_t &privkey)
us::gov::crypto::ec::keys::pub_t::from_hex
static pub_t from_hex(const string &)
us::gov::crypto::ec::keys::pub_t::read_ser
void read_ser(const vector< unsigned char > &)
Definition: ec.cpp:193
us::gov::crypto::sha256
Definition: sha256.h:33
us::gov::crypto::ec::sign
ko sign(const keys::priv_t &pk, const vector< unsigned char > &msg, sig_t &signature) const
us::gov::crypto::ec::instance
static ec instance
Definition: ec.h:143
us::gov::crypto::ec::keys::pub_t::compute_hash
hash_t compute_hash() const
Definition: ec.cpp:113
us::gov::crypto::ec::keys::keys
keys(keys &&other)
us::gov::crypto::ec::keys::pub_t::pub_t
pub_t()
Definition: ec.cpp:94
us::gov::crypto::b58::decode
std::vector< unsigned char > decode(const std::string &s)
Definition: base58.cpp:54
us::gov::crypto::ec::verify
bool verify(const keys::pub_t &pk, const vector< unsigned char > &msg, const sig_t &signature_der) const
us::gov::crypto::ec::generate_shared_key
ko generate_shared_key(unsigned char *, size_t, const keys::priv_t &, const keys::pub_t &)
us::gov::engine::auth::pub_t
peer::daemon::pub_t pub_t
Definition: auth_app.h:53
us::gov::crypto::ec::verify_not_normalized
bool verify_not_normalized(const keys::pub_t &pk, const sigmsg_hasher_t::value_type &, const sig_t &signature_der) const
us::gov::crypto::ec::keys::priv_t
Definition: ec.h:77
us::ko
const ko_t * ko
Definition: ko.h:27
us::gov::crypto::ec::keys::pub_t::zero
void zero()
Definition: ec.h:70
us::gov::crypto::ec::keys::generate
static keys generate()
us::gov::crypto::ec::ec
ec()
us::gov::crypto::ripemd160
Definition: ripemd160.h:34
ripemd160.h
us::gov::crypto::ec::keys::priv
priv_t priv
Definition: ec.h:98
us::gov::crypto::ec::keys::pub
pub_t pub
Definition: ec.h:99
sha256.h
c
Definition: client.cpp:417
us::ok
static constexpr ko ok
Definition: ko.h:28
us::gov::crypto::ec::keys::verify
static bool verify(const priv_t &privkey)
us::gov::crypto::ec::keys::pub_t::hasher_t
crypto::ripemd160 hasher_t
Definition: ec.h:45
us::gov::cash::keys
crypto::ec::keys keys
Definition: app.h:52
us::gov::crypto::ec::keys::pub_t::set_b58
bool set_b58(const string &)
Definition: ec.cpp:241
likely
#define likely(x)
Definition: likely.h:29
us::gov::crypto::ec::keys::priv_t::is_zero
bool is_zero() const
std
Definition: app.h:380
us::gov::crypto::ec::keys::reset_if_distinct
int reset_if_distinct(const priv_t &)
us::gov::crypto::ec::keys::keys
keys()
Definition: ec.h:88
us::gov::crypto::ec
Definition: ec.h:34
us::gov::crypto::b58::encode
std::string encode(const std::string &s)
Definition: base58.cpp:62
us::gov::crypto::ec::sign
ko sign(const keys::priv_t &pk, const sigmsg_hasher_t::value_type &, sig_t &signature) const
us::gov::crypto::ec::keys::priv_t::priv_t
priv_t(const string &b58)
us::gov::crypto::ec::sign
ko sign(const keys::priv_t &pk, const string &text, sig_t &signature) const
us::gov::crypto::ec::keys::pub_t::hash
const hash_t & hash() const
Definition: ec.cpp:105
us::gov::crypto::ec::keys
Definition: ec.h:43
config.h
base58.h
us::gov::crypto
Definition: base58.cpp:19
us::gov::crypto::ec::verify
bool verify(const keys::pub_t &pk, const string &text, const sig_t &signature_der) const
us::gov::crypto::ripemd160::write
void write(const vector< unsigned char > &)
us::gov::crypto::ec::verify_not_normalized
bool verify_not_normalized(const keys::pub_t &pk, const vector< unsigned char > &msg, const sig_t &signature_der) const
us::gov::crypto::ec::sigmsg_hasher_t
sha256 sigmsg_hasher_t
Definition: ec.h:36
us::gov::crypto::ec::keys::pub_t::to_b58
string to_b58() const
Definition: ec.cpp:173
us::gov::crypto::ec::~ec
~ec()
us::gov::crypto::ec::from_b58
static vector< unsigned char > from_b58(const string &hex)
gov.h
secperr_callback
static void secperr_callback(const char *str, void *data)
Definition: ec.cpp:38
us::gov::crypto::sha256::finalize
void finalize(unsigned char hash[output_size])
ec.h
us::gov::crypto::ec::keys::priv_t::zero
void zero()
Definition: ec.h:84
us::gov::crypto::ec::sig_t
vector< unsigned char > sig_t
Definition: ec.h:125
log
#define log
Definition: gov.h:83
us::gov::crypto::ec::keys::pub_t::hash_cached
hash_t hash_cached
Definition: ec.h:74
us::gov::crypto::ec::ctx
secp256k1_context * ctx
Definition: ec.h:144
us::gov::crypto::operator>>
istream & operator>>(istream &is, ec::keys::pub_t &k)
Definition: ec.h:152