@@ -85,6 +85,9 @@ parser_error_t compute_tree_size(uint32_t num_right_siblings, uint32_t index_of_
8585
8686 while (remaining_right_siblings > 0 ) {
8787 if ((index_of_final_node & mask ) == 0 ) {
88+ if (index_of_final_node > UINT32_MAX - mask ) {
89+ return parser_value_out_of_range ;
90+ }
8891 index_of_final_node |= mask ;
8992 remaining_right_siblings -- ;
9093 }
@@ -128,6 +131,10 @@ static parser_error_t hash_leaf(proof_t *proof) {
128131// Declaring array sizes as static will trigger a warning if the user pass an array smaller than the size
129132static parser_error_t merge_branches (const uint8_t left [CX_SHA256_SIZE ], const uint8_t right [CX_SHA256_SIZE ],
130133 uint8_t output [CX_SHA256_SIZE ]) {
134+ CHECK_INPUT (left );
135+ CHECK_INPUT (right );
136+ CHECK_INPUT (output );
137+
131138 bytes_t buffer_left = {0 };
132139 buffer_left .ptr = left ;
133140 buffer_left .len = CX_SHA256_SIZE ;
@@ -183,7 +190,12 @@ static parser_error_t check_range_proof_inner(proof_t *proof, uint32_t start_ind
183190 if (proof -> lemmas_ctx .last_index < 0 ) {
184191 return parser_value_out_of_range ;
185192 }
186- right = proof -> lemmas_ctx .lemmas .data .buffer .ptr + CX_SHA256_SIZE * proof -> lemmas_ctx .last_index ;
193+ uint32_t offset = CX_SHA256_SIZE * proof -> lemmas_ctx .last_index ;
194+ if (offset >= proof -> lemmas_ctx .lemmas .data .buffer .len ) {
195+ return parser_value_out_of_range ;
196+ }
197+
198+ right = proof -> lemmas_ctx .lemmas .data .buffer .ptr + offset ;
187199 proof -> lemmas_ctx .last_index -- ;
188200
189201 buffer_right .ptr = right ;
@@ -207,10 +219,14 @@ static parser_error_t check_range_proof_inner(proof_t *proof, uint32_t start_ind
207219 left = proof -> hash ;
208220 }
209221 } else {
210- left = proof -> lemmas_ctx .lemmas .data .buffer .ptr + CX_SHA256_SIZE * proof -> lemmas_ctx .last_index ;
211222 if (proof -> lemmas_ctx .last_index < 0 ) {
212223 return parser_value_out_of_range ;
213224 }
225+ uint32_t offset = CX_SHA256_SIZE * proof -> lemmas_ctx .last_index ;
226+ if (offset >= proof -> lemmas_ctx .lemmas .data .buffer .len ) {
227+ return parser_value_out_of_range ;
228+ }
229+ left = proof -> lemmas_ctx .lemmas .data .buffer .ptr + offset ;
214230 proof -> lemmas_ctx .last_index -- ;
215231
216232 buffer_left .ptr = left ;
@@ -277,10 +293,14 @@ static parser_error_t get_hash_single_proof(uint32_t index, proof_t *proof) {
277293 * @return parser_error_t Error code indicating the result of the operation.
278294 */
279295parser_error_t get_root_hash (const merkle_proof_t * metadata , uint8_t metadataDigest [CX_SHA256_SIZE ]) {
296+ CHECK_INPUT (metadata );
297+ CHECK_INPUT (metadataDigest );
298+
280299 proof_t proof = {0 };
281300 proof .leaves = metadata -> leaves ;
282301 proof .lemmas_ctx = metadata -> lemmas_ctx ;
283302 proof .indices = metadata -> indices ;
303+ MEMSET (proof .hash , 0 , CX_SHA256_SIZE );
284304
285305 print_buffer (& proof .leaves .data .buffer , "leaves data" );
286306 print_u32 ("leaves.qty:" , proof .leaves .entries );
@@ -301,6 +321,11 @@ parser_error_t get_root_hash(const merkle_proof_t *metadata, uint8_t metadataDig
301321
302322parser_error_t verify_merkle_proofs (const merkle_proof_t * metadata ) {
303323 CHECK_INPUT (metadata );
324+ CHECK_INPUT (metadata -> root_hash .ptr );
325+
326+ if (metadata -> root_hash .len < CX_SHA256_SIZE ) {
327+ return parser_unexpected_buffer_end ;
328+ }
304329
305330 uint8_t root_hash [CX_SHA256_SIZE ] = {0 };
306331 CHECK_ERROR (get_root_hash (metadata , root_hash ));
0 commit comments