vboot_common.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Copyright (c) 2013 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. */
  7. #ifndef VBOOT_REFERENCE_VBOOT_COMMON_H_
  8. #define VBOOT_REFERENCE_VBOOT_COMMON_H_
  9. #include "vboot_struct.h"
  10. #ifndef ARRAY_SIZE
  11. #define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
  12. #endif
  13. /* Test an important condition at compile time, not run time */
  14. #ifndef BUILD_ASSERT
  15. #define _BA1_(cond, line) \
  16. extern int __build_assertion_ ## line[1 - 2*!(cond)] \
  17. __attribute__ ((unused))
  18. #define _BA0_(c, x) _BA1_(c, x)
  19. #define BUILD_ASSERT(cond) _BA0_(cond, __LINE__)
  20. #endif
  21. /* Error Codes for all common functions. */
  22. enum {
  23. VBOOT_SUCCESS = 0,
  24. /* Key block internal structure is invalid, or not a key block */
  25. VBOOT_KEY_BLOCK_INVALID,
  26. /* Key block signature check failed */
  27. VBOOT_KEY_BLOCK_SIGNATURE,
  28. /* Key block hash check failed */
  29. VBOOT_KEY_BLOCK_HASH,
  30. /* Invalid public key passed to a signature verficiation function. */
  31. VBOOT_PUBLIC_KEY_INVALID,
  32. /* Preamble internal structure is invalid */
  33. VBOOT_PREAMBLE_INVALID,
  34. /* Preamble signature check failed */
  35. VBOOT_PREAMBLE_SIGNATURE,
  36. /* Shared data is invalid. */
  37. VBOOT_SHARED_DATA_INVALID,
  38. /* Kernel Preamble does not contain flags */
  39. VBOOT_KERNEL_PREAMBLE_NO_FLAGS,
  40. VBOOT_ERROR_MAX,
  41. };
  42. extern const char *kVbootErrors[VBOOT_ERROR_MAX];
  43. /**
  44. * Return offset of ptr from base.
  45. */
  46. uint64_t OffsetOf(const void *base, const void *ptr);
  47. /*
  48. * Helper functions to get data pointed to by a public key or signature.
  49. */
  50. uint8_t *GetPublicKeyData(VbPublicKey *key);
  51. const uint8_t *GetPublicKeyDataC(const VbPublicKey *key);
  52. uint8_t *GetSignatureData(VbSignature *sig);
  53. const uint8_t *GetSignatureDataC(const VbSignature *sig);
  54. /*
  55. * Helper functions to verify the data pointed to by a subfield is inside the
  56. * parent data. Returns 0 if inside, 1 if error.
  57. */
  58. int VerifyMemberInside(const void *parent, uint64_t parent_size,
  59. const void *member, uint64_t member_size,
  60. uint64_t member_data_offset,
  61. uint64_t member_data_size);
  62. int VerifyPublicKeyInside(const void *parent, uint64_t parent_size,
  63. const VbPublicKey *key);
  64. int VerifySignatureInside(const void *parent, uint64_t parent_size,
  65. const VbSignature *sig);
  66. /**
  67. * Initialize a public key to refer to [key_data].
  68. */
  69. void PublicKeyInit(VbPublicKey *key, uint8_t *key_data, uint64_t key_size);
  70. /**
  71. * Copy a public key from [src] to [dest].
  72. *
  73. * Returns 0 if success, non-zero if error.
  74. */
  75. int PublicKeyCopy(VbPublicKey *dest, const VbPublicKey *src);
  76. /**
  77. * Retrieve the 16-bit vmlinuz header address and size from the kernel preamble
  78. * if there is one. These are only available in Kernel Preamble Header version
  79. * >= 2.1. If given a header 2.0 or lower, will set address and size to 0 (this
  80. * is not considered an error).
  81. *
  82. * Returns VBOOT_SUCCESS if successful.
  83. */
  84. int VbGetKernelVmlinuzHeader(const VbKernelPreambleHeader *preamble,
  85. uint64_t *vmlinuz_header_address,
  86. uint64_t *vmlinuz_header_size);
  87. /**
  88. * Checks if the kernel preamble has flags field. This is available only if the
  89. * Kernel Preamble Header version >=2.2. If give a header of 2.1 or lower, it
  90. * will return VBOOT_KERNEL_PREAMBLE_NO_FLAGS.
  91. *
  92. * Returns VBOOT_SUCCESS if version is >=2.2.
  93. */
  94. int VbKernelHasFlags(const VbKernelPreambleHeader *preamble);
  95. /**
  96. * Verify that the Vmlinuz Header is contained inside of the kernel blob.
  97. *
  98. * Returns VBOOT_SUCCESS or VBOOT_PREAMBLE_INVALID on error
  99. */
  100. int VerifyVmlinuzInsideKBlob(uint64_t kblob, uint64_t kblob_size,
  101. uint64_t header, uint64_t header_size);
  102. /**
  103. * Initialize a verified boot shared data structure.
  104. *
  105. * Returns 0 if success, non-zero if error.
  106. */
  107. int VbSharedDataInit(VbSharedDataHeader *header, uint64_t size);
  108. /**
  109. * Reserve [size] bytes of the shared data area. Returns the offset of the
  110. * reserved data from the start of the shared data buffer, or 0 if error.
  111. */
  112. uint64_t VbSharedDataReserve(VbSharedDataHeader *header, uint64_t size);
  113. /**
  114. * Copy the kernel subkey into the shared data.
  115. *
  116. * Returns 0 if success, non-zero if error.
  117. */
  118. int VbSharedDataSetKernelKey(VbSharedDataHeader *header,
  119. const VbPublicKey *src);
  120. #endif /* VBOOT_REFERENCE_VBOOT_COMMON_H_ */