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