Android AVB 之 AvbVBMetaImageHeader

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),下表中是整个header的构成,理论上整张表格都应该蓝色标注,但为了区分Authentication和Auxiliary的关键数据,所以单独拎出来了

  • Authentication data – variable size,下表中红色字体部分,是Authentication相关的一些数据,比如在vbmeta中,Authentication的offset就是从头开始偏移256个字节,总长度是header中解析出来的Authentication data block size

  • Auxiliary data – variable size,同上,绿色部分是Auxiliary相关的一些数据,仅仅是为了语义上的区分,Auxiliary数据在vbmeta的最后一块,其起始地址是头+256偏移+Authentication data block size,总长度是Auxiliary data block size

示例:

给个详细例子如下(vbmeta.img的二进制显示,前256个字节即为AvbVBMetaImageHeader):

数据描述: 

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

 

Four bytes equal to "AVB0"
(AVB_MAGIC).

 

确认是否是AVB0

4 4

Major version

 
The major version of libavb
 
8 4

Minor version

 
The minor version of libavb
 
12 8 Authentication data block size  
The size of the signature block
 
20 8 Auxiliary data block size  
The size of the auxiliary data block
 
28 4 Algorithm  
The verification algorithm used
see |AvbAlgorithmType| enum
 
32 8 Hash offset  
Offset into the "Authentication data" 
block of hash data.
注意,这里的偏移是相对Authentication block的偏移

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  
Length of the hash data.
48 8

Signature offset

 
Offset into the "Authentication data" 
block of signature data.
同上,注意偏移是相对的。
56 8 Signature size  
Length of the signature data.
64 8 Public key offset  
Offset into the "Auxiliary data" 
block of public key data.

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  
Length of the public key data.
80 8 Public key metadata offset  
Offset into the "Auxiliary data" 
block of public key metadata.
88 8 Public key metadata size  
Length of the public key metadata.

Must be set to zero if there is 
no public key metadata.
96 8 Descriptors offset  
Offset into the "Auxiliary data" 
block of descriptor data.
104 8 Descriptors size  
Length of descriptor data.
112 8 Rollback index  
The rollback index which can be used to 
prevent rollback to older version.
这个是防回滚的index。
 
120 4 Flags  
Flags from the AvbVBMetaImageFlags enumeration.

This must be set to zero if the vbmeta 
image is not a top-level image.
 
124 4 Reserved  
Reserved to ensure |release_string| 
start on a 16-byte
boundary.
Must be set to zeroes.
 
128 48

Release strings

 
The release string from avbtool.
Is guaranteed to be NUL terminated
 
 
176 80 Reserved  
Padding to ensure struct is size 
AVB_VBMETA_IMAGE_HEADER_SIZE
bytes.
This must be set to zeroes.
 

 

 

posted @ 2023-03-24 15:58  xiululu  阅读(334)  评论(0编辑  收藏  举报