hre.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2013
  4. * Reinhard Pfau, Guntermann & Drunck GmbH, reinhard.pfau@gdsys.cc
  5. */
  6. #include <common.h>
  7. #include <malloc.h>
  8. #include <fs.h>
  9. #include <i2c.h>
  10. #include <mmc.h>
  11. #include <tpm-v1.h>
  12. #include <u-boot/sha1.h>
  13. #include <asm/byteorder.h>
  14. #include <asm/unaligned.h>
  15. #include <pca9698.h>
  16. #include "hre.h"
  17. /* other constants */
  18. enum {
  19. ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
  20. ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
  21. ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
  22. ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
  23. ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
  24. };
  25. enum {
  26. I2C_SOC_0 = 0,
  27. I2C_SOC_1 = 1,
  28. };
  29. enum access_mode {
  30. HREG_NONE = 0,
  31. HREG_RD = 1,
  32. HREG_WR = 2,
  33. HREG_RDWR = 3,
  34. };
  35. /* register constants */
  36. enum {
  37. FIX_HREG_DEVICE_ID_HASH = 0,
  38. FIX_HREG_UNUSED1 = 1,
  39. FIX_HREG_UNUSED2 = 2,
  40. FIX_HREG_VENDOR = 3,
  41. COUNT_FIX_HREGS
  42. };
  43. static struct h_reg pcr_hregs[24];
  44. static struct h_reg fix_hregs[COUNT_FIX_HREGS];
  45. static struct h_reg var_hregs[8];
  46. /* hre opcodes */
  47. enum {
  48. /* opcodes w/o data */
  49. HRE_NOP = 0x00,
  50. HRE_SYNC = HRE_NOP,
  51. HRE_CHECK0 = 0x01,
  52. /* opcodes w/o data, w/ sync dst */
  53. /* opcodes w/ data */
  54. HRE_LOAD = 0x81,
  55. /* opcodes w/data, w/sync dst */
  56. HRE_XOR = 0xC1,
  57. HRE_AND = 0xC2,
  58. HRE_OR = 0xC3,
  59. HRE_EXTEND = 0xC4,
  60. HRE_LOADKEY = 0xC5,
  61. };
  62. /* hre errors */
  63. enum {
  64. HRE_E_OK = 0,
  65. HRE_E_TPM_FAILURE,
  66. HRE_E_INVALID_HREG,
  67. };
  68. static uint64_t device_id;
  69. static uint64_t device_cl;
  70. static uint64_t device_type;
  71. static uint32_t platform_key_handle;
  72. static uint32_t hre_tpm_err;
  73. static int hre_err = HRE_E_OK;
  74. #define IS_PCR_HREG(spec) ((spec) & 0x20)
  75. #define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
  76. #define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
  77. #define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
  78. static const uint8_t vendor[] = "Guntermann & Drunck";
  79. /**
  80. * @brief get the size of a given (TPM) NV area
  81. * @param index NV index of the area to get size for
  82. * @param size pointer to the size
  83. * @return 0 on success, != 0 on error
  84. */
  85. static int get_tpm_nv_size(uint32_t index, uint32_t *size)
  86. {
  87. uint32_t err;
  88. uint8_t info[72];
  89. uint8_t *ptr;
  90. uint16_t v16;
  91. err = tpm_get_capability(TPM_CAP_NV_INDEX, index,
  92. info, sizeof(info));
  93. if (err) {
  94. printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
  95. index, err);
  96. return 1;
  97. }
  98. /* skip tag and nvIndex */
  99. ptr = info + 6;
  100. /* skip 2 pcr info fields */
  101. v16 = get_unaligned_be16(ptr);
  102. ptr += 2 + v16 + 1 + 20;
  103. v16 = get_unaligned_be16(ptr);
  104. ptr += 2 + v16 + 1 + 20;
  105. /* skip permission and flags */
  106. ptr += 6 + 3;
  107. *size = get_unaligned_be32(ptr);
  108. return 0;
  109. }
  110. /**
  111. * @brief search for a key by usage auth and pub key hash.
  112. * @param auth usage auth of the key to search for
  113. * @param pubkey_digest (SHA1) hash of the pub key structure of the key
  114. * @param[out] handle the handle of the key iff found
  115. * @return 0 if key was found in TPM; != 0 if not.
  116. */
  117. static int find_key(const uint8_t auth[20], const uint8_t pubkey_digest[20],
  118. uint32_t *handle)
  119. {
  120. uint16_t key_count;
  121. uint32_t key_handles[10];
  122. uint8_t buf[288];
  123. uint8_t *ptr;
  124. uint32_t err;
  125. uint8_t digest[20];
  126. size_t buf_len;
  127. unsigned int i;
  128. /* fetch list of already loaded keys in the TPM */
  129. err = tpm_get_capability(TPM_CAP_HANDLE, TPM_RT_KEY, buf, sizeof(buf));
  130. if (err)
  131. return -1;
  132. key_count = get_unaligned_be16(buf);
  133. ptr = buf + 2;
  134. for (i = 0; i < key_count; ++i, ptr += 4)
  135. key_handles[i] = get_unaligned_be32(ptr);
  136. /* now search a(/ the) key which we can access with the given auth */
  137. for (i = 0; i < key_count; ++i) {
  138. buf_len = sizeof(buf);
  139. err = tpm_get_pub_key_oiap(key_handles[i], auth, buf, &buf_len);
  140. if (err && err != TPM_AUTHFAIL)
  141. return -1;
  142. if (err)
  143. continue;
  144. sha1_csum(buf, buf_len, digest);
  145. if (!memcmp(digest, pubkey_digest, 20)) {
  146. *handle = key_handles[i];
  147. return 0;
  148. }
  149. }
  150. return 1;
  151. }
  152. /**
  153. * @brief read CCDM common data from TPM NV
  154. * @return 0 if CCDM common data was found and read, !=0 if something failed.
  155. */
  156. static int read_common_data(void)
  157. {
  158. uint32_t size = 0;
  159. uint32_t err;
  160. uint8_t buf[256];
  161. sha1_context ctx;
  162. if (get_tpm_nv_size(NV_COMMON_DATA_INDEX, &size) ||
  163. size < NV_COMMON_DATA_MIN_SIZE)
  164. return 1;
  165. err = tpm_nv_read_value(NV_COMMON_DATA_INDEX,
  166. buf, min(sizeof(buf), size));
  167. if (err) {
  168. printf("tpm_nv_read_value() failed: %u\n", err);
  169. return 1;
  170. }
  171. device_id = get_unaligned_be64(buf);
  172. device_cl = get_unaligned_be64(buf + 8);
  173. device_type = get_unaligned_be64(buf + 16);
  174. sha1_starts(&ctx);
  175. sha1_update(&ctx, buf, 24);
  176. sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
  177. fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
  178. platform_key_handle = get_unaligned_be32(buf + 24);
  179. return 0;
  180. }
  181. /**
  182. * @brief get pointer to hash register by specification
  183. * @param spec specification of a hash register
  184. * @return pointer to hash register or NULL if @a spec does not qualify a
  185. * valid hash register; NULL else.
  186. */
  187. static struct h_reg *get_hreg(uint8_t spec)
  188. {
  189. uint8_t idx;
  190. idx = HREG_IDX(spec);
  191. if (IS_FIX_HREG(spec)) {
  192. if (idx < ARRAY_SIZE(fix_hregs))
  193. return fix_hregs + idx;
  194. hre_err = HRE_E_INVALID_HREG;
  195. } else if (IS_PCR_HREG(spec)) {
  196. if (idx < ARRAY_SIZE(pcr_hregs))
  197. return pcr_hregs + idx;
  198. hre_err = HRE_E_INVALID_HREG;
  199. } else if (IS_VAR_HREG(spec)) {
  200. if (idx < ARRAY_SIZE(var_hregs))
  201. return var_hregs + idx;
  202. hre_err = HRE_E_INVALID_HREG;
  203. }
  204. return NULL;
  205. }
  206. /**
  207. * @brief get pointer of a hash register by specification and usage.
  208. * @param spec specification of a hash register
  209. * @param mode access mode (read or write or read/write)
  210. * @return pointer to hash register if found and valid; NULL else.
  211. *
  212. * This func uses @a get_reg() to determine the hash register for a given spec.
  213. * If a register is found it is validated according to the desired access mode.
  214. * The value of automatic registers (PCR register and fixed registers) is
  215. * loaded or computed on read access.
  216. */
  217. static struct h_reg *access_hreg(uint8_t spec, enum access_mode mode)
  218. {
  219. struct h_reg *result;
  220. result = get_hreg(spec);
  221. if (!result)
  222. return NULL;
  223. if (mode & HREG_WR) {
  224. if (IS_FIX_HREG(spec)) {
  225. hre_err = HRE_E_INVALID_HREG;
  226. return NULL;
  227. }
  228. }
  229. if (mode & HREG_RD) {
  230. if (!result->valid) {
  231. if (IS_PCR_HREG(spec)) {
  232. hre_tpm_err = tpm_pcr_read(HREG_IDX(spec),
  233. result->digest, 20);
  234. result->valid = (hre_tpm_err == TPM_SUCCESS);
  235. } else if (IS_FIX_HREG(spec)) {
  236. switch (HREG_IDX(spec)) {
  237. case FIX_HREG_DEVICE_ID_HASH:
  238. read_common_data();
  239. break;
  240. case FIX_HREG_VENDOR:
  241. memcpy(result->digest, vendor, 20);
  242. result->valid = true;
  243. break;
  244. }
  245. } else {
  246. result->valid = true;
  247. }
  248. }
  249. if (!result->valid) {
  250. hre_err = HRE_E_INVALID_HREG;
  251. return NULL;
  252. }
  253. }
  254. return result;
  255. }
  256. static void *compute_and(void *_dst, const void *_src, size_t n)
  257. {
  258. uint8_t *dst = _dst;
  259. const uint8_t *src = _src;
  260. size_t i;
  261. for (i = n; i-- > 0; )
  262. *dst++ &= *src++;
  263. return _dst;
  264. }
  265. static void *compute_or(void *_dst, const void *_src, size_t n)
  266. {
  267. uint8_t *dst = _dst;
  268. const uint8_t *src = _src;
  269. size_t i;
  270. for (i = n; i-- > 0; )
  271. *dst++ |= *src++;
  272. return _dst;
  273. }
  274. static void *compute_xor(void *_dst, const void *_src, size_t n)
  275. {
  276. uint8_t *dst = _dst;
  277. const uint8_t *src = _src;
  278. size_t i;
  279. for (i = n; i-- > 0; )
  280. *dst++ ^= *src++;
  281. return _dst;
  282. }
  283. static void *compute_extend(void *_dst, const void *_src, size_t n)
  284. {
  285. uint8_t digest[20];
  286. sha1_context ctx;
  287. sha1_starts(&ctx);
  288. sha1_update(&ctx, _dst, n);
  289. sha1_update(&ctx, _src, n);
  290. sha1_finish(&ctx, digest);
  291. memcpy(_dst, digest, min(n, sizeof(digest)));
  292. return _dst;
  293. }
  294. static int hre_op_loadkey(struct h_reg *src_reg, struct h_reg *dst_reg,
  295. const void *key, size_t key_size)
  296. {
  297. uint32_t parent_handle;
  298. uint32_t key_handle;
  299. if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
  300. return -1;
  301. if (find_key(src_reg->digest, dst_reg->digest, &parent_handle))
  302. return -1;
  303. hre_tpm_err = tpm_load_key2_oiap(parent_handle, key, key_size,
  304. src_reg->digest, &key_handle);
  305. if (hre_tpm_err) {
  306. hre_err = HRE_E_TPM_FAILURE;
  307. return -1;
  308. }
  309. return 0;
  310. }
  311. /**
  312. * @brief executes the next opcode on the hash register engine.
  313. * @param[in,out] ip pointer to the opcode (instruction pointer)
  314. * @param[in,out] code_size (remaining) size of the code
  315. * @return new instruction pointer on success, NULL on error.
  316. */
  317. static const uint8_t *hre_execute_op(const uint8_t **ip, size_t *code_size)
  318. {
  319. bool dst_modified = false;
  320. uint32_t ins;
  321. uint8_t opcode;
  322. uint8_t src_spec;
  323. uint8_t dst_spec;
  324. uint16_t data_size;
  325. struct h_reg *src_reg, *dst_reg;
  326. uint8_t buf[20];
  327. const uint8_t *src_buf, *data;
  328. uint8_t *ptr;
  329. int i;
  330. void * (*bin_func)(void *, const void *, size_t);
  331. if (*code_size < 4)
  332. return NULL;
  333. ins = get_unaligned_be32(*ip);
  334. opcode = **ip;
  335. data = *ip + 4;
  336. src_spec = (ins >> 18) & 0x3f;
  337. dst_spec = (ins >> 12) & 0x3f;
  338. data_size = (ins & 0x7ff);
  339. debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
  340. opcode, src_spec, dst_spec, data_size);
  341. if ((opcode & 0x80) && (data_size + 4) > *code_size)
  342. return NULL;
  343. src_reg = access_hreg(src_spec, HREG_RD);
  344. if (hre_err || hre_tpm_err)
  345. return NULL;
  346. dst_reg = access_hreg(dst_spec, (opcode & 0x40) ? HREG_RDWR : HREG_WR);
  347. if (hre_err || hre_tpm_err)
  348. return NULL;
  349. switch (opcode) {
  350. case HRE_NOP:
  351. goto end;
  352. case HRE_CHECK0:
  353. if (src_reg) {
  354. for (i = 0; i < 20; ++i) {
  355. if (src_reg->digest[i])
  356. return NULL;
  357. }
  358. }
  359. break;
  360. case HRE_LOAD:
  361. bin_func = memcpy;
  362. goto do_bin_func;
  363. case HRE_XOR:
  364. bin_func = compute_xor;
  365. goto do_bin_func;
  366. case HRE_AND:
  367. bin_func = compute_and;
  368. goto do_bin_func;
  369. case HRE_OR:
  370. bin_func = compute_or;
  371. goto do_bin_func;
  372. case HRE_EXTEND:
  373. bin_func = compute_extend;
  374. do_bin_func:
  375. if (!dst_reg)
  376. return NULL;
  377. if (src_reg) {
  378. src_buf = src_reg->digest;
  379. } else {
  380. if (!data_size) {
  381. memset(buf, 0, 20);
  382. src_buf = buf;
  383. } else if (data_size == 1) {
  384. memset(buf, *data, 20);
  385. src_buf = buf;
  386. } else if (data_size >= 20) {
  387. src_buf = data;
  388. } else {
  389. src_buf = buf;
  390. for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
  391. i -= data_size, ptr += data_size)
  392. memcpy(ptr, data,
  393. min_t(size_t, i, data_size));
  394. }
  395. }
  396. bin_func(dst_reg->digest, src_buf, 20);
  397. dst_reg->valid = true;
  398. dst_modified = true;
  399. break;
  400. case HRE_LOADKEY:
  401. if (hre_op_loadkey(src_reg, dst_reg, data, data_size))
  402. return NULL;
  403. break;
  404. default:
  405. return NULL;
  406. }
  407. if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
  408. hre_tpm_err = tpm_extend(HREG_IDX(dst_spec), dst_reg->digest,
  409. dst_reg->digest);
  410. if (hre_tpm_err) {
  411. hre_err = HRE_E_TPM_FAILURE;
  412. return NULL;
  413. }
  414. }
  415. end:
  416. *ip += 4;
  417. *code_size -= 4;
  418. if (opcode & 0x80) {
  419. *ip += data_size;
  420. *code_size -= data_size;
  421. }
  422. return *ip;
  423. }
  424. /**
  425. * @brief runs a program on the hash register engine.
  426. * @param code pointer to the (HRE) code.
  427. * @param code_size size of the code (in bytes).
  428. * @return 0 on success, != 0 on failure.
  429. */
  430. int hre_run_program(const uint8_t *code, size_t code_size)
  431. {
  432. size_t code_left;
  433. const uint8_t *ip = code;
  434. code_left = code_size;
  435. hre_tpm_err = 0;
  436. hre_err = HRE_E_OK;
  437. while (code_left > 0)
  438. if (!hre_execute_op(&ip, &code_left))
  439. return -1;
  440. return hre_err;
  441. }
  442. int hre_verify_program(struct key_program *prg)
  443. {
  444. uint32_t crc;
  445. crc = crc32(0, prg->code, prg->code_size);
  446. if (crc != prg->code_crc) {
  447. printf("HRC crc mismatch: %08x != %08x\n",
  448. crc, prg->code_crc);
  449. return 1;
  450. }
  451. return 0;
  452. }