2018-01-07 05:05:16 +00:00
|
|
|
// Copyright (c) 2014-2018, The Monero Project
|
2014-07-23 13:03:52 +00:00
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
// permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
// conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other
|
|
|
|
// materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
|
|
|
// used to endorse or promote products derived from this software without specific
|
|
|
|
// prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2013-11-14 22:28:17 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
2017-09-10 16:35:59 +00:00
|
|
|
#include <iostream>
|
2016-03-11 12:25:28 +00:00
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
#include <boost/thread/lock_guard.hpp>
|
2017-02-19 02:42:10 +00:00
|
|
|
#include <boost/optional.hpp>
|
2017-10-26 09:21:06 +00:00
|
|
|
#include <type_traits>
|
2014-03-03 22:07:58 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "common/pod-class.h"
|
2017-10-26 09:21:06 +00:00
|
|
|
#include "common/util.h"
|
2017-12-21 14:51:29 +00:00
|
|
|
#include "memwipe.h"
|
2014-03-03 22:07:58 +00:00
|
|
|
#include "generic-ops.h"
|
2017-09-10 16:35:59 +00:00
|
|
|
#include "hex.h"
|
|
|
|
#include "span.h"
|
2014-03-03 22:07:58 +00:00
|
|
|
#include "hash.h"
|
2013-11-14 22:28:17 +00:00
|
|
|
|
|
|
|
namespace crypto {
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "random.h"
|
|
|
|
}
|
|
|
|
|
2013-11-14 22:28:17 +00:00
|
|
|
#pragma pack(push, 1)
|
2014-03-03 22:07:58 +00:00
|
|
|
POD_CLASS ec_point {
|
2013-11-14 22:28:17 +00:00
|
|
|
char data[32];
|
|
|
|
};
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
POD_CLASS ec_scalar {
|
2014-06-06 10:55:41 +00:00
|
|
|
char data[32];
|
2013-11-14 22:28:17 +00:00
|
|
|
};
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
POD_CLASS public_key: ec_point {
|
|
|
|
friend class crypto_ops;
|
2013-11-14 22:28:17 +00:00
|
|
|
};
|
|
|
|
|
2017-10-26 09:21:06 +00:00
|
|
|
using secret_key = tools::scrubbed<ec_scalar>;
|
2013-11-14 22:28:17 +00:00
|
|
|
|
2016-05-13 19:45:20 +00:00
|
|
|
POD_CLASS public_keyV {
|
|
|
|
std::vector<public_key> keys;
|
|
|
|
int rows;
|
|
|
|
};
|
|
|
|
|
|
|
|
POD_CLASS secret_keyV {
|
|
|
|
std::vector<secret_key> keys;
|
|
|
|
int rows;
|
|
|
|
};
|
|
|
|
|
|
|
|
POD_CLASS public_keyM {
|
|
|
|
int cols;
|
|
|
|
int rows;
|
|
|
|
std::vector<secret_keyV> column_vectors;
|
|
|
|
};
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
POD_CLASS key_derivation: ec_point {
|
2013-11-14 22:28:17 +00:00
|
|
|
friend class crypto_ops;
|
|
|
|
};
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
POD_CLASS key_image: ec_point {
|
2013-11-14 22:28:17 +00:00
|
|
|
friend class crypto_ops;
|
|
|
|
};
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
POD_CLASS signature {
|
2013-11-14 22:28:17 +00:00
|
|
|
ec_scalar c, r;
|
|
|
|
friend class crypto_ops;
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
2017-02-19 02:42:10 +00:00
|
|
|
void hash_to_scalar(const void *data, size_t length, ec_scalar &res);
|
2018-07-04 21:17:20 +00:00
|
|
|
void random32_unbiased(unsigned char *bytes);
|
2017-02-19 02:42:10 +00:00
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
static_assert(sizeof(ec_point) == 32 && sizeof(ec_scalar) == 32 &&
|
|
|
|
sizeof(public_key) == 32 && sizeof(secret_key) == 32 &&
|
|
|
|
sizeof(key_derivation) == 32 && sizeof(key_image) == 32 &&
|
2013-11-14 22:28:17 +00:00
|
|
|
sizeof(signature) == 64, "Invalid structure size");
|
|
|
|
|
|
|
|
class crypto_ops {
|
|
|
|
crypto_ops();
|
|
|
|
crypto_ops(const crypto_ops &);
|
|
|
|
void operator=(const crypto_ops &);
|
|
|
|
~crypto_ops();
|
|
|
|
|
2014-06-06 18:18:11 +00:00
|
|
|
static secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key = secret_key(), bool recover = false);
|
|
|
|
friend secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key, bool recover);
|
2013-11-14 22:28:17 +00:00
|
|
|
static bool check_key(const public_key &);
|
|
|
|
friend bool check_key(const public_key &);
|
2014-03-03 22:07:58 +00:00
|
|
|
static bool secret_key_to_public_key(const secret_key &, public_key &);
|
|
|
|
friend bool secret_key_to_public_key(const secret_key &, public_key &);
|
|
|
|
static bool generate_key_derivation(const public_key &, const secret_key &, key_derivation &);
|
|
|
|
friend bool generate_key_derivation(const public_key &, const secret_key &, key_derivation &);
|
2016-08-10 11:48:20 +00:00
|
|
|
static void derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res);
|
|
|
|
friend void derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res);
|
2014-03-03 22:07:58 +00:00
|
|
|
static bool derive_public_key(const key_derivation &, std::size_t, const public_key &, public_key &);
|
|
|
|
friend bool derive_public_key(const key_derivation &, std::size_t, const public_key &, public_key &);
|
|
|
|
static void derive_secret_key(const key_derivation &, std::size_t, const secret_key &, secret_key &);
|
|
|
|
friend void derive_secret_key(const key_derivation &, std::size_t, const secret_key &, secret_key &);
|
2017-02-19 02:42:10 +00:00
|
|
|
static bool derive_subaddress_public_key(const public_key &, const key_derivation &, std::size_t, public_key &);
|
|
|
|
friend bool derive_subaddress_public_key(const public_key &, const key_derivation &, std::size_t, public_key &);
|
2013-11-14 22:28:17 +00:00
|
|
|
static void generate_signature(const hash &, const public_key &, const secret_key &, signature &);
|
|
|
|
friend void generate_signature(const hash &, const public_key &, const secret_key &, signature &);
|
|
|
|
static bool check_signature(const hash &, const public_key &, const signature &);
|
|
|
|
friend bool check_signature(const hash &, const public_key &, const signature &);
|
2017-02-19 02:42:10 +00:00
|
|
|
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 &);
|
|
|
|
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 &);
|
|
|
|
static bool check_tx_proof(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const signature &);
|
|
|
|
friend bool check_tx_proof(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const signature &);
|
2013-11-14 22:28:17 +00:00
|
|
|
static void generate_key_image(const public_key &, const secret_key &, key_image &);
|
|
|
|
friend void generate_key_image(const public_key &, const secret_key &, key_image &);
|
|
|
|
static void generate_ring_signature(const hash &, const key_image &,
|
|
|
|
const public_key *const *, std::size_t, const secret_key &, std::size_t, signature *);
|
|
|
|
friend void generate_ring_signature(const hash &, const key_image &,
|
|
|
|
const public_key *const *, std::size_t, const secret_key &, std::size_t, signature *);
|
|
|
|
static bool check_ring_signature(const hash &, const key_image &,
|
|
|
|
const public_key *const *, std::size_t, const signature *);
|
|
|
|
friend bool check_ring_signature(const hash &, const key_image &,
|
|
|
|
const public_key *const *, std::size_t, const signature *);
|
|
|
|
};
|
|
|
|
|
2018-04-10 11:20:31 +00:00
|
|
|
void generate_random_bytes_thread_safe(size_t N, uint8_t *bytes);
|
|
|
|
|
2016-05-14 21:08:10 +00:00
|
|
|
/* Generate N random bytes
|
|
|
|
*/
|
|
|
|
inline void rand(size_t N, uint8_t *bytes) {
|
2018-04-10 11:20:31 +00:00
|
|
|
generate_random_bytes_thread_safe(N, bytes);
|
2016-05-14 21:08:10 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
/* Generate a value filled with random bytes.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
typename std::enable_if<std::is_pod<T>::value, T>::type rand() {
|
|
|
|
typename std::remove_cv<T>::type res;
|
2018-04-10 11:20:31 +00:00
|
|
|
generate_random_bytes_thread_safe(sizeof(T), (uint8_t*)&res);
|
2014-03-03 22:07:58 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate a new key pair
|
2013-11-14 22:28:17 +00:00
|
|
|
*/
|
2014-06-06 18:18:11 +00:00
|
|
|
inline secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key = secret_key(), bool recover = false) {
|
|
|
|
return crypto_ops::generate_keys(pub, sec, recovery_key, recover);
|
2013-11-14 22:28:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
/* Check a public key. Returns true if it is valid, false otherwise.
|
2013-11-14 22:28:17 +00:00
|
|
|
*/
|
|
|
|
inline bool check_key(const public_key &key) {
|
|
|
|
return crypto_ops::check_key(key);
|
|
|
|
}
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
/* Checks a private key and computes the corresponding public key.
|
2013-11-14 22:28:17 +00:00
|
|
|
*/
|
2014-03-03 22:07:58 +00:00
|
|
|
inline bool secret_key_to_public_key(const secret_key &sec, public_key &pub) {
|
|
|
|
return crypto_ops::secret_key_to_public_key(sec, pub);
|
2013-11-14 22:28:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
/* To generate an ephemeral key used to send money to:
|
|
|
|
* * The sender generates a new key pair, which becomes the transaction key. The public transaction key is included in "extra" field.
|
|
|
|
* * Both the sender and the receiver generate key derivation from the transaction key, the receivers' "view" key and the output index.
|
|
|
|
* * The sender uses key derivation and the receivers' "spend" key to derive an ephemeral public key.
|
|
|
|
* * The receiver can either derive the public key (to check that the transaction is addressed to him) or the private key (to spend the money).
|
2013-11-14 22:28:17 +00:00
|
|
|
*/
|
2014-03-03 22:07:58 +00:00
|
|
|
inline bool generate_key_derivation(const public_key &key1, const secret_key &key2, key_derivation &derivation) {
|
|
|
|
return crypto_ops::generate_key_derivation(key1, key2, derivation);
|
|
|
|
}
|
|
|
|
inline bool derive_public_key(const key_derivation &derivation, std::size_t output_index,
|
|
|
|
const public_key &base, public_key &derived_key) {
|
|
|
|
return crypto_ops::derive_public_key(derivation, output_index, base, derived_key);
|
|
|
|
}
|
2016-08-10 11:48:20 +00:00
|
|
|
inline void derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res) {
|
|
|
|
return crypto_ops::derivation_to_scalar(derivation, output_index, res);
|
|
|
|
}
|
2014-03-03 22:07:58 +00:00
|
|
|
inline void derive_secret_key(const key_derivation &derivation, std::size_t output_index,
|
|
|
|
const secret_key &base, secret_key &derived_key) {
|
|
|
|
crypto_ops::derive_secret_key(derivation, output_index, base, derived_key);
|
2013-11-14 22:28:17 +00:00
|
|
|
}
|
2017-02-19 02:42:10 +00:00
|
|
|
inline bool derive_subaddress_public_key(const public_key &out_key, const key_derivation &derivation, std::size_t output_index, public_key &result) {
|
|
|
|
return crypto_ops::derive_subaddress_public_key(out_key, derivation, output_index, result);
|
|
|
|
}
|
2013-11-14 22:28:17 +00:00
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
/* Generation and checking of a standard signature.
|
2013-11-14 22:28:17 +00:00
|
|
|
*/
|
2014-03-03 22:07:58 +00:00
|
|
|
inline void generate_signature(const hash &prefix_hash, const public_key &pub, const secret_key &sec, signature &sig) {
|
|
|
|
crypto_ops::generate_signature(prefix_hash, pub, sec, sig);
|
|
|
|
}
|
|
|
|
inline bool check_signature(const hash &prefix_hash, const public_key &pub, const signature &sig) {
|
|
|
|
return crypto_ops::check_signature(prefix_hash, pub, sig);
|
2013-11-14 22:28:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:22:16 +00:00
|
|
|
/* Generation and checking of a tx proof; given a tx pubkey R, the recipient's view pubkey A, and the key
|
|
|
|
* derivation D, the signature proves the knowledge of the tx secret key r such that R=r*G and D=r*A
|
2017-02-19 02:42:10 +00:00
|
|
|
* When the recipient's address is a subaddress, the tx pubkey R is defined as R=r*B where B is the recipient's spend pubkey
|
2017-03-29 23:22:16 +00:00
|
|
|
*/
|
2017-02-19 02:42:10 +00:00
|
|
|
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) {
|
|
|
|
crypto_ops::generate_tx_proof(prefix_hash, R, A, B, D, r, sig);
|
2017-03-29 23:22:16 +00:00
|
|
|
}
|
2017-02-19 02:42:10 +00:00
|
|
|
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) {
|
|
|
|
return crypto_ops::check_tx_proof(prefix_hash, R, A, B, D, sig);
|
2017-03-29 23:22:16 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
/* To send money to a key:
|
|
|
|
* * The sender generates an ephemeral key and includes it in transaction output.
|
|
|
|
* * To spend the money, the receiver generates a key image from it.
|
|
|
|
* * Then he selects a bunch of outputs, including the one he spends, and uses them to generate a ring signature.
|
|
|
|
* To check the signature, it is necessary to collect all the keys that were used to generate it. To detect double spends, it is necessary to check that each key image is used at most once.
|
2013-11-14 22:28:17 +00:00
|
|
|
*/
|
2014-03-03 22:07:58 +00:00
|
|
|
inline void generate_key_image(const public_key &pub, const secret_key &sec, key_image &image) {
|
|
|
|
crypto_ops::generate_key_image(pub, sec, image);
|
|
|
|
}
|
|
|
|
inline void generate_ring_signature(const hash &prefix_hash, const key_image &image,
|
2013-11-14 22:28:17 +00:00
|
|
|
const public_key *const *pubs, std::size_t pubs_count,
|
|
|
|
const secret_key &sec, std::size_t sec_index,
|
|
|
|
signature *sig) {
|
2014-03-03 22:07:58 +00:00
|
|
|
crypto_ops::generate_ring_signature(prefix_hash, image, pubs, pubs_count, sec, sec_index, sig);
|
|
|
|
}
|
|
|
|
inline bool check_ring_signature(const hash &prefix_hash, const key_image &image,
|
|
|
|
const public_key *const *pubs, std::size_t pubs_count,
|
|
|
|
const signature *sig) {
|
|
|
|
return crypto_ops::check_ring_signature(prefix_hash, image, pubs, pubs_count, sig);
|
2013-11-14 22:28:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
/* Variants with vector<const public_key *> parameters.
|
2013-11-14 22:28:17 +00:00
|
|
|
*/
|
2014-03-03 22:07:58 +00:00
|
|
|
inline void generate_ring_signature(const hash &prefix_hash, const key_image &image,
|
|
|
|
const std::vector<const public_key *> &pubs,
|
|
|
|
const secret_key &sec, std::size_t sec_index,
|
|
|
|
signature *sig) {
|
|
|
|
generate_ring_signature(prefix_hash, image, pubs.data(), pubs.size(), sec, sec_index, sig);
|
|
|
|
}
|
|
|
|
inline bool check_ring_signature(const hash &prefix_hash, const key_image &image,
|
|
|
|
const std::vector<const public_key *> &pubs,
|
2013-11-14 22:28:17 +00:00
|
|
|
const signature *sig) {
|
2014-03-03 22:07:58 +00:00
|
|
|
return check_ring_signature(prefix_hash, image, pubs.data(), pubs.size(), sig);
|
2013-11-14 22:28:17 +00:00
|
|
|
}
|
2017-09-10 16:35:59 +00:00
|
|
|
|
|
|
|
inline std::ostream &operator <<(std::ostream &o, const crypto::public_key &v) {
|
|
|
|
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o;
|
|
|
|
}
|
|
|
|
inline std::ostream &operator <<(std::ostream &o, const crypto::secret_key &v) {
|
|
|
|
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o;
|
|
|
|
}
|
|
|
|
inline std::ostream &operator <<(std::ostream &o, const crypto::key_derivation &v) {
|
|
|
|
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o;
|
|
|
|
}
|
|
|
|
inline std::ostream &operator <<(std::ostream &o, const crypto::key_image &v) {
|
|
|
|
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o;
|
|
|
|
}
|
|
|
|
inline std::ostream &operator <<(std::ostream &o, const crypto::signature &v) {
|
|
|
|
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o;
|
|
|
|
}
|
|
|
|
|
2018-07-15 11:59:58 +00:00
|
|
|
const extern crypto::public_key null_pkey;
|
|
|
|
const extern crypto::secret_key null_skey;
|
2014-03-03 22:07:58 +00:00
|
|
|
}
|
2013-11-14 22:28:17 +00:00
|
|
|
|
2016-11-07 18:50:05 +00:00
|
|
|
CRYPTO_MAKE_HASHABLE(public_key)
|
2017-02-19 02:42:10 +00:00
|
|
|
CRYPTO_MAKE_HASHABLE(secret_key)
|
2014-03-03 22:07:58 +00:00
|
|
|
CRYPTO_MAKE_HASHABLE(key_image)
|
|
|
|
CRYPTO_MAKE_COMPARABLE(signature)
|