Commit | Line | Data |
---|---|---|
cd9434cc T |
1 | /* |
2 | * Copyright (c) 2015 TRUSTONIC LIMITED | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * 1. Redistributions of source code must retain the above copyright notice, | |
9 | * this list of conditions and the following disclaimer. | |
10 | * | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * | |
15 | * 3. Neither the name of the TRUSTONIC LIMITED nor the names of its | |
16 | * contributors may be used to endorse or promote products derived from | |
17 | * this software without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | |
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
26 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
28 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
29 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 | */ | |
31 | ||
32 | #include <hardware/keymaster_defs.h> | |
33 | #include <hardware/keymaster1.h> | |
34 | ||
35 | #include "test_km_util.h" | |
36 | ||
37 | #undef LOG_ANDROID | |
38 | #undef LOG_TAG | |
39 | #define LOG_TAG "TlcTeeKeyMasterTest" | |
40 | #include "log.h" | |
41 | ||
42 | // static void print_blob( | |
43 | // const keymaster_blob_t *blob) | |
44 | // { | |
45 | // print_buffer(blob->data, blob->data_length); | |
46 | // } | |
47 | ||
48 | static void print_uint64( | |
49 | uint64_t x) | |
50 | { | |
51 | printf("0x%08x%08x\n", (uint32_t)(x >> 32), (uint32_t)(x & 0xFFFFFFFF)); | |
52 | } | |
53 | ||
54 | static void print_bool( | |
55 | bool x) | |
56 | { | |
57 | if (x) { | |
58 | printf("true\n"); | |
59 | } else { | |
60 | printf("false\n"); | |
61 | } | |
62 | } | |
63 | ||
64 | static void print_param_set( | |
65 | const keymaster_key_param_set_t *param_set) | |
66 | { | |
67 | for (size_t i = 0; i < param_set->length; i++) { | |
68 | const keymaster_key_param_t param = param_set->params[i]; | |
69 | switch (param.tag) { | |
70 | #define PRINT_ENUM printf("0x%08x\n", param.enumerated) | |
71 | #define PRINT_UINT printf("%u\n", param.integer) | |
72 | #define PRINT_ULONG print_uint64(param.long_integer) | |
73 | #define PRINT_BOOL print_bool(param.boolean) | |
74 | #define PRINT_DATETIME print_uint64(param.date_time) | |
75 | #define PARAM_CASE(tag, printval) \ | |
76 | case tag: \ | |
77 | printf("%s: ", #tag); \ | |
78 | printval; \ | |
79 | break; | |
80 | #define PARAM_CASE_ENUM(tag) PARAM_CASE(tag, PRINT_ENUM) | |
81 | #define PARAM_CASE_UINT(tag) PARAM_CASE(tag, PRINT_UINT) | |
82 | #define PARAM_CASE_ULONG(tag) PARAM_CASE(tag, PRINT_ULONG) | |
83 | #define PARAM_CASE_BOOL(tag) PARAM_CASE(tag, PRINT_BOOL) | |
84 | #define PARAM_CASE_DATETIME(tag) PARAM_CASE(tag, PRINT_DATETIME) | |
85 | PARAM_CASE_DATETIME(KM_TAG_ACTIVE_DATETIME) | |
86 | PARAM_CASE_ENUM(KM_TAG_ALGORITHM) | |
87 | PARAM_CASE_UINT(KM_TAG_AUTH_TIMEOUT) | |
88 | PARAM_CASE_ENUM(KM_TAG_BLOB_USAGE_REQUIREMENTS) | |
89 | PARAM_CASE_ENUM(KM_TAG_BLOCK_MODE) | |
90 | PARAM_CASE_BOOL(KM_TAG_BOOTLOADER_ONLY) | |
91 | PARAM_CASE_BOOL(KM_TAG_CALLER_NONCE) | |
92 | PARAM_CASE_DATETIME(KM_TAG_CREATION_DATETIME) | |
93 | PARAM_CASE_ENUM(KM_TAG_DIGEST) | |
94 | PARAM_CASE_UINT(KM_TAG_KEY_SIZE) | |
95 | PARAM_CASE_UINT(KM_TAG_MAX_USES_PER_BOOT) | |
96 | PARAM_CASE_UINT(KM_TAG_MIN_MAC_LENGTH) | |
97 | PARAM_CASE_UINT(KM_TAG_MIN_SECONDS_BETWEEN_OPS) | |
98 | PARAM_CASE_BOOL(KM_TAG_NO_AUTH_REQUIRED) | |
99 | PARAM_CASE_ENUM(KM_TAG_ORIGIN) | |
100 | PARAM_CASE_DATETIME(KM_TAG_ORIGINATION_EXPIRE_DATETIME) | |
101 | PARAM_CASE_ENUM(KM_TAG_PADDING) | |
102 | PARAM_CASE_ENUM(KM_TAG_PURPOSE) | |
103 | PARAM_CASE_BOOL(KM_TAG_ROLLBACK_RESISTANT) | |
104 | PARAM_CASE_ULONG(KM_TAG_RSA_PUBLIC_EXPONENT) | |
105 | PARAM_CASE_DATETIME(KM_TAG_USAGE_EXPIRE_DATETIME) | |
106 | PARAM_CASE_ENUM(KM_TAG_USER_AUTH_TYPE) | |
107 | PARAM_CASE_ULONG(KM_TAG_USER_SECURE_ID) | |
108 | #undef PARAM_CASE_ENUM | |
109 | #undef PARAM_CASE_UINT | |
110 | #undef PARAM_CASE_ULONG | |
111 | #undef PARAM_CASE_BOOL | |
112 | #undef PARAM_CASE_DATETIME | |
113 | #undef PARAM_CASE | |
114 | #undef PRINT_ENUM | |
115 | #undef PRINT_UINT | |
116 | #undef PRINT_ULONG | |
117 | #undef PRINT_BOOL | |
118 | #undef PRINT_DATETIME | |
119 | default: | |
120 | printf("(unknown tag 0x%08x)\n", param.tag); | |
121 | } | |
122 | } | |
123 | } | |
124 | ||
125 | void print_buffer( | |
126 | const uint8_t *buf, | |
127 | size_t buflen) | |
128 | { | |
129 | printf("["); | |
130 | for (size_t i = 0; i < buflen; i++) { | |
131 | printf("0x%02x, ", buf[i]); | |
132 | } | |
133 | printf("]\n"); | |
134 | } | |
135 | ||
136 | void print_characteristics( | |
137 | const keymaster_key_characteristics_t *characteristics) | |
138 | { | |
139 | printf("\nHardware-enforced characteristics:\n"); | |
140 | print_param_set(&characteristics->hw_enforced); | |
141 | printf("\nSoftware-enforced characteristics:\n"); | |
142 | print_param_set(&characteristics->sw_enforced); | |
143 | } | |
144 | ||
145 | keymaster_error_t print_raw_key( | |
146 | keymaster1_device_t *device, | |
147 | const keymaster_key_blob_t *key_blob) | |
148 | { | |
149 | keymaster_error_t res = KM_ERROR_OK; | |
150 | keymaster_blob_t key_data = {0, 0}; | |
151 | ||
152 | CHECK_RESULT_OK( device->export_key(device, | |
153 | KM_KEY_FORMAT_RAW, key_blob, NULL, NULL, &key_data) ); | |
154 | ||
155 | print_buffer(key_data.data, key_data.data_length); | |
156 | ||
157 | end: | |
158 | km_free_blob(&key_data); | |
159 | return res; | |
160 | } | |
161 | ||
162 | uint32_t block_length( | |
163 | keymaster_digest_t digest) | |
164 | { | |
165 | switch (digest) { | |
166 | case KM_DIGEST_MD5: | |
167 | return 128; | |
168 | case KM_DIGEST_SHA1: | |
169 | return 160; | |
170 | case KM_DIGEST_SHA_2_224: | |
171 | return 224; | |
172 | case KM_DIGEST_SHA_2_256: | |
173 | return 256; | |
174 | case KM_DIGEST_SHA_2_384: | |
175 | return 384; | |
176 | case KM_DIGEST_SHA_2_512: | |
177 | return 512; | |
178 | default: | |
179 | return 0; | |
180 | } | |
181 | } | |
182 | ||
183 | void km_free_blob( | |
184 | keymaster_blob_t *blob) | |
185 | { | |
186 | if (blob != NULL) { | |
187 | free((void*)blob->data); | |
188 | blob->data = NULL; | |
189 | blob->data_length = 0; | |
190 | } | |
191 | } | |
192 | ||
193 | void km_free_key_blob( | |
194 | keymaster_key_blob_t *key_blob) | |
195 | { | |
196 | if (key_blob != NULL) { | |
197 | free((void*)key_blob->key_material); | |
198 | key_blob->key_material = NULL; | |
199 | key_blob->key_material_size = 0; | |
200 | } | |
201 | } |