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 "internal/libspdm_common_lib.h"
8 :
9 : #if LIBSPDM_DEBUG_PRINT_ENABLE
10 : typedef struct {
11 : uint8_t code;
12 : const char *code_str;
13 : } libspdm_code_str_struct_t;
14 :
15 136409 : const char *libspdm_get_code_str(uint8_t request_code)
16 : {
17 : size_t index;
18 :
19 : static const libspdm_code_str_struct_t code_str_struct[] = {
20 : /* SPDM response code (1.0) */
21 : { SPDM_DIGESTS, "SPDM_DIGESTS" },
22 : { SPDM_CERTIFICATE, "SPDM_CERTIFICATE" },
23 : { SPDM_CHALLENGE_AUTH, "SPDM_CHALLENGE_AUTH" },
24 : { SPDM_VERSION, "SPDM_VERSION" },
25 : { SPDM_MEASUREMENTS, "SPDM_MEASUREMENTS" },
26 : { SPDM_CAPABILITIES, "SPDM_CAPABILITIES" },
27 : { SPDM_ALGORITHMS, "SPDM_ALGORITHMS" },
28 : { SPDM_VENDOR_DEFINED_RESPONSE, "SPDM_VENDOR_DEFINED_RESPONSE" },
29 : { SPDM_ERROR, "SPDM_ERROR" },
30 : /* SPDM response code (1.1) */
31 : { SPDM_KEY_EXCHANGE_RSP, "SPDM_KEY_EXCHANGE_RSP" },
32 : { SPDM_FINISH_RSP, "SPDM_FINISH_RSP" },
33 : { SPDM_PSK_EXCHANGE_RSP, "SPDM_PSK_EXCHANGE_RSP" },
34 : { SPDM_PSK_FINISH_RSP, "SPDM_PSK_FINISH_RSP" },
35 : { SPDM_HEARTBEAT_ACK, "SPDM_HEARTBEAT_ACK" },
36 : { SPDM_KEY_UPDATE_ACK, "SPDM_KEY_UPDATE_ACK" },
37 : { SPDM_ENCAPSULATED_REQUEST, "SPDM_ENCAPSULATED_REQUEST" },
38 : { SPDM_ENCAPSULATED_RESPONSE_ACK, "SPDM_ENCAPSULATED_RESPONSE_ACK" },
39 : { SPDM_END_SESSION_ACK, "SPDM_END_SESSION_ACK" },
40 : /* SPDM response code (1.2) */
41 : { SPDM_CSR, "SPDM_CSR" },
42 : { SPDM_SET_CERTIFICATE_RSP, "SPDM_SET_CERTIFICATE_RSP" },
43 : { SPDM_CHUNK_SEND_ACK, "SPDM_CHUNK_SEND_ACK" },
44 : { SPDM_CHUNK_RESPONSE, "SPDM_CHUNK_RESPONSE" },
45 : /* SPDM response code (1.3 )*/
46 : { SPDM_SUPPORTED_EVENT_TYPES, "SPDM_SUPPORTED_EVENT_TYPES" },
47 : { SPDM_SUBSCRIBE_EVENT_TYPES_ACK, "SPDM_SUBSCRIBE_EVENT_TYPES_ACK" },
48 : { SPDM_EVENT_ACK, "SPDM_EVENT_ACK" },
49 : { SPDM_MEASUREMENT_EXTENSION_LOG, "SPDM_MEASUREMENT_EXTENSION_LOG" },
50 : { SPDM_KEY_PAIR_INFO, "SPDM_KEY_PAIR_INFO" },
51 : { SPDM_SET_KEY_PAIR_INFO_ACK, "SPDM_SET_KEY_PAIR_INFO_ACK" },
52 : { SPDM_ENDPOINT_INFO, "SPDM_ENDPOINT_INFO" },
53 : /* SPDM request code (1.0) */
54 : { SPDM_GET_DIGESTS, "SPDM_GET_DIGESTS" },
55 : { SPDM_GET_CERTIFICATE, "SPDM_GET_CERTIFICATE" },
56 : { SPDM_CHALLENGE, "SPDM_CHALLENGE" },
57 : { SPDM_GET_VERSION, "SPDM_GET_VERSION" },
58 : { SPDM_GET_MEASUREMENTS, "SPDM_GET_MEASUREMENTS" },
59 : { SPDM_GET_CAPABILITIES, "SPDM_GET_CAPABILITIES" },
60 : { SPDM_NEGOTIATE_ALGORITHMS, "SPDM_NEGOTIATE_ALGORITHMS" },
61 : { SPDM_VENDOR_DEFINED_REQUEST, "SPDM_VENDOR_DEFINED_REQUEST" },
62 : { SPDM_RESPOND_IF_READY, "SPDM_RESPOND_IF_READY" },
63 : /* SPDM request code (1.1) */
64 : { SPDM_KEY_EXCHANGE, "SPDM_KEY_EXCHANGE" },
65 : { SPDM_FINISH, "SPDM_FINISH" },
66 : { SPDM_PSK_EXCHANGE, "SPDM_PSK_EXCHANGE" },
67 : { SPDM_PSK_FINISH, "SPDM_PSK_FINISH" },
68 : { SPDM_HEARTBEAT, "SPDM_HEARTBEAT" },
69 : { SPDM_KEY_UPDATE, "SPDM_KEY_UPDATE" },
70 : { SPDM_GET_ENCAPSULATED_REQUEST, "SPDM_GET_ENCAPSULATED_REQUEST" },
71 : { SPDM_DELIVER_ENCAPSULATED_RESPONSE, "SPDM_DELIVER_ENCAPSULATED_RESPONSE" },
72 : { SPDM_END_SESSION, "SPDM_END_SESSION" },
73 : /* SPDM request code (1.2) */
74 : { SPDM_GET_CSR, "SPDM_GET_CSR" },
75 : { SPDM_SET_CERTIFICATE, "SPDM_SET_CERTIFICATE" },
76 : { SPDM_CHUNK_SEND, "SPDM_CHUNK_SEND" },
77 : { SPDM_CHUNK_GET, "SPDM_CHUNK_GET" },
78 : /* SPDM request code (1.3) */
79 : { SPDM_GET_SUPPORTED_EVENT_TYPES, "SPDM_GET_SUPPORTED_EVENT_TYPES" },
80 : { SPDM_SUBSCRIBE_EVENT_TYPES, "SPDM_SUBSCRIBE_EVENT_TYPES" },
81 : { SPDM_SEND_EVENT, "SPDM_SEND_EVENT" },
82 : { SPDM_GET_MEASUREMENT_EXTENSION_LOG, "SPDM_GET_MEASUREMENT_EXTENSION_LOG" },
83 : { SPDM_GET_KEY_PAIR_INFO, "SPDM_GET_KEY_PAIR_INFO" },
84 : { SPDM_SET_KEY_PAIR_INFO, "SPDM_SET_KEY_PAIR_INFO" },
85 : { SPDM_GET_ENDPOINT_INFO, "SPDM_GET_ENDPOINT_INFO" },
86 : };
87 :
88 4893749 : for (index = 0; index < LIBSPDM_ARRAY_SIZE(code_str_struct); index++) {
89 4893747 : if (request_code == code_str_struct[index].code) {
90 136407 : return code_str_struct[index].code_str;
91 : }
92 : }
93 :
94 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "RequestResponseCode 0x%x not found.\n", request_code));
95 :
96 2 : return "<unknown>";
97 : }
98 :
99 31 : void libspdm_internal_dump_hex_str(const uint8_t *data, size_t size)
100 : {
101 : size_t index;
102 1023 : for (index = 0; index < size; index++) {
103 992 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "%02x", data[index]));
104 : }
105 31 : }
106 :
107 213174 : void libspdm_internal_dump_data(const uint8_t *data, size_t size)
108 : {
109 : size_t index;
110 3838803 : for (index = 0; index < size; index++) {
111 3625629 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "%02x ", data[index]));
112 : }
113 213174 : }
114 :
115 138927 : void libspdm_internal_dump_hex(const uint8_t *data, size_t size)
116 : {
117 : size_t index;
118 : size_t count;
119 : size_t left;
120 :
121 : #define COLUMN_SIZE (16 * 2)
122 :
123 138927 : count = size / COLUMN_SIZE;
124 138927 : left = size % COLUMN_SIZE;
125 211256 : for (index = 0; index < count; index++) {
126 72329 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "%04zx: ", index * COLUMN_SIZE));
127 72329 : LIBSPDM_INTERNAL_DUMP_DATA(data + index * COLUMN_SIZE, COLUMN_SIZE);
128 72329 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
129 : }
130 :
131 138927 : if (left != 0) {
132 138594 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "%04zx: ", index * COLUMN_SIZE));
133 138594 : LIBSPDM_INTERNAL_DUMP_DATA(data + index * COLUMN_SIZE, left);
134 138594 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
135 : }
136 138927 : }
137 : #endif /* LIBSPDM_DEBUG_PRINT_ENABLE */
138 :
139 254 : uint32_t libspdm_read_uint24(const uint8_t *buffer)
140 : {
141 254 : return (uint32_t)(buffer[0] | buffer[1] << 8 | buffer[2] << 16);
142 : }
143 :
144 2323 : void libspdm_write_uint24(uint8_t *buffer, uint32_t value)
145 : {
146 2323 : buffer[0] = (uint8_t)(value & 0xFF);
147 2323 : buffer[1] = (uint8_t)((value >> 8) & 0xFF);
148 2323 : buffer[2] = (uint8_t)((value >> 16) & 0xFF);
149 2323 : }
150 :
151 559 : uint16_t libspdm_read_uint16(const uint8_t *buffer)
152 : {
153 559 : return (uint16_t)(buffer[0] | buffer[1] << 8);
154 : }
155 :
156 258 : void libspdm_write_uint16(uint8_t *buffer, uint16_t value)
157 : {
158 258 : buffer[0] = (uint8_t)(value & 0xFF);
159 258 : buffer[1] = (uint8_t)((value >> 8) & 0xFF);
160 258 : }
161 :
162 551 : uint32_t libspdm_read_uint32(const uint8_t *buffer)
163 : {
164 551 : return (uint32_t)(buffer[0] | buffer[1] << 8 | buffer[2] << 16 | buffer[3] << 24);
165 : }
166 :
167 24 : void libspdm_write_uint32(uint8_t *buffer, uint32_t value)
168 : {
169 24 : buffer[0] = (uint8_t)(value & 0xFF);
170 24 : buffer[1] = (uint8_t)((value >> 8) & 0xFF);
171 24 : buffer[2] = (uint8_t)((value >> 16) & 0xFF);
172 24 : buffer[3] = (uint8_t)((value >> 24) & 0xFF);
173 24 : }
174 :
175 0 : uint64_t libspdm_read_uint64(const uint8_t *buffer)
176 : {
177 0 : return (uint64_t)(buffer[0]) |
178 0 : ((uint64_t)(buffer[1]) << 8) |
179 0 : ((uint64_t)(buffer[2]) << 16) |
180 0 : ((uint64_t)(buffer[3]) << 24) |
181 0 : ((uint64_t)(buffer[4]) << 32) |
182 0 : ((uint64_t)(buffer[5]) << 40) |
183 0 : ((uint64_t)(buffer[6]) << 48) |
184 0 : ((uint64_t)(buffer[7]) << 56);
185 : }
186 :
187 0 : void libspdm_write_uint64(uint8_t *buffer, uint64_t value)
188 : {
189 0 : buffer[0] = (uint8_t)(value & 0xFF);
190 0 : buffer[1] = (uint8_t)((value >> 8) & 0xFF);
191 0 : buffer[2] = (uint8_t)((value >> 16) & 0xFF);
192 0 : buffer[3] = (uint8_t)((value >> 24) & 0xFF);
193 0 : buffer[4] = (uint8_t)((value >> 32) & 0xFF);
194 0 : buffer[5] = (uint8_t)((value >> 40) & 0xFF);
195 0 : buffer[6] = (uint8_t)((value >> 48) & 0xFF);
196 0 : buffer[7] = (uint8_t)((value >> 56) & 0xFF);
197 0 : }
198 :
199 453 : libspdm_return_t libspdm_append_managed_buffer(void *m_buffer, const void *buffer,
200 : size_t buffer_size)
201 : {
202 : libspdm_managed_buffer_t *managed_buffer;
203 :
204 453 : LIBSPDM_ASSERT(buffer != NULL);
205 :
206 453 : if (buffer_size == 0) {
207 0 : return LIBSPDM_STATUS_SUCCESS;
208 : }
209 :
210 453 : managed_buffer = m_buffer;
211 :
212 453 : LIBSPDM_ASSERT(buffer_size != 0);
213 453 : LIBSPDM_ASSERT(managed_buffer->max_buffer_size >= managed_buffer->buffer_size);
214 :
215 453 : if (buffer_size > managed_buffer->max_buffer_size - managed_buffer->buffer_size) {
216 : /* Do not LIBSPDM_ASSERT here, because command processor will append message from external.*/
217 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR,
218 : "libspdm_append_managed_buffer 0x%x fail, rest 0x%x only\n",
219 : (uint32_t)buffer_size,
220 : (uint32_t)(managed_buffer->max_buffer_size - managed_buffer->buffer_size)));
221 1 : return LIBSPDM_STATUS_BUFFER_FULL;
222 : }
223 452 : LIBSPDM_ASSERT(buffer_size <= managed_buffer->max_buffer_size - managed_buffer->buffer_size);
224 :
225 452 : libspdm_copy_mem((uint8_t *)(managed_buffer + 1) + managed_buffer->buffer_size,
226 : buffer_size, buffer, buffer_size);
227 452 : managed_buffer->buffer_size += buffer_size;
228 :
229 452 : return LIBSPDM_STATUS_SUCCESS;
230 : }
231 :
232 848 : void libspdm_reset_managed_buffer(void *m_buffer)
233 : {
234 : libspdm_managed_buffer_t *managed_buffer;
235 :
236 848 : managed_buffer = m_buffer;
237 848 : managed_buffer->buffer_size = 0;
238 :
239 848 : libspdm_zero_mem(managed_buffer + 1, managed_buffer->max_buffer_size);
240 848 : }
241 :
242 680 : size_t libspdm_get_managed_buffer_size(void *m_buffer)
243 : {
244 : libspdm_managed_buffer_t *managed_buffer;
245 :
246 680 : managed_buffer = m_buffer;
247 :
248 680 : return managed_buffer->buffer_size;
249 : }
250 :
251 669 : void *libspdm_get_managed_buffer(void *m_buffer)
252 : {
253 : libspdm_managed_buffer_t *managed_buffer;
254 :
255 669 : managed_buffer = m_buffer;
256 :
257 669 : return (managed_buffer + 1);
258 : }
259 :
260 113 : void libspdm_init_managed_buffer(void *m_buffer, size_t max_buffer_size)
261 : {
262 : libspdm_managed_buffer_t *managed_buffer;
263 :
264 113 : managed_buffer = m_buffer;
265 113 : managed_buffer->max_buffer_size = max_buffer_size;
266 :
267 113 : libspdm_reset_managed_buffer(m_buffer);
268 113 : }
269 :
270 0 : uint32_t libspdm_module_version(void)
271 : {
272 0 : return (LIBSPDM_MAJOR_VERSION << 24) |
273 : (LIBSPDM_MINOR_VERSION << 16) |
274 : (LIBSPDM_PATCH_VERSION << 8) |
275 : (LIBSPDM_ALPHA);
276 : }
277 :
278 : /*true: FIPS enabled, false: FIPS disabled*/
279 0 : bool libspdm_get_fips_mode(void)
280 : {
281 : #if LIBSPDM_FIPS_MODE
282 0 : return true;
283 : #else
284 : return false;
285 : #endif
286 : }
287 :
288 75 : uint32_t libspdm_mask_capability_flags(libspdm_context_t *spdm_context,
289 : bool is_request_flags, uint32_t flags)
290 : {
291 75 : switch (libspdm_get_connection_version(spdm_context)) {
292 9 : case SPDM_MESSAGE_VERSION_10:
293 9 : if (is_request_flags) {
294 : /* A 1.0 Requester does not have any capability flags. */
295 2 : return 0;
296 : } else {
297 7 : return (flags & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_10_MASK);
298 : }
299 45 : case SPDM_MESSAGE_VERSION_11:
300 45 : if (is_request_flags) {
301 39 : return (flags & SPDM_GET_CAPABILITIES_REQUEST_FLAGS_11_MASK);
302 : } else {
303 6 : return (flags & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_11_MASK);
304 : }
305 12 : case SPDM_MESSAGE_VERSION_12:
306 12 : if (is_request_flags) {
307 10 : return (flags & SPDM_GET_CAPABILITIES_REQUEST_FLAGS_12_MASK);
308 : } else {
309 2 : return (flags & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_12_MASK);
310 : }
311 9 : case SPDM_MESSAGE_VERSION_13:
312 9 : if (is_request_flags) {
313 5 : return (flags & SPDM_GET_CAPABILITIES_REQUEST_FLAGS_13_MASK);
314 : } else {
315 4 : return (flags & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_13_MASK);
316 : }
317 0 : case SPDM_MESSAGE_VERSION_14:
318 0 : if (is_request_flags) {
319 0 : return (flags & SPDM_GET_CAPABILITIES_REQUEST_FLAGS_14_MASK);
320 : } else {
321 0 : return (flags & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_14_MASK);
322 : }
323 0 : default:
324 0 : LIBSPDM_ASSERT(false);
325 0 : return 0;
326 : }
327 : }
328 :
329 0 : uint16_t libspdm_mask_capability_ext_flags(libspdm_context_t *spdm_context,
330 : bool is_request_flags, uint16_t ext_flags)
331 : {
332 0 : switch (libspdm_get_connection_version(spdm_context)) {
333 0 : case SPDM_MESSAGE_VERSION_10:
334 : case SPDM_MESSAGE_VERSION_11:
335 : case SPDM_MESSAGE_VERSION_12:
336 : case SPDM_MESSAGE_VERSION_13:
337 : /* No extended capability flags are defined for SPDM 1.0 - 1.3. */
338 0 : return 0;
339 0 : case SPDM_MESSAGE_VERSION_14:
340 0 : if (is_request_flags) {
341 : /* No extended request flags are defined for SPDM 1.4. */
342 0 : return 0;
343 : } else {
344 0 : return (ext_flags & SPDM_GET_CAPABILITIES_EXTENDED_RESPONSE_FLAGS_14_MASK);
345 : }
346 0 : default:
347 0 : LIBSPDM_ASSERT(false);
348 0 : return 0;
349 : }
350 : }
351 :
352 0 : uint32_t libspdm_mask_base_hash_algo(libspdm_context_t *spdm_context, uint32_t base_hash_algo)
353 : {
354 0 : const uint8_t spdm_version = libspdm_get_connection_version(spdm_context);
355 :
356 0 : if (spdm_version >= SPDM_MESSAGE_VERSION_12) {
357 0 : return (base_hash_algo & SPDM_ALGORITHMS_BASE_HASH_ALGO_12_MASK);
358 : } else {
359 0 : return (base_hash_algo & SPDM_ALGORITHMS_BASE_HASH_ALGO_10_MASK);
360 : }
361 : }
362 :
363 0 : uint32_t libspdm_mask_measurement_hash_algo(libspdm_context_t *spdm_context,
364 : uint32_t measurement_hash_algo)
365 : {
366 0 : const uint8_t spdm_version = libspdm_get_connection_version(spdm_context);
367 :
368 0 : if (spdm_version >= SPDM_MESSAGE_VERSION_12) {
369 0 : return (measurement_hash_algo & SPDM_ALGORITHMS_MEASUREMENT_HASH_ALGO_12_MASK);
370 : } else {
371 0 : return (measurement_hash_algo & SPDM_ALGORITHMS_MEASUREMENT_HASH_ALGO_10_MASK);
372 : }
373 : }
374 :
375 0 : uint8_t libspdm_mask_measurement_specification(uint8_t measurement_specification)
376 : {
377 0 : return (measurement_specification & SPDM_MEASUREMENT_SPECIFICATION_10_MASK);
378 : }
379 :
380 0 : uint8_t libspdm_mask_mel_specification(libspdm_context_t *spdm_context, uint8_t mel_specification)
381 : {
382 0 : LIBSPDM_ASSERT(libspdm_get_connection_version(spdm_context) >= SPDM_MESSAGE_VERSION_13);
383 :
384 0 : return (mel_specification & SPDM_MEL_SPECIFICATION_13_MASK);
385 : }
386 :
387 0 : uint32_t libspdm_mask_base_asym_algo(libspdm_context_t *spdm_context, uint32_t base_asym_algo)
388 : {
389 0 : const uint8_t spdm_version = libspdm_get_connection_version(spdm_context);
390 :
391 0 : if (spdm_version >= SPDM_MESSAGE_VERSION_12) {
392 0 : return (base_asym_algo & SPDM_ALGORITHMS_BASE_ASYM_ALGO_12_MASK);
393 : } else {
394 0 : return (base_asym_algo & SPDM_ALGORITHMS_BASE_ASYM_ALGO_10_MASK);
395 : }
396 : }
397 :
398 0 : uint16_t libspdm_mask_alg_supported(libspdm_context_t *spdm_context, uint8_t alg_type,
399 : uint16_t alg_supported)
400 : {
401 0 : const uint8_t spdm_version = libspdm_get_connection_version(spdm_context);
402 :
403 0 : LIBSPDM_ASSERT(spdm_version >= SPDM_MESSAGE_VERSION_11);
404 :
405 0 : switch (alg_type) {
406 0 : case SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_DHE:
407 0 : if (spdm_version >= SPDM_MESSAGE_VERSION_12) {
408 0 : return (alg_supported & SPDM_NEGOTIATE_ALGORITHMS_ALG_SUPPORTED_DHE_12_MASK);
409 : } else {
410 0 : return (alg_supported & SPDM_NEGOTIATE_ALGORITHMS_ALG_SUPPORTED_DHE_11_MASK);
411 : }
412 0 : case SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_AEAD:
413 0 : if (spdm_version >= SPDM_MESSAGE_VERSION_12) {
414 0 : return (alg_supported & SPDM_NEGOTIATE_ALGORITHMS_ALG_SUPPORTED_AEAD_12_MASK);
415 : } else {
416 0 : return (alg_supported & SPDM_NEGOTIATE_ALGORITHMS_ALG_SUPPORTED_AEAD_11_MASK);
417 : }
418 0 : case SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_REQ_BASE_ASYM_ALG:
419 0 : if (spdm_version >= SPDM_MESSAGE_VERSION_12) {
420 0 : return (alg_supported &
421 : SPDM_NEGOTIATE_ALGORITHMS_ALG_SUPPORTED_REQ_BASE_ASYM_ALG_12_MASK);
422 : } else {
423 0 : return (alg_supported &
424 : SPDM_NEGOTIATE_ALGORITHMS_ALG_SUPPORTED_REQ_BASE_ASYM_ALG_11_MASK);
425 : }
426 0 : case SPDM_NEGOTIATE_ALGORITHMS_STRUCT_TABLE_ALG_TYPE_KEY_SCHEDULE:
427 0 : return (alg_supported & SPDM_NEGOTIATE_ALGORITHMS_ALG_SUPPORTED_KEY_SCHEDULE_11_MASK);
428 0 : default:
429 0 : LIBSPDM_ASSERT(false);
430 0 : return 0;
431 : }
432 : }
433 :
434 36 : bool libspdm_validate_svh_vendor_id_len(uint16_t id, uint8_t vendor_id_len)
435 : {
436 36 : switch (id) {
437 28 : case SPDM_REGISTRY_ID_DMTF:
438 : case SPDM_REGISTRY_ID_VESA:
439 28 : return (vendor_id_len == 0);
440 8 : case SPDM_REGISTRY_ID_TCG:
441 : case SPDM_REGISTRY_ID_USB:
442 : case SPDM_REGISTRY_ID_PCISIG:
443 : case SPDM_REGISTRY_ID_MIPI:
444 : case SPDM_REGISTRY_ID_CXL:
445 : case SPDM_REGISTRY_ID_JEDEC:
446 8 : return ((vendor_id_len == 0) || (vendor_id_len == 2));
447 0 : case SPDM_REGISTRY_ID_IANA:
448 : case SPDM_REGISTRY_ID_HDBASET:
449 0 : return ((vendor_id_len == 0) || (vendor_id_len == 4));
450 0 : case SPDM_REGISTRY_ID_IANA_CBOR:
451 0 : switch (vendor_id_len) {
452 : /* A CBOR-encoded tag number is either a simple one byte identifier, or a one byte
453 : * identifier followed by a one, two, four, or eight byte unsigned integer. */
454 0 : case 1:
455 : case 2:
456 : case 3:
457 : case 5:
458 : case 9:
459 0 : return true;
460 0 : default:
461 0 : return false;
462 : }
463 0 : case SPDM_REGISTRY_ID_DMTF_DSP:
464 0 : return (vendor_id_len == 2);
465 0 : default:
466 0 : return false;
467 : }
468 : }
469 :
470 100 : bool libspdm_check_for_space(const uint8_t *ptr, const uint8_t *end_ptr, size_t increment)
471 : {
472 100 : LIBSPDM_ASSERT(ptr <= end_ptr);
473 :
474 100 : return ((uintptr_t)(end_ptr - ptr) >= increment);
475 : }
|