Skip to content

Commit

Permalink
Merge pull request #249 from LedgerHQ/missing-return-checks
Browse files Browse the repository at this point in the history
Add return value checks for hash functions from the sdk
  • Loading branch information
bigspider authored Apr 3, 2024
2 parents 031719b + f4d38ed commit 1ddabaf
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,18 @@ int bip32_CKDpub(const serialized_extended_pubkey_t *parent,
}

void crypto_ripemd160(const uint8_t *in, uint16_t inlen, uint8_t out[static 20]) {
cx_ripemd160_hash(in, inlen, out);
int res = cx_ripemd160_hash(in, inlen, out);
LEDGER_ASSERT(res == CX_OK, "Unexpected error in ripemd160 computation. Returned: %d", res);
}

void crypto_hash160(const uint8_t *in, uint16_t inlen, uint8_t out[static 20]) {
PRINT_STACK_POINTER();

uint8_t buffer[32];
cx_hash_sha256(in, inlen, buffer, 32);
int res = cx_hash_sha256(in, inlen, buffer, 32);
LEDGER_ASSERT(res == CX_SHA256_SIZE,
"Unexpected error in sha256 computation. Returned: %d",
res);
crypto_ripemd160(buffer, 32, out);
}

Expand Down Expand Up @@ -216,8 +220,15 @@ int crypto_get_uncompressed_pubkey(const uint8_t compressed_key[static 33],
// TODO: missing unit tests
void crypto_get_checksum(const uint8_t *in, uint16_t in_len, uint8_t out[static 4]) {
uint8_t buffer[32];
cx_hash_sha256(in, in_len, buffer, 32);
cx_hash_sha256(buffer, 32, buffer, 32);
size_t res;
res = cx_hash_sha256(in, in_len, buffer, 32);
LEDGER_ASSERT(res == CX_SHA256_SIZE,
"Unexpected error in sha256 computation. Returned: %d",
res);
res = cx_hash_sha256(buffer, 32, buffer, 32);
LEDGER_ASSERT(res == CX_SHA256_SIZE,
"Unexpected error in sha256 computation. Returned: %d",
res);
memmove(out, buffer, 4);
}

Expand Down Expand Up @@ -401,16 +412,21 @@ int crypto_ecdsa_sign_sha256_hash_with_key(const uint32_t bip32_path[],
}

void crypto_tr_tagged_hash_init(cx_sha256_t *hash_context, const uint8_t *tag, uint16_t tag_len) {
int res;
// we recycle the input to save memory (will reinit later)
cx_sha256_init(hash_context);

uint8_t hashtag[32];
crypto_hash_update(&hash_context->header, tag, tag_len);
crypto_hash_digest(&hash_context->header, hashtag, sizeof(hashtag));
res = crypto_hash_update(&hash_context->header, tag, tag_len);
LEDGER_ASSERT(res == CX_OK, "Unexpected error in sha256 computation. Returned: %d", res);
res = crypto_hash_digest(&hash_context->header, hashtag, sizeof(hashtag));
LEDGER_ASSERT(res == CX_OK, "Unexpected error in sha256 computation. Returned: %d", res);

cx_sha256_init(hash_context);
crypto_hash_update(&hash_context->header, hashtag, sizeof(hashtag));
crypto_hash_update(&hash_context->header, hashtag, sizeof(hashtag));
res = crypto_hash_update(&hash_context->header, hashtag, sizeof(hashtag));
LEDGER_ASSERT(res == CX_OK, "Unexpected error in sha256 computation. Returned: %d", res);
res = crypto_hash_update(&hash_context->header, hashtag, sizeof(hashtag));
LEDGER_ASSERT(res == CX_OK, "Unexpected error in sha256 computation. Returned: %d", res);
}

void crypto_tr_tapleaf_hash_init(cx_sha256_t *hash_context) {
Expand Down

0 comments on commit 1ddabaf

Please sign in to comment.