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_sha3_256(void *fips_selftest_context)
14 : {
15 1 : bool result = true;
16 :
17 : #if LIBSPDM_SHA3_256_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_SHA3_256) != 0) {
28 0 : return true;
29 : }
30 :
31 1 : const uint8_t msg[] = {0x7f, 0x94};
32 : /*Test Vectors: https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#sha3vsha3vss */
33 : uint8_t sha3_256_result[32];
34 1 : const uint8_t sha3_256_answer[] = {
35 : 0xde, 0x01, 0x6a, 0xcf, 0xc1, 0xa2, 0xe2, 0x2e,
36 : 0x39, 0x52, 0x6c, 0x60, 0x9d, 0x9c, 0x69, 0xd8,
37 : 0x56, 0xa5, 0x43, 0xfe, 0xbb, 0x3c, 0xb4, 0x26,
38 : 0xee, 0x1f, 0x13, 0x18, 0xd7, 0x80, 0xea, 0x88
39 : };
40 1 : libspdm_zero_mem(sha3_256_result, sizeof(sha3_256_result));
41 1 : result = libspdm_sha3_256_hash_all(msg, sizeof(msg), sha3_256_result);
42 1 : if (!result) {
43 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen sha3_256 failed \n"));
44 0 : result = false;
45 0 : goto update;
46 : }
47 :
48 1 : if (!libspdm_consttime_is_mem_equal(sha3_256_result, sha3_256_answer,
49 : sizeof(sha3_256_answer))) {
50 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "sha3_256 KAT failed \n"));
51 0 : result = false;
52 0 : goto update;
53 : }
54 :
55 1 : update:
56 : /* mark it as tested*/
57 1 : context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_SHA3_256;
58 :
59 : /* record test result*/
60 1 : if (result) {
61 1 : context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_SHA3_256;
62 : } else {
63 0 : context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_SHA3_256;
64 : }
65 :
66 : #endif/* LIBSPDM_SHA3_256_SUPPORT */
67 :
68 1 : return result;
69 : }
70 :
71 1 : bool libspdm_fips_selftest_sha3_384(void *fips_selftest_context)
72 : {
73 1 : bool result = true;
74 :
75 : #if LIBSPDM_SHA3_384_SUPPORT
76 1 : libspdm_fips_selftest_context_t *context = fips_selftest_context;
77 1 : LIBSPDM_ASSERT(fips_selftest_context != NULL);
78 :
79 : /* any test fail cause the FIPS fail*/
80 1 : if (context->tested_algo != context->self_test_result) {
81 0 : return false;
82 : }
83 :
84 : /* check if run before.*/
85 1 : if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_SHA3_384) != 0) {
86 0 : return true;
87 : }
88 :
89 : uint8_t sha3_384_result[48];
90 : /*Test Vectors: https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#sha3vsha3vss */
91 1 : const uint8_t msg[] = {0x89, 0xcc};
92 1 : const uint8_t sha3_384_answer[] = {
93 : 0xcf, 0x9b, 0xe5, 0x91, 0x0e, 0x2c, 0x4f, 0x89,
94 : 0x5b, 0x9e, 0x92, 0x08, 0x02, 0x26, 0x52, 0xbb,
95 : 0x4d, 0x6a, 0x7e, 0x85, 0x84, 0x5b, 0x2a, 0x6c,
96 : 0x22, 0x1c, 0x22, 0x84, 0x1e, 0xc0, 0x74, 0x64,
97 : 0xae, 0xe9, 0xfb, 0x5f, 0x89, 0x38, 0xb2, 0xda,
98 : 0xa8, 0x7b, 0xe3, 0x37, 0xf0, 0x38, 0xcb, 0xcf
99 : };
100 1 : libspdm_zero_mem(sha3_384_result, sizeof(sha3_384_result));
101 1 : result = libspdm_sha3_384_hash_all(msg, sizeof(msg), sha3_384_result);
102 1 : if (!result) {
103 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen sha3_384 failed \n"));
104 0 : result = false;
105 0 : goto update;
106 : }
107 :
108 1 : if (!libspdm_consttime_is_mem_equal(sha3_384_result, sha3_384_answer,
109 : sizeof(sha3_384_answer))) {
110 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "sha3_384 KAT failed \n"));
111 0 : result = false;
112 0 : goto update;
113 : }
114 :
115 1 : update:
116 : /* mark it as tested*/
117 1 : context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_SHA3_384;
118 :
119 : /* record test result*/
120 1 : if (result) {
121 1 : context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_SHA3_384;
122 : } else {
123 0 : context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_SHA3_384;
124 : }
125 :
126 : #endif/* LIBSPDM_SHA3_384_SUPPORT */
127 :
128 1 : return result;
129 : }
130 :
131 1 : bool libspdm_fips_selftest_sha3_512(void *fips_selftest_context)
132 : {
133 1 : bool result = true;
134 :
135 : #if LIBSPDM_SHA3_512_SUPPORT
136 1 : libspdm_fips_selftest_context_t *context = fips_selftest_context;
137 1 : LIBSPDM_ASSERT(fips_selftest_context != NULL);
138 :
139 : /* any test fail cause the FIPS fail*/
140 1 : if (context->tested_algo != context->self_test_result) {
141 0 : return false;
142 : }
143 :
144 : /* check if run before.*/
145 1 : if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_SHA3_512) != 0) {
146 0 : return true;
147 : }
148 :
149 : uint8_t sha3_512_result[64];
150 : /*Test Vectors: https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#sha3vsha3vss */
151 1 : const uint8_t msg[] = {0xb1, 0x39};
152 1 : const uint8_t sha3_512_answer[] = {
153 : 0xd3, 0xe5, 0xc0, 0x26, 0x47, 0x05, 0xe8, 0x1d,
154 : 0x0c, 0x90, 0xf9, 0x9d, 0xae, 0xff, 0x00, 0x89,
155 : 0xfa, 0x3e, 0x91, 0x77, 0xd3, 0xd5, 0xbc, 0x74,
156 : 0x9c, 0xde, 0x10, 0xf0, 0x35, 0xf4, 0x95, 0x65,
157 : 0x55, 0x44, 0xf8, 0x57, 0x79, 0x91, 0x71, 0x2e,
158 : 0xb5, 0x18, 0x01, 0x5b, 0xe2, 0x9d, 0x19, 0x5b,
159 : 0x7e, 0xbf, 0x61, 0xe8, 0xd2, 0x93, 0x90, 0xea,
160 : 0xf1, 0x47, 0x88, 0x08, 0x2b, 0x11, 0x97, 0x6d
161 : };
162 1 : libspdm_zero_mem(sha3_512_result, sizeof(sha3_512_result));
163 1 : result = libspdm_sha3_512_hash_all(msg, sizeof(msg), sha3_512_result);
164 1 : if (!result) {
165 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "gen sha3_512 failed \n"));
166 0 : result = false;
167 0 : goto update;
168 : }
169 :
170 1 : if (!libspdm_consttime_is_mem_equal(sha3_512_result, sha3_512_answer,
171 : sizeof(sha3_512_answer))) {
172 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "sha3_512 KAT failed \n"));
173 0 : result = false;
174 0 : goto update;
175 : }
176 :
177 1 : update:
178 : /* mark it as tested*/
179 1 : context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_SHA3_512;
180 :
181 : /* record test result*/
182 1 : if (result) {
183 1 : context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_SHA3_512;
184 : } else {
185 0 : context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_SHA3_512;
186 : }
187 :
188 : #endif/* LIBSPDM_SHA3_512_SUPPORT */
189 :
190 1 : return result;
191 : }
192 :
193 : #endif/*LIBSPDM_FIPS_MODE*/
|