1 /* Copyright 2019 The ChromiumOS Authors
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 host library vboot2 key functions
6 */
7
8 #include "2common.h"
9 #include "common/tests.h"
10 #include "host_common.h"
11
12 /* Public key utility functions */
public_key_tests(void)13 static void public_key_tests(void)
14 {
15 struct vb2_packed_key k[3];
16 struct vb2_packed_key j[5];
17
18 /* Fill some bits of the public key data */
19 memset(j, 0, sizeof(j));
20 memset(k, 0x42, sizeof(k));
21 k[1].key_size = 12345;
22 k[2].key_version = 67;
23
24 vb2_init_packed_key(k, (uint8_t*)(k + 1),
25 2 * sizeof(struct vb2_packed_key));
26 TEST_EQ(k->key_offset, sizeof(struct vb2_packed_key),
27 "vb2_init_packed_key key_offset");
28 TEST_EQ(k->key_size, 2 * sizeof(struct vb2_packed_key),
29 "vb2_init_packed_key key_size");
30 TEST_EQ(k->algorithm, VB2_ALG_COUNT, "vb2_init_packed_key algorithm");
31 TEST_EQ(k->key_version, 0, "vb2_init_packed_key key_version");
32
33 /* Set algorithm and version, so we can tell if they get copied */
34 k->algorithm = 3;
35 k->key_version = 21;
36
37 /* Copying to a smaller destination should fail */
38 vb2_init_packed_key(j, (uint8_t*)(j + 1),
39 2 * sizeof(struct vb2_packed_key) - 1);
40 TEST_NEQ(0, vb2_copy_packed_key(j, k), "vb2_copy_packed_key too small");
41
42 /* Copying to same or larger size should succeed */
43 vb2_init_packed_key(j, (uint8_t*)(j + 2),
44 2 * sizeof(struct vb2_packed_key) + 1);
45 TEST_EQ(0, vb2_copy_packed_key(j, k), "vb2_copy_packed_key same");
46 /* Offset in destination shouldn't have been modified */
47 TEST_EQ(j->key_offset, 2 * sizeof(struct vb2_packed_key),
48 "vb2_copy_packed_key key_offset");
49 /* Size should have been reduced to match the source */
50 TEST_EQ(k->key_size, 2 * sizeof(struct vb2_packed_key),
51 "vb2_copy_packed_key key_size");
52 /* Other fields should have been copied */
53 TEST_EQ(k->algorithm, j->algorithm, "vb2_copy_packed_key algorithm");
54 TEST_EQ(k->key_version, j->key_version,
55 "vb2_copy_packed_key key_version");
56 /* Data should have been copied */
57 TEST_EQ(0,
58 memcmp(vb2_packed_key_data(k),
59 vb2_packed_key_data(j), k->key_size),
60 "vb2_copy_packed_key data");
61 }
62
main(int argc,char * argv[])63 int main(int argc, char* argv[])
64 {
65 public_key_tests();
66
67 return gTestSuccess ? 0 : 255;
68 }
69