avb_hash_descriptor.c 1.5 KB

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