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 index NV index of the area to get size for
97 * @param size pointer to the size
98 * @return 0 on success, != 0 on error
100 static int get_tpm_nv_size(uint32_t index, uint32_t *size)
107 err = tpm_get_capability(TPM_CAP_NV_INDEX, index,
110 printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
115 /* skip tag and nvIndex */
117 /* skip 2 pcr info fields */
118 v16 = get_unaligned_be16(ptr);
119 ptr += 2 + v16 + 1 + 20;
120 v16 = get_unaligned_be16(ptr);
121 ptr += 2 + v16 + 1 + 20;
122 /* skip permission and flags */
125 *size = get_unaligned_be32(ptr);
130 * @brief search for a key by usage auth and pub key hash.
131 * @param auth usage auth of the key to search for
132 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
133 * @param[out] handle the handle of the key iff found
134 * @return 0 if key was found in TPM; != 0 if not.
136 static int find_key(const uint8_t auth[20], const uint8_t pubkey_digest[20],
140 uint32_t key_handles[10];
148 /* fetch list of already loaded keys in the TPM */
149 err = tpm_get_capability(TPM_CAP_HANDLE, TPM_RT_KEY, buf, sizeof(buf));
152 key_count = get_unaligned_be16(buf);
154 for (i = 0; i < key_count; ++i, ptr += 4)
155 key_handles[i] = get_unaligned_be32(ptr);
157 /* now search a(/ the) key which we can access with the given auth */
158 for (i = 0; i < key_count; ++i) {
159 buf_len = sizeof(buf);
160 err = tpm_get_pub_key_oiap(key_handles[i], auth, buf, &buf_len);
161 if (err && err != TPM_AUTHFAIL)
165 sha1_csum(buf, buf_len, digest);
166 if (!memcmp(digest, pubkey_digest, 20)) {
167 *handle = key_handles[i];
175 * @brief read CCDM common data from TPM NV
176 * @return 0 if CCDM common data was found and read, !=0 if something failed.
178 static int read_common_data(void)
185 if (get_tpm_nv_size(NV_COMMON_DATA_INDEX, &size) ||
186 size < NV_COMMON_DATA_MIN_SIZE)
188 err = tpm_nv_read_value(NV_COMMON_DATA_INDEX,
189 buf, min(sizeof(buf), size));
191 printf("tpm_nv_read_value() failed: %u\n", err);
195 device_id = get_unaligned_be64(buf);
196 device_cl = get_unaligned_be64(buf + 8);
197 device_type = get_unaligned_be64(buf + 16);
200 sha1_update(&ctx, buf, 24);
201 sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
202 fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
204 platform_key_handle = get_unaligned_be32(buf + 24);
210 * @brief get pointer to hash register by specification
211 * @param spec specification of a hash register
212 * @return pointer to hash register or NULL if @a spec does not qualify a
213 * valid hash register; NULL else.
215 static struct h_reg *get_hreg(uint8_t spec)
219 idx = HREG_IDX(spec);
220 if (IS_FIX_HREG(spec)) {
221 if (idx < ARRAY_SIZE(fix_hregs))
222 return fix_hregs + idx;
223 hre_err = HRE_E_INVALID_HREG;
224 } else if (IS_PCR_HREG(spec)) {
225 if (idx < ARRAY_SIZE(pcr_hregs))
226 return pcr_hregs + idx;
227 hre_err = HRE_E_INVALID_HREG;
228 } else if (IS_VAR_HREG(spec)) {
229 if (idx < ARRAY_SIZE(var_hregs))
230 return var_hregs + idx;
231 hre_err = HRE_E_INVALID_HREG;
237 * @brief get pointer of a hash register by specification and usage.
238 * @param spec specification of a hash register
239 * @param mode access mode (read or write or read/write)
240 * @return pointer to hash register if found and valid; NULL else.
242 * This func uses @a get_reg() to determine the hash register for a given spec.
243 * If a register is found it is validated according to the desired access mode.
244 * The value of automatic registers (PCR register and fixed registers) is
245 * loaded or computed on read access.
247 static struct h_reg *access_hreg(uint8_t spec, enum access_mode mode)
249 struct h_reg *result;
251 result = get_hreg(spec);
255 if (mode & HREG_WR) {
256 if (IS_FIX_HREG(spec)) {
257 hre_err = HRE_E_INVALID_HREG;
261 if (mode & HREG_RD) {
262 if (!result->valid) {
263 if (IS_PCR_HREG(spec)) {
264 hre_tpm_err = tpm_pcr_read(HREG_IDX(spec),
266 result->valid = (hre_tpm_err == TPM_SUCCESS);
267 } else if (IS_FIX_HREG(spec)) {
268 switch (HREG_IDX(spec)) {
269 case FIX_HREG_DEVICE_ID_HASH:
272 case FIX_HREG_VENDOR:
273 memcpy(result->digest, vendor, 20);
274 result->valid = true;
278 result->valid = true;
281 if (!result->valid) {
282 hre_err = HRE_E_INVALID_HREG;
290 static void *compute_and(void *_dst, const void *_src, size_t n)
293 const uint8_t *src = _src;
296 for (i = n; i-- > 0; )
302 static void *compute_or(void *_dst, const void *_src, size_t n)
305 const uint8_t *src = _src;
308 for (i = n; i-- > 0; )
314 static void *compute_xor(void *_dst, const void *_src, size_t n)
317 const uint8_t *src = _src;
320 for (i = n; i-- > 0; )
326 static void *compute_extend(void *_dst, const void *_src, size_t n)
332 sha1_update(&ctx, _dst, n);
333 sha1_update(&ctx, _src, n);
334 sha1_finish(&ctx, digest);
335 memcpy(_dst, digest, min(n, sizeof(digest)));
340 static int hre_op_loadkey(struct h_reg *src_reg, struct h_reg *dst_reg,
341 const void *key, size_t key_size)
343 uint32_t parent_handle;
346 if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
348 if (find_key(src_reg->digest, dst_reg->digest, &parent_handle))
350 hre_tpm_err = tpm_load_key2_oiap(parent_handle, key, key_size,
351 src_reg->digest, &key_handle);
353 hre_err = HRE_E_TPM_FAILURE;
361 * @brief executes the next opcode on the hash register engine.
362 * @param[in,out] ip pointer to the opcode (instruction pointer)
363 * @param[in,out] code_size (remaining) size of the code
364 * @return new instruction pointer on success, NULL on error.
366 static const uint8_t *hre_execute_op(const uint8_t **ip, size_t *code_size)
368 bool dst_modified = false;
374 struct h_reg *src_reg, *dst_reg;
376 const uint8_t *src_buf, *data;
379 void * (*bin_func)(void *, const void *, size_t);
384 ins = get_unaligned_be32(*ip);
387 src_spec = (ins >> 18) & 0x3f;
388 dst_spec = (ins >> 12) & 0x3f;
389 data_size = (ins & 0x7ff);
391 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
392 opcode, src_spec, dst_spec, data_size);
394 if ((opcode & 0x80) && (data_size + 4) > *code_size)
397 src_reg = access_hreg(src_spec, HREG_RD);
398 if (hre_err || hre_tpm_err)
400 dst_reg = access_hreg(dst_spec, (opcode & 0x40) ? HREG_RDWR : HREG_WR);
401 if (hre_err || hre_tpm_err)
409 for (i = 0; i < 20; ++i) {
410 if (src_reg->digest[i])
419 bin_func = compute_xor;
422 bin_func = compute_and;
425 bin_func = compute_or;
428 bin_func = compute_extend;
433 src_buf = src_reg->digest;
438 } else if (data_size == 1) {
439 memset(buf, *data, 20);
441 } else if (data_size >= 20) {
445 for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
446 i -= data_size, ptr += data_size)
448 min_t(size_t, i, data_size));
451 bin_func(dst_reg->digest, src_buf, 20);
452 dst_reg->valid = true;
456 if (hre_op_loadkey(src_reg, dst_reg, data, data_size))
463 if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
464 hre_tpm_err = tpm_extend(HREG_IDX(dst_spec), dst_reg->digest,
467 hre_err = HRE_E_TPM_FAILURE;
476 *code_size -= data_size;
483 * @brief runs a program on the hash register engine.
484 * @param code pointer to the (HRE) code.
485 * @param code_size size of the code (in bytes).
486 * @return 0 on success, != 0 on failure.
488 int hre_run_program(const uint8_t *code, size_t code_size)
491 const uint8_t *ip = code;
493 code_left = code_size;
496 while (code_left > 0)
497 if (!hre_execute_op(&ip, &code_left))
503 int hre_verify_program(struct key_program *prg)
507 crc = crc32(0, prg->code, prg->code_size);
509 if (crc != prg->code_crc) {
510 printf("HRC crc mismatch: %08x != %08x\n",