xref: /aosp_15_r20/external/avb/libavb/avb_hash_descriptor.h (revision d289c2ba6de359471b23d594623b906876bc48a0)
1*d289c2baSAndroid Build Coastguard Worker /*
2*d289c2baSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*d289c2baSAndroid Build Coastguard Worker  *
4*d289c2baSAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person
5*d289c2baSAndroid Build Coastguard Worker  * obtaining a copy of this software and associated documentation
6*d289c2baSAndroid Build Coastguard Worker  * files (the "Software"), to deal in the Software without
7*d289c2baSAndroid Build Coastguard Worker  * restriction, including without limitation the rights to use, copy,
8*d289c2baSAndroid Build Coastguard Worker  * modify, merge, publish, distribute, sublicense, and/or sell copies
9*d289c2baSAndroid Build Coastguard Worker  * of the Software, and to permit persons to whom the Software is
10*d289c2baSAndroid Build Coastguard Worker  * furnished to do so, subject to the following conditions:
11*d289c2baSAndroid Build Coastguard Worker  *
12*d289c2baSAndroid Build Coastguard Worker  * The above copyright notice and this permission notice shall be
13*d289c2baSAndroid Build Coastguard Worker  * included in all copies or substantial portions of the Software.
14*d289c2baSAndroid Build Coastguard Worker  *
15*d289c2baSAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*d289c2baSAndroid Build Coastguard Worker  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*d289c2baSAndroid Build Coastguard Worker  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*d289c2baSAndroid Build Coastguard Worker  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*d289c2baSAndroid Build Coastguard Worker  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*d289c2baSAndroid Build Coastguard Worker  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*d289c2baSAndroid Build Coastguard Worker  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*d289c2baSAndroid Build Coastguard Worker  * SOFTWARE.
23*d289c2baSAndroid Build Coastguard Worker  */
24*d289c2baSAndroid Build Coastguard Worker 
25*d289c2baSAndroid Build Coastguard Worker #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
26*d289c2baSAndroid Build Coastguard Worker #error "Never include this file directly, include libavb.h instead."
27*d289c2baSAndroid Build Coastguard Worker #endif
28*d289c2baSAndroid Build Coastguard Worker 
29*d289c2baSAndroid Build Coastguard Worker #ifndef AVB_HASH_DESCRIPTOR_H_
30*d289c2baSAndroid Build Coastguard Worker #define AVB_HASH_DESCRIPTOR_H_
31*d289c2baSAndroid Build Coastguard Worker 
32*d289c2baSAndroid Build Coastguard Worker #include "avb_descriptor.h"
33*d289c2baSAndroid Build Coastguard Worker 
34*d289c2baSAndroid Build Coastguard Worker #ifdef __cplusplus
35*d289c2baSAndroid Build Coastguard Worker extern "C" {
36*d289c2baSAndroid Build Coastguard Worker #endif
37*d289c2baSAndroid Build Coastguard Worker 
38*d289c2baSAndroid Build Coastguard Worker /* Flags for hash descriptors.
39*d289c2baSAndroid Build Coastguard Worker  *
40*d289c2baSAndroid Build Coastguard Worker  * AVB_HASH_DESCRIPTOR_FLAGS_DO_NOT_USE_AB: Do not apply the default A/B
41*d289c2baSAndroid Build Coastguard Worker  *   partition logic to this partition. This is intentionally a negative boolean
42*d289c2baSAndroid Build Coastguard Worker  *   because A/B should be both the default and most used in practice.
43*d289c2baSAndroid Build Coastguard Worker  */
44*d289c2baSAndroid Build Coastguard Worker typedef enum {
45*d289c2baSAndroid Build Coastguard Worker   AVB_HASH_DESCRIPTOR_FLAGS_DO_NOT_USE_AB = (1 << 0),
46*d289c2baSAndroid Build Coastguard Worker } AvbHashDescriptorFlags;
47*d289c2baSAndroid Build Coastguard Worker 
48*d289c2baSAndroid Build Coastguard Worker /* A descriptor containing information about hash for an image.
49*d289c2baSAndroid Build Coastguard Worker  *
50*d289c2baSAndroid Build Coastguard Worker  * This descriptor is typically used for boot partitions to verify the
51*d289c2baSAndroid Build Coastguard Worker  * entire kernel+initramfs image before executing it.
52*d289c2baSAndroid Build Coastguard Worker  *
53*d289c2baSAndroid Build Coastguard Worker  * Following this struct are |partition_name_len| bytes of the
54*d289c2baSAndroid Build Coastguard Worker  * partition name (UTF-8 encoded), |salt_len| bytes of salt, and then
55*d289c2baSAndroid Build Coastguard Worker  * |digest_len| bytes of the digest.
56*d289c2baSAndroid Build Coastguard Worker  *
57*d289c2baSAndroid Build Coastguard Worker  * The |reserved| field is for future expansion and must be set to NUL
58*d289c2baSAndroid Build Coastguard Worker  * bytes.
59*d289c2baSAndroid Build Coastguard Worker  *
60*d289c2baSAndroid Build Coastguard Worker  * Changes in v1.1:
61*d289c2baSAndroid Build Coastguard Worker  *   - flags field is added which supports AVB_HASH_DESCRIPTOR_FLAGS_USE_AB
62*d289c2baSAndroid Build Coastguard Worker  *   - digest_len may be zero, which indicates the use of a persistent digest
63*d289c2baSAndroid Build Coastguard Worker  */
64*d289c2baSAndroid Build Coastguard Worker typedef struct AvbHashDescriptor {
65*d289c2baSAndroid Build Coastguard Worker   AvbDescriptor parent_descriptor;
66*d289c2baSAndroid Build Coastguard Worker   uint64_t image_size;
67*d289c2baSAndroid Build Coastguard Worker   uint8_t hash_algorithm[32];
68*d289c2baSAndroid Build Coastguard Worker   uint32_t partition_name_len;
69*d289c2baSAndroid Build Coastguard Worker   uint32_t salt_len;
70*d289c2baSAndroid Build Coastguard Worker   uint32_t digest_len;
71*d289c2baSAndroid Build Coastguard Worker   uint32_t flags;
72*d289c2baSAndroid Build Coastguard Worker   uint8_t reserved[60];
73*d289c2baSAndroid Build Coastguard Worker } AVB_ATTR_PACKED AvbHashDescriptor;
74*d289c2baSAndroid Build Coastguard Worker 
75*d289c2baSAndroid Build Coastguard Worker /* Copies |src| to |dest| and validates, byte-swapping fields in the
76*d289c2baSAndroid Build Coastguard Worker  * process if needed. Returns true if valid, false if invalid.
77*d289c2baSAndroid Build Coastguard Worker  *
78*d289c2baSAndroid Build Coastguard Worker  * Data following the struct is not validated nor copied.
79*d289c2baSAndroid Build Coastguard Worker  */
80*d289c2baSAndroid Build Coastguard Worker bool avb_hash_descriptor_validate_and_byteswap(const AvbHashDescriptor* src,
81*d289c2baSAndroid Build Coastguard Worker                                                AvbHashDescriptor* dest)
82*d289c2baSAndroid Build Coastguard Worker     AVB_ATTR_WARN_UNUSED_RESULT;
83*d289c2baSAndroid Build Coastguard Worker 
84*d289c2baSAndroid Build Coastguard Worker #ifdef __cplusplus
85*d289c2baSAndroid Build Coastguard Worker }
86*d289c2baSAndroid Build Coastguard Worker #endif
87*d289c2baSAndroid Build Coastguard Worker 
88*d289c2baSAndroid Build Coastguard Worker #endif /* AVB_HASH_DESCRIPTOR_H_ */
89