api.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. * Externally-callable APIs
  6. * (Firmware portion)
  7. */
  8. #include "2sysincludes.h"
  9. #include "2api.h"
  10. #include "2common.h"
  11. #include "2misc.h"
  12. #include "2nvstorage.h"
  13. #include "2secdata.h"
  14. #include "2sha.h"
  15. #include "2rsa.h"
  16. #include "vb21_common.h"
  17. int vb21api_fw_phase3(struct vb2_context *ctx)
  18. {
  19. int rv;
  20. /* Verify firmware keyblock */
  21. rv = vb21_load_fw_keyblock(ctx);
  22. if (rv) {
  23. vb2_fail(ctx, VB2_RECOVERY_RO_INVALID_RW, rv);
  24. return rv;
  25. }
  26. /* Verify firmware preamble */
  27. rv = vb21_load_fw_preamble(ctx);
  28. if (rv) {
  29. vb2_fail(ctx, VB2_RECOVERY_RO_INVALID_RW, rv);
  30. return rv;
  31. }
  32. return VB2_SUCCESS;
  33. }
  34. int vb21api_init_hash(struct vb2_context *ctx,
  35. const struct vb2_id *id,
  36. uint32_t *size)
  37. {
  38. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  39. const struct vb21_fw_preamble *pre;
  40. const struct vb21_signature *sig = NULL;
  41. struct vb2_digest_context *dc;
  42. struct vb2_workbuf wb;
  43. uint32_t hash_offset;
  44. int i, rv;
  45. vb2_workbuf_from_ctx(ctx, &wb);
  46. /* Get preamble pointer */
  47. if (!sd->workbuf_preamble_size)
  48. return VB2_ERROR_API_INIT_HASH_PREAMBLE;
  49. pre = (const struct vb21_fw_preamble *)
  50. (ctx->workbuf + sd->workbuf_preamble_offset);
  51. /* Find the matching signature */
  52. hash_offset = pre->hash_offset;
  53. for (i = 0; i < pre->hash_count; i++) {
  54. sig = (const struct vb21_signature *)
  55. ((uint8_t *)pre + hash_offset);
  56. if (!memcmp(id, &sig->id, sizeof(*id)))
  57. break;
  58. hash_offset += sig->c.total_size;
  59. }
  60. if (i >= pre->hash_count)
  61. return VB2_ERROR_API_INIT_HASH_ID; /* No match */
  62. /* Allocate workbuf space for the hash */
  63. if (sd->workbuf_hash_size) {
  64. dc = (struct vb2_digest_context *)
  65. (ctx->workbuf + sd->workbuf_hash_offset);
  66. } else {
  67. uint32_t dig_size = sizeof(*dc);
  68. dc = vb2_workbuf_alloc(&wb, dig_size);
  69. if (!dc)
  70. return VB2_ERROR_API_INIT_HASH_WORKBUF;
  71. sd->workbuf_hash_offset = vb2_offset_of(ctx->workbuf, dc);
  72. sd->workbuf_hash_size = dig_size;
  73. ctx->workbuf_used = sd->workbuf_hash_offset + dig_size;
  74. }
  75. sd->hash_tag = vb2_offset_of(ctx->workbuf, sig);
  76. sd->hash_remaining_size = sig->data_size;
  77. if (size)
  78. *size = sig->data_size;
  79. if (!(pre->flags & VB21_FIRMWARE_PREAMBLE_DISALLOW_HWCRYPTO)) {
  80. rv = vb2ex_hwcrypto_digest_init(sig->hash_alg, sig->data_size);
  81. if (!rv) {
  82. VB2_DEBUG("Using HW crypto engine for hash_alg %d\n",
  83. sig->hash_alg);
  84. dc->hash_alg = sig->hash_alg;
  85. dc->using_hwcrypto = 1;
  86. return VB2_SUCCESS;
  87. }
  88. if (rv != VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED)
  89. return rv;
  90. VB2_DEBUG("HW crypto for hash_alg %d not supported, using SW\n",
  91. sig->hash_alg);
  92. } else {
  93. VB2_DEBUG("HW crypto forbidden by preamble, using SW\n");
  94. }
  95. return vb2_digest_init(dc, sig->hash_alg);
  96. }
  97. int vb21api_check_hash(struct vb2_context *ctx)
  98. {
  99. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  100. struct vb2_digest_context *dc = (struct vb2_digest_context *)
  101. (ctx->workbuf + sd->workbuf_hash_offset);
  102. struct vb2_workbuf wb;
  103. uint8_t *digest;
  104. uint32_t digest_size = vb2_digest_size(dc->hash_alg);
  105. const struct vb21_signature *sig;
  106. int rv;
  107. vb2_workbuf_from_ctx(ctx, &wb);
  108. /* Get signature pointer */
  109. if (!sd->hash_tag)
  110. return VB2_ERROR_API_CHECK_HASH_TAG;
  111. sig = (const struct vb21_signature *)(ctx->workbuf + sd->hash_tag);
  112. /* Must have initialized hash digest work area */
  113. if (!sd->workbuf_hash_size)
  114. return VB2_ERROR_API_CHECK_HASH_WORKBUF;
  115. /* Should have hashed the right amount of data */
  116. if (sd->hash_remaining_size)
  117. return VB2_ERROR_API_CHECK_HASH_SIZE;
  118. /* Allocate the digest */
  119. digest = vb2_workbuf_alloc(&wb, digest_size);
  120. if (!digest)
  121. return VB2_ERROR_API_CHECK_HASH_WORKBUF_DIGEST;
  122. /* Finalize the digest */
  123. if (dc->using_hwcrypto)
  124. rv = vb2ex_hwcrypto_digest_finalize(digest, digest_size);
  125. else
  126. rv = vb2_digest_finalize(dc, digest, digest_size);
  127. if (rv)
  128. return rv;
  129. /* Compare with the signature */
  130. if (vb2_safe_memcmp(digest, (const uint8_t *)sig + sig->sig_offset,
  131. digest_size))
  132. return VB2_ERROR_API_CHECK_HASH_SIG;
  133. /* TODO: the old check-hash function called vb2_fail() on any mismatch.
  134. * I don't think it should do that; the caller should. */
  135. return VB2_SUCCESS;
  136. }