3 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <linux/ctype.h>
12 #include <asm/unaligned.h>
24 /* fetch list of already loaded keys in the TPM */
25 err = tpm_get_capability(TPM_CAP_HANDLE, TPM_RT_KEY, buf, sizeof(buf));
28 key_count = get_unaligned_be16(buf);
30 for (i = 0; i < key_count; ++i, ptr += 4) {
31 err = tpm_flush_specific(get_unaligned_be32(ptr), TPM_RT_KEY);
32 if (err && err != TPM_KEY_OWNER_CONTROL)
39 int decode_hexstr(char *hexstr, u8 **result)
41 int len = strlen(hexstr);
49 *result = (u8 *)malloc(bytes);
51 for (i = 0; i < len; i++) {
52 char cur = tolower(hexstr[i]);
55 if ((cur >= 'a' && cur <= 'f') || (cur >= '0' && cur <= '9')) {
56 val = cur - (cur > '9' ? 87 : 48);
61 (*result)[i / 2] = acc + val;
71 int extract_subprogram(u8 **progdata, u32 expected_magic,
72 struct key_program **result)
74 struct key_program *prog = *result;
75 u32 magic, code_crc, code_size;
77 magic = get_unaligned_be32(*progdata);
78 code_crc = get_unaligned_be32(*progdata + 4);
79 code_size = get_unaligned_be32(*progdata + 8);
83 if (magic != expected_magic)
86 *result = malloc(sizeof(struct key_program) + code_size);
92 prog->code_crc = code_crc;
93 prog->code_size = code_size;
94 memcpy(prog->code, *progdata, code_size);
96 *progdata += code_size;
98 if (hre_verify_program(prog)) {
106 struct key_program *parse_and_check_keyprog(u8 *progdata)
108 struct key_program *result = NULL, *hmac = NULL;
110 /* Part 1: Load key program */
112 if (extract_subprogram(&progdata, MAGIC_KEY_PROGRAM, &result))
115 /* Part 2: Load hmac program */
117 if (extract_subprogram(&progdata, MAGIC_HMAC, &hmac))
125 int load_and_run_keyprog(void)
130 struct key_program *prog;
132 cmd = getenv("loadkeyprogram");
134 if (!cmd || run_command(cmd, 0))
137 hexprog = getenv("keyprogram");
139 if (decode_hexstr(hexprog, &binprog))
142 prog = parse_and_check_keyprog(binprog);
148 if (hre_run_program(prog->code, prog->code_size)) {
153 printf("\nSD code ran successfully\n");