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  * Tests for firmware secure storage library.
6  */
7 
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include "test_common.h"
14 #include "vboot_common.h"
15 
16 #include "2common.h"
17 #include "2api.h"
18 #include "2misc.h"
19 #include "2secdata.h"
20 
test_changed(struct vb2_context * ctx,int changed,const char * why)21 static void test_changed(struct vb2_context *ctx, int changed, const char *why)
22 {
23 	if (changed)
24 		TEST_NEQ(ctx->flags & VB2_CONTEXT_SECDATA_CHANGED, 0, why);
25 	else
26 		TEST_EQ(ctx->flags & VB2_CONTEXT_SECDATA_CHANGED, 0, why);
27 
28 	ctx->flags &= ~VB2_CONTEXT_SECDATA_CHANGED;
29 };
30 
secdata_test(void)31 static void secdata_test(void)
32 {
33 	uint8_t workbuf[VB2_WORKBUF_RECOMMENDED_SIZE]
34 		__attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
35 	struct vb2_context c = {
36 		.flags = 0,
37 		.workbuf = workbuf,
38 		.workbuf_size = sizeof(workbuf),
39 	};
40 	uint32_t v = 1;
41 
42 	/* Blank data is invalid */
43 	memset(c.secdata, 0xa6, sizeof(c.secdata));
44 	TEST_EQ(vb2_secdata_check_crc(&c),
45 		VB2_ERROR_SECDATA_CRC, "Check blank CRC");
46 	TEST_EQ(vb2_secdata_init(&c),
47 		 VB2_ERROR_SECDATA_CRC, "Init blank CRC");
48 
49 	/* Create good data */
50 	TEST_SUCC(vb2_secdata_create(&c), "Create");
51 	TEST_SUCC(vb2_secdata_check_crc(&c), "Check created CRC");
52 	TEST_SUCC(vb2_secdata_init(&c), "Init created CRC");
53 	test_changed(&c, 1, "Create changes data");
54 
55 	/* Now corrupt it */
56 	c.secdata[2]++;
57 	TEST_EQ(vb2_secdata_check_crc(&c),
58 		VB2_ERROR_SECDATA_CRC, "Check invalid CRC");
59 	TEST_EQ(vb2_secdata_init(&c),
60 		 VB2_ERROR_SECDATA_CRC, "Init invalid CRC");
61 
62 	vb2_secdata_create(&c);
63 	c.flags = 0;
64 
65 	/* Read/write flags */
66 	TEST_SUCC(vb2_secdata_get(&c, VB2_SECDATA_FLAGS, &v), "Get flags");
67 	TEST_EQ(v, 0, "Flags created 0");
68 	test_changed(&c, 0, "Get doesn't change data");
69 	TEST_SUCC(vb2_secdata_set(&c, VB2_SECDATA_FLAGS, 0x12), "Set flags");
70 	test_changed(&c, 1, "Set changes data");
71 	TEST_SUCC(vb2_secdata_set(&c, VB2_SECDATA_FLAGS, 0x12), "Set flags 2");
72 	test_changed(&c, 0, "Set again doesn't change data");
73 	TEST_SUCC(vb2_secdata_get(&c, VB2_SECDATA_FLAGS, &v), "Get flags 2");
74 	TEST_EQ(v, 0x12, "Flags changed");
75 	TEST_EQ(vb2_secdata_set(&c, VB2_SECDATA_FLAGS, 0x100),
76 		VB2_ERROR_SECDATA_SET_FLAGS, "Bad flags");
77 
78 	/* Read/write versions */
79 	TEST_SUCC(vb2_secdata_get(&c, VB2_SECDATA_VERSIONS, &v),
80 		  "Get versions");
81 	TEST_EQ(v, 0, "Versions created 0");
82 	test_changed(&c, 0, "Get doesn't change data");
83 	TEST_SUCC(vb2_secdata_set(&c, VB2_SECDATA_VERSIONS, 0x123456ff),
84 		  "Set versions");
85 	test_changed(&c, 1, "Set changes data");
86 	TEST_SUCC(vb2_secdata_set(&c, VB2_SECDATA_VERSIONS, 0x123456ff),
87 		  "Set versions 2");
88 	test_changed(&c, 0, "Set again doesn't change data");
89 	TEST_SUCC(vb2_secdata_get(&c, VB2_SECDATA_VERSIONS, &v),
90 		  "Get versions 2");
91 	TEST_EQ(v, 0x123456ff, "Versions changed");
92 
93 	/* Invalid field fails */
94 	TEST_EQ(vb2_secdata_get(&c, -1, &v),
95 		VB2_ERROR_SECDATA_GET_PARAM, "Get invalid");
96 	TEST_EQ(vb2_secdata_set(&c, -1, 456),
97 		VB2_ERROR_SECDATA_SET_PARAM, "Set invalid");
98 	test_changed(&c, 0, "Set invalid field doesn't change data");
99 
100 	/* Read/write uninitialized data fails */
101 	vb2_get_sd(&c)->status &= ~VB2_SD_STATUS_SECDATA_INIT;
102 	TEST_EQ(vb2_secdata_get(&c, VB2_SECDATA_VERSIONS, &v),
103 		VB2_ERROR_SECDATA_GET_UNINITIALIZED, "Get uninitialized");
104 	test_changed(&c, 0, "Get uninitialized doesn't change data");
105 	TEST_EQ(vb2_secdata_set(&c, VB2_SECDATA_VERSIONS, 0x123456ff),
106 		VB2_ERROR_SECDATA_SET_UNINITIALIZED, "Set uninitialized");
107 	test_changed(&c, 0, "Set uninitialized doesn't change data");
108 }
109 
main(int argc,char * argv[])110 int main(int argc, char* argv[])
111 {
112 	secdata_test();
113 
114 	return gTestSuccess ? 0 : 255;
115 }
116