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