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 : /**
14 : * HMAC-SHA256 KAT covers SHA256 KAT.
15 : **/
16 2 : bool libspdm_fips_selftest_hmac_sha256(void *fips_selftest_context)
17 : {
18 2 : bool result = true;
19 :
20 : #if LIBSPDM_SHA256_SUPPORT
21 2 : libspdm_fips_selftest_context_t *context = fips_selftest_context;
22 2 : LIBSPDM_ASSERT(fips_selftest_context != NULL);
23 :
24 : /* any test fail cause the FIPS fail*/
25 2 : if (context->tested_algo != context->self_test_result) {
26 0 : return false;
27 : }
28 :
29 : /* check if run before.*/
30 2 : if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_HMAC_SHA256) != 0) {
31 1 : return true;
32 : }
33 :
34 1 : const uint8_t key[32] = {0};
35 1 : const uint8_t msg[32] = {0};
36 :
37 : uint8_t hmac_256_result[32];
38 1 : const uint8_t hmac_sha256_answer[] = {
39 : 0x33, 0xad, 0x0a, 0x1c, 0x60, 0x7e, 0xc0, 0x3b,
40 : 0x09, 0xe6, 0xcd, 0x98, 0x93, 0x68, 0x0c, 0xe2,
41 : 0x10, 0xad, 0xf3, 0x00, 0xaa, 0x1f, 0x26, 0x60,
42 : 0xe1, 0xb2, 0x2e, 0x10, 0xf1, 0x70, 0xf9, 0x2a
43 : };
44 1 : libspdm_zero_mem(hmac_256_result, sizeof(hmac_256_result));
45 1 : result = libspdm_hmac_sha256_all(msg, sizeof(msg), key, sizeof(key), hmac_256_result);
46 1 : if (!result) {
47 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen hmac_sha256 failed \n"));
48 0 : goto update;
49 : }
50 :
51 1 : if (!libspdm_consttime_is_mem_equal(hmac_256_result, hmac_sha256_answer,
52 : sizeof(hmac_sha256_answer))) {
53 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "hmac_sha256 KAT failed \n"));
54 0 : result = false;
55 0 : goto update;
56 : }
57 :
58 1 : update:
59 : /* mark it as tested*/
60 1 : context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA256;
61 :
62 : /* record test result*/
63 1 : if (result) {
64 1 : context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA256;
65 : } else {
66 0 : context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_HMAC_SHA256;
67 : }
68 :
69 : #endif/*LIBSPDM_SHA256_SUPPORT*/
70 :
71 1 : return result;
72 : }
73 :
74 : /**
75 : * HMAC-SHA384 KAT covers SHA384 KAT.
76 : **/
77 2 : bool libspdm_fips_selftest_hmac_sha384(void *fips_selftest_context)
78 : {
79 2 : bool result = true;
80 :
81 : #if LIBSPDM_SHA384_SUPPORT
82 2 : libspdm_fips_selftest_context_t *context = fips_selftest_context;
83 2 : LIBSPDM_ASSERT(fips_selftest_context != NULL);
84 :
85 : /* any test fail cause the FIPS fail*/
86 2 : if (context->tested_algo != context->self_test_result) {
87 0 : return false;
88 : }
89 :
90 : /* check if run before.*/
91 2 : if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384) != 0) {
92 1 : return true;
93 : }
94 :
95 1 : const uint8_t key[32] = {0};
96 1 : const uint8_t msg[32] = {0};
97 :
98 : uint8_t hmac_384_result[48];
99 1 : const uint8_t hmac_sha384_answer[] = {
100 : 0xe6, 0x65, 0xec, 0x75, 0xdc, 0xa3, 0x23, 0xdf,
101 : 0x31, 0x80, 0x40, 0x60, 0xe1, 0xb0, 0xd8, 0x28,
102 : 0xb5, 0x0a, 0x6a, 0x8a, 0x53, 0x9c, 0xfe, 0xdd,
103 : 0x9a, 0xa0, 0x07, 0x4b, 0x5b, 0x36, 0x44, 0x5d,
104 : 0xef, 0xbc, 0x47, 0x45, 0x3d, 0xf8, 0xd0, 0xc1,
105 : 0x4b, 0x7a, 0xd2, 0x06, 0x2e, 0x7b, 0xbd, 0xb1
106 : };
107 1 : libspdm_zero_mem(hmac_384_result, sizeof(hmac_384_result));
108 1 : result = libspdm_hmac_sha384_all(msg, sizeof(msg), key, sizeof(key), hmac_384_result);
109 1 : if (!result) {
110 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen hmac_sha384 failed \n"));
111 0 : goto update;
112 : }
113 :
114 1 : if (!libspdm_consttime_is_mem_equal(hmac_384_result, hmac_sha384_answer,
115 : sizeof(hmac_sha384_answer))) {
116 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "hmac_sha384 KAT failed \n"));
117 0 : result = false;
118 0 : goto update;
119 : }
120 :
121 1 : update:
122 : /* mark it as tested*/
123 1 : context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384;
124 :
125 : /* record test result*/
126 1 : if (result) {
127 1 : context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384;
128 : } else {
129 0 : context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_HMAC_SHA384;
130 : }
131 :
132 : #endif/*LIBSPDM_SHA384_SUPPORT*/
133 :
134 1 : return result;
135 : }
136 :
137 : /**
138 : * HMAC-SHA512 KAT covers SHA512 KAT.
139 : **/
140 2 : bool libspdm_fips_selftest_hmac_sha512(void *fips_selftest_context)
141 : {
142 2 : bool result = true;
143 :
144 : #if LIBSPDM_SHA512_SUPPORT
145 2 : libspdm_fips_selftest_context_t *context = fips_selftest_context;
146 2 : LIBSPDM_ASSERT(fips_selftest_context != NULL);
147 :
148 : /* any test fail cause the FIPS fail*/
149 2 : if (context->tested_algo != context->self_test_result) {
150 0 : return false;
151 : }
152 :
153 : /* check if run before.*/
154 2 : if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512) != 0) {
155 1 : return true;
156 : }
157 :
158 1 : const uint8_t key[32] = {0};
159 1 : const uint8_t msg[32] = {0};
160 :
161 : uint8_t hmac_512_result[64];
162 1 : const uint8_t hmac_sha512_answer[] = {
163 : 0xba, 0xe4, 0x6c, 0xeb, 0xeb, 0xbb, 0x90, 0x40,
164 : 0x9a, 0xbc, 0x5a, 0xcf, 0x7a, 0xc2, 0x1f, 0xdb,
165 : 0x33, 0x9c, 0x01, 0xce, 0x15, 0x19, 0x2c, 0x52,
166 : 0xfb, 0x9e, 0x8a, 0xa1, 0x1a, 0x8d, 0xe9, 0xa4,
167 : 0xea, 0x15, 0xa0, 0x45, 0xf2, 0xbe, 0x24, 0x5f,
168 : 0xbb, 0x98, 0x91, 0x6a, 0x9a, 0xe8, 0x1b, 0x35,
169 : 0x3e, 0x33, 0xb9, 0xc4, 0x2a, 0x55, 0x38, 0x0c,
170 : 0x51, 0x58, 0x24, 0x1d, 0xae, 0xb3, 0xc6, 0xdd
171 : };
172 1 : libspdm_zero_mem(hmac_512_result, sizeof(hmac_512_result));
173 1 : result = libspdm_hmac_sha512_all(msg, sizeof(msg), key, sizeof(key), hmac_512_result);
174 1 : if (!result) {
175 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen hmac_sha512 failed \n"));
176 0 : goto update;
177 : }
178 :
179 1 : if (!libspdm_consttime_is_mem_equal(hmac_512_result, hmac_sha512_answer,
180 : sizeof(hmac_sha512_answer))) {
181 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "hmac_sha512 KAT failed \n"));
182 0 : result = false;
183 0 : goto update;
184 : }
185 1 : update:
186 : /* mark it as tested*/
187 1 : context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512;
188 :
189 : /* record test result*/
190 1 : if (result) {
191 1 : context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512;
192 : } else {
193 0 : context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_HMAC_SHA512;
194 : }
195 :
196 : #endif/*LIBSPDM_SHA512_SUPPORT*/
197 :
198 1 : return result;
199 : }
200 :
201 : #endif/*LIBSPDM_FIPS_MODE*/
|