Home | History | Annotate | Download | only in lib21
      1 /* 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  * Host functions for keyblocks
      6  */
      7 
      8 #include "2sysincludes.h"
      9 #include "2common.h"
     10 #include "2rsa.h"
     11 #include "host_common.h"
     12 #include "host_fw_preamble2.h"
     13 #include "host_key2.h"
     14 #include "host_keyblock2.h"
     15 #include "host_misc.h"
     16 #include "host_signature2.h"
     17 #include "vb2_common.h"
     18 
     19 int vb2_fw_preamble_create(struct vb2_fw_preamble **fp_ptr,
     20 			   const struct vb2_private_key *signing_key,
     21 			   const struct vb2_signature **hash_list,
     22 			   uint32_t hash_count,
     23 			   uint32_t fw_version,
     24 			   uint32_t flags,
     25 			   const char *desc)
     26 {
     27 	struct vb2_fw_preamble fp = {
     28 		.c.magic = VB2_MAGIC_FW_PREAMBLE,
     29 		.c.struct_version_major = VB2_FW_PREAMBLE_VERSION_MAJOR,
     30 		.c.struct_version_minor = VB2_FW_PREAMBLE_VERSION_MAJOR,
     31 		.c.fixed_size = sizeof(fp),
     32 		.c.desc_size = vb2_desc_size(desc),
     33 		.flags = flags,
     34 		.fw_version = fw_version,
     35 		.hash_count = hash_count,
     36 	};
     37 
     38 	uint32_t hash_next;
     39 	uint32_t sig_size;
     40 	uint8_t *buf;
     41 	int i;
     42 
     43 	*fp_ptr = NULL;
     44 
     45 	/* Determine component sizes */
     46 	hash_next = fp.hash_offset = fp.c.fixed_size + fp.c.desc_size;
     47 
     48 	for (i = 0; i < hash_count; i++)
     49 		hash_next += hash_list[i]->c.total_size;
     50 
     51 	fp.sig_offset = hash_next;
     52 
     53 	if (vb2_sig_size_for_key(&sig_size, signing_key, NULL))
     54 		return VB2_FW_PREAMBLE_CREATE_SIG_SIZE;
     55 
     56 	fp.c.total_size = fp.sig_offset + sig_size;
     57 
     58 	/* Allocate buffer and copy components */
     59 	buf = calloc(fp.c.total_size, 1);
     60 	if (!buf)
     61 		return VB2_FW_PREAMBLE_CREATE_ALLOC;
     62 
     63 	memcpy(buf, &fp, sizeof(fp));
     64 	if (fp.c.desc_size)
     65 		strcpy((char *)buf + fp.c.fixed_size, desc);
     66 
     67 	hash_next = fp.hash_offset;
     68 	for (i = 0; i < hash_count; i++) {
     69 		memcpy(buf + hash_next, hash_list[i],
     70 		       hash_list[i]->c.total_size);
     71 		hash_next += hash_list[i]->c.total_size;
     72 	}
     73 
     74 	/* Sign the preamble */
     75 	if (vb2_sign_object(buf, fp.sig_offset, signing_key, NULL)) {
     76 		free(buf);
     77 		return VB2_FW_PREAMBLE_CREATE_SIGN;
     78 	}
     79 
     80 	*fp_ptr = (struct vb2_fw_preamble *)buf;
     81 	return VB2_SUCCESS;
     82 }
     83