aboutsummaryrefslogtreecommitdiff
path: root/src/device
diff options
context:
space:
mode:
Diffstat (limited to 'src/device')
-rw-r--r--src/device/device_default.cpp19
-rw-r--r--src/device/device_ledger.cpp70
-rw-r--r--src/device/device_ledger.hpp44
3 files changed, 119 insertions, 14 deletions
diff --git a/src/device/device_default.cpp b/src/device/device_default.cpp
index dc06ce237..57ac7c1b2 100644
--- a/src/device/device_default.cpp
+++ b/src/device/device_default.cpp
@@ -36,9 +36,7 @@
#include "cryptonote_basic/subaddress_index.h"
#include "cryptonote_core/cryptonote_tx_utils.h"
#include "ringct/rctOps.h"
-
-#define ENCRYPTED_PAYMENT_ID_TAIL 0x8d
-#define CHACHA8_KEY_TAIL 0x8c
+#include "cryptonote_config.h"
namespace hw {
@@ -107,7 +105,7 @@ namespace hw {
epee::mlocked<tools::scrubbed_arr<char, sizeof(view_key) + sizeof(spend_key) + 1>> data;
memcpy(data.data(), &view_key, sizeof(view_key));
memcpy(data.data() + sizeof(view_key), &spend_key, sizeof(spend_key));
- data[sizeof(data) - 1] = CHACHA8_KEY_TAIL;
+ data[sizeof(data) - 1] = config::HASH_KEY_WALLET;
crypto::generate_chacha_key(data.data(), sizeof(data), key, kdf_rounds);
return true;
}
@@ -196,14 +194,13 @@ namespace hw {
}
crypto::secret_key device_default::get_subaddress_secret_key(const crypto::secret_key &a, const cryptonote::subaddress_index &index) {
- const char prefix[] = "SubAddr";
- char data[sizeof(prefix) + sizeof(crypto::secret_key) + 2 * sizeof(uint32_t)];
- memcpy(data, prefix, sizeof(prefix));
- memcpy(data + sizeof(prefix), &a, sizeof(crypto::secret_key));
+ char data[sizeof(config::HASH_KEY_SUBADDRESS) + sizeof(crypto::secret_key) + 2 * sizeof(uint32_t)];
+ memcpy(data, config::HASH_KEY_SUBADDRESS, sizeof(config::HASH_KEY_SUBADDRESS));
+ memcpy(data + sizeof(config::HASH_KEY_SUBADDRESS), &a, sizeof(crypto::secret_key));
uint32_t idx = SWAP32LE(index.major);
- memcpy(data + sizeof(prefix) + sizeof(crypto::secret_key), &idx, sizeof(uint32_t));
+ memcpy(data + sizeof(config::HASH_KEY_SUBADDRESS) + sizeof(crypto::secret_key), &idx, sizeof(uint32_t));
idx = SWAP32LE(index.minor);
- memcpy(data + sizeof(prefix) + sizeof(crypto::secret_key) + sizeof(uint32_t), &idx, sizeof(uint32_t));
+ memcpy(data + sizeof(config::HASH_KEY_SUBADDRESS) + sizeof(crypto::secret_key) + sizeof(uint32_t), &idx, sizeof(uint32_t));
crypto::secret_key m;
crypto::hash_to_scalar(data, sizeof(data), m);
return m;
@@ -344,7 +341,7 @@ namespace hw {
return false;
memcpy(data, &derivation, 32);
- data[32] = ENCRYPTED_PAYMENT_ID_TAIL;
+ data[32] = config::HASH_KEY_ENCRYPTED_PAYMENT_ID;
cn_fast_hash(data, 33, hash);
for (size_t b = 0; b < 8; ++b)
diff --git a/src/device/device_ledger.cpp b/src/device/device_ledger.cpp
index 49f54e5a5..eaa9f910d 100644
--- a/src/device/device_ledger.cpp
+++ b/src/device/device_ledger.cpp
@@ -55,7 +55,10 @@ namespace hw {
}
#define TRACKD MTRACE("hw")
- #define ASSERT_SW(sw,ok,msk) CHECK_AND_ASSERT_THROW_MES(((sw)&(mask))==(ok), "Wrong Device Status : SW=" << std::hex << (sw) << " (EXPECT=" << std::hex << (ok) << ", MASK=" << std::hex << (mask) << ")") ;
+ #define ASSERT_SW(sw,ok,msk) CHECK_AND_ASSERT_THROW_MES(((sw)&(mask))==(ok), \
+ "Wrong Device Status: " << "0x" << std::hex << (sw) << " (" << Status::to_string(sw) << "), " << \
+ "EXPECTED 0x" << std::hex << (ok) << " (" << Status::to_string(ok) << "), " << \
+ "MASK 0x" << std::hex << (mask));
#define ASSERT_T0(exp) CHECK_AND_ASSERT_THROW_MES(exp, "Protocol assert failure: "#exp ) ;
#define ASSERT_X(exp,msg) CHECK_AND_ASSERT_THROW_MES(exp, msg);
@@ -64,6 +67,71 @@ namespace hw {
crypto::secret_key dbg_spendkey;
#endif
+ struct Status
+ {
+ unsigned int code;
+ const char *string;
+
+ constexpr operator unsigned int() const
+ {
+ return this->code;
+ }
+
+ static const char *to_string(unsigned int code);
+ };
+
+ // Must be sorted in ascending order by the code
+ #define LEDGER_STATUS(status) {status, #status}
+ constexpr Status status_codes[] = {
+ LEDGER_STATUS(SW_BYTES_REMAINING_00),
+ LEDGER_STATUS(SW_WARNING_STATE_UNCHANGED),
+ LEDGER_STATUS(SW_STATE_TERMINATED),
+ LEDGER_STATUS(SW_MORE_DATA_AVAILABLE),
+ LEDGER_STATUS(SW_WRONG_LENGTH),
+ LEDGER_STATUS(SW_LOGICAL_CHANNEL_NOT_SUPPORTED),
+ LEDGER_STATUS(SW_SECURE_MESSAGING_NOT_SUPPORTED),
+ LEDGER_STATUS(SW_LAST_COMMAND_EXPECTED),
+ LEDGER_STATUS(SW_COMMAND_CHAINING_NOT_SUPPORTED),
+ LEDGER_STATUS(SW_SECURITY_LOAD_KEY),
+ LEDGER_STATUS(SW_SECURITY_COMMITMENT_CONTROL),
+ LEDGER_STATUS(SW_SECURITY_AMOUNT_CHAIN_CONTROL),
+ LEDGER_STATUS(SW_SECURITY_COMMITMENT_CHAIN_CONTROL),
+ LEDGER_STATUS(SW_SECURITY_OUTKEYS_CHAIN_CONTROL),
+ LEDGER_STATUS(SW_SECURITY_MAXOUTPUT_REACHED),
+ LEDGER_STATUS(SW_SECURITY_TRUSTED_INPUT),
+ LEDGER_STATUS(SW_CLIENT_NOT_SUPPORTED),
+ LEDGER_STATUS(SW_SECURITY_STATUS_NOT_SATISFIED),
+ LEDGER_STATUS(SW_FILE_INVALID),
+ LEDGER_STATUS(SW_PIN_BLOCKED),
+ LEDGER_STATUS(SW_DATA_INVALID),
+ LEDGER_STATUS(SW_CONDITIONS_NOT_SATISFIED),
+ LEDGER_STATUS(SW_COMMAND_NOT_ALLOWED),
+ LEDGER_STATUS(SW_APPLET_SELECT_FAILED),
+ LEDGER_STATUS(SW_WRONG_DATA),
+ LEDGER_STATUS(SW_FUNC_NOT_SUPPORTED),
+ LEDGER_STATUS(SW_FILE_NOT_FOUND),
+ LEDGER_STATUS(SW_RECORD_NOT_FOUND),
+ LEDGER_STATUS(SW_FILE_FULL),
+ LEDGER_STATUS(SW_INCORRECT_P1P2),
+ LEDGER_STATUS(SW_REFERENCED_DATA_NOT_FOUND),
+ LEDGER_STATUS(SW_WRONG_P1P2),
+ LEDGER_STATUS(SW_CORRECT_LENGTH_00),
+ LEDGER_STATUS(SW_INS_NOT_SUPPORTED),
+ LEDGER_STATUS(SW_CLA_NOT_SUPPORTED),
+ LEDGER_STATUS(SW_UNKNOWN),
+ LEDGER_STATUS(SW_OK),
+ LEDGER_STATUS(SW_ALGORITHM_UNSUPPORTED)
+ };
+
+ const char *Status::to_string(unsigned int code)
+ {
+ constexpr size_t status_codes_size = sizeof(status_codes) / sizeof(status_codes[0]);
+ constexpr const Status *status_codes_end = &status_codes[status_codes_size];
+
+ const Status *item = std::lower_bound(&status_codes[0], status_codes_end, code);
+ return (item == status_codes_end || code < *item) ? "UNKNOWN" : item->string;
+ }
+
/* ===================================================================== */
/* === hmacmap ==== */
/* ===================================================================== */
diff --git a/src/device/device_ledger.hpp b/src/device/device_ledger.hpp
index 05a26143a..e3e30fba8 100644
--- a/src/device/device_ledger.hpp
+++ b/src/device/device_ledger.hpp
@@ -58,6 +58,46 @@ namespace hw {
#ifdef WITH_DEVICE_LEDGER
+ // Origin: https://github.com/LedgerHQ/ledger-app-monero/blob/master/src/monero_types.h
+ #define SW_BYTES_REMAINING_00 0x6100
+ #define SW_WARNING_STATE_UNCHANGED 0x6200
+ #define SW_STATE_TERMINATED 0x6285
+ #define SW_MORE_DATA_AVAILABLE 0x6310
+ #define SW_WRONG_LENGTH 0x6700
+ #define SW_LOGICAL_CHANNEL_NOT_SUPPORTED 0x6881
+ #define SW_SECURE_MESSAGING_NOT_SUPPORTED 0x6882
+ #define SW_LAST_COMMAND_EXPECTED 0x6883
+ #define SW_COMMAND_CHAINING_NOT_SUPPORTED 0x6884
+ #define SW_SECURITY_LOAD_KEY 0x6900
+ #define SW_SECURITY_COMMITMENT_CONTROL 0x6911
+ #define SW_SECURITY_AMOUNT_CHAIN_CONTROL 0x6912
+ #define SW_SECURITY_COMMITMENT_CHAIN_CONTROL 0x6913
+ #define SW_SECURITY_OUTKEYS_CHAIN_CONTROL 0x6914
+ #define SW_SECURITY_MAXOUTPUT_REACHED 0x6915
+ #define SW_SECURITY_TRUSTED_INPUT 0x6916
+ #define SW_CLIENT_NOT_SUPPORTED 0x6930
+ #define SW_SECURITY_STATUS_NOT_SATISFIED 0x6982
+ #define SW_FILE_INVALID 0x6983
+ #define SW_PIN_BLOCKED 0x6983
+ #define SW_DATA_INVALID 0x6984
+ #define SW_CONDITIONS_NOT_SATISFIED 0x6985
+ #define SW_COMMAND_NOT_ALLOWED 0x6986
+ #define SW_APPLET_SELECT_FAILED 0x6999
+ #define SW_WRONG_DATA 0x6a80
+ #define SW_FUNC_NOT_SUPPORTED 0x6a81
+ #define SW_FILE_NOT_FOUND 0x6a82
+ #define SW_RECORD_NOT_FOUND 0x6a83
+ #define SW_FILE_FULL 0x6a84
+ #define SW_INCORRECT_P1P2 0x6a86
+ #define SW_REFERENCED_DATA_NOT_FOUND 0x6a88
+ #define SW_WRONG_P1P2 0x6b00
+ #define SW_CORRECT_LENGTH_00 0x6c00
+ #define SW_INS_NOT_SUPPORTED 0x6d00
+ #define SW_CLA_NOT_SUPPORTED 0x6e00
+ #define SW_UNKNOWN 0x6f00
+ #define SW_OK 0x9000
+ #define SW_ALGORITHM_UNSUPPORTED 0x9484
+
namespace {
bool apdu_verbose =true;
}
@@ -128,8 +168,8 @@ namespace hw {
unsigned int id;
void logCMD(void);
void logRESP(void);
- unsigned int exchange(unsigned int ok=0x9000, unsigned int mask=0xFFFF);
- unsigned int exchange_wait_on_input(unsigned int ok=0x9000, unsigned int mask=0xFFFF);
+ unsigned int exchange(unsigned int ok=SW_OK, unsigned int mask=0xFFFF);
+ unsigned int exchange_wait_on_input(unsigned int ok=SW_OK, unsigned int mask=0xFFFF);
void reset_buffer(void);
int set_command_header(unsigned char ins, unsigned char p1 = 0x00, unsigned char p2 = 0x00);
int set_command_header_noopt(unsigned char ins, unsigned char p1 = 0x00, unsigned char p2 = 0x00);