summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/default/src/console_cmd/vboot_hash.c
blob: bdc9dddc66e10d0937041f918ef7c9dcf8cd8940 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "console.h"
#include "flash.h"
#include "printf.h"
#include "sha256.h"
#include "system.h"
#include "test/drivers/test_state.h"
#include "vboot_hash.h"

#include <string.h>

#include <zephyr/fff.h>
#include <zephyr/shell/shell_dummy.h>
#include <zephyr/sys/util.h>
#include <zephyr/ztest.h>

#define CUSTOM_HASH_LENGTH (32)

struct console_cmd_hash_fixture {
	uint8_t rw_hash[SHA256_DIGEST_SIZE];
	uint8_t ro_hash[SHA256_DIGEST_SIZE];
};

ZTEST_F(console_cmd_hash, get_rw)
{
	const char *outbuffer;
	size_t buffer_size;

	/* Start calculating the RW image hash */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "hash rw"), NULL);

	/* Wait for completion */
	WAIT_FOR(!vboot_hash_in_progress(), 1000000, k_sleep(K_MSEC(10)));

	/* Call again with no args to see the resulting hash */
	shell_backend_dummy_clear_output(get_ec_shell());
	zassert_ok(shell_execute_cmd(get_ec_shell(), "hash"), NULL);
	outbuffer =
		shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);

	/* The RW hash should be reported. */
	char hash_buf[hex_str_buf_size(SHA256_DIGEST_SIZE)];

	snprintf_hex_buffer(hash_buf, sizeof(hash_buf),
			    HEX_BUF(fixture->rw_hash, SHA256_DIGEST_SIZE));

	zassert_ok(!strstr(outbuffer, hash_buf), "Output was: `%s`", outbuffer);
}

ZTEST_F(console_cmd_hash, get_ro)
{
	const char *outbuffer;
	size_t buffer_size;

	/* Start calculating the RW image hash */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "hash ro"), NULL);

	/* Wait for completion */
	WAIT_FOR(!vboot_hash_in_progress(), 1000000, k_sleep(K_MSEC(10)));

	/* Call again with no args to see the resulting hash */
	shell_backend_dummy_clear_output(get_ec_shell());
	zassert_ok(shell_execute_cmd(get_ec_shell(), "hash"), NULL);
	outbuffer =
		shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);

	/* The RO hash should be reported. */
	char hash_buf[hex_str_buf_size(SHA256_DIGEST_SIZE)];

	snprintf_hex_buffer(hash_buf, sizeof(hash_buf),
			    HEX_BUF(fixture->ro_hash, SHA256_DIGEST_SIZE));

	zassert_ok(!strstr(outbuffer, hash_buf), "Output was: `%s`", outbuffer);
}

ZTEST_F(console_cmd_hash, abort)
{
	const char *outbuffer;
	size_t buffer_size;

	/* Start calculating the RO image hash */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "hash ro"), NULL);

	/* Immediately cancel */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "hash abort"), NULL);

	/* Call again with no args to check status */
	shell_backend_dummy_clear_output(get_ec_shell());
	zassert_ok(shell_execute_cmd(get_ec_shell(), "hash"), NULL);
	outbuffer =
		shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);

	/* Depending on timing, we should see one of these statuses */
	zassert_true(strstr(outbuffer, "(invalid)") ||
			     strstr(outbuffer, "(aborting)"),
		     "Output was: `%s`", outbuffer);
}

ZTEST_F(console_cmd_hash, custom_range)
{
	char command[256];
	uint32_t offset = flash_get_rw_offset(system_get_active_copy());

	/* Hash an arbitrary portion of the flash */
	snprintf(command, sizeof(command), "hash 0x%x %u", offset,
		 CUSTOM_HASH_LENGTH);
	zassert_ok(shell_execute_cmd(get_ec_shell(), command));

	WAIT_FOR(!vboot_hash_in_progress(), 1000000, k_sleep(K_MSEC(10)));

	/* Calculate the expected hash */
	struct sha256_ctx hash_ctx;
	uint8_t *hash;
	uint8_t buf[CUSTOM_HASH_LENGTH];

	zassert_ok(crec_flash_read(offset, sizeof(buf), buf));
	SHA256_init(&hash_ctx);
	SHA256_update(&hash_ctx, buf, sizeof(buf));
	hash = SHA256_final(&hash_ctx);

	/* Call again with no args to check status */
	const char *outbuffer;
	size_t buffer_size;

	shell_backend_dummy_clear_output(get_ec_shell());
	zassert_ok(shell_execute_cmd(get_ec_shell(), "hash"));
	outbuffer =
		shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);

	/* Compare hashes */
	char hash_buf[hex_str_buf_size(SHA256_DIGEST_SIZE)];

	snprintf_hex_buffer(hash_buf, sizeof(hash_buf),
			    HEX_BUF(hash, SHA256_DIGEST_SIZE));

	zassert_ok(!strstr(outbuffer, hash_buf), "Output was: `%s`. Actual: %s",
		   outbuffer, hash_buf);
}

ZTEST_F(console_cmd_hash, custom_range_with_nonce)
{
	char command[256];
	uint32_t offset = flash_get_rw_offset(system_get_active_copy());
	int nonce = 1234;

	/* Hash an arbitrary portion of the flash w/ nonce */
	snprintf(command, sizeof(command), "hash 0x%x %u %d", offset,
		 CUSTOM_HASH_LENGTH, nonce);
	zassert_ok(shell_execute_cmd(get_ec_shell(), command));

	WAIT_FOR(!vboot_hash_in_progress(), 1000000, k_sleep(K_MSEC(10)));

	/* Calculate the expected hash */
	struct sha256_ctx hash_ctx;
	uint8_t *hash;
	uint8_t buf[CUSTOM_HASH_LENGTH];

	zassert_ok(crec_flash_read(offset, sizeof(buf), buf));
	SHA256_init(&hash_ctx);
	SHA256_update(&hash_ctx, (const uint8_t *)&nonce, sizeof(nonce));
	SHA256_update(&hash_ctx, buf, sizeof(buf));
	hash = SHA256_final(&hash_ctx);

	/* Call again with no args to check status */
	const char *outbuffer;
	size_t buffer_size;

	shell_backend_dummy_clear_output(get_ec_shell());
	zassert_ok(shell_execute_cmd(get_ec_shell(), "hash"));
	outbuffer =
		shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);

	/* Compare hashes */
	char hash_buf[hex_str_buf_size(SHA256_DIGEST_SIZE)];

	snprintf_hex_buffer(hash_buf, sizeof(hash_buf),
			    HEX_BUF(hash, SHA256_DIGEST_SIZE));

	zassert_ok(!strstr(outbuffer, hash_buf), "Output was: `%s`. Actual: %s",
		   outbuffer, hash_buf);
}

ZTEST(console_cmd_hash, invalid)
{
	/* Invalid subcommand */
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "hash foo"));

	/* For custom ranges, non-numbers are invalid */
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "hash a b"));
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "hash 1 b"));
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "hash 1 2 c"));
}

static void *setup(void)
{
	static struct console_cmd_hash_fixture f;

	return &f;
}

static void before(void *data)
{
	struct console_cmd_hash_fixture *f =
		(struct console_cmd_hash_fixture *)data;
	const uint8_t *hash_ptr;
	int rv;

	/* Stop and clear current hash */
	vboot_hash_abort();

	/* Get the RW hash and save it to our fixture */
	rv = vboot_get_rw_hash(&hash_ptr);
	zassert_ok(rv, "Got %d", rv);
	memcpy(f->rw_hash, hash_ptr, sizeof(f->rw_hash));

	/* Compute the RO hash, too */
	rv = vboot_get_ro_hash(&hash_ptr);
	zassert_ok(rv, "Got %d", rv);
	memcpy(f->ro_hash, hash_ptr, sizeof(f->ro_hash));
}

static void after(void *data)
{
	ARG_UNUSED(data);

	/* Stop and clear current hash */
	vboot_hash_abort();

	/* Wait a moment to allow the hashing to stop */
	k_sleep(K_MSEC(100));
}

ZTEST_SUITE(console_cmd_hash, drivers_predicate_post_main, setup, before, after,
	    NULL);