Skip to content
Open
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
21 changes: 18 additions & 3 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,24 @@ zxerr_t crypto_sign_secp256k1(
}

// Hash the message
uint8_t messageDigest[CX_KECCAK_256_SIZE] = {0};
CHECK_CX_OK(cx_keccak_256_hash(message, messageLen, messageDigest));
uint8_t messageDigest[BLAKE2B_DIGEST_SIZE] = {0};
zxerr_t error = zxerr_unknown;

// When the payload is bigger than MAX_SIGN_SIZE, it needs to be hashed with blake2b_256 before hashing with keccak_256.
// https://github.com/paritytech/polkadot-sdk/blob/1a512570552119a49a8ecb2abfb7021954c4422d/substrate/primitives/runtime/src/generic/unchecked_extrinsic.rs#L567
if (messageLen > MAX_SIGN_SIZE) {
uint8_t intermediate_digest[BLAKE2B_DIGEST_SIZE];
// Hash it with blake2b
cx_blake2b_t ctx;
CATCH_CXERROR(cx_blake2b_init_no_throw(&ctx, 256));
CATCH_CXERROR(cx_hash_no_throw(&ctx.header, CX_LAST, message, messageLen, intermediate_digest, BLAKE2B_DIGEST_SIZE));

// Then hash with keccak_256
CHECK_CX_OK(cx_keccak_256_hash(intermediate_digest, BLAKE2B_DIGEST_SIZE, messageDigest));
} else {
// Mandatory, hash the message with keccak_256
CHECK_CX_OK(cx_keccak_256_hash(message, messageLen, messageDigest));
}

cx_ecfp_private_key_t cx_privateKey;
uint8_t privateKeyData[SECP256K1_PK_LEN_UNCOMPRESSED - 1] = {0};
Expand All @@ -164,7 +180,6 @@ zxerr_t crypto_sign_secp256k1(
*sigSize = 0;

signature_t *const signature = (signature_t *)output;
zxerr_t error = zxerr_unknown;

CATCH_CXERROR(os_derive_bip32_with_seed_no_throw(HDW_NORMAL, CX_CURVE_SECP256K1, hdPath, HDPATH_LEN_DEFAULT,
privateKeyData, NULL, NULL, 0));
Expand Down