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 : * AES_GCM self_test
15 : **/
16 1 : bool libspdm_fips_selftest_aes_gcm(void *fips_selftest_context)
17 : {
18 1 : bool result = true;
19 :
20 : #if LIBSPDM_AEAD_GCM_SUPPORT
21 1 : libspdm_fips_selftest_context_t *context = fips_selftest_context;
22 1 : LIBSPDM_ASSERT(fips_selftest_context != NULL);
23 :
24 : /* any test fail cause the FIPS fail*/
25 1 : if (context->tested_algo != context->self_test_result) {
26 0 : return false;
27 : }
28 :
29 : /* check if run before.*/
30 1 : if ((context->tested_algo & LIBSPDM_FIPS_SELF_TEST_AES_GCM) != 0) {
31 0 : return true;
32 : }
33 :
34 : uint8_t output_ciphertext[1024];
35 : uint8_t output_plaintext[1024];
36 : uint8_t output_tag[1024];
37 : size_t output_ciphertext_size;
38 : size_t output_plaintext_size;
39 :
40 1 : output_ciphertext_size = sizeof(output_ciphertext);
41 1 : output_plaintext_size = sizeof(output_plaintext);
42 :
43 : /*Key to use crypto operation*/
44 1 : const uint8_t key[] = {
45 : 0x01, 0xd1, 0xa6, 0x9e, 0x73, 0x7f, 0xf2, 0xea,
46 : 0x53, 0x56, 0x6f, 0xa2, 0xd7, 0xc0, 0x80, 0xd6
47 : };
48 :
49 : /*IV to perform the crypto operation upon*/
50 1 : const uint8_t iv[] = {
51 : 0x0f, 0x3e, 0xd5, 0x9c, 0xa5, 0xb3, 0x0f, 0xb5,
52 : 0xea, 0x4e, 0x13, 0x60
53 : };
54 :
55 : /*Data to perform the crypto operation upon*/
56 1 : const uint8_t input[] = {
57 : 0xf6, 0x28, 0xc8, 0x61, 0xe5, 0x97, 0x04, 0xd9,
58 : 0xba, 0xc8, 0x00, 0xc0, 0x4c, 0x1a, 0x2d, 0x7e,
59 : 0x6c, 0x44, 0x61, 0x3f, 0xa4, 0x64, 0xb0, 0xe1,
60 : 0x17, 0x3d, 0x8d, 0xba, 0xc0, 0x14, 0x72, 0xd3,
61 : 0xc6, 0x8a, 0x5e, 0xb4, 0xf3, 0x16, 0x7f, 0xd0,
62 : 0x21, 0x29, 0x76, 0x85, 0x86, 0x4d, 0x78, 0x86,
63 : 0x14, 0x98, 0x32, 0x5e, 0xa6, 0xda, 0x6f, 0xce,
64 : 0x98, 0x2c, 0xe9, 0x6e, 0xaa, 0x77, 0x18, 0xb8,
65 : 0x89, 0xb7, 0x2d, 0x13, 0xc5, 0x61, 0xb3, 0xaf,
66 : 0xe7, 0x13, 0xa7, 0x38, 0x45, 0xd4, 0x4c, 0x4a,
67 : 0xc6, 0xdc, 0xe5, 0x29, 0x6c, 0xd5, 0xd9, 0xc6,
68 : 0xde, 0xe2, 0x5d, 0x78, 0xfd, 0xa7, 0x3a, 0x45,
69 : 0x7e, 0xdf, 0x00, 0xd0, 0x6a, 0xb0, 0xe8, 0x3a,
70 : 0x86, 0x48, 0xa7, 0xaf, 0x7e, 0x6f, 0x33, 0xb2
71 : };
72 :
73 : /*Additional auth data*/
74 1 : const uint8_t aad[] = {
75 : 0x66, 0x2a, 0x0a, 0x62, 0xe2, 0xb5, 0xa5, 0xa9,
76 : 0xae, 0x46, 0x19, 0x16, 0x46, 0xf5, 0x26, 0xd8,
77 : 0x8b, 0xf1, 0xac, 0xe1, 0x11, 0xee, 0xcf, 0x66,
78 : 0x8c, 0x3b, 0xde, 0x57, 0x42, 0x2b, 0xa8, 0x02,
79 : 0xc4, 0x60, 0x24, 0xb3, 0xa3, 0x84, 0xb5, 0x52,
80 : 0x12, 0x98, 0xfe, 0x1e
81 : };
82 :
83 : /*Expected ciphertext*/
84 1 : const uint8_t expected_ciphertext[] = {
85 : 0x2d, 0x4a, 0x0b, 0x34, 0x20, 0xcd, 0x7a, 0xe7,
86 : 0x91, 0x1e, 0x5a, 0x53, 0x5e, 0x2b, 0x7b, 0x8e,
87 : 0x42, 0x37, 0xf0, 0xeb, 0x5a, 0x84, 0xc5, 0xea,
88 : 0x95, 0xd3, 0xe7, 0xe2, 0xb4, 0xb8, 0x88, 0xe0,
89 : 0x4f, 0x28, 0xe3, 0x41, 0x7f, 0x05, 0x8d, 0x7c,
90 : 0x4d, 0xae, 0x05, 0x92, 0xfc, 0x27, 0xfc, 0x67,
91 : 0x94, 0x9f, 0x24, 0xa5, 0x5e, 0x70, 0xd9, 0xa7,
92 : 0xb3, 0xd2, 0x78, 0xf0, 0xcd, 0x75, 0x4e, 0x43,
93 : 0xe8, 0xad, 0xd4, 0x54, 0x57, 0xf8, 0x67, 0x1d,
94 : 0x31, 0xbf, 0x45, 0xef, 0x1f, 0xaf, 0xec, 0x3b,
95 : 0x4b, 0x3c, 0x90, 0x65, 0x83, 0x32, 0x6c, 0x9b,
96 : 0x5c, 0xc2, 0x30, 0xc4, 0x5a, 0x6e, 0xec, 0x74,
97 : 0xe3, 0x51, 0x11, 0xb5, 0x51, 0x04, 0xc5, 0xc0,
98 : 0x68, 0x50, 0xc4, 0xb8, 0xd1, 0x9a, 0x8e, 0x37
99 : };
100 :
101 : /*Expected Auth Tag*/
102 1 : const uint8_t expected_tag[] = {
103 : 0xc1, 0x37, 0xee, 0x11, 0x32, 0xf3, 0x75, 0x0a,
104 : 0xd6, 0x57, 0x78, 0x77, 0x40, 0x05, 0x91, 0x41
105 : };
106 :
107 : /*KAT test*/
108 1 : libspdm_zero_mem(output_ciphertext, sizeof(output_ciphertext));
109 1 : libspdm_zero_mem(output_tag, sizeof(output_tag));
110 1 : result = libspdm_aead_aes_gcm_encrypt(key, sizeof(key), iv, sizeof(iv), aad, sizeof(aad),
111 : input, sizeof(input), output_tag, sizeof(expected_tag),
112 : output_ciphertext, &output_ciphertext_size);
113 1 : if (!result) {
114 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "aes_gcm encrypt failed \n"));
115 0 : goto update;
116 : }
117 :
118 1 : if (output_ciphertext_size != sizeof(expected_ciphertext)) {
119 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "aes_gcm KAT failed \n"));
120 0 : result = false;
121 0 : goto update;
122 : }
123 :
124 1 : if (!libspdm_consttime_is_mem_equal(output_ciphertext, expected_ciphertext,
125 : sizeof(expected_ciphertext))) {
126 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "aes_gcm KAT failed \n"));
127 0 : result = false;
128 0 : goto update;
129 : }
130 :
131 1 : if (!libspdm_consttime_is_mem_equal(output_tag, expected_tag, sizeof(expected_tag))) {
132 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "aes_gcm KAT failed \n"));
133 0 : result = false;
134 0 : goto update;
135 : }
136 :
137 1 : libspdm_zero_mem(output_plaintext, sizeof(output_plaintext));
138 1 : result = libspdm_aead_aes_gcm_decrypt(key, sizeof(key), iv, sizeof(iv), aad, sizeof(aad),
139 : expected_ciphertext, sizeof(expected_ciphertext),
140 : expected_tag, sizeof(expected_tag),
141 : output_plaintext, &output_plaintext_size);
142 1 : if (!result) {
143 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "aes_gcm decrypt failed \n"));
144 0 : result = false;
145 0 : goto update;
146 : }
147 :
148 1 : if (output_plaintext_size != sizeof(input)) {
149 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "aes_gcm selftest failed \n"));
150 0 : result = false;
151 0 : goto update;
152 : }
153 :
154 1 : if (!libspdm_consttime_is_mem_equal(output_plaintext, input, sizeof(input))) {
155 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "aes_gcm selftest failed \n"));
156 0 : result = false;
157 0 : goto update;
158 : }
159 :
160 1 : update:
161 : /* mark it as tested*/
162 1 : context->tested_algo |= LIBSPDM_FIPS_SELF_TEST_AES_GCM;
163 :
164 : /* record test result*/
165 1 : if (result) {
166 1 : context->self_test_result |= LIBSPDM_FIPS_SELF_TEST_AES_GCM;
167 : } else {
168 0 : context->self_test_result &= ~LIBSPDM_FIPS_SELF_TEST_AES_GCM;
169 : }
170 :
171 : #endif/*LIBSPDM_AEAD_GCM_SUPPORT*/
172 :
173 1 : return result;
174 : }
175 :
176 : #endif
|