Tom Rini | 897a1d9 | 2018-06-19 11:21:44 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: MIT |
Igor Opaniuk | d8f9d2a | 2018-06-03 21:56:36 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 The Android Open Source Project |
Igor Opaniuk | d8f9d2a | 2018-06-03 21:56:36 +0300 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include "avb_hash_descriptor.h" |
| 7 | #include "avb_util.h" |
| 8 | |
| 9 | bool avb_hash_descriptor_validate_and_byteswap(const AvbHashDescriptor* src, |
| 10 | AvbHashDescriptor* dest) { |
| 11 | uint64_t expected_size; |
| 12 | |
| 13 | avb_memcpy(dest, src, sizeof(AvbHashDescriptor)); |
| 14 | |
| 15 | if (!avb_descriptor_validate_and_byteswap((const AvbDescriptor*)src, |
| 16 | (AvbDescriptor*)dest)) |
| 17 | return false; |
| 18 | |
| 19 | if (dest->parent_descriptor.tag != AVB_DESCRIPTOR_TAG_HASH) { |
| 20 | avb_error("Invalid tag for hash descriptor.\n"); |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | dest->image_size = avb_be64toh(dest->image_size); |
| 25 | dest->partition_name_len = avb_be32toh(dest->partition_name_len); |
| 26 | dest->salt_len = avb_be32toh(dest->salt_len); |
| 27 | dest->digest_len = avb_be32toh(dest->digest_len); |
| 28 | dest->flags = avb_be32toh(dest->flags); |
| 29 | |
| 30 | /* Check that partition_name, salt, and digest are fully contained. */ |
| 31 | expected_size = sizeof(AvbHashDescriptor) - sizeof(AvbDescriptor); |
| 32 | if (!avb_safe_add_to(&expected_size, dest->partition_name_len) || |
| 33 | !avb_safe_add_to(&expected_size, dest->salt_len) || |
| 34 | !avb_safe_add_to(&expected_size, dest->digest_len)) { |
| 35 | avb_error("Overflow while adding up sizes.\n"); |
| 36 | return false; |
| 37 | } |
| 38 | if (expected_size > dest->parent_descriptor.num_bytes_following) { |
| 39 | avb_error("Descriptor payload size overflow.\n"); |
| 40 | return false; |
| 41 | } |
| 42 | return true; |
| 43 | } |