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

Txn hash fix #15

Merged
merged 5 commits into from
Jun 28, 2024
Merged
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
2 changes: 1 addition & 1 deletion app/Makefile.version
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ APPVERSION_M=0
# This is the minor version
APPVERSION_N=0
# This is the patch version
APPVERSION_P=11
APPVERSION_P=12
8 changes: 3 additions & 5 deletions app/src/common/actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ __Z_INLINE void app_sign() {
tx_getTxnHash(txnHash);
uint8_t publickeyRandomness[KEY_LENGTH] = {0};
tx_getPublicKeyRandomness(publickeyRandomness);
const uint16_t totalDescriptions = tx_SignableDescriptionsLen();
const zxerr_t err =
crypto_sign(totalDescriptions, publickeyRandomness, txnHash, G_io_apdu_buffer, IO_APDU_BUFFER_SIZE - 3);
const zxerr_t err = crypto_sign(publickeyRandomness, txnHash, G_io_apdu_buffer, IO_APDU_BUFFER_SIZE - 3);

if (err != zxerr_ok) {
set_code(G_io_apdu_buffer, 0, APDU_CODE_SIGN_VERIFY_ERROR);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 2);
} else {
set_code(G_io_apdu_buffer, (totalDescriptions * REDJUBJUB_SIGNATURE_LEN), APDU_CODE_OK);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, (totalDescriptions * REDJUBJUB_SIGNATURE_LEN) + 2);
set_code(G_io_apdu_buffer, REDJUBJUB_SIGNATURE_LEN, APDU_CODE_OK);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, REDJUBJUB_SIGNATURE_LEN + 2);
}
}

Expand Down
6 changes: 0 additions & 6 deletions app/src/common/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ void tx_getPublicKeyRandomness(uint8_t randomness[KEY_LENGTH]) {
MEMCPY(randomness, tx_obj.publicKeyRandomness.ptr, KEY_LENGTH);
}

uint16_t tx_SignableDescriptionsLen() {
// Return signable elements quantity
const uint16_t totalDescriptions = tx_obj.spends.elements + tx_obj.mints.elements;
return totalDescriptions;
}

const char *tx_parse() {
MEMZERO(&tx_obj, sizeof(tx_obj));

Expand Down
1 change: 0 additions & 1 deletion app/src/common/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,3 @@ zxerr_t tx_getItem(int8_t displayIdx, char *outKey, uint16_t outKeyLen, char *ou

void tx_getTxnHash(uint8_t txnHash[HASH_LEN]);
void tx_getPublicKeyRandomness(uint8_t randomness[KEY_LENGTH]);
uint16_t tx_SignableDescriptionsLen();
17 changes: 4 additions & 13 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ zxerr_t crypto_generateSaplingKeys(uint8_t *output, uint16_t outputLen, key_kind
return error;
}

zxerr_t crypto_sign(const uint16_t signatures, const uint8_t publickeyRandomness[32], const uint8_t txnHash[32],
uint8_t *output, uint16_t outputLen) {
if (output == NULL || outputLen < (signatures * REDJUBJUB_SIGNATURE_LEN)) {
zxerr_t crypto_sign(const uint8_t publickeyRandomness[32], const uint8_t txnHash[32], uint8_t *output, uint16_t outputLen) {
if (output == NULL || outputLen < REDJUBJUB_SIGNATURE_LEN) {
return zxerr_no_data;
}
MEMZERO(output, outputLen);
Expand All @@ -138,17 +137,9 @@ zxerr_t crypto_sign(const uint16_t signatures, const uint8_t publickeyRandomness
randomizeKey(saplingKeys.ask, publickeyRandomness, randomnizedPrivateKey);

if (error == zxerr_ok) {
// key will be used as private key: first we randomize it
uint8_t rng[RNG_LEN] = {0};
uint8_t *outputPtr = output;
for (uint16_t i = 0; i < signatures; i++) {
cx_rng_no_throw(rng, RNG_LEN);
error = crypto_signRedjubjub(randomnizedPrivateKey, rng, txnHash, outputPtr);
if (error != zxerr_ok) {
break;
}
outputPtr += REDJUBJUB_SIGNATURE_LEN;
}
cx_rng_no_throw(rng, RNG_LEN);
error = crypto_signRedjubjub(randomnizedPrivateKey, rng, txnHash, output);
}

catch_cx_error:
Expand Down
3 changes: 1 addition & 2 deletions app/src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ extern "C" {
extern uint32_t hdPath[HDPATH_LEN_DEFAULT];

zxerr_t crypto_fillKeys(uint8_t *buffer, uint16_t bufferLen, key_kind_e requestedKey, uint16_t *cmdResponseLen);
zxerr_t crypto_sign(const uint16_t signatures, const uint8_t publickeyRandomness[32], const uint8_t txnHash[32],
uint8_t *output, uint16_t outputLen);
zxerr_t crypto_sign(const uint8_t publickeyRandomness[32], const uint8_t txnHash[32], uint8_t *output, uint16_t outputLen);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/crypto_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ parser_error_t transaction_signature_hash(parser_tx_t *txObj, uint8_t output[HAS
// Spends
const uint16_t SPENDLEN = 32 + 192 + 32 + 32 + 4 + 32 + 64;
for (uint64_t i = 0; i < txObj->spends.elements; i++) {
const uint8_t *spend_i = txObj->spends.data.ptr + (SPENDLEN * i) + (32 * (i + 1));
const uint8_t *spend_i = txObj->spends.data.ptr + (SPENDLEN * i) + 32;
// Don't hash neither public_key_randomness(32) nor binding_signature(64)
#if defined(LEDGER_SPECIFIC)
ASSERT_CX_OK(cx_blake2b_update(&ctx, spend_i, SPENDLEN - (32 + 64)));
Expand Down
162 changes: 162 additions & 0 deletions tests/signature.cpp

Large diffs are not rendered by default.

Binary file added tests_zemu/snapshots/sp-blind_sign3/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-blind_sign3/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-blind_sign3/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-blind_sign3/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-blind_sign3/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-blind_sign3/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-blind_sign3/00006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-blind_sign3/00007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-blind_sign3/00008.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/sp-mainmenu/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/sp-mainmenu/00010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/st-blind_sign3/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/st-blind_sign3/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/st-blind_sign3/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/st-blind_sign3/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/st-blind_sign3/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/st-mainmenu/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-blind_sign3/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-blind_sign3/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-blind_sign3/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-blind_sign3/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-blind_sign3/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-blind_sign3/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-blind_sign3/00006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-blind_sign3/00007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-blind_sign3/00008.png
Binary file modified tests_zemu/snapshots/x-mainmenu/00004.png
Binary file modified tests_zemu/snapshots/x-mainmenu/00010.png
3 changes: 3 additions & 0 deletions tests_zemu/tests/common.ts

Large diffs are not rendered by default.

31 changes: 30 additions & 1 deletion tests_zemu/tests/standard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
******************************************************************************* */

import Zemu, { ButtonKind, zondaxMainmenuNavigation } from '@zondax/zemu'
import { PATH, defaultOptions, expectedKeys, models, spend_1_output_1, spend_1_output_4_mint_1_burn_1 } from './common'
import { PATH, defaultOptions, expectedKeys, models, spend_1_output_1, spend_1_output_4_mint_1_burn_1, spend_2_output_6_mint_2_burn_1 } from './common'
import IronfishApp, { IronfishKeys, ResponseAddress, ResponseProofGenKey, ResponseViewKey } from '@zondax/ledger-ironfish'

jest.setTimeout(45000)
Expand Down Expand Up @@ -224,4 +224,33 @@ describe('Standard', function () {
await sim.close()
}
})

test.concurrent.each(models)('blind-signing3', async function (m) {
const sim = new Zemu(m.path)
try {
await sim.start({ ...defaultOptions, model: m.name })
const app = new IronfishApp(sim.getTransport())

const txBlob = Buffer.from(spend_2_output_6_mint_2_burn_1, 'hex')
const responsePublicAddress = await app.retrieveKeys(PATH, IronfishKeys.PublicAddress, false)
console.log(responsePublicAddress)

// do not wait here.. we need to navigate
const signatureRequest = app.sign(PATH, txBlob)

// Wait until we are not in the main menu
await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot())
await sim.compareSnapshotsAndApprove('.', `${m.prefix.toLowerCase()}-blind_sign3`)

const signatureResponse = await signatureRequest
console.log(signatureResponse)

console.log(signatureResponse.signatures?.length)

expect(signatureResponse.returnCode).toEqual(0x9000)
expect(signatureResponse.errorMessage).toEqual('No errors')
} finally {
await sim.close()
}
})
})
Loading