Skip to content

Commit 4dd8a46

Browse files
committed
complete scan
1 parent 65a251a commit 4dd8a46

15 files changed

+383
-164
lines changed

src/carrot_core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ set(carrot_core_sources
3030
account_secrets.cpp
3131
address_utils.cpp
3232
carrot_enote_scan.cpp
33+
core_types.cpp
3334
destination.cpp
3435
enote_utils.cpp
36+
hash_functions.cpp
3537
payment_proposal.cpp)
3638

3739
monero_find_all_headers(carrot_core_headers, "${CMAKE_CURRENT_SOURCE_DIR}")

src/carrot_core/account_secrets.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ void make_carrot_generateimage_key(const crypto::secret_key &s_view_balance,
7070
derive_scalar(transcript.data(), transcript.size, &s_view_balance, to_bytes(k_generate_image_out));
7171
}
7272
//-------------------------------------------------------------------------------------------------------------------
73+
void make_carrot_viewincoming_key(const crypto::secret_key &s_view_balance,
74+
crypto::secret_key &k_view_out)
75+
{
76+
// k_v = H_n(s_vb)
77+
const auto transcript = sp::make_fixed_transcript<CARROT_DOMAIN_SEP_INCOMING_VIEW_KEY>();
78+
derive_scalar(transcript.data(), transcript.size, &s_view_balance, to_bytes(k_view_out));
79+
}
80+
//-------------------------------------------------------------------------------------------------------------------
7381
void make_carrot_generateaddress_secret(const crypto::secret_key &s_view_balance,
7482
crypto::secret_key &s_generate_address_out)
7583
{

src/carrot_core/account_secrets.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,14 @@ void make_carrot_viewbalance_secret(const crypto::secret_key &s_master,
7373
*/
7474
void make_carrot_generateimage_key(const crypto::secret_key &s_view_balance,
7575
crypto::secret_key &k_generate_image_out);
76-
76+
/**
77+
* brief: make_carrot_viewincoming_key - view-incoming key, for identifying received external enotes
78+
* k_v = H_n(s_vb)
79+
* param: s_view_balance - s_vb
80+
* outparam: k_view_out - k_v
81+
*/
82+
void make_carrot_viewincoming_key(const crypto::secret_key &s_view_balance,
83+
crypto::secret_key &k_view_out);
7784
/**
7885
* brief: make_carrot_generateaddress_secret - generate-address secret, for generating addresses
7986
* s_ga = H_32(s_vb)

src/carrot_core/carrot_enote_scan.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace carrot
4343
{
4444
bool try_scan_carrot_enote_external(const CarrotEnoteV1 &enote,
4545
const std::optional<encrypted_payment_id_t> encrypted_payment_id,
46-
const unsigned char s_sender_receiver_unctx[32],
46+
const crypto::x25519_pubkey &s_sender_receiver_unctx,
4747
const crypto::secret_key &k_view,
4848
const crypto::public_key &account_spend_pubkey,
4949
crypto::secret_key &sender_extension_g_out,
@@ -59,12 +59,12 @@ bool try_scan_carrot_enote_external(const CarrotEnoteV1 &enote,
5959
make_carrot_input_context(enote.tx_first_key_image, input_context);
6060

6161
// if vt' != vt, then FAIL
62-
if (!test_carrot_view_tag(s_sender_receiver_unctx, input_context, enote.onetime_address, enote.view_tag))
62+
if (!test_carrot_view_tag(s_sender_receiver_unctx.data, input_context, enote.onetime_address, enote.view_tag))
6363
return false;
6464

6565
// s^ctx_sr = H_32(s_sr, D_e, input_context)
6666
crypto::hash s_sender_receiver;
67-
make_carrot_sender_receiver_secret(s_sender_receiver_unctx,
67+
make_carrot_sender_receiver_secret(s_sender_receiver_unctx.data,
6868
enote.enote_ephemeral_pubkey,
6969
input_context,
7070
s_sender_receiver);
@@ -74,7 +74,8 @@ bool try_scan_carrot_enote_external(const CarrotEnoteV1 &enote,
7474
enote.amount_enc,
7575
enote.onetime_address,
7676
enote.amount_commitment,
77-
enote_type_out, amount_out,
77+
enote_type_out,
78+
amount_out,
7879
amount_blinding_factor_out))
7980
return false;
8081

@@ -100,8 +101,23 @@ bool try_scan_carrot_enote_external(const CarrotEnoteV1 &enote,
100101
else
101102
payment_id_out = null_payment_id;
102103

103-
//
104-
return false;
104+
// anchor = anchor_enc XOR m_anchor
105+
const janus_anchor_t nominal_anchor = decrypt_carrot_anchor(enote.anchor_enc,
106+
s_sender_receiver,
107+
enote.onetime_address);
108+
109+
// verify Janus attack protection
110+
if (!verify_carrot_janus_protection(input_context,
111+
enote.onetime_address,
112+
k_view,
113+
account_spend_pubkey,
114+
address_spend_pubkey_out,
115+
enote.enote_ephemeral_pubkey,
116+
nominal_anchor,
117+
payment_id_out))
118+
return false;
119+
120+
return true;
105121
}
106122

107123
} //namespace carrot

src/carrot_core/carrot_enote_scan.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace carrot
4545
{
4646
bool try_scan_carrot_enote_external(const CarrotEnoteV1 &enote,
4747
const std::optional<encrypted_payment_id_t> encrypted_payment_id,
48-
const unsigned char s_sender_receiver_unctx[32],
48+
const crypto::x25519_pubkey &s_sender_receiver_unctx,
4949
const crypto::secret_key &k_view,
5050
const crypto::public_key &account_spend_pubkey,
5151
crypto::secret_key &sender_extension_g_out,

src/carrot_core/core_types.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright (c) 2022, The Monero Project
2+
//
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without modification, are
6+
// permitted provided that the following conditions are met:
7+
//
8+
// 1. Redistributions of source code must retain the above copyright notice, this list of
9+
// conditions and the following disclaimer.
10+
//
11+
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12+
// of conditions and the following disclaimer in the documentation and/or other
13+
// materials provided with the distribution.
14+
//
15+
// 3. Neither the name of the copyright holder nor the names of its contributors may be
16+
// used to endorse or promote products derived from this software without specific
17+
// prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20+
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21+
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22+
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27+
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
//paired header
30+
#include "core_types.h"
31+
32+
//local headers
33+
#include "crypto/crypto.h"
34+
35+
//third party headers
36+
#include <cstring>
37+
38+
//standard headers
39+
40+
namespace carrot
41+
{
42+
//-------------------------------------------------------------------------------------------------------------------
43+
//-------------------------------------------------------------------------------------------------------------------
44+
template <std::size_t Sz>
45+
static void xor_bytes(const unsigned char(&a)[Sz], const unsigned char(&b)[Sz], unsigned char(&c_out)[Sz])
46+
{
47+
for (std::size_t i{0}; i < Sz; ++i)
48+
c_out[i] = a[i] ^ b[i];
49+
}
50+
//-------------------------------------------------------------------------------------------------------------------
51+
//-------------------------------------------------------------------------------------------------------------------
52+
template <typename T>
53+
static T xor_bytes(const T &a, const T &b)
54+
{
55+
T temp;
56+
xor_bytes(a.bytes, b.bytes, temp.bytes);
57+
return temp;
58+
}
59+
//-------------------------------------------------------------------------------------------------------------------
60+
//-------------------------------------------------------------------------------------------------------------------
61+
bool operator==(const janus_anchor_t &a, const janus_anchor_t &b)
62+
{
63+
return memcmp(&a, &b, sizeof(janus_anchor_t)) == 0;
64+
}
65+
//-------------------------------------------------------------------------------------------------------------------
66+
janus_anchor_t operator^(const janus_anchor_t &a, const janus_anchor_t &b)
67+
{
68+
return xor_bytes(a, b);
69+
}
70+
//-------------------------------------------------------------------------------------------------------------------
71+
bool operator==(const encrypted_amount_t &a, const encrypted_amount_t &b)
72+
{
73+
return memcmp(&a, &b, sizeof(encrypted_amount_t)) == 0;
74+
}
75+
//-------------------------------------------------------------------------------------------------------------------
76+
encrypted_amount_t operator^(const encrypted_amount_t &a, const encrypted_amount_t &b)
77+
{
78+
return xor_bytes(a, b);
79+
}
80+
//-------------------------------------------------------------------------------------------------------------------
81+
bool operator==(const payment_id_t &a, const payment_id_t &b)
82+
{
83+
return memcmp(&a, &b, sizeof(payment_id_t)) == 0;
84+
}
85+
//-------------------------------------------------------------------------------------------------------------------
86+
payment_id_t operator^(const payment_id_t &a, const payment_id_t &b)
87+
{
88+
return xor_bytes(a, b);
89+
}
90+
//-------------------------------------------------------------------------------------------------------------------
91+
bool operator==(const input_context_t &a, const input_context_t &b)
92+
{
93+
return memcmp(&a, &b, sizeof(input_context_t)) == 0;
94+
}
95+
//-------------------------------------------------------------------------------------------------------------------
96+
bool operator==(const view_tag_t &a, const view_tag_t &b)
97+
{
98+
return memcmp(&a, &b, sizeof(view_tag_t)) == 0;
99+
}
100+
//-------------------------------------------------------------------------------------------------------------------
101+
janus_anchor_t gen_janus_anchor()
102+
{
103+
return crypto::rand<janus_anchor_t>();
104+
}
105+
//-------------------------------------------------------------------------------------------------------------------
106+
payment_id_t gen_payment_id()
107+
{
108+
return crypto::rand<payment_id_t>();
109+
}
110+
//-------------------------------------------------------------------------------------------------------------------
111+
view_tag_t gen_view_tag()
112+
{
113+
return crypto::rand<view_tag_t>();
114+
}
115+
//-------------------------------------------------------------------------------------------------------------------
116+
input_context_t gen_input_context()
117+
{
118+
return crypto::rand<input_context_t>();
119+
}
120+
//-------------------------------------------------------------------------------------------------------------------
121+
} //namespace carrot

src/carrot_core/core_types.h

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,26 @@ struct input_context_t final
9797

9898
/// overloaded operators: address tag
9999
bool operator==(const janus_anchor_t &a, const janus_anchor_t &b);
100-
inline bool operator!=(const janus_anchor_t &a, const janus_anchor_t &b) { return !(a == b); }
100+
static inline bool operator!=(const janus_anchor_t &a, const janus_anchor_t &b) { return !(a == b); }
101101
janus_anchor_t operator^(const janus_anchor_t &a, const janus_anchor_t &b);
102102

103103
/// overloaded operators: encrypted amount
104104
bool operator==(const encrypted_amount_t &a, const encrypted_amount_t &b);
105-
inline bool operator!=(const encrypted_amount_t &a, const encrypted_amount_t &b) { return !(a == b); }
105+
static inline bool operator!=(const encrypted_amount_t &a, const encrypted_amount_t &b) { return !(a == b); }
106106
encrypted_amount_t operator^(const encrypted_amount_t &a, const encrypted_amount_t &b);
107107

108108
/// overloaded operators: payment ID
109109
bool operator==(const payment_id_t &a, const payment_id_t &b);
110-
inline bool operator!=(const payment_id_t &a, const payment_id_t &b) { return !(a == b); }
110+
static inline bool operator!=(const payment_id_t &a, const payment_id_t &b) { return !(a == b); }
111111
payment_id_t operator^(const payment_id_t &a, const payment_id_t &b);
112112

113113
/// overloaded operators: input context
114114
bool operator==(const input_context_t &a, const input_context_t &b);
115-
inline bool operator!=(const input_context_t &a, const input_context_t &b) { return !(a == b); }
115+
static inline bool operator!=(const input_context_t &a, const input_context_t &b) { return !(a == b); }
116116

117117
/// overloaded operators: view tag
118118
bool operator==(const view_tag_t &a, const view_tag_t &b);
119-
inline bool operator!=(const view_tag_t &a, const view_tag_t &b) { return !(a == b); }
119+
static inline bool operator!=(const view_tag_t &a, const view_tag_t &b) { return !(a == b); }
120120

121121
/// generate a random janus anchor
122122
janus_anchor_t gen_janus_anchor();
@@ -128,21 +128,3 @@ view_tag_t gen_view_tag();
128128
input_context_t gen_input_context();
129129

130130
} //namespace carrot
131-
132-
namespace std
133-
{
134-
template<class> struct hash;
135-
136-
/// implement STL hashing for address_index_t
137-
template<>
138-
struct hash<carrot::janus_anchor_t>
139-
{
140-
std::size_t operator()(const carrot::janus_anchor_t &_v) const;
141-
};
142-
/// implement STL hashing for input_context_t
143-
template<>
144-
struct hash<carrot::input_context_t>
145-
{
146-
std::size_t operator()(const carrot::input_context_t &_v) const;
147-
};
148-
} //namespace std

src/carrot_core/destination.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct CarrotDestinationV1 final
6363

6464
/// equality operators
6565
bool operator==(const CarrotDestinationV1 &a, const CarrotDestinationV1 &b);
66-
bool operator!=(const CarrotDestinationV1 &a, const CarrotDestinationV1 &b) { return !(a == b); }
66+
static inline bool operator!=(const CarrotDestinationV1 &a, const CarrotDestinationV1 &b) { return !(a == b); }
6767

6868
/**
6969
* brief: make_carrot_main_address_v1 - make a destination address

src/carrot_core/enote_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void make_carrot_sender_receiver_secret(const unsigned char s_sender_receiver_un
189189
// s^ctx_sr = H_32(s_sr, D_e, input_context)
190190
const auto transcript = sp::make_fixed_transcript<CARROT_DOMAIN_SEP_SENDER_RECEIVER_SECRET>(
191191
enote_ephemeral_pubkey, input_context);
192-
derive_bytes_32(transcript.data(), transcript.size, &s_sender_receiver_unctx, &s_sender_receiver_out);
192+
derive_bytes_32(transcript.data(), transcript.size, s_sender_receiver_unctx, &s_sender_receiver_out);
193193
}
194194
//-------------------------------------------------------------------------------------------------------------------
195195
void make_carrot_onetime_address_extension_g(const crypto::hash &s_sender_receiver,

src/carrot_core/hash_functions.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828

2929
//paired header
30-
#include "sp_hash_functions.h"
30+
#include "hash_functions.h"
3131

3232
//local headers
3333
extern "C"
@@ -41,11 +41,10 @@ extern "C"
4141

4242
//standard headers
4343

44-
4544
#undef MONERO_DEFAULT_LOG_CATEGORY
4645
#define MONERO_DEFAULT_LOG_CATEGORY "carrot"
4746

48-
namespace sp
47+
namespace carrot
4948
{
5049
//-------------------------------------------------------------------------------------------------------------------
5150
// H_x[k](data)
@@ -107,4 +106,4 @@ void derive_scalar(const void *data, const std::size_t data_length, const void *
107106
memcpy(hash_out, temp, 32);
108107
}
109108
//-------------------------------------------------------------------------------------------------------------------
110-
} //namespace sp
109+
} //namespace carrot

0 commit comments

Comments
 (0)