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 1 : bool libspdm_fips_selftest_ffdh(void *fips_selftest_context)
13 : {
14 1 : bool result = true;
15 :
16 : #if LIBSPDM_FFDHE_SUPPORT
17 1 : libspdm_fips_selftest_context_t *context = fips_selftest_context;
18 1 : LIBSPDM_ASSERT(fips_selftest_context != NULL);
19 :
20 : /* any test fail cause the FIPS fail*/
21 1 : if (context->tested_algo != context->self_test_result) {
22 0 : return false;
23 : }
24 :
25 : /* check if run before.*/
26 1 : if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_FFDH) != 0) {
27 0 : return true;
28 : }
29 :
30 : void *dh1;
31 : void *dh2;
32 : uint8_t ff_public_key1[256];
33 : size_t ff_public_key1_length;
34 : uint8_t ff_public_key2[256];
35 : size_t ff_public_key2_length;
36 : uint8_t ff_key1[256];
37 : size_t ff_key1_length;
38 : uint8_t ff_key2[256];
39 : size_t ff_key2_length;
40 :
41 1 : ff_public_key1_length = sizeof(ff_public_key1);
42 1 : ff_public_key2_length = sizeof(ff_public_key2);
43 1 : ff_key1_length = sizeof(ff_key1);
44 1 : ff_key2_length = sizeof(ff_key2);
45 :
46 1 : dh1 = libspdm_dh_new_by_nid(LIBSPDM_CRYPTO_NID_FFDHE2048);
47 1 : if (dh1 == NULL) {
48 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "FFDH gen dh1 failed \n"));
49 0 : result = false;
50 0 : goto update;
51 : }
52 :
53 1 : dh2 = libspdm_dh_new_by_nid(LIBSPDM_CRYPTO_NID_FFDHE2048);
54 1 : if (dh2 == NULL) {
55 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "FFDH gen dh2 failed \n"));
56 0 : libspdm_dh_free(dh1);
57 0 : result = false;
58 0 : goto update;
59 : }
60 :
61 1 : result = libspdm_dh_generate_key(dh1, ff_public_key1, &ff_public_key1_length);
62 1 : if (!result || ff_public_key1_length != 256) {
63 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "FFDH generate key1 failed \n"));
64 0 : libspdm_dh_free(dh1);
65 0 : libspdm_dh_free(dh2);
66 0 : result = false;
67 0 : goto update;
68 : }
69 :
70 1 : result = libspdm_dh_generate_key(dh2, ff_public_key2, &ff_public_key2_length);
71 1 : if (!result || ff_public_key2_length != 256) {
72 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "FFDH generate key2 failed \n"));
73 0 : libspdm_dh_free(dh1);
74 0 : libspdm_dh_free(dh2);
75 0 : result = false;
76 0 : goto update;
77 : }
78 :
79 1 : result = libspdm_dh_compute_key(dh1, ff_public_key2, ff_public_key2_length,
80 : ff_key1, &ff_key1_length);
81 1 : if (!result || ff_key1_length != 256) {
82 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "FFDH compute key failed \n"));
83 0 : libspdm_dh_free(dh1);
84 0 : libspdm_dh_free(dh2);
85 0 : result = false;
86 0 : goto update;
87 : }
88 :
89 1 : result = libspdm_dh_compute_key(dh2, ff_public_key1, ff_public_key1_length,
90 : ff_key2, &ff_key2_length);
91 1 : if (!result || ff_key2_length != 256) {
92 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "FFDH compute key failed \n"));
93 0 : libspdm_dh_free(dh1);
94 0 : libspdm_dh_free(dh2);
95 0 : result = false;
96 0 : goto update;
97 : }
98 :
99 : /*self_test*/
100 1 : if (ff_key1_length != ff_key2_length) {
101 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "FFDH self_test failed \n"));
102 0 : libspdm_dh_free(dh1);
103 0 : libspdm_dh_free(dh2);
104 0 : result = false;
105 0 : goto update;
106 : }
107 :
108 1 : if (!libspdm_consttime_is_mem_equal(ff_key1, ff_key2, ff_key1_length)) {
109 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "FFDH self_test failed \n"));
110 0 : libspdm_dh_free(dh1);
111 0 : libspdm_dh_free(dh2);
112 0 : result = false;
113 0 : goto update;
114 : }
115 :
116 1 : libspdm_dh_free(dh1);
117 1 : libspdm_dh_free(dh2);
118 :
119 1 : update:
120 : /* mark it as tested*/
121 1 : context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_FFDH;
122 :
123 : /* record test result*/
124 1 : if (result) {
125 1 : context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_FFDH;
126 : } else {
127 0 : context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_FFDH;
128 : }
129 :
130 : #endif/*LIBSPDM_FFDHE_SUPPORT*/
131 :
132 1 : return result;
133 : }
134 :
135 : #endif/*LIBSPDM_FIPS_MODE*/
|