1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #ifdef AVB_INSIDE_LIBAVB_AFTL_H
26 #error "You can't include avb_aftl_util.h in the public header libavb_aftl.h."
27 #endif
28 
29 #ifndef AVB_COMPILATION
30 #error "Never include this file, it may only be used from internal avb code."
31 #endif
32 
33 #ifndef AVB_AFTL_UTIL_H_
34 #define AVB_AFTL_UTIL_H_
35 
36 #include "avb_aftl_types.h"
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #define AVB_AFTL_MAGIC 0x4c544641
43 #define avb_aftl_member_size(type, member) sizeof(((type*)0)->member)
44 
45 /* Performs a SHA256 hash operation on data. */
46 bool avb_aftl_sha256(
47     uint8_t* data,                     /* Data to be hashed. */
48     uint64_t length,                   /* Size of data. */
49     uint8_t hash[AVB_AFTL_HASH_SIZE]); /* Resulting SHA256 hash. */
50 
51 /* Calculates a SHA256 hash of the TrillianLogRootDescriptor in icp_entry. */
52 bool avb_aftl_hash_log_root_descriptor(
53     AftlIcpEntry* icp_entry, /* The icp_entry containing the descriptor. */
54     uint8_t* hash);          /* The resulting hash of the descriptor data. */
55 
56 /* RFC 6962 Hashing function for leaves of a Merkle tree. */
57 bool avb_aftl_rfc6962_hash_leaf(
58     uint8_t* leaf,      /* The Merkle tree leaf data to be hashed. */
59     uint64_t leaf_size, /* Size of the leaf data. */
60     uint8_t* hash);     /* Resulting RFC 6962 hash of the leaf data. */
61 
62 /* Computes an inner hash as detailed by https://tools.ietf.org/html/rfc6962. */
63 bool avb_aftl_rfc6962_hash_children(
64     uint8_t* left_child,       /* The left child node data. */
65     uint64_t left_child_size,  /* Size of the left child node data. */
66     uint8_t* right_child,      /* The right child node data. */
67     uint64_t right_child_size, /* Size of the right child node data. */
68     uint8_t
69         hash[AVB_AFTL_HASH_SIZE]); /* Resulting RFC 6962 hash of the children.*/
70 
71 /* Computes a subtree hash along the left-side tree border. */
72 bool avb_aftl_chain_border_right(
73     uint8_t* seed,              /* Data containing the starting hash. */
74     uint64_t seed_size,         /* Size of the starting hash data. */
75     uint8_t* proof,             /* The hashes in the inclusion proof. */
76     uint32_t proof_entry_count, /* Number of inclusion proof entries. */
77     uint8_t* hash);             /* Resulting subtree hash. */
78 
79 /* Computes a subtree hash on or below the tree's right border. */
80 bool avb_aftl_chain_inner(
81     uint8_t* seed,              /* Data containing the starting hash. */
82     uint64_t seed_size,         /* Size of the starting hash data. */
83     uint8_t* proof,             /* The hashes in the inclusion proof. */
84     uint32_t proof_entry_count, /* Number of inclusion proof entries. */
85     uint64_t leaf_index,        /* The current Merkle tree leaf index. */
86     uint8_t* hash);             /* Resulting subtree hash. */
87 
88 /* Counts leading zeros. Used in Merkle tree hash validation .*/
89 unsigned int avb_aftl_count_leading_zeros(
90     uint64_t val); /* Value to count leading zeros of. */
91 
92 /* Calculates the expected Merkle tree hash. */
93 bool avb_aftl_root_from_icp(
94     uint64_t leaf_index,                 /* The leaf index in the Merkle tree.*/
95     uint64_t tree_size,                  /* The size of the Merkle tree. */
96     uint8_t proof[][AVB_AFTL_HASH_SIZE], /* Inclusion proof hash data. */
97     uint32_t proof_entry_count,          /* Number of inclusion proof hashes. */
98     uint8_t* leaf_hash,      /* The leaf hash to prove inclusion of. */
99     uint64_t leaf_hash_size, /* Size of the leaf hash. */
100     uint8_t* root_hash);     /* The resulting tree root hash. */
101 
102 /* Allocates and populates an AftlImage from a binary blob. */
103 AftlImage* parse_aftl_image(uint8_t* aftl_blob, size_t aftl_blob_size);
104 
105 /* Allocates and populates an AftlIcpEntry and all sub-fields from
106    a binary blob. It is assumed that the blob points to an AftlIcpEntry. */
107 AftlIcpEntry* parse_icp_entry(uint8_t** aftl_blob, size_t* remaining_size);
108 
109 /* Frees an AftlIcpEntry and all sub-fields that were previously
110    allocated by a call to parse_icp_entry. */
111 void free_aftl_icp_entry(AftlIcpEntry* aftl_icp_entry);
112 
113 /* Frees an AftlImage and all sub-fields that were previously
114    allocated by a call to parse_aftl_image. */
115 void free_aftl_image(AftlImage* image);
116 
117 #ifdef __cplusplus
118 }
119 #endif
120 
121 #endif /* AVB_AFTL_UTIL_H_ */
122