1 // SPDX-License-Identifier: GPL-2.0+
4 * Reinhard Pfau, Guntermann & Drunck GmbH, reinhard.pfau@gdsys.cc
14 #include <u-boot/crc.h>
15 #include <u-boot/sha1.h>
16 #include <asm/byteorder.h>
17 #include <asm/unaligned.h>
24 ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
25 ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
26 ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
27 ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
28 ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
43 /* register constants */
45 FIX_HREG_DEVICE_ID_HASH = 0,
52 static struct h_reg pcr_hregs[24];
53 static struct h_reg fix_hregs[COUNT_FIX_HREGS];
54 static struct h_reg var_hregs[8];
58 /* opcodes w/o data */
62 /* opcodes w/o data, w/ sync dst */
65 /* opcodes w/data, w/sync dst */
80 static uint64_t device_id;
81 static uint64_t device_cl;
82 static uint64_t device_type;
84 static uint32_t platform_key_handle;
86 static uint32_t hre_tpm_err;
87 static int hre_err = HRE_E_OK;
89 #define IS_PCR_HREG(spec) ((spec) & 0x20)
90 #define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
91 #define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
92 #define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
94 static const uint8_t vendor[] = "Guntermann & Drunck";
97 * @brief get the size of a given (TPM) NV area
98 * @param tpm TPM device
99 * @param index NV index of the area to get size for
100 * @param size pointer to the size
101 * @return 0 on success, != 0 on error
103 static int get_tpm_nv_size(struct udevice *tpm, uint32_t index, uint32_t *size)
110 err = tpm1_get_capability(tpm, TPM_CAP_NV_INDEX, index, info,
113 printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
118 /* skip tag and nvIndex */
120 /* skip 2 pcr info fields */
121 v16 = get_unaligned_be16(ptr);
122 ptr += 2 + v16 + 1 + 20;
123 v16 = get_unaligned_be16(ptr);
124 ptr += 2 + v16 + 1 + 20;
125 /* skip permission and flags */
128 *size = get_unaligned_be32(ptr);
133 * @brief search for a key by usage auth and pub key hash.
134 * @param tpm TPM device
135 * @param auth usage auth of the key to search for
136 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
137 * @param[out] handle the handle of the key iff found
138 * @return 0 if key was found in TPM; != 0 if not.
140 static int find_key(struct udevice *tpm, const uint8_t auth[20],
141 const uint8_t pubkey_digest[20], uint32_t *handle)
144 uint32_t key_handles[10];
152 /* fetch list of already loaded keys in the TPM */
153 err = tpm1_get_capability(tpm, TPM_CAP_HANDLE, TPM_RT_KEY, buf,
157 key_count = get_unaligned_be16(buf);
159 for (i = 0; i < key_count; ++i, ptr += 4)
160 key_handles[i] = get_unaligned_be32(ptr);
162 /* now search a(/ the) key which we can access with the given auth */
163 for (i = 0; i < key_count; ++i) {
164 buf_len = sizeof(buf);
165 err = tpm1_get_pub_key_oiap(tpm, key_handles[i], auth, buf,
167 if (err && err != TPM_AUTHFAIL)
171 sha1_csum(buf, buf_len, digest);
172 if (!memcmp(digest, pubkey_digest, 20)) {
173 *handle = key_handles[i];
181 * @brief read CCDM common data from TPM NV
182 * @param tpm TPM device
183 * @return 0 if CCDM common data was found and read, !=0 if something failed.
185 static int read_common_data(struct udevice *tpm)
192 if (get_tpm_nv_size(tpm, NV_COMMON_DATA_INDEX, &size) ||
193 size < NV_COMMON_DATA_MIN_SIZE)
195 err = tpm1_nv_read_value(tpm, NV_COMMON_DATA_INDEX, buf,
196 min(sizeof(buf), size));
198 printf("tpm_nv_read_value() failed: %u\n", err);
202 device_id = get_unaligned_be64(buf);
203 device_cl = get_unaligned_be64(buf + 8);
204 device_type = get_unaligned_be64(buf + 16);
207 sha1_update(&ctx, buf, 24);
208 sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
209 fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
211 platform_key_handle = get_unaligned_be32(buf + 24);
217 * @brief get pointer to hash register by specification
218 * @param spec specification of a hash register
219 * @return pointer to hash register or NULL if @a spec does not qualify a
220 * valid hash register; NULL else.
222 static struct h_reg *get_hreg(uint8_t spec)
226 idx = HREG_IDX(spec);
227 if (IS_FIX_HREG(spec)) {
228 if (idx < ARRAY_SIZE(fix_hregs))
229 return fix_hregs + idx;
230 hre_err = HRE_E_INVALID_HREG;
231 } else if (IS_PCR_HREG(spec)) {
232 if (idx < ARRAY_SIZE(pcr_hregs))
233 return pcr_hregs + idx;
234 hre_err = HRE_E_INVALID_HREG;
235 } else if (IS_VAR_HREG(spec)) {
236 if (idx < ARRAY_SIZE(var_hregs))
237 return var_hregs + idx;
238 hre_err = HRE_E_INVALID_HREG;
244 * @brief get pointer of a hash register by specification and usage.
245 * @param tpm TPM device
246 * @param spec specification of a hash register
247 * @param mode access mode (read or write or read/write)
248 * @return pointer to hash register if found and valid; NULL else.
250 * This func uses @a get_reg() to determine the hash register for a given spec.
251 * If a register is found it is validated according to the desired access mode.
252 * The value of automatic registers (PCR register and fixed registers) is
253 * loaded or computed on read access.
255 static struct h_reg *access_hreg(struct udevice *tpm, uint8_t spec,
256 enum access_mode mode)
258 struct h_reg *result;
260 result = get_hreg(spec);
264 if (mode & HREG_WR) {
265 if (IS_FIX_HREG(spec)) {
266 hre_err = HRE_E_INVALID_HREG;
270 if (mode & HREG_RD) {
271 if (!result->valid) {
272 if (IS_PCR_HREG(spec)) {
273 hre_tpm_err = tpm1_pcr_read(tpm, HREG_IDX(spec),
275 result->valid = (hre_tpm_err == TPM_SUCCESS);
276 } else if (IS_FIX_HREG(spec)) {
277 switch (HREG_IDX(spec)) {
278 case FIX_HREG_DEVICE_ID_HASH:
279 read_common_data(tpm);
281 case FIX_HREG_VENDOR:
282 memcpy(result->digest, vendor, 20);
283 result->valid = true;
287 result->valid = true;
290 if (!result->valid) {
291 hre_err = HRE_E_INVALID_HREG;
299 static void *compute_and(void *_dst, const void *_src, size_t n)
302 const uint8_t *src = _src;
305 for (i = n; i-- > 0; )
311 static void *compute_or(void *_dst, const void *_src, size_t n)
314 const uint8_t *src = _src;
317 for (i = n; i-- > 0; )
323 static void *compute_xor(void *_dst, const void *_src, size_t n)
326 const uint8_t *src = _src;
329 for (i = n; i-- > 0; )
335 static void *compute_extend(void *_dst, const void *_src, size_t n)
341 sha1_update(&ctx, _dst, n);
342 sha1_update(&ctx, _src, n);
343 sha1_finish(&ctx, digest);
344 memcpy(_dst, digest, min(n, sizeof(digest)));
349 static int hre_op_loadkey(struct udevice *tpm, struct h_reg *src_reg,
350 struct h_reg *dst_reg, const void *key,
353 uint32_t parent_handle;
356 if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
358 if (find_key(tpm, src_reg->digest, dst_reg->digest, &parent_handle))
360 hre_tpm_err = tpm1_load_key2_oiap(tpm, parent_handle, key, key_size,
361 src_reg->digest, &key_handle);
363 hre_err = HRE_E_TPM_FAILURE;
371 * @brief executes the next opcode on the hash register engine.
372 * @param tpm TPM device
373 * @param[in,out] ip pointer to the opcode (instruction pointer)
374 * @param[in,out] code_size (remaining) size of the code
375 * @return new instruction pointer on success, NULL on error.
377 static const uint8_t *hre_execute_op(struct udevice *tpm, const uint8_t **ip,
380 bool dst_modified = false;
386 struct h_reg *src_reg, *dst_reg;
388 const uint8_t *src_buf, *data;
391 void * (*bin_func)(void *, const void *, size_t);
396 ins = get_unaligned_be32(*ip);
399 src_spec = (ins >> 18) & 0x3f;
400 dst_spec = (ins >> 12) & 0x3f;
401 data_size = (ins & 0x7ff);
403 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
404 opcode, src_spec, dst_spec, data_size);
406 if ((opcode & 0x80) && (data_size + 4) > *code_size)
409 src_reg = access_hreg(tpm, src_spec, HREG_RD);
410 if (hre_err || hre_tpm_err)
412 dst_reg = access_hreg(tpm, dst_spec,
413 (opcode & 0x40) ? HREG_RDWR : HREG_WR);
414 if (hre_err || hre_tpm_err)
422 for (i = 0; i < 20; ++i) {
423 if (src_reg->digest[i])
432 bin_func = compute_xor;
435 bin_func = compute_and;
438 bin_func = compute_or;
441 bin_func = compute_extend;
446 src_buf = src_reg->digest;
451 } else if (data_size == 1) {
452 memset(buf, *data, 20);
454 } else if (data_size >= 20) {
458 for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
459 i -= data_size, ptr += data_size)
461 min_t(size_t, i, data_size));
464 bin_func(dst_reg->digest, src_buf, 20);
465 dst_reg->valid = true;
469 if (hre_op_loadkey(tpm, src_reg, dst_reg, data, data_size))
476 if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
477 hre_tpm_err = tpm1_extend(tpm, HREG_IDX(dst_spec),
478 dst_reg->digest, dst_reg->digest);
480 hre_err = HRE_E_TPM_FAILURE;
489 *code_size -= data_size;
496 * @brief runs a program on the hash register engine.
497 * @param tpm TPM device
498 * @param code pointer to the (HRE) code.
499 * @param code_size size of the code (in bytes).
500 * @return 0 on success, != 0 on failure.
502 int hre_run_program(struct udevice *tpm, const uint8_t *code, size_t code_size)
505 const uint8_t *ip = code;
507 code_left = code_size;
510 while (code_left > 0)
511 if (!hre_execute_op(tpm, &ip, &code_left))
517 int hre_verify_program(struct key_program *prg)
521 crc = crc32(0, prg->code, prg->code_size);
523 if (crc != prg->code_crc) {
524 printf("HRC crc mismatch: %08x != %08x\n",