summaryrefslogtreecommitdiff
path: root/board/cr50/fips_cmd.c
blob: 6642bd33961fdc0b574130fc642b127bfe64c382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* Copyright 2021 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "builtin/endian.h"
#include "console.h"
#include "dcrypto.h"
#include "ec_commands.h"
#include "extension.h"
#include "fips.h"
#include "fips_rand.h"
#include "flash_log.h"
#include "hooks.h"
#include "new_nvmem.h"
#include "nvmem.h"
#include "nvmem_vars.h"
#include "registers.h"
#include "scratch_reg1.h"
#include "shared_mem.h"
#include "system.h"
#include "task.h"
#include "tpm_nvmem_ops.h"
#include "u2f_impl.h"

/**
 * Create IRQ handler calling FIPS module's dcrypto_done_interrupt() on
 * interrupt. Generated code calls some of the EC OS task management
 * functions which are not security/crypto related, so to avoid rewriting
 * macro using FIPS vtable, move it outside FIPS module.
 */
DECLARE_IRQ(GC_IRQNUM_CRYPTO0_HOST_CMD_DONE_INT, dcrypto_done_interrupt, 1);

#define CPRINTS(format, args...) cprints(CC_SYSTEM, format, ##args)

/* Print on console current FIPS mode. */
static void fips_print_mode(void)
{
	enum fips_status status = fips_status();

	if (status == FIPS_UNINITIALIZED)
		CPRINTS("FIPS mode not initialized");
	else if (status & FIPS_ERROR_MASK)
		CPRINTS("FIPS error code 0x%08x, not-approved", status);
	else
		CPRINTS("Running in FIPS 140-2 %s mode",
			((status & FIPS_MODE_ACTIVE) &&
			 (status & FIPS_POWER_UP_TEST_DONE)) ?
				"approved" :
				      "not-approved");
}

/* Print time it took tests to run or print error message. */
static void fips_print_test_time(void)
{
	if (fips_last_kat_test_duration != -1ULL)
		CPRINTS("FIPS power-up tests completed in %llu",
			fips_last_kat_test_duration);
}

static void fips_print_status(void)
{
	/* Once FIPS power-up tests completed we can enable console output. */
	console_enable_output();

	fips_print_test_time();
	fips_print_mode();
}
DECLARE_HOOK(HOOK_INIT, fips_print_status, HOOK_PRIO_INIT_PRINT_FIPS_STATUS);

#ifdef CRYPTO_TEST_SETUP
static const uint8_t k_salt = NVMEM_VAR_G2F_SALT;

/* Can't include TPM2 headers, so just define constant locally. */
#define HR_NV_INDEX (1U << 24)

/* Wipe old U2F keys. */
static void u2f_zeroize_non_fips(void)
{
	const uint32_t u2fobjs[] = { TPM_HIDDEN_U2F_KEK | HR_NV_INDEX,
				     TPM_HIDDEN_U2F_KH_SALT | HR_NV_INDEX, 0 };
	/* Delete NVMEM_VAR_G2F_SALT. */
	setvar(&k_salt, sizeof(k_salt), NULL, 0);
	/* Remove U2F keys and wipe all deleted objects. */
	nvmem_erase_tpm_data_selective(u2fobjs);
}

/* Set U2F keys to old or new version. */
static void fips_set_u2f_keys(bool active)
{
	if (!active) {
		/* Old version. */
		uint8_t random[32];
		/* Create fake u2f keys old style */
		fips_trng_bytes(random, sizeof(random));
		setvar(&k_salt, sizeof(k_salt), random, sizeof(random));

		fips_trng_bytes(random, sizeof(random));
		write_tpm_nvmem_hidden(TPM_HIDDEN_U2F_KEK, sizeof(random),
				       random, 1);
		fips_trng_bytes(random, sizeof(random));
		write_tpm_nvmem_hidden(TPM_HIDDEN_U2F_KH_SALT, sizeof(random),
				       random, 1);
	} else {
		/**
		 * TODO(sukhomlinov): Implement new key generation after merging
		 * https://crrev.com/c/3034852 and adding FIPS key gen.
		 */
		u2f_zeroize_non_fips();
	}
	system_reset(EC_RESET_FLAG_SECURITY);
}
#endif

/* Console command 'fips' to report and change status, run tests */
static int cmd_fips_status(int argc, char **argv)
{
	fips_print_mode();
	fips_print_test_time();
	CPRINTS("FIPS crypto allowed: %u", fips_crypto_allowed());

	cflush();

	if (argc == 2) {
		if (!strncmp(argv[1], "test", 4)) {
			fips_power_up_tests();
			fips_print_test_time();
			fips_print_mode();
		}
#ifdef CRYPTO_TEST_SETUP
		else if (!strncmp(argv[1], "new", 3))
			fips_set_u2f_keys(true); /* we can reboot here... */
		else if (!strncmp(argv[1], "old", 3))
			fips_set_u2f_keys(false); /* we can reboot here... */
		else if (!strncmp(argv[1], "trng", 4))
			fips_break_cmd = FIPS_BREAK_TRNG;
		else if (!strncmp(argv[1], "sha", 3))
			fips_break_cmd = FIPS_BREAK_SHA256;
#endif
	}
	return 0;
}

DECLARE_SAFE_CONSOLE_COMMAND(
	fips, cmd_fips_status,
#ifdef CRYPTO_TEST_SETUP
	"[test | new | old | trng | sha]",
	"Report FIPS status, switch U2F key, run tests, simulate errors");
#else
	"[test]", "Report FIPS status, run tests");
#endif

/**
 * Vendor command implementation to report & change status, run tests.
 * Command structure:
 *
 * field     |    size  |                  note
 * =========================================================================
 * op        |    1     | 0 - get status, 1 - set FIPS ON (remove old U2F)
 *           |          | 2 - run tests, 3 .. 8 - simulate errors
 */
static enum vendor_cmd_rc fips_cmd(enum vendor_cmd_cc code, void *buf,
				   size_t input_size, size_t *response_size)
{
	uint8_t *cmd = buf;
	uint32_t fips_reverse;

	*response_size = 0;
	if (input_size != 1)
		return VENDOR_RC_BOGUS_ARGS;

	switch ((enum fips_cmd) *cmd) {
	case FIPS_CMD_GET_STATUS:
		fips_reverse = htobe32(fips_status());
		memcpy(buf, &fips_reverse, sizeof(fips_reverse));
		*response_size = sizeof(fips_reverse);
		break;
	case FIPS_CMD_TEST:
		fips_power_up_tests();
		fips_reverse = htobe32(fips_status());
		memcpy(buf, &fips_reverse, sizeof(fips_reverse));
		*response_size = sizeof(fips_reverse);
		break;
#ifdef CRYPTO_TEST_SETUP
	case FIPS_CMD_ON:
		fips_set_u2f_keys(true); /* we can reboot here... */
		break;
	case FIPS_CMD_BREAK_TRNG:
		fips_break_cmd = FIPS_BREAK_TRNG;
		break;
	case FIPS_CMD_BREAK_SHA256:
		fips_break_cmd = FIPS_BREAK_SHA256;
		break;
	case FIPS_CMD_BREAK_HMAC_SHA256:
		fips_break_cmd = FIPS_BREAK_HMAC_SHA256;
		break;
	case FIPS_CMD_BREAK_HMAC_DRBG:
		fips_break_cmd = FIPS_BREAK_HMAC_DRBG;
		break;
	case FIPS_CMD_BREAK_ECDSA:
		fips_break_cmd = FIPS_BREAK_ECDSA;
		break;
#ifdef CONFIG_FIPS_AES_CBC_256
	case FIPS_CMD_BREAK_AES256:
		fips_break_cmd = FIPS_BREAK_AES256;
		break;
#endif /* CONFIG_FIPS_AES_CBC_256 */
#ifdef CONFIG_FIPS_RSA2048
	case FIPS_CMD_BREAK_RSA2048:
		fips_break_cmd = FIPS_BREAK_RSA2048;
		break;
#endif /* CONFIG_FIPS_RSA2048 */
	case FIPS_CMD_NO_BREAK:
		fips_break_cmd = FIPS_NO_BREAK;
		break;
#endif /* CRYPTO_TEST_SETUP */
	default:
		return VENDOR_RC_BOGUS_ARGS;
	}

	return VENDOR_RC_SUCCESS;
}

DECLARE_VENDOR_COMMAND(VENDOR_CC_FIPS_CMD, fips_cmd);