diff --git a/Makefile b/Makefile index df9129c..e9d02a2 100644 --- a/Makefile +++ b/Makefile @@ -79,7 +79,7 @@ VARIANT_PARAM = COIN VARIANT_VALUES = KAS # Enabling DEBUG flag will enable PRINTF and disable optimizations -DEBUG = 1 +#DEBUG = 1 ######################################## # Application custom permissions # diff --git a/doc/COMMANDS.md b/doc/COMMANDS.md index f15e093..0aa991c 100644 --- a/doc/COMMANDS.md +++ b/doc/COMMANDS.md @@ -173,5 +173,11 @@ Transactions signed with ECDSA are currently not supported. | 0xB00A | `SW_WRONG_BIP32_COIN_TYPE` | `Coin Type` must be `111111'` | | 0xB00B | `SW_WRONG_BIP32_TYPE` | `Type` passed is not valid. Must be either `0` for `Receive` or `1` for `Change`| | 0xB00C | `SW_WRONG_BIP32_PATH_LEN` | Path length must be `5` | -| 0xB00D | `SW_MESSAGE_TOO_LONG` | Message len greater than max | +| 0xB010 | `SW_MESSAGE_PARSING_FAIL` | Unable to parse message data | +| 0xB011 | `SW_MESSAGE_TOO_LONG` | Message len greater than max | +| 0xB012 | `SW_MESSAGE_TOO_SHORT` | Message len is 0 | +| 0xB013 | `SW_MESSAGE_ADDRESS_TYPE_FAIL` | Address type could not be parsed or is not `0`/`1` | +| 0xB014 | `SW_MESSAGE_ADDRESS_INDEX_FAIL` | Address index could not be parsed | +| 0xB015 | `SW_MESSAGE_LEN_PARSING_FAIL` | Message length could not be parsed | +| 0xB016 | `SW_MESSAGE_UNEXPECTED` | Unexpected error while parsing message | | 0x9000 | `OK` | Success | diff --git a/fuzzing/fuzz_tx_parser.cc b/fuzzing/fuzz_tx_parser.cc index 04fa0b1..4179b83 100644 --- a/fuzzing/fuzz_tx_parser.cc +++ b/fuzzing/fuzz_tx_parser.cc @@ -28,7 +28,7 @@ extern "C" { #include "buffer.h" -#include "common/format.h" +#include "common/format_local.h" #include "transaction/deserialize.h" #include "transaction/types.h" } diff --git a/fuzzing/fuzz_txin_parser.cc b/fuzzing/fuzz_txin_parser.cc index cfdb234..5bc030f 100644 --- a/fuzzing/fuzz_txin_parser.cc +++ b/fuzzing/fuzz_txin_parser.cc @@ -28,7 +28,7 @@ extern "C" { #include "buffer.h" -#include "common/format.h" +#include "common/format_local.h" #include "transaction/deserialize.h" #include "transaction/types.h" } diff --git a/fuzzing/fuzz_txout_parser.cc b/fuzzing/fuzz_txout_parser.cc index 9b70ea3..cee3e4a 100644 --- a/fuzzing/fuzz_txout_parser.cc +++ b/fuzzing/fuzz_txout_parser.cc @@ -28,7 +28,7 @@ extern "C" { #include "buffer.h" -#include "common/format.h" +#include "common/format_local.h" #include "transaction/deserialize.h" #include "transaction/types.h" } diff --git a/src/common/format.c b/src/common/format.c deleted file mode 100644 index d614b61..0000000 --- a/src/common/format.c +++ /dev/null @@ -1,187 +0,0 @@ -/***************************************************************************** - * MIT License - * - * Copyright (c) 2023 coderofstuff - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - *****************************************************************************/ - -#include // size_t -#include // int*_t, uint*_t -#include // strncpy, memmove -#include // bool - -#include "format.h" - -bool format_i64(char *dst, size_t dst_len, const int64_t value) { - char temp[] = "-9223372036854775808"; - - char *ptr = temp; - int64_t num = value; - int sign = 1; - - if (value < 0) { - sign = -1; - } - - while (num != 0) { - *ptr++ = '0' + (num % 10) * sign; - num /= 10; - } - - if (value < 0) { - *ptr++ = '-'; - } else if (value == 0) { - *ptr++ = '0'; - } - - int distance = (ptr - temp) + 1; - - if ((int) dst_len < distance) { - return false; - } - - size_t index = 0; - - while (--ptr >= temp) { - dst[index++] = *ptr; - } - - dst[index] = '\0'; - - return true; -} - -bool format_u64(char *out, size_t outLen, uint64_t in) { - uint8_t i = 0; - - if (outLen == 0) { - return false; - } - outLen--; - - while (in > 9) { - out[i] = in % 10 + '0'; - in /= 10; - i++; - if (i + 1 > outLen) { - return false; - } - } - out[i] = in + '0'; - out[i + 1] = '\0'; - - uint8_t j = 0; - char tmp; - - // revert the string - while (j < i) { - // swap out[j] and out[i] - tmp = out[j]; - out[j] = out[i]; - out[i] = tmp; - - i--; - j++; - } - return true; -} - -bool format_fpu64_trimmed(char *dst, size_t dst_len, const uint64_t value, uint8_t decimals) { - char buffer[21] = {0}; - - if (!format_u64(buffer, sizeof(buffer), value)) { - return false; - } - - if (value == 0) { - if (dst_len <= 1) { - return false; - } - - dst[0] = '0'; - dst[1] = '\0'; - - return true; - } - - size_t digits = strlen(buffer); - size_t offset = 0; - - while (offset < decimals && digits > offset + 1 && buffer[digits - offset - 1] == '0') { - buffer[digits - offset - 1] = '\0'; - offset++; - } - - digits -= offset; - decimals -= offset; - - if (decimals == 0) { - strncpy(dst, buffer, dst_len); - } else if (digits <= decimals) { - if (dst_len <= 2 + decimals - digits) { - return false; - } - *dst++ = '0'; - *dst++ = '.'; - for (uint16_t i = 0; i < decimals - digits; i++, dst++) { - *dst = '0'; - } - dst_len -= 2 + decimals - digits; - strncpy(dst, buffer, dst_len); - } else { - if (dst_len <= digits + 1 + decimals) { - return false; - } - - const size_t shift = digits - decimals; - memmove(dst, buffer, shift); - dst[shift] = '.'; - strncpy(dst + shift + 1, buffer + shift, decimals); - } - - return true; -} - -int format_hex(const uint8_t *in, size_t in_len, char *out, size_t out_len) { - if (out_len < 2 * in_len + 1) { - return -1; - } - - const char hex[] = "0123456789ABCDEF"; - size_t i = 0; - int written = 0; - - while (i < in_len && (i * 2 + (2 + 1)) <= out_len) { - uint8_t high_nibble = (in[i] & 0xF0) >> 4; - *out = hex[high_nibble]; - out++; - - uint8_t low_nibble = in[i] & 0x0F; - *out = hex[low_nibble]; - out++; - - i++; - written += 2; - } - - *out = '\0'; - - return written + 1; -} diff --git a/src/common/format.h b/src/common/format.h deleted file mode 100644 index 1c8d486..0000000 --- a/src/common/format.h +++ /dev/null @@ -1,92 +0,0 @@ -/***************************************************************************** - * MIT License - * - * Copyright (c) 2023 coderofstuff - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - *****************************************************************************/ -#pragma once - -#include // size_t -#include // int*_t, uint*_t -#include // bool - -/** - * Format 64-bit signed integer as string. - * - * @param[out] dst - * Pointer to output string. - * @param[in] dst_len - * Length of output string. - * @param[in] value - * 64-bit signed integer to format. - * - * @return true if success, false otherwise. - * - */ -bool format_i64(char *dst, size_t dst_len, const int64_t value); - -/** - * Format 64-bit unsigned integer as string. - * - * @param[out] dst - * Pointer to output string. - * @param[in] dst_len - * Length of output string. - * @param[in] value - * 64-bit unsigned integer to format. - * - * @return true if success, false otherwise. - * - */ -bool format_u64(char *dst, size_t dst_len, uint64_t value); - -/** - * Format 64-bit unsigned integer as string with decimals. - * - * @param[out] dst - * Pointer to output string. - * @param[in] dst_len - * Length of output string. - * @param[in] value - * 64-bit unsigned integer to format. - * @param[in] decimals - * Number of digits after decimal separator. - * - * @return true if success, false otherwise. - * - */ -bool format_fpu64_trimmed(char *dst, size_t dst_len, const uint64_t value, uint8_t decimals); - -/** - * Format byte buffer to uppercase hexadecimal string. - * - * @param[in] in - * Pointer to input byte buffer. - * @param[in] in_len - * Length of input byte buffer. - * @param[out] out - * Pointer to output string. - * @param[in] out_len - * Length of output string. - * - * @return number of bytes written if success, -1 otherwise. - * - */ -int format_hex(const uint8_t *in, size_t in_len, char *out, size_t out_len); diff --git a/src/common/format_local.c b/src/common/format_local.c new file mode 100644 index 0000000..bea7994 --- /dev/null +++ b/src/common/format_local.c @@ -0,0 +1,60 @@ +/***************************************************************************** + * MIT License + * + * Copyright (c) 2023 coderofstuff + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ + +#include // size_t +#include // int*_t, uint*_t +#include // strncpy, memmove +#include // bool +#include // sprintf, snprintf +#include // isprint, isspace + +#include "./format_local.h" + +void format_message_to_sign(char* msg_dest, int msg_dest_len, char* msg_src, int msg_src_len) { + int c; + int dest_idx = 0; + + for (int i = 0; i < msg_src_len && dest_idx < msg_dest_len; i++) { + c = msg_src[i]; + if (isspace(c)) // to replace all white-space characters as spaces + { + c = ' '; + } + if (isprint(c)) { + sprintf(msg_dest + dest_idx, "%c", (char) c); + dest_idx++; + } else { + int remaining_buffer_length = msg_dest_len - dest_idx - 1; + if (remaining_buffer_length >= 4) // 4 being the fixed length of \x00 + { + snprintf(msg_dest + dest_idx, remaining_buffer_length, "\\x%02x", c); + dest_idx += 4; + } else { + // fill the rest of the UI buffer spaces, to consider the buffer full + memset(msg_dest + dest_idx, ' ', remaining_buffer_length); + dest_idx += remaining_buffer_length; + } + } + } +} diff --git a/src/common/macros.h b/src/common/format_local.h similarity index 68% rename from src/common/macros.h rename to src/common/format_local.h index cc8f3d3..5fb8fca 100644 --- a/src/common/macros.h +++ b/src/common/format_local.h @@ -23,7 +23,21 @@ *****************************************************************************/ #pragma once +#include // size_t +#include // int*_t, uint*_t +#include // bool + /** - * Macro for the size of a specific structure field. + * Calculate the fees by checking the difference between inputs and outputs + * @param[out] msg_dest + * Pointer to formatted message destination + * @param[out] msg_dest_len + * Length of message destination. + * @param[in] msg_src + * Pointer to message source to format. + * @param[in] msg_src_len + * Length of message source. + * + * @return number of bytes written if success, -1 otherwise. */ -#define MEMBER_SIZE(type, member) (sizeof(((type *) 0)->member)) +void format_message_to_sign(char* msg_dest, int msg_dest_len, char* msg_src, int msg_src_len); diff --git a/src/common/write.c b/src/common/write.c deleted file mode 100644 index 0c0eccc..0000000 --- a/src/common/write.c +++ /dev/null @@ -1,72 +0,0 @@ -/***************************************************************************** - * MIT License - * - * Copyright (c) 2023 coderofstuff - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - *****************************************************************************/ - -#include // uint*_t -#include // size_t - -void write_u16_be(uint8_t *ptr, size_t offset, uint16_t value) { - ptr[offset + 0] = (uint8_t)(value >> 8); - ptr[offset + 1] = (uint8_t)(value >> 0); -} - -void write_u32_be(uint8_t *ptr, size_t offset, uint32_t value) { - ptr[offset + 0] = (uint8_t)(value >> 24); - ptr[offset + 1] = (uint8_t)(value >> 16); - ptr[offset + 2] = (uint8_t)(value >> 8); - ptr[offset + 3] = (uint8_t)(value >> 0); -} - -void write_u64_be(uint8_t *ptr, size_t offset, uint64_t value) { - ptr[offset + 0] = (uint8_t)(value >> 56); - ptr[offset + 1] = (uint8_t)(value >> 48); - ptr[offset + 2] = (uint8_t)(value >> 40); - ptr[offset + 3] = (uint8_t)(value >> 32); - ptr[offset + 4] = (uint8_t)(value >> 24); - ptr[offset + 5] = (uint8_t)(value >> 16); - ptr[offset + 6] = (uint8_t)(value >> 8); - ptr[offset + 7] = (uint8_t)(value >> 0); -} - -void write_u16_le(uint8_t *ptr, size_t offset, uint16_t value) { - ptr[offset + 0] = (uint8_t)(value >> 0); - ptr[offset + 1] = (uint8_t)(value >> 8); -} - -void write_u32_le(uint8_t *ptr, size_t offset, uint32_t value) { - ptr[offset + 0] = (uint8_t)(value >> 0); - ptr[offset + 1] = (uint8_t)(value >> 8); - ptr[offset + 2] = (uint8_t)(value >> 16); - ptr[offset + 3] = (uint8_t)(value >> 24); -} - -void write_u64_le(uint8_t *ptr, size_t offset, uint64_t value) { - ptr[offset + 0] = (uint8_t)(value >> 0); - ptr[offset + 1] = (uint8_t)(value >> 8); - ptr[offset + 2] = (uint8_t)(value >> 16); - ptr[offset + 3] = (uint8_t)(value >> 24); - ptr[offset + 4] = (uint8_t)(value >> 32); - ptr[offset + 5] = (uint8_t)(value >> 40); - ptr[offset + 6] = (uint8_t)(value >> 48); - ptr[offset + 7] = (uint8_t)(value >> 56); -} diff --git a/src/common/write.h b/src/common/write.h deleted file mode 100644 index ad73955..0000000 --- a/src/common/write.h +++ /dev/null @@ -1,105 +0,0 @@ -/***************************************************************************** - * MIT License - * - * Copyright (c) 2023 coderofstuff - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - *****************************************************************************/ -#pragma once - -#include // uint*_t -#include // size_t - -/** - * Write 16-bit unsigned integer value as Big Endian. - * - * @param[out] ptr - * Pointer to output byte buffer. - * @param[in] offset - * Offset in the output byte buffer. - * @param[in] value - * 16-bit unsigned integer to write in output byte buffer as Big Endian. - * - */ -void write_u16_be(const uint8_t *ptr, size_t offset, uint16_t value); - -/** - * Write 32-bit unsigned integer value as Big Endian. - * - * @param[out] ptr - * Pointer to output byte buffer. - * @param[in] offset - * Offset in the output byte buffer. - * @param[in] value - * 32-bit unsigned integer to write in output byte buffer as Big Endian. - * - */ -void write_u32_be(uint8_t *ptr, size_t offset, uint32_t value); - -/** - * Write 64-bit unsigned integer value as Big Endian. - * - * @param[out] ptr - * Pointer to output byte buffer. - * @param[in] offset - * Offset in the output byte buffer. - * @param[in] value - * 64-bit unsigned integer to write in output byte buffer as Big Endian. - * - */ -void write_u64_be(uint8_t *ptr, size_t offset, uint64_t value); - -/** - * Write 16-bit unsigned integer value as Little Endian. - * - * @param[out] ptr - * Pointer to output byte buffer. - * @param[in] offset - * Offset in the output byte buffer. - * @param[in] value - * 16-bit unsigned integer to write in output byte buffer as Little Endian. - * - */ -void write_u16_le(uint8_t *ptr, size_t offset, uint16_t value); - -/** - * Write 32-bit unsigned integer value as Little Endian. - * - * @param[out] ptr - * Pointer to output byte buffer. - * @param[in] offset - * Offset in the output byte buffer. - * @param[in] value - * 32-bit unsigned integer to write in output byte buffer as Little Endian. - * - */ -void write_u32_le(uint8_t *ptr, size_t offset, uint32_t value); - -/** - * Write 64-bit unsigned integer value as Little Endian. - * - * @param[out] ptr - * Pointer to output byte buffer. - * @param[in] offset - * Offset in the output byte buffer. - * @param[in] value - * 64-bit unsigned integer to write in output byte buffer as Little Endian. - * - */ -void write_u64_le(uint8_t *ptr, size_t offset, uint64_t value); diff --git a/src/handler/sign_msg.c b/src/handler/sign_msg.c index 5700235..5005842 100644 --- a/src/handler/sign_msg.c +++ b/src/handler/sign_msg.c @@ -50,16 +50,24 @@ int handler_sign_msg(buffer_t *cdata) { G_context.state = STATE_NONE; if (!buffer_read_u8(cdata, &G_context.msg_info.address_type)) { - return io_send_sw(SW_WRONG_DATA_LENGTH); + return io_send_sw(SW_MESSAGE_ADDRESS_TYPE_FAIL); + } + + if (G_context.msg_info.address_type != 0 && G_context.msg_info.address_type != 1) { + return io_send_sw(SW_MESSAGE_ADDRESS_TYPE_FAIL); } if (!buffer_read_u32(cdata, &G_context.msg_info.address_index, BE)) { - return io_send_sw(SW_WRONG_DATA_LENGTH); + return io_send_sw(SW_MESSAGE_ADDRESS_TYPE_FAIL); } uint8_t message_len = 0; if (!buffer_read_u8(cdata, &message_len)) { - return io_send_sw(SW_WRONG_DATA_LENGTH); + return io_send_sw(SW_MESSAGE_LEN_PARSING_FAIL); + } + + if (message_len == 0) { + return io_send_sw(SW_MESSAGE_TOO_SHORT); } if (message_len > MAX_MESSAGE_LEN) { @@ -69,13 +77,13 @@ int handler_sign_msg(buffer_t *cdata) { G_context.msg_info.message_len = (size_t) message_len; if (!buffer_can_read(cdata, G_context.msg_info.message_len)) { - return io_send_sw(SW_WRONG_DATA_LENGTH); + return io_send_sw(SW_MESSAGE_PARSING_FAIL); } memcpy(G_context.msg_info.message, cdata->ptr + cdata->offset, G_context.msg_info.message_len); if (!buffer_seek_cur(cdata, G_context.msg_info.message_len)) { - return io_send_sw(SW_WRONG_DATA_LENGTH); + return io_send_sw(SW_MESSAGE_UNEXPECTED); } G_context.bip32_path[0] = 0x8000002C; diff --git a/src/helper/send_response.h b/src/helper/send_response.h index 49b9695..f15e6f3 100644 --- a/src/helper/send_response.h +++ b/src/helper/send_response.h @@ -24,8 +24,7 @@ #pragma once #include "os.h" - -#include "../common/macros.h" +#include "macros.h" /** * Length of public key. diff --git a/src/sw.h b/src/sw.h index 2f81e8a..a60be47 100644 --- a/src/sw.h +++ b/src/sw.h @@ -84,8 +84,14 @@ */ #define SW_SIGNATURE_FAIL 0xB008 -#define SW_WRONG_BIP32_PURPOSE 0xB009 -#define SW_WRONG_BIP32_COIN_TYPE 0xB00A -#define SW_WRONG_BIP32_TYPE 0xB00B -#define SW_WRONG_BIP32_PATH_LEN 0xB00C -#define SW_MESSAGE_TOO_LONG 0xB00D +#define SW_WRONG_BIP32_PURPOSE 0xB009 +#define SW_WRONG_BIP32_COIN_TYPE 0xB00A +#define SW_WRONG_BIP32_TYPE 0xB00B +#define SW_WRONG_BIP32_PATH_LEN 0xB00C +#define SW_MESSAGE_PARSING_FAIL 0xB010 +#define SW_MESSAGE_TOO_LONG 0xB011 +#define SW_MESSAGE_TOO_SHORT 0xB012 +#define SW_MESSAGE_ADDRESS_TYPE_FAIL 0xB013 +#define SW_MESSAGE_ADDRESS_INDEX_FAIL 0xB014 +#define SW_MESSAGE_LEN_PARSING_FAIL 0xB015 +#define SW_MESSAGE_UNEXPECTED 0xB016 diff --git a/src/ui/bagl_display.c b/src/ui/bagl_display.c index 6e55f66..30b58e7 100644 --- a/src/ui/bagl_display.c +++ b/src/ui/bagl_display.c @@ -44,7 +44,8 @@ #include "../transaction/types.h" #include "../transaction/utils.h" #include "bip32.h" -#include "../common/format.h" +#include "../common/format_local.h" +#include "format.h" #include "../menu.h" static action_validate_cb g_validate_callback; @@ -229,7 +230,7 @@ int ui_display_transaction() { } // Step with icon and text -UX_STEP_NOCB(ux_display_confirm_message_step, pn, {&C_icon_eye, "Confirm Message"}); +UX_STEP_NOCB(ux_display_confirm_message_step, pn, {&C_icon_eye, "Review Message"}); // Step with title/text for message UX_STEP_NOCB(ux_display_message_step, @@ -267,11 +268,11 @@ int ui_display_message() { } memset(g_message, 0, sizeof(g_message)); - snprintf(g_message, - sizeof(g_message), - "%.*s", - G_context.msg_info.message_len, - G_context.msg_info.message); + + format_message_to_sign(g_message, + (int) sizeof(g_message), + (char *) G_context.msg_info.message, + (int) G_context.msg_info.message_len); g_validate_callback = &ui_action_validate_message; diff --git a/src/ui/nbgl_display_address.c b/src/ui/nbgl_display_address.c index 112bb8f..351f8c8 100644 --- a/src/ui/nbgl_display_address.c +++ b/src/ui/nbgl_display_address.c @@ -43,7 +43,7 @@ #include "../types.h" #include "../transaction/types.h" #include "bip32.h" -#include "../common/format.h" +#include "format.h" #include "../menu.h" static char g_address[43]; diff --git a/src/ui/nbgl_display_message.c b/src/ui/nbgl_display_message.c index dee6e2e..40e8093 100644 --- a/src/ui/nbgl_display_message.c +++ b/src/ui/nbgl_display_message.c @@ -43,7 +43,8 @@ #include "../types.h" #include "../transaction/types.h" #include "bip32.h" -#include "../common/format.h" +#include "../common/format_local.h" +#include "format.h" #include "../menu.h" static char g_message[MAX_MESSAGE_LEN]; @@ -59,6 +60,15 @@ static void confirm_message_rejection(void) { nbgl_useCaseStatus("Message signing\ncancelled", false, ui_menu_main); } +static void ask_message_rejection_confirmation(void) { + // display a choice to confirm/cancel rejection + nbgl_useCaseConfirm("Reject message?", + NULL, + "Yes, Reject", + "Go back to message", + confirm_message_rejection); +} + static void confirm_message_approval(void) { // display a success status page and go back to main validate_message(true); @@ -69,7 +79,7 @@ static void review_message_choice(bool confirm) { if (confirm) { confirm_message_approval(); } else { - confirm_message_rejection(); + ask_message_rejection_confirmation(); } } @@ -108,18 +118,17 @@ int ui_display_message() { } memset(g_message, 0, sizeof(g_message)); - snprintf(g_message, - sizeof(g_message), - "%.*s", - G_context.msg_info.message_len, - G_context.msg_info.message); + format_message_to_sign(g_message, + sizeof(g_message), + (char *) G_context.msg_info.message, + G_context.msg_info.message_len); nbgl_useCaseReviewStart(&C_stax_app_kaspa_64px, "Sign Message", NULL, - "Cancel", + "Reject message", continue_message_review, - confirm_message_rejection); + ask_message_rejection_confirmation); return 0; } diff --git a/src/ui/nbgl_display_transaction.c b/src/ui/nbgl_display_transaction.c index 103626e..90c3297 100755 --- a/src/ui/nbgl_display_transaction.c +++ b/src/ui/nbgl_display_transaction.c @@ -44,7 +44,7 @@ #include "../transaction/types.h" #include "../transaction/utils.h" #include "bip32.h" -#include "../common/format.h" +#include "format.h" #include "../menu.h" // Buffer where the transaction amount string is written diff --git a/tests/application_client/kaspa_command_sender.py b/tests/application_client/kaspa_command_sender.py index 74b4530..e5af126 100644 --- a/tests/application_client/kaspa_command_sender.py +++ b/tests/application_client/kaspa_command_sender.py @@ -38,25 +38,31 @@ class InsType(IntEnum): SIGN_MESSAGE = 0x07 class Errors(IntEnum): - SW_DENY = 0x6985 - SW_WRONG_P1P2 = 0x6A86 - SW_WRONG_DATA_LENGTH = 0x6A87 - SW_INS_NOT_SUPPORTED = 0x6D00 - SW_CLA_NOT_SUPPORTED = 0x6E00 - SW_WRONG_RESPONSE_LENGTH = 0xB000 - SW_DISPLAY_BIP32_PATH_FAIL = 0xB001 - SW_DISPLAY_ADDRESS_FAIL = 0xB002 - SW_DISPLAY_AMOUNT_FAIL = 0xB003 - SW_WRONG_TX_LENGTH = 0xB004 - SW_TX_PARSING_FAIL = 0xB005 - SW_TX_HASH_FAIL = 0xB006 - SW_BAD_STATE = 0xB007 - SW_SIGNATURE_FAIL = 0xB008 - SW_WRONG_BIP32_PURPOSE = 0xB009 - SW_WRONG_BIP32_COIN_TYPE = 0xB00A - SW_WRONG_BIP32_TYPE = 0xB00B - SW_WRONG_BIP32_PATH_LEN = 0xB00C - SW_MESSAGE_TOO_LONG = 0xB00D + SW_DENY = 0x6985 + SW_WRONG_P1P2 = 0x6A86 + SW_WRONG_DATA_LENGTH = 0x6A87 + SW_INS_NOT_SUPPORTED = 0x6D00 + SW_CLA_NOT_SUPPORTED = 0x6E00 + SW_WRONG_RESPONSE_LENGTH = 0xB000 + SW_DISPLAY_BIP32_PATH_FAIL = 0xB001 + SW_DISPLAY_ADDRESS_FAIL = 0xB002 + SW_DISPLAY_AMOUNT_FAIL = 0xB003 + SW_WRONG_TX_LENGTH = 0xB004 + SW_TX_PARSING_FAIL = 0xB005 + SW_TX_HASH_FAIL = 0xB006 + SW_BAD_STATE = 0xB007 + SW_SIGNATURE_FAIL = 0xB008 + SW_WRONG_BIP32_PURPOSE = 0xB009 + SW_WRONG_BIP32_COIN_TYPE = 0xB00A + SW_WRONG_BIP32_TYPE = 0xB00B + SW_WRONG_BIP32_PATH_LEN = 0xB00C + SW_MESSAGE_PARSING_FAIL = 0xB010 + SW_MESSAGE_TOO_LONG = 0xB011 + SW_MESSAGE_TOO_SHORT = 0xB012 + SW_MESSAGE_ADDRESS_TYPE_FAIL = 0xB013 + SW_MESSAGE_ADDRESS_INDEX_FAIL = 0xB014 + SW_MESSAGE_LEN_PARSING_FAIL = 0xB015 + SW_MESSAGE_UNEXPECTED = 0xB016 def split_message(message: bytes, max_size: int) -> List[bytes]: return [message[x:x + max_size] for x in range(0, len(message), max_size)] diff --git a/tests/snapshots/nanos/test_sign_message_kanji/00000.png b/tests/snapshots/nanos/test_sign_message_kanji/00000.png index f81b25d..b102b44 100644 Binary files a/tests/snapshots/nanos/test_sign_message_kanji/00000.png and b/tests/snapshots/nanos/test_sign_message_kanji/00000.png differ diff --git a/tests/snapshots/nanos/test_sign_message_kanji/00002.png b/tests/snapshots/nanos/test_sign_message_kanji/00002.png index 82a806e..1b502cc 100644 Binary files a/tests/snapshots/nanos/test_sign_message_kanji/00002.png and b/tests/snapshots/nanos/test_sign_message_kanji/00002.png differ diff --git a/tests/snapshots/nanos/test_sign_message_kanji/00003.png b/tests/snapshots/nanos/test_sign_message_kanji/00003.png index 66c411c..b090b48 100644 Binary files a/tests/snapshots/nanos/test_sign_message_kanji/00003.png and b/tests/snapshots/nanos/test_sign_message_kanji/00003.png differ diff --git a/tests/snapshots/nanos/test_sign_message_kanji/00004.png b/tests/snapshots/nanos/test_sign_message_kanji/00004.png index 9ab6248..a934b17 100644 Binary files a/tests/snapshots/nanos/test_sign_message_kanji/00004.png and b/tests/snapshots/nanos/test_sign_message_kanji/00004.png differ diff --git a/tests/snapshots/nanos/test_sign_message_kanji/00005.png b/tests/snapshots/nanos/test_sign_message_kanji/00005.png new file mode 100644 index 0000000..7181669 Binary files /dev/null and b/tests/snapshots/nanos/test_sign_message_kanji/00005.png differ diff --git a/tests/snapshots/nanos/test_sign_message_kanji/00006.png b/tests/snapshots/nanos/test_sign_message_kanji/00006.png new file mode 100644 index 0000000..2b23e62 Binary files /dev/null and b/tests/snapshots/nanos/test_sign_message_kanji/00006.png differ diff --git a/tests/snapshots/nanos/test_sign_message_kanji/00007.png b/tests/snapshots/nanos/test_sign_message_kanji/00007.png new file mode 100644 index 0000000..66c411c Binary files /dev/null and b/tests/snapshots/nanos/test_sign_message_kanji/00007.png differ diff --git a/tests/snapshots/nanos/test_sign_message_kanji/00008.png b/tests/snapshots/nanos/test_sign_message_kanji/00008.png new file mode 100644 index 0000000..9ab6248 Binary files /dev/null and b/tests/snapshots/nanos/test_sign_message_kanji/00008.png differ diff --git a/tests/snapshots/nanos/test_sign_message_refused/00000.png b/tests/snapshots/nanos/test_sign_message_refused/00000.png new file mode 100644 index 0000000..b102b44 Binary files /dev/null and b/tests/snapshots/nanos/test_sign_message_refused/00000.png differ diff --git a/tests/snapshots/nanos/test_sign_message_refused/00001.png b/tests/snapshots/nanos/test_sign_message_refused/00001.png new file mode 100644 index 0000000..f6d2cca Binary files /dev/null and b/tests/snapshots/nanos/test_sign_message_refused/00001.png differ diff --git a/tests/snapshots/nanos/test_sign_message_refused/00002.png b/tests/snapshots/nanos/test_sign_message_refused/00002.png new file mode 100644 index 0000000..0964ed8 Binary files /dev/null and b/tests/snapshots/nanos/test_sign_message_refused/00002.png differ diff --git a/tests/snapshots/nanos/test_sign_message_refused/00003.png b/tests/snapshots/nanos/test_sign_message_refused/00003.png new file mode 100644 index 0000000..66c411c Binary files /dev/null and b/tests/snapshots/nanos/test_sign_message_refused/00003.png differ diff --git a/tests/snapshots/nanos/test_sign_message_refused/00004.png b/tests/snapshots/nanos/test_sign_message_refused/00004.png new file mode 100644 index 0000000..9c7e704 Binary files /dev/null and b/tests/snapshots/nanos/test_sign_message_refused/00004.png differ diff --git a/tests/snapshots/nanos/test_sign_message_refused/00005.png b/tests/snapshots/nanos/test_sign_message_refused/00005.png new file mode 100644 index 0000000..9ab6248 Binary files /dev/null and b/tests/snapshots/nanos/test_sign_message_refused/00005.png differ diff --git a/tests/snapshots/nanos/test_sign_message_simple/00000.png b/tests/snapshots/nanos/test_sign_message_simple/00000.png index f81b25d..b102b44 100644 Binary files a/tests/snapshots/nanos/test_sign_message_simple/00000.png and b/tests/snapshots/nanos/test_sign_message_simple/00000.png differ diff --git a/tests/snapshots/nanosp/test_sign_message_kanji/00000.png b/tests/snapshots/nanosp/test_sign_message_kanji/00000.png index a95fdd2..0307217 100644 Binary files a/tests/snapshots/nanosp/test_sign_message_kanji/00000.png and b/tests/snapshots/nanosp/test_sign_message_kanji/00000.png differ diff --git a/tests/snapshots/nanosp/test_sign_message_kanji/00002.png b/tests/snapshots/nanosp/test_sign_message_kanji/00002.png index 79c9c7d..aa776ce 100644 Binary files a/tests/snapshots/nanosp/test_sign_message_kanji/00002.png and b/tests/snapshots/nanosp/test_sign_message_kanji/00002.png differ diff --git a/tests/snapshots/nanosp/test_sign_message_kanji/00003.png b/tests/snapshots/nanosp/test_sign_message_kanji/00003.png index 53ae651..02e376a 100644 Binary files a/tests/snapshots/nanosp/test_sign_message_kanji/00003.png and b/tests/snapshots/nanosp/test_sign_message_kanji/00003.png differ diff --git a/tests/snapshots/nanosp/test_sign_message_kanji/00004.png b/tests/snapshots/nanosp/test_sign_message_kanji/00004.png index 63b43ce..53ae651 100644 Binary files a/tests/snapshots/nanosp/test_sign_message_kanji/00004.png and b/tests/snapshots/nanosp/test_sign_message_kanji/00004.png differ diff --git a/tests/snapshots/nanosp/test_sign_message_kanji/00005.png b/tests/snapshots/nanosp/test_sign_message_kanji/00005.png new file mode 100644 index 0000000..63b43ce Binary files /dev/null and b/tests/snapshots/nanosp/test_sign_message_kanji/00005.png differ diff --git a/tests/snapshots/nanosp/test_sign_message_refused/00000.png b/tests/snapshots/nanosp/test_sign_message_refused/00000.png new file mode 100644 index 0000000..0307217 Binary files /dev/null and b/tests/snapshots/nanosp/test_sign_message_refused/00000.png differ diff --git a/tests/snapshots/nanosp/test_sign_message_refused/00001.png b/tests/snapshots/nanosp/test_sign_message_refused/00001.png new file mode 100644 index 0000000..90305d9 Binary files /dev/null and b/tests/snapshots/nanosp/test_sign_message_refused/00001.png differ diff --git a/tests/snapshots/nanosp/test_sign_message_refused/00002.png b/tests/snapshots/nanosp/test_sign_message_refused/00002.png new file mode 100644 index 0000000..6df9c9d Binary files /dev/null and b/tests/snapshots/nanosp/test_sign_message_refused/00002.png differ diff --git a/tests/snapshots/nanosp/test_sign_message_refused/00003.png b/tests/snapshots/nanosp/test_sign_message_refused/00003.png new file mode 100644 index 0000000..53ae651 Binary files /dev/null and b/tests/snapshots/nanosp/test_sign_message_refused/00003.png differ diff --git a/tests/snapshots/nanosp/test_sign_message_refused/00004.png b/tests/snapshots/nanosp/test_sign_message_refused/00004.png new file mode 100644 index 0000000..c922246 Binary files /dev/null and b/tests/snapshots/nanosp/test_sign_message_refused/00004.png differ diff --git a/tests/snapshots/nanosp/test_sign_message_refused/00005.png b/tests/snapshots/nanosp/test_sign_message_refused/00005.png new file mode 100644 index 0000000..63b43ce Binary files /dev/null and b/tests/snapshots/nanosp/test_sign_message_refused/00005.png differ diff --git a/tests/snapshots/nanosp/test_sign_message_simple/00000.png b/tests/snapshots/nanosp/test_sign_message_simple/00000.png index a95fdd2..0307217 100644 Binary files a/tests/snapshots/nanosp/test_sign_message_simple/00000.png and b/tests/snapshots/nanosp/test_sign_message_simple/00000.png differ diff --git a/tests/snapshots/nanox/test_sign_message_kanji/00000.png b/tests/snapshots/nanox/test_sign_message_kanji/00000.png index a95fdd2..0307217 100644 Binary files a/tests/snapshots/nanox/test_sign_message_kanji/00000.png and b/tests/snapshots/nanox/test_sign_message_kanji/00000.png differ diff --git a/tests/snapshots/nanox/test_sign_message_kanji/00002.png b/tests/snapshots/nanox/test_sign_message_kanji/00002.png index 79c9c7d..aa776ce 100644 Binary files a/tests/snapshots/nanox/test_sign_message_kanji/00002.png and b/tests/snapshots/nanox/test_sign_message_kanji/00002.png differ diff --git a/tests/snapshots/nanox/test_sign_message_kanji/00003.png b/tests/snapshots/nanox/test_sign_message_kanji/00003.png index 53ae651..02e376a 100644 Binary files a/tests/snapshots/nanox/test_sign_message_kanji/00003.png and b/tests/snapshots/nanox/test_sign_message_kanji/00003.png differ diff --git a/tests/snapshots/nanox/test_sign_message_kanji/00004.png b/tests/snapshots/nanox/test_sign_message_kanji/00004.png index 8ce05d9..53ae651 100644 Binary files a/tests/snapshots/nanox/test_sign_message_kanji/00004.png and b/tests/snapshots/nanox/test_sign_message_kanji/00004.png differ diff --git a/tests/snapshots/nanox/test_sign_message_kanji/00005.png b/tests/snapshots/nanox/test_sign_message_kanji/00005.png new file mode 100644 index 0000000..8ce05d9 Binary files /dev/null and b/tests/snapshots/nanox/test_sign_message_kanji/00005.png differ diff --git a/tests/snapshots/nanox/test_sign_message_refused/00000.png b/tests/snapshots/nanox/test_sign_message_refused/00000.png new file mode 100644 index 0000000..0307217 Binary files /dev/null and b/tests/snapshots/nanox/test_sign_message_refused/00000.png differ diff --git a/tests/snapshots/nanox/test_sign_message_refused/00001.png b/tests/snapshots/nanox/test_sign_message_refused/00001.png new file mode 100644 index 0000000..90305d9 Binary files /dev/null and b/tests/snapshots/nanox/test_sign_message_refused/00001.png differ diff --git a/tests/snapshots/nanox/test_sign_message_refused/00002.png b/tests/snapshots/nanox/test_sign_message_refused/00002.png new file mode 100644 index 0000000..6df9c9d Binary files /dev/null and b/tests/snapshots/nanox/test_sign_message_refused/00002.png differ diff --git a/tests/snapshots/nanox/test_sign_message_refused/00003.png b/tests/snapshots/nanox/test_sign_message_refused/00003.png new file mode 100644 index 0000000..53ae651 Binary files /dev/null and b/tests/snapshots/nanox/test_sign_message_refused/00003.png differ diff --git a/tests/snapshots/nanox/test_sign_message_refused/00004.png b/tests/snapshots/nanox/test_sign_message_refused/00004.png new file mode 100644 index 0000000..e90cd9d Binary files /dev/null and b/tests/snapshots/nanox/test_sign_message_refused/00004.png differ diff --git a/tests/snapshots/nanox/test_sign_message_refused/00005.png b/tests/snapshots/nanox/test_sign_message_refused/00005.png new file mode 100644 index 0000000..8ce05d9 Binary files /dev/null and b/tests/snapshots/nanox/test_sign_message_refused/00005.png differ diff --git a/tests/snapshots/nanox/test_sign_message_simple/00000.png b/tests/snapshots/nanox/test_sign_message_simple/00000.png index a95fdd2..0307217 100644 Binary files a/tests/snapshots/nanox/test_sign_message_simple/00000.png and b/tests/snapshots/nanox/test_sign_message_simple/00000.png differ diff --git a/tests/snapshots/stax/test_sign_message_kanji/00000.png b/tests/snapshots/stax/test_sign_message_kanji/00000.png index ef8c0f1..e72560f 100644 Binary files a/tests/snapshots/stax/test_sign_message_kanji/00000.png and b/tests/snapshots/stax/test_sign_message_kanji/00000.png differ diff --git a/tests/snapshots/stax/test_sign_message_kanji/00001.png b/tests/snapshots/stax/test_sign_message_kanji/00001.png index ea783c2..104908a 100644 Binary files a/tests/snapshots/stax/test_sign_message_kanji/00001.png and b/tests/snapshots/stax/test_sign_message_kanji/00001.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part0/00000.png b/tests/snapshots/stax/test_sign_message_refused/part0/00000.png new file mode 100644 index 0000000..e72560f Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part0/00000.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part0/00001.png b/tests/snapshots/stax/test_sign_message_refused/part0/00001.png new file mode 100644 index 0000000..56afa4b Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part0/00001.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part0/00002.png b/tests/snapshots/stax/test_sign_message_refused/part0/00002.png new file mode 100644 index 0000000..df803b1 Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part0/00002.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part0/00003.png b/tests/snapshots/stax/test_sign_message_refused/part0/00003.png new file mode 100644 index 0000000..ad6b72e Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part0/00003.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part1/00000.png b/tests/snapshots/stax/test_sign_message_refused/part1/00000.png new file mode 100644 index 0000000..e72560f Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part1/00000.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part1/00001.png b/tests/snapshots/stax/test_sign_message_refused/part1/00001.png new file mode 100644 index 0000000..e13708c Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part1/00001.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part1/00002.png b/tests/snapshots/stax/test_sign_message_refused/part1/00002.png new file mode 100644 index 0000000..56afa4b Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part1/00002.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part1/00003.png b/tests/snapshots/stax/test_sign_message_refused/part1/00003.png new file mode 100644 index 0000000..df803b1 Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part1/00003.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part1/00004.png b/tests/snapshots/stax/test_sign_message_refused/part1/00004.png new file mode 100644 index 0000000..ad6b72e Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part1/00004.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part2/00000.png b/tests/snapshots/stax/test_sign_message_refused/part2/00000.png new file mode 100644 index 0000000..e72560f Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part2/00000.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part2/00001.png b/tests/snapshots/stax/test_sign_message_refused/part2/00001.png new file mode 100644 index 0000000..e13708c Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part2/00001.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part2/00002.png b/tests/snapshots/stax/test_sign_message_refused/part2/00002.png new file mode 100644 index 0000000..c428ef3 Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part2/00002.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part2/00003.png b/tests/snapshots/stax/test_sign_message_refused/part2/00003.png new file mode 100644 index 0000000..56afa4b Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part2/00003.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part2/00004.png b/tests/snapshots/stax/test_sign_message_refused/part2/00004.png new file mode 100644 index 0000000..df803b1 Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part2/00004.png differ diff --git a/tests/snapshots/stax/test_sign_message_refused/part2/00005.png b/tests/snapshots/stax/test_sign_message_refused/part2/00005.png new file mode 100644 index 0000000..ad6b72e Binary files /dev/null and b/tests/snapshots/stax/test_sign_message_refused/part2/00005.png differ diff --git a/tests/snapshots/stax/test_sign_message_simple/00000.png b/tests/snapshots/stax/test_sign_message_simple/00000.png index ef8c0f1..e72560f 100644 Binary files a/tests/snapshots/stax/test_sign_message_simple/00000.png and b/tests/snapshots/stax/test_sign_message_simple/00000.png differ diff --git a/tests/test_sign_personal_message_cmd.py b/tests/test_sign_personal_message_cmd.py index 34b53fd..7812b57 100644 --- a/tests/test_sign_personal_message_cmd.py +++ b/tests/test_sign_personal_message_cmd.py @@ -108,4 +108,37 @@ def test_sign_message_too_long(firmware, backend, navigator, test_name): last_response = client.send_raw_apdu(InsType.SIGN_MESSAGE, p1=P1.P1_INPUTS, p2=P2.P2_LAST, data=message_data.serialize()) assert last_response.status == Errors.SW_MESSAGE_TOO_LONG - \ No newline at end of file + +def test_sign_message_refused(firmware, backend, navigator, test_name): + # Use the app interface instead of raw interface + client = KaspaCommandSender(backend) + + address_type = 1 + address_index = 6 + message = "Hello Kaspa!" + + message_data = PersonalMessage(address_type, address_index, message) + + if firmware.device.startswith("nano"): + with client.sign_message(message_data=message_data): + # Disable raising when trying to unpack an error APDU + backend.raise_policy = RaisePolicy.RAISE_NOTHING + navigator.navigate_until_text_and_compare(NavInsID.RIGHT_CLICK, + [NavInsID.BOTH_CLICK], + "Reject", + ROOT_SCREENSHOT_PATH, + test_name) + + assert client.get_async_response().status == Errors.SW_DENY + else: + for i in range(3): + instructions = [NavInsID.USE_CASE_REVIEW_TAP] * i + instructions += [NavInsID.USE_CASE_REVIEW_REJECT, + NavInsID.USE_CASE_CHOICE_CONFIRM, + NavInsID.USE_CASE_STATUS_DISMISS] + with client.sign_message(message_data=message_data): + backend.raise_policy = RaisePolicy.RAISE_NOTHING + navigator.navigate_and_compare(ROOT_SCREENSHOT_PATH, + test_name + f"/part{i}", + instructions) + assert client.get_async_response().status == Errors.SW_DENY \ No newline at end of file diff --git a/unit-tests/CMakeLists.txt b/unit-tests/CMakeLists.txt index 0842ec4..10fade0 100644 --- a/unit-tests/CMakeLists.txt +++ b/unit-tests/CMakeLists.txt @@ -61,7 +61,7 @@ add_library(read SHARED /opt/ledger-secure-sdk/lib_standard_app/read.c) add_library(sighash SHARED ../src/sighash.c) add_library(personal_message SHARED ../src/personal_message.c) add_library(write SHARED /opt/ledger-secure-sdk/lib_standard_app/write.c) -add_library(format SHARED ../src/common/format.c) +add_library(format_local SHARED ../src/common/format_local.c) add_library(apdu_parser SHARED ../src/apdu/parser.c) add_library(transaction_deserialize ../src/transaction/deserialize.c) add_library(transaction_serialize ../src/transaction/serialize.c) @@ -69,7 +69,7 @@ add_library(transaction_utils ../src/transaction/utils.c) add_library(varint SHARED /opt/ledger-secure-sdk/lib_standard_app/varint.c) target_link_libraries(test_address PUBLIC cmocka gcov address cashaddr) -target_link_libraries(test_format PUBLIC cmocka gcov format) +target_link_libraries(test_format PUBLIC cmocka gcov format_local) target_link_libraries(test_sighash PUBLIC cmocka gcov sighash blake2b write) target_link_libraries(test_personal_message PUBLIC cmocka gcov personal_message blake2b write) target_link_libraries(test_apdu_parser PUBLIC cmocka gcov apdu_parser) diff --git a/unit-tests/test_format.c b/unit-tests/test_format.c index 06a1396..e3c2e28 100644 --- a/unit-tests/test_format.c +++ b/unit-tests/test_format.c @@ -30,109 +30,21 @@ #include -#include "common/format.h" +#include "common/format_local.h" -static void test_format_i64(void **state) { +static void test_format_message_to_sign_simple(void **state) { (void) state; - char temp[22] = {0}; + char message[] = "Hello Kaspa!"; + char dest[12] = {0}; - int64_t value = 0; - assert_true(format_i64(temp, sizeof(temp), value)); - assert_string_equal(temp, "0"); + format_message_to_sign(dest, (int) sizeof(dest), message, (int) sizeof(message)); - value = (int64_t) 9223372036854775807ull; // MAX_INT64 - memset(temp, 0, sizeof(temp)); - assert_true(format_i64(temp, sizeof(temp), value)); - assert_string_equal(temp, "9223372036854775807"); - - // buffer too small - assert_false(format_i64(temp, sizeof(temp) - 5, value)); - - value = (int64_t) -9223372036854775808ull; // MIN_INT64 - memset(temp, 0, sizeof(temp)); - assert_true(format_i64(temp, sizeof(temp), value)); - assert_string_equal(temp, "-9223372036854775808"); -} - -static void test_format_u64(void **state) { - (void) state; - - char temp[21] = {0}; - - uint64_t value = 0; - assert_true(format_u64(temp, sizeof(temp), value)); - assert_string_equal(temp, "0"); - - value = (uint64_t) 18446744073709551615ull; // MAX_UNT64 - memset(temp, 0, sizeof(temp)); - assert_true(format_u64(temp, sizeof(temp), value)); - assert_string_equal(temp, "18446744073709551615"); - - // buffer too small - assert_false(format_u64(temp, sizeof(temp) - 5, value)); -} - -static void test_format_fpu64_trimmed(void **state) { - (void) state; - - char temp[22] = {0}; - - uint64_t amount = 0ull; // satoshi - memset(temp, 0, sizeof(temp)); - assert_true(format_fpu64_trimmed(temp, sizeof(temp), amount, 8)); - assert_string_equal(temp, "0"); // BTC - - amount = 100000000ull; // satoshi - memset(temp, 0, sizeof(temp)); - assert_true(format_fpu64_trimmed(temp, sizeof(temp), amount, 8)); - assert_string_equal(temp, "1"); // BTC - - amount = 24964823ull; // satoshi - memset(temp, 0, sizeof(temp)); - assert_true(format_fpu64_trimmed(temp, sizeof(temp), amount, 8)); - assert_string_equal(temp, "0.24964823"); // BTC - - amount = 100ull; // satoshi - memset(temp, 0, sizeof(temp)); - assert_true(format_fpu64_trimmed(temp, sizeof(temp), amount, 8)); - assert_string_equal(temp, "0.000001"); // BTC - // buffer too small - assert_false(format_fpu64_trimmed(temp, sizeof(temp) - 16, amount, 8)); - - char temp2[50] = {0}; - - amount = 1000000000000000000ull; // wei - assert_true(format_fpu64_trimmed(temp2, sizeof(temp2), amount, 18)); - assert_string_equal(temp2, "1"); // ETH - - amount = 100000000000000000ull; // wei - assert_true(format_fpu64_trimmed(temp2, sizeof(temp2), amount, 18)); - assert_string_equal(temp2, "0.1"); // ETH - - // buffer too small - amount = 1000000000000000001ull; // wei - assert_false(format_fpu64_trimmed(temp2, sizeof(temp2) - 20, amount, 18)); -} - -static void test_format_hex(void **state) { - (void) state; - - uint8_t address[] = {0xde, 0xb, 0x29, 0x56, 0x69, 0xa9, 0xfd, 0x93, 0xd5, 0xf2, - 0x8d, 0x9e, 0xc8, 0x5e, 0x40, 0xf4, 0xcb, 0x69, 0x7b, 0xae}; - char output[2 * sizeof(address) + 1] = {0}; - - assert_int_equal(2 * sizeof(address) + 1, - format_hex(address, sizeof(address), output, sizeof(output))); - assert_string_equal(output, "DE0B295669A9FD93D5F28D9EC85E40F4CB697BAE"); - assert_int_equal(-1, format_hex(address, sizeof(address), output, sizeof(address))); + assert_string_equal(dest, "Hello Kaspa!"); } int main() { - const struct CMUnitTest tests[] = {cmocka_unit_test(test_format_i64), - cmocka_unit_test(test_format_u64), - cmocka_unit_test(test_format_fpu64_trimmed), - cmocka_unit_test(test_format_hex)}; + const struct CMUnitTest tests[] = {cmocka_unit_test(test_format_message_to_sign_simple)}; return cmocka_run_group_tests(tests, NULL, NULL); }