15b69db07SJason Zhu /* 25b69db07SJason Zhu * Copyright (C) 2016 The Android Open Source Project 35b69db07SJason Zhu * 45b69db07SJason Zhu * Permission is hereby granted, free of charge, to any person 55b69db07SJason Zhu * obtaining a copy of this software and associated documentation 65b69db07SJason Zhu * files (the "Software"), to deal in the Software without 75b69db07SJason Zhu * restriction, including without limitation the rights to use, copy, 85b69db07SJason Zhu * modify, merge, publish, distribute, sublicense, and/or sell copies 95b69db07SJason Zhu * of the Software, and to permit persons to whom the Software is 105b69db07SJason Zhu * furnished to do so, subject to the following conditions: 115b69db07SJason Zhu * 125b69db07SJason Zhu * The above copyright notice and this permission notice shall be 135b69db07SJason Zhu * included in all copies or substantial portions of the Software. 145b69db07SJason Zhu * 155b69db07SJason Zhu * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 165b69db07SJason Zhu * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 175b69db07SJason Zhu * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 185b69db07SJason Zhu * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 195b69db07SJason Zhu * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 205b69db07SJason Zhu * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 215b69db07SJason Zhu * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 225b69db07SJason Zhu * SOFTWARE. 235b69db07SJason Zhu */ 245b69db07SJason Zhu 255b69db07SJason Zhu /* 265b69db07SJason Zhu #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION) 275b69db07SJason Zhu #error "Never include this file directly, include libavb.h instead." 285b69db07SJason Zhu #endif 295b69db07SJason Zhu */ 305b69db07SJason Zhu 315b69db07SJason Zhu #ifndef AVB_VBMETA_IMAGE_H_ 325b69db07SJason Zhu #define AVB_VBMETA_IMAGE_H_ 335b69db07SJason Zhu 345b69db07SJason Zhu #include <android_avb/avb_sysdeps.h> 355b69db07SJason Zhu 365b69db07SJason Zhu #ifdef __cplusplus 375b69db07SJason Zhu extern "C" { 385b69db07SJason Zhu #endif 395b69db07SJason Zhu 40*37a7bc39SJason Zhu #include "avb_crypto.h" 41*37a7bc39SJason Zhu #include "avb_descriptor.h" 425b69db07SJason Zhu 435b69db07SJason Zhu /* Size of the vbmeta image header. */ 445b69db07SJason Zhu #define AVB_VBMETA_IMAGE_HEADER_SIZE 256 455b69db07SJason Zhu 465b69db07SJason Zhu /* Magic for the vbmeta image header. */ 475b69db07SJason Zhu #define AVB_MAGIC "AVB0" 485b69db07SJason Zhu #define AVB_MAGIC_LEN 4 495b69db07SJason Zhu 505b69db07SJason Zhu /* Maximum size of the release string including the terminating NUL byte. */ 515b69db07SJason Zhu #define AVB_RELEASE_STRING_SIZE 48 525b69db07SJason Zhu 535b69db07SJason Zhu /* Flags for the vbmeta image. 545b69db07SJason Zhu * 555b69db07SJason Zhu * AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED: If this flag is set, 565b69db07SJason Zhu * hashtree image verification will be disabled. 57*37a7bc39SJason Zhu * 58*37a7bc39SJason Zhu * AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED: If this flag is set, 59*37a7bc39SJason Zhu * verification will be disabled and descriptors will not be parsed. 605b69db07SJason Zhu */ 615b69db07SJason Zhu typedef enum { 625b69db07SJason Zhu AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED = (1 << 0), 635b69db07SJason Zhu AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED = (1 << 1) 645b69db07SJason Zhu } AvbVBMetaImageFlags; 655b69db07SJason Zhu 665b69db07SJason Zhu /* Binary format for header of the vbmeta image. 675b69db07SJason Zhu * 685b69db07SJason Zhu * The vbmeta image consists of three blocks: 695b69db07SJason Zhu * 705b69db07SJason Zhu * +-----------------------------------------+ 715b69db07SJason Zhu * | Header data - fixed size | 725b69db07SJason Zhu * +-----------------------------------------+ 735b69db07SJason Zhu * | Authentication data - variable size | 745b69db07SJason Zhu * +-----------------------------------------+ 755b69db07SJason Zhu * | Auxiliary data - variable size | 765b69db07SJason Zhu * +-----------------------------------------+ 775b69db07SJason Zhu * 785b69db07SJason Zhu * The "Header data" block is described by this struct and is always 795b69db07SJason Zhu * |AVB_VBMETA_IMAGE_HEADER_SIZE| bytes long. 805b69db07SJason Zhu * 815b69db07SJason Zhu * The "Authentication data" block is |authentication_data_block_size| 825b69db07SJason Zhu * bytes long and contains the hash and signature used to authenticate 835b69db07SJason Zhu * the vbmeta image. The type of the hash and signature is defined by 845b69db07SJason Zhu * the |algorithm_type| field. 855b69db07SJason Zhu * 865b69db07SJason Zhu * The "Auxiliary data" is |auxiliary_data_block_size| bytes long and 875b69db07SJason Zhu * contains the auxiliary data including the public key used to make 885b69db07SJason Zhu * the signature and descriptors. 895b69db07SJason Zhu * 905b69db07SJason Zhu * The public key is at offset |public_key_offset| with size 915b69db07SJason Zhu * |public_key_size| in this block. The size of the public key data is 925b69db07SJason Zhu * defined by the |algorithm_type| field. The format of the public key 935b69db07SJason Zhu * data is described in the |AvbRSAPublicKeyHeader| struct. 945b69db07SJason Zhu * 955b69db07SJason Zhu * The descriptors starts at |descriptors_offset| from the beginning 965b69db07SJason Zhu * of the "Auxiliary Data" block and take up |descriptors_size| 975b69db07SJason Zhu * bytes. Each descriptor is stored as a |AvbDescriptor| with tag and 985b69db07SJason Zhu * number of bytes following. The number of descriptors can be 995b69db07SJason Zhu * determined by walking this data until |descriptors_size| is 1005b69db07SJason Zhu * exhausted. 1015b69db07SJason Zhu * 1025b69db07SJason Zhu * The size of each of the "Authentication data" and "Auxiliary data" 1035b69db07SJason Zhu * blocks must be divisible by 64. This is to ensure proper alignment. 1045b69db07SJason Zhu * 1055b69db07SJason Zhu * Descriptors are free-form blocks stored in a part of the vbmeta 1065b69db07SJason Zhu * image subject to the same integrity checks as the rest of the 1075b69db07SJason Zhu * image. See the documentation for |AvbDescriptor| for well-known 1085b69db07SJason Zhu * descriptors. See avb_descriptor_foreach() for a convenience 1095b69db07SJason Zhu * function to iterate over descriptors. 1105b69db07SJason Zhu * 1115b69db07SJason Zhu * This struct is versioned, see the |required_libavb_version_major| 1125b69db07SJason Zhu * and |required_libavb_version_minor| fields. This represents the 1135b69db07SJason Zhu * minimum version of libavb required to verify the header and depends 1145b69db07SJason Zhu * on the features (e.g. algorithms, descriptors) used. Note that this 1155b69db07SJason Zhu * may be 1.0 even if generated by an avbtool from 1.4 but where no 1165b69db07SJason Zhu * features introduced after 1.0 has been used. See the "Versioning 1175b69db07SJason Zhu * and compatibility" section in the README.md file for more details. 1185b69db07SJason Zhu * 1195b69db07SJason Zhu * All fields are stored in network byte order when serialized. To 1205b69db07SJason Zhu * generate a copy with fields swapped to native byte order, use the 1215b69db07SJason Zhu * function avb_vbmeta_image_header_to_host_byte_order(). 1225b69db07SJason Zhu * 1235b69db07SJason Zhu * Before reading and/or using any of this data, you MUST verify it 1245b69db07SJason Zhu * using avb_vbmeta_image_verify() and reject it unless it's signed by 1255b69db07SJason Zhu * a known good public key. 1265b69db07SJason Zhu */ 1275b69db07SJason Zhu typedef struct AvbVBMetaImageHeader { 1285b69db07SJason Zhu /* 0: Four bytes equal to "AVB0" (AVB_MAGIC). */ 1295b69db07SJason Zhu uint8_t magic[AVB_MAGIC_LEN]; 1305b69db07SJason Zhu 1315b69db07SJason Zhu /* 4: The major version of libavb required for this header. */ 1325b69db07SJason Zhu uint32_t required_libavb_version_major; 1335b69db07SJason Zhu /* 8: The minor version of libavb required for this header. */ 1345b69db07SJason Zhu uint32_t required_libavb_version_minor; 1355b69db07SJason Zhu 1365b69db07SJason Zhu /* 12: The size of the signature block. */ 1375b69db07SJason Zhu uint64_t authentication_data_block_size; 1385b69db07SJason Zhu /* 20: The size of the auxiliary data block. */ 1395b69db07SJason Zhu uint64_t auxiliary_data_block_size; 1405b69db07SJason Zhu 1415b69db07SJason Zhu /* 28: The verification algorithm used, see |AvbAlgorithmType| enum. */ 1425b69db07SJason Zhu uint32_t algorithm_type; 1435b69db07SJason Zhu 1445b69db07SJason Zhu /* 32: Offset into the "Authentication data" block of hash data. */ 1455b69db07SJason Zhu uint64_t hash_offset; 1465b69db07SJason Zhu /* 40: Length of the hash data. */ 1475b69db07SJason Zhu uint64_t hash_size; 1485b69db07SJason Zhu 1495b69db07SJason Zhu /* 48: Offset into the "Authentication data" block of signature data. */ 1505b69db07SJason Zhu uint64_t signature_offset; 1515b69db07SJason Zhu /* 56: Length of the signature data. */ 1525b69db07SJason Zhu uint64_t signature_size; 1535b69db07SJason Zhu 1545b69db07SJason Zhu /* 64: Offset into the "Auxiliary data" block of public key data. */ 1555b69db07SJason Zhu uint64_t public_key_offset; 1565b69db07SJason Zhu /* 72: Length of the public key data. */ 1575b69db07SJason Zhu uint64_t public_key_size; 1585b69db07SJason Zhu 1595b69db07SJason Zhu /* 80: Offset into the "Auxiliary data" block of public key metadata. */ 1605b69db07SJason Zhu uint64_t public_key_metadata_offset; 1615b69db07SJason Zhu /* 88: Length of the public key metadata. Must be set to zero if there 1625b69db07SJason Zhu * is no public key metadata. 1635b69db07SJason Zhu */ 1645b69db07SJason Zhu uint64_t public_key_metadata_size; 1655b69db07SJason Zhu 1665b69db07SJason Zhu /* 96: Offset into the "Auxiliary data" block of descriptor data. */ 1675b69db07SJason Zhu uint64_t descriptors_offset; 1685b69db07SJason Zhu /* 104: Length of descriptor data. */ 1695b69db07SJason Zhu uint64_t descriptors_size; 1705b69db07SJason Zhu 1715b69db07SJason Zhu /* 112: The rollback index which can be used to prevent rollback to 1725b69db07SJason Zhu * older versions. 1735b69db07SJason Zhu */ 1745b69db07SJason Zhu uint64_t rollback_index; 1755b69db07SJason Zhu 1765b69db07SJason Zhu /* 120: Flags from the AvbVBMetaImageFlags enumeration. This must be 1775b69db07SJason Zhu * set to zero if the vbmeta image is not a top-level image. 1785b69db07SJason Zhu */ 1795b69db07SJason Zhu uint32_t flags; 1805b69db07SJason Zhu 1815b69db07SJason Zhu /* 124: Reserved to ensure |release_string| start on a 16-byte 1825b69db07SJason Zhu * boundary. Must be set to zeroes. 1835b69db07SJason Zhu */ 1845b69db07SJason Zhu uint8_t reserved0[4]; 1855b69db07SJason Zhu 1865b69db07SJason Zhu /* 128: The release string from avbtool, e.g. "avbtool 1.0.0" or 1875b69db07SJason Zhu * "avbtool 1.0.0 xyz_board Git-234abde89". Is guaranteed to be NUL 1885b69db07SJason Zhu * terminated. Applications must not make assumptions about how this 1895b69db07SJason Zhu * string is formatted. 1905b69db07SJason Zhu */ 1915b69db07SJason Zhu uint8_t release_string[AVB_RELEASE_STRING_SIZE]; 1925b69db07SJason Zhu 1935b69db07SJason Zhu /* 176: Padding to ensure struct is size AVB_VBMETA_IMAGE_HEADER_SIZE 1945b69db07SJason Zhu * bytes. This must be set to zeroes. 1955b69db07SJason Zhu */ 1965b69db07SJason Zhu uint8_t reserved[80]; 1975b69db07SJason Zhu } AVB_ATTR_PACKED AvbVBMetaImageHeader; 1985b69db07SJason Zhu 1995b69db07SJason Zhu /* Copies |src| to |dest|, byte-swapping fields in the process. 2005b69db07SJason Zhu * 2015b69db07SJason Zhu * Make sure you've verified |src| using avb_vbmeta_image_verify() 2025b69db07SJason Zhu * before accessing the data and/or using this function. 2035b69db07SJason Zhu */ 2045b69db07SJason Zhu void avb_vbmeta_image_header_to_host_byte_order(const AvbVBMetaImageHeader* src, 2055b69db07SJason Zhu AvbVBMetaImageHeader* dest); 2065b69db07SJason Zhu 2075b69db07SJason Zhu /* Return codes used in avb_vbmeta_image_verify(). 2085b69db07SJason Zhu * 2095b69db07SJason Zhu * AVB_VBMETA_VERIFY_RESULT_OK is returned if the vbmeta image header 2105b69db07SJason Zhu * is valid, the hash is correct and the signature is correct. Keep in 2115b69db07SJason Zhu * mind that you still need to check that you know the public key used 2125b69db07SJason Zhu * to sign the image, see avb_vbmeta_image_verify() for details. 2135b69db07SJason Zhu * 2145b69db07SJason Zhu * AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED is returned if the vbmeta 2155b69db07SJason Zhu * image header is valid but there is no signature or hash. 2165b69db07SJason Zhu * 2175b69db07SJason Zhu * AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER is returned if the 2185b69db07SJason Zhu * header of the vbmeta image is invalid, for example, invalid magic 2195b69db07SJason Zhu * or inconsistent data. 2205b69db07SJason Zhu * 2215b69db07SJason Zhu * AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION is returned if a) the 2225b69db07SJason Zhu * vbmeta image requires a minimum version of libavb which exceeds the 2235b69db07SJason Zhu * version of libavb used; or b) the vbmeta image major version 2245b69db07SJason Zhu * differs from the major version of libavb in use. 2255b69db07SJason Zhu * 2265b69db07SJason Zhu * AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH is returned if the hash 2275b69db07SJason Zhu * stored in the "Authentication data" block does not match the 2285b69db07SJason Zhu * calculated hash. 2295b69db07SJason Zhu * 2305b69db07SJason Zhu * AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH is returned if the 2315b69db07SJason Zhu * signature stored in the "Authentication data" block is invalid or 2325b69db07SJason Zhu * doesn't match the public key stored in the vbmeta image. 2335b69db07SJason Zhu */ 2345b69db07SJason Zhu typedef enum { 2355b69db07SJason Zhu AVB_VBMETA_VERIFY_RESULT_OK, 2365b69db07SJason Zhu AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED, 2375b69db07SJason Zhu AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER, 2385b69db07SJason Zhu AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION, 2395b69db07SJason Zhu AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH, 2405b69db07SJason Zhu AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH, 2415b69db07SJason Zhu } AvbVBMetaVerifyResult; 2425b69db07SJason Zhu 2435b69db07SJason Zhu /* Get a textual representation of |result|. */ 2445b69db07SJason Zhu const char* avb_vbmeta_verify_result_to_string(AvbVBMetaVerifyResult result); 2455b69db07SJason Zhu 2465b69db07SJason Zhu /* Checks that vbmeta image at |data| of size |length| is a valid 2475b69db07SJason Zhu * vbmeta image. The complete contents of the vbmeta image must be 2485b69db07SJason Zhu * passed in. It's fine if |length| is bigger than the actual image, 2495b69db07SJason Zhu * typically callers of this function will load the entire contents of 2505b69db07SJason Zhu * the 'vbmeta_a' or 'vbmeta_b' partition and pass in its length (for 2515b69db07SJason Zhu * example, 1 MiB). 2525b69db07SJason Zhu * 2535b69db07SJason Zhu * See the |AvbVBMetaImageHeader| struct for information about the 2545b69db07SJason Zhu * three blocks (header, authentication, auxiliary) that make up a 2555b69db07SJason Zhu * vbmeta image. 2565b69db07SJason Zhu * 2575b69db07SJason Zhu * If the function returns |AVB_VBMETA_VERIFY_RESULT_OK| and 2585b69db07SJason Zhu * |out_public_key_data| is non-NULL, it will be set to point inside 2595b69db07SJason Zhu * |data| for where the serialized public key data is stored and 2605b69db07SJason Zhu * |out_public_key_length|, if non-NULL, will be set to the length of 2615b69db07SJason Zhu * the public key data. If there is no public key in the metadata then 2625b69db07SJason Zhu * |out_public_key_data| is set to NULL. 2635b69db07SJason Zhu * 2645b69db07SJason Zhu * See the |AvbVBMetaVerifyResult| enum for possible return values. 2655b69db07SJason Zhu * 2665b69db07SJason Zhu * VERY IMPORTANT: 2675b69db07SJason Zhu * 2685b69db07SJason Zhu * 1. Even if |AVB_VBMETA_VERIFY_RESULT_OK| is returned, you still 2695b69db07SJason Zhu * need to check that the public key embedded in the image 2705b69db07SJason Zhu * matches a known key! You can use 'avbtool extract_public_key' 2715b69db07SJason Zhu * to extract the key (at build time, then store it along your 2725b69db07SJason Zhu * code) and compare it to what is returned in 2735b69db07SJason Zhu * |out_public_key_data|. 2745b69db07SJason Zhu * 2755b69db07SJason Zhu * 2. You need to check the |rollback_index| field against a stored 2765b69db07SJason Zhu * value in NVRAM and reject the vbmeta image if the value in 2775b69db07SJason Zhu * NVRAM is bigger than |rollback_index|. You must also update 2785b69db07SJason Zhu * the value stored in NVRAM to the smallest value of 2795b69db07SJason Zhu * |rollback_index| field from boot images in all bootable and 2805b69db07SJason Zhu * authentic slots marked as GOOD. 2815b69db07SJason Zhu * 2825b69db07SJason Zhu * This is a low-level function to only verify the vbmeta data - you 2835b69db07SJason Zhu * are likely looking for avb_slot_verify() instead for verifying 2845b69db07SJason Zhu * integrity data for a whole set of partitions. 2855b69db07SJason Zhu */ 2865b69db07SJason Zhu AvbVBMetaVerifyResult avb_vbmeta_image_verify( 2875b69db07SJason Zhu const uint8_t* data, 2885b69db07SJason Zhu size_t length, 2895b69db07SJason Zhu const uint8_t** out_public_key_data, 2905b69db07SJason Zhu size_t* out_public_key_length) AVB_ATTR_WARN_UNUSED_RESULT; 2915b69db07SJason Zhu 2925b69db07SJason Zhu #ifdef __cplusplus 2935b69db07SJason Zhu } 2945b69db07SJason Zhu #endif 2955b69db07SJason Zhu 2965b69db07SJason Zhu #endif /* AVB_VBMETA_IMAGE_H_ */ 297