Debian Codegraph @

src/device/log.cpp

raw symbols SCIP
1 // Copyright (c) 2017-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
30 #include "misc_log_ex.h"
31 #include "log.hpp"
32
33 namespace hw {
34
35 #undef MONERO_DEFAULT_LOG_CATEGORY
36 #define MONERO_DEFAULT_LOG_CATEGORY "device"
37
38 void buffer_to_str(char *to_buff, size_t to_len, const char *buff, size_t len) {
39 CHECK_AND_ASSERT_THROW_MES(to_len > (len*2), "destination buffer too short. At least" << (len*2+1) << " bytes required");
40 for (size_t i=0; i<len; i++) {
41 sprintf(to_buff+2*i, "%.02x", (unsigned char)buff[i]);
42 }
43 }
44
45 void log_hexbuffer(const std::string &msg, const char* buff, size_t len) {
46 char logstr[1025];
47 buffer_to_str(logstr, sizeof(logstr), buff, len);
48 MDEBUG(msg<< ": " << logstr);
49 }
50
51 void log_message(const std::string &msg, const std::string &info ) {
52 MDEBUG(msg << ": " << info);
53 }
54
55
56 #ifdef WITH_DEVICE_LEDGER
57 namespace ledger {
58
59 #undef MONERO_DEFAULT_LOG_CATEGORY
60 #define MONERO_DEFAULT_LOG_CATEGORY "device.ledger"
61
62
63 #ifdef DEBUG_HWDEVICE
64 extern crypto::secret_key dbg_viewkey;
65 extern crypto::secret_key dbg_spendkey;
66
67
68 void decrypt(char* buf, size_t len) {
69 #if defined(IODUMMYCRYPT_HWDEVICE) || defined(IONOCRYPT_HWDEVICE)
70 size_t i;
71 if (len == 32) {
72 //view key?
73 for (i = 0; i<32; i++) {
74 if (buf[i] != 0) break;
75 }
76 if (i == 32) {
77 memmove(buf, hw::ledger::dbg_viewkey.data, 32);
78 return;
79 }
80 //spend key?
81 for (i = 0; i<32; i++) {
82 if (buf[i] != (char)0xff) break;
83 }
84 if (i == 32) {
85 memmove(buf, hw::ledger::dbg_spendkey.data, 32);
86 return;
87 }
88 }
89 #if defined(IODUMMYCRYPT_HWDEVICE)
90 //std decrypt: XOR.55h
91 for (i = 0; i<len;i++) {
92 buf[i] ^= 0x55;
93 }
94 #endif
95 #endif
96 }
97
98 crypto::key_derivation decrypt(const crypto::key_derivation &derivation) {
99 crypto::key_derivation x = derivation;
100 decrypt(x.data, 32);
101 return x;
102 }
103
104 cryptonote::account_keys decrypt(const cryptonote::account_keys& keys) {
105 cryptonote::account_keys x = keys;
106 decrypt(x.m_view_secret_key.data, 32);
107 decrypt(x.m_spend_secret_key.data, 32);
108 return x;
109 }
110
111
112 crypto::secret_key decrypt(const crypto::secret_key &sec) {
113 crypto::secret_key x = sec;
114 decrypt(x.data, 32);
115 return x;
116 }
117
118 rct::key decrypt(const rct::key &sec) {
119 rct::key x = sec;
120 decrypt((char*)x.bytes, 32);
121 return x;
122 }
123
124 crypto::ec_scalar decrypt(const crypto::ec_scalar &res) {
125 crypto::ec_scalar x = res;
126 decrypt((char*)x.data, 32);
127 return x;
128 }
129
130 rct::keyV decrypt(const rct::keyV &keys) {
131 rct::keyV x ;
132 x.reserve(keys.size());
133 for (unsigned int j = 0; j<keys.size(); j++) {
134 x.push_back(decrypt(keys[j]));
135 }
136 return x;
137 }
138
139 static void check(const std::string &msg, const std::string &info, const char *h, const char *d, size_t len, bool crypted) {
140 char dd[32];
141 char logstr[128];
142
143 CHECK_AND_ASSERT_THROW_MES(len <= sizeof(dd), "invalid len");
144 memmove(dd,d,len);
145 if (crypted) {
146 CHECK_AND_ASSERT_THROW_MES(len<=32, "encrypted data greater than 32");
147 decrypt(dd,len);
148 }
149
150 if (memcmp(h,dd,len)) {
151 log_message("ASSERT EQ FAIL", msg + ": "+ info );
152 log_hexbuffer(" host ", h, len);
153 log_hexbuffer(" device", dd, len);
154
155 } else {
156 buffer_to_str(logstr, 128, dd, len);
157 log_message("ASSERT EQ OK", msg + ": "+ info + ": "+ std::string(logstr) );
158 }
159 }
160
161 void check32(const std::string &msg, const std::string &info, const char *h, const char *d, bool crypted) {
162 check(msg, info, h, d, 32, crypted);
163 }
164
165 void check8(const std::string &msg, const std::string &info, const char *h, const char *d, bool crypted) {
166 check(msg, info, h, d, 8, crypted);
167 }
168
169 void check1(const std::string &msg, const std::string &info, const char *h, const char *d, bool crypted) {
170 check(msg, info, h, d, 1, crypted);
171 }
172 #endif
173
174 }
175 #endif //WITH_DEVICE_LEDGER
176
177 }