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 : :
13 : 8 : void libspdm_fips_selftest_ecdh(void *fips_selftest_context)
14 : : {
15 : : #if LIBSPDM_ECDHE_SUPPORT
16 : 8 : bool result = true;
17 : :
18 : 8 : libspdm_fips_selftest_context_t *context = fips_selftest_context;
19 : 8 : LIBSPDM_ASSERT(fips_selftest_context != NULL);
20 : :
21 : : /* any test fail cause the FIPS fail*/
22 [ + + ]: 8 : if (context->tested_algo != context->self_test_result) {
23 : 4 : return;
24 : : }
25 : :
26 : : /* check if run before.*/
27 [ + + ]: 6 : if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_ECDH) != 0) {
28 : 2 : return;
29 : : }
30 : :
31 : : void *ec_context;
32 : : uint8_t common_key[66];
33 : : size_t common_key_length;
34 : :
35 : 4 : common_key_length = sizeof(common_key);
36 : 4 : libspdm_zero_mem(common_key, common_key_length);
37 : : /* Test vector (private key, peer public key and shared secret) from the
38 : : * NIST CAVP: ecccdhtestvectors.zip, KAS_ECC_CDH_PrimitiveTest.txt,
39 : : * [P-256], COUNT = 0.
40 : : * https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/component-testing
41 : : */
42 : : /* self private Key*/
43 : 4 : const uint8_t self_privkey[] = {
44 : : 0x7d, 0x7d, 0xc5, 0xf7, 0x1e, 0xb2, 0x9d, 0xda,
45 : : 0xf8, 0x0d, 0x62, 0x14, 0x63, 0x2e, 0xea, 0xe0,
46 : : 0x3d, 0x90, 0x58, 0xaf, 0x1f, 0xb6, 0xd2, 0x2e,
47 : : 0xd8, 0x0b, 0xad, 0xb6, 0x2b, 0xc1, 0xa5, 0x34
48 : : };
49 : :
50 : : /* peer public Key*/
51 : 4 : const uint8_t peer_public[] = {
52 : : 0x70, 0x0c, 0x48, 0xf7, 0x7f, 0x56, 0x58, 0x4c,
53 : : 0x5c, 0xc6, 0x32, 0xca, 0x65, 0x64, 0x0d, 0xb9,
54 : : 0x1b, 0x6b, 0xac, 0xce, 0x3a, 0x4d, 0xf6, 0xb4,
55 : : 0x2c, 0xe7, 0xcc, 0x83, 0x88, 0x33, 0xd2, 0x87,
56 : : 0xdb, 0x71, 0xe5, 0x09, 0xe3, 0xfd, 0x9b, 0x06,
57 : : 0x0d, 0xdb, 0x20, 0xba, 0x5c, 0x51, 0xdc, 0xc5,
58 : : 0x94, 0x8d, 0x46, 0xfb, 0xf6, 0x40, 0xdf, 0xe0,
59 : : 0x44, 0x17, 0x82, 0xca, 0xb8, 0x5f, 0xa4, 0xac
60 : : };
61 : :
62 : : /* expected ecdh common secret*/
63 : 4 : const uint8_t expected_ecdh_secret[] = {
64 : : 0x46, 0xfc, 0x62, 0x10, 0x64, 0x20, 0xff, 0x01,
65 : : 0x2e, 0x54, 0xa4, 0x34, 0xfb, 0xdd, 0x2d, 0x25,
66 : : 0xcc, 0xc5, 0x85, 0x20, 0x60, 0x56, 0x1e, 0x68,
67 : : 0x04, 0x0d, 0xd7, 0x77, 0x89, 0x97, 0xbd, 0x7b
68 : : };
69 : :
70 : 4 : ec_context = libspdm_ec_new_by_nid(LIBSPDM_CRYPTO_NID_ECDSA_NIST_P256);
71 [ - + ]: 4 : if (ec_context == NULL) {
72 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDH new failed \n"));
73 : 0 : result = false;
74 : 0 : goto update;
75 : : }
76 : :
77 : 4 : result = libspdm_ec_set_priv_key(ec_context, self_privkey, sizeof(self_privkey));
78 [ - + ]: 4 : if (!result) {
79 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDH set private key failed \n"));
80 : 0 : libspdm_ec_free(ec_context);
81 : 0 : result = false;
82 : 0 : goto update;
83 : : }
84 : :
85 : 4 : result = libspdm_ec_compute_key(ec_context, peer_public, sizeof(peer_public), common_key,
86 : : &common_key_length);
87 [ - + ]: 4 : if (!result) {
88 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDH compute key failed \n"));
89 : 0 : libspdm_ec_free(ec_context);
90 : 0 : result = false;
91 : 0 : goto update;
92 : : }
93 : :
94 : : /*KAT test*/
95 [ - + ]: 4 : if (common_key_length != sizeof(expected_ecdh_secret)) {
96 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDH KAT failed \n"));
97 : 0 : libspdm_ec_free(ec_context);
98 : 0 : result = false;
99 : 0 : goto update;
100 : : }
101 : :
102 [ - + ]: 4 : if (!libspdm_consttime_is_mem_equal(common_key, expected_ecdh_secret,
103 : : sizeof(expected_ecdh_secret))) {
104 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDH KAT failed \n"));
105 : 0 : libspdm_ec_free(ec_context);
106 : 0 : result = false;
107 : 0 : goto update;
108 : : }
109 : :
110 : 4 : libspdm_ec_free(ec_context);
111 : :
112 : 4 : update:
113 : : /* mark it as tested*/
114 : 4 : context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_ECDH;
115 : :
116 : : /* record test result*/
117 [ + - ]: 4 : if (result) {
118 : 4 : context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_ECDH;
119 : : } else {
120 : 0 : context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_ECDH;
121 : : }
122 : :
123 : : #endif/*LIBSPDM_ECDHE_SUPPORT*/
124 : : }
125 : :
126 : : #endif/*LIBSPDM_FIPS_MODE*/
|