xref: /openbmc/u-boot/lib/libavb/avb_hashtree_descriptor.c (revision 897a1d947e7e50cf03113dcbe505ffb63dce45a9)
1*897a1d94STom Rini // SPDX-License-Identifier: MIT
2d8f9d2afSIgor Opaniuk /*
3d8f9d2afSIgor Opaniuk  * Copyright (C) 2016 The Android Open Source Project
4d8f9d2afSIgor Opaniuk  */
5d8f9d2afSIgor Opaniuk 
6d8f9d2afSIgor Opaniuk #include "avb_hashtree_descriptor.h"
7d8f9d2afSIgor Opaniuk #include "avb_util.h"
8d8f9d2afSIgor Opaniuk 
avb_hashtree_descriptor_validate_and_byteswap(const AvbHashtreeDescriptor * src,AvbHashtreeDescriptor * dest)9d8f9d2afSIgor Opaniuk bool avb_hashtree_descriptor_validate_and_byteswap(
10d8f9d2afSIgor Opaniuk     const AvbHashtreeDescriptor* src, AvbHashtreeDescriptor* dest) {
11d8f9d2afSIgor Opaniuk   uint64_t expected_size;
12d8f9d2afSIgor Opaniuk 
13d8f9d2afSIgor Opaniuk   avb_memcpy(dest, src, sizeof(AvbHashtreeDescriptor));
14d8f9d2afSIgor Opaniuk 
15d8f9d2afSIgor Opaniuk   if (!avb_descriptor_validate_and_byteswap((const AvbDescriptor*)src,
16d8f9d2afSIgor Opaniuk                                             (AvbDescriptor*)dest))
17d8f9d2afSIgor Opaniuk     return false;
18d8f9d2afSIgor Opaniuk 
19d8f9d2afSIgor Opaniuk   if (dest->parent_descriptor.tag != AVB_DESCRIPTOR_TAG_HASHTREE) {
20d8f9d2afSIgor Opaniuk     avb_error("Invalid tag for hashtree descriptor.\n");
21d8f9d2afSIgor Opaniuk     return false;
22d8f9d2afSIgor Opaniuk   }
23d8f9d2afSIgor Opaniuk 
24d8f9d2afSIgor Opaniuk   dest->dm_verity_version = avb_be32toh(dest->dm_verity_version);
25d8f9d2afSIgor Opaniuk   dest->image_size = avb_be64toh(dest->image_size);
26d8f9d2afSIgor Opaniuk   dest->tree_offset = avb_be64toh(dest->tree_offset);
27d8f9d2afSIgor Opaniuk   dest->tree_size = avb_be64toh(dest->tree_size);
28d8f9d2afSIgor Opaniuk   dest->data_block_size = avb_be32toh(dest->data_block_size);
29d8f9d2afSIgor Opaniuk   dest->hash_block_size = avb_be32toh(dest->hash_block_size);
30d8f9d2afSIgor Opaniuk   dest->fec_num_roots = avb_be32toh(dest->fec_num_roots);
31d8f9d2afSIgor Opaniuk   dest->fec_offset = avb_be64toh(dest->fec_offset);
32d8f9d2afSIgor Opaniuk   dest->fec_size = avb_be64toh(dest->fec_size);
33d8f9d2afSIgor Opaniuk   dest->partition_name_len = avb_be32toh(dest->partition_name_len);
34d8f9d2afSIgor Opaniuk   dest->salt_len = avb_be32toh(dest->salt_len);
35d8f9d2afSIgor Opaniuk   dest->root_digest_len = avb_be32toh(dest->root_digest_len);
36d8f9d2afSIgor Opaniuk   dest->flags = avb_be32toh(dest->flags);
37d8f9d2afSIgor Opaniuk 
38d8f9d2afSIgor Opaniuk   /* Check that partition_name, salt, and root_digest are fully contained. */
39d8f9d2afSIgor Opaniuk   expected_size = sizeof(AvbHashtreeDescriptor) - sizeof(AvbDescriptor);
40d8f9d2afSIgor Opaniuk   if (!avb_safe_add_to(&expected_size, dest->partition_name_len) ||
41d8f9d2afSIgor Opaniuk       !avb_safe_add_to(&expected_size, dest->salt_len) ||
42d8f9d2afSIgor Opaniuk       !avb_safe_add_to(&expected_size, dest->root_digest_len)) {
43d8f9d2afSIgor Opaniuk     avb_error("Overflow while adding up sizes.\n");
44d8f9d2afSIgor Opaniuk     return false;
45d8f9d2afSIgor Opaniuk   }
46d8f9d2afSIgor Opaniuk   if (expected_size > dest->parent_descriptor.num_bytes_following) {
47d8f9d2afSIgor Opaniuk     avb_error("Descriptor payload size overflow.\n");
48d8f9d2afSIgor Opaniuk     return false;
49d8f9d2afSIgor Opaniuk   }
50d8f9d2afSIgor Opaniuk   return true;
51d8f9d2afSIgor Opaniuk }
52