Branch data Line data Source code
1 : : /**
2 : : * Copyright Notice:
3 : : * Copyright 2023-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_crypt_lib.h"
8 : : #include "internal/libspdm_common_lib.h"
9 : : #include "internal/libspdm_fips_lib.h"
10 : :
11 : : #if LIBSPDM_FIPS_MODE
12 : 8 : void libspdm_fips_selftest_ffdh(void *fips_selftest_context)
13 : : {
14 : : #if LIBSPDM_FFDHE_SUPPORT
15 : 8 : bool result = true;
16 : :
17 : 8 : libspdm_fips_selftest_context_t *context = fips_selftest_context;
18 : 8 : LIBSPDM_ASSERT(fips_selftest_context != NULL);
19 : :
20 : : /* any test fail cause the FIPS fail*/
21 [ + + ]: 8 : if (context->tested_algo != context->self_test_result) {
22 : 4 : return;
23 : : }
24 : :
25 : : /* check if run before.*/
26 [ + + ]: 6 : if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_FFDH) != 0) {
27 : 2 : return;
28 : : }
29 : :
30 : : void *dh1;
31 : : uint8_t ff_key1[256];
32 : : size_t ff_key1_length;
33 : :
34 : 4 : ff_key1_length = sizeof(ff_key1);
35 : :
36 : : /* Test vector (private key, peer public key and shared secret) from the NIST
37 : : * ACVP-Server: KAS-FFC-SSC-Sp800-56Ar3/internalProjection.json, tgId 1
38 : : * (domainParameterGenerationMode = ffdhe2048, scheme = dhEphem), tcId 1.
39 : : * The ffdhe2048 domain parameters are from RFC 7919.
40 : : * https://github.com/usnistgov/ACVP-Server/tree/master/gen-val/json-files/KAS-FFC-SSC-Sp800-56Ar3
41 : : */
42 : : /* self private key*/
43 : 4 : const uint8_t self_privkey[] = {
44 : : 0x71, 0x38, 0x0e, 0x69, 0x93, 0x3d, 0x9b, 0x7f,
45 : : 0x45, 0xf1, 0xca, 0x91, 0x89, 0x46, 0xee, 0x38,
46 : : 0xca, 0xf1, 0x2d, 0xba, 0xea, 0xe8, 0xab, 0x16,
47 : : 0x9b, 0x20, 0x5f, 0x2e, 0xbd, 0x9f, 0x43, 0x81,
48 : : 0x53, 0x79, 0x1c, 0x5c, 0x76, 0x84, 0x54, 0xbb,
49 : : 0x28, 0x0c, 0xd2, 0xcd, 0x9c, 0x73, 0x77, 0x79,
50 : : 0x47, 0xc9, 0x51, 0x56, 0x2e, 0x68, 0x52, 0x9f,
51 : : 0xd3, 0x21, 0xc3, 0x33, 0xc0, 0x00, 0x55, 0xb0,
52 : : 0xf5, 0xfe, 0xff, 0xd1, 0xb0, 0xa0, 0xa2, 0xcc,
53 : : 0xc5, 0x13, 0xe9, 0x74, 0x13, 0x32, 0xce, 0x9a,
54 : : 0x38, 0xe6, 0x45, 0x01, 0x93, 0x95, 0x16, 0x41,
55 : : 0x62, 0x46, 0x7e, 0x41, 0xc0, 0x80, 0xe5, 0x99,
56 : : 0xbd, 0x23, 0xf2, 0x9d, 0xf9, 0x8c, 0xe5, 0xba,
57 : : 0x18, 0x85, 0x8d, 0xb7, 0x05, 0x36, 0x3a, 0x6a,
58 : : 0x5a, 0x2f, 0x4e, 0xf7, 0x7a, 0x20, 0x52, 0x17,
59 : : 0x54, 0x6e, 0xaa, 0x29, 0x58, 0x35, 0x31, 0xda,
60 : : 0xde, 0xdc, 0xdd, 0x21, 0x87, 0x29, 0x63, 0x2c,
61 : : 0x43, 0xa1, 0x35, 0x40, 0x91, 0x3f, 0xdf, 0xa1,
62 : : 0x08, 0xca, 0x8f, 0x0c, 0xbb, 0xb0, 0xb9, 0x58,
63 : : 0x08, 0x1b, 0x40, 0xae, 0x20, 0xd6, 0x43, 0x71,
64 : : 0x93, 0xb8, 0x7e, 0x21, 0x0c, 0x8a, 0x22, 0x75,
65 : : 0xa1, 0x22, 0xe0, 0xee, 0xb9, 0xe2, 0xb2, 0x28,
66 : : 0xed, 0x16, 0xf0, 0x69, 0xc5, 0xe5, 0x90, 0xb7,
67 : : 0x34, 0x73, 0x12, 0xb2, 0x85, 0xf8, 0xbb, 0xea,
68 : : 0x79, 0x10, 0xe7, 0xb7, 0xee, 0xef, 0x72, 0x02,
69 : : 0x53, 0x94, 0x67, 0xd7, 0x69, 0xb4, 0xa8, 0x32,
70 : : 0xda, 0x59, 0x3c, 0x25, 0x4e, 0xce, 0xe9, 0xb4,
71 : : 0x40, 0xa3, 0xa8, 0x2b, 0xf7, 0xf2, 0xee, 0x44,
72 : : 0xb6, 0x90, 0x5c, 0xda, 0x7f, 0xdf, 0xd6, 0xe3,
73 : : 0x52, 0xa2, 0xd5, 0xac, 0xd3, 0x92, 0x46, 0x18,
74 : : 0x38, 0x80, 0x40, 0x71, 0x2d, 0x5b, 0xdb, 0xcb,
75 : : 0xc5, 0xfe, 0x5a, 0x20, 0x04, 0x8e, 0xf0, 0x5c
76 : : };
77 : :
78 : : /* peer public key*/
79 : 4 : const uint8_t peer_public[] = {
80 : : 0xd1, 0x5b, 0x21, 0x74, 0xdc, 0x6b, 0x2d, 0x45,
81 : : 0xf0, 0xd5, 0xef, 0x1d, 0xe4, 0xd9, 0x09, 0x74,
82 : : 0x95, 0x01, 0x87, 0x06, 0x32, 0xf1, 0xd4, 0x1b,
83 : : 0x6a, 0x48, 0x3a, 0x57, 0x03, 0x3c, 0x37, 0x87,
84 : : 0x2b, 0xd5, 0x8c, 0x47, 0xd8, 0x40, 0x58, 0x83,
85 : : 0xea, 0x95, 0x67, 0x37, 0xea, 0xef, 0xff, 0xa3,
86 : : 0x66, 0x96, 0x2a, 0x60, 0x82, 0xc7, 0x21, 0x48,
87 : : 0xa7, 0xca, 0x52, 0x1d, 0x9e, 0xbc, 0xb8, 0xf2,
88 : : 0xe6, 0x1b, 0x3c, 0xa1, 0x72, 0x0e, 0x0b, 0x08,
89 : : 0x4c, 0x29, 0x31, 0xf0, 0x61, 0xeb, 0xf6, 0x17,
90 : : 0x66, 0x47, 0x1d, 0xc8, 0x20, 0x18, 0xb1, 0xb1,
91 : : 0x87, 0x3f, 0x5e, 0xf9, 0xf1, 0x4b, 0xa8, 0x66,
92 : : 0x5a, 0xc9, 0x1e, 0xb1, 0xfc, 0xdf, 0x11, 0xa4,
93 : : 0x44, 0x25, 0xb3, 0xb4, 0x17, 0x7f, 0x8a, 0xe4,
94 : : 0x71, 0x7a, 0x98, 0xe5, 0xb9, 0xd3, 0xf9, 0x57,
95 : : 0x08, 0x57, 0xa1, 0xd2, 0x30, 0xc4, 0x0e, 0xef,
96 : : 0x15, 0xbd, 0x3e, 0x96, 0xf4, 0x68, 0x30, 0xee,
97 : : 0xb9, 0x95, 0xe6, 0xb8, 0x14, 0xda, 0x23, 0x20,
98 : : 0x5d, 0xd6, 0xa1, 0x40, 0x8a, 0xd4, 0x89, 0x26,
99 : : 0x10, 0xd2, 0x60, 0x81, 0x3a, 0x2b, 0x4b, 0x9e,
100 : : 0xa3, 0x8c, 0xe8, 0xf8, 0x25, 0x0c, 0x5d, 0xb3,
101 : : 0x7b, 0x20, 0x26, 0x37, 0x5e, 0xda, 0x1a, 0x8c,
102 : : 0xba, 0x43, 0x9f, 0x63, 0xd9, 0x60, 0x08, 0xf6,
103 : : 0x25, 0xb5, 0x6d, 0x08, 0x27, 0x5e, 0x6e, 0x77,
104 : : 0x3e, 0x9d, 0xee, 0x43, 0x5b, 0x9d, 0x0a, 0x2e,
105 : : 0x83, 0x1d, 0x02, 0xcd, 0x03, 0xaa, 0x5d, 0x9e,
106 : : 0xba, 0xa7, 0xfc, 0x75, 0x2c, 0x75, 0xb3, 0x17,
107 : : 0x9f, 0xa4, 0x3d, 0x4e, 0xe8, 0x99, 0x45, 0xdc,
108 : : 0x2c, 0x44, 0x01, 0x7c, 0x7c, 0x9a, 0xbc, 0xd4,
109 : : 0xa2, 0x10, 0x03, 0xb9, 0x82, 0xc4, 0xbd, 0x39,
110 : : 0x8d, 0x2c, 0x22, 0x8e, 0x46, 0x89, 0x70, 0x45,
111 : : 0x18, 0x05, 0x66, 0x25, 0x51, 0x7b, 0x50, 0xad
112 : : };
113 : :
114 : : /* expected ffdh common secret*/
115 : 4 : const uint8_t expected_ffdh_secret[] = {
116 : : 0x5d, 0x52, 0xa8, 0x86, 0x7a, 0x2b, 0x18, 0xfc,
117 : : 0x49, 0x24, 0x29, 0xe6, 0xb7, 0x8a, 0x36, 0x6e,
118 : : 0x4a, 0x9a, 0xcf, 0xc7, 0x6c, 0xd5, 0x00, 0xb9,
119 : : 0x2e, 0x99, 0x3f, 0x1e, 0xbe, 0x29, 0x44, 0x5f,
120 : : 0x39, 0x4f, 0x34, 0xe4, 0x89, 0x3a, 0x1e, 0x39,
121 : : 0x01, 0x86, 0x24, 0xa5, 0xaf, 0x0b, 0x27, 0xb6,
122 : : 0x65, 0xc5, 0xa8, 0xf1, 0x95, 0x7e, 0x30, 0x43,
123 : : 0xd3, 0x3f, 0x13, 0xdd, 0xda, 0x80, 0x8e, 0x80,
124 : : 0x24, 0xe2, 0x0d, 0xfd, 0xcc, 0xfc, 0xe8, 0x0c,
125 : : 0x9d, 0xe6, 0x5d, 0x5c, 0xa1, 0x8c, 0x76, 0x28,
126 : : 0x28, 0xe3, 0xa9, 0x04, 0x4e, 0x97, 0xbf, 0xa0,
127 : : 0x51, 0x00, 0x79, 0x72, 0xab, 0x20, 0x03, 0xc5,
128 : : 0xec, 0x00, 0xb7, 0xe6, 0xd2, 0x9d, 0xea, 0x98,
129 : : 0xe1, 0x1e, 0x0e, 0x85, 0x40, 0x54, 0x77, 0xcb,
130 : : 0xbf, 0x92, 0x19, 0xc0, 0x18, 0xbd, 0xa9, 0x19,
131 : : 0x76, 0x57, 0xf1, 0x39, 0xe7, 0x5b, 0xa1, 0x6a,
132 : : 0x6c, 0x68, 0xbb, 0xfa, 0x65, 0x4b, 0xc3, 0xd4,
133 : : 0x5b, 0x52, 0xea, 0x06, 0xe7, 0xd6, 0x5b, 0xcb,
134 : : 0x6d, 0xcb, 0x35, 0xe6, 0xa0, 0x59, 0x41, 0x93,
135 : : 0x8d, 0x0d, 0x19, 0x5f, 0x4f, 0xd1, 0xc1, 0x2b,
136 : : 0xdc, 0x18, 0x4c, 0x10, 0x4b, 0xf9, 0x78, 0x79,
137 : : 0xa2, 0x5b, 0x69, 0x84, 0x7b, 0x97, 0x3a, 0xad,
138 : : 0x93, 0x35, 0x88, 0xeb, 0xa9, 0x1e, 0x41, 0x4a,
139 : : 0x0e, 0xe5, 0x16, 0x92, 0x59, 0x69, 0x1c, 0xb3,
140 : : 0xfe, 0x55, 0x1f, 0xe8, 0xa7, 0x48, 0xd3, 0xf9,
141 : : 0x3e, 0xd3, 0x23, 0xd0, 0x18, 0x88, 0xbc, 0xa0,
142 : : 0xc8, 0x7a, 0xeb, 0xd0, 0xe3, 0xbb, 0xb4, 0x8f,
143 : : 0x68, 0x36, 0xe4, 0x6d, 0x55, 0x58, 0xf2, 0x88,
144 : : 0x9e, 0xaa, 0xb8, 0x68, 0x04, 0xc6, 0x8c, 0x15,
145 : : 0x09, 0x24, 0x3c, 0x63, 0xa2, 0x4d, 0xde, 0xff,
146 : : 0x05, 0x66, 0xff, 0x72, 0x6a, 0xd3, 0x38, 0x5a,
147 : : 0xdd, 0x44, 0xc8, 0x11, 0xcb, 0x09, 0xb4, 0x6f
148 : : };
149 : :
150 : 4 : dh1 = libspdm_dh_new_by_nid(LIBSPDM_CRYPTO_NID_FFDHE2048);
151 [ - + ]: 4 : if (dh1 == NULL) {
152 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "FFDH new failed \n"));
153 : 0 : result = false;
154 : 0 : goto update;
155 : : }
156 : :
157 : 4 : result = libspdm_dh_set_priv_key(dh1, self_privkey, sizeof(self_privkey));
158 [ - + ]: 4 : if (!result) {
159 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "FFDH set private key failed \n"));
160 : 0 : libspdm_dh_free(dh1);
161 : 0 : result = false;
162 : 0 : goto update;
163 : : }
164 : :
165 : 4 : result = libspdm_dh_compute_key(dh1, peer_public, sizeof(peer_public),
166 : : ff_key1, &ff_key1_length);
167 [ - + ]: 4 : if (!result) {
168 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "FFDH compute key failed \n"));
169 : 0 : libspdm_dh_free(dh1);
170 : 0 : result = false;
171 : 0 : goto update;
172 : : }
173 : :
174 : : /*KAT test*/
175 [ - + ]: 4 : if (ff_key1_length != sizeof(expected_ffdh_secret)) {
176 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "FFDH KAT failed \n"));
177 : 0 : libspdm_dh_free(dh1);
178 : 0 : result = false;
179 : 0 : goto update;
180 : : }
181 : :
182 [ - + ]: 4 : if (!libspdm_consttime_is_mem_equal(ff_key1, expected_ffdh_secret,
183 : : sizeof(expected_ffdh_secret))) {
184 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "FFDH KAT failed \n"));
185 : 0 : libspdm_dh_free(dh1);
186 : 0 : result = false;
187 : 0 : goto update;
188 : : }
189 : :
190 : 4 : libspdm_dh_free(dh1);
191 : :
192 : 4 : update:
193 : : /* mark it as tested*/
194 : 4 : context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_FFDH;
195 : :
196 : : /* record test result*/
197 [ + - ]: 4 : if (result) {
198 : 4 : context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_FFDH;
199 : : } else {
200 : 0 : context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_FFDH;
201 : : }
202 : :
203 : : #endif/*LIBSPDM_FFDHE_SUPPORT*/
204 : : }
205 : :
206 : : #endif/*LIBSPDM_FIPS_MODE*/
|