Android AVB 之 AvbVBMetaImageHeader

发布时间 2023-03-24 16:04:53作者: xiululu

external/avb/libavb/avb_vbmeta_image.h

struct AvbVBMetaImageHeader:

/* Binary format for header of the vbmeta image.
 *
 * The vbmeta image consists of three blocks:
 *
 *  +-----------------------------------------+
 *  | Header data - fixed size                |
 *  +-----------------------------------------+
 *  | Authentication data - variable size     |
 *  +-----------------------------------------+
 *  | Auxiliary data - variable size          |
 *  +-----------------------------------------+
 *
 * The "Header data" block is described by this struct and is always
 * |AVB_VBMETA_IMAGE_HEADER_SIZE| bytes long.//#define AVB_VBMETA_IMAGE_HEADER_SIZE 256
 *
 * The "Authentication data" block is |authentication_data_block_size|
 * bytes long and contains the hash and signature used to authenticate
 * the vbmeta image. The type of the hash and signature is defined by
 * the |algorithm_type| field.
 *
 * The "Auxiliary data" is |auxiliary_data_block_size| bytes long and
 * contains the auxiliary data including the public key used to make
 * the signature and descriptors.
 *
 * The public key is at offset |public_key_offset| with size
 * |public_key_size| in this block. The size of the public key data is
 * defined by the |algorithm_type| field. The format of the public key
 * data is described in the |AvbRSAPublicKeyHeader| struct.
 *
 * The descriptors starts at |descriptors_offset| from the beginning
 * of the "Auxiliary Data" block and take up |descriptors_size|
 * bytes. Each descriptor is stored as a |AvbDescriptor| with tag and
 * number of bytes following. The number of descriptors can be
 * determined by walking this data until |descriptors_size| is
 * exhausted.
 *
  * The size of each of the "Authentication data" and "Auxiliary data"
  * blocks must be divisible by 64. This is to ensure proper alignment.
  *
  * Descriptors are free-form blocks stored in a part of the vbmeta
  * image subject to the same integrity checks as the rest of the
  * image. See the documentation for |AvbDescriptor| for well-known
  * descriptors. See avb_descriptor_foreach() for a convenience
  * function to iterate over descriptors.
  *
  * This struct is versioned, see the |required_libavb_version_major|
  * and |required_libavb_version_minor| fields. This represents the
  * minimum version of libavb required to verify the header and depends
  * on the features (e.g. algorithms, descriptors) used. Note that this
  * may be 1.0 even if generated by an avbtool from 1.4 but where no
  * features introduced after 1.0 has been used. See the "Versioning
  * and compatibility" section in the README.md file for more details.
  *
  * All fields are stored in network byte order when serialized. To
  * generate a copy with fields swapped to native byte order, use the
  * function avb_vbmeta_image_header_to_host_byte_order().
  *
  * Before reading and/or using any of this data, you MUST verify it
  * using avb_vbmeta_image_verify() and reject it unless it's signed by
  * a known good public key.
  */
 typedef struct AvbVBMetaImageHeader {
   /*   0: Four bytes equal to "AVB0" (AVB_MAGIC). */
   uint8_t magic[AVB_MAGIC_LEN];
 
   /*   4: The major version of libavb required for this header. */
   uint32_t required_libavb_version_major;
   /*   8: The minor version of libavb required for this header. */
   uint32_t required_libavb_version_minor;
 
   /*  12: The size of the signature block. */
   uint64_t authentication_data_block_size;
   /*  20: The size of the auxiliary data block. */
   uint64_t auxiliary_data_block_size;
 
   /*  28: The verification algorithm used, see |AvbAlgorithmType| enum. */
   uint32_t algorithm_type;
 
   /*  32: Offset into the "Authentication data" block of hash data. */
   uint64_t hash_offset;
   /*  40: Length of the hash data. */
   uint64_t hash_size;
 
   /*  48: Offset into the "Authentication data" block of signature data. */
   uint64_t signature_offset;
   /*  56: Length of the signature data. */
   uint64_t signature_size;
 
   /*  64: Offset into the "Auxiliary data" block of public key data. */
   uint64_t public_key_offset;
   /*  72: Length of the public key data. */
   uint64_t public_key_size;
 
   /*  80: Offset into the "Auxiliary data" block of public key metadata. */
   uint64_t public_key_metadata_offset;
   /*  88: Length of the public key metadata. Must be set to zero if there
    *  is no public key metadata.
    */
   uint64_t public_key_metadata_size;
 
   /*  96: Offset into the "Auxiliary data" block of descriptor data. */
   uint64_t descriptors_offset;
   /* 104: Length of descriptor data. */
   uint64_t descriptors_size;
 
   /* 112: The rollback index which can be used to prevent rollback to
    *  older versions.
    */
   uint64_t rollback_index;
 
   /* 120: Flags from the AvbVBMetaImageFlags enumeration. This must be
    * set to zero if the vbmeta image is not a top-level image.
    */
   uint32_t flags;
 
   /* 124: Reserved to ensure |release_string| start on a 16-byte
    * boundary. Must be set to zeroes.
    */
   uint8_t reserved0[4];
 
   /* 128: The release string from avbtool, e.g. "avbtool 1.0.0" or
    * "avbtool 1.0.0 xyz_board Git-234abde89". Is guaranteed to be NUL
    * terminated. Applications must not make assumptions about how this
    * string is formatted.
    */
   uint8_t release_string[AVB_RELEASE_STRING_SIZE];
 
   /* 176: Padding to ensure struct is size AVB_VBMETA_IMAGE_HEADER_SIZE
    * bytes. This must be set to zeroes.
    */
   uint8_t reserved[80];
 } AVB_ATTR_PACKED AvbVBMetaImageHeader;

细节说明如下表,其中:

  • Header data – fixed size (256 bytes)

  • Authentication data – variable size

  • Auxiliary data – variable size

offset size(bytes) data 部分关键校验过程
0 4 Magic

确认是否是AVB0

4 4

Major version

 
8 4

Minor version

 
12 8 Authentication data block size  
20 8 Auxiliary data block size  
28 4 Algorithm  
32 8 Hash offset

avb_vbmeta_image.c中avb_vbmeta_image_verify()函数

avb_sha256_init(&sha256_ctx);
avb_sha256_update(
&sha256_ctx, header_block, sizeof(AvbVBMetaImageHeader));
avb_sha256_update(
&sha256_ctx, auxiliary_block, h.auxiliary_data_block_size);
computed_hash = avb_sha256_final(&sha256_ctx);

计算header+auxiliary部分的hash值,与vbmeta中保存的hash值做比较

if (avb_safe_memcmp(authentication_block + h.hash_offset,
computed_hash, h.hash_size) != 0) {//vbmeta中存储的hash值
avb_error("Hash does not match!\n");
ret = AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH;
goto out;
}

40 8 Hash size
48 8

Signature offset

56 8 Signature size
64 8 Public key offset

avb_vbmeta_image.c中avb_vbmeta_image_verify()函数

/*Verify a RSA PKCS1.5 signature against an expected hash*/

verification_result =
avb_rsa_verify(auxiliary_block + h.public_key_offset, h.public_key_size,/*public key*/
authentication_block + h.signature_offset, h.signature_size,/*signature*/
authentication_block + h.hash_offset,h.hash_size,
algorithm->padding, algorithm→padding_len);

avb_rsa_verify中RSA校验hash通过之后,获取oem public key

if (out_public_key_data != NULL) {
*out_public_key_data = auxiliary_block + h.public_key_offset;
}
if (out_public_key_length != NULL) {
*out_public_key_length = h.public_key_size;
}

获取到的oem public key跟OEMPublicKey.h中存储的public key相比较,相同则vbmeta可信

72 8 Public key size
80 8 Public key metadata offset
88 8 Public key metadata size
96 8 Descriptors offset
104 8 Descriptors size
112 8 Rollback index  
120 4 Flags  
124 4 Reserved  
128 48

Release strings

 
176 80 Reserved