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 :
13 1 : bool libspdm_fips_selftest_ecdh(void *fips_selftest_context)
14 : {
15 1 : bool result = true;
16 :
17 : #if LIBSPDM_ECDHE_SUPPORT
18 1 : libspdm_fips_selftest_context_t *context = fips_selftest_context;
19 1 : LIBSPDM_ASSERT(fips_selftest_context != NULL);
20 :
21 : /* any test fail cause the FIPS fail*/
22 1 : if (context->tested_algo != context->self_test_result) {
23 0 : return false;
24 : }
25 :
26 : /* check if run before.*/
27 1 : if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_ECDH) != 0) {
28 0 : return true;
29 : }
30 :
31 : void *ec_context;
32 : uint8_t common_key[66];
33 : size_t common_key_length;
34 :
35 1 : common_key_length = sizeof(common_key);
36 1 : libspdm_zero_mem(common_key, common_key_length);
37 : /* self private Key*/
38 1 : const uint8_t self_privkey[] = {
39 : 0xd6, 0x84, 0xd1, 0x7c, 0xe3, 0x6b, 0xe7, 0x08,
40 : 0xbc, 0xd9, 0x89, 0x3f, 0xbb, 0xf4, 0xf2, 0xcf,
41 : 0x8d, 0x7f, 0xd4, 0x72, 0xbc, 0xfb, 0x54, 0x29,
42 : 0xd9, 0x86, 0xe2, 0x86, 0xc2, 0x38, 0xe5, 0x88
43 : };
44 :
45 : /* peer public Key*/
46 1 : const uint8_t peer_public[] = {
47 : 0x54, 0xbc, 0x5f, 0x6b, 0x70, 0x9b, 0x29, 0x5c,
48 : 0xa9, 0x43, 0xd0, 0xb7, 0xf3, 0xa2, 0x4b, 0xf0,
49 : 0x76, 0xb1, 0xd1, 0x9f, 0x55, 0x6a, 0x4e, 0xa0,
50 : 0x40, 0x54, 0xd2, 0xb1, 0x2f, 0x0f, 0xc1, 0x6d,
51 : 0xe7, 0x53, 0xe1, 0x3a, 0xd9, 0xb9, 0x2d, 0xd6,
52 : 0x3a, 0xda, 0x9d, 0xa9, 0xa9, 0x4e, 0xdd, 0x30,
53 : 0x60, 0x24, 0x9f, 0x9d, 0xcb, 0xfc, 0x1a, 0x56,
54 : 0x35, 0x63, 0x64, 0xe2, 0x64, 0xcf, 0x00, 0xed
55 : };
56 :
57 : /* expected ecdh common secret*/
58 1 : const uint8_t expected_ecdh_secret[] = {
59 : 0x05, 0xd5, 0xc8, 0x66, 0x83, 0x59, 0xe8, 0x33,
60 : 0x1d, 0xb7, 0x68, 0x2f, 0x98, 0x71, 0x2f, 0xfe,
61 : 0x2d, 0xfa, 0x10, 0xe6, 0x67, 0x89, 0x81, 0xd8,
62 : 0x51, 0xd9, 0x72, 0x47, 0x17, 0x7b, 0xa3, 0x5e
63 : };
64 :
65 1 : ec_context = libspdm_ec_new_by_nid(LIBSPDM_CRYPTO_NID_ECDSA_NIST_P256);
66 1 : if (ec_context == NULL) {
67 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDH new failed \n"));
68 0 : result = false;
69 0 : goto update;
70 : }
71 :
72 1 : result = libspdm_ec_set_priv_key(ec_context, self_privkey, sizeof(self_privkey));
73 1 : if (!result) {
74 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDH set private key failed \n"));
75 0 : libspdm_ec_free(ec_context);
76 0 : result = false;
77 0 : goto update;
78 : }
79 :
80 1 : result = libspdm_ec_compute_key(ec_context, peer_public, sizeof(peer_public), common_key,
81 : &common_key_length);
82 1 : if (!result) {
83 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDH compute key failed \n"));
84 0 : libspdm_ec_free(ec_context);
85 0 : result = false;
86 0 : goto update;
87 : }
88 :
89 : /*KAT test*/
90 1 : if (common_key_length != sizeof(expected_ecdh_secret)) {
91 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDH KAT failed \n"));
92 0 : libspdm_ec_free(ec_context);
93 0 : result = false;
94 0 : goto update;
95 : }
96 :
97 1 : if (!libspdm_consttime_is_mem_equal(common_key, expected_ecdh_secret,
98 : sizeof(expected_ecdh_secret))) {
99 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDH KAT failed \n"));
100 0 : result = false;
101 0 : goto update;
102 : }
103 :
104 1 : update:
105 : /* mark it as tested*/
106 1 : context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_ECDH;
107 :
108 : /* record test result*/
109 1 : if (result) {
110 1 : context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_ECDH;
111 : } else {
112 0 : context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_ECDH;
113 : }
114 :
115 : #endif/*LIBSPDM_ECDHE_SUPPORT*/
116 :
117 1 : return result;
118 : }
119 :
120 : #endif/*LIBSPDM_FIPS_MODE*/
|