avb_hash_descriptor.c 1.5 KB

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