diff options
author | Randall Spangler <rspangler@chromium.org> | 2014-11-25 10:52:59 -0800 |
---|---|---|
committer | chrome-internal-fetch <chrome-internal-fetch@google.com> | 2014-12-01 20:30:50 +0000 |
commit | 42a850059ca07f5e14fdf31990a35ad63e3f1bd8 (patch) | |
tree | ab65411dc63c7d86bf6ada386020873ea92bb80b /host | |
parent | 9328bbff521625e788396ef9c5b26b79e6d1a7cb (diff) | |
download | vboot_reference-42a850059ca07f5e14fdf31990a35ad63e3f1bd8.tar.gz |
vboot2: Add host lib function to create a vb2-style firmware preamble
And associated unit tests
BUG=chromium:423882
BRANCH=none
TEST=VBOOT2=1 make runtests
Change-Id: I3bf6ff6c6e32dfd0dd737f9b04ff0546e9e0a463
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/231728
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
Diffstat (limited to 'host')
-rw-r--r-- | host/lib/host_fw_preamble2.c | 81 | ||||
-rw-r--r-- | host/lib/include/host_fw_preamble2.h | 36 |
2 files changed, 117 insertions, 0 deletions
diff --git a/host/lib/host_fw_preamble2.c b/host/lib/host_fw_preamble2.c new file mode 100644 index 00000000..759af1b4 --- /dev/null +++ b/host/lib/host_fw_preamble2.c @@ -0,0 +1,81 @@ +/* Copyright (c) 2014 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. + * + * Host functions for keyblocks + */ + +#include "2sysincludes.h" +#include "2common.h" +#include "2rsa.h" +#include "host_common.h" +#include "host_fw_preamble2.h" +#include "host_key2.h" +#include "host_keyblock2.h" +#include "host_misc.h" +#include "host_signature2.h" + +int vb2_fw_preamble_create(struct vb2_fw_preamble2 **fp_ptr, + const struct vb2_private_key *signing_key, + const struct vb2_signature2 **hash_list, + uint32_t hash_count, + uint32_t fw_version, + uint32_t flags, + const char *desc) +{ + struct vb2_fw_preamble2 fp = { + .c.magic = VB2_MAGIC_FW_PREAMBLE2, + .c.struct_version_major = VB2_FW_PREAMBLE2_VERSION_MAJOR, + .c.struct_version_minor = VB2_FW_PREAMBLE2_VERSION_MAJOR, + .c.fixed_size = sizeof(fp), + .c.desc_size = vb2_desc_size(desc), + .flags = flags, + .firmware_version = fw_version, + .hash_count = hash_count, + }; + + uint32_t hash_next; + uint32_t sig_size; + uint8_t *buf; + int i; + + *fp_ptr = NULL; + + /* Determine component sizes */ + hash_next = fp.hash_offset = fp.c.fixed_size + fp.c.desc_size; + + for (i = 0; i < hash_count; i++) + hash_next += hash_list[i]->c.total_size; + + fp.sig_offset = hash_next; + + if (vb2_sig_size_for_key(&sig_size, signing_key, NULL)) + return VB2_FW_PREAMBLE_CREATE_SIG_SIZE; + + fp.c.total_size = fp.sig_offset + sig_size; + + /* Allocate buffer and copy components */ + buf = malloc(fp.c.total_size); + if (!buf) + return VB2_FW_PREAMBLE_CREATE_ALLOC; + + memcpy(buf, &fp, sizeof(fp)); + if (fp.c.desc_size) + strcpy((char *)buf + fp.c.fixed_size, desc); + + hash_next = fp.hash_offset; + for (i = 0; i < hash_count; i++) { + memcpy(buf + hash_next, hash_list[i], + hash_list[i]->c.total_size); + hash_next += hash_list[i]->c.total_size; + } + + /* Sign the preamble */ + if (vb2_sign_object(buf, fp.sig_offset, signing_key, NULL)) { + free(buf); + return VB2_FW_PREAMBLE_CREATE_SIGN; + } + + *fp_ptr = (struct vb2_fw_preamble2 *)buf; + return VB2_SUCCESS; +} diff --git a/host/lib/include/host_fw_preamble2.h b/host/lib/include/host_fw_preamble2.h new file mode 100644 index 00000000..d8c53838 --- /dev/null +++ b/host/lib/include/host_fw_preamble2.h @@ -0,0 +1,36 @@ +/* Copyright (c) 2014 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. + * + * Host-side functions for firmware preamble + */ + +#ifndef VBOOT_REFERENCE_HOST_FW_PREAMBLE2_H_ +#define VBOOT_REFERENCE_HOST_FW_PREAMBLE2_H_ + +#include "2struct.h" + +struct vb2_private_key; + +/** + * Create and sign a firmware preamble. + * + * @param fp_ptr On success, points to a newly allocated preamble buffer. + * Caller is responsible for calling free() on this. + * @param signing_key Key to sign the preamble with + * @param hash_list Component hashes to include in the keyblock + * @param hash_count Number of component hashes + * @param fw_version Firmware version + * @param flags Flags for preamble + * @param desc Description for preamble, or NULL if none + * @return VB2_SUCCESS, or non-zero error code if failure. + */ +int vb2_fw_preamble_create(struct vb2_fw_preamble2 **fp_ptr, + const struct vb2_private_key *signing_key, + const struct vb2_signature2 **hash_list, + uint32_t hash_count, + uint32_t fw_version, + uint32_t flags, + const char *desc); + +#endif /* VBOOT_REFERENCE_HOST_FW_PREAMBLE2_H_ */ |