blob: 0df2cf698146772060ee182a82064aae72b1dfed [file] [log] [blame]
Randall Spanglerefa37b82014-11-12 16:20:50 -08001/* 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
9#include "2sysincludes.h"
10#include "2api.h"
11#include "2common.h"
12#include "2misc.h"
13#include "2nvstorage.h"
14#include "2secdata.h"
15#include "2sha.h"
16#include "2rsa.h"
17
18int vb2api_fw_phase3_2(struct vb2_context *ctx)
19{
20 int rv;
21
22 /* Verify firmware keyblock */
23 rv = vb2_load_fw_keyblock2(ctx);
24 if (rv) {
25 vb2_fail(ctx, VB2_RECOVERY_RO_INVALID_RW, rv);
26 return rv;
27 }
28
29 /* Verify firmware preamble */
30 rv = vb2_load_fw_preamble2(ctx);
31 if (rv) {
32 vb2_fail(ctx, VB2_RECOVERY_RO_INVALID_RW, rv);
33 return rv;
34 }
35
36 return VB2_SUCCESS;
37}
38
39int vb2api_init_hash2(struct vb2_context *ctx,
40 const struct vb2_guid *guid,
41 uint32_t *size)
42{
43 struct vb2_shared_data *sd = vb2_get_sd(ctx);
44 const struct vb2_fw_preamble2 *pre;
45 const struct vb2_signature2 *sig = NULL;
46 struct vb2_digest_context *dc;
47 struct vb2_workbuf wb;
48 uint32_t hash_offset;
49 int i;
50
51 vb2_workbuf_from_ctx(ctx, &wb);
52
53 /* Get preamble pointer */
54 if (!sd->workbuf_preamble_size)
55 return VB2_ERROR_API_INIT_HASH_PREAMBLE;
56 pre = (const struct vb2_fw_preamble2 *)
57 (ctx->workbuf + sd->workbuf_preamble_offset);
58
59 /* Find the matching signature */
60 hash_offset = pre->hash_offset;
61 for (i = 0; i < pre->hash_count; i++) {
62 sig = (const struct vb2_signature2 *)
63 ((uint8_t *)pre + hash_offset);
64
65 if (!memcmp(guid, &sig->guid, sizeof(*guid)))
66 break;
67
68 hash_offset += sig->c.total_size;
69 }
70 if (i >= pre->hash_count)
71 return VB2_ERROR_API_INIT_HASH_GUID; /* No match */
72
73 /* Allocate workbuf space for the hash */
74 if (sd->workbuf_hash_size) {
75 dc = (struct vb2_digest_context *)
76 (ctx->workbuf + sd->workbuf_hash_offset);
77 } else {
78 uint32_t dig_size = sizeof(*dc);
79
80 dc = vb2_workbuf_alloc(&wb, dig_size);
81 if (!dc)
82 return VB2_ERROR_API_INIT_HASH_WORKBUF;
83
84 sd->workbuf_hash_offset = vb2_offset_of(ctx->workbuf, dc);
85 sd->workbuf_hash_size = dig_size;
86 ctx->workbuf_used = sd->workbuf_hash_offset + dig_size;
87 }
88
89 sd->hash_tag = vb2_offset_of(ctx->workbuf, sig);
90 sd->hash_remaining_size = sig->data_size;
91
92 if (size)
93 *size = sig->data_size;
94
95 return vb2_digest_init(dc, sig->hash_alg);
96}
97
98int vb2api_check_hash2(struct vb2_context *ctx)
99{
100 struct vb2_shared_data *sd = vb2_get_sd(ctx);
101 struct vb2_digest_context *dc = (struct vb2_digest_context *)
102 (ctx->workbuf + sd->workbuf_hash_offset);
103 struct vb2_workbuf wb;
104
105 uint8_t *digest;
106 uint32_t digest_size = vb2_digest_size(dc->hash_alg);
107
108 const struct vb2_signature2 *sig;
109
110 int rv;
111
112 vb2_workbuf_from_ctx(ctx, &wb);
113
114 /* Get signature pointer */
115 if (!sd->hash_tag)
116 return VB2_ERROR_API_CHECK_HASH_TAG;
117 sig = (const struct vb2_signature2 *)(ctx->workbuf + sd->hash_tag);
118
119 /* Must have initialized hash digest work area */
120 if (!sd->workbuf_hash_size)
121 return VB2_ERROR_API_CHECK_HASH_WORKBUF;
122
123 /* Should have hashed the right amount of data */
124 if (sd->hash_remaining_size)
125 return VB2_ERROR_API_CHECK_HASH_SIZE;
126
127 /* Allocate the digest */
128 digest = vb2_workbuf_alloc(&wb, digest_size);
129 if (!digest)
130 return VB2_ERROR_API_CHECK_HASH_WORKBUF_DIGEST;
131
132 /* Finalize the digest */
133 rv = vb2_digest_finalize(dc, digest, digest_size);
134 if (rv)
135 return rv;
136
137 /* Compare with the signature */
138 if (vb2_safe_memcmp(digest, (const uint8_t *)sig + sig->sig_offset,
139 digest_size))
140 return VB2_ERROR_API_CHECK_HASH_SIG;
141
142 // TODO: the old check-hash function called vb2_fail() on any mismatch.
143 // I don't think it should do that; the caller should.
144
145 return VB2_SUCCESS;
146}