Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cardano): Message signing #88

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions common/protob/messages-cardano.proto
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,49 @@ message CardanoTxBodyHash {
*/
message CardanoSignTxFinished {
}

/**
* Request: Ask device to sign a message containing arbitrary data
* @start
* @next CardanoMessageItemAck
*/
message CardanoSignMessageInit {
optional uint32 protocol_magic = 1; // network's protocol magic
optional uint32 network_id = 2; // network id - mainnet or testnet
repeated uint32 signing_path = 3; // BIP-32-style path to derive the signing key from master node
required uint32 payload_size = 4; // size of the payload to be signed
required bool hash_payload = 5; // whether to hash the payload before signing
required bool prefer_hex_display = 6; // whether to decode payload as hex even if valid ascii
optional CardanoAddressParametersType address_parameters = 7;
required CardanoDerivationType derivation_type = 8;
}

/**
* @next CardanoMessagePayloadChunk
* @next CardanoMessageItemHostAck
*/
message CardanoMessageItemAck {
}

/**
* @next CardanoMessageItemAck
*/
message CardanoMessagePayloadChunk {
required bytes data = 1; // expected maximum chunk size is 1024 bytes
}

/**
* @next CardanoSignMessageFinished
*/
message CardanoMessageItemHostAck {
}

/**
* Response: Contains signature for message and address used in signed headers
* @end
*/
message CardanoSignMessageFinished {
required bytes signature = 1; // signature for structure containing payload and protected headers
required bytes address = 2; // value of the address field in protected headers
required bytes pub_key = 3; // public key given by signing path
}
5 changes: 5 additions & 0 deletions common/protob/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ enum MessageType {
MessageType_CardanoTxInlineDatumChunk = 335 [(wire_in) = true];
MessageType_CardanoTxReferenceScriptChunk = 336 [(wire_in) = true];
MessageType_CardanoTxReferenceInput = 337 [(wire_in) = true];
MessageType_CardanoSignMessageInit = 338 [(wire_in) = true];
MessageType_CardanoMessagePayloadChunk = 339 [(wire_in) = true];
MessageType_CardanoMessageItemAck = 340 [(wire_out) = true];
MessageType_CardanoMessageItemHostAck = 341 [(wire_in) = true];
MessageType_CardanoSignMessageFinished = 342 [(wire_out) = true];

// Ripple
MessageType_RippleGetAddress = 400 [(wire_in) = true];
Expand Down
39 changes: 39 additions & 0 deletions common/tests/fixtures/cardano/sign_message.failed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"setup": {
"mnemonic": "all all all all all all all all all all all all",
"passphrase": ""
},
"tests": [
{
"description": "Missing network id and protocol magic",
"parameters": {
"signing_path": "m/1852'/1815'/4'/0/0",
"address_parameters": {
"addressType": 0,
"path": "m/1852'/1815'/0'/0/0",
"stakingPath": "m/1852'/1815'/0'/2/0"
},
"payload": "",
"hash_payload": false,
"prefer_hex_display": true
},
"result": {
"error_message": "Must specify network_id and protocol_magic if using address_parameters"
}
},
{
"description": "Unhashed payload too long",
"parameters": {
"signing_path": "m/1852'/1815'/4'/0/0",
"network_id": 1,
"protocol_magic": 764824073,
"payload": "566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765",
"hash_payload": false,
"prefer_hex_display": true
},
"result": {
"error_message": "Payload too long to sign without hashing"
}
}
]
}
159 changes: 159 additions & 0 deletions common/tests/fixtures/cardano/sign_message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
{
"setup": {
"mnemonic": "all all all all all all all all all all all all",
"passphrase": ""
},
"tests": [
{
"description": "Sign short non-ASCII payload",
"parameters": {
"signing_path": "m/1852'/1815'/4'/0/0",
"payload": "ff00",
"hash_payload": false,
"prefer_hex_display": true
},
"result": {
"signature": "5ad6ba670e65353b2c1ad4053a1ed4a9348a73fe965ffa0afafa24bad06e3eb3e325d49029604c09bf665c3c43a750ec81a43b1f8b746b07e999b913b980d006",
"address": "d9553a4de9c7ad8532abdb1d0a7f425b8007d25c9f1edcf0b5f5c3ba",
"pub_key": "67e3d14e1e0db2a6ff68832994451f63d3e92a4ee85c3681d72634ccfe889f2c"
}
},
{
"description": "Sign short non-ASCII payload with address parameters",
"parameters": {
"signing_path": "m/1852'/1815'/4'/0/0",
"network_id": 1,
"protocol_magic": 764824073,
"payload": "ff00",
"hash_payload": false,
"address_parameters": {
"addressType": 0,
"path": "m/1852'/1815'/0'/0/0",
"stakingPath": "m/1852'/1815'/0'/2/0"
},
"prefer_hex_display": true
},
"result": {
"signature": "9efaff0b74c0beb2cadd727d8bafe13b31107235c5fc46c6c33e596e024d391c9fbe37072e43965add6ee0a4788562382031486b74fd59d636aa1ca3b1ddfe06",
"address": "0180f9e2c88e6c817008f3a812ed889b4a4da8e0bd103f86e7335422aa122a946b9ad3d2ddf029d3a828f0468aece76895f15c9efbd69b4277",
"pub_key": "67e3d14e1e0db2a6ff68832994451f63d3e92a4ee85c3681d72634ccfe889f2c"
}
},
{
"description": "Sign short non-ASCII payload hash",
"parameters": {
"signing_path": "m/1852'/1815'/4'/0/0",
"payload": "ff00",
"hash_payload": true,
"prefer_hex_display": true
},
"result": {
"signature": "2c325e542fa78d76d916e50f50b85e770354a44e071f08fdb8ec5d0bcbf844cf70dcf5c87b7a51cd7f0269a59eec8d438c3c27eb42b971e7ccb7f864714c4b06",
"address": "d9553a4de9c7ad8532abdb1d0a7f425b8007d25c9f1edcf0b5f5c3ba",
"pub_key": "67e3d14e1e0db2a6ff68832994451f63d3e92a4ee85c3681d72634ccfe889f2c"
}
},
{
"description": "Sign short ASCII payload",
"parameters": {
"signing_path": "m/1852'/1815'/4'/0/0",
"payload": "54657374",
"hash_payload": false,
"prefer_hex_display": false
},
"result": {
"signature": "2201b8e7fa9ea919935e06ecc3e845433855066acaaf61cb8e624a2eb7139b73a9d126e7ee04548fff06ac933bd5419fc78c5aebee9b536cbee1481b52ec3e03",
"address": "d9553a4de9c7ad8532abdb1d0a7f425b8007d25c9f1edcf0b5f5c3ba",
"pub_key": "67e3d14e1e0db2a6ff68832994451f63d3e92a4ee85c3681d72634ccfe889f2c"
}
},
{
"description": "Sign short ASCII payload rendered as hex",
"parameters": {
"signing_path": "m/1852'/1815'/4'/0/0",
"payload": "54657374",
"hash_payload": false,
"prefer_hex_display": true
},
"result": {
"signature": "2201b8e7fa9ea919935e06ecc3e845433855066acaaf61cb8e624a2eb7139b73a9d126e7ee04548fff06ac933bd5419fc78c5aebee9b536cbee1481b52ec3e03",
"address": "d9553a4de9c7ad8532abdb1d0a7f425b8007d25c9f1edcf0b5f5c3ba",
"pub_key": "67e3d14e1e0db2a6ff68832994451f63d3e92a4ee85c3681d72634ccfe889f2c"
}
},
{
"description": "Sign empty payload",
"parameters": {
"signing_path": "m/1852'/1815'/4'/0/0",
"payload": "",
"hash_payload": false,
"prefer_hex_display": true
},
"result": {
"signature": "b09177a06cb2deba7ada89fec96fc4380e746f67c6b16a9ef9ae6b7cbbe941fdad8a8a573b809cd88db296b91b476c436033a29d86a63959e270047e47cd5d0d",
"address": "d9553a4de9c7ad8532abdb1d0a7f425b8007d25c9f1edcf0b5f5c3ba",
"pub_key": "67e3d14e1e0db2a6ff68832994451f63d3e92a4ee85c3681d72634ccfe889f2c"
}
},
{
"description": "Sign empty payload hash",
"parameters": {
"signing_path": "m/1852'/1815'/4'/0/0",
"payload": "",
"hash_payload": true,
"prefer_hex_display": true
},
"result": {
"signature": "a2503039a8ec620e05d9e4345339d61cd11480fbfcc75ea1a10789751a7c5f46ba06786eb1719da62db76c20313ad3445839b8117abac206cc4bd63ea623fc07",
"address": "d9553a4de9c7ad8532abdb1d0a7f425b8007d25c9f1edcf0b5f5c3ba",
"pub_key": "67e3d14e1e0db2a6ff68832994451f63d3e92a4ee85c3681d72634ccfe889f2c"
}
},
{
"description": "Sign long ASCII payload hash",
"parameters": {
"signing_path": "m/1852'/1815'/4'/0/0",
"payload": "566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765566572794c6f6e674d657373616765",
"hash_payload": true,
"prefer_hex_display": false
},
"result": {
"signature": "92331e75bb4c3208317ac422f2fc9d8b9b09d3f81cc487edaa7028d262553e5691532fb166a40e45eb2e4addd4280ff7e07bd4249e964d969e91555317b05f08",
"address": "d9553a4de9c7ad8532abdb1d0a7f425b8007d25c9f1edcf0b5f5c3ba",
"pub_key": "67e3d14e1e0db2a6ff68832994451f63d3e92a4ee85c3681d72634ccfe889f2c"
}
},
{
"description": "Non-ASCII payload falls back to hex",
"parameters": {
"signing_path": "m/1852'/1815'/4'/0/0",
"network_id": 1,
"protocol_magic": 764824073,
"payload": "ff",
"hash_payload": false,
"prefer_hex_display": false
},
"result": {
"signature": "eb7e48bb2de912b24ea4e295cfbf338e95e2a2d9c5eb41d72ddd8ebf96ca6128696e91e65e20a755821781b8ea0b4aa7ffcfa6ab0c7da2c67ecaf33fcd596509",
"address": "d9553a4de9c7ad8532abdb1d0a7f425b8007d25c9f1edcf0b5f5c3ba",
"pub_key": "67e3d14e1e0db2a6ff68832994451f63d3e92a4ee85c3681d72634ccfe889f2c"
}
},
{
"description": "Ambiguous ASCII payload falls back to hex",
"parameters": {
"signing_path": "m/1852'/1815'/4'/0/0",
"network_id": 1,
"protocol_magic": 764824073,
"payload": "20",
"hash_payload": false,
"prefer_hex_display": false
},
"result": {
"signature": "1cd79c511e58e0d00bc402c24631a982c0f596e13cad38447d4f31213d085b23b9990b45023450f5ab4d47987993af662702b07bfc8195feb80b2c9e1c1dc10f",
"address": "d9553a4de9c7ad8532abdb1d0a7f425b8007d25c9f1edcf0b5f5c3ba",
"pub_key": "67e3d14e1e0db2a6ff68832994451f63d3e92a4ee85c3681d72634ccfe889f2c"
}
}
]
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a long ascii message (so that we have a UI test for that).

1 change: 1 addition & 0 deletions core/.changelog.d/3509.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cardano: Add support for signing arbitrary messages
7 changes: 7 additions & 0 deletions core/embed/rust/librust_qstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ static void _librust_qstrs(void) {
MP_QSTR_cardano__collateral_output_contains_tokens;
MP_QSTR_cardano__collateral_return;
MP_QSTR_cardano__confirm;
MP_QSTR_cardano__confirm_message;
MP_QSTR_cardano__confirm_signing_stake_pool;
MP_QSTR_cardano__confirm_transaction;
MP_QSTR_cardano__confirming_a_multisig_transaction;
Expand All @@ -695,6 +696,7 @@ static void _librust_qstrs(void) {
MP_QSTR_cardano__delegating_to_key_hash;
MP_QSTR_cardano__delegating_to_script;
MP_QSTR_cardano__deposit;
MP_QSTR_cardano__empty_message;
MP_QSTR_cardano__for_account_and_index_template;
MP_QSTR_cardano__for_account_template;
MP_QSTR_cardano__for_key_hash;
Expand All @@ -707,6 +709,9 @@ static void _librust_qstrs(void) {
MP_QSTR_cardano__intro_text_registration_payment;
MP_QSTR_cardano__key_hash;
MP_QSTR_cardano__margin;
MP_QSTR_cardano__message_hash;
MP_QSTR_cardano__message_hex;
MP_QSTR_cardano__message_text;
MP_QSTR_cardano__multisig_path;
MP_QSTR_cardano__nested_scripts_template;
MP_QSTR_cardano__network;
Expand Down Expand Up @@ -741,6 +746,8 @@ static void _librust_qstrs(void) {
MP_QSTR_cardano__script_reward;
MP_QSTR_cardano__sending;
MP_QSTR_cardano__show_simple;
MP_QSTR_cardano__sign_message_hash_path_template;
MP_QSTR_cardano__sign_message_path_template;
MP_QSTR_cardano__sign_tx_path_template;
MP_QSTR_cardano__stake_delegation;
MP_QSTR_cardano__stake_deregistration;
Expand Down
Loading
Loading