diff --git a/Makefile b/Makefile index 81da667..fa0c9e0 100644 --- a/Makefile +++ b/Makefile @@ -157,6 +157,11 @@ ifneq ($(CAL_CI_KEY),0) DEFINES += HAVE_CAL_CI_KEY endif +# ENS +ifneq ($(TARGET_NAME),TARGET_NANOS) +DEFINES += HAVE_DOMAIN_NAME +endif + # Enabling debug PRINTF DEBUG:=0 ifneq ($(DEBUG),0) diff --git a/src/apdu_constants.h b/src/apdu_constants.h index cab0688..7911d6b 100644 --- a/src/apdu_constants.h +++ b/src/apdu_constants.h @@ -24,6 +24,8 @@ #define INS_EIP712_STRUCT_DEF 0x1A #define INS_EIP712_STRUCT_IMPL 0x1C #define INS_EIP712_FILTERING 0x1E +#define INS_ENS_GET_CHALLENGE 0x20 +#define INS_ENS_PROVIDE_INFO 0x22 #define P1_CONFIRM 0x01 #define P1_NON_CONFIRM 0x00 #define P2_NO_CHAINCODE 0x00 diff --git a/src/main.c b/src/main.c index 3ba8bd6..ea024ab 100644 --- a/src/main.c +++ b/src/main.c @@ -29,6 +29,7 @@ #include "handle_get_printable_amount.h" #include "handle_check_address.h" #include "commands_712.h" +#include "challenge.h" #ifdef HAVE_STARKWARE #include "stark_crypto.h" @@ -749,6 +750,12 @@ void handleApdu(unsigned int *flags, unsigned int *tx) { break; #endif // HAVE_EIP712_FULL_SUPPORT +#ifdef HAVE_DOMAIN_NAME + case INS_ENS_GET_CHALLENGE: + handle_get_challenge(); + break; +#endif // HAVE_DOMAIN_NAME + #if 0 case 0xFF: // return to dashboard goto return_to_dashboard; @@ -980,6 +987,11 @@ void coin_main(chain_config_t *coin_config) { BLE_power(1, "Nano X"); #endif // HAVE_BLE +#ifdef HAVE_DOMAIN_NAME + // to prevent it from having a fixed value at boot + roll_challenge(); +#endif // HAVE_DOMAIN_NAME + app_main(); } CATCH(EXCEPTION_IO_RESET) { diff --git a/src_features/getChallenge/challenge.h b/src_features/getChallenge/challenge.h new file mode 100644 index 0000000..9fdb01d --- /dev/null +++ b/src_features/getChallenge/challenge.h @@ -0,0 +1,14 @@ +#ifdef HAVE_DOMAIN_NAME + +#ifndef CHALLENGE_H_ +#define CHALLENGE_H_ + +#include + +void roll_challenge(void); +uint32_t get_challenge(void); +void handle_get_challenge(void); + +#endif // CHALLENGE_H_ + +#endif // HAVE_DOMAIN_NAME diff --git a/src_features/getChallenge/cmd_get_challenge.c b/src_features/getChallenge/cmd_get_challenge.c new file mode 100644 index 0000000..93e52d9 --- /dev/null +++ b/src_features/getChallenge/cmd_get_challenge.c @@ -0,0 +1,38 @@ +#ifdef HAVE_DOMAIN_NAME + +#include +#include +#include +#include "apdu_constants.h" +#include "challenge.h" + +static uint32_t challenge; + +/** + * Generate a new challenge from the Random Number Generator + */ +void roll_challenge(void) { + challenge = cx_rng_u32(); +} + +/** + * Get the current challenge + * + * @return challenge + */ +uint32_t get_challenge(void) { + return challenge; +} + +/** + * Send back the current challenge + */ +void handle_get_challenge(void) { + PRINTF("New challenge -> %u\n", get_challenge()); + U4BE_ENCODE(G_io_apdu_buffer, 0, get_challenge()); + U2BE_ENCODE(G_io_apdu_buffer, 4, APDU_RESPONSE_OK); + + io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 6); +} + +#endif // HAVE_DOMAIN_NAME