Branch data Line data Source code
1 : : /**
2 : : * Copyright Notice:
3 : : * Copyright 2021-2026 DMTF. All rights reserved.
4 : : * License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
5 : : **/
6 : :
7 : : #include "library/spdm_transport_pcidoe_lib.h"
8 : : #include "industry_standard/pcidoe.h"
9 : : #include "hal/library/debuglib.h"
10 : : #include "hal/library/memlib.h"
11 : :
12 : 10 : uint8_t libspdm_pci_doe_get_sequence_number(uint64_t sequence_number,
13 : : uint8_t *sequence_number_buffer)
14 : : {
15 : 10 : libspdm_copy_mem(sequence_number_buffer, LIBSPDM_PCI_DOE_SEQUENCE_NUMBER_COUNT,
16 : : &sequence_number, LIBSPDM_PCI_DOE_SEQUENCE_NUMBER_COUNT);
17 : 10 : return LIBSPDM_PCI_DOE_SEQUENCE_NUMBER_COUNT;
18 : : }
19 : :
20 : 6 : uint32_t libspdm_pci_doe_get_max_random_number_count(void)
21 : : {
22 : 6 : return LIBSPDM_PCI_DOE_MAX_RANDOM_NUMBER_COUNT;
23 : : }
24 : :
25 : 10 : spdm_version_number_t libspdm_pci_doe_get_secured_spdm_version(
26 : : spdm_version_number_t secured_message_version)
27 : : {
28 : : /* PCI-SIG uses DSP0277 version */
29 : 10 : return secured_message_version;
30 : : }
31 : :
32 : : /**
33 : : * Encode a normal message or secured message to a transport message.
34 : : *
35 : : * @param session_id Indicates if it is a secured message protected via SPDM session.
36 : : * If session_id is NULL, it is a normal message.
37 : : * If session_id is NOT NULL, it is a secured message.
38 : : * @param message_size size in bytes of the message data buffer.
39 : : * @param message A pointer to a source buffer to store the message.
40 : : * @param transport_message_size size in bytes of the transport message data buffer.
41 : : * @param transport_message A pointer to a destination buffer to store the transport message.
42 : : **/
43 : 14 : libspdm_return_t libspdm_pci_doe_encode_message(const uint32_t *session_id,
44 : : size_t message_size, void *message,
45 : : size_t *transport_message_size,
46 : : void **transport_message)
47 : : {
48 : : size_t aligned_message_size;
49 : : size_t alignment;
50 : : uint32_t data32;
51 : : pci_doe_data_object_header_t *pci_doe_header;
52 : :
53 : 14 : alignment = LIBSPDM_PCI_DOE_ALIGNMENT;
54 : 14 : aligned_message_size =
55 : 14 : (message_size + (alignment - 1)) & ~(alignment - 1);
56 : :
57 : 14 : LIBSPDM_ASSERT(*transport_message_size >=
58 : : aligned_message_size + sizeof(pci_doe_data_object_header_t));
59 : 14 : if (*transport_message_size <
60 [ - + ]: 14 : aligned_message_size + sizeof(pci_doe_data_object_header_t)) {
61 : 0 : *transport_message_size = aligned_message_size +
62 : : sizeof(pci_doe_data_object_header_t);
63 : 0 : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
64 : : }
65 : 14 : *transport_message_size =
66 : 14 : aligned_message_size + sizeof(pci_doe_data_object_header_t);
67 : 14 : *transport_message = (uint8_t *)message - sizeof(pci_doe_data_object_header_t);
68 : 14 : pci_doe_header = *transport_message;
69 : 14 : pci_doe_header->vendor_id = PCI_DOE_VENDOR_ID_PCISIG;
70 [ + + ]: 14 : if (session_id != NULL) {
71 : 4 : pci_doe_header->data_object_type =
72 : : PCI_DOE_DATA_OBJECT_TYPE_SECURED_SPDM;
73 : 4 : data32 = libspdm_read_uint32((const uint8_t *)message);
74 : 4 : LIBSPDM_ASSERT(*session_id == data32);
75 [ - + ]: 4 : if (*session_id != data32) {
76 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
77 : : }
78 : : } else {
79 : 10 : pci_doe_header->data_object_type =
80 : : PCI_DOE_DATA_OBJECT_TYPE_SPDM;
81 : : }
82 : 14 : pci_doe_header->reserved = 0;
83 [ + + ]: 14 : if (*transport_message_size > PCI_DOE_MAX_SIZE_IN_BYTE) {
84 : 2 : return LIBSPDM_STATUS_BUFFER_FULL;
85 [ + + ]: 12 : } else if (*transport_message_size == PCI_DOE_MAX_SIZE_IN_BYTE) {
86 : 2 : pci_doe_header->length = 0;
87 : : } else {
88 : 10 : pci_doe_header->length =
89 : 10 : (uint32_t)*transport_message_size / sizeof(uint32_t);
90 : : }
91 : :
92 : 12 : libspdm_zero_mem((uint8_t *)message + message_size,
93 : : aligned_message_size - message_size);
94 : 12 : return LIBSPDM_STATUS_SUCCESS;
95 : : }
96 : :
97 : 2 : libspdm_return_t libspdm_pci_doe_encode_discovery(size_t message_size, void *message,
98 : : size_t *transport_message_size,
99 : : void **transport_message)
100 : : {
101 : : size_t aligned_message_size;
102 : : size_t alignment;
103 : : pci_doe_data_object_header_t *pci_doe_header;
104 : :
105 : 2 : alignment = LIBSPDM_PCI_DOE_ALIGNMENT;
106 : 2 : aligned_message_size =
107 : 2 : (message_size + (alignment - 1)) & ~(alignment - 1);
108 : :
109 : 2 : LIBSPDM_ASSERT(*transport_message_size >=
110 : : aligned_message_size + sizeof(pci_doe_data_object_header_t));
111 : 2 : if (*transport_message_size <
112 [ - + ]: 2 : aligned_message_size + sizeof(pci_doe_data_object_header_t)) {
113 : 0 : *transport_message_size = aligned_message_size +
114 : : sizeof(pci_doe_data_object_header_t);
115 : 0 : return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
116 : : }
117 : :
118 : 2 : *transport_message_size =
119 : 2 : aligned_message_size + sizeof(pci_doe_data_object_header_t);
120 : 2 : *transport_message = (uint8_t *)message - sizeof(pci_doe_data_object_header_t);
121 : 2 : pci_doe_header = *transport_message;
122 : 2 : pci_doe_header->vendor_id = PCI_DOE_VENDOR_ID_PCISIG;
123 : 2 : pci_doe_header->data_object_type = PCI_DOE_DATA_OBJECT_TYPE_DOE_DISCOVERY;
124 : 2 : pci_doe_header->reserved = 0;
125 : :
126 [ - + ]: 2 : if (*transport_message_size > PCI_DOE_MAX_SIZE_IN_BYTE) {
127 : 0 : return LIBSPDM_STATUS_BUFFER_FULL;
128 [ - + ]: 2 : } else if (*transport_message_size == PCI_DOE_MAX_SIZE_IN_BYTE) {
129 : 0 : pci_doe_header->length = 0;
130 : : } else {
131 : 2 : pci_doe_header->length =
132 : 2 : (uint32_t)*transport_message_size / sizeof(uint32_t);
133 : : }
134 : :
135 : 2 : libspdm_zero_mem((uint8_t *)message + message_size,
136 : : aligned_message_size - message_size);
137 : :
138 : 2 : return LIBSPDM_STATUS_SUCCESS;
139 : : }
140 : :
141 : : /**
142 : : * Decode a transport message to a normal message or secured message.
143 : : *
144 : : * @param session_id Indicates if it is a secured message protected via SPDM session.
145 : : * If *session_id is NULL, it is a normal message.
146 : : * If *session_id is NOT NULL, it is a secured message.
147 : : * @param transport_message_size size in bytes of the transport message data buffer.
148 : : * @param transport_message A pointer to a source buffer to store the transport message.
149 : : * @param message_size size in bytes of the message data buffer.
150 : : * @param message A pointer to a destination buffer to store the message.
151 : : **/
152 : 34 : libspdm_return_t libspdm_pci_doe_decode_message(uint32_t **session_id,
153 : : size_t transport_message_size,
154 : : void *transport_message,
155 : : size_t *message_size,
156 : : void **message)
157 : : {
158 : : const pci_doe_data_object_header_t *pci_doe_header;
159 : : uint32_t length;
160 : :
161 : 34 : LIBSPDM_ASSERT(transport_message_size > sizeof(pci_doe_data_object_header_t));
162 [ - + ]: 34 : if (transport_message_size <= sizeof(pci_doe_data_object_header_t)) {
163 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
164 : : }
165 : :
166 : 34 : pci_doe_header = transport_message;
167 [ + + ]: 34 : if (pci_doe_header->vendor_id != PCI_DOE_VENDOR_ID_PCISIG) {
168 : 2 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
169 : : }
170 : :
171 [ + + + ]: 32 : switch (pci_doe_header->data_object_type) {
172 : 10 : case PCI_DOE_DATA_OBJECT_TYPE_SECURED_SPDM:
173 : 10 : LIBSPDM_ASSERT(session_id != NULL);
174 [ - + ]: 10 : if (session_id == NULL) {
175 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
176 : : }
177 [ + + ]: 10 : if (transport_message_size <=
178 : : sizeof(pci_doe_data_object_header_t) + sizeof(uint32_t)) {
179 : 2 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
180 : : }
181 : 8 : *session_id = (void *)((uint8_t *)transport_message +
182 : : sizeof(pci_doe_data_object_header_t));
183 : 8 : break;
184 : 18 : case PCI_DOE_DATA_OBJECT_TYPE_SPDM:
185 [ + - ]: 18 : if (session_id != NULL) {
186 : 18 : *session_id = NULL;
187 : : }
188 : 18 : break;
189 : 4 : default:
190 : 4 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
191 : : }
192 : :
193 [ + + ]: 26 : if (pci_doe_header->reserved != 0) {
194 : 2 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
195 : : }
196 [ + + ]: 24 : if (pci_doe_header->length >= PCI_DOE_MAX_SIZE_IN_DW) {
197 : 2 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
198 [ + + ]: 22 : } else if (pci_doe_header->length == 0) {
199 : 4 : length = PCI_DOE_MAX_SIZE_IN_BYTE;
200 : : } else {
201 : 18 : length = pci_doe_header->length * sizeof(uint32_t);
202 : : }
203 [ + + ]: 22 : if (length != transport_message_size) {
204 : 2 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
205 : : }
206 : :
207 : 20 : LIBSPDM_ASSERT(((transport_message_size - sizeof(pci_doe_data_object_header_t)) &
208 : : (LIBSPDM_PCI_DOE_ALIGNMENT - 1)) == 0);
209 : :
210 : 20 : *message_size = transport_message_size - sizeof(pci_doe_data_object_header_t);
211 : 20 : *message = (uint8_t *)transport_message + sizeof(pci_doe_data_object_header_t);
212 : 20 : return LIBSPDM_STATUS_SUCCESS;
213 : : }
214 : :
215 : 18 : libspdm_return_t libspdm_pci_doe_decode_discovery_request(size_t transport_message_size,
216 : : const void *transport_message,
217 : : uint8_t *index)
218 : : {
219 : : const pci_doe_data_object_header_t *pci_doe_header;
220 : : uint32_t length;
221 : : const uint8_t *message;
222 : :
223 : 18 : LIBSPDM_ASSERT(transport_message_size > sizeof(pci_doe_data_object_header_t));
224 [ - + ]: 18 : if (transport_message_size <= sizeof(pci_doe_data_object_header_t)) {
225 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
226 : : }
227 : :
228 : 18 : pci_doe_header = transport_message;
229 [ - + ]: 18 : if (pci_doe_header->vendor_id != PCI_DOE_VENDOR_ID_PCISIG) {
230 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
231 : : }
232 : :
233 [ + + ]: 18 : switch (pci_doe_header->data_object_type) {
234 : 8 : case PCI_DOE_DATA_OBJECT_TYPE_DOE_DISCOVERY:
235 : : /*
236 : : * Check whether the received message is a DOE discovery message.
237 : : * DOE discovery is not part of the SPDM spec, instead it's part
238 : : * of the PCIe DOE spec. DOE discovery is mandatory for all
239 : : * implementations.
240 : : */
241 : 8 : message = (const uint8_t *)transport_message + sizeof(pci_doe_data_object_header_t);
242 [ + + ]: 8 : if (index != NULL) {
243 : 4 : *index = *message;
244 : : }
245 : 8 : break;
246 : 10 : default:
247 : 10 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
248 : : }
249 : :
250 [ - + ]: 8 : if (pci_doe_header->reserved != 0) {
251 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
252 : : }
253 [ - + ]: 8 : if (pci_doe_header->length >= PCI_DOE_MAX_SIZE_IN_DW) {
254 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
255 [ - + ]: 8 : } else if (pci_doe_header->length == 0) {
256 : 0 : length = PCI_DOE_MAX_SIZE_IN_BYTE;
257 : : } else {
258 : 8 : length = pci_doe_header->length * sizeof(uint32_t);
259 : : }
260 [ - + ]: 8 : if (length != transport_message_size) {
261 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
262 : : }
263 : :
264 : 8 : return LIBSPDM_STATUS_SUCCESS;
265 : : }
266 : :
267 : 8 : libspdm_return_t libspdm_pci_doe_decode_discovery_request_version(size_t transport_message_size,
268 : : const void *transport_message,
269 : : uint8_t *version)
270 : : {
271 : : const uint8_t *message;
272 : : libspdm_return_t status;
273 : :
274 : : /*
275 : : * Calling libspdm_pci_doe_decode_discovery_request to check the format of transport_message.
276 : : * So that the duplicated checking code is skipped.
277 : : */
278 : 8 : status = libspdm_pci_doe_decode_discovery_request(transport_message_size, transport_message,
279 : : NULL);
280 [ + + ]: 8 : if (LIBSPDM_STATUS_IS_ERROR(status)) {
281 : 4 : return status;
282 : : }
283 : :
284 : : /*
285 : : * DOE discovery is not part of the SPDM spec, instead it's part
286 : : * of the PCIe DOE spec. DOE discovery is mandatory for all
287 : : * implementations.
288 : : *
289 : : * DOE Discovery Version is introduced in PCIe Spec 6.1 Section 6.30.1.1.
290 : : * It is Byte-1 in DOE discovery request.
291 : : */
292 : 4 : message = (const uint8_t *)transport_message + sizeof(pci_doe_data_object_header_t) + 1;
293 [ + - ]: 4 : if (version != NULL) {
294 : 4 : *version = *message;
295 : : }
296 : :
297 : 4 : return LIBSPDM_STATUS_SUCCESS;
298 : : }
299 : :
300 : 12 : libspdm_return_t libspdm_pci_doe_decode_discovery_response(size_t transport_message_size,
301 : : void *transport_message,
302 : : uint16_t *vendor_id,
303 : : uint8_t *protocol,
304 : : uint8_t *next_index)
305 : : {
306 : : const pci_doe_data_object_header_t *pci_doe_header;
307 : : uint32_t length;
308 : : uint8_t *message;
309 : :
310 : 12 : LIBSPDM_ASSERT(transport_message_size > sizeof(pci_doe_data_object_header_t));
311 [ - + ]: 12 : if (transport_message_size <= sizeof(pci_doe_data_object_header_t)) {
312 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
313 : : }
314 : :
315 : 12 : pci_doe_header = transport_message;
316 [ - + ]: 12 : if (pci_doe_header->vendor_id != PCI_DOE_VENDOR_ID_PCISIG) {
317 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
318 : : }
319 : :
320 [ + + ]: 12 : switch (pci_doe_header->data_object_type) {
321 : 8 : case PCI_DOE_DATA_OBJECT_TYPE_DOE_DISCOVERY:
322 : : /*
323 : : * Check whether the received message is a DOE discovery message.
324 : : * DOE discovery is not part of the SPDM spec, instead it's part
325 : : * of the PCIe DOE spec. DOE discovery is mandatory for all
326 : : * implementations.
327 : : */
328 [ + + ]: 8 : if (transport_message_size <
329 : : sizeof(pci_doe_data_object_header_t) + sizeof(pci_doe_discovery_response_t)) {
330 : 2 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
331 : : }
332 : 6 : message = (uint8_t *)transport_message + sizeof(pci_doe_data_object_header_t);
333 [ + - ]: 6 : if (vendor_id != NULL) {
334 : 6 : *vendor_id = libspdm_read_uint16(message);
335 : : }
336 [ + - ]: 6 : if (protocol != NULL) {
337 : 6 : *protocol = *(message + sizeof(uint16_t));
338 : : }
339 [ + - ]: 6 : if (next_index != NULL) {
340 : 6 : *next_index = *(message + sizeof(uint16_t) + sizeof(uint8_t));
341 : : }
342 : 6 : break;
343 : 4 : default:
344 : 4 : return LIBSPDM_STATUS_UNSUPPORTED_CAP;
345 : : }
346 : :
347 [ - + ]: 6 : if (pci_doe_header->reserved != 0) {
348 : 0 : return LIBSPDM_STATUS_INVALID_MSG_FIELD;
349 : : }
350 [ - + ]: 6 : if (pci_doe_header->length >= PCI_DOE_MAX_SIZE_IN_DW) {
351 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
352 [ - + ]: 6 : } else if (pci_doe_header->length == 0) {
353 : 0 : length = PCI_DOE_MAX_SIZE_IN_BYTE;
354 : : } else {
355 : 6 : length = pci_doe_header->length * sizeof(uint32_t);
356 : : }
357 [ - + ]: 6 : if (length != transport_message_size) {
358 : 0 : return LIBSPDM_STATUS_INVALID_MSG_SIZE;
359 : : }
360 : :
361 : 6 : return LIBSPDM_STATUS_SUCCESS;
362 : : }
363 : :
364 : 2 : uint32_t libspdm_transport_pci_doe_get_header_size(
365 : : void *spdm_context)
366 : : {
367 : 2 : return sizeof(pci_doe_data_object_header_t) + sizeof(spdm_secured_message_cipher_header_t);
368 : : }
|