| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include <cstddef> |
| 34 |
#include <iostream> |
| 35 |
#include <boost/optional.hpp> |
| 36 |
#include <type_traits> |
| 37 |
#include <vector> |
| 38 |
#include <random> |
| 39 |
|
| 40 |
#include "common/pod-class.h" |
| 41 |
#include "memwipe.h" |
| 42 |
#include "mlocker.h" |
| 43 |
#include "generic-ops.h" |
| 44 |
#include "hex.h" |
| 45 |
#include "span.h" |
| 46 |
#include "hash.h" |
| 47 |
|
| 48 |
namespace crypto { |
| 49 |
|
| 50 |
extern "C" { |
| 51 |
#include "random.h" |
| 52 |
} |
| 53 |
|
| 54 |
#pragma pack(push, 1) |
| 55 |
POD_CLASS ec_point { |
| 56 |
char data[32]; |
| 57 |
}; |
| 58 |
|
| 59 |
POD_CLASS ec_scalar { |
| 60 |
char data[32]; |
| 61 |
}; |
| 62 |
|
| 63 |
POD_CLASS public_key: ec_point { |
| 64 |
friend class crypto_ops; |
| 65 |
}; |
| 66 |
|
| 67 |
POD_CLASS public_key_memsafe : epee::mlocked<tools::scrubbed<public_key>> { |
| 68 |
public_key_memsafe() = default; |
| 69 |
public_key_memsafe(const public_key &original) { memcpy(this->data, original.data, 32); } |
| 70 |
}; |
| 71 |
|
| 72 |
using secret_key = epee::mlocked<tools::scrubbed<ec_scalar>>; |
| 73 |
|
| 74 |
POD_CLASS public_keyV { |
| 75 |
std::vector<public_key> keys; |
| 76 |
int rows; |
| 77 |
}; |
| 78 |
|
| 79 |
POD_CLASS secret_keyV { |
| 80 |
std::vector<secret_key> keys; |
| 81 |
int rows; |
| 82 |
}; |
| 83 |
|
| 84 |
POD_CLASS public_keyM { |
| 85 |
int cols; |
| 86 |
int rows; |
| 87 |
std::vector<secret_keyV> column_vectors; |
| 88 |
}; |
| 89 |
|
| 90 |
POD_CLASS key_derivation: ec_point { |
| 91 |
friend class crypto_ops; |
| 92 |
}; |
| 93 |
|
| 94 |
POD_CLASS key_image: ec_point { |
| 95 |
friend class crypto_ops; |
| 96 |
}; |
| 97 |
|
| 98 |
POD_CLASS signature { |
| 99 |
ec_scalar c, r; |
| 100 |
friend class crypto_ops; |
| 101 |
}; |
| 102 |
|
| 103 |
POD_CLASS view_tag { |
| 104 |
char data; |
| 105 |
}; |
| 106 |
#pragma pack(pop) |
| 107 |
|
| 108 |
void hash_to_scalar(const void *data, size_t length, ec_scalar &res); |
| 109 |
void random32_unbiased(unsigned char *bytes); |
| 110 |
|
| 111 |
static_assert(sizeof(ec_point) == 32 && sizeof(ec_scalar) == 32 && |
| 112 |
sizeof(public_key) == 32 && sizeof(public_key_memsafe) == 32 && sizeof(secret_key) == 32 && |
| 113 |
sizeof(key_derivation) == 32 && sizeof(key_image) == 32 && |
| 114 |
sizeof(signature) == 64 && sizeof(view_tag) == 1, "Invalid structure size"); |
| 115 |
|
| 116 |
class crypto_ops { |
| 117 |
crypto_ops(); |
| 118 |
crypto_ops(const crypto_ops &); |
| 119 |
void operator=(const crypto_ops &); |
| 120 |
~crypto_ops(); |
| 121 |
|
| 122 |
static secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key = secret_key(), bool recover = false); |
| 123 |
friend secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key, bool recover); |
| 124 |
static bool check_key(const public_key &); |
| 125 |
friend bool check_key(const public_key &); |
| 126 |
static bool secret_key_to_public_key(const secret_key &, public_key &); |
| 127 |
friend bool secret_key_to_public_key(const secret_key &, public_key &); |
| 128 |
static bool generate_key_derivation(const public_key &, const secret_key &, key_derivation &); |
| 129 |
friend bool generate_key_derivation(const public_key &, const secret_key &, key_derivation &); |
| 130 |
static void derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res); |
| 131 |
friend void derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res); |
| 132 |
static bool derive_public_key(const key_derivation &, std::size_t, const public_key &, public_key &); |
| 133 |
friend bool derive_public_key(const key_derivation &, std::size_t, const public_key &, public_key &); |
| 134 |
static void derive_secret_key(const key_derivation &, std::size_t, const secret_key &, secret_key &); |
| 135 |
friend void derive_secret_key(const key_derivation &, std::size_t, const secret_key &, secret_key &); |
| 136 |
static bool derive_subaddress_public_key(const public_key &, const key_derivation &, std::size_t, public_key &); |
| 137 |
friend bool derive_subaddress_public_key(const public_key &, const key_derivation &, std::size_t, public_key &); |
| 138 |
static void generate_signature(const hash &, const public_key &, const secret_key &, signature &); |
| 139 |
friend void generate_signature(const hash &, const public_key &, const secret_key &, signature &); |
| 140 |
static bool check_signature(const hash &, const public_key &, const signature &); |
| 141 |
friend bool check_signature(const hash &, const public_key &, const signature &); |
| 142 |
static void generate_tx_proof(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const secret_key &, signature &); |
| 143 |
friend void generate_tx_proof(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const secret_key &, signature &); |
| 144 |
static void generate_tx_proof_v1(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const secret_key &, signature &); |
| 145 |
friend void generate_tx_proof_v1(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const secret_key &, signature &); |
| 146 |
static bool check_tx_proof(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const signature &, const int); |
| 147 |
friend bool check_tx_proof(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const signature &, const int); |
| 148 |
static void generate_key_image(const public_key &, const secret_key &, key_image &); |
| 149 |
friend void generate_key_image(const public_key &, const secret_key &, key_image &); |
| 150 |
static void generate_ring_signature(const hash &, const key_image &, |
| 151 |
const public_key *const *, std::size_t, const secret_key &, std::size_t, signature *); |
| 152 |
friend void generate_ring_signature(const hash &, const key_image &, |
| 153 |
const public_key *const *, std::size_t, const secret_key &, std::size_t, signature *); |
| 154 |
static bool check_ring_signature(const hash &, const key_image &, |
| 155 |
const public_key *const *, std::size_t, const signature *); |
| 156 |
friend bool check_ring_signature(const hash &, const key_image &, |
| 157 |
const public_key *const *, std::size_t, const signature *); |
| 158 |
static void derive_view_tag(const key_derivation &, std::size_t, view_tag &); |
| 159 |
friend void derive_view_tag(const key_derivation &, std::size_t, view_tag &); |
| 160 |
}; |
| 161 |
|
| 162 |
void generate_random_bytes_thread_safe(size_t N, uint8_t *bytes); |
| 163 |
void add_extra_entropy_thread_safe(const void *ptr, size_t bytes); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
inline void rand(size_t N, uint8_t *bytes) { |
| 168 |
generate_random_bytes_thread_safe(N, bytes); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
template<typename T> |
| 174 |
T rand() { |
| 175 |
static_assert(std::is_standard_layout<T>(), "cannot write random bytes into non-standard layout type"); |
| 176 |
static_assert(std::is_trivially_copyable<T>(), "cannot write random bytes into non-trivially copyable type"); |
| 177 |
typename std::remove_cv<T>::type res; |
| 178 |
generate_random_bytes_thread_safe(sizeof(T), (uint8_t*)&res); |
| 179 |
return res; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
struct random_device |
| 185 |
{ |
| 186 |
typedef uint64_t result_type; |
| 187 |
static constexpr result_type min() { return 0; } |
| 188 |
static constexpr result_type max() { return result_type(-1); } |
| 189 |
result_type operator()() const { return crypto::rand<result_type>(); } |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
template<typename T> |
| 195 |
typename std::enable_if<std::is_integral<T>::value, T>::type rand_range(T range_min, T range_max) { |
| 196 |
crypto::random_device rd; |
| 197 |
std::uniform_int_distribution<T> dis(range_min, range_max); |
| 198 |
return dis(rd); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
template<typename T> |
| 204 |
typename std::enable_if<std::is_unsigned<T>::value, T>::type rand_idx(T sz) { |
| 205 |
return crypto::rand_range<T>(0, sz-1); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
inline secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key = secret_key(), bool recover = false) { |
| 211 |
return crypto_ops::generate_keys(pub, sec, recovery_key, recover); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
inline bool check_key(const public_key &key) { |
| 217 |
return crypto_ops::check_key(key); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
inline bool secret_key_to_public_key(const secret_key &sec, public_key &pub) { |
| 223 |
return crypto_ops::secret_key_to_public_key(sec, pub); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
inline bool generate_key_derivation(const public_key &key1, const secret_key &key2, key_derivation &derivation) { |
| 233 |
return crypto_ops::generate_key_derivation(key1, key2, derivation); |
| 234 |
} |
| 235 |
inline bool derive_public_key(const key_derivation &derivation, std::size_t output_index, |
| 236 |
const public_key &base, public_key &derived_key) { |
| 237 |
return crypto_ops::derive_public_key(derivation, output_index, base, derived_key); |
| 238 |
} |
| 239 |
inline void derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res) { |
| 240 |
return crypto_ops::derivation_to_scalar(derivation, output_index, res); |
| 241 |
} |
| 242 |
inline void derive_secret_key(const key_derivation &derivation, std::size_t output_index, |
| 243 |
const secret_key &base, secret_key &derived_key) { |
| 244 |
crypto_ops::derive_secret_key(derivation, output_index, base, derived_key); |
| 245 |
} |
| 246 |
inline bool derive_subaddress_public_key(const public_key &out_key, const key_derivation &derivation, std::size_t output_index, public_key &result) { |
| 247 |
return crypto_ops::derive_subaddress_public_key(out_key, derivation, output_index, result); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
inline void generate_signature(const hash &prefix_hash, const public_key &pub, const secret_key &sec, signature &sig) { |
| 253 |
crypto_ops::generate_signature(prefix_hash, pub, sec, sig); |
| 254 |
} |
| 255 |
inline bool check_signature(const hash &prefix_hash, const public_key &pub, const signature &sig) { |
| 256 |
return crypto_ops::check_signature(prefix_hash, pub, sig); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
inline void generate_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const boost::optional<public_key> &B, const public_key &D, const secret_key &r, signature &sig) { |
| 264 |
crypto_ops::generate_tx_proof(prefix_hash, R, A, B, D, r, sig); |
| 265 |
} |
| 266 |
inline void generate_tx_proof_v1(const hash &prefix_hash, const public_key &R, const public_key &A, const boost::optional<public_key> &B, const public_key &D, const secret_key &r, signature &sig) { |
| 267 |
crypto_ops::generate_tx_proof_v1(prefix_hash, R, A, B, D, r, sig); |
| 268 |
} |
| 269 |
inline bool check_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const boost::optional<public_key> &B, const public_key &D, const signature &sig, const int version) { |
| 270 |
return crypto_ops::check_tx_proof(prefix_hash, R, A, B, D, sig, version); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
inline void generate_key_image(const public_key &pub, const secret_key &sec, key_image &image) { |
| 280 |
crypto_ops::generate_key_image(pub, sec, image); |
| 281 |
} |
| 282 |
inline void generate_ring_signature(const hash &prefix_hash, const key_image &image, |
| 283 |
const public_key *const *pubs, std::size_t pubs_count, |
| 284 |
const secret_key &sec, std::size_t sec_index, |
| 285 |
signature *sig) { |
| 286 |
crypto_ops::generate_ring_signature(prefix_hash, image, pubs, pubs_count, sec, sec_index, sig); |
| 287 |
} |
| 288 |
inline bool check_ring_signature(const hash &prefix_hash, const key_image &image, |
| 289 |
const public_key *const *pubs, std::size_t pubs_count, |
| 290 |
const signature *sig) { |
| 291 |
return crypto_ops::check_ring_signature(prefix_hash, image, pubs, pubs_count, sig); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
inline void generate_ring_signature(const hash &prefix_hash, const key_image &image, |
| 297 |
const std::vector<const public_key *> &pubs, |
| 298 |
const secret_key &sec, std::size_t sec_index, |
| 299 |
signature *sig) { |
| 300 |
generate_ring_signature(prefix_hash, image, pubs.data(), pubs.size(), sec, sec_index, sig); |
| 301 |
} |
| 302 |
inline bool check_ring_signature(const hash &prefix_hash, const key_image &image, |
| 303 |
const std::vector<const public_key *> &pubs, |
| 304 |
const signature *sig) { |
| 305 |
return check_ring_signature(prefix_hash, image, pubs.data(), pubs.size(), sig); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
inline void derive_view_tag(const key_derivation &derivation, std::size_t output_index, view_tag &vt) { |
| 313 |
crypto_ops::derive_view_tag(derivation, output_index, vt); |
| 314 |
} |
| 315 |
|
| 316 |
inline std::ostream &operator <<(std::ostream &o, const crypto::public_key &v) { |
| 317 |
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
struct secret_key_explicit_print_ref { const crypto::secret_key &sk; }; |
| 325 |
inline std::ostream &operator <<(std::ostream &o, const secret_key_explicit_print_ref v) { |
| 326 |
epee::to_hex::formatted(o, epee::as_byte_span(unwrap(unwrap(v.sk)))); return o; |
| 327 |
} |
| 328 |
inline std::ostream &operator <<(std::ostream &o, const crypto::key_derivation &v) { |
| 329 |
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o; |
| 330 |
} |
| 331 |
inline std::ostream &operator <<(std::ostream &o, const crypto::key_image &v) { |
| 332 |
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o; |
| 333 |
} |
| 334 |
inline std::ostream &operator <<(std::ostream &o, const crypto::signature &v) { |
| 335 |
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o; |
| 336 |
} |
| 337 |
inline std::ostream &operator <<(std::ostream &o, const crypto::view_tag &v) { |
| 338 |
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o; |
| 339 |
} |
| 340 |
|
| 341 |
const extern crypto::public_key null_pkey; |
| 342 |
const extern crypto::secret_key null_skey; |
| 343 |
|
| 344 |
inline bool operator<(const public_key &p1, const public_key &p2) { return memcmp(&p1, &p2, sizeof(public_key)) < 0; } |
| 345 |
inline bool operator>(const public_key &p1, const public_key &p2) { return p2 < p1; } |
| 346 |
} |
| 347 |
|
| 348 |
CRYPTO_MAKE_HASHABLE(public_key) |
| 349 |
CRYPTO_MAKE_HASHABLE_CONSTANT_TIME(secret_key) |
| 350 |
CRYPTO_MAKE_HASHABLE_CONSTANT_TIME(public_key_memsafe) |
| 351 |
CRYPTO_MAKE_HASHABLE(key_image) |
| 352 |
CRYPTO_MAKE_COMPARABLE(signature) |
| 353 |
CRYPTO_MAKE_COMPARABLE(view_tag) |