blob: 2987e037d53cf20184255147f966cc4c49efe6f6 [file] [log] [blame]
Randall Spangler3333e572014-05-14 11:37:52 -07001/* 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 * Secure storage APIs
6 */
7
8#include "2sysincludes.h"
9#include "2common.h"
10#include "2crc8.h"
11#include "2misc.h"
12#include "2secdata.h"
13
14int vb2_secdata_check_crc(const struct vb2_context *ctx)
15{
16 const struct vb2_secdata *sec =
17 (const struct vb2_secdata *)ctx->secdata;
18
19 /* Verify CRC */
20 if (sec->crc8 != vb2_crc8(sec, offsetof(struct vb2_secdata, crc8)))
Randall Spanglerb9be5362014-06-05 13:32:11 -070021 return VB2_ERROR_SECDATA_CRC;
Randall Spangler3333e572014-05-14 11:37:52 -070022
23 return VB2_SUCCESS;
24}
25
26int vb2_secdata_create(struct vb2_context *ctx)
27{
28 struct vb2_secdata *sec = (struct vb2_secdata *)ctx->secdata;
29
30 /* Clear the entire struct */
31 memset(sec, 0, sizeof(*sec));
32
33 /* Set to current version */
34 sec->struct_version = VB2_SECDATA_VERSION;
35
36 /* Calculate initial CRC */
37 sec->crc8 = vb2_crc8(sec, offsetof(struct vb2_secdata, crc8));
38 ctx->flags |= VB2_CONTEXT_SECDATA_CHANGED;
39 return VB2_SUCCESS;
40}
41
42int vb2_secdata_init(struct vb2_context *ctx)
43{
44 struct vb2_shared_data *sd = vb2_get_sd(ctx);
45 struct vb2_secdata *sec = (struct vb2_secdata *)ctx->secdata;
46 int rv;
47
48 /* Data must be new enough to have a CRC */
49 if (sec->struct_version < 2)
Randall Spanglerb9be5362014-06-05 13:32:11 -070050 return VB2_ERROR_SECDATA_VERSION;
Randall Spangler3333e572014-05-14 11:37:52 -070051
52 rv = vb2_secdata_check_crc(ctx);
53 if (rv)
54 return rv;
55
56 /* Set status flag */
57 sd->status |= VB2_SD_STATUS_SECDATA_INIT;
58 // TODO: unit test for that
59
60 return VB2_SUCCESS;
61}
62
63int vb2_secdata_get(struct vb2_context *ctx,
64 enum vb2_secdata_param param,
65 uint32_t *dest)
66{
67 struct vb2_secdata *sec = (struct vb2_secdata *)ctx->secdata;
68
69 switch(param) {
70 case VB2_SECDATA_FLAGS:
71 *dest = sec->flags;
72 return VB2_SUCCESS;
73
74 case VB2_SECDATA_VERSIONS:
75 *dest = sec->fw_versions;
76 return VB2_SUCCESS;
77
78 default:
Randall Spanglerb9be5362014-06-05 13:32:11 -070079 return VB2_ERROR_SECDATA_GET_PARAM;
Randall Spangler3333e572014-05-14 11:37:52 -070080 }
81}
82
83int vb2_secdata_set(struct vb2_context *ctx,
84 enum vb2_secdata_param param,
85 uint32_t value)
86{
87 struct vb2_secdata *sec = (struct vb2_secdata *)ctx->secdata;
88 uint32_t now;
89
90 /* If not changing the value, don't regenerate the CRC. */
91 if (vb2_secdata_get(ctx, param, &now) == VB2_SUCCESS && now == value)
92 return VB2_SUCCESS;
93
94 switch(param) {
95 case VB2_SECDATA_FLAGS:
96 /* Make sure flags is in valid range */
97 if (value > 0xff)
Randall Spanglerb9be5362014-06-05 13:32:11 -070098 return VB2_ERROR_SECDATA_SET_FLAGS;
Randall Spangler3333e572014-05-14 11:37:52 -070099
100 sec->flags = value;
101 break;
102
103 case VB2_SECDATA_VERSIONS:
104 sec->fw_versions = value;
105 break;
106
107 default:
Randall Spanglerb9be5362014-06-05 13:32:11 -0700108 return VB2_ERROR_SECDATA_SET_PARAM;
Randall Spangler3333e572014-05-14 11:37:52 -0700109 }
110
111 /* Regenerate CRC */
112 sec->crc8 = vb2_crc8(sec, offsetof(struct vb2_secdata, crc8));
113 ctx->flags |= VB2_CONTEXT_SECDATA_CHANGED;
114 return VB2_SUCCESS;
115}