blob: 56593aaece73a0bf57817aa29e555d71a9d2c606 [file] [log] [blame]
Randall Spangler786acda2014-05-22 13:27:07 -07001/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Common functions between firmware and kernel verified boot.
6 * (Firmware portion)
7 */
8
9#include "2sysincludes.h"
10#include "2common.h"
Randall Spangler7141d732014-05-15 15:34:54 -070011#include "2rsa.h"
12#include "2sha.h"
Randall Spangler786acda2014-05-22 13:27:07 -070013
14int vb2_align(uint8_t **ptr, uint32_t *size, uint32_t align, uint32_t want_size)
15{
16 uintptr_t p = (uintptr_t)*ptr;
17 uintptr_t offs = p & (align - 1);
18
19 if (offs) {
20 offs = align - offs;
21
22 if (*size < offs)
Randall Spangler21457212014-06-06 09:30:14 -070023 return VB2_ERROR_ALIGN_BIGGER_THAN_SIZE;
Randall Spangler786acda2014-05-22 13:27:07 -070024
25 *ptr += offs;
26 *size -= offs;
27 }
28
29 if (*size < want_size)
Randall Spangler21457212014-06-06 09:30:14 -070030 return VB2_ERROR_ALIGN_SIZE;
Randall Spangler786acda2014-05-22 13:27:07 -070031
32 return VB2_SUCCESS;
33}
34
35void vb2_workbuf_init(struct vb2_workbuf *wb, uint8_t *buf, uint32_t size)
36{
37 wb->buf = buf;
38 wb->size = size;
39
40 /* Align the buffer so allocations will be aligned */
41 if (vb2_align(&wb->buf, &wb->size, VB2_WORKBUF_ALIGN, 0))
42 wb->size = 0;
43}
44
45/**
46 * Round up a number to a multiple of VB2_WORKBUF_ALIGN
47 *
48 * @param v Number to round up
49 * @return The number, rounded up.
50 */
51static __inline uint32_t wb_round_up(uint32_t v)
52{
53 return (v + VB2_WORKBUF_ALIGN - 1) & ~(VB2_WORKBUF_ALIGN - 1);
54}
55
56void *vb2_workbuf_alloc(struct vb2_workbuf *wb, uint32_t size)
57{
58 uint8_t *ptr = wb->buf;
59
60 /* Round up size to work buffer alignment */
61 size = wb_round_up(size);
62
63 if (size > wb->size)
64 return NULL;
65
66 wb->buf += size;
67 wb->size -= size;
68
69 return ptr;
70}
71
72void *vb2_workbuf_realloc(struct vb2_workbuf *wb,
73 uint32_t oldsize,
74 uint32_t newsize)
75{
76 /*
77 * Just free and allocate to update the size. No need to move/copy
78 * memory, since the new pointer is guaranteed to be the same as the
79 * old one. The new allocation can fail, if the new size is too big.
80 */
81 vb2_workbuf_free(wb, oldsize);
82 return vb2_workbuf_alloc(wb, newsize);
83}
84
85void vb2_workbuf_free(struct vb2_workbuf *wb, uint32_t size)
86{
87 /* Round up size to work buffer alignment */
88 size = wb_round_up(size);
89
90 wb->buf -= size;
91 wb->size += size;
92}
93
Randall Spangler7141d732014-05-15 15:34:54 -070094const uint8_t *vb2_packed_key_data(const struct vb2_packed_key *key)
95{
96 return (const uint8_t *)key + key->key_offset;
97}
98
99uint8_t *vb2_signature_data(struct vb2_signature *sig)
100{
101 return (uint8_t *)sig + sig->sig_offset;
102}
103
Randall Spangler786acda2014-05-22 13:27:07 -0700104ptrdiff_t vb2_offset_of(const void *base, const void *ptr)
105{
106 return (uintptr_t)ptr - (uintptr_t)base;
107}
Randall Spangler7141d732014-05-15 15:34:54 -0700108
Randall Spangler21457212014-06-06 09:30:14 -0700109int vb2_verify_member_inside(const void *parent, size_t parent_size,
110 const void *member, size_t member_size,
111 ptrdiff_t member_data_offset,
112 size_t member_data_size)
Randall Spangler7141d732014-05-15 15:34:54 -0700113{
114 const size_t psize = (size_t)parent_size;
115 const uintptr_t parent_end = (uintptr_t)parent + parent_size;
116 const ptrdiff_t member_offs = vb2_offset_of(parent, member);
117 const ptrdiff_t member_end_offs = member_offs + member_size;
118 const ptrdiff_t data_offs = member_offs + member_data_offset;
119 const ptrdiff_t data_end_offs = data_offs + member_data_size;
120
121 /* Make sure parent doesn't wrap */
122 if (parent_end < (uintptr_t)parent)
Randall Spangler21457212014-06-06 09:30:14 -0700123 return VB2_ERROR_INSIDE_PARENT_WRAPS;
Randall Spangler7141d732014-05-15 15:34:54 -0700124
125 /*
126 * Make sure the member is fully contained in the parent and doesn't
127 * wrap. Use >, not >=, since member_size = 0 is possible.
128 */
129 if (member_end_offs < member_offs)
Randall Spangler21457212014-06-06 09:30:14 -0700130 return VB2_ERROR_INSIDE_MEMBER_WRAPS;
131 if (member_offs < 0 || member_offs > psize || member_end_offs > psize)
132 return VB2_ERROR_INSIDE_MEMBER_OUTSIDE;
Randall Spangler7141d732014-05-15 15:34:54 -0700133
134 /* Make sure parent fully contains member data */
135 if (data_end_offs < data_offs)
Randall Spangler21457212014-06-06 09:30:14 -0700136 return VB2_ERROR_INSIDE_DATA_WRAPS;
137 if (data_offs < 0 || data_offs > psize || data_end_offs > psize)
138 return VB2_ERROR_INSIDE_DATA_OUTSIDE;
Randall Spangler7141d732014-05-15 15:34:54 -0700139
140 return VB2_SUCCESS;
141}
142
143int vb2_verify_signature_inside(const void *parent,
144 uint32_t parent_size,
145 const struct vb2_signature *sig)
146{
147 return vb2_verify_member_inside(parent, parent_size,
148 sig, sizeof(*sig),
149 sig->sig_offset, sig->sig_size);
150}
151
152int vb2_verify_packed_key_inside(const void *parent,
153 uint32_t parent_size,
154 const struct vb2_packed_key *key)
155{
156 return vb2_verify_member_inside(parent, parent_size,
157 key, sizeof(*key),
158 key->key_offset, key->key_size);
159}
160
161int vb2_unpack_key(struct vb2_public_key *key,
162 const uint8_t *buf,
163 uint32_t size)
164{
165 const struct vb2_packed_key *packed_key =
166 (const struct vb2_packed_key *)buf;
167 const uint32_t *buf32;
168 uint32_t expected_key_size;
169 int rv;
170
171 /* Make sure passed buffer is big enough for the packed key */
172 rv = vb2_verify_packed_key_inside(buf, size, packed_key);
173 if (rv)
174 return rv;
175
176 if (packed_key->algorithm >= VB2_ALG_COUNT) {
177 VB2_DEBUG("Invalid algorithm.\n");
178 return VB2_ERROR_BAD_ALGORITHM;
179 }
180
181 expected_key_size = vb2_packed_key_size(packed_key->algorithm);
182 if (!expected_key_size || expected_key_size != packed_key->key_size) {
183 VB2_DEBUG("Wrong key size for algorithm\n");
184 return VB2_ERROR_BAD_KEY;
185 }
186
187 /* Make sure source buffer is 32-bit aligned */
188 buf32 = (const uint32_t *)vb2_packed_key_data(packed_key);
189 if (!vb_aligned(buf32, sizeof(uint32_t)))
190 return VB2_ERROR_BUFFER_UNALIGNED;
191
192 /* Sanity check key array size */
193 key->arrsize = buf32[0];
194 if (key->arrsize * sizeof(uint32_t) !=
195 vb2_rsa_sig_size(packed_key->algorithm))
196 return VB2_ERROR_BAD_KEY;
197
198 key->n0inv = buf32[1];
199
200 /* Arrays point inside the key data */
201 key->n = buf32 + 2;
202 key->rr = buf32 + 2 + key->arrsize;
203
204 key->algorithm = packed_key->algorithm;
205
206 return VB2_SUCCESS;
207}
208
209int vb2_verify_data(const uint8_t *data,
210 uint32_t size,
211 struct vb2_signature *sig,
212 const struct vb2_public_key *key,
213 struct vb2_workbuf *wb)
214{
215 struct vb2_workbuf wblocal = *wb;
216 struct vb2_digest_context *dc;
217 uint8_t *digest;
218 uint32_t digest_size;
219 int rv;
220
221 if (key->algorithm >= VB2_ALG_COUNT)
222 return VB2_ERROR_BAD_ALGORITHM;
223
224 if (sig->sig_size != vb2_rsa_sig_size(key->algorithm)) {
225 VB2_DEBUG("Wrong data signature size for algorithm, "
226 "sig_size=%d, expected %d for algorithm %d.\n",
227 (int)sig->sig_size, vb2_rsa_sig_size(key->algorithm),
228 key->algorithm);
229 return VB2_ERROR_BAD_SIGNATURE;
230 }
231
232 if (sig->data_size > size) {
233 VB2_DEBUG("Data buffer smaller than length of signed data.\n");
234 return VB2_ERROR_UNKNOWN;
235 }
236
237 /* Digest goes at start of work buffer */
238 digest_size = vb2_digest_size(key->algorithm);
239 digest = vb2_workbuf_alloc(&wblocal, digest_size);
240 if (!digest)
241 return VB2_ERROR_WORKBUF_TOO_SMALL;
242
243 /* Hashing requires temp space for the context */
244 dc = vb2_workbuf_alloc(&wblocal, sizeof(*dc));
245 if (!dc)
246 return VB2_ERROR_WORKBUF_TOO_SMALL;
247
248 rv = vb2_digest_init(dc, key->algorithm);
249 if (rv)
250 return rv;
251
252 rv = vb2_digest_extend(dc, data, sig->data_size);
253 if (rv)
254 return rv;
255
256 rv = vb2_digest_finalize(dc, digest, digest_size);
257 if (rv)
258 return rv;
259
260 vb2_workbuf_free(&wblocal, sizeof(*dc));
261
262 return vb2_verify_digest(key, vb2_signature_data(sig), digest,
263 &wblocal);
264}
265
266int vb2_verify_keyblock(struct vb2_keyblock *block,
267 uint32_t size,
268 const struct vb2_public_key *key,
269 struct vb2_workbuf *wb)
270{
271 struct vb2_signature *sig;
272 int rv;
273
274 /* Sanity checks before attempting signature of data */
275 if(size < sizeof(*block)) {
276 VB2_DEBUG("Not enough space for key block header.\n");
277 return VB2_ERROR_BAD_KEYBLOCK;
278 }
279 if (memcmp(block->magic, KEY_BLOCK_MAGIC, KEY_BLOCK_MAGIC_SIZE)) {
280 VB2_DEBUG("Not a valid verified boot key block.\n");
281 return VB2_ERROR_BAD_KEYBLOCK;
282 }
283 if (block->header_version_major != KEY_BLOCK_HEADER_VERSION_MAJOR) {
284 VB2_DEBUG("Incompatible key block header version.\n");
285 return VB2_ERROR_BAD_KEYBLOCK;
286 }
287 if (size < block->keyblock_size) {
288 VB2_DEBUG("Not enough data for key block.\n");
289 return VB2_ERROR_BAD_KEYBLOCK;
290 }
291
292 /* Check signature */
293 sig = &block->keyblock_signature;
294
295 if (vb2_verify_signature_inside(block, block->keyblock_size, sig)) {
296 VB2_DEBUG("Key block signature off end of block\n");
297 return VB2_ERROR_BAD_KEYBLOCK;
298 }
299
300 /* Make sure advertised signature data sizes are sane. */
301 if (block->keyblock_size < sig->data_size) {
302 VB2_DEBUG("Signature calculated past end of block\n");
303 return VB2_ERROR_BAD_KEYBLOCK;
304 }
305
306 VB2_DEBUG("Checking key block signature...\n");
307 rv = vb2_verify_data((const uint8_t *)block, size, sig, key, wb);
308 if (rv) {
309 VB2_DEBUG("Invalid key block signature.\n");
310 return VB2_ERROR_BAD_SIGNATURE;
311 }
312
313 /* Verify we signed enough data */
314 if (sig->data_size < sizeof(struct vb2_keyblock)) {
315 VB2_DEBUG("Didn't sign enough data\n");
316 return VB2_ERROR_BAD_KEYBLOCK;
317 }
318
319 /* Verify data key is inside the block and inside signed data */
320 if (vb2_verify_packed_key_inside(block, block->keyblock_size,
321 &block->data_key)) {
322 VB2_DEBUG("Data key off end of key block\n");
323 return VB2_ERROR_BAD_KEYBLOCK;
324 }
325 if (vb2_verify_packed_key_inside(block, sig->data_size,
326 &block->data_key)) {
327 VB2_DEBUG("Data key off end of signed data\n");
328 return VB2_ERROR_BAD_KEYBLOCK;
329 }
330
331 /* Success */
332 return VB2_SUCCESS;
333}
334
335int vb2_verify_fw_preamble(struct vb2_fw_preamble *preamble,
336 uint32_t size,
337 const struct vb2_public_key *key,
338 struct vb2_workbuf *wb)
339{
340 struct vb2_signature *sig = &preamble->preamble_signature;
341
342 VB2_DEBUG("Verifying preamble.\n");
343
344 /* Sanity checks before attempting signature of data */
345 if(size < EXPECTED_VB2FIRMWAREPREAMBLEHEADER2_1_SIZE) {
346 VB2_DEBUG("Not enough data for preamble header 2.1.\n");
347 return VB2_ERROR_BAD_PREAMBLE;
348 }
349 if (preamble->header_version_major !=
350 FIRMWARE_PREAMBLE_HEADER_VERSION_MAJOR) {
351 VB2_DEBUG("Incompatible firmware preamble header version.\n");
352 return VB2_ERROR_BAD_PREAMBLE;
353 }
354
355 if (preamble->header_version_minor < 1) {
356 VB2_DEBUG("Only preamble header 2.1+ supported\n");
357 return VB2_ERROR_BAD_PREAMBLE;
358 }
359
360 if (size < preamble->preamble_size) {
361 VB2_DEBUG("Not enough data for preamble.\n");
362 return VB2_ERROR_BAD_PREAMBLE;
363 }
364
365 /* Check signature */
366 if (vb2_verify_signature_inside(preamble, preamble->preamble_size,
367 sig)) {
368 VB2_DEBUG("Preamble signature off end of preamble\n");
369 return VB2_ERROR_BAD_PREAMBLE;
370 }
371
372 /* Make sure advertised signature data sizes are sane. */
373 if (preamble->preamble_size < sig->data_size) {
374 VB2_DEBUG("Signature calculated past end of the block\n");
375 return VB2_ERROR_BAD_PREAMBLE;
376 }
377
378 if (vb2_verify_data((const uint8_t *)preamble, size, sig, key, wb)) {
379 VB2_DEBUG("Preamble signature validation failed\n");
380 return VB2_ERROR_BAD_SIGNATURE;
381 }
382
383 /* Verify we signed enough data */
384 if (sig->data_size < sizeof(struct vb2_fw_preamble)) {
385 VB2_DEBUG("Didn't sign enough data\n");
386 return VB2_ERROR_BAD_PREAMBLE;
387 }
388
389 /* Verify body signature is inside the signed data */
390 if (vb2_verify_signature_inside(preamble, sig->data_size,
391 &preamble->body_signature)) {
392 VB2_DEBUG("Firmware body signature off end of preamble\n");
393 return VB2_ERROR_BAD_PREAMBLE;
394 }
395
396 /* Verify kernel subkey is inside the signed data */
397 if (vb2_verify_packed_key_inside(preamble, sig->data_size,
398 &preamble->kernel_subkey)) {
399 VB2_DEBUG("Kernel subkey off end of preamble\n");
400 return VB2_ERROR_BAD_PREAMBLE;
401 }
402
403 /* Success */
404 return VB2_SUCCESS;
405}