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