blob: af11aef41c3814fad2b61864fbfa4789a64f2bab [file] [log] [blame]
Randall Spanglerbf9c2762015-05-12 13:44:30 -07001/* Copyright 2015 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 - kernel version space
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_secdatak_check_crc(const struct vb2_context *ctx)
15{
16 const struct vb2_secdatak *sec =
17 (const struct vb2_secdatak *)ctx->secdatak;
18
19 /* Verify CRC */
20 if (sec->crc8 != vb2_crc8(sec, offsetof(struct vb2_secdatak, crc8)))
21 return VB2_ERROR_SECDATAK_CRC;
22
23 return VB2_SUCCESS;
24}
25
26int vb2_secdatak_create(struct vb2_context *ctx)
27{
28 struct vb2_secdatak *sec = (struct vb2_secdatak *)ctx->secdatak;
29
30 /* Clear the entire struct */
31 memset(sec, 0, sizeof(*sec));
32
33 /* Set to current version */
34 sec->struct_version = VB2_SECDATAK_VERSION;
35
36 /* Set UID */
37 sec->uid = VB2_SECDATAK_UID;
38
39 /* Calculate initial CRC */
40 sec->crc8 = vb2_crc8(sec, offsetof(struct vb2_secdatak, crc8));
41 ctx->flags |= VB2_CONTEXT_SECDATAK_CHANGED;
42 return VB2_SUCCESS;
43}
44
45int vb2_secdatak_init(struct vb2_context *ctx)
46{
47 struct vb2_secdatak *sec = (struct vb2_secdatak *)ctx->secdatak;
48 struct vb2_shared_data *sd = vb2_get_sd(ctx);
49 int rv;
50
51 rv = vb2_secdatak_check_crc(ctx);
52 if (rv)
53 return rv;
54
55 /* Make sure the UID is correct */
56 if (sec->uid != VB2_SECDATAK_UID)
57 return VB2_ERROR_SECDATAK_UID;
58
59 /* Set status flag */
60 sd->status |= VB2_SD_STATUS_SECDATAK_INIT;
61 /* TODO: unit test for that */
62
63 return VB2_SUCCESS;
64}
65
66int vb2_secdatak_get(struct vb2_context *ctx,
67 enum vb2_secdatak_param param,
68 uint32_t *dest)
69{
70 struct vb2_secdatak *sec = (struct vb2_secdatak *)ctx->secdatak;
71
72 if (!(vb2_get_sd(ctx)->status & VB2_SD_STATUS_SECDATAK_INIT))
73 return VB2_ERROR_SECDATAK_GET_UNINITIALIZED;
74
75 switch(param) {
76 case VB2_SECDATAK_VERSIONS:
77 *dest = sec->kernel_versions;
78 return VB2_SUCCESS;
79
80 default:
81 return VB2_ERROR_SECDATAK_GET_PARAM;
82 }
83}
84
85int vb2_secdatak_set(struct vb2_context *ctx,
86 enum vb2_secdatak_param param,
87 uint32_t value)
88{
89 struct vb2_secdatak *sec = (struct vb2_secdatak *)ctx->secdatak;
90 uint32_t now;
91
92 if (!(vb2_get_sd(ctx)->status & VB2_SD_STATUS_SECDATAK_INIT))
93 return VB2_ERROR_SECDATAK_SET_UNINITIALIZED;
94
95 /* If not changing the value, don't regenerate the CRC. */
96 if (vb2_secdatak_get(ctx, param, &now) == VB2_SUCCESS && now == value)
97 return VB2_SUCCESS;
98
99 switch(param) {
100 case VB2_SECDATAK_VERSIONS:
101 sec->kernel_versions = value;
102 break;
103
104 default:
105 return VB2_ERROR_SECDATAK_SET_PARAM;
106 }
107
108 /* Regenerate CRC */
109 sec->crc8 = vb2_crc8(sec, offsetof(struct vb2_secdatak, crc8));
110 ctx->flags |= VB2_CONTEXT_SECDATAK_CHANGED;
111 return VB2_SUCCESS;
112}