1 // SPDX-License-Identifier: GPL-2.0+
4 * Reinhard Pfau, Guntermann & Drunck GmbH, reinhard.pfau@gdsys.cc
13 #include <u-boot/sha1.h>
14 #include <asm/byteorder.h>
15 #include <asm/unaligned.h>
22 ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
23 ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
24 ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
25 ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
26 ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
41 /* register constants */
43 FIX_HREG_DEVICE_ID_HASH = 0,
50 static struct h_reg pcr_hregs[24];
51 static struct h_reg fix_hregs[COUNT_FIX_HREGS];
52 static struct h_reg var_hregs[8];
56 /* opcodes w/o data */
60 /* opcodes w/o data, w/ sync dst */
63 /* opcodes w/data, w/sync dst */
78 static uint64_t device_id;
79 static uint64_t device_cl;
80 static uint64_t device_type;
82 static uint32_t platform_key_handle;
84 static uint32_t hre_tpm_err;
85 static int hre_err = HRE_E_OK;
87 #define IS_PCR_HREG(spec) ((spec) & 0x20)
88 #define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
89 #define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
90 #define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
92 static const uint8_t vendor[] = "Guntermann & Drunck";
95 * @brief get the size of a given (TPM) NV area
96 * @param tpm TPM device
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(struct udevice *tpm, uint32_t index, uint32_t *size)
108 err = tpm_get_capability(tpm, 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 tpm TPM device
133 * @param auth usage auth of the key to search for
134 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
135 * @param[out] handle the handle of the key iff found
136 * @return 0 if key was found in TPM; != 0 if not.
138 static int find_key(struct udevice *tpm, const uint8_t auth[20],
139 const uint8_t pubkey_digest[20], uint32_t *handle)
142 uint32_t key_handles[10];
150 /* fetch list of already loaded keys in the TPM */
151 err = tpm_get_capability(tpm, TPM_CAP_HANDLE, TPM_RT_KEY, buf,
155 key_count = get_unaligned_be16(buf);
157 for (i = 0; i < key_count; ++i, ptr += 4)
158 key_handles[i] = get_unaligned_be32(ptr);
160 /* now search a(/ the) key which we can access with the given auth */
161 for (i = 0; i < key_count; ++i) {
162 buf_len = sizeof(buf);
163 err = tpm_get_pub_key_oiap(tpm, key_handles[i], auth, buf,
165 if (err && err != TPM_AUTHFAIL)
169 sha1_csum(buf, buf_len, digest);
170 if (!memcmp(digest, pubkey_digest, 20)) {
171 *handle = key_handles[i];
179 * @brief read CCDM common data from TPM NV
180 * @param tpm TPM device
181 * @return 0 if CCDM common data was found and read, !=0 if something failed.
183 static int read_common_data(struct udevice *tpm)
190 if (get_tpm_nv_size(tpm, NV_COMMON_DATA_INDEX, &size) ||
191 size < NV_COMMON_DATA_MIN_SIZE)
193 err = tpm_nv_read_value(tpm, NV_COMMON_DATA_INDEX,
194 buf, min(sizeof(buf), size));
196 printf("tpm_nv_read_value() failed: %u\n", err);
200 device_id = get_unaligned_be64(buf);
201 device_cl = get_unaligned_be64(buf + 8);
202 device_type = get_unaligned_be64(buf + 16);
205 sha1_update(&ctx, buf, 24);
206 sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
207 fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
209 platform_key_handle = get_unaligned_be32(buf + 24);
215 * @brief get pointer to hash register by specification
216 * @param spec specification of a hash register
217 * @return pointer to hash register or NULL if @a spec does not qualify a
218 * valid hash register; NULL else.
220 static struct h_reg *get_hreg(uint8_t spec)
224 idx = HREG_IDX(spec);
225 if (IS_FIX_HREG(spec)) {
226 if (idx < ARRAY_SIZE(fix_hregs))
227 return fix_hregs + idx;
228 hre_err = HRE_E_INVALID_HREG;
229 } else if (IS_PCR_HREG(spec)) {
230 if (idx < ARRAY_SIZE(pcr_hregs))
231 return pcr_hregs + idx;
232 hre_err = HRE_E_INVALID_HREG;
233 } else if (IS_VAR_HREG(spec)) {
234 if (idx < ARRAY_SIZE(var_hregs))
235 return var_hregs + idx;
236 hre_err = HRE_E_INVALID_HREG;
242 * @brief get pointer of a hash register by specification and usage.
243 * @param tpm TPM device
244 * @param spec specification of a hash register
245 * @param mode access mode (read or write or read/write)
246 * @return pointer to hash register if found and valid; NULL else.
248 * This func uses @a get_reg() to determine the hash register for a given spec.
249 * If a register is found it is validated according to the desired access mode.
250 * The value of automatic registers (PCR register and fixed registers) is
251 * loaded or computed on read access.
253 static struct h_reg *access_hreg(struct udevice *tpm, uint8_t spec,
254 enum access_mode mode)
256 struct h_reg *result;
258 result = get_hreg(spec);
262 if (mode & HREG_WR) {
263 if (IS_FIX_HREG(spec)) {
264 hre_err = HRE_E_INVALID_HREG;
268 if (mode & HREG_RD) {
269 if (!result->valid) {
270 if (IS_PCR_HREG(spec)) {
271 hre_tpm_err = tpm_pcr_read(tpm, HREG_IDX(spec),
273 result->valid = (hre_tpm_err == TPM_SUCCESS);
274 } else if (IS_FIX_HREG(spec)) {
275 switch (HREG_IDX(spec)) {
276 case FIX_HREG_DEVICE_ID_HASH:
277 read_common_data(tpm);
279 case FIX_HREG_VENDOR:
280 memcpy(result->digest, vendor, 20);
281 result->valid = true;
285 result->valid = true;
288 if (!result->valid) {
289 hre_err = HRE_E_INVALID_HREG;
297 static void *compute_and(void *_dst, const void *_src, size_t n)
300 const uint8_t *src = _src;
303 for (i = n; i-- > 0; )
309 static void *compute_or(void *_dst, const void *_src, size_t n)
312 const uint8_t *src = _src;
315 for (i = n; i-- > 0; )
321 static void *compute_xor(void *_dst, const void *_src, size_t n)
324 const uint8_t *src = _src;
327 for (i = n; i-- > 0; )
333 static void *compute_extend(void *_dst, const void *_src, size_t n)
339 sha1_update(&ctx, _dst, n);
340 sha1_update(&ctx, _src, n);
341 sha1_finish(&ctx, digest);
342 memcpy(_dst, digest, min(n, sizeof(digest)));
347 static int hre_op_loadkey(struct udevice *tpm, struct h_reg *src_reg,
348 struct h_reg *dst_reg, const void *key,
351 uint32_t parent_handle;
354 if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
356 if (find_key(tpm, src_reg->digest, dst_reg->digest, &parent_handle))
358 hre_tpm_err = tpm_load_key2_oiap(tpm, parent_handle, key, key_size,
359 src_reg->digest, &key_handle);
361 hre_err = HRE_E_TPM_FAILURE;
369 * @brief executes the next opcode on the hash register engine.
370 * @param tpm TPM device
371 * @param[in,out] ip pointer to the opcode (instruction pointer)
372 * @param[in,out] code_size (remaining) size of the code
373 * @return new instruction pointer on success, NULL on error.
375 static const uint8_t *hre_execute_op(struct udevice *tpm, const uint8_t **ip,
378 bool dst_modified = false;
384 struct h_reg *src_reg, *dst_reg;
386 const uint8_t *src_buf, *data;
389 void * (*bin_func)(void *, const void *, size_t);
394 ins = get_unaligned_be32(*ip);
397 src_spec = (ins >> 18) & 0x3f;
398 dst_spec = (ins >> 12) & 0x3f;
399 data_size = (ins & 0x7ff);
401 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
402 opcode, src_spec, dst_spec, data_size);
404 if ((opcode & 0x80) && (data_size + 4) > *code_size)
407 src_reg = access_hreg(tpm, src_spec, HREG_RD);
408 if (hre_err || hre_tpm_err)
410 dst_reg = access_hreg(tpm, dst_spec,
411 (opcode & 0x40) ? HREG_RDWR : HREG_WR);
412 if (hre_err || hre_tpm_err)
420 for (i = 0; i < 20; ++i) {
421 if (src_reg->digest[i])
430 bin_func = compute_xor;
433 bin_func = compute_and;
436 bin_func = compute_or;
439 bin_func = compute_extend;
444 src_buf = src_reg->digest;
449 } else if (data_size == 1) {
450 memset(buf, *data, 20);
452 } else if (data_size >= 20) {
456 for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
457 i -= data_size, ptr += data_size)
459 min_t(size_t, i, data_size));
462 bin_func(dst_reg->digest, src_buf, 20);
463 dst_reg->valid = true;
467 if (hre_op_loadkey(tpm, src_reg, dst_reg, data, data_size))
474 if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
475 hre_tpm_err = tpm_extend(tpm, HREG_IDX(dst_spec),
476 dst_reg->digest, dst_reg->digest);
478 hre_err = HRE_E_TPM_FAILURE;
487 *code_size -= data_size;
494 * @brief runs a program on the hash register engine.
495 * @param tpm TPM device
496 * @param code pointer to the (HRE) code.
497 * @param code_size size of the code (in bytes).
498 * @return 0 on success, != 0 on failure.
500 int hre_run_program(struct udevice *tpm, const uint8_t *code, size_t code_size)
503 const uint8_t *ip = code;
505 code_left = code_size;
508 while (code_left > 0)
509 if (!hre_execute_op(tpm, &ip, &code_left))
515 int hre_verify_program(struct key_program *prg)
519 crc = crc32(0, prg->code, prg->code_size);
521 if (crc != prg->code_crc) {
522 printf("HRC crc mismatch: %08x != %08x\n",