1*5b69db07SJason Zhu /* 2*5b69db07SJason Zhu * Copyright (C) 2016 The Android Open Source Project 3*5b69db07SJason Zhu * 4*5b69db07SJason Zhu * Permission is hereby granted, free of charge, to any person 5*5b69db07SJason Zhu * obtaining a copy of this software and associated documentation 6*5b69db07SJason Zhu * files (the "Software"), to deal in the Software without 7*5b69db07SJason Zhu * restriction, including without limitation the rights to use, copy, 8*5b69db07SJason Zhu * modify, merge, publish, distribute, sublicense, and/or sell copies 9*5b69db07SJason Zhu * of the Software, and to permit persons to whom the Software is 10*5b69db07SJason Zhu * furnished to do so, subject to the following conditions: 11*5b69db07SJason Zhu * 12*5b69db07SJason Zhu * The above copyright notice and this permission notice shall be 13*5b69db07SJason Zhu * included in all copies or substantial portions of the Software. 14*5b69db07SJason Zhu * 15*5b69db07SJason Zhu * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16*5b69db07SJason Zhu * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17*5b69db07SJason Zhu * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18*5b69db07SJason Zhu * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19*5b69db07SJason Zhu * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20*5b69db07SJason Zhu * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21*5b69db07SJason Zhu * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*5b69db07SJason Zhu * SOFTWARE. 23*5b69db07SJason Zhu */ 24*5b69db07SJason Zhu 25*5b69db07SJason Zhu /* 26*5b69db07SJason Zhu #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION) 27*5b69db07SJason Zhu #error "Never include this file directly, include libavb.h instead." 28*5b69db07SJason Zhu #endif 29*5b69db07SJason Zhu */ 30*5b69db07SJason Zhu 31*5b69db07SJason Zhu #ifndef AVB_VBMETA_IMAGE_H_ 32*5b69db07SJason Zhu #define AVB_VBMETA_IMAGE_H_ 33*5b69db07SJason Zhu 34*5b69db07SJason Zhu #include <android_avb/avb_sysdeps.h> 35*5b69db07SJason Zhu 36*5b69db07SJason Zhu #ifdef __cplusplus 37*5b69db07SJason Zhu extern "C" { 38*5b69db07SJason Zhu #endif 39*5b69db07SJason Zhu 40*5b69db07SJason Zhu #include <android_avb/avb_crypto.h> 41*5b69db07SJason Zhu #include <android_avb/avb_descriptor.h> 42*5b69db07SJason Zhu 43*5b69db07SJason Zhu /* Size of the vbmeta image header. */ 44*5b69db07SJason Zhu #define AVB_VBMETA_IMAGE_HEADER_SIZE 256 45*5b69db07SJason Zhu 46*5b69db07SJason Zhu /* Magic for the vbmeta image header. */ 47*5b69db07SJason Zhu #define AVB_MAGIC "AVB0" 48*5b69db07SJason Zhu #define AVB_MAGIC_LEN 4 49*5b69db07SJason Zhu 50*5b69db07SJason Zhu /* Maximum size of the release string including the terminating NUL byte. */ 51*5b69db07SJason Zhu #define AVB_RELEASE_STRING_SIZE 48 52*5b69db07SJason Zhu 53*5b69db07SJason Zhu /* Flags for the vbmeta image. 54*5b69db07SJason Zhu * 55*5b69db07SJason Zhu * AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED: If this flag is set, 56*5b69db07SJason Zhu * hashtree image verification will be disabled. 57*5b69db07SJason Zhu */ 58*5b69db07SJason Zhu typedef enum { 59*5b69db07SJason Zhu AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED = (1 << 0), 60*5b69db07SJason Zhu AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED = (1 << 1) 61*5b69db07SJason Zhu } AvbVBMetaImageFlags; 62*5b69db07SJason Zhu 63*5b69db07SJason Zhu /* Binary format for header of the vbmeta image. 64*5b69db07SJason Zhu * 65*5b69db07SJason Zhu * The vbmeta image consists of three blocks: 66*5b69db07SJason Zhu * 67*5b69db07SJason Zhu * +-----------------------------------------+ 68*5b69db07SJason Zhu * | Header data - fixed size | 69*5b69db07SJason Zhu * +-----------------------------------------+ 70*5b69db07SJason Zhu * | Authentication data - variable size | 71*5b69db07SJason Zhu * +-----------------------------------------+ 72*5b69db07SJason Zhu * | Auxiliary data - variable size | 73*5b69db07SJason Zhu * +-----------------------------------------+ 74*5b69db07SJason Zhu * 75*5b69db07SJason Zhu * The "Header data" block is described by this struct and is always 76*5b69db07SJason Zhu * |AVB_VBMETA_IMAGE_HEADER_SIZE| bytes long. 77*5b69db07SJason Zhu * 78*5b69db07SJason Zhu * The "Authentication data" block is |authentication_data_block_size| 79*5b69db07SJason Zhu * bytes long and contains the hash and signature used to authenticate 80*5b69db07SJason Zhu * the vbmeta image. The type of the hash and signature is defined by 81*5b69db07SJason Zhu * the |algorithm_type| field. 82*5b69db07SJason Zhu * 83*5b69db07SJason Zhu * The "Auxiliary data" is |auxiliary_data_block_size| bytes long and 84*5b69db07SJason Zhu * contains the auxiliary data including the public key used to make 85*5b69db07SJason Zhu * the signature and descriptors. 86*5b69db07SJason Zhu * 87*5b69db07SJason Zhu * The public key is at offset |public_key_offset| with size 88*5b69db07SJason Zhu * |public_key_size| in this block. The size of the public key data is 89*5b69db07SJason Zhu * defined by the |algorithm_type| field. The format of the public key 90*5b69db07SJason Zhu * data is described in the |AvbRSAPublicKeyHeader| struct. 91*5b69db07SJason Zhu * 92*5b69db07SJason Zhu * The descriptors starts at |descriptors_offset| from the beginning 93*5b69db07SJason Zhu * of the "Auxiliary Data" block and take up |descriptors_size| 94*5b69db07SJason Zhu * bytes. Each descriptor is stored as a |AvbDescriptor| with tag and 95*5b69db07SJason Zhu * number of bytes following. The number of descriptors can be 96*5b69db07SJason Zhu * determined by walking this data until |descriptors_size| is 97*5b69db07SJason Zhu * exhausted. 98*5b69db07SJason Zhu * 99*5b69db07SJason Zhu * The size of each of the "Authentication data" and "Auxiliary data" 100*5b69db07SJason Zhu * blocks must be divisible by 64. This is to ensure proper alignment. 101*5b69db07SJason Zhu * 102*5b69db07SJason Zhu * Descriptors are free-form blocks stored in a part of the vbmeta 103*5b69db07SJason Zhu * image subject to the same integrity checks as the rest of the 104*5b69db07SJason Zhu * image. See the documentation for |AvbDescriptor| for well-known 105*5b69db07SJason Zhu * descriptors. See avb_descriptor_foreach() for a convenience 106*5b69db07SJason Zhu * function to iterate over descriptors. 107*5b69db07SJason Zhu * 108*5b69db07SJason Zhu * This struct is versioned, see the |required_libavb_version_major| 109*5b69db07SJason Zhu * and |required_libavb_version_minor| fields. This represents the 110*5b69db07SJason Zhu * minimum version of libavb required to verify the header and depends 111*5b69db07SJason Zhu * on the features (e.g. algorithms, descriptors) used. Note that this 112*5b69db07SJason Zhu * may be 1.0 even if generated by an avbtool from 1.4 but where no 113*5b69db07SJason Zhu * features introduced after 1.0 has been used. See the "Versioning 114*5b69db07SJason Zhu * and compatibility" section in the README.md file for more details. 115*5b69db07SJason Zhu * 116*5b69db07SJason Zhu * All fields are stored in network byte order when serialized. To 117*5b69db07SJason Zhu * generate a copy with fields swapped to native byte order, use the 118*5b69db07SJason Zhu * function avb_vbmeta_image_header_to_host_byte_order(). 119*5b69db07SJason Zhu * 120*5b69db07SJason Zhu * Before reading and/or using any of this data, you MUST verify it 121*5b69db07SJason Zhu * using avb_vbmeta_image_verify() and reject it unless it's signed by 122*5b69db07SJason Zhu * a known good public key. 123*5b69db07SJason Zhu */ 124*5b69db07SJason Zhu typedef struct AvbVBMetaImageHeader { 125*5b69db07SJason Zhu /* 0: Four bytes equal to "AVB0" (AVB_MAGIC). */ 126*5b69db07SJason Zhu uint8_t magic[AVB_MAGIC_LEN]; 127*5b69db07SJason Zhu 128*5b69db07SJason Zhu /* 4: The major version of libavb required for this header. */ 129*5b69db07SJason Zhu uint32_t required_libavb_version_major; 130*5b69db07SJason Zhu /* 8: The minor version of libavb required for this header. */ 131*5b69db07SJason Zhu uint32_t required_libavb_version_minor; 132*5b69db07SJason Zhu 133*5b69db07SJason Zhu /* 12: The size of the signature block. */ 134*5b69db07SJason Zhu uint64_t authentication_data_block_size; 135*5b69db07SJason Zhu /* 20: The size of the auxiliary data block. */ 136*5b69db07SJason Zhu uint64_t auxiliary_data_block_size; 137*5b69db07SJason Zhu 138*5b69db07SJason Zhu /* 28: The verification algorithm used, see |AvbAlgorithmType| enum. */ 139*5b69db07SJason Zhu uint32_t algorithm_type; 140*5b69db07SJason Zhu 141*5b69db07SJason Zhu /* 32: Offset into the "Authentication data" block of hash data. */ 142*5b69db07SJason Zhu uint64_t hash_offset; 143*5b69db07SJason Zhu /* 40: Length of the hash data. */ 144*5b69db07SJason Zhu uint64_t hash_size; 145*5b69db07SJason Zhu 146*5b69db07SJason Zhu /* 48: Offset into the "Authentication data" block of signature data. */ 147*5b69db07SJason Zhu uint64_t signature_offset; 148*5b69db07SJason Zhu /* 56: Length of the signature data. */ 149*5b69db07SJason Zhu uint64_t signature_size; 150*5b69db07SJason Zhu 151*5b69db07SJason Zhu /* 64: Offset into the "Auxiliary data" block of public key data. */ 152*5b69db07SJason Zhu uint64_t public_key_offset; 153*5b69db07SJason Zhu /* 72: Length of the public key data. */ 154*5b69db07SJason Zhu uint64_t public_key_size; 155*5b69db07SJason Zhu 156*5b69db07SJason Zhu /* 80: Offset into the "Auxiliary data" block of public key metadata. */ 157*5b69db07SJason Zhu uint64_t public_key_metadata_offset; 158*5b69db07SJason Zhu /* 88: Length of the public key metadata. Must be set to zero if there 159*5b69db07SJason Zhu * is no public key metadata. 160*5b69db07SJason Zhu */ 161*5b69db07SJason Zhu uint64_t public_key_metadata_size; 162*5b69db07SJason Zhu 163*5b69db07SJason Zhu /* 96: Offset into the "Auxiliary data" block of descriptor data. */ 164*5b69db07SJason Zhu uint64_t descriptors_offset; 165*5b69db07SJason Zhu /* 104: Length of descriptor data. */ 166*5b69db07SJason Zhu uint64_t descriptors_size; 167*5b69db07SJason Zhu 168*5b69db07SJason Zhu /* 112: The rollback index which can be used to prevent rollback to 169*5b69db07SJason Zhu * older versions. 170*5b69db07SJason Zhu */ 171*5b69db07SJason Zhu uint64_t rollback_index; 172*5b69db07SJason Zhu 173*5b69db07SJason Zhu /* 120: Flags from the AvbVBMetaImageFlags enumeration. This must be 174*5b69db07SJason Zhu * set to zero if the vbmeta image is not a top-level image. 175*5b69db07SJason Zhu */ 176*5b69db07SJason Zhu uint32_t flags; 177*5b69db07SJason Zhu 178*5b69db07SJason Zhu /* 124: Reserved to ensure |release_string| start on a 16-byte 179*5b69db07SJason Zhu * boundary. Must be set to zeroes. 180*5b69db07SJason Zhu */ 181*5b69db07SJason Zhu uint8_t reserved0[4]; 182*5b69db07SJason Zhu 183*5b69db07SJason Zhu /* 128: The release string from avbtool, e.g. "avbtool 1.0.0" or 184*5b69db07SJason Zhu * "avbtool 1.0.0 xyz_board Git-234abde89". Is guaranteed to be NUL 185*5b69db07SJason Zhu * terminated. Applications must not make assumptions about how this 186*5b69db07SJason Zhu * string is formatted. 187*5b69db07SJason Zhu */ 188*5b69db07SJason Zhu uint8_t release_string[AVB_RELEASE_STRING_SIZE]; 189*5b69db07SJason Zhu 190*5b69db07SJason Zhu /* 176: Padding to ensure struct is size AVB_VBMETA_IMAGE_HEADER_SIZE 191*5b69db07SJason Zhu * bytes. This must be set to zeroes. 192*5b69db07SJason Zhu */ 193*5b69db07SJason Zhu uint8_t reserved[80]; 194*5b69db07SJason Zhu } AVB_ATTR_PACKED AvbVBMetaImageHeader; 195*5b69db07SJason Zhu 196*5b69db07SJason Zhu /* Copies |src| to |dest|, byte-swapping fields in the process. 197*5b69db07SJason Zhu * 198*5b69db07SJason Zhu * Make sure you've verified |src| using avb_vbmeta_image_verify() 199*5b69db07SJason Zhu * before accessing the data and/or using this function. 200*5b69db07SJason Zhu */ 201*5b69db07SJason Zhu void avb_vbmeta_image_header_to_host_byte_order(const AvbVBMetaImageHeader* src, 202*5b69db07SJason Zhu AvbVBMetaImageHeader* dest); 203*5b69db07SJason Zhu 204*5b69db07SJason Zhu /* Return codes used in avb_vbmeta_image_verify(). 205*5b69db07SJason Zhu * 206*5b69db07SJason Zhu * AVB_VBMETA_VERIFY_RESULT_OK is returned if the vbmeta image header 207*5b69db07SJason Zhu * is valid, the hash is correct and the signature is correct. Keep in 208*5b69db07SJason Zhu * mind that you still need to check that you know the public key used 209*5b69db07SJason Zhu * to sign the image, see avb_vbmeta_image_verify() for details. 210*5b69db07SJason Zhu * 211*5b69db07SJason Zhu * AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED is returned if the vbmeta 212*5b69db07SJason Zhu * image header is valid but there is no signature or hash. 213*5b69db07SJason Zhu * 214*5b69db07SJason Zhu * AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER is returned if the 215*5b69db07SJason Zhu * header of the vbmeta image is invalid, for example, invalid magic 216*5b69db07SJason Zhu * or inconsistent data. 217*5b69db07SJason Zhu * 218*5b69db07SJason Zhu * AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION is returned if a) the 219*5b69db07SJason Zhu * vbmeta image requires a minimum version of libavb which exceeds the 220*5b69db07SJason Zhu * version of libavb used; or b) the vbmeta image major version 221*5b69db07SJason Zhu * differs from the major version of libavb in use. 222*5b69db07SJason Zhu * 223*5b69db07SJason Zhu * AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH is returned if the hash 224*5b69db07SJason Zhu * stored in the "Authentication data" block does not match the 225*5b69db07SJason Zhu * calculated hash. 226*5b69db07SJason Zhu * 227*5b69db07SJason Zhu * AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH is returned if the 228*5b69db07SJason Zhu * signature stored in the "Authentication data" block is invalid or 229*5b69db07SJason Zhu * doesn't match the public key stored in the vbmeta image. 230*5b69db07SJason Zhu */ 231*5b69db07SJason Zhu typedef enum { 232*5b69db07SJason Zhu AVB_VBMETA_VERIFY_RESULT_OK, 233*5b69db07SJason Zhu AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED, 234*5b69db07SJason Zhu AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER, 235*5b69db07SJason Zhu AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION, 236*5b69db07SJason Zhu AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH, 237*5b69db07SJason Zhu AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH, 238*5b69db07SJason Zhu } AvbVBMetaVerifyResult; 239*5b69db07SJason Zhu 240*5b69db07SJason Zhu /* Get a textual representation of |result|. */ 241*5b69db07SJason Zhu const char* avb_vbmeta_verify_result_to_string(AvbVBMetaVerifyResult result); 242*5b69db07SJason Zhu 243*5b69db07SJason Zhu /* Checks that vbmeta image at |data| of size |length| is a valid 244*5b69db07SJason Zhu * vbmeta image. The complete contents of the vbmeta image must be 245*5b69db07SJason Zhu * passed in. It's fine if |length| is bigger than the actual image, 246*5b69db07SJason Zhu * typically callers of this function will load the entire contents of 247*5b69db07SJason Zhu * the 'vbmeta_a' or 'vbmeta_b' partition and pass in its length (for 248*5b69db07SJason Zhu * example, 1 MiB). 249*5b69db07SJason Zhu * 250*5b69db07SJason Zhu * See the |AvbVBMetaImageHeader| struct for information about the 251*5b69db07SJason Zhu * three blocks (header, authentication, auxiliary) that make up a 252*5b69db07SJason Zhu * vbmeta image. 253*5b69db07SJason Zhu * 254*5b69db07SJason Zhu * If the function returns |AVB_VBMETA_VERIFY_RESULT_OK| and 255*5b69db07SJason Zhu * |out_public_key_data| is non-NULL, it will be set to point inside 256*5b69db07SJason Zhu * |data| for where the serialized public key data is stored and 257*5b69db07SJason Zhu * |out_public_key_length|, if non-NULL, will be set to the length of 258*5b69db07SJason Zhu * the public key data. If there is no public key in the metadata then 259*5b69db07SJason Zhu * |out_public_key_data| is set to NULL. 260*5b69db07SJason Zhu * 261*5b69db07SJason Zhu * See the |AvbVBMetaVerifyResult| enum for possible return values. 262*5b69db07SJason Zhu * 263*5b69db07SJason Zhu * VERY IMPORTANT: 264*5b69db07SJason Zhu * 265*5b69db07SJason Zhu * 1. Even if |AVB_VBMETA_VERIFY_RESULT_OK| is returned, you still 266*5b69db07SJason Zhu * need to check that the public key embedded in the image 267*5b69db07SJason Zhu * matches a known key! You can use 'avbtool extract_public_key' 268*5b69db07SJason Zhu * to extract the key (at build time, then store it along your 269*5b69db07SJason Zhu * code) and compare it to what is returned in 270*5b69db07SJason Zhu * |out_public_key_data|. 271*5b69db07SJason Zhu * 272*5b69db07SJason Zhu * 2. You need to check the |rollback_index| field against a stored 273*5b69db07SJason Zhu * value in NVRAM and reject the vbmeta image if the value in 274*5b69db07SJason Zhu * NVRAM is bigger than |rollback_index|. You must also update 275*5b69db07SJason Zhu * the value stored in NVRAM to the smallest value of 276*5b69db07SJason Zhu * |rollback_index| field from boot images in all bootable and 277*5b69db07SJason Zhu * authentic slots marked as GOOD. 278*5b69db07SJason Zhu * 279*5b69db07SJason Zhu * This is a low-level function to only verify the vbmeta data - you 280*5b69db07SJason Zhu * are likely looking for avb_slot_verify() instead for verifying 281*5b69db07SJason Zhu * integrity data for a whole set of partitions. 282*5b69db07SJason Zhu */ 283*5b69db07SJason Zhu AvbVBMetaVerifyResult avb_vbmeta_image_verify( 284*5b69db07SJason Zhu const uint8_t* data, 285*5b69db07SJason Zhu size_t length, 286*5b69db07SJason Zhu const uint8_t** out_public_key_data, 287*5b69db07SJason Zhu size_t* out_public_key_length) AVB_ATTR_WARN_UNUSED_RESULT; 288*5b69db07SJason Zhu 289*5b69db07SJason Zhu #ifdef __cplusplus 290*5b69db07SJason Zhu } 291*5b69db07SJason Zhu #endif 292*5b69db07SJason Zhu 293*5b69db07SJason Zhu #endif /* AVB_VBMETA_IMAGE_H_ */ 294