summaryrefslogtreecommitdiff
path: root/firmware/bdb/secrets.c
blob: bd6c6fcfeaf4263aa95ede67a72895c84c2c171a (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* Copyright 2016 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 "2sysincludes.h"
#include "2hmac.h"
#include "2sha.h"
#include "bdb_api.h"
#include "bdb_struct.h"
#include "bdb.h"
#include "secrets.h"

const uint8_t secret_constant_a[] = {
	0xad, 0xf8, 0xd1, 0xd9, 0x48, 0xe6, 0xb3, 0xe4, 0xe0, 0xc4,
	0xd8, 0x66, 0x97, 0x95, 0x71, 0xa8, 0xc6, 0xc6, 0xc6, 0xc6,
	0xc6, 0xc6,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uint8_t secret_constant_kv1[] = {
	0x0a, 0x9e, 0xc9, 0x20, 0x29, 0xa3, 0x5d, 0xd7, 0x27, 0x55,
	0xb6, 0xa6, 0xb4, 0x80, 0x7c, 0x73, 0xc6, 0xc6, 0xc6, 0xc6,
	0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
	0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
	0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
	0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
	0xc6, 0xc6, 0xc6, 0x08};

const uint8_t secret_constant_k[] = {
	/*
	 * Digest of kernel data key struct fills first 32 bytes
	 */
	            0x1e, 0x1d, 0xec, 0xf2, 0x6d, 0x27, 0xa6, 0xd9,
	0x67, 0x0f, 0x34, 0xc5, 0xfa, 0x01, 0x68, 0xf6, 0xc6, 0xc6,
	0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
	0xc6, 0xc6, 0xc6, 0x03};

const uint8_t secret_constant_l[] = {
	/*
	 * Digest of kernel data key struct fills first 32 bytes
	 */
	            0x9b, 0xc0, 0x29, 0xd3, 0xc3, 0x90, 0x7f, 0x82,
	0x56, 0xe2, 0x67, 0x79, 0x11, 0x74, 0xbe, 0xd0, 0xc6, 0xc6,
	0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
	0xc6, 0xc6, 0xc6, 0x04};

const uint8_t secret_constant_p[] = {
	/*
	 * Digest of KDB key struct fills first 32 bytes
	 */
	            0xfe, 0x31, 0xed, 0xed, 0x45, 0xfd, 0x8a, 0x5d,
	0x87, 0x90, 0xac, 0x17, 0x02, 0x89, 0x2c, 0xba, 0xc6, 0xc6,
	0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
	0xc6, 0xc6, 0xc6, 0x0c};

const uint8_t secret_constant_q[] = {
	/*
	 * Digest of KDB key struct fills first 32 bytes
	 */
	            0xc7, 0x60, 0x83, 0x0f, 0x20, 0x44, 0x5d, 0x9c,
	0x70, 0x96, 0x05, 0x2d, 0x51, 0x4b, 0x15, 0x99, 0xc6, 0xc6,
	0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
	0xc6, 0xc6, 0xc6, 0x0d};

/**
 * Get constant with digest
 *
 * This function computes a digest of the given buffer and concatenates it
 * to the given constant.
 *
 * @param buf		Data from which a digest is computed
 * @param buf_size	Size of <buf>
 * @param constant	Buffer containing constant
 * @param out		Buffer where the result is stored
 * @return		BDB_SUCCESS on success or !BDB_SUCCESS otherwise
 */
static int get_constant_with_digest(const uint8_t *buf, uint32_t buf_size,
				    const uint8_t *constant, uint8_t *out)
{
	int digest_size = vb2_digest_size(VB2_HASH_SHA256);
	const struct bdb_key *key = (const struct bdb_key *)buf;

	if (!buf)
		return !BDB_SUCCESS;

	if (bdb_check_key(key, buf_size))
		return !BDB_SUCCESS;

	if (vb2_digest_buffer(buf, buf_size, VB2_HASH_SHA256, out, digest_size))
		return !BDB_SUCCESS;

	memcpy(out + digest_size, constant,
	       BDB_CONSTANT_BLOCK_SIZE - digest_size);

	return BDB_SUCCESS;
}

/**
 * Derive secrets for SP-RO
 *
 * This function extends a BDS to derive secrets as done by SP-RO (a.k.a. mask
 * rom).
 *
 * @param ctx		VBoot context
 * @param type		Type of secret to derive
 * @param wsr		Work secret register
 * @param buf		Data from which a digest is computed
 * @param buf_size	Size of <buf>
 * @param extend	sha256 extension function to be used
 * @return		BDB_SUCCESS on success or BDB_ERROR_* otherwise
 */
static int derive_secret_ro(struct vba_context *ctx, enum bdb_secret_type type,
			    uint8_t *wsr, const uint8_t *buf, uint32_t buf_size,
			    f_extend extend)
{
	uint8_t c[BDB_CONSTANT_BLOCK_SIZE];
	uint8_t *from;
	const uint8_t *by = (const uint8_t *)c;
	uint8_t *to;

	switch (type) {
	case BDB_SECRET_TYPE_WSR:
		from = to = wsr;
		by = secret_constant_x;
		break;
	case BDB_SECRET_TYPE_BDB:
		from = wsr;
		to = ctx->secrets->bdb;
		if (get_constant_with_digest(buf, buf_size,
					     secret_constant_p, c))
			return BDB_ERROR_SECRET_BDB;
		break;
	case BDB_SECRET_TYPE_BOOT_PATH:
		from = wsr;
		to = ctx->secrets->boot_path;
		if (get_constant_with_digest(buf, buf_size,
					     secret_constant_k, c))
			return BDB_ERROR_SECRET_BOOT_PATH;
		break;
	case BDB_SECRET_TYPE_BOOT_VERIFIED:
		from = wsr;
		to = ctx->secrets->boot_verified;
		if (ctx->flags & VBA_CONTEXT_FLAG_BDB_KEY_EFUSED)
			by = secret_constant_fv0;
		else
			by = secret_constant_fv1;
		break;
	case BDB_SECRET_TYPE_NVM_WP:
		from = wsr;
		by = secret_constant_a;
		to = ctx->secrets->nvm_wp;
		break;
	case BDB_SECRET_TYPE_NVM_RW:
		from = ctx->secrets->nvm_wp;
		by = secret_constant_b;
		to = ctx->secrets->nvm_rw;
		break;
	default:
		return BDB_ERROR_SECRET_TYPE;
	}

	if (extend)
		extend(from, by, to);
	else
		vb2_sha256_extend(from, by, to);

	return BDB_SUCCESS;
}

int vba_derive_secret(struct vba_context *ctx, enum bdb_secret_type type,
		      uint8_t *wsr, const uint8_t *buf, uint32_t buf_size)
{
	uint8_t c[BDB_CONSTANT_BLOCK_SIZE];
	uint8_t *from;
	const uint8_t *by = (const uint8_t *)c;
	uint8_t *to;

	switch (type) {
	case BDB_SECRET_TYPE_WSR:
		from = to = wsr;
		by = secret_constant_y;
		break;
	case BDB_SECRET_TYPE_BDB:
		from = to = ctx->secrets->bdb;
		if (get_constant_with_digest(buf, buf_size,
					     secret_constant_q, c))
			return BDB_ERROR_SECRET_BDB;
		break;
	case BDB_SECRET_TYPE_BOOT_PATH:
		from = to = ctx->secrets->boot_path;
		if (get_constant_with_digest(buf, buf_size,
					     secret_constant_l, c))
			return BDB_ERROR_SECRET_BOOT_PATH;
		break;
	case BDB_SECRET_TYPE_BOOT_VERIFIED:
		from = to = ctx->secrets->boot_verified;
		if (ctx->flags & VBA_CONTEXT_FLAG_KERNEL_DATA_KEY_VERIFIED)
			by = secret_constant_kv1;
		else
			by = secret_constant_kv0;
		break;
	case BDB_SECRET_TYPE_BUC:
		from = ctx->secrets->boot_verified;
		by = secret_constant_c;
		to = ctx->secrets->buc;
		break;
	default:
		return BDB_ERROR_SECRET_TYPE;
	}

	vb2_sha256_extend(from, by, to);

	return BDB_SUCCESS;
}

int vba_clear_secret(struct vba_context *ctx, enum bdb_secret_type type)
{
	uint8_t *s;

	switch (type) {
	case BDB_SECRET_TYPE_NVM_RW:
		s = ctx->secrets->nvm_rw;
		break;
	case BDB_SECRET_TYPE_BDB:
		s = ctx->secrets->bdb;
		break;
	case BDB_SECRET_TYPE_BOOT_PATH:
		s = ctx->secrets->boot_path;
		break;
	case BDB_SECRET_TYPE_BOOT_VERIFIED:
		s = ctx->secrets->boot_verified;
		break;
	case BDB_SECRET_TYPE_BUC:
		s = ctx->secrets->buc;
		break;
	default:
		return BDB_ERROR_SECRET_TYPE;
	}

	memset(s, 0, BDB_SECRET_SIZE);
	return BDB_SUCCESS;
}

int vba_extend_secrets_ro(struct vba_context *ctx, const uint8_t *bdb,
			  uint8_t *wsr, f_extend extend)
{
	const struct bdb_key *bdbkey = bdb_get_bdbkey(bdb);
	const struct bdb_key *datakey = bdb_get_datakey(bdb);

	derive_secret_ro(ctx, BDB_SECRET_TYPE_BDB, wsr, (const uint8_t *)bdbkey,
			 bdbkey->struct_size, extend);
	derive_secret_ro(ctx, BDB_SECRET_TYPE_BOOT_PATH, wsr,
			 (const uint8_t *)datakey, datakey->struct_size,
			 extend);
	derive_secret_ro(ctx, BDB_SECRET_TYPE_BOOT_VERIFIED, wsr, NULL, 0,
			 extend);
	derive_secret_ro(ctx, BDB_SECRET_TYPE_NVM_WP, wsr, NULL, 0, extend);
	/* Deriving NVM-RW has to be done after NVM-WP */
	derive_secret_ro(ctx, BDB_SECRET_TYPE_NVM_RW, wsr, NULL, 0, extend);
	/* Extending WSR has to be done last. */
	derive_secret_ro(ctx, BDB_SECRET_TYPE_WSR, wsr, NULL, 0, extend);

	return BDB_SUCCESS;
}