xref: /aosp_15_r20/external/open-dice/include/dice/dice.h (revision 60b67249c2e226f42f35cc6cfe66c6048e0bae6b)
1*60b67249SAndroid Build Coastguard Worker // Copyright 2020 Google LLC
2*60b67249SAndroid Build Coastguard Worker //
3*60b67249SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*60b67249SAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*60b67249SAndroid Build Coastguard Worker // the License at
6*60b67249SAndroid Build Coastguard Worker //
7*60b67249SAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*60b67249SAndroid Build Coastguard Worker //
9*60b67249SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*60b67249SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*60b67249SAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*60b67249SAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*60b67249SAndroid Build Coastguard Worker // the License.
14*60b67249SAndroid Build Coastguard Worker 
15*60b67249SAndroid Build Coastguard Worker #ifndef DICE_DICE_H_
16*60b67249SAndroid Build Coastguard Worker #define DICE_DICE_H_
17*60b67249SAndroid Build Coastguard Worker 
18*60b67249SAndroid Build Coastguard Worker #include <stddef.h>
19*60b67249SAndroid Build Coastguard Worker #include <stdint.h>
20*60b67249SAndroid Build Coastguard Worker 
21*60b67249SAndroid Build Coastguard Worker #include "dice/types.h"
22*60b67249SAndroid Build Coastguard Worker 
23*60b67249SAndroid Build Coastguard Worker #ifdef __cplusplus
24*60b67249SAndroid Build Coastguard Worker extern "C" {
25*60b67249SAndroid Build Coastguard Worker #endif
26*60b67249SAndroid Build Coastguard Worker 
27*60b67249SAndroid Build Coastguard Worker #define DICE_CDI_SIZE 32
28*60b67249SAndroid Build Coastguard Worker #define DICE_HASH_SIZE 64
29*60b67249SAndroid Build Coastguard Worker #define DICE_HIDDEN_SIZE 64
30*60b67249SAndroid Build Coastguard Worker #define DICE_INLINE_CONFIG_SIZE 64
31*60b67249SAndroid Build Coastguard Worker #define DICE_PRIVATE_KEY_SEED_SIZE 32
32*60b67249SAndroid Build Coastguard Worker #define DICE_ID_SIZE 20
33*60b67249SAndroid Build Coastguard Worker 
34*60b67249SAndroid Build Coastguard Worker // Contains a full set of input values describing the target program or system.
35*60b67249SAndroid Build Coastguard Worker // See the Open Profile for DICE specification for a detailed explanation of
36*60b67249SAndroid Build Coastguard Worker // these inputs.
37*60b67249SAndroid Build Coastguard Worker //
38*60b67249SAndroid Build Coastguard Worker // Fields:
39*60b67249SAndroid Build Coastguard Worker //    code_hash: A hash or similar representation of the target code.
40*60b67249SAndroid Build Coastguard Worker //    code_descriptor: An optional descriptor to be included in the certificate.
41*60b67249SAndroid Build Coastguard Worker //        This descriptor is opaque to the DICE flow and is included verbatim
42*60b67249SAndroid Build Coastguard Worker //        in the certificate with no validation. May be null.
43*60b67249SAndroid Build Coastguard Worker //    code_descriptor_size: The size in bytes of |code_descriptor|.
44*60b67249SAndroid Build Coastguard Worker //    config_type: Indicates how to interpret the remaining config-related
45*60b67249SAndroid Build Coastguard Worker //        fields. If the type is 'inline', then the 64 byte configuration input
46*60b67249SAndroid Build Coastguard Worker //        value must be provided in |config_value| and |config_descriptor| is
47*60b67249SAndroid Build Coastguard Worker //        ignored. If the type is 'descriptor', then |config_descriptor| is
48*60b67249SAndroid Build Coastguard Worker //        hashed to get the configuration input value and |config_value| is
49*60b67249SAndroid Build Coastguard Worker //        ignored.
50*60b67249SAndroid Build Coastguard Worker //    config_value: A 64-byte configuration input value when |config_type| is
51*60b67249SAndroid Build Coastguard Worker //        kDiceConfigTypeInline. Otherwise, this field is ignored.
52*60b67249SAndroid Build Coastguard Worker //    config_descriptor: A descriptor to be hashed for the configuration input
53*60b67249SAndroid Build Coastguard Worker //        value when |config_type| is kDiceConfigTypeDescriptor. Otherwise,
54*60b67249SAndroid Build Coastguard Worker //        this field is ignored and may be null.
55*60b67249SAndroid Build Coastguard Worker //    config_descriptor_size: The size in bytes of |config_descriptor|.
56*60b67249SAndroid Build Coastguard Worker //    authority_hash: A hash or similar representation of the authority used to
57*60b67249SAndroid Build Coastguard Worker //        verify the target code. If the code is not verified or the authority
58*60b67249SAndroid Build Coastguard Worker //        is implicit, for example hard coded as part of the code currently
59*60b67249SAndroid Build Coastguard Worker //        executing, then this value should be set to all zero bytes.
60*60b67249SAndroid Build Coastguard Worker //    authority_descriptor: An optional descriptor to be included in the
61*60b67249SAndroid Build Coastguard Worker //        certificate. This descriptor is opaque to the DICE flow and is
62*60b67249SAndroid Build Coastguard Worker //        included verbatim in the certificate with no validation. May be null.
63*60b67249SAndroid Build Coastguard Worker //    authority_descriptor_size: The size in bytes of |authority_descriptor|.
64*60b67249SAndroid Build Coastguard Worker //    mode: The current operating mode.
65*60b67249SAndroid Build Coastguard Worker //    hidden: Additional input which will not appear in certificates. If this is
66*60b67249SAndroid Build Coastguard Worker //        not used it should be set to all zero bytes.
67*60b67249SAndroid Build Coastguard Worker typedef struct DiceInputValues_ {
68*60b67249SAndroid Build Coastguard Worker   uint8_t code_hash[DICE_HASH_SIZE];
69*60b67249SAndroid Build Coastguard Worker   const uint8_t* code_descriptor;
70*60b67249SAndroid Build Coastguard Worker   size_t code_descriptor_size;
71*60b67249SAndroid Build Coastguard Worker   DiceConfigType config_type;
72*60b67249SAndroid Build Coastguard Worker   uint8_t config_value[DICE_INLINE_CONFIG_SIZE];
73*60b67249SAndroid Build Coastguard Worker   const uint8_t* config_descriptor;
74*60b67249SAndroid Build Coastguard Worker   size_t config_descriptor_size;
75*60b67249SAndroid Build Coastguard Worker   uint8_t authority_hash[DICE_HASH_SIZE];
76*60b67249SAndroid Build Coastguard Worker   const uint8_t* authority_descriptor;
77*60b67249SAndroid Build Coastguard Worker   size_t authority_descriptor_size;
78*60b67249SAndroid Build Coastguard Worker   DiceMode mode;
79*60b67249SAndroid Build Coastguard Worker   uint8_t hidden[DICE_HIDDEN_SIZE];
80*60b67249SAndroid Build Coastguard Worker } DiceInputValues;
81*60b67249SAndroid Build Coastguard Worker 
82*60b67249SAndroid Build Coastguard Worker // Derives a |cdi_private_key_seed| from a |cdi_attest| value. On success
83*60b67249SAndroid Build Coastguard Worker // populates |cdi_private_key_seed| and returns kDiceResultOk.
84*60b67249SAndroid Build Coastguard Worker DiceResult DiceDeriveCdiPrivateKeySeed(
85*60b67249SAndroid Build Coastguard Worker     void* context, const uint8_t cdi_attest[DICE_CDI_SIZE],
86*60b67249SAndroid Build Coastguard Worker     uint8_t cdi_private_key_seed[DICE_PRIVATE_KEY_SEED_SIZE]);
87*60b67249SAndroid Build Coastguard Worker 
88*60b67249SAndroid Build Coastguard Worker // Derives an |id| from a |cdi_public_key| value. Because public keys can vary
89*60b67249SAndroid Build Coastguard Worker // in length depending on the algorithm, the |cdi_public_key_size| in bytes must
90*60b67249SAndroid Build Coastguard Worker // be provided. When interpreted as an integer, |id| is big-endian. On success
91*60b67249SAndroid Build Coastguard Worker // populates |id| and returns kDiceResultOk.
92*60b67249SAndroid Build Coastguard Worker DiceResult DiceDeriveCdiCertificateId(void* context,
93*60b67249SAndroid Build Coastguard Worker                                       const uint8_t* cdi_public_key,
94*60b67249SAndroid Build Coastguard Worker                                       size_t cdi_public_key_size,
95*60b67249SAndroid Build Coastguard Worker                                       uint8_t id[DICE_ID_SIZE]);
96*60b67249SAndroid Build Coastguard Worker 
97*60b67249SAndroid Build Coastguard Worker // Executes the main DICE flow.
98*60b67249SAndroid Build Coastguard Worker //
99*60b67249SAndroid Build Coastguard Worker // Given a full set of input values and the current CDI values, computes the
100*60b67249SAndroid Build Coastguard Worker // next CDI values and a matching certificate. See the Open Profile for DICE
101*60b67249SAndroid Build Coastguard Worker // specification for a detailed explanation of this flow.
102*60b67249SAndroid Build Coastguard Worker // In certain cases, the caller may not need to generate the CDI certificate.
103*60b67249SAndroid Build Coastguard Worker // The caller should signal this by setting the certificate parameters to
104*60b67249SAndroid Build Coastguard Worker // null/zero values appropriately.
105*60b67249SAndroid Build Coastguard Worker //
106*60b67249SAndroid Build Coastguard Worker // Parameters:
107*60b67249SAndroid Build Coastguard Worker //    context: Context provided by the caller that is opaque to this library
108*60b67249SAndroid Build Coastguard Worker //        but is passed through to the integration-provided operations in
109*60b67249SAndroid Build Coastguard Worker //        dice/ops.h. The value is, therefore, integration-specific and may be
110*60b67249SAndroid Build Coastguard Worker //        null.
111*60b67249SAndroid Build Coastguard Worker //    current_cdi_attest, current_cdi_seal: The current CDI values as produced
112*60b67249SAndroid Build Coastguard Worker //        by a previous DICE flow. If this is the first DICE flow in a system,
113*60b67249SAndroid Build Coastguard Worker //        the Unique Device Secret (UDS) should be used for both of these
114*60b67249SAndroid Build Coastguard Worker //        arguments.
115*60b67249SAndroid Build Coastguard Worker //    input_values: A set of input values describing the target program or
116*60b67249SAndroid Build Coastguard Worker //        system.
117*60b67249SAndroid Build Coastguard Worker //    next_cdi_certificate_buffer_size: The size in bytes of the buffer pointed
118*60b67249SAndroid Build Coastguard Worker //        to by the |next_cdi_certificate| argument. This should be set to zero
119*60b67249SAndroid Build Coastguard Worker //        if next CDI certificate should not be computed.
120*60b67249SAndroid Build Coastguard Worker //    next_cdi_certificate: On success, will be populated with the generated
121*60b67249SAndroid Build Coastguard Worker //        certificate, up to |next_cdi_certificate_buffer_size| in size. If the
122*60b67249SAndroid Build Coastguard Worker //        certificate cannot fit in the buffer, |next_cdi_certificate_size| is
123*60b67249SAndroid Build Coastguard Worker //        populated with the required size and kDiceResultBufferTooSmall is
124*60b67249SAndroid Build Coastguard Worker //        returned. This should be set to NULL if next CDI certificate should
125*60b67249SAndroid Build Coastguard Worker //        not be computed.
126*60b67249SAndroid Build Coastguard Worker //    next_cdi_certificate_actual_size: On success, will be populated with the
127*60b67249SAndroid Build Coastguard Worker //        size, in bytes, of the certificate data written to
128*60b67249SAndroid Build Coastguard Worker //        |next_cdi_certificate|. If kDiceResultBufferTooSmall is returned, will
129*60b67249SAndroid Build Coastguard Worker //        be populated with the required buffer size. This should be set to NULL
130*60b67249SAndroid Build Coastguard Worker //        if next CDI certificate should not be computed.
131*60b67249SAndroid Build Coastguard Worker //    next_cdi_attest: On success, will be populated with the next CDI value for
132*60b67249SAndroid Build Coastguard Worker //        attestation.
133*60b67249SAndroid Build Coastguard Worker //    next_cdi_seal: On success, will be populated with the next CDI value for
134*60b67249SAndroid Build Coastguard Worker //        sealing.
135*60b67249SAndroid Build Coastguard Worker DiceResult DiceMainFlow(void* context,
136*60b67249SAndroid Build Coastguard Worker                         const uint8_t current_cdi_attest[DICE_CDI_SIZE],
137*60b67249SAndroid Build Coastguard Worker                         const uint8_t current_cdi_seal[DICE_CDI_SIZE],
138*60b67249SAndroid Build Coastguard Worker                         const DiceInputValues* input_values,
139*60b67249SAndroid Build Coastguard Worker                         size_t next_cdi_certificate_buffer_size,
140*60b67249SAndroid Build Coastguard Worker                         uint8_t* next_cdi_certificate,
141*60b67249SAndroid Build Coastguard Worker                         size_t* next_cdi_certificate_actual_size,
142*60b67249SAndroid Build Coastguard Worker                         uint8_t next_cdi_attest[DICE_CDI_SIZE],
143*60b67249SAndroid Build Coastguard Worker                         uint8_t next_cdi_seal[DICE_CDI_SIZE]);
144*60b67249SAndroid Build Coastguard Worker 
145*60b67249SAndroid Build Coastguard Worker #ifdef __cplusplus
146*60b67249SAndroid Build Coastguard Worker }  // extern "C"
147*60b67249SAndroid Build Coastguard Worker #endif
148*60b67249SAndroid Build Coastguard Worker 
149*60b67249SAndroid Build Coastguard Worker #endif  // DICE_DICE_H_
150