1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2016 The Android Open Source Project
6 #include "avb_vbmeta_image.h"
7 #include "avb_crypto.h"
11 #include "avb_version.h"
13 AvbVBMetaVerifyResult avb_vbmeta_image_verify(
16 const uint8_t** out_public_key_data,
17 size_t* out_public_key_length) {
18 AvbVBMetaVerifyResult ret;
19 AvbVBMetaImageHeader h;
20 uint8_t* computed_hash;
21 const AvbAlgorithmData* algorithm;
22 AvbSHA256Ctx sha256_ctx;
23 AvbSHA512Ctx sha512_ctx;
24 const uint8_t* header_block;
25 const uint8_t* authentication_block;
26 const uint8_t* auxiliary_block;
27 int verification_result;
29 ret = AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER;
31 if (out_public_key_data != NULL) {
32 *out_public_key_data = NULL;
34 if (out_public_key_length != NULL) {
35 *out_public_key_length = 0;
38 /* Ensure magic is correct. */
39 if (avb_safe_memcmp(data, AVB_MAGIC, AVB_MAGIC_LEN) != 0) {
40 avb_error("Magic is incorrect.\n");
44 /* Before we byteswap, ensure length is long enough. */
45 if (length < sizeof(AvbVBMetaImageHeader)) {
46 avb_error("Length is smaller than header.\n");
49 avb_vbmeta_image_header_to_host_byte_order((const AvbVBMetaImageHeader*)data,
52 /* Ensure we don't attempt to access any fields if we do not meet
53 * the specified minimum version of libavb.
55 if ((h.required_libavb_version_major != AVB_VERSION_MAJOR) ||
56 (h.required_libavb_version_minor > AVB_VERSION_MINOR)) {
57 avb_error("Mismatch between image version and libavb version.\n");
58 ret = AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION;
62 /* Ensure |release_string| ends with a NUL byte. */
63 if (h.release_string[AVB_RELEASE_STRING_SIZE - 1] != '\0') {
64 avb_error("Release string does not end with a NUL byte.\n");
68 /* Ensure inner block sizes are multiple of 64. */
69 if ((h.authentication_data_block_size & 0x3f) != 0 ||
70 (h.auxiliary_data_block_size & 0x3f) != 0) {
71 avb_error("Block size is not a multiple of 64.\n");
75 /* Ensure block sizes all add up to at most |length|. */
76 uint64_t block_total = sizeof(AvbVBMetaImageHeader);
77 if (!avb_safe_add_to(&block_total, h.authentication_data_block_size) ||
78 !avb_safe_add_to(&block_total, h.auxiliary_data_block_size)) {
79 avb_error("Overflow while computing size of boot image.\n");
82 if (block_total > length) {
83 avb_error("Block sizes add up to more than given length.\n");
87 uintptr_t data_ptr = (uintptr_t)data;
88 /* Ensure passed in memory doesn't wrap. */
89 if (!avb_safe_add(NULL, (uint64_t)data_ptr, length)) {
90 avb_error("Boot image location and length mismatch.\n");
94 /* Ensure hash and signature are entirely in the Authentication data block. */
96 if (!avb_safe_add(&hash_end, h.hash_offset, h.hash_size) ||
97 hash_end > h.authentication_data_block_size) {
98 avb_error("Hash is not entirely in its block.\n");
101 uint64_t signature_end;
102 if (!avb_safe_add(&signature_end, h.signature_offset, h.signature_size) ||
103 signature_end > h.authentication_data_block_size) {
104 avb_error("Signature is not entirely in its block.\n");
108 /* Ensure public key is entirely in the Auxiliary data block. */
110 if (!avb_safe_add(&pubkey_end, h.public_key_offset, h.public_key_size) ||
111 pubkey_end > h.auxiliary_data_block_size) {
112 avb_error("Public key is not entirely in its block.\n");
116 /* Ensure public key metadata (if set) is entirely in the Auxiliary
118 if (h.public_key_metadata_size > 0) {
119 uint64_t pubkey_md_end;
120 if (!avb_safe_add(&pubkey_md_end,
121 h.public_key_metadata_offset,
122 h.public_key_metadata_size) ||
123 pubkey_md_end > h.auxiliary_data_block_size) {
124 avb_error("Public key metadata is not entirely in its block.\n");
129 /* Bail early if there's no hash or signature. */
130 if (h.algorithm_type == AVB_ALGORITHM_TYPE_NONE) {
131 ret = AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED;
135 /* Ensure algorithm field is supported. */
136 algorithm = avb_get_algorithm_data(h.algorithm_type);
138 avb_error("Invalid or unknown algorithm.\n");
142 /* Bail if the embedded hash size doesn't match the chosen algorithm. */
143 if (h.hash_size != algorithm->hash_len) {
144 avb_error("Embedded hash has wrong size.\n");
148 /* No overflow checks needed from here-on after since all block
149 * sizes and offsets have been verified above.
153 authentication_block = header_block + sizeof(AvbVBMetaImageHeader);
154 auxiliary_block = authentication_block + h.authentication_data_block_size;
156 switch (h.algorithm_type) {
157 /* Explicit fall-through: */
158 case AVB_ALGORITHM_TYPE_SHA256_RSA2048:
159 case AVB_ALGORITHM_TYPE_SHA256_RSA4096:
160 case AVB_ALGORITHM_TYPE_SHA256_RSA8192:
161 avb_sha256_init(&sha256_ctx);
163 &sha256_ctx, header_block, sizeof(AvbVBMetaImageHeader));
165 &sha256_ctx, auxiliary_block, h.auxiliary_data_block_size);
166 computed_hash = avb_sha256_final(&sha256_ctx);
168 /* Explicit fall-through: */
169 case AVB_ALGORITHM_TYPE_SHA512_RSA2048:
170 case AVB_ALGORITHM_TYPE_SHA512_RSA4096:
171 case AVB_ALGORITHM_TYPE_SHA512_RSA8192:
172 avb_sha512_init(&sha512_ctx);
174 &sha512_ctx, header_block, sizeof(AvbVBMetaImageHeader));
176 &sha512_ctx, auxiliary_block, h.auxiliary_data_block_size);
177 computed_hash = avb_sha512_final(&sha512_ctx);
180 avb_error("Unknown algorithm.\n");
184 if (avb_safe_memcmp(authentication_block + h.hash_offset,
187 avb_error("Hash does not match!\n");
188 ret = AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH;
192 verification_result =
193 avb_rsa_verify(auxiliary_block + h.public_key_offset,
195 authentication_block + h.signature_offset,
197 authentication_block + h.hash_offset,
200 algorithm->padding_len);
202 if (verification_result == 0) {
203 ret = AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH;
207 if (h.public_key_size > 0) {
208 if (out_public_key_data != NULL) {
209 *out_public_key_data = auxiliary_block + h.public_key_offset;
211 if (out_public_key_length != NULL) {
212 *out_public_key_length = h.public_key_size;
216 ret = AVB_VBMETA_VERIFY_RESULT_OK;
222 void avb_vbmeta_image_header_to_host_byte_order(const AvbVBMetaImageHeader* src,
223 AvbVBMetaImageHeader* dest) {
224 avb_memcpy(dest, src, sizeof(AvbVBMetaImageHeader));
226 dest->required_libavb_version_major =
227 avb_be32toh(dest->required_libavb_version_major);
228 dest->required_libavb_version_minor =
229 avb_be32toh(dest->required_libavb_version_minor);
231 dest->authentication_data_block_size =
232 avb_be64toh(dest->authentication_data_block_size);
233 dest->auxiliary_data_block_size =
234 avb_be64toh(dest->auxiliary_data_block_size);
236 dest->algorithm_type = avb_be32toh(dest->algorithm_type);
238 dest->hash_offset = avb_be64toh(dest->hash_offset);
239 dest->hash_size = avb_be64toh(dest->hash_size);
241 dest->signature_offset = avb_be64toh(dest->signature_offset);
242 dest->signature_size = avb_be64toh(dest->signature_size);
244 dest->public_key_offset = avb_be64toh(dest->public_key_offset);
245 dest->public_key_size = avb_be64toh(dest->public_key_size);
247 dest->public_key_metadata_offset =
248 avb_be64toh(dest->public_key_metadata_offset);
249 dest->public_key_metadata_size = avb_be64toh(dest->public_key_metadata_size);
251 dest->descriptors_offset = avb_be64toh(dest->descriptors_offset);
252 dest->descriptors_size = avb_be64toh(dest->descriptors_size);
254 dest->rollback_index = avb_be64toh(dest->rollback_index);
255 dest->flags = avb_be32toh(dest->flags);
258 const char* avb_vbmeta_verify_result_to_string(AvbVBMetaVerifyResult result) {
259 const char* ret = NULL;
262 case AVB_VBMETA_VERIFY_RESULT_OK:
265 case AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED:
266 ret = "OK_NOT_SIGNED";
268 case AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER:
269 ret = "INVALID_VBMETA_HEADER";
271 case AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION:
272 ret = "UNSUPPORTED_VERSION";
274 case AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH:
275 ret = "HASH_MISMATCH";
277 case AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH:
278 ret = "SIGNATURE_MISMATCH";
280 /* Do not add a 'default:' case here because of -Wswitch. */
284 avb_error("Unknown AvbVBMetaVerifyResult value.\n");