host_fw_preamble.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #include "2sysincludes.h"
  8. #include "2common.h"
  9. #include "2rsa.h"
  10. #include "host_common.h"
  11. #include "host_fw_preamble2.h"
  12. #include "host_key2.h"
  13. #include "host_keyblock2.h"
  14. #include "host_misc.h"
  15. #include "host_signature2.h"
  16. #include "vb21_common.h"
  17. int vb21_fw_preamble_create(struct vb21_fw_preamble **fp_ptr,
  18. const struct vb2_private_key *signing_key,
  19. const struct vb21_signature **hash_list,
  20. uint32_t hash_count,
  21. uint32_t fw_version,
  22. uint32_t flags,
  23. const char *desc)
  24. {
  25. struct vb21_fw_preamble fp = {
  26. .c.magic = VB21_MAGIC_FW_PREAMBLE,
  27. .c.struct_version_major = VB21_FW_PREAMBLE_VERSION_MAJOR,
  28. .c.struct_version_minor = VB21_FW_PREAMBLE_VERSION_MAJOR,
  29. .c.fixed_size = sizeof(fp),
  30. .c.desc_size = vb2_desc_size(desc),
  31. .flags = flags,
  32. .fw_version = fw_version,
  33. .hash_count = hash_count,
  34. };
  35. uint32_t hash_next;
  36. uint32_t sig_size;
  37. uint8_t *buf;
  38. int i;
  39. *fp_ptr = NULL;
  40. /* Determine component sizes */
  41. hash_next = fp.hash_offset = fp.c.fixed_size + fp.c.desc_size;
  42. for (i = 0; i < hash_count; i++)
  43. hash_next += hash_list[i]->c.total_size;
  44. fp.sig_offset = hash_next;
  45. if (vb21_sig_size_for_key(&sig_size, signing_key, NULL))
  46. return VB2_FW_PREAMBLE_CREATE_SIG_SIZE;
  47. fp.c.total_size = fp.sig_offset + sig_size;
  48. /* Allocate buffer and copy components */
  49. buf = calloc(fp.c.total_size, 1);
  50. if (!buf)
  51. return VB2_FW_PREAMBLE_CREATE_ALLOC;
  52. memcpy(buf, &fp, sizeof(fp));
  53. if (fp.c.desc_size)
  54. strcpy((char *)buf + fp.c.fixed_size, desc);
  55. hash_next = fp.hash_offset;
  56. for (i = 0; i < hash_count; i++) {
  57. memcpy(buf + hash_next, hash_list[i],
  58. hash_list[i]->c.total_size);
  59. hash_next += hash_list[i]->c.total_size;
  60. }
  61. /* Sign the preamble */
  62. if (vb21_sign_object(buf, fp.sig_offset, signing_key, NULL)) {
  63. free(buf);
  64. return VB2_FW_PREAMBLE_CREATE_SIGN;
  65. }
  66. *fp_ptr = (struct vb21_fw_preamble *)buf;
  67. return VB2_SUCCESS;
  68. }