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