3 * Reinhard Pfau, Guntermann & Drunck GmbH, reinhard.pfau@gdsys.cc
5 * SPDX-License-Identifier: GPL-2.0+
14 #include <u-boot/sha1.h>
15 #include <asm/byteorder.h>
16 #include <asm/unaligned.h>
23 ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
24 ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
25 ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
26 ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
27 ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
42 /* register constants */
44 FIX_HREG_DEVICE_ID_HASH = 0,
51 static struct h_reg pcr_hregs[24];
52 static struct h_reg fix_hregs[COUNT_FIX_HREGS];
53 static struct h_reg var_hregs[8];
57 /* opcodes w/o data */
61 /* opcodes w/o data, w/ sync dst */
64 /* opcodes w/data, w/sync dst */
79 static uint64_t device_id;
80 static uint64_t device_cl;
81 static uint64_t device_type;
83 static uint32_t platform_key_handle;
85 static uint32_t hre_tpm_err;
86 static int hre_err = HRE_E_OK;
88 #define IS_PCR_HREG(spec) ((spec) & 0x20)
89 #define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
90 #define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
91 #define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
93 static const uint8_t vendor[] = "Guntermann & Drunck";
96 * @brief get the size of a given (TPM) NV area
97 * @param index NV index of the area to get size for
98 * @param size pointer to the size
99 * @return 0 on success, != 0 on error
101 static int get_tpm_nv_size(uint32_t index, uint32_t *size)
108 err = tpm_get_capability(TPM_CAP_NV_INDEX, index,
111 printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
116 /* skip tag and nvIndex */
118 /* skip 2 pcr info fields */
119 v16 = get_unaligned_be16(ptr);
120 ptr += 2 + v16 + 1 + 20;
121 v16 = get_unaligned_be16(ptr);
122 ptr += 2 + v16 + 1 + 20;
123 /* skip permission and flags */
126 *size = get_unaligned_be32(ptr);
131 * @brief search for a key by usage auth and pub key hash.
132 * @param auth usage auth of the key to search for
133 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
134 * @param[out] handle the handle of the key iff found
135 * @return 0 if key was found in TPM; != 0 if not.
137 static int find_key(const uint8_t auth[20], const uint8_t pubkey_digest[20],
141 uint32_t key_handles[10];
149 /* fetch list of already loaded keys in the TPM */
150 err = tpm_get_capability(TPM_CAP_HANDLE, TPM_RT_KEY, buf, sizeof(buf));
153 key_count = get_unaligned_be16(buf);
155 for (i = 0; i < key_count; ++i, ptr += 4)
156 key_handles[i] = get_unaligned_be32(ptr);
158 /* now search a(/ the) key which we can access with the given auth */
159 for (i = 0; i < key_count; ++i) {
160 buf_len = sizeof(buf);
161 err = tpm_get_pub_key_oiap(key_handles[i], auth, buf, &buf_len);
162 if (err && err != TPM_AUTHFAIL)
166 sha1_csum(buf, buf_len, digest);
167 if (!memcmp(digest, pubkey_digest, 20)) {
168 *handle = key_handles[i];
176 * @brief read CCDM common data from TPM NV
177 * @return 0 if CCDM common data was found and read, !=0 if something failed.
179 static int read_common_data(void)
186 if (get_tpm_nv_size(NV_COMMON_DATA_INDEX, &size) ||
187 size < NV_COMMON_DATA_MIN_SIZE)
189 err = tpm_nv_read_value(NV_COMMON_DATA_INDEX,
190 buf, min(sizeof(buf), size));
192 printf("tpm_nv_read_value() failed: %u\n", err);
196 device_id = get_unaligned_be64(buf);
197 device_cl = get_unaligned_be64(buf + 8);
198 device_type = get_unaligned_be64(buf + 16);
201 sha1_update(&ctx, buf, 24);
202 sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
203 fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
205 platform_key_handle = get_unaligned_be32(buf + 24);
211 * @brief get pointer to hash register by specification
212 * @param spec specification of a hash register
213 * @return pointer to hash register or NULL if @a spec does not qualify a
214 * valid hash register; NULL else.
216 static struct h_reg *get_hreg(uint8_t spec)
220 idx = HREG_IDX(spec);
221 if (IS_FIX_HREG(spec)) {
222 if (idx < ARRAY_SIZE(fix_hregs))
223 return fix_hregs + idx;
224 hre_err = HRE_E_INVALID_HREG;
225 } else if (IS_PCR_HREG(spec)) {
226 if (idx < ARRAY_SIZE(pcr_hregs))
227 return pcr_hregs + idx;
228 hre_err = HRE_E_INVALID_HREG;
229 } else if (IS_VAR_HREG(spec)) {
230 if (idx < ARRAY_SIZE(var_hregs))
231 return var_hregs + idx;
232 hre_err = HRE_E_INVALID_HREG;
238 * @brief get pointer of a hash register by specification and usage.
239 * @param spec specification of a hash register
240 * @param mode access mode (read or write or read/write)
241 * @return pointer to hash register if found and valid; NULL else.
243 * This func uses @a get_reg() to determine the hash register for a given spec.
244 * If a register is found it is validated according to the desired access mode.
245 * The value of automatic registers (PCR register and fixed registers) is
246 * loaded or computed on read access.
248 static struct h_reg *access_hreg(uint8_t spec, enum access_mode mode)
250 struct h_reg *result;
252 result = get_hreg(spec);
256 if (mode & HREG_WR) {
257 if (IS_FIX_HREG(spec)) {
258 hre_err = HRE_E_INVALID_HREG;
262 if (mode & HREG_RD) {
263 if (!result->valid) {
264 if (IS_PCR_HREG(spec)) {
265 hre_tpm_err = tpm_pcr_read(HREG_IDX(spec),
267 result->valid = (hre_tpm_err == TPM_SUCCESS);
268 } else if (IS_FIX_HREG(spec)) {
269 switch (HREG_IDX(spec)) {
270 case FIX_HREG_DEVICE_ID_HASH:
273 case FIX_HREG_VENDOR:
274 memcpy(result->digest, vendor, 20);
275 result->valid = true;
279 result->valid = true;
282 if (!result->valid) {
283 hre_err = HRE_E_INVALID_HREG;
291 static void *compute_and(void *_dst, const void *_src, size_t n)
294 const uint8_t *src = _src;
297 for (i = n; i-- > 0; )
303 static void *compute_or(void *_dst, const void *_src, size_t n)
306 const uint8_t *src = _src;
309 for (i = n; i-- > 0; )
315 static void *compute_xor(void *_dst, const void *_src, size_t n)
318 const uint8_t *src = _src;
321 for (i = n; i-- > 0; )
327 static void *compute_extend(void *_dst, const void *_src, size_t n)
333 sha1_update(&ctx, _dst, n);
334 sha1_update(&ctx, _src, n);
335 sha1_finish(&ctx, digest);
336 memcpy(_dst, digest, min(n, sizeof(digest)));
341 static int hre_op_loadkey(struct h_reg *src_reg, struct h_reg *dst_reg,
342 const void *key, size_t key_size)
344 uint32_t parent_handle;
347 if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
349 if (find_key(src_reg->digest, dst_reg->digest, &parent_handle))
351 hre_tpm_err = tpm_load_key2_oiap(parent_handle, key, key_size,
352 src_reg->digest, &key_handle);
354 hre_err = HRE_E_TPM_FAILURE;
362 * @brief executes the next opcode on the hash register engine.
363 * @param[in,out] ip pointer to the opcode (instruction pointer)
364 * @param[in,out] code_size (remaining) size of the code
365 * @return new instruction pointer on success, NULL on error.
367 static const uint8_t *hre_execute_op(const uint8_t **ip, size_t *code_size)
369 bool dst_modified = false;
375 struct h_reg *src_reg, *dst_reg;
377 const uint8_t *src_buf, *data;
380 void * (*bin_func)(void *, const void *, size_t);
385 ins = get_unaligned_be32(*ip);
388 src_spec = (ins >> 18) & 0x3f;
389 dst_spec = (ins >> 12) & 0x3f;
390 data_size = (ins & 0x7ff);
392 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
393 opcode, src_spec, dst_spec, data_size);
395 if ((opcode & 0x80) && (data_size + 4) > *code_size)
398 src_reg = access_hreg(src_spec, HREG_RD);
399 if (hre_err || hre_tpm_err)
401 dst_reg = access_hreg(dst_spec, (opcode & 0x40) ? HREG_RDWR : HREG_WR);
402 if (hre_err || hre_tpm_err)
410 for (i = 0; i < 20; ++i) {
411 if (src_reg->digest[i])
420 bin_func = compute_xor;
423 bin_func = compute_and;
426 bin_func = compute_or;
429 bin_func = compute_extend;
434 src_buf = src_reg->digest;
439 } else if (data_size == 1) {
440 memset(buf, *data, 20);
442 } else if (data_size >= 20) {
446 for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
447 i -= data_size, ptr += data_size)
449 min_t(size_t, i, data_size));
452 bin_func(dst_reg->digest, src_buf, 20);
453 dst_reg->valid = true;
457 if (hre_op_loadkey(src_reg, dst_reg, data, data_size))
464 if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
465 hre_tpm_err = tpm_extend(HREG_IDX(dst_spec), dst_reg->digest,
468 hre_err = HRE_E_TPM_FAILURE;
477 *code_size -= data_size;
484 * @brief runs a program on the hash register engine.
485 * @param code pointer to the (HRE) code.
486 * @param code_size size of the code (in bytes).
487 * @return 0 on success, != 0 on failure.
489 int hre_run_program(const uint8_t *code, size_t code_size)
492 const uint8_t *ip = code;
494 code_left = code_size;
497 while (code_left > 0)
498 if (!hre_execute_op(&ip, &code_left))
504 int hre_verify_program(struct key_program *prg)
508 crc = crc32(0, prg->code, prg->code_size);
510 if (crc != prg->code_crc) {
511 printf("HRC crc mismatch: %08x != %08x\n",