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