forked from LedgerHQ/app-flow
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add message signing (may not work yet)
No tests for message signing, but everything else works as before.
- Loading branch information
Showing
6 changed files
with
264 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/******************************************************************************* | ||
* (c) 2022 Vacuumlabs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
********************************************************************************/ | ||
#include "message.h" | ||
#include "zxformat.h" | ||
#include "app_mode.h" | ||
#include "hdpath.h" | ||
#include "crypto.h" | ||
#include "buffering.h" | ||
|
||
#define MAX_MESSAGE_SHOW_LENGTH 34 * 5 | ||
#define MAX_MESSAGE_LENGTH 0x7FFF | ||
|
||
struct { | ||
uint8_t hash[CX_SHA256_SIZE]; | ||
uint16_t length; | ||
uint8_t *message; | ||
bool canBeDisplayed; | ||
|
||
} messageData; | ||
|
||
zxerr_t message_parse() { | ||
messageData.message = buffering_get_buffer()->data; | ||
messageData.length = buffering_get_buffer()->pos; | ||
|
||
if (messageData.length > MAX_MESSAGE_LENGTH) { | ||
return zxerr_out_of_bounds; | ||
} | ||
|
||
for (size_t j = 0; j < messageData.length; j++) { | ||
if (messageData.message[j] < 32 || messageData.message[j] > 127) { | ||
return zxerr_out_of_bounds; | ||
} | ||
} | ||
|
||
sha256(messageData.message, messageData.length, messageData.hash); | ||
messageData.canBeDisplayed = false; | ||
|
||
if (messageData.length <= MAX_MESSAGE_SHOW_LENGTH) { | ||
messageData.canBeDisplayed = true; | ||
} | ||
|
||
return zxerr_ok; | ||
} | ||
|
||
zxerr_t message_getNumItems(uint8_t *num_items) { | ||
*num_items = 2 + (!messageData.canBeDisplayed ? 1 : 0) + (app_mode_expert() ? 1 : 0); | ||
return zxerr_ok; | ||
} | ||
|
||
// retrieves a readable output for each field / page | ||
zxerr_t message_getItem(int8_t displayIdx, | ||
char *outKey, | ||
uint16_t outKeyLen, | ||
char *outVal, | ||
uint16_t outValLen, | ||
uint8_t pageIdx, | ||
uint8_t *pageCount) { | ||
switch (displayIdx) { | ||
case 0: | ||
snprintf(outKey, outKeyLen, "Review"); | ||
snprintf(outVal, outValLen, "the message to sign"); | ||
return zxerr_ok; | ||
case 1: | ||
if (messageData.canBeDisplayed) { | ||
snprintf(outKey, outKeyLen, "Message"); | ||
pageStringExt(outVal, | ||
outValLen, | ||
(char *) messageData.message, | ||
messageData.length, | ||
pageIdx, | ||
pageCount); | ||
return zxerr_ok; | ||
} else { | ||
snprintf(outKey, outKeyLen, "Message too long,"); | ||
snprintf(outVal, outValLen, "validate hash on a secure device."); | ||
return zxerr_ok; | ||
} | ||
} | ||
|
||
displayIdx -= 2; | ||
|
||
if (displayIdx == 0 && !messageData.canBeDisplayed) { | ||
snprintf(outKey, outKeyLen, "Message hash"); | ||
pageStringHex(outVal, | ||
outValLen, | ||
(const char *) messageData.hash, | ||
sizeof(messageData.hash), | ||
pageIdx, | ||
pageCount); | ||
return zxerr_ok; | ||
} | ||
|
||
if (!messageData.canBeDisplayed) { | ||
displayIdx -= 1; | ||
} | ||
|
||
if (displayIdx == 0 && app_mode_expert()) { | ||
snprintf(outKey, outKeyLen, "Your Path"); | ||
char buffer[100]; | ||
path_options_to_string(buffer, | ||
sizeof(buffer), | ||
hdPath.data, | ||
HDPATH_LEN_DEFAULT, | ||
cryptoOptions); | ||
pageString(outVal, outValLen, buffer, pageIdx, pageCount); | ||
return zxerr_ok; | ||
} | ||
|
||
return zxerr_no_data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/******************************************************************************* | ||
* (c) 2022 Vacuumlabs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
********************************************************************************/ | ||
#pragma once | ||
|
||
#include "zxerror.h" | ||
|
||
zxerr_t message_parse(); | ||
|
||
zxerr_t message_getNumItems(uint8_t *num_items); | ||
|
||
zxerr_t message_getItem(int8_t displayIdx, | ||
char *outKey, | ||
uint16_t outKeyLen, | ||
char *outVal, | ||
uint16_t outValLen, | ||
uint8_t pageIdx, | ||
uint8_t *pageCount); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters