Skip to content

Commit d38166b

Browse files
authored
Merge pull request #49 from coderofstuff/message-signing
Message signing
2 parents 6e1516d + e95a319 commit d38166b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+852
-164
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)"
3535

3636
ifeq ($(TARGET_NAME),TARGET_NANOS)
3737
DEFINES += MAX_INPUT_COUNT=15
38+
DEFINES += MAX_MESSAGE_LEN=120
3839
else ifeq ($(TARGET_NAME),TARGET_STAX)
3940
DEFINES += MAX_INPUT_COUNT=128
41+
DEFINES += MAX_MESSAGE_LEN=200
4042
else
4143
DEFINES += MAX_INPUT_COUNT=128
44+
DEFINES += MAX_MESSAGE_LEN=200
4245
endif
4346

4447
# Application source files
@@ -76,7 +79,7 @@ VARIANT_PARAM = COIN
7679
VARIANT_VALUES = KAS
7780

7881
# Enabling DEBUG flag will enable PRINTF and disable optimizations
79-
#DEBUG = 1
82+
DEBUG = 1
8083

8184
########################################
8285
# Application custom permissions #

doc/COMMANDS.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,36 @@ Transactions signed with ECDSA are currently not supported.
121121

122122
\* While `has_more` is non-zero, you can ask for the next signature by sending another APDU back
123123

124+
## SIGN_MESSAGE
125+
126+
### Command
127+
128+
| CLA | INS | P1 | P2 | Lc | CData |
129+
| --- | --- | --- | --- | --- | --- |
130+
| 0xE0 | 0x07 | 0x00 | 0x00 | var | `address_type (1)` \|\| `address_index (4)` \|\|<br>`message_len (1 bytes)` \|\| `message (var bytes)` |
131+
132+
| CData Part | Description |
133+
| --- | --- |
134+
| `address_type` | Either `00` for Receive Address or `01` for Change Address |
135+
| `address_index` | Any value from `00000000` to `11111111` |
136+
| `message_len` | How long the message is. Must be a value from `1` to `128`, inclusive |
137+
| `message` | The message to sign |
138+
139+
### Response
140+
141+
| Length <br/>(bytes) | SW | RData |
142+
| --- | --- | --- |
143+
| var | 0x9000 | See Response Breakdown |
144+
145+
#### Response Breakdown
146+
147+
| Data | Description |
148+
| --- | --- |
149+
| `len(sig)` | The length of the signature. Always 64 bytes with Schnorr |
150+
| `sig` | The Schnorr signature |
151+
| `len(message_hash)` | The length of the message hash. Always 32 bytes |
152+
| `message_hash` | The hash that was signed. |
153+
124154
## Status Words
125155

126156
| SW | SW name | Description |
@@ -143,4 +173,5 @@ Transactions signed with ECDSA are currently not supported.
143173
| 0xB00A | `SW_WRONG_BIP32_COIN_TYPE` | `Coin Type` must be `111111'` |
144174
| 0xB00B | `SW_WRONG_BIP32_TYPE` | `Type` passed is not valid. Must be either `0` for `Receive` or `1` for `Change`|
145175
| 0xB00C | `SW_WRONG_BIP32_PATH_LEN` | Path length must be `5` |
176+
| 0xB00D | `SW_MESSAGE_TOO_LONG` | Message len greater than max |
146177
| 0x9000 | `OK` | Success |

fuzzing/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ endif()
2222

2323
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2424

25-
add_compile_definitions(MAX_INPUT_COUNT=15 USB_SEGMENT_SIZE=64)
25+
add_compile_definitions(MAX_INPUT_COUNT=15 MAX_MESSAGE_LEN=200 USB_SEGMENT_SIZE=64)
2626

2727
include(extra/TxParser.cmake)
2828

src/apdu/dispatcher.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "../handler/get_app_name.h"
3636
#include "../handler/get_public_key.h"
3737
#include "../handler/sign_tx.h"
38+
#include "../handler/sign_msg.h"
3839

3940
#ifdef HAVE_DEBUG_APDU
4041
#include "../handler/debug.h"
@@ -92,6 +93,13 @@ int apdu_dispatcher(const command_t *cmd) {
9293
buf.offset = 0;
9394

9495
return handler_sign_tx(&buf, cmd->p1, (bool) (cmd->p2 & P2_MORE));
96+
case SIGN_MESSAGE:
97+
98+
buf.ptr = cmd->data;
99+
buf.size = cmd->lc;
100+
buf.offset = 0;
101+
102+
return handler_sign_msg(&buf);
95103
#ifdef HAVE_DEBUG_APDU
96104
case DEBUG_APDU:
97105
return handler_debug(cmd->p1);

src/common/bip32.c

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/constants.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@
4343
*/
4444
#define MAX_APPNAME_LEN 64
4545

46-
/**
47-
* Maximum transaction length (bytes).
48-
*/
49-
#define MAX_TRANSACTION_LEN 128
50-
5146
/**
5247
* Maximum signature length (bytes).
5348
* Schnorr signatures only have 64 bytes
@@ -70,4 +65,6 @@
7065
*/
7166
#define SIGNING_KEY "TransactionSigningHash"
7267

68+
#define MESSAGE_SIGNING_KEY "PersonalMessageSigningHash"
69+
7370
#define MAX_OUTPUT_COUNT 2

src/crypto.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "globals.h"
3232

3333
#include "sighash.h"
34+
#include "personal_message.h"
3435

3536
bool crypto_validate_public_key(const uint32_t *bip32_path,
3637
uint8_t bip32_path_len,
@@ -54,7 +55,7 @@ bool crypto_validate_public_key(const uint32_t *bip32_path,
5455
return memcmp(raw_pubkey + 1, compressed_public_key, 32) == 0;
5556
}
5657

57-
int crypto_sign_message(void) {
58+
int crypto_sign_transaction(void) {
5859
cx_ecfp_private_key_t private_key = {0};
5960
cx_ecfp_public_key_t public_key = {0};
6061
uint8_t chain_code[32] = {0};
@@ -117,3 +118,37 @@ int crypto_sign_message(void) {
117118

118119
return error;
119120
}
121+
122+
int crypto_sign_personal_message(void) {
123+
hash_personal_message(G_context.msg_info.message,
124+
G_context.msg_info.message_len,
125+
G_context.msg_info.message_hash);
126+
127+
cx_ecfp_private_key_t private_key = {0};
128+
uint8_t chain_code[32] = {0};
129+
130+
int error = bip32_derive_init_privkey_256(CX_CURVE_256K1,
131+
G_context.bip32_path,
132+
G_context.bip32_path_len,
133+
&private_key,
134+
chain_code);
135+
136+
BEGIN_TRY {
137+
TRY {
138+
size_t sig_len = sizeof(G_context.tx_info.signature);
139+
error = cx_ecschnorr_sign_no_throw(&private_key,
140+
CX_ECSCHNORR_BIP0340 | CX_RND_TRNG,
141+
CX_SHA256,
142+
G_context.msg_info.message_hash,
143+
sizeof(G_context.msg_info.message_hash),
144+
G_context.msg_info.signature,
145+
&sig_len);
146+
}
147+
FINALLY {
148+
explicit_bzero(&private_key, sizeof(private_key));
149+
}
150+
}
151+
END_TRY;
152+
153+
return error;
154+
}

src/crypto.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* @return 0 on success, error number otherwise.
3838
*
3939
*/
40-
int crypto_sign_message(void);
40+
int crypto_sign_transaction(void);
4141

4242
/**
4343
* Checks if the compressed public key matches the
@@ -56,3 +56,14 @@ int crypto_sign_message(void);
5656
bool crypto_validate_public_key(const uint32_t *bip32_path,
5757
uint8_t bip32_path_len,
5858
uint8_t compressed_public_key[static 32]);
59+
60+
/**
61+
* Sign personal message hash in global context.
62+
*
63+
* @see G_context.bip32_path,
64+
* G_context.msg_info.signature.
65+
*
66+
* @return 0 on success, error number otherwise.
67+
*
68+
*/
69+
int crypto_sign_personal_message(void);

src/handler/sign_msg.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*****************************************************************************
2+
* MIT License
3+
*
4+
* Copyright (c) 2023 coderofstuff
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*****************************************************************************/
24+
#include <stdint.h> // uint*_t
25+
#include <string.h> // memset, explicit_bzero
26+
27+
#include "types.h"
28+
#include "buffer.h"
29+
#include "./globals.h"
30+
#include "./sign_msg.h"
31+
#include "../sw.h"
32+
#include "../ui/display.h"
33+
#include "../helper/send_response.h"
34+
35+
/**
36+
* Handler for SIGN_MESSAGE command. If successfully parse BIP32 path
37+
* and message, sign the message and send APDU response.
38+
*
39+
* @see G_context.bip32_path, G_context.msg_info
40+
*
41+
* @param[in,out] cdata
42+
* Command data with BIP32 path and raw message.
43+
*
44+
* @return zero or positive integer if success, negative integer otherwise.
45+
*
46+
*/
47+
int handler_sign_msg(buffer_t *cdata) {
48+
explicit_bzero(&G_context, sizeof(G_context));
49+
G_context.req_type = CONFIRM_MESSAGE;
50+
G_context.state = STATE_NONE;
51+
52+
if (!buffer_read_u8(cdata, &G_context.msg_info.address_type)) {
53+
return io_send_sw(SW_WRONG_DATA_LENGTH);
54+
}
55+
56+
if (!buffer_read_u32(cdata, &G_context.msg_info.address_index, BE)) {
57+
return io_send_sw(SW_WRONG_DATA_LENGTH);
58+
}
59+
60+
uint8_t message_len = 0;
61+
if (!buffer_read_u8(cdata, &message_len)) {
62+
return io_send_sw(SW_WRONG_DATA_LENGTH);
63+
}
64+
65+
if (message_len > MAX_MESSAGE_LEN) {
66+
return io_send_sw(SW_MESSAGE_TOO_LONG);
67+
}
68+
69+
G_context.msg_info.message_len = (size_t) message_len;
70+
71+
if (!buffer_can_read(cdata, G_context.msg_info.message_len)) {
72+
return io_send_sw(SW_WRONG_DATA_LENGTH);
73+
}
74+
75+
memcpy(G_context.msg_info.message, cdata->ptr + cdata->offset, G_context.msg_info.message_len);
76+
77+
if (!buffer_seek_cur(cdata, G_context.msg_info.message_len)) {
78+
return io_send_sw(SW_WRONG_DATA_LENGTH);
79+
}
80+
81+
G_context.bip32_path[0] = 0x8000002C;
82+
G_context.bip32_path[1] = 0x8001b207;
83+
G_context.bip32_path[2] = 0x80000000;
84+
G_context.bip32_path[3] = (uint32_t)(G_context.msg_info.address_type);
85+
G_context.bip32_path[4] = G_context.msg_info.address_index;
86+
87+
G_context.bip32_path_len = 5;
88+
89+
return ui_display_message();
90+
}

0 commit comments

Comments
 (0)