Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2025 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_secured_message_lib.h"
8 :
9 : /**
10 : * Allocates and Initializes one KEM context for subsequent use,
11 : * based upon negotiated KEM algorithm.
12 : *
13 : * @param kem_alg SPDM kem_alg
14 : * @param is_initiator if the caller is initiator.
15 : * true: initiator
16 : * false: not an initiator
17 : *
18 : * @return Pointer to the KEM context that has been initialized.
19 : **/
20 0 : void *libspdm_secured_message_kem_new(spdm_version_number_t spdm_version,
21 : uint32_t kem_alg, bool is_initiator)
22 : {
23 0 : return libspdm_kem_new(spdm_version, kem_alg, is_initiator);
24 : }
25 :
26 : /**
27 : * Release the specified KEM context,
28 : * based upon negotiated KEM algorithm.
29 : *
30 : * @param kem_alg SPDM kem_alg
31 : * @param kem_context Pointer to the KEM context to be released.
32 : **/
33 0 : void libspdm_secured_message_kem_free(uint32_t kem_alg, void *kem_context)
34 : {
35 0 : libspdm_kem_free(kem_alg, kem_context);
36 0 : }
37 :
38 : /**
39 : * Generates KEM public key,
40 : * based upon negotiated KEM algorithm.
41 : *
42 : * @param kem_alg SPDM kem_alg
43 : * @param kem_context Pointer to the KEM context.
44 : * @param encap_key Pointer to the buffer to receive generated public key.
45 : * @param encap_key_size On input, the size of public_key buffer in bytes.
46 : * On output, the size of data returned in public_key buffer in bytes.
47 : *
48 : * @retval true KEM public key generation succeeded.
49 : * @retval false KEM public key generation failed.
50 : * @retval false public_key_size is not large enough.
51 : **/
52 0 : bool libspdm_secured_message_kem_generate_key(uint32_t kem_alg,
53 : void *kem_context,
54 : uint8_t *encap_key,
55 : size_t *encap_key_size)
56 : {
57 0 : return libspdm_kem_generate_key(kem_alg, kem_context, encap_key, encap_key_size);
58 : }
59 :
60 : /**
61 : * Computes exchanged common key,
62 : * based upon negotiated KEM algorithm.
63 : *
64 : * @param kem_alg SPDM kem_alg
65 : * @param kem_context Pointer to the kem context.
66 : * @param peer_encap_key Pointer to the peer's public key.
67 : * @param peer_encap_key_size Size of peer's public key in bytes.
68 : * @param cipher_text Pointer to the buffer to receive generated cipher text.
69 : * @param cipher_text_size On input, the size of cipher text buffer in bytes.
70 : * On output, the size of data returned in cipher_text buffer in bytes.
71 : * @param spdm_secured_message_context A pointer to the SPDM secured message context.
72 : *
73 : * @retval true DHE exchanged key generation succeeded.
74 : * @retval false DHE exchanged key generation failed.
75 : * @retval false key_size is not large enough.
76 : **/
77 0 : bool libspdm_secured_message_kem_encapsulate(
78 : uint32_t kem_alg, void *kem_context,
79 : const uint8_t *peer_encap_key, size_t peer_encap_key_size,
80 : uint8_t *cipher_text, size_t *cipher_text_size,
81 : void *spdm_secured_message_context)
82 : {
83 : libspdm_secured_message_context_t *secured_message_context;
84 : uint8_t final_key[LIBSPDM_MAX_KEM_SS_KEY_SIZE];
85 : size_t final_key_size;
86 : bool ret;
87 :
88 0 : secured_message_context = spdm_secured_message_context;
89 :
90 0 : final_key_size = sizeof(final_key);
91 0 : ret = libspdm_kem_encapsulate(kem_alg, kem_context, peer_encap_key,
92 : peer_encap_key_size, cipher_text,
93 : cipher_text_size, final_key,
94 : &final_key_size);
95 0 : if (!ret) {
96 0 : return ret;
97 : }
98 0 : libspdm_copy_mem(secured_message_context->master_secret.shared_secret,
99 : sizeof(secured_message_context->master_secret.shared_secret),
100 : final_key, final_key_size);
101 0 : libspdm_zero_mem(final_key, final_key_size);
102 0 : secured_message_context->shared_key_size = final_key_size;
103 0 : return true;
104 : }
105 :
106 : /**
107 : * Computes exchanged common key,
108 : * based upon negotiated KEM algorithm.
109 : *
110 : * @param kem_alg SPDM kem_alg
111 : * @param kem_context Pointer to the kem context.
112 : * @param peer_cipher_text Pointer to the peer's public key.
113 : * @param peer_cipher_text_size Size of peer's public key in bytes.
114 : * @param spdm_secured_message_context A pointer to the SPDM secured message context.
115 : *
116 : * @retval true DHE exchanged key generation succeeded.
117 : * @retval false DHE exchanged key generation failed.
118 : * @retval false key_size is not large enough.
119 : **/
120 0 : bool libspdm_secured_message_kem_decapsulate(
121 : uint32_t kem_alg, void *kem_context,
122 : const uint8_t *peer_cipher_text, size_t peer_cipher_text_size,
123 : void *spdm_secured_message_context)
124 : {
125 : libspdm_secured_message_context_t *secured_message_context;
126 : uint8_t final_key[LIBSPDM_MAX_KEM_SS_KEY_SIZE];
127 : size_t final_key_size;
128 : bool ret;
129 :
130 0 : secured_message_context = spdm_secured_message_context;
131 :
132 0 : final_key_size = sizeof(final_key);
133 0 : ret = libspdm_kem_decapsulate(kem_alg, kem_context, peer_cipher_text,
134 : peer_cipher_text_size, final_key,
135 : &final_key_size);
136 0 : if (!ret) {
137 0 : return ret;
138 : }
139 0 : libspdm_copy_mem(secured_message_context->master_secret.shared_secret,
140 : sizeof(secured_message_context->master_secret.shared_secret),
141 : final_key, final_key_size);
142 0 : libspdm_zero_mem(final_key, final_key_size);
143 0 : secured_message_context->shared_key_size = final_key_size;
144 0 : return true;
145 : }
|