1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2016 The Android Open Source Project
6 #include "avb_slot_verify.h"
7 #include "avb_chain_partition_descriptor.h"
8 #include "avb_cmdline.h"
9 #include "avb_footer.h"
10 #include "avb_hash_descriptor.h"
11 #include "avb_hashtree_descriptor.h"
12 #include "avb_kernel_cmdline_descriptor.h"
15 #include "avb_vbmeta_image.h"
16 #include "avb_version.h"
18 /* Maximum number of partitions that can be loaded with avb_slot_verify(). */
19 #define MAX_NUMBER_OF_LOADED_PARTITIONS 32
21 /* Maximum number of vbmeta images that can be loaded with avb_slot_verify(). */
22 #define MAX_NUMBER_OF_VBMETA_IMAGES 32
24 /* Maximum size of a vbmeta image - 64 KiB. */
25 #define VBMETA_MAX_SIZE (64 * 1024)
27 /* Helper function to see if we should continue with verification in
28 * allow_verification_error=true mode if something goes wrong. See the
29 * comments for the avb_slot_verify() function for more information.
31 static inline bool result_should_continue(AvbSlotVerifyResult result) {
33 case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
34 case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
35 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
36 case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
37 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT:
40 case AVB_SLOT_VERIFY_RESULT_OK:
41 case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
42 case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
43 case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
50 static AvbSlotVerifyResult load_full_partition(AvbOps* ops,
51 const char* part_name,
53 uint8_t** out_image_buf,
54 bool* out_image_preloaded) {
58 /* Make sure that we do not overwrite existing data. */
59 avb_assert(*out_image_buf == NULL);
60 avb_assert(!*out_image_preloaded);
62 /* We are going to implicitly cast image_size from uint64_t to size_t in the
63 * following code, so we need to make sure that the cast is safe. */
64 if (image_size != (size_t)(image_size)) {
65 avb_errorv(part_name, ": Partition size too large to load.\n", NULL);
66 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
69 /* Try use a preloaded one. */
70 if (ops->get_preloaded_partition != NULL) {
71 io_ret = ops->get_preloaded_partition(
72 ops, part_name, image_size, out_image_buf, &part_num_read);
73 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
74 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
75 } else if (io_ret != AVB_IO_RESULT_OK) {
76 avb_errorv(part_name, ": Error loading data from partition.\n", NULL);
77 return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
80 if (*out_image_buf != NULL) {
81 if (part_num_read != image_size) {
82 avb_errorv(part_name, ": Read incorrect number of bytes.\n", NULL);
83 return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
85 *out_image_preloaded = true;
89 /* Allocate and copy the partition. */
90 if (!*out_image_preloaded) {
91 *out_image_buf = avb_malloc(image_size);
92 if (*out_image_buf == NULL) {
93 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
96 io_ret = ops->read_from_partition(ops,
102 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
103 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
104 } else if (io_ret != AVB_IO_RESULT_OK) {
105 avb_errorv(part_name, ": Error loading data from partition.\n", NULL);
106 return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
108 if (part_num_read != image_size) {
109 avb_errorv(part_name, ": Read incorrect number of bytes.\n", NULL);
110 return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
114 return AVB_SLOT_VERIFY_RESULT_OK;
117 static AvbSlotVerifyResult read_persistent_digest(AvbOps* ops,
118 const char* part_name,
119 size_t expected_digest_size,
120 uint8_t* out_digest) {
121 char* persistent_value_name = NULL;
122 AvbIOResult io_ret = AVB_IO_RESULT_OK;
123 size_t stored_digest_size = 0;
125 if (ops->read_persistent_value == NULL) {
126 avb_errorv(part_name, ": Persistent values are not implemented.\n", NULL);
127 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
129 persistent_value_name =
130 avb_strdupv(AVB_NPV_PERSISTENT_DIGEST_PREFIX, part_name, NULL);
131 if (persistent_value_name == NULL) {
132 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
134 io_ret = ops->read_persistent_value(ops,
135 persistent_value_name,
136 expected_digest_size,
138 &stored_digest_size);
139 avb_free(persistent_value_name);
140 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
141 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
142 } else if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_VALUE) {
143 avb_errorv(part_name, ": Persistent digest does not exist.\n", NULL);
144 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
145 } else if (io_ret == AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE ||
146 io_ret == AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE ||
147 expected_digest_size != stored_digest_size) {
149 part_name, ": Persistent digest is not of expected size.\n", NULL);
150 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
151 } else if (io_ret != AVB_IO_RESULT_OK) {
152 avb_errorv(part_name, ": Error reading persistent digest.\n", NULL);
153 return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
155 return AVB_SLOT_VERIFY_RESULT_OK;
158 static AvbSlotVerifyResult load_and_verify_hash_partition(
160 const char* const* requested_partitions,
161 const char* ab_suffix,
162 bool allow_verification_error,
163 const AvbDescriptor* descriptor,
164 AvbSlotVerifyData* slot_data) {
165 AvbHashDescriptor hash_desc;
166 const uint8_t* desc_partition_name = NULL;
167 const uint8_t* desc_salt;
168 const uint8_t* desc_digest;
169 char part_name[AVB_PART_NAME_MAX_SIZE];
170 AvbSlotVerifyResult ret;
172 uint8_t* image_buf = NULL;
173 bool image_preloaded = false;
178 size_t expected_digest_len = 0;
179 uint8_t expected_digest_buf[AVB_SHA512_DIGEST_SIZE];
180 const uint8_t* expected_digest = NULL;
182 if (!avb_hash_descriptor_validate_and_byteswap(
183 (const AvbHashDescriptor*)descriptor, &hash_desc)) {
184 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
188 desc_partition_name =
189 ((const uint8_t*)descriptor) + sizeof(AvbHashDescriptor);
190 desc_salt = desc_partition_name + hash_desc.partition_name_len;
191 desc_digest = desc_salt + hash_desc.salt_len;
193 if (!avb_validate_utf8(desc_partition_name, hash_desc.partition_name_len)) {
194 avb_error("Partition name is not valid UTF-8.\n");
195 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
199 /* Don't bother loading or validating unless the partition was
200 * requested in the first place.
202 found = avb_strv_find_str(requested_partitions,
203 (const char*)desc_partition_name,
204 hash_desc.partition_name_len);
206 ret = AVB_SLOT_VERIFY_RESULT_OK;
210 if ((hash_desc.flags & AVB_HASH_DESCRIPTOR_FLAGS_DO_NOT_USE_AB) != 0) {
211 /* No ab_suffix, just copy the partition name as is. */
212 if (hash_desc.partition_name_len >= AVB_PART_NAME_MAX_SIZE) {
213 avb_error("Partition name does not fit.\n");
214 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
217 avb_memcpy(part_name, desc_partition_name, hash_desc.partition_name_len);
218 part_name[hash_desc.partition_name_len] = '\0';
219 } else if (hash_desc.digest_len == 0 && avb_strlen(ab_suffix) != 0) {
220 /* No ab_suffix allowed for partitions without a digest in the descriptor
221 * because these partitions hold data unique to this device and are not
222 * updated using an A/B scheme.
224 avb_error("Cannot use A/B with a persistent digest.\n");
225 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
228 /* Add ab_suffix to the partition name. */
229 if (!avb_str_concat(part_name,
231 (const char*)desc_partition_name,
232 hash_desc.partition_name_len,
234 avb_strlen(ab_suffix))) {
235 avb_error("Partition name and suffix does not fit.\n");
236 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
241 /* If we're allowing verification errors then hash_desc.image_size
242 * may no longer match what's in the partition... so in this case
243 * just load the entire partition.
245 * For example, this can happen if a developer does 'fastboot flash
246 * boot /path/to/new/and/bigger/boot.img'. We want this to work
247 * since it's such a common workflow.
249 image_size = hash_desc.image_size;
250 if (allow_verification_error) {
251 if (ops->get_size_of_partition == NULL) {
252 avb_errorv(part_name,
253 ": The get_size_of_partition() operation is "
254 "not implemented so we may not load the entire partition. "
258 io_ret = ops->get_size_of_partition(ops, part_name, &image_size);
259 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
260 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
262 } else if (io_ret != AVB_IO_RESULT_OK) {
263 avb_errorv(part_name, ": Error determining partition size.\n", NULL);
264 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
267 avb_debugv(part_name, ": Loading entire partition.\n", NULL);
271 ret = load_full_partition(
272 ops, part_name, image_size, &image_buf, &image_preloaded);
273 if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
277 if (avb_strcmp((const char*)hash_desc.hash_algorithm, "sha256") == 0) {
278 AvbSHA256Ctx sha256_ctx;
279 avb_sha256_init(&sha256_ctx);
280 avb_sha256_update(&sha256_ctx, desc_salt, hash_desc.salt_len);
281 avb_sha256_update(&sha256_ctx, image_buf, hash_desc.image_size);
282 digest = avb_sha256_final(&sha256_ctx);
283 digest_len = AVB_SHA256_DIGEST_SIZE;
284 } else if (avb_strcmp((const char*)hash_desc.hash_algorithm, "sha512") == 0) {
285 AvbSHA512Ctx sha512_ctx;
286 avb_sha512_init(&sha512_ctx);
287 avb_sha512_update(&sha512_ctx, desc_salt, hash_desc.salt_len);
288 avb_sha512_update(&sha512_ctx, image_buf, hash_desc.image_size);
289 digest = avb_sha512_final(&sha512_ctx);
290 digest_len = AVB_SHA512_DIGEST_SIZE;
292 avb_errorv(part_name, ": Unsupported hash algorithm.\n", NULL);
293 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
297 if (hash_desc.digest_len == 0) {
298 // Expect a match to a persistent digest.
299 avb_debugv(part_name, ": No digest, using persistent digest.\n", NULL);
300 expected_digest_len = digest_len;
301 expected_digest = expected_digest_buf;
302 avb_assert(expected_digest_len <= sizeof(expected_digest_buf));
304 read_persistent_digest(ops, part_name, digest_len, expected_digest_buf);
305 if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
309 // Expect a match to the digest in the descriptor.
310 expected_digest_len = hash_desc.digest_len;
311 expected_digest = desc_digest;
314 if (digest_len != expected_digest_len) {
316 part_name, ": Digest in descriptor not of expected size.\n", NULL);
317 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
321 if (avb_safe_memcmp(digest, expected_digest, digest_len) != 0) {
322 avb_errorv(part_name,
323 ": Hash of data does not match digest in descriptor.\n",
325 ret = AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
329 ret = AVB_SLOT_VERIFY_RESULT_OK;
333 /* If it worked and something was loaded, copy to slot_data. */
334 if ((ret == AVB_SLOT_VERIFY_RESULT_OK || result_should_continue(ret)) &&
336 AvbPartitionData* loaded_partition;
337 if (slot_data->num_loaded_partitions == MAX_NUMBER_OF_LOADED_PARTITIONS) {
338 avb_errorv(part_name, ": Too many loaded partitions.\n", NULL);
339 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
343 &slot_data->loaded_partitions[slot_data->num_loaded_partitions++];
344 loaded_partition->partition_name = avb_strdup(found);
345 loaded_partition->data_size = image_size;
346 loaded_partition->data = image_buf;
347 loaded_partition->preloaded = image_preloaded;
352 if (image_buf != NULL && !image_preloaded) {
358 static AvbSlotVerifyResult load_requested_partitions(
360 const char* const* requested_partitions,
361 const char* ab_suffix,
362 AvbSlotVerifyData* slot_data) {
363 AvbSlotVerifyResult ret;
364 uint8_t* image_buf = NULL;
365 bool image_preloaded = false;
368 if (ops->get_size_of_partition == NULL) {
369 avb_error("get_size_of_partition() not implemented.\n");
370 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
374 for (n = 0; requested_partitions[n] != NULL; n++) {
375 char part_name[AVB_PART_NAME_MAX_SIZE];
378 AvbPartitionData* loaded_partition;
380 if (!avb_str_concat(part_name,
382 requested_partitions[n],
383 avb_strlen(requested_partitions[n]),
385 avb_strlen(ab_suffix))) {
386 avb_error("Partition name and suffix does not fit.\n");
387 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
391 io_ret = ops->get_size_of_partition(ops, part_name, &image_size);
392 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
393 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
395 } else if (io_ret != AVB_IO_RESULT_OK) {
396 avb_errorv(part_name, ": Error determining partition size.\n", NULL);
397 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
400 avb_debugv(part_name, ": Loading entire partition.\n", NULL);
402 ret = load_full_partition(
403 ops, part_name, image_size, &image_buf, &image_preloaded);
404 if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
408 /* Move to slot_data. */
409 if (slot_data->num_loaded_partitions == MAX_NUMBER_OF_LOADED_PARTITIONS) {
410 avb_errorv(part_name, ": Too many loaded partitions.\n", NULL);
411 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
415 &slot_data->loaded_partitions[slot_data->num_loaded_partitions++];
416 loaded_partition->partition_name = avb_strdup(requested_partitions[n]);
417 if (loaded_partition->partition_name == NULL) {
418 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
421 loaded_partition->data_size = image_size;
422 loaded_partition->data = image_buf; /* Transferring the owner. */
423 loaded_partition->preloaded = image_preloaded;
425 image_preloaded = false;
428 ret = AVB_SLOT_VERIFY_RESULT_OK;
431 /* Free the current buffer if any. */
432 if (image_buf != NULL && !image_preloaded) {
435 /* Buffers that are already saved in slot_data will be handled by the caller
436 * even on failure. */
440 static AvbSlotVerifyResult load_and_verify_vbmeta(
442 const char* const* requested_partitions,
443 const char* ab_suffix,
444 bool allow_verification_error,
445 AvbVBMetaImageFlags toplevel_vbmeta_flags,
446 int rollback_index_location,
447 const char* partition_name,
448 size_t partition_name_len,
449 const uint8_t* expected_public_key,
450 size_t expected_public_key_length,
451 AvbSlotVerifyData* slot_data,
452 AvbAlgorithmType* out_algorithm_type,
453 AvbCmdlineSubstList* out_additional_cmdline_subst) {
454 char full_partition_name[AVB_PART_NAME_MAX_SIZE];
455 AvbSlotVerifyResult ret;
457 size_t vbmeta_offset;
459 uint8_t* vbmeta_buf = NULL;
460 size_t vbmeta_num_read;
461 AvbVBMetaVerifyResult vbmeta_ret;
462 const uint8_t* pk_data;
464 AvbVBMetaImageHeader vbmeta_header;
465 uint64_t stored_rollback_index;
466 const AvbDescriptor** descriptors = NULL;
467 size_t num_descriptors;
470 bool is_vbmeta_partition;
471 AvbVBMetaData* vbmeta_image_data = NULL;
473 ret = AVB_SLOT_VERIFY_RESULT_OK;
475 avb_assert(slot_data != NULL);
477 /* Since we allow top-level vbmeta in 'boot', use
478 * rollback_index_location to determine whether we're the main
481 is_main_vbmeta = (rollback_index_location == 0);
482 is_vbmeta_partition = (avb_strcmp(partition_name, "vbmeta") == 0);
484 if (!avb_validate_utf8((const uint8_t*)partition_name, partition_name_len)) {
485 avb_error("Partition name is not valid UTF-8.\n");
486 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
490 /* Construct full partition name. */
491 if (!avb_str_concat(full_partition_name,
492 sizeof full_partition_name,
496 avb_strlen(ab_suffix))) {
497 avb_error("Partition name and suffix does not fit.\n");
498 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
502 avb_debugv("Loading vbmeta struct from partition '",
507 /* If we're loading from the main vbmeta partition, the vbmeta
508 * struct is in the beginning. Otherwise we have to locate it via a
511 if (is_vbmeta_partition) {
513 vbmeta_size = VBMETA_MAX_SIZE;
515 uint8_t footer_buf[AVB_FOOTER_SIZE];
516 size_t footer_num_read;
519 io_ret = ops->read_from_partition(ops,
525 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
526 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
528 } else if (io_ret != AVB_IO_RESULT_OK) {
529 avb_errorv(full_partition_name, ": Error loading footer.\n", NULL);
530 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
533 avb_assert(footer_num_read == AVB_FOOTER_SIZE);
535 if (!avb_footer_validate_and_byteswap((const AvbFooter*)footer_buf,
537 avb_errorv(full_partition_name, ": Error validating footer.\n", NULL);
538 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
542 /* Basic footer sanity check since the data is untrusted. */
543 if (footer.vbmeta_size > VBMETA_MAX_SIZE) {
545 full_partition_name, ": Invalid vbmeta size in footer.\n", NULL);
546 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
550 vbmeta_offset = footer.vbmeta_offset;
551 vbmeta_size = footer.vbmeta_size;
554 vbmeta_buf = avb_malloc(vbmeta_size);
555 if (vbmeta_buf == NULL) {
556 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
560 io_ret = ops->read_from_partition(ops,
566 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
567 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
569 } else if (io_ret != AVB_IO_RESULT_OK) {
570 /* If we're looking for 'vbmeta' but there is no such partition,
571 * go try to get it from the boot partition instead.
573 if (is_main_vbmeta && io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION &&
574 is_vbmeta_partition) {
575 avb_debugv(full_partition_name,
576 ": No such partition. Trying 'boot' instead.\n",
578 ret = load_and_verify_vbmeta(ops,
579 requested_partitions,
581 allow_verification_error,
582 0 /* toplevel_vbmeta_flags */,
583 0 /* rollback_index_location */,
586 NULL /* expected_public_key */,
587 0 /* expected_public_key_length */,
590 out_additional_cmdline_subst);
593 avb_errorv(full_partition_name, ": Error loading vbmeta data.\n", NULL);
594 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
598 avb_assert(vbmeta_num_read <= vbmeta_size);
600 /* Check if the image is properly signed and get the public key used
604 avb_vbmeta_image_verify(vbmeta_buf, vbmeta_num_read, &pk_data, &pk_len);
605 switch (vbmeta_ret) {
606 case AVB_VBMETA_VERIFY_RESULT_OK:
607 avb_assert(pk_data != NULL && pk_len > 0);
610 case AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED:
611 case AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH:
612 case AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH:
613 ret = AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
614 avb_errorv(full_partition_name,
615 ": Error verifying vbmeta image: ",
616 avb_vbmeta_verify_result_to_string(vbmeta_ret),
619 if (!allow_verification_error) {
624 case AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER:
625 /* No way to continue this case. */
626 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
627 avb_errorv(full_partition_name,
628 ": Error verifying vbmeta image: invalid vbmeta header\n",
632 case AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION:
633 /* No way to continue this case. */
634 ret = AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION;
635 avb_errorv(full_partition_name,
636 ": Error verifying vbmeta image: unsupported AVB version\n",
641 /* Byteswap the header. */
642 avb_vbmeta_image_header_to_host_byte_order((AvbVBMetaImageHeader*)vbmeta_buf,
645 /* If we're the toplevel, assign flags so they'll be passed down. */
646 if (is_main_vbmeta) {
647 toplevel_vbmeta_flags = (AvbVBMetaImageFlags)vbmeta_header.flags;
649 if (vbmeta_header.flags != 0) {
650 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
651 avb_errorv(full_partition_name,
652 ": chained vbmeta image has non-zero flags\n",
658 /* Check if key used to make signature matches what is expected. */
659 if (pk_data != NULL) {
660 if (expected_public_key != NULL) {
661 avb_assert(!is_main_vbmeta);
662 if (expected_public_key_length != pk_len ||
663 avb_safe_memcmp(expected_public_key, pk_data, pk_len) != 0) {
664 avb_errorv(full_partition_name,
665 ": Public key used to sign data does not match key in chain "
666 "partition descriptor.\n",
668 ret = AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED;
669 if (!allow_verification_error) {
674 bool key_is_trusted = false;
675 const uint8_t* pk_metadata = NULL;
676 size_t pk_metadata_len = 0;
678 if (vbmeta_header.public_key_metadata_size > 0) {
679 pk_metadata = vbmeta_buf + sizeof(AvbVBMetaImageHeader) +
680 vbmeta_header.authentication_data_block_size +
681 vbmeta_header.public_key_metadata_offset;
682 pk_metadata_len = vbmeta_header.public_key_metadata_size;
685 avb_assert(is_main_vbmeta);
686 io_ret = ops->validate_vbmeta_public_key(
687 ops, pk_data, pk_len, pk_metadata, pk_metadata_len, &key_is_trusted);
688 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
689 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
691 } else if (io_ret != AVB_IO_RESULT_OK) {
692 avb_errorv(full_partition_name,
693 ": Error while checking public key used to sign data.\n",
695 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
698 if (!key_is_trusted) {
699 avb_errorv(full_partition_name,
700 ": Public key used to sign data rejected.\n",
702 ret = AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED;
703 if (!allow_verification_error) {
710 /* Check rollback index. */
711 io_ret = ops->read_rollback_index(
712 ops, rollback_index_location, &stored_rollback_index);
713 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
714 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
716 } else if (io_ret != AVB_IO_RESULT_OK) {
717 avb_errorv(full_partition_name,
718 ": Error getting rollback index for location.\n",
720 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
723 if (vbmeta_header.rollback_index < stored_rollback_index) {
726 ": Image rollback index is less than the stored rollback index.\n",
728 ret = AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX;
729 if (!allow_verification_error) {
734 /* Copy vbmeta to vbmeta_images before recursing. */
735 if (is_main_vbmeta) {
736 avb_assert(slot_data->num_vbmeta_images == 0);
738 avb_assert(slot_data->num_vbmeta_images > 0);
740 if (slot_data->num_vbmeta_images == MAX_NUMBER_OF_VBMETA_IMAGES) {
741 avb_errorv(full_partition_name, ": Too many vbmeta images.\n", NULL);
742 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
745 vbmeta_image_data = &slot_data->vbmeta_images[slot_data->num_vbmeta_images++];
746 vbmeta_image_data->partition_name = avb_strdup(partition_name);
747 vbmeta_image_data->vbmeta_data = vbmeta_buf;
748 /* Note that |vbmeta_buf| is actually |vbmeta_num_read| bytes long
749 * and this includes data past the end of the image. Pass the
750 * actual size of the vbmeta image. Also, no need to use
751 * avb_safe_add() since the header has already been verified.
753 vbmeta_image_data->vbmeta_size =
754 sizeof(AvbVBMetaImageHeader) +
755 vbmeta_header.authentication_data_block_size +
756 vbmeta_header.auxiliary_data_block_size;
757 vbmeta_image_data->verify_result = vbmeta_ret;
759 /* If verification has been disabled by setting a bit in the image,
760 * we're done... except that we need to load the entirety of the
761 * requested partitions.
763 if (vbmeta_header.flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED) {
764 AvbSlotVerifyResult sub_ret;
766 full_partition_name, ": VERIFICATION_DISABLED bit is set.\n", NULL);
767 /* If load_requested_partitions() fail it is always a fatal
768 * failure (e.g. ERROR_INVALID_ARGUMENT, ERROR_OOM, etc.) rather
769 * than recoverable (e.g. one where result_should_continue()
770 * returns true) and we want to convey that error.
772 sub_ret = load_requested_partitions(
773 ops, requested_partitions, ab_suffix, slot_data);
774 if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
780 /* Now go through all descriptors and take the appropriate action:
782 * - hash descriptor: Load data from partition, calculate hash, and
783 * checks that it matches what's in the hash descriptor.
785 * - hashtree descriptor: Do nothing since verification happens
786 * on-the-fly from within the OS. (Unless the descriptor uses a
787 * persistent digest, in which case we need to find it).
789 * - chained partition descriptor: Load the footer, load the vbmeta
790 * image, verify vbmeta image (includes rollback checks, hash
791 * checks, bail on chained partitions).
794 avb_descriptor_get_all(vbmeta_buf, vbmeta_num_read, &num_descriptors);
795 for (n = 0; n < num_descriptors; n++) {
798 if (!avb_descriptor_validate_and_byteswap(descriptors[n], &desc)) {
799 avb_errorv(full_partition_name, ": Descriptor is invalid.\n", NULL);
800 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
805 case AVB_DESCRIPTOR_TAG_HASH: {
806 AvbSlotVerifyResult sub_ret;
807 sub_ret = load_and_verify_hash_partition(ops,
808 requested_partitions,
810 allow_verification_error,
813 if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
815 if (!allow_verification_error || !result_should_continue(ret)) {
821 case AVB_DESCRIPTOR_TAG_CHAIN_PARTITION: {
822 AvbSlotVerifyResult sub_ret;
823 AvbChainPartitionDescriptor chain_desc;
824 const uint8_t* chain_partition_name;
825 const uint8_t* chain_public_key;
827 /* Only allow CHAIN_PARTITION descriptors in the main vbmeta image. */
828 if (!is_main_vbmeta) {
829 avb_errorv(full_partition_name,
830 ": Encountered chain descriptor not in main image.\n",
832 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
836 if (!avb_chain_partition_descriptor_validate_and_byteswap(
837 (AvbChainPartitionDescriptor*)descriptors[n], &chain_desc)) {
838 avb_errorv(full_partition_name,
839 ": Chain partition descriptor is invalid.\n",
841 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
845 if (chain_desc.rollback_index_location == 0) {
846 avb_errorv(full_partition_name,
847 ": Chain partition has invalid "
848 "rollback_index_location field.\n",
850 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
854 chain_partition_name = ((const uint8_t*)descriptors[n]) +
855 sizeof(AvbChainPartitionDescriptor);
856 chain_public_key = chain_partition_name + chain_desc.partition_name_len;
859 load_and_verify_vbmeta(ops,
860 requested_partitions,
862 allow_verification_error,
863 toplevel_vbmeta_flags,
864 chain_desc.rollback_index_location,
865 (const char*)chain_partition_name,
866 chain_desc.partition_name_len,
868 chain_desc.public_key_len,
870 NULL, /* out_algorithm_type */
871 NULL /* out_additional_cmdline_subst */);
872 if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
874 if (!result_should_continue(ret)) {
880 case AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE: {
881 const uint8_t* kernel_cmdline;
882 AvbKernelCmdlineDescriptor kernel_cmdline_desc;
885 if (!avb_kernel_cmdline_descriptor_validate_and_byteswap(
886 (AvbKernelCmdlineDescriptor*)descriptors[n],
887 &kernel_cmdline_desc)) {
888 avb_errorv(full_partition_name,
889 ": Kernel cmdline descriptor is invalid.\n",
891 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
895 kernel_cmdline = ((const uint8_t*)descriptors[n]) +
896 sizeof(AvbKernelCmdlineDescriptor);
898 if (!avb_validate_utf8(kernel_cmdline,
899 kernel_cmdline_desc.kernel_cmdline_length)) {
900 avb_errorv(full_partition_name,
901 ": Kernel cmdline is not valid UTF-8.\n",
903 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
907 /* Compare the flags for top-level VBMeta struct with flags in
908 * the command-line descriptor so command-line snippets only
909 * intended for a certain mode (dm-verity enabled/disabled)
910 * are skipped if applicable.
912 apply_cmdline = true;
913 if (toplevel_vbmeta_flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
914 if (kernel_cmdline_desc.flags &
915 AVB_KERNEL_CMDLINE_FLAGS_USE_ONLY_IF_HASHTREE_NOT_DISABLED) {
916 apply_cmdline = false;
919 if (kernel_cmdline_desc.flags &
920 AVB_KERNEL_CMDLINE_FLAGS_USE_ONLY_IF_HASHTREE_DISABLED) {
921 apply_cmdline = false;
926 if (slot_data->cmdline == NULL) {
928 avb_calloc(kernel_cmdline_desc.kernel_cmdline_length + 1);
929 if (slot_data->cmdline == NULL) {
930 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
933 avb_memcpy(slot_data->cmdline,
935 kernel_cmdline_desc.kernel_cmdline_length);
937 /* new cmdline is: <existing_cmdline> + ' ' + <newcmdline> + '\0' */
938 size_t orig_size = avb_strlen(slot_data->cmdline);
940 orig_size + 1 + kernel_cmdline_desc.kernel_cmdline_length + 1;
941 char* new_cmdline = avb_calloc(new_size);
942 if (new_cmdline == NULL) {
943 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
946 avb_memcpy(new_cmdline, slot_data->cmdline, orig_size);
947 new_cmdline[orig_size] = ' ';
948 avb_memcpy(new_cmdline + orig_size + 1,
950 kernel_cmdline_desc.kernel_cmdline_length);
951 avb_free(slot_data->cmdline);
952 slot_data->cmdline = new_cmdline;
957 case AVB_DESCRIPTOR_TAG_HASHTREE: {
958 AvbHashtreeDescriptor hashtree_desc;
960 if (!avb_hashtree_descriptor_validate_and_byteswap(
961 (AvbHashtreeDescriptor*)descriptors[n], &hashtree_desc)) {
963 full_partition_name, ": Hashtree descriptor is invalid.\n", NULL);
964 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
968 /* We only need to continue when there is no digest in the descriptor.
969 * This is because the only processing here is to find the digest and
970 * make it available on the kernel command line.
972 if (hashtree_desc.root_digest_len == 0) {
973 char part_name[AVB_PART_NAME_MAX_SIZE];
974 size_t digest_len = 0;
975 uint8_t digest_buf[AVB_SHA512_DIGEST_SIZE];
976 const uint8_t* desc_partition_name =
977 ((const uint8_t*)descriptors[n]) + sizeof(AvbHashtreeDescriptor);
979 if (!avb_validate_utf8(desc_partition_name,
980 hashtree_desc.partition_name_len)) {
981 avb_error("Partition name is not valid UTF-8.\n");
982 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
986 /* No ab_suffix for partitions without a digest in the descriptor
987 * because these partitions hold data unique to this device and are
988 * not updated using an A/B scheme.
990 if ((hashtree_desc.flags &
991 AVB_HASHTREE_DESCRIPTOR_FLAGS_DO_NOT_USE_AB) == 0 &&
992 avb_strlen(ab_suffix) != 0) {
993 avb_error("Cannot use A/B with a persistent root digest.\n");
994 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
997 if (hashtree_desc.partition_name_len >= AVB_PART_NAME_MAX_SIZE) {
998 avb_error("Partition name does not fit.\n");
999 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1003 part_name, desc_partition_name, hashtree_desc.partition_name_len);
1004 part_name[hashtree_desc.partition_name_len] = '\0';
1006 /* Determine the expected digest size from the hash algorithm. */
1007 if (avb_strcmp((const char*)hashtree_desc.hash_algorithm, "sha1") ==
1009 digest_len = AVB_SHA1_DIGEST_SIZE;
1010 } else if (avb_strcmp((const char*)hashtree_desc.hash_algorithm,
1012 digest_len = AVB_SHA256_DIGEST_SIZE;
1013 } else if (avb_strcmp((const char*)hashtree_desc.hash_algorithm,
1015 digest_len = AVB_SHA512_DIGEST_SIZE;
1017 avb_errorv(part_name, ": Unsupported hash algorithm.\n", NULL);
1018 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1022 ret = read_persistent_digest(ops, part_name, digest_len, digest_buf);
1023 if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
1027 if (out_additional_cmdline_subst) {
1029 avb_add_root_digest_substitution(part_name,
1032 out_additional_cmdline_subst);
1033 if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
1040 case AVB_DESCRIPTOR_TAG_PROPERTY:
1046 if (rollback_index_location >= AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS) {
1048 full_partition_name, ": Invalid rollback_index_location.\n", NULL);
1049 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1053 slot_data->rollback_indexes[rollback_index_location] =
1054 vbmeta_header.rollback_index;
1056 if (out_algorithm_type != NULL) {
1057 *out_algorithm_type = (AvbAlgorithmType)vbmeta_header.algorithm_type;
1061 /* If |vbmeta_image_data| isn't NULL it means that it adopted
1062 * |vbmeta_buf| so in that case don't free it here.
1064 if (vbmeta_image_data == NULL) {
1065 if (vbmeta_buf != NULL) {
1066 avb_free(vbmeta_buf);
1069 if (descriptors != NULL) {
1070 avb_free(descriptors);
1075 AvbSlotVerifyResult avb_slot_verify(AvbOps* ops,
1076 const char* const* requested_partitions,
1077 const char* ab_suffix,
1078 AvbSlotVerifyFlags flags,
1079 AvbHashtreeErrorMode hashtree_error_mode,
1080 AvbSlotVerifyData** out_data) {
1081 AvbSlotVerifyResult ret;
1082 AvbSlotVerifyData* slot_data = NULL;
1083 AvbAlgorithmType algorithm_type = AVB_ALGORITHM_TYPE_NONE;
1084 bool using_boot_for_vbmeta = false;
1085 AvbVBMetaImageHeader toplevel_vbmeta;
1086 bool allow_verification_error =
1087 (flags & AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR);
1088 AvbCmdlineSubstList* additional_cmdline_subst = NULL;
1090 /* Fail early if we're missing the AvbOps needed for slot verification.
1092 * For now, handle get_size_of_partition() not being implemented. In
1093 * a later release we may change that.
1095 avb_assert(ops->read_is_device_unlocked != NULL);
1096 avb_assert(ops->read_from_partition != NULL);
1097 avb_assert(ops->validate_vbmeta_public_key != NULL);
1098 avb_assert(ops->read_rollback_index != NULL);
1099 avb_assert(ops->get_unique_guid_for_partition != NULL);
1101 if (out_data != NULL) {
1105 /* Allowing dm-verity errors defeats the purpose of verified boot so
1106 * only allow this if set up to allow verification errors
1107 * (e.g. typically only UNLOCKED mode).
1109 if (hashtree_error_mode == AVB_HASHTREE_ERROR_MODE_LOGGING &&
1110 !allow_verification_error) {
1111 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
1115 slot_data = avb_calloc(sizeof(AvbSlotVerifyData));
1116 if (slot_data == NULL) {
1117 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1120 slot_data->vbmeta_images =
1121 avb_calloc(sizeof(AvbVBMetaData) * MAX_NUMBER_OF_VBMETA_IMAGES);
1122 if (slot_data->vbmeta_images == NULL) {
1123 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1126 slot_data->loaded_partitions =
1127 avb_calloc(sizeof(AvbPartitionData) * MAX_NUMBER_OF_LOADED_PARTITIONS);
1128 if (slot_data->loaded_partitions == NULL) {
1129 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1133 additional_cmdline_subst = avb_new_cmdline_subst_list();
1134 if (additional_cmdline_subst == NULL) {
1135 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1139 ret = load_and_verify_vbmeta(ops,
1140 requested_partitions,
1142 allow_verification_error,
1143 0 /* toplevel_vbmeta_flags */,
1144 0 /* rollback_index_location */,
1146 avb_strlen("vbmeta"),
1147 NULL /* expected_public_key */,
1148 0 /* expected_public_key_length */,
1151 additional_cmdline_subst);
1152 if (!allow_verification_error && ret != AVB_SLOT_VERIFY_RESULT_OK) {
1156 /* If things check out, mangle the kernel command-line as needed. */
1157 if (result_should_continue(ret)) {
1158 if (avb_strcmp(slot_data->vbmeta_images[0].partition_name, "vbmeta") != 0) {
1160 avb_strcmp(slot_data->vbmeta_images[0].partition_name, "boot") == 0);
1161 using_boot_for_vbmeta = true;
1164 /* Byteswap top-level vbmeta header since we'll need it below. */
1165 avb_vbmeta_image_header_to_host_byte_order(
1166 (const AvbVBMetaImageHeader*)slot_data->vbmeta_images[0].vbmeta_data,
1169 /* Fill in |ab_suffix| field. */
1170 slot_data->ab_suffix = avb_strdup(ab_suffix);
1171 if (slot_data->ab_suffix == NULL) {
1172 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1176 /* If verification is disabled, we are done ... we specifically
1177 * don't want to add any androidboot.* options since verification
1180 if (toplevel_vbmeta.flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED) {
1181 /* Since verification is disabled we didn't process any
1182 * descriptors and thus there's no cmdline... so set root= such
1183 * that the system partition is mounted.
1185 avb_assert(slot_data->cmdline == NULL);
1186 slot_data->cmdline =
1187 avb_strdup("root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)");
1188 if (slot_data->cmdline == NULL) {
1189 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1193 /* Add options - any failure in avb_append_options() is either an
1196 AvbSlotVerifyResult sub_ret = avb_append_options(ops,
1200 hashtree_error_mode);
1201 if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
1207 /* Substitute $(ANDROID_SYSTEM_PARTUUID) and friends. */
1208 if (slot_data->cmdline != NULL) {
1210 new_cmdline = avb_sub_cmdline(ops,
1213 using_boot_for_vbmeta,
1214 additional_cmdline_subst);
1215 if (new_cmdline != slot_data->cmdline) {
1216 if (new_cmdline == NULL) {
1217 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1220 avb_free(slot_data->cmdline);
1221 slot_data->cmdline = new_cmdline;
1225 if (out_data != NULL) {
1226 *out_data = slot_data;
1228 avb_slot_verify_data_free(slot_data);
1232 avb_free_cmdline_subst_list(additional_cmdline_subst);
1233 additional_cmdline_subst = NULL;
1235 if (!allow_verification_error) {
1236 avb_assert(ret == AVB_SLOT_VERIFY_RESULT_OK);
1242 if (slot_data != NULL) {
1243 avb_slot_verify_data_free(slot_data);
1245 if (additional_cmdline_subst != NULL) {
1246 avb_free_cmdline_subst_list(additional_cmdline_subst);
1251 void avb_slot_verify_data_free(AvbSlotVerifyData* data) {
1252 if (data->ab_suffix != NULL) {
1253 avb_free(data->ab_suffix);
1255 if (data->cmdline != NULL) {
1256 avb_free(data->cmdline);
1258 if (data->vbmeta_images != NULL) {
1260 for (n = 0; n < data->num_vbmeta_images; n++) {
1261 AvbVBMetaData* vbmeta_image = &data->vbmeta_images[n];
1262 if (vbmeta_image->partition_name != NULL) {
1263 avb_free(vbmeta_image->partition_name);
1265 if (vbmeta_image->vbmeta_data != NULL) {
1266 avb_free(vbmeta_image->vbmeta_data);
1269 avb_free(data->vbmeta_images);
1271 if (data->loaded_partitions != NULL) {
1273 for (n = 0; n < data->num_loaded_partitions; n++) {
1274 AvbPartitionData* loaded_partition = &data->loaded_partitions[n];
1275 if (loaded_partition->partition_name != NULL) {
1276 avb_free(loaded_partition->partition_name);
1278 if (loaded_partition->data != NULL && !loaded_partition->preloaded) {
1279 avb_free(loaded_partition->data);
1282 avb_free(data->loaded_partitions);
1287 const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result) {
1288 const char* ret = NULL;
1291 case AVB_SLOT_VERIFY_RESULT_OK:
1294 case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
1297 case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
1300 case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
1301 ret = "ERROR_VERIFICATION";
1303 case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
1304 ret = "ERROR_ROLLBACK_INDEX";
1306 case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
1307 ret = "ERROR_PUBLIC_KEY_REJECTED";
1309 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
1310 ret = "ERROR_INVALID_METADATA";
1312 case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
1313 ret = "ERROR_UNSUPPORTED_VERSION";
1315 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT:
1316 ret = "ERROR_INVALID_ARGUMENT";
1318 /* Do not add a 'default:' case here because of -Wswitch. */
1322 avb_error("Unknown AvbSlotVerifyResult value.\n");
1329 void avb_slot_verify_data_calculate_vbmeta_digest(AvbSlotVerifyData* data,
1330 AvbDigestType digest_type,
1331 uint8_t* out_digest) {
1335 switch (digest_type) {
1336 case AVB_DIGEST_TYPE_SHA256: {
1338 avb_sha256_init(&ctx);
1339 for (n = 0; n < data->num_vbmeta_images; n++) {
1340 avb_sha256_update(&ctx,
1341 data->vbmeta_images[n].vbmeta_data,
1342 data->vbmeta_images[n].vbmeta_size);
1344 avb_memcpy(out_digest, avb_sha256_final(&ctx), AVB_SHA256_DIGEST_SIZE);
1348 case AVB_DIGEST_TYPE_SHA512: {
1350 avb_sha512_init(&ctx);
1351 for (n = 0; n < data->num_vbmeta_images; n++) {
1352 avb_sha512_update(&ctx,
1353 data->vbmeta_images[n].vbmeta_data,
1354 data->vbmeta_images[n].vbmeta_size);
1356 avb_memcpy(out_digest, avb_sha512_final(&ctx), AVB_SHA512_DIGEST_SIZE);
1360 /* Do not add a 'default:' case here because of -Wswitch. */
1364 avb_fatal("Unknown digest type");