blob: 807de463b40f2c025e3fbb163bdc85ca343a4cb9 [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
21#define DICE_WRAPPING_KEY_SIZE 32
22#define DICE_CERT_SIZE 1024
23
24#define INVALID_HANDLE 0xFFFFFFFF
25#define INVALID_COMPONENT_IDX 0xFFFF
26#define INVALID_NONCE_VALUE 0xFFFF
Maulik Patelad2f3db2023-05-17 15:41:36 +010027#define INVALID_LAYER_IDX 65535
Maulik Patel54d65f72023-06-28 13:04:36 +010028#define DPE_ROT_LAYER_IDX 0
29
30/* Below configuration defines are platform dependant */
31#define MAX_NUM_OF_COMPONENTS 30
32#define MAX_NUM_OF_LAYERS 10
33#define DPE_PLATFORM_LAYER_IDX 1
34#define DPE_SECURE_WORLD_AND_HYPERVISOR_LAYER_IDX 2
35/* Below threshold defines the threshold below which a context cannot be destroyed */
36#define DPE_DESTROY_CONTEXT_THRESHOLD_LAYER_IDX DPE_SECURE_WORLD_AND_HYPERVISOR_LAYER_IDX
Maulik Patelad2f3db2023-05-17 15:41:36 +010037
38/* Most significant 16 bits represent nonce & remaining 16 bits represent component index */
39#define GET_IDX(handle) ((handle) & 0xffff)
40#define GET_NONCE(handle) ((handle >> 16) & 0xffff)
41
42#define SET_IDX(handle, idx) ((handle & 0xffff0000) | idx)
43#define SET_NONCE(handle, nonce) ((handle & 0x00ffff) | (nonce << 16))
44
45struct component_context_data_t {
46 uint8_t measurement_value[DICE_HASH_SIZE];
47 uint8_t measurement_descriptor[DICE_CODE_DESCRIPTOR_MAX_SIZE];
48 size_t measurement_descriptor_size;
49 uint8_t signer_id[DICE_HASH_SIZE];
50 uint8_t signer_id_descriptor[DICE_AUTHORITY_DESCRIPTOR_MAX_SIZE];
51 size_t signer_id_descriptor_size;
52 uint8_t config_value[DICE_INLINE_CONFIG_SIZE];
53 uint8_t config_descriptor[DICE_CONFIG_DESCRIPTOR_MAX_SIZE];
54 size_t config_descriptor_size;
55 DiceMode mode;
56 uint8_t hidden[DICE_HIDDEN_SIZE];
57};
58
59struct component_context_t {
60 struct component_context_data_t data; /* Component context data */
61 bool in_use; /* Flag to indicate if element is used */
62 bool is_leaf; /* Is the component allowed to derive */
63 uint16_t nonce; /* Context handle nonce for the component */
64 uint16_t parent_idx; /* Parent component's index */
65 uint16_t linked_layer_idx; /* Layer component is linked to */
66 uint32_t expected_mhu_id; /* Expected mhu to authorise derivation */
67};
68
69struct layer_context_data_t {
Maulik Patel58595d32023-06-22 10:08:53 +010070 psa_key_id_t cdi_key_id;
Maulik Patelad2f3db2023-05-17 15:41:36 +010071 uint8_t cdi_seal[DICE_CDI_SIZE];
72 uint8_t wrapping_key[DICE_WRAPPING_KEY_SIZE];
Maulik Patel58595d32023-06-22 10:08:53 +010073 psa_key_id_t attest_key_id;
Maulik Patelad2f3db2023-05-17 15:41:36 +010074 uint8_t cert_buf[DICE_CERT_SIZE];
75 size_t cert_buf_size;
76};
77
78enum layer_state_t {
79 LAYER_STATE_CLOSED = 0,
80 LAYER_STATE_OPEN,
81 LAYER_STATE_FINALISED
82};
83
84struct layer_context_t {
85 struct layer_context_data_t data;
86 uint16_t parent_layer_idx;
Maulik Patel58595d32023-06-22 10:08:53 +010087 uint8_t attest_cdi_hash_input[DPE_HASH_ALG_SIZE];
Maulik Patelad2f3db2023-05-17 15:41:36 +010088 enum layer_state_t state;
89};
90
91/**
92 * \brief Derives a root of trust component context and creates certificate.
93 *
94 * \param[in] dice_inputs Pointer to dice_input buffer.
95 * \param[out] new_child_context_handle A new handle for child context.
96 * \param[out] new_parent_context_handle A new handle for parent context.
97 *
98 * \return Returns error code of type dpe_error_t
99 */
100dpe_error_t derive_rot_context(const DiceInputValues *dice_inputs,
101 int *new_child_ctx_handle,
102 int *new_parent_ctx_handle);
103
104/**
105 * \brief Derives a child component context and optionally creates certificate
106 * chain.
107 *
108 * \param[in] input_context_handle Input handle to child component context
109 * \param[in] retain_parent_context Flag to indicate if parent context need
110 * to be retained. TRUE only if a client
111 * is calling DPE commands multiple times
112 * \param[in] allow_child_to_derive Flag to indicate if requested child can
113 * derive further.
114 * \param[in] create_certificate Flag to indicate if certificate needs
115 * to be created. TRUE only if it is the
116 * last component in the layer.
117 * \param[in] dice_inputs Pointer to dice_input buffer.
118 * \param[in] client_id Identifier of the client calling the
119 * service.
120 * \param[out] new_child_context_handle A new handle for child context.
121 * \param[out] new_parent_context_handle A new handle for parent context.
122 *
123 * \return Returns error code of type dpe_error_t
124 */
125dpe_error_t derive_child_request(int input_context_handle,
126 bool retain_parent_context,
127 bool allow_child_to_derive,
128 bool create_certificate,
129 const DiceInputValues *dice_inputs,
130 int32_t client_id,
131 int *new_child_context_handle,
132 int *new_parent_context_handle);
133
134/**
Maulik Patel54d65f72023-06-28 13:04:36 +0100135 * \brief Destroys a component context and optionally depending on argument
136 * destroy_recursively, destroys all its child context too.
137 *
138 * \param[in] input_context_handle Input handle to child component context
139 * \param[in] destroy_recursively Flag to indicate if all derived contexts
140 * should also be destroyed recursively.
141 *
142 * \return Returns error code of type dpe_error_t
143 */
144dpe_error_t destroy_context_request(int input_ctx_handle,
145 bool destroy_recursively);
146
147/**
Maulik Patelad2f3db2023-05-17 15:41:36 +0100148 * \brief Initialise all DPE Layer and component contexts
149 *
150 */
151void initialise_all_dpe_contexts(void);
152
153#ifdef __cplusplus
154}
155#endif
156
157#endif /* __DPE_CONTEXT_MNGR_H__ */