blob: d488a500c893c19d4fec908900a0369bca93ffbd [file] [log] [blame]
Maulik Patelad2f3db2023-05-17 15:41:36 +01001/*
2 * Copyright (c) 2023, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef __DPE_CONTEXT_MNGR_H__
9#define __DPE_CONTEXT_MNGR_H__
10
11#include <stddef.h>
12#include <stdint.h>
13#include <stdbool.h>
14#include "dice_protection_environment.h"
Maulik Patel58595d32023-06-22 10:08:53 +010015#include "dpe_crypto_config.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010016
17#ifdef __cplusplus
18extern "C" {
19#endif
20
Maulik Patel2358bbb2023-07-21 10:56:56 +010021#define DICE_CERT_SIZE 3072
Maulik Patelad2f3db2023-05-17 15:41:36 +010022
23#define INVALID_HANDLE 0xFFFFFFFF
24#define INVALID_COMPONENT_IDX 0xFFFF
25#define INVALID_NONCE_VALUE 0xFFFF
Maulik Patelad2f3db2023-05-17 15:41:36 +010026#define INVALID_LAYER_IDX 65535
Maulik Patel54d65f72023-06-28 13:04:36 +010027#define DPE_ROT_LAYER_IDX 0
28
29/* Below configuration defines are platform dependant */
30#define MAX_NUM_OF_COMPONENTS 30
Maulik Patel2358bbb2023-07-21 10:56:56 +010031#define MAX_NUM_OF_LAYERS 6
Maulik Patel54d65f72023-06-28 13:04:36 +010032#define DPE_PLATFORM_LAYER_IDX 1
33#define DPE_SECURE_WORLD_AND_HYPERVISOR_LAYER_IDX 2
34/* Below threshold defines the threshold below which a context cannot be destroyed */
35#define DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX DPE_SECURE_WORLD_AND_HYPERVISOR_LAYER_IDX
Maulik Patelad2f3db2023-05-17 15:41:36 +010036
37/* Most significant 16 bits represent nonce & remaining 16 bits represent component index */
38#define GET_IDX(handle) ((handle) & 0xffff)
39#define GET_NONCE(handle) ((handle >> 16) & 0xffff)
40
41#define SET_IDX(handle, idx) ((handle & 0xffff0000) | idx)
42#define SET_NONCE(handle, nonce) ((handle & 0x00ffff) | (nonce << 16))
43
44struct component_context_data_t {
45 uint8_t measurement_value[DICE_HASH_SIZE];
46 uint8_t measurement_descriptor[DICE_CODE_DESCRIPTOR_MAX_SIZE];
47 size_t measurement_descriptor_size;
48 uint8_t signer_id[DICE_HASH_SIZE];
49 uint8_t signer_id_descriptor[DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE];
50 size_t signer_id_descriptor_size;
51 uint8_t config_value[DICE_INLINE_CONFIG_SIZE];
52 uint8_t config_descriptor[DICE_CONFIG_DESCRIPTOR_MAX_SIZE];
53 size_t config_descriptor_size;
54 DiceMode mode;
55 uint8_t hidden[DICE_HIDDEN_SIZE];
56};
57
58struct component_context_t {
59 struct component_context_data_t data; /* Component context data */
60 bool in_use; /* Flag to indicate if element is used */
61 bool is_leaf; /* Is the component allowed to derive */
62 uint16_t nonce; /* Context handle nonce for the component */
63 uint16_t parent_idx; /* Parent component's index */
64 uint16_t linked_layer_idx; /* Layer component is linked to */
65 uint32_t expected_mhu_id; /* Expected mhu to authorise derivation */
66};
67
68struct layer_context_data_t {
Maulik Patel58595d32023-06-22 10:08:53 +010069 psa_key_id_t cdi_key_id;
Maulik Patelad2f3db2023-05-17 15:41:36 +010070 uint8_t cdi_seal[DICE_CDI_SIZE];
Maulik Patel2358bbb2023-07-21 10:56:56 +010071 uint8_t cdi_id[DICE_ID_SIZE];
Maulik Patel58595d32023-06-22 10:08:53 +010072 psa_key_id_t attest_key_id;
Maulik Patel2358bbb2023-07-21 10:56:56 +010073 uint8_t attest_pub_key[DPE_ATTEST_PUB_KEY_SIZE];
74 size_t attest_pub_key_len;
Maulik Patelad2f3db2023-05-17 15:41:36 +010075 uint8_t cert_buf[DICE_CERT_SIZE];
Maulik Patel2358bbb2023-07-21 10:56:56 +010076 size_t cert_buf_len;
Maulik Patelad2f3db2023-05-17 15:41:36 +010077};
78
79enum layer_state_t {
80 LAYER_STATE_CLOSED = 0,
81 LAYER_STATE_OPEN,
82 LAYER_STATE_FINALISED
83};
84
85struct layer_context_t {
86 struct layer_context_data_t data;
87 uint16_t parent_layer_idx;
Maulik Patel58595d32023-06-22 10:08:53 +010088 uint8_t attest_cdi_hash_input[DPE_HASH_ALG_SIZE];
Maulik Patelad2f3db2023-05-17 15:41:36 +010089 enum layer_state_t state;
90};
91
92/**
93 * \brief Derives a root of trust component context and creates certificate.
94 *
95 * \param[in] dice_inputs Pointer to dice_input buffer.
96 * \param[out] new_child_context_handle A new handle for child context.
97 * \param[out] new_parent_context_handle A new handle for parent context.
98 *
99 * \return Returns error code of type dpe_error_t
100 */
101dpe_error_t derive_rot_context(const DiceInputValues *dice_inputs,
102 int *new_child_ctx_handle,
103 int *new_parent_ctx_handle);
104
105/**
106 * \brief Derives a child component context and optionally creates certificate
107 * chain.
108 *
109 * \param[in] input_context_handle Input handle to child component context
110 * \param[in] retain_parent_context Flag to indicate if parent context need
111 * to be retained. TRUE only if a client
112 * is calling DPE commands multiple times
113 * \param[in] allow_child_to_derive Flag to indicate if requested child can
114 * derive further.
115 * \param[in] create_certificate Flag to indicate if certificate needs
116 * to be created. TRUE only if it is the
117 * last component in the layer.
118 * \param[in] dice_inputs Pointer to dice_input buffer.
119 * \param[in] client_id Identifier of the client calling the
120 * service.
121 * \param[out] new_child_context_handle A new handle for child context.
122 * \param[out] new_parent_context_handle A new handle for parent context.
123 *
124 * \return Returns error code of type dpe_error_t
125 */
126dpe_error_t derive_child_request(int input_context_handle,
127 bool retain_parent_context,
128 bool allow_child_to_derive,
129 bool create_certificate,
130 const DiceInputValues *dice_inputs,
131 int32_t client_id,
132 int *new_child_context_handle,
133 int *new_parent_context_handle);
134
135/**
Maulik Patel54d65f72023-06-28 13:04:36 +0100136 * \brief Destroys a component context and optionally depending on argument
137 * destroy_recursively, destroys all its child context too.
138 *
139 * \param[in] input_context_handle Input handle to child component context
140 * \param[in] destroy_recursively Flag to indicate if all derived contexts
141 * should also be destroyed recursively.
142 *
143 * \return Returns error code of type dpe_error_t
144 */
145dpe_error_t destroy_context_request(int input_ctx_handle,
146 bool destroy_recursively);
147
148/**
Maulik Patelad2f3db2023-05-17 15:41:36 +0100149 * \brief Initialise all DPE Layer and component contexts
150 *
151 */
152void initialise_all_dpe_contexts(void);
153
Maulik Patel2358bbb2023-07-21 10:56:56 +0100154/**
155 * \brief Function to get the pointer to a component context if linked to a layer
156 *
157 * \param[in] layer_idx Index of the linked layer
158 * \param[in] component_idx Index of the component context in the array
159 *
160 * \return Returns pointer to the component context if it is linked to the input
161 * layer else returns NULL
162 */
163struct component_context_t* get_component_if_linked_to_layer(uint16_t layer_idx,
164 uint16_t component_idx);
165
Maulik Patelad2f3db2023-05-17 15:41:36 +0100166#ifdef __cplusplus
167}
168#endif
169
170#endif /* __DPE_CONTEXT_MNGR_H__ */