1 //**********************************************************************;
2 // Copyright (c) 2017, Intel Corporation
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
15 // 3. Neither the name of Intel Corporation nor the names of its contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 // THE POSSIBILITY OF SUCH DAMAGE.
30 //**********************************************************************;
39 #include "tpm2_alg_util.h"
40 #include "tpm2_attr_util.h"
41 #include "tpm2_tool.h"
42 #include "tpm2_util.h"
44 bool tpm2_util_concat_buffer(TPM2B_MAX_BUFFER *result, TPM2B *append) {
46 if (!result || !append) {
50 if ((result->size + append->size) < result->size) {
54 if ((result->size + append->size) > TPM2_MAX_DIGEST_BUFFER) {
58 memcpy(&result->buffer[result->size], append->buffer, append->size);
59 result->size += append->size;
64 bool tpm2_util_string_to_uint16(const char *str, uint16_t *value) {
67 bool result = tpm2_util_string_to_uint32(str, &tmp);
72 /* overflow on 16 bits? */
73 if (tmp > UINT16_MAX) {
77 *value = (uint16_t) tmp;
81 bool tpm2_util_string_to_uint32(const char *str, uint32_t *value) {
85 if (str == NULL || *str == '\0') {
89 /* clear errno before the call, should be 0 afterwards */
91 uint32_t tmp = strtoul(str, &endptr, 0);
97 * The entire string should be able to be converted or fail
98 * We already checked that str starts with a null byte, so no
99 * need to check that again per the man page.
101 if (*endptr != '\0') {
109 int tpm2_util_hex_to_byte_structure(const char *inStr, UINT16 *byteLength,
111 int strLength; //if the inStr likes "1a2b...", no prefix "0x"
113 if (inStr == NULL || byteLength == NULL || byteBuffer == NULL)
115 strLength = strlen(inStr);
118 for (i = 0; i < strLength; i++) {
119 if (!isxdigit(inStr[i]))
123 if (*byteLength < strLength / 2)
126 *byteLength = strLength / 2;
128 for (i = 0; i < *byteLength; i++) {
129 char tmpStr[4] = { 0 };
130 tmpStr[0] = inStr[i * 2];
131 tmpStr[1] = inStr[i * 2 + 1];
132 byteBuffer[i] = strtol(tmpStr, NULL, 16);
137 void tpm2_util_hexdump(const BYTE *data, size_t len) {
139 if (!output_enabled) {
144 for (i=0; i < len; i++) {
145 printf("%02x", data[i]);
149 bool tpm2_util_hexdump_file(FILE *fd, size_t len) {
150 BYTE* buff = (BYTE*)malloc(len);
152 LOG_ERR("malloc() failed");
156 bool res = files_read_bytes(fd, buff, len);
158 LOG_ERR("Failed to read file");
163 tpm2_util_hexdump(buff, len);
169 bool tpm2_util_print_tpm2b_file(FILE *fd)
172 bool res = files_read_16(fd, &len);
174 LOG_ERR("File read failed");
177 return tpm2_util_hexdump_file(fd, len);
180 bool tpm2_util_is_big_endian(void) {
185 test_word = 0xFF000000;
186 test_byte = (uint8_t *) (&test_word);
188 return test_byte[0] == 0xFF;
191 #define STRING_BYTES_ENDIAN_CONVERT(size) \
192 UINT##size tpm2_util_endian_swap_##size(UINT##size data) { \
194 UINT##size converted; \
195 UINT8 *bytes = (UINT8 *)&data; \
196 UINT8 *tmp = (UINT8 *)&converted; \
199 for(i=0; i < sizeof(UINT##size); i ++) { \
200 tmp[i] = bytes[sizeof(UINT##size) - i - 1]; \
206 STRING_BYTES_ENDIAN_CONVERT(16)
207 STRING_BYTES_ENDIAN_CONVERT(32)
208 STRING_BYTES_ENDIAN_CONVERT(64)
210 #define STRING_BYTES_ENDIAN_HTON(size) \
211 UINT##size tpm2_util_hton_##size(UINT##size data) { \
213 bool is_big_endian = tpm2_util_is_big_endian(); \
214 if (is_big_endian) { \
218 return tpm2_util_endian_swap_##size(data); \
221 STRING_BYTES_ENDIAN_HTON(16)
222 STRING_BYTES_ENDIAN_HTON(32)
223 STRING_BYTES_ENDIAN_HTON(64)
226 * Converting from host-to-network (hton) or network-to-host (ntoh) is
227 * the same operation: if endianess differs between host and data, swap
228 * endianess. Thus we can just call the hton routines, but have some nice
231 UINT16 tpm2_util_ntoh_16(UINT16 data) {
232 return tpm2_util_hton_16(data);
235 UINT32 tpm2_util_ntoh_32(UINT32 data) {
236 return tpm2_util_hton_32(data);
238 UINT64 tpm2_util_ntoh_64(UINT64 data) {
239 return tpm2_util_hton_64(data);
242 UINT32 tpm2_util_pop_count(UINT32 data) {
244 static const UINT8 bits_per_nibble[] =
245 {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
248 UINT8 *d = (UINT8 *)&data;
251 for (i=0; i < sizeof(data); i++) {
252 count += bits_per_nibble[d[i] & 0x0f];
253 count += bits_per_nibble[d[i] >> 4];
259 #define TPM2_UTIL_KEYDATA_INIT { .len = 0 };
261 typedef struct tpm2_util_keydata tpm2_util_keydata;
262 struct tpm2_util_keydata {
270 static void tpm2_util_public_to_keydata(TPM2B_PUBLIC *public, tpm2_util_keydata *keydata) {
272 switch (public->publicArea.type) {
275 keydata->entries[0].name = tpm2_alg_util_algtostr(public->publicArea.type);
276 keydata->entries[0].value = (TPM2B *)&public->publicArea.unique.rsa;
278 case TPM2_ALG_KEYEDHASH:
280 keydata->entries[0].name = tpm2_alg_util_algtostr(public->publicArea.type);
281 keydata->entries[0].value = (TPM2B *)&public->publicArea.unique.keyedHash;
283 case TPM2_ALG_SYMCIPHER:
285 keydata->entries[0].name = tpm2_alg_util_algtostr(public->publicArea.type);
286 keydata->entries[0].value = (TPM2B *)&public->publicArea.unique.sym;
290 keydata->entries[0].name = "x";
291 keydata->entries[0].value = (TPM2B *)&public->publicArea.unique.ecc.x;
292 keydata->entries[1].name = "y";
293 keydata->entries[1].value = (TPM2B *)&public->publicArea.unique.ecc.y;
296 LOG_WARN("The algorithm type(0x%4.4x) is not supported",
297 public->publicArea.type);
303 void print_yaml_indent(size_t indent_count) {
304 while (indent_count--) {
305 tpm2_tool_output(" ");
309 void tpm2_util_tpma_object_to_yaml(TPMA_OBJECT obj) {
311 char *attrs = tpm2_attr_util_obj_attrtostr(obj);
312 tpm2_tool_output("attributes:\n");
313 tpm2_tool_output(" value: %s\n", attrs);
314 tpm2_tool_output(" raw: 0x%x\n", obj);
318 void tpm2_util_public_to_yaml(TPM2B_PUBLIC *public) {
320 tpm2_tool_output("algorithm:\n");
321 tpm2_tool_output(" value: %s\n", tpm2_alg_util_algtostr(public->publicArea.nameAlg));
322 tpm2_tool_output(" raw: 0x%x\n", public->publicArea.nameAlg);
324 tpm2_util_tpma_object_to_yaml(public->publicArea.objectAttributes);
326 tpm2_tool_output("type: \n");
327 tpm2_tool_output(" value: %s\n", tpm2_alg_util_algtostr(public->publicArea.type));
328 tpm2_tool_output(" raw: 0x%x\n", public->publicArea.type);
330 tpm2_util_keydata keydata = TPM2_UTIL_KEYDATA_INIT;
331 tpm2_util_public_to_keydata(public, &keydata);
334 /* if no keydata len will be 0 and it wont print */
335 for (i=0; i < keydata.len; i++) {
336 tpm2_tool_output(" %s: ", keydata.entries[i].name);
337 tpm2_util_print_tpm2b(keydata.entries[i].value);
338 tpm2_tool_output("\n");
341 if (public->publicArea.authPolicy.size) {
342 tpm2_tool_output("authorization policy: ");
343 tpm2_util_hexdump(public->publicArea.authPolicy.buffer,
344 public->publicArea.authPolicy.size);
345 tpm2_tool_output("\n");