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 : : #if LIBSPDM_ECDSA_SUPPORT
14 : : /*hardcode random for ecdsa*/
15 : 6 : static int libspdm_hardcode_random_number_ecdsa(void *rng_state, unsigned char *output, size_t len)
16 : : {
17 : : /*Random number*/
18 : 6 : const uint8_t random[] = {
19 : : 0x01, 0x5b, 0x92, 0x22, 0xfc, 0x35, 0xc5, 0x2b,
20 : : 0x79, 0x1c, 0xcd, 0x37, 0x07, 0x7d, 0xae, 0x6a,
21 : : 0x88, 0x1f, 0x7d, 0x03, 0x30, 0x92, 0x67, 0x47,
22 : : 0x0b, 0xce, 0x31, 0x7c, 0x46, 0xdd, 0x90, 0xbf
23 : : };
24 : 6 : libspdm_copy_mem(output, len, random, sizeof(random));
25 : :
26 : 6 : return 0;
27 : : }
28 : : #endif /* LIBSPDM_ECDSA_SUPPORT */
29 : :
30 : 8 : void libspdm_fips_selftest_ecdsa(void *fips_selftest_context)
31 : : {
32 : : #if LIBSPDM_ECDSA_SUPPORT
33 : 8 : bool result = true;
34 : :
35 : 8 : libspdm_fips_selftest_context_t *context = fips_selftest_context;
36 : 8 : LIBSPDM_ASSERT(fips_selftest_context != NULL);
37 : :
38 : : /* any test fail cause the FIPS fail*/
39 [ + + ]: 8 : if (context->tested_algo != context->self_test_result) {
40 : 4 : return;
41 : : }
42 : :
43 : : /* check if run before.*/
44 [ + + ]: 6 : if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_ECDSA) != 0) {
45 : 2 : return;
46 : : }
47 : :
48 : : uint8_t signature[32 * 2];
49 : : size_t sig_size;
50 : : void *ec_context;
51 : :
52 : 4 : sig_size = sizeof(signature);
53 : 4 : libspdm_zero_mem(signature, sig_size);
54 : :
55 : : /* Message hash */
56 : 4 : const uint8_t message_hash[] = {
57 : : 0x9a, 0x1b, 0x6d, 0xe3, 0xff, 0xd0, 0x7e, 0x77,
58 : : 0xe2, 0x54, 0xc6, 0x27, 0x9a, 0xc3, 0x28, 0x07,
59 : : 0x42, 0x43, 0x1a, 0x32, 0xc8, 0xaf, 0x0c, 0x87,
60 : : 0x94, 0xff, 0x40, 0x75, 0x09, 0xfe, 0x40, 0xd8
61 : : };
62 : :
63 : : /* Private Key */
64 : 4 : const uint8_t priv_key[] = {
65 : : 0xd6, 0x84, 0xd1, 0x7c, 0xe3, 0x6b, 0xe7, 0x08,
66 : : 0xbc, 0xd9, 0x89, 0x3f, 0xbb, 0xf4, 0xf2, 0xcf,
67 : : 0x8d, 0x7f, 0xd4, 0x72, 0xbc, 0xfb, 0x54, 0x29,
68 : : 0xd9, 0x86, 0xe2, 0x86, 0xc2, 0x38, 0xe5, 0xed
69 : : };
70 : :
71 : : /* Public Key */
72 : 4 : const uint8_t public_key[] = {
73 : : 0x54, 0xbc, 0x5f, 0x6b, 0x70, 0x9b, 0x29, 0x5c,
74 : : 0xa9, 0x43, 0xd0, 0xb7, 0xf3, 0xa2, 0x4b, 0xf0,
75 : : 0x76, 0xb1, 0xd1, 0x9f, 0x55, 0x6a, 0x4e, 0xa0,
76 : : 0x40, 0x54, 0xd2, 0xb1, 0x2f, 0x0f, 0xc1, 0x6d,
77 : : 0xe7, 0x53, 0xe1, 0x3a, 0xd9, 0xb9, 0x2d, 0xd6,
78 : : 0x3a, 0xda, 0x9d, 0xa9, 0xa9, 0x4e, 0xdd, 0x30,
79 : : 0x60, 0x24, 0x9f, 0x9d, 0xcb, 0xfc, 0x1a, 0x56,
80 : : 0x35, 0x63, 0x64, 0xe2, 0x64, 0xcf, 0x00, 0xed
81 : : };
82 : :
83 : : /* Expected signature*/
84 : 4 : const uint8_t expected_signature[] = {
85 : : 0xe1, 0x6a, 0xe9, 0x76, 0x61, 0x97, 0x8e, 0xe9,
86 : : 0xc7, 0x0f, 0xe0, 0x20, 0xc0, 0x65, 0xe1, 0x6c,
87 : : 0x89, 0x6f, 0x24, 0x6b, 0x4e, 0x88, 0x10, 0xcd,
88 : : 0xb4, 0x9a, 0xcc, 0x20, 0xcf, 0xa5, 0xb0, 0xc9,
89 : : 0xc1, 0x02, 0x4b, 0xa7, 0x41, 0xef, 0x51, 0x8f,
90 : : 0xe3, 0x11, 0xec, 0x95, 0xe2, 0xf4, 0x83, 0x97,
91 : : 0x3d, 0x32, 0x72, 0xf6, 0x4b, 0x34, 0xd3, 0x9f,
92 : : 0x25, 0x6a, 0x12, 0x3b, 0x7c, 0x87, 0xc4, 0x4d
93 : : };
94 : :
95 : 4 : ec_context = libspdm_ec_new_by_nid(LIBSPDM_CRYPTO_NID_ECDSA_NIST_P256);
96 [ - + ]: 4 : if (ec_context == NULL) {
97 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ec_context new failed \n"));
98 : 0 : result = false;
99 : 0 : goto update;
100 : : }
101 : :
102 : 4 : result = libspdm_ec_set_pub_key(ec_context, public_key, sizeof(public_key));
103 [ - + ]: 4 : if (!result) {
104 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ec_context set pub_key failed \n"));
105 : 0 : libspdm_ec_free(ec_context);
106 : 0 : result = false;
107 : 0 : goto update;
108 : : }
109 : :
110 : 4 : result = libspdm_ec_set_priv_key(ec_context, priv_key, sizeof(priv_key));
111 [ - + ]: 4 : if (!result) {
112 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ec_context set priv_key failed \n"));
113 : 0 : libspdm_ec_free(ec_context);
114 : 0 : result = false;
115 : 0 : goto update;
116 : : }
117 : :
118 : : /*ECDSA KAT test*/
119 : 4 : result = libspdm_ecdsa_sign_ex(ec_context, LIBSPDM_CRYPTO_NID_SHA256,
120 : : message_hash, sizeof(message_hash),
121 : : signature, &sig_size,
122 : : libspdm_hardcode_random_number_ecdsa);
123 [ - + ]: 4 : if (!result) {
124 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDSA sign failed \n"));
125 : 0 : libspdm_ec_free(ec_context);
126 : 0 : result = false;
127 : 0 : goto update;
128 : : }
129 : :
130 [ - + ]: 4 : if (sig_size != sizeof(expected_signature)) {
131 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDSA KAT failed \n"));
132 : 0 : libspdm_ec_free(ec_context);
133 : 0 : result = false;
134 : 0 : goto update;
135 : : }
136 : :
137 [ - + ]: 4 : if (!libspdm_consttime_is_mem_equal(signature, expected_signature,
138 : : sizeof(expected_signature))) {
139 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDSA KAT failed \n"));
140 : 0 : libspdm_ec_free(ec_context);
141 : 0 : result = false;
142 : 0 : goto update;
143 : : }
144 : :
145 : 4 : result = libspdm_ecdsa_verify(ec_context, LIBSPDM_CRYPTO_NID_SHA256,
146 : : message_hash, sizeof(message_hash),
147 : : signature, sig_size);
148 [ - + ]: 4 : if (!result) {
149 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "ECDSA selftest failed \n"));
150 : 0 : libspdm_ec_free(ec_context);
151 : 0 : result = false;
152 : 0 : goto update;
153 : : }
154 : :
155 : 4 : libspdm_ec_free(ec_context);
156 : :
157 : 4 : update:
158 : : /* mark it as tested*/
159 : 4 : context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_ECDSA;
160 : :
161 : : /* record test result*/
162 [ + - ]: 4 : if (result) {
163 : 4 : context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_ECDSA;
164 : : } else {
165 : 0 : context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_ECDSA;
166 : : }
167 : :
168 : : #endif/*LIBSPDM_ECDSA_SUPPORT*/
169 : : }
170 : :
171 : : #endif/*LIBSPDM_FIPS_MODE*/
|