tpm2-sessions.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 James.Bottomley@HansenPartnership.com
  4. *
  5. * Cryptographic helper routines for handling TPM2 sessions for
  6. * authorization HMAC and request response encryption.
  7. *
  8. * The idea is to ensure that every TPM command is HMAC protected by a
  9. * session, meaning in-flight tampering would be detected and in
  10. * addition all sensitive inputs and responses should be encrypted.
  11. *
  12. * The basic way this works is to use a TPM feature called salted
  13. * sessions where a random secret used in session construction is
  14. * encrypted to the public part of a known TPM key. The problem is we
  15. * have no known keys, so initially a primary Elliptic Curve key is
  16. * derived from the NULL seed (we use EC because most TPMs generate
  17. * these keys much faster than RSA ones). The curve used is NIST_P256
  18. * because that's now mandated to be present in 'TCG TPM v2.0
  19. * Provisioning Guidance'
  20. *
  21. * Threat problems: the initial TPM2_CreatePrimary is not (and cannot
  22. * be) session protected, so a clever Man in the Middle could return a
  23. * public key they control to this command and from there intercept
  24. * and decode all subsequent session based transactions. The kernel
  25. * cannot mitigate this threat but, after boot, userspace can get
  26. * proof this has not happened by asking the TPM to certify the NULL
  27. * key. This certification would chain back to the TPM Endorsement
  28. * Certificate and prove the NULL seed primary had not been tampered
  29. * with and thus all sessions must have been cryptographically secure.
  30. * To assist with this, the initial NULL seed public key name is made
  31. * available in a sysfs file.
  32. *
  33. * Use of these functions:
  34. *
  35. * The design is all the crypto, hash and hmac gunk is confined in this
  36. * file and never needs to be seen even by the kernel internal user. To
  37. * the user there's an init function tpm2_sessions_init() that needs to
  38. * be called once per TPM which generates the NULL seed primary key.
  39. *
  40. * These are the usage functions:
  41. *
  42. * tpm2_end_auth_session() kills the session and frees the resources.
  43. * Under normal operation this function is done by
  44. * tpm_buf_check_hmac_response(), so this is only to be used on
  45. * error legs where the latter is not executed.
  46. * tpm_buf_append_name() to add a handle to the buffer. This must be
  47. * used in place of the usual tpm_buf_append_u32() for adding
  48. * handles because handles have to be processed specially when
  49. * calculating the HMAC. In particular, for NV, volatile and
  50. * permanent objects you now need to provide the name.
  51. * tpm_buf_append_hmac_session() which appends the hmac session to the
  52. * buf in the same way tpm_buf_append_auth does().
  53. * tpm_buf_fill_hmac_session() This calculates the correct hash and
  54. * places it in the buffer. It must be called after the complete
  55. * command buffer is finalized so it can fill in the correct HMAC
  56. * based on the parameters.
  57. * tpm_buf_check_hmac_response() which checks the session response in
  58. * the buffer and calculates what it should be. If there's a
  59. * mismatch it will log a warning and return an error. If
  60. * tpm_buf_append_hmac_session() did not specify
  61. * TPM_SA_CONTINUE_SESSION then the session will be closed (if it
  62. * hasn't been consumed) and the auth structure freed.
  63. */
  64. #include "tpm.h"
  65. #include <linux/random.h>
  66. #include <linux/scatterlist.h>
  67. #include <linux/unaligned.h>
  68. #include <crypto/kpp.h>
  69. #include <crypto/ecdh.h>
  70. #include <crypto/hash.h>
  71. #include <crypto/hmac.h>
  72. /* maximum number of names the TPM must remember for authorization */
  73. #define AUTH_MAX_NAMES 3
  74. #define AES_KEY_BYTES AES_KEYSIZE_128
  75. #define AES_KEY_BITS (AES_KEY_BYTES*8)
  76. /*
  77. * This is the structure that carries all the auth information (like
  78. * session handle, nonces, session key and auth) from use to use it is
  79. * designed to be opaque to anything outside.
  80. */
  81. struct tpm2_auth {
  82. u32 handle;
  83. /*
  84. * This has two meanings: before tpm_buf_fill_hmac_session()
  85. * it marks the offset in the buffer of the start of the
  86. * sessions (i.e. after all the handles). Once the buffer has
  87. * been filled it markes the session number of our auth
  88. * session so we can find it again in the response buffer.
  89. *
  90. * The two cases are distinguished because the first offset
  91. * must always be greater than TPM_HEADER_SIZE and the second
  92. * must be less than or equal to 5.
  93. */
  94. u32 session;
  95. /*
  96. * the size here is variable and set by the size of our_nonce
  97. * which must be between 16 and the name hash length. we set
  98. * the maximum sha256 size for the greatest protection
  99. */
  100. u8 our_nonce[SHA256_DIGEST_SIZE];
  101. u8 tpm_nonce[SHA256_DIGEST_SIZE];
  102. /*
  103. * the salt is only used across the session command/response
  104. * after that it can be used as a scratch area
  105. */
  106. union {
  107. u8 salt[EC_PT_SZ];
  108. /* scratch for key + IV */
  109. u8 scratch[AES_KEY_BYTES + AES_BLOCK_SIZE];
  110. };
  111. /*
  112. * the session key and passphrase are the same size as the
  113. * name digest (sha256 again). The session key is constant
  114. * for the use of the session and the passphrase can change
  115. * with every invocation.
  116. *
  117. * Note: these fields must be adjacent and in this order
  118. * because several HMAC/KDF schemes use the combination of the
  119. * session_key and passphrase.
  120. */
  121. u8 session_key[SHA256_DIGEST_SIZE];
  122. u8 passphrase[SHA256_DIGEST_SIZE];
  123. int passphrase_len;
  124. struct crypto_aes_ctx aes_ctx;
  125. /* saved session attributes: */
  126. u8 attrs;
  127. __be32 ordinal;
  128. /*
  129. * memory for three authorization handles. We know them by
  130. * handle, but they are part of the session by name, which
  131. * we must compute and remember
  132. */
  133. u32 name_h[AUTH_MAX_NAMES];
  134. u8 name[AUTH_MAX_NAMES][2 + SHA512_DIGEST_SIZE];
  135. };
  136. #ifdef CONFIG_TCG_TPM2_HMAC
  137. /*
  138. * Name Size based on TPM algorithm (assumes no hash bigger than 255)
  139. */
  140. static u8 name_size(const u8 *name)
  141. {
  142. static u8 size_map[] = {
  143. [TPM_ALG_SHA1] = SHA1_DIGEST_SIZE,
  144. [TPM_ALG_SHA256] = SHA256_DIGEST_SIZE,
  145. [TPM_ALG_SHA384] = SHA384_DIGEST_SIZE,
  146. [TPM_ALG_SHA512] = SHA512_DIGEST_SIZE,
  147. };
  148. u16 alg = get_unaligned_be16(name);
  149. return size_map[alg] + 2;
  150. }
  151. static int tpm2_parse_read_public(char *name, struct tpm_buf *buf)
  152. {
  153. struct tpm_header *head = (struct tpm_header *)buf->data;
  154. off_t offset = TPM_HEADER_SIZE;
  155. u32 tot_len = be32_to_cpu(head->length);
  156. u32 val;
  157. /* we're starting after the header so adjust the length */
  158. tot_len -= TPM_HEADER_SIZE;
  159. /* skip public */
  160. val = tpm_buf_read_u16(buf, &offset);
  161. if (val > tot_len)
  162. return -EINVAL;
  163. offset += val;
  164. /* name */
  165. val = tpm_buf_read_u16(buf, &offset);
  166. if (val != name_size(&buf->data[offset]))
  167. return -EINVAL;
  168. memcpy(name, &buf->data[offset], val);
  169. /* forget the rest */
  170. return 0;
  171. }
  172. static int tpm2_read_public(struct tpm_chip *chip, u32 handle, char *name)
  173. {
  174. struct tpm_buf buf;
  175. int rc;
  176. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_READ_PUBLIC);
  177. if (rc)
  178. return rc;
  179. tpm_buf_append_u32(&buf, handle);
  180. rc = tpm_transmit_cmd(chip, &buf, 0, "read public");
  181. if (rc == TPM2_RC_SUCCESS)
  182. rc = tpm2_parse_read_public(name, &buf);
  183. tpm_buf_destroy(&buf);
  184. return rc;
  185. }
  186. #endif /* CONFIG_TCG_TPM2_HMAC */
  187. /**
  188. * tpm_buf_append_name() - add a handle area to the buffer
  189. * @chip: the TPM chip structure
  190. * @buf: The buffer to be appended
  191. * @handle: The handle to be appended
  192. * @name: The name of the handle (may be NULL)
  193. *
  194. * In order to compute session HMACs, we need to know the names of the
  195. * objects pointed to by the handles. For most objects, this is simply
  196. * the actual 4 byte handle or an empty buf (in these cases @name
  197. * should be NULL) but for volatile objects, permanent objects and NV
  198. * areas, the name is defined as the hash (according to the name
  199. * algorithm which should be set to sha256) of the public area to
  200. * which the two byte algorithm id has been appended. For these
  201. * objects, the @name pointer should point to this. If a name is
  202. * required but @name is NULL, then TPM2_ReadPublic() will be called
  203. * on the handle to obtain the name.
  204. *
  205. * As with most tpm_buf operations, success is assumed because failure
  206. * will be caused by an incorrect programming model and indicated by a
  207. * kernel message.
  208. */
  209. void tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
  210. u32 handle, u8 *name)
  211. {
  212. #ifdef CONFIG_TCG_TPM2_HMAC
  213. enum tpm2_mso_type mso = tpm2_handle_mso(handle);
  214. struct tpm2_auth *auth;
  215. int slot;
  216. #endif
  217. if (!tpm2_chip_auth(chip)) {
  218. tpm_buf_append_handle(chip, buf, handle);
  219. return;
  220. }
  221. #ifdef CONFIG_TCG_TPM2_HMAC
  222. slot = (tpm_buf_length(buf) - TPM_HEADER_SIZE) / 4;
  223. if (slot >= AUTH_MAX_NAMES) {
  224. dev_err(&chip->dev, "TPM: too many handles\n");
  225. return;
  226. }
  227. auth = chip->auth;
  228. WARN(auth->session != tpm_buf_length(buf),
  229. "name added in wrong place\n");
  230. tpm_buf_append_u32(buf, handle);
  231. auth->session += 4;
  232. if (mso == TPM2_MSO_PERSISTENT ||
  233. mso == TPM2_MSO_VOLATILE ||
  234. mso == TPM2_MSO_NVRAM) {
  235. if (!name)
  236. tpm2_read_public(chip, handle, auth->name[slot]);
  237. } else {
  238. if (name)
  239. dev_err(&chip->dev, "TPM: Handle does not require name but one is specified\n");
  240. }
  241. auth->name_h[slot] = handle;
  242. if (name)
  243. memcpy(auth->name[slot], name, name_size(name));
  244. #endif
  245. }
  246. EXPORT_SYMBOL_GPL(tpm_buf_append_name);
  247. void tpm_buf_append_auth(struct tpm_chip *chip, struct tpm_buf *buf,
  248. u8 attributes, u8 *passphrase, int passphrase_len)
  249. {
  250. /* offset tells us where the sessions area begins */
  251. int offset = buf->handles * 4 + TPM_HEADER_SIZE;
  252. u32 len = 9 + passphrase_len;
  253. if (tpm_buf_length(buf) != offset) {
  254. /* not the first session so update the existing length */
  255. len += get_unaligned_be32(&buf->data[offset]);
  256. put_unaligned_be32(len, &buf->data[offset]);
  257. } else {
  258. tpm_buf_append_u32(buf, len);
  259. }
  260. /* auth handle */
  261. tpm_buf_append_u32(buf, TPM2_RS_PW);
  262. /* nonce */
  263. tpm_buf_append_u16(buf, 0);
  264. /* attributes */
  265. tpm_buf_append_u8(buf, 0);
  266. /* passphrase */
  267. tpm_buf_append_u16(buf, passphrase_len);
  268. tpm_buf_append(buf, passphrase, passphrase_len);
  269. }
  270. /**
  271. * tpm_buf_append_hmac_session() - Append a TPM session element
  272. * @chip: the TPM chip structure
  273. * @buf: The buffer to be appended
  274. * @attributes: The session attributes
  275. * @passphrase: The session authority (NULL if none)
  276. * @passphrase_len: The length of the session authority (0 if none)
  277. *
  278. * This fills in a session structure in the TPM command buffer, except
  279. * for the HMAC which cannot be computed until the command buffer is
  280. * complete. The type of session is controlled by the @attributes,
  281. * the main ones of which are TPM2_SA_CONTINUE_SESSION which means the
  282. * session won't terminate after tpm_buf_check_hmac_response(),
  283. * TPM2_SA_DECRYPT which means this buffers first parameter should be
  284. * encrypted with a session key and TPM2_SA_ENCRYPT, which means the
  285. * response buffer's first parameter needs to be decrypted (confusing,
  286. * but the defines are written from the point of view of the TPM).
  287. *
  288. * Any session appended by this command must be finalized by calling
  289. * tpm_buf_fill_hmac_session() otherwise the HMAC will be incorrect
  290. * and the TPM will reject the command.
  291. *
  292. * As with most tpm_buf operations, success is assumed because failure
  293. * will be caused by an incorrect programming model and indicated by a
  294. * kernel message.
  295. */
  296. void tpm_buf_append_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf,
  297. u8 attributes, u8 *passphrase,
  298. int passphrase_len)
  299. {
  300. #ifdef CONFIG_TCG_TPM2_HMAC
  301. u8 nonce[SHA256_DIGEST_SIZE];
  302. struct tpm2_auth *auth;
  303. u32 len;
  304. #endif
  305. if (!tpm2_chip_auth(chip)) {
  306. tpm_buf_append_auth(chip, buf, attributes, passphrase,
  307. passphrase_len);
  308. return;
  309. }
  310. #ifdef CONFIG_TCG_TPM2_HMAC
  311. /* The first write to /dev/tpm{rm0} will flush the session. */
  312. attributes |= TPM2_SA_CONTINUE_SESSION;
  313. /*
  314. * The Architecture Guide requires us to strip trailing zeros
  315. * before computing the HMAC
  316. */
  317. while (passphrase && passphrase_len > 0 && passphrase[passphrase_len - 1] == '\0')
  318. passphrase_len--;
  319. auth = chip->auth;
  320. auth->attrs = attributes;
  321. auth->passphrase_len = passphrase_len;
  322. if (passphrase_len)
  323. memcpy(auth->passphrase, passphrase, passphrase_len);
  324. if (auth->session != tpm_buf_length(buf)) {
  325. /* we're not the first session */
  326. len = get_unaligned_be32(&buf->data[auth->session]);
  327. if (4 + len + auth->session != tpm_buf_length(buf)) {
  328. WARN(1, "session length mismatch, cannot append");
  329. return;
  330. }
  331. /* add our new session */
  332. len += 9 + 2 * SHA256_DIGEST_SIZE;
  333. put_unaligned_be32(len, &buf->data[auth->session]);
  334. } else {
  335. tpm_buf_append_u32(buf, 9 + 2 * SHA256_DIGEST_SIZE);
  336. }
  337. /* random number for our nonce */
  338. get_random_bytes(nonce, sizeof(nonce));
  339. memcpy(auth->our_nonce, nonce, sizeof(nonce));
  340. tpm_buf_append_u32(buf, auth->handle);
  341. /* our new nonce */
  342. tpm_buf_append_u16(buf, SHA256_DIGEST_SIZE);
  343. tpm_buf_append(buf, nonce, SHA256_DIGEST_SIZE);
  344. tpm_buf_append_u8(buf, auth->attrs);
  345. /* and put a placeholder for the hmac */
  346. tpm_buf_append_u16(buf, SHA256_DIGEST_SIZE);
  347. tpm_buf_append(buf, nonce, SHA256_DIGEST_SIZE);
  348. #endif
  349. }
  350. EXPORT_SYMBOL_GPL(tpm_buf_append_hmac_session);
  351. #ifdef CONFIG_TCG_TPM2_HMAC
  352. static int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy,
  353. u32 *handle, u8 *name);
  354. /*
  355. * It turns out the crypto hmac(sha256) is hard for us to consume
  356. * because it assumes a fixed key and the TPM seems to change the key
  357. * on every operation, so we weld the hmac init and final functions in
  358. * here to give it the same usage characteristics as a regular hash
  359. */
  360. static void tpm2_hmac_init(struct sha256_state *sctx, u8 *key, u32 key_len)
  361. {
  362. u8 pad[SHA256_BLOCK_SIZE];
  363. int i;
  364. sha256_init(sctx);
  365. for (i = 0; i < sizeof(pad); i++) {
  366. if (i < key_len)
  367. pad[i] = key[i];
  368. else
  369. pad[i] = 0;
  370. pad[i] ^= HMAC_IPAD_VALUE;
  371. }
  372. sha256_update(sctx, pad, sizeof(pad));
  373. }
  374. static void tpm2_hmac_final(struct sha256_state *sctx, u8 *key, u32 key_len,
  375. u8 *out)
  376. {
  377. u8 pad[SHA256_BLOCK_SIZE];
  378. int i;
  379. for (i = 0; i < sizeof(pad); i++) {
  380. if (i < key_len)
  381. pad[i] = key[i];
  382. else
  383. pad[i] = 0;
  384. pad[i] ^= HMAC_OPAD_VALUE;
  385. }
  386. /* collect the final hash; use out as temporary storage */
  387. sha256_final(sctx, out);
  388. sha256_init(sctx);
  389. sha256_update(sctx, pad, sizeof(pad));
  390. sha256_update(sctx, out, SHA256_DIGEST_SIZE);
  391. sha256_final(sctx, out);
  392. }
  393. /*
  394. * assume hash sha256 and nonces u, v of size SHA256_DIGEST_SIZE but
  395. * otherwise standard tpm2_KDFa. Note output is in bytes not bits.
  396. */
  397. static void tpm2_KDFa(u8 *key, u32 key_len, const char *label, u8 *u,
  398. u8 *v, u32 bytes, u8 *out)
  399. {
  400. u32 counter = 1;
  401. const __be32 bits = cpu_to_be32(bytes * 8);
  402. while (bytes > 0) {
  403. struct sha256_state sctx;
  404. __be32 c = cpu_to_be32(counter);
  405. tpm2_hmac_init(&sctx, key, key_len);
  406. sha256_update(&sctx, (u8 *)&c, sizeof(c));
  407. sha256_update(&sctx, label, strlen(label)+1);
  408. sha256_update(&sctx, u, SHA256_DIGEST_SIZE);
  409. sha256_update(&sctx, v, SHA256_DIGEST_SIZE);
  410. sha256_update(&sctx, (u8 *)&bits, sizeof(bits));
  411. tpm2_hmac_final(&sctx, key, key_len, out);
  412. bytes -= SHA256_DIGEST_SIZE;
  413. counter++;
  414. out += SHA256_DIGEST_SIZE;
  415. }
  416. }
  417. /*
  418. * Somewhat of a bastardization of the real KDFe. We're assuming
  419. * we're working with known point sizes for the input parameters and
  420. * the hash algorithm is fixed at sha256. Because we know that the
  421. * point size is 32 bytes like the hash size, there's no need to loop
  422. * in this KDF.
  423. */
  424. static void tpm2_KDFe(u8 z[EC_PT_SZ], const char *str, u8 *pt_u, u8 *pt_v,
  425. u8 *out)
  426. {
  427. struct sha256_state sctx;
  428. /*
  429. * this should be an iterative counter, but because we know
  430. * we're only taking 32 bytes for the point using a sha256
  431. * hash which is also 32 bytes, there's only one loop
  432. */
  433. __be32 c = cpu_to_be32(1);
  434. sha256_init(&sctx);
  435. /* counter (BE) */
  436. sha256_update(&sctx, (u8 *)&c, sizeof(c));
  437. /* secret value */
  438. sha256_update(&sctx, z, EC_PT_SZ);
  439. /* string including trailing zero */
  440. sha256_update(&sctx, str, strlen(str)+1);
  441. sha256_update(&sctx, pt_u, EC_PT_SZ);
  442. sha256_update(&sctx, pt_v, EC_PT_SZ);
  443. sha256_final(&sctx, out);
  444. }
  445. static void tpm_buf_append_salt(struct tpm_buf *buf, struct tpm_chip *chip,
  446. struct tpm2_auth *auth)
  447. {
  448. struct crypto_kpp *kpp;
  449. struct kpp_request *req;
  450. struct scatterlist s[2], d[1];
  451. struct ecdh p = {0};
  452. u8 encoded_key[EC_PT_SZ], *x, *y;
  453. unsigned int buf_len;
  454. /* secret is two sized points */
  455. tpm_buf_append_u16(buf, (EC_PT_SZ + 2)*2);
  456. /*
  457. * we cheat here and append uninitialized data to form
  458. * the points. All we care about is getting the two
  459. * co-ordinate pointers, which will be used to overwrite
  460. * the uninitialized data
  461. */
  462. tpm_buf_append_u16(buf, EC_PT_SZ);
  463. x = &buf->data[tpm_buf_length(buf)];
  464. tpm_buf_append(buf, encoded_key, EC_PT_SZ);
  465. tpm_buf_append_u16(buf, EC_PT_SZ);
  466. y = &buf->data[tpm_buf_length(buf)];
  467. tpm_buf_append(buf, encoded_key, EC_PT_SZ);
  468. sg_init_table(s, 2);
  469. sg_set_buf(&s[0], x, EC_PT_SZ);
  470. sg_set_buf(&s[1], y, EC_PT_SZ);
  471. kpp = crypto_alloc_kpp("ecdh-nist-p256", CRYPTO_ALG_INTERNAL, 0);
  472. if (IS_ERR(kpp)) {
  473. dev_err(&chip->dev, "crypto ecdh allocation failed\n");
  474. return;
  475. }
  476. buf_len = crypto_ecdh_key_len(&p);
  477. if (sizeof(encoded_key) < buf_len) {
  478. dev_err(&chip->dev, "salt buffer too small needs %d\n",
  479. buf_len);
  480. goto out;
  481. }
  482. crypto_ecdh_encode_key(encoded_key, buf_len, &p);
  483. /* this generates a random private key */
  484. crypto_kpp_set_secret(kpp, encoded_key, buf_len);
  485. /* salt is now the public point of this private key */
  486. req = kpp_request_alloc(kpp, GFP_KERNEL);
  487. if (!req)
  488. goto out;
  489. kpp_request_set_input(req, NULL, 0);
  490. kpp_request_set_output(req, s, EC_PT_SZ*2);
  491. crypto_kpp_generate_public_key(req);
  492. /*
  493. * we're not done: now we have to compute the shared secret
  494. * which is our private key multiplied by the tpm_key public
  495. * point, we actually only take the x point and discard the y
  496. * point and feed it through KDFe to get the final secret salt
  497. */
  498. sg_set_buf(&s[0], chip->null_ec_key_x, EC_PT_SZ);
  499. sg_set_buf(&s[1], chip->null_ec_key_y, EC_PT_SZ);
  500. kpp_request_set_input(req, s, EC_PT_SZ*2);
  501. sg_init_one(d, auth->salt, EC_PT_SZ);
  502. kpp_request_set_output(req, d, EC_PT_SZ);
  503. crypto_kpp_compute_shared_secret(req);
  504. kpp_request_free(req);
  505. /*
  506. * pass the shared secret through KDFe for salt. Note salt
  507. * area is used both for input shared secret and output salt.
  508. * This works because KDFe fully consumes the secret before it
  509. * writes the salt
  510. */
  511. tpm2_KDFe(auth->salt, "SECRET", x, chip->null_ec_key_x, auth->salt);
  512. out:
  513. crypto_free_kpp(kpp);
  514. }
  515. /**
  516. * tpm_buf_fill_hmac_session() - finalize the session HMAC
  517. * @chip: the TPM chip structure
  518. * @buf: The buffer to be appended
  519. *
  520. * This command must not be called until all of the parameters have
  521. * been appended to @buf otherwise the computed HMAC will be
  522. * incorrect.
  523. *
  524. * This function computes and fills in the session HMAC using the
  525. * session key and, if TPM2_SA_DECRYPT was specified, computes the
  526. * encryption key and encrypts the first parameter of the command
  527. * buffer with it.
  528. *
  529. * As with most tpm_buf operations, success is assumed because failure
  530. * will be caused by an incorrect programming model and indicated by a
  531. * kernel message.
  532. */
  533. void tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
  534. {
  535. u32 cc, handles, val;
  536. struct tpm2_auth *auth = chip->auth;
  537. int i;
  538. struct tpm_header *head = (struct tpm_header *)buf->data;
  539. off_t offset_s = TPM_HEADER_SIZE, offset_p;
  540. u8 *hmac = NULL;
  541. u32 attrs;
  542. u8 cphash[SHA256_DIGEST_SIZE];
  543. struct sha256_state sctx;
  544. if (!auth)
  545. return;
  546. /* save the command code in BE format */
  547. auth->ordinal = head->ordinal;
  548. cc = be32_to_cpu(head->ordinal);
  549. i = tpm2_find_cc(chip, cc);
  550. if (i < 0) {
  551. dev_err(&chip->dev, "Command 0x%x not found in TPM\n", cc);
  552. return;
  553. }
  554. attrs = chip->cc_attrs_tbl[i];
  555. handles = (attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0);
  556. /*
  557. * just check the names, it's easy to make mistakes. This
  558. * would happen if someone added a handle via
  559. * tpm_buf_append_u32() instead of tpm_buf_append_name()
  560. */
  561. for (i = 0; i < handles; i++) {
  562. u32 handle = tpm_buf_read_u32(buf, &offset_s);
  563. if (auth->name_h[i] != handle) {
  564. dev_err(&chip->dev, "TPM: handle %d wrong for name\n",
  565. i);
  566. return;
  567. }
  568. }
  569. /* point offset_s to the start of the sessions */
  570. val = tpm_buf_read_u32(buf, &offset_s);
  571. /* point offset_p to the start of the parameters */
  572. offset_p = offset_s + val;
  573. for (i = 1; offset_s < offset_p; i++) {
  574. u32 handle = tpm_buf_read_u32(buf, &offset_s);
  575. u16 len;
  576. u8 a;
  577. /* nonce (already in auth) */
  578. len = tpm_buf_read_u16(buf, &offset_s);
  579. offset_s += len;
  580. a = tpm_buf_read_u8(buf, &offset_s);
  581. len = tpm_buf_read_u16(buf, &offset_s);
  582. if (handle == auth->handle && auth->attrs == a) {
  583. hmac = &buf->data[offset_s];
  584. /*
  585. * save our session number so we know which
  586. * session in the response belongs to us
  587. */
  588. auth->session = i;
  589. }
  590. offset_s += len;
  591. }
  592. if (offset_s != offset_p) {
  593. dev_err(&chip->dev, "TPM session length is incorrect\n");
  594. return;
  595. }
  596. if (!hmac) {
  597. dev_err(&chip->dev, "TPM could not find HMAC session\n");
  598. return;
  599. }
  600. /* encrypt before HMAC */
  601. if (auth->attrs & TPM2_SA_DECRYPT) {
  602. u16 len;
  603. /* need key and IV */
  604. tpm2_KDFa(auth->session_key, SHA256_DIGEST_SIZE
  605. + auth->passphrase_len, "CFB", auth->our_nonce,
  606. auth->tpm_nonce, AES_KEY_BYTES + AES_BLOCK_SIZE,
  607. auth->scratch);
  608. len = tpm_buf_read_u16(buf, &offset_p);
  609. aes_expandkey(&auth->aes_ctx, auth->scratch, AES_KEY_BYTES);
  610. aescfb_encrypt(&auth->aes_ctx, &buf->data[offset_p],
  611. &buf->data[offset_p], len,
  612. auth->scratch + AES_KEY_BYTES);
  613. /* reset p to beginning of parameters for HMAC */
  614. offset_p -= 2;
  615. }
  616. sha256_init(&sctx);
  617. /* ordinal is already BE */
  618. sha256_update(&sctx, (u8 *)&head->ordinal, sizeof(head->ordinal));
  619. /* add the handle names */
  620. for (i = 0; i < handles; i++) {
  621. enum tpm2_mso_type mso = tpm2_handle_mso(auth->name_h[i]);
  622. if (mso == TPM2_MSO_PERSISTENT ||
  623. mso == TPM2_MSO_VOLATILE ||
  624. mso == TPM2_MSO_NVRAM) {
  625. sha256_update(&sctx, auth->name[i],
  626. name_size(auth->name[i]));
  627. } else {
  628. __be32 h = cpu_to_be32(auth->name_h[i]);
  629. sha256_update(&sctx, (u8 *)&h, 4);
  630. }
  631. }
  632. if (offset_s != tpm_buf_length(buf))
  633. sha256_update(&sctx, &buf->data[offset_s],
  634. tpm_buf_length(buf) - offset_s);
  635. sha256_final(&sctx, cphash);
  636. /* now calculate the hmac */
  637. tpm2_hmac_init(&sctx, auth->session_key, sizeof(auth->session_key)
  638. + auth->passphrase_len);
  639. sha256_update(&sctx, cphash, sizeof(cphash));
  640. sha256_update(&sctx, auth->our_nonce, sizeof(auth->our_nonce));
  641. sha256_update(&sctx, auth->tpm_nonce, sizeof(auth->tpm_nonce));
  642. sha256_update(&sctx, &auth->attrs, 1);
  643. tpm2_hmac_final(&sctx, auth->session_key, sizeof(auth->session_key)
  644. + auth->passphrase_len, hmac);
  645. }
  646. EXPORT_SYMBOL(tpm_buf_fill_hmac_session);
  647. /**
  648. * tpm_buf_check_hmac_response() - check the TPM return HMAC for correctness
  649. * @chip: the TPM chip structure
  650. * @buf: the original command buffer (which now contains the response)
  651. * @rc: the return code from tpm_transmit_cmd
  652. *
  653. * If @rc is non zero, @buf may not contain an actual return, so @rc
  654. * is passed through as the return and the session cleaned up and
  655. * de-allocated if required (this is required if
  656. * TPM2_SA_CONTINUE_SESSION was not specified as a session flag).
  657. *
  658. * If @rc is zero, the response HMAC is computed against the returned
  659. * @buf and matched to the TPM one in the session area. If there is a
  660. * mismatch, an error is logged and -EINVAL returned.
  661. *
  662. * The reason for this is that the command issue and HMAC check
  663. * sequence should look like:
  664. *
  665. * rc = tpm_transmit_cmd(...);
  666. * rc = tpm_buf_check_hmac_response(&buf, auth, rc);
  667. * if (rc)
  668. * ...
  669. *
  670. * Which is easily layered into the current contrl flow.
  671. *
  672. * Returns: 0 on success or an error.
  673. */
  674. int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
  675. int rc)
  676. {
  677. struct tpm_header *head = (struct tpm_header *)buf->data;
  678. struct tpm2_auth *auth = chip->auth;
  679. off_t offset_s, offset_p;
  680. u8 rphash[SHA256_DIGEST_SIZE];
  681. u32 attrs, cc;
  682. struct sha256_state sctx;
  683. u16 tag = be16_to_cpu(head->tag);
  684. int parm_len, len, i, handles;
  685. if (!auth)
  686. return rc;
  687. cc = be32_to_cpu(auth->ordinal);
  688. if (auth->session >= TPM_HEADER_SIZE) {
  689. WARN(1, "tpm session not filled correctly\n");
  690. goto out;
  691. }
  692. if (rc != 0)
  693. /* pass non success rc through and close the session */
  694. goto out;
  695. rc = -EINVAL;
  696. if (tag != TPM2_ST_SESSIONS) {
  697. dev_err(&chip->dev, "TPM: HMAC response check has no sessions tag\n");
  698. goto out;
  699. }
  700. i = tpm2_find_cc(chip, cc);
  701. if (i < 0)
  702. goto out;
  703. attrs = chip->cc_attrs_tbl[i];
  704. handles = (attrs >> TPM2_CC_ATTR_RHANDLE) & 1;
  705. /* point to area beyond handles */
  706. offset_s = TPM_HEADER_SIZE + handles * 4;
  707. parm_len = tpm_buf_read_u32(buf, &offset_s);
  708. offset_p = offset_s;
  709. offset_s += parm_len;
  710. /* skip over any sessions before ours */
  711. for (i = 0; i < auth->session - 1; i++) {
  712. len = tpm_buf_read_u16(buf, &offset_s);
  713. offset_s += len + 1;
  714. len = tpm_buf_read_u16(buf, &offset_s);
  715. offset_s += len;
  716. }
  717. /* TPM nonce */
  718. len = tpm_buf_read_u16(buf, &offset_s);
  719. if (offset_s + len > tpm_buf_length(buf))
  720. goto out;
  721. if (len != SHA256_DIGEST_SIZE)
  722. goto out;
  723. memcpy(auth->tpm_nonce, &buf->data[offset_s], len);
  724. offset_s += len;
  725. attrs = tpm_buf_read_u8(buf, &offset_s);
  726. len = tpm_buf_read_u16(buf, &offset_s);
  727. if (offset_s + len != tpm_buf_length(buf))
  728. goto out;
  729. if (len != SHA256_DIGEST_SIZE)
  730. goto out;
  731. /*
  732. * offset_s points to the HMAC. now calculate comparison, beginning
  733. * with rphash
  734. */
  735. sha256_init(&sctx);
  736. /* yes, I know this is now zero, but it's what the standard says */
  737. sha256_update(&sctx, (u8 *)&head->return_code,
  738. sizeof(head->return_code));
  739. /* ordinal is already BE */
  740. sha256_update(&sctx, (u8 *)&auth->ordinal, sizeof(auth->ordinal));
  741. sha256_update(&sctx, &buf->data[offset_p], parm_len);
  742. sha256_final(&sctx, rphash);
  743. /* now calculate the hmac */
  744. tpm2_hmac_init(&sctx, auth->session_key, sizeof(auth->session_key)
  745. + auth->passphrase_len);
  746. sha256_update(&sctx, rphash, sizeof(rphash));
  747. sha256_update(&sctx, auth->tpm_nonce, sizeof(auth->tpm_nonce));
  748. sha256_update(&sctx, auth->our_nonce, sizeof(auth->our_nonce));
  749. sha256_update(&sctx, &auth->attrs, 1);
  750. /* we're done with the rphash, so put our idea of the hmac there */
  751. tpm2_hmac_final(&sctx, auth->session_key, sizeof(auth->session_key)
  752. + auth->passphrase_len, rphash);
  753. if (memcmp(rphash, &buf->data[offset_s], SHA256_DIGEST_SIZE) == 0) {
  754. rc = 0;
  755. } else {
  756. dev_err(&chip->dev, "TPM: HMAC check failed\n");
  757. goto out;
  758. }
  759. /* now do response decryption */
  760. if (auth->attrs & TPM2_SA_ENCRYPT) {
  761. /* need key and IV */
  762. tpm2_KDFa(auth->session_key, SHA256_DIGEST_SIZE
  763. + auth->passphrase_len, "CFB", auth->tpm_nonce,
  764. auth->our_nonce, AES_KEY_BYTES + AES_BLOCK_SIZE,
  765. auth->scratch);
  766. len = tpm_buf_read_u16(buf, &offset_p);
  767. aes_expandkey(&auth->aes_ctx, auth->scratch, AES_KEY_BYTES);
  768. aescfb_decrypt(&auth->aes_ctx, &buf->data[offset_p],
  769. &buf->data[offset_p], len,
  770. auth->scratch + AES_KEY_BYTES);
  771. }
  772. out:
  773. if ((auth->attrs & TPM2_SA_CONTINUE_SESSION) == 0) {
  774. if (rc)
  775. /* manually close the session if it wasn't consumed */
  776. tpm2_flush_context(chip, auth->handle);
  777. kfree_sensitive(auth);
  778. chip->auth = NULL;
  779. } else {
  780. /* reset for next use */
  781. auth->session = TPM_HEADER_SIZE;
  782. }
  783. return rc;
  784. }
  785. EXPORT_SYMBOL(tpm_buf_check_hmac_response);
  786. /**
  787. * tpm2_end_auth_session() - kill the allocated auth session
  788. * @chip: the TPM chip structure
  789. *
  790. * ends the session started by tpm2_start_auth_session and frees all
  791. * the resources. Under normal conditions,
  792. * tpm_buf_check_hmac_response() will correctly end the session if
  793. * required, so this function is only for use in error legs that will
  794. * bypass the normal invocation of tpm_buf_check_hmac_response().
  795. */
  796. void tpm2_end_auth_session(struct tpm_chip *chip)
  797. {
  798. struct tpm2_auth *auth = chip->auth;
  799. if (!auth)
  800. return;
  801. tpm2_flush_context(chip, auth->handle);
  802. kfree_sensitive(auth);
  803. chip->auth = NULL;
  804. }
  805. EXPORT_SYMBOL(tpm2_end_auth_session);
  806. static int tpm2_parse_start_auth_session(struct tpm2_auth *auth,
  807. struct tpm_buf *buf)
  808. {
  809. struct tpm_header *head = (struct tpm_header *)buf->data;
  810. u32 tot_len = be32_to_cpu(head->length);
  811. off_t offset = TPM_HEADER_SIZE;
  812. u32 val;
  813. /* we're starting after the header so adjust the length */
  814. tot_len -= TPM_HEADER_SIZE;
  815. /* should have handle plus nonce */
  816. if (tot_len != 4 + 2 + sizeof(auth->tpm_nonce))
  817. return -EINVAL;
  818. auth->handle = tpm_buf_read_u32(buf, &offset);
  819. val = tpm_buf_read_u16(buf, &offset);
  820. if (val != sizeof(auth->tpm_nonce))
  821. return -EINVAL;
  822. memcpy(auth->tpm_nonce, &buf->data[offset], sizeof(auth->tpm_nonce));
  823. /* now compute the session key from the nonces */
  824. tpm2_KDFa(auth->salt, sizeof(auth->salt), "ATH", auth->tpm_nonce,
  825. auth->our_nonce, sizeof(auth->session_key),
  826. auth->session_key);
  827. return 0;
  828. }
  829. static int tpm2_load_null(struct tpm_chip *chip, u32 *null_key)
  830. {
  831. unsigned int offset = 0; /* dummy offset for null seed context */
  832. u8 name[SHA256_DIGEST_SIZE + 2];
  833. u32 tmp_null_key;
  834. int rc;
  835. rc = tpm2_load_context(chip, chip->null_key_context, &offset,
  836. &tmp_null_key);
  837. if (rc != -EINVAL) {
  838. if (!rc)
  839. *null_key = tmp_null_key;
  840. goto err;
  841. }
  842. /* Try to re-create null key, given the integrity failure: */
  843. rc = tpm2_create_primary(chip, TPM2_RH_NULL, &tmp_null_key, name);
  844. if (rc)
  845. goto err;
  846. /* Return null key if the name has not been changed: */
  847. if (!memcmp(name, chip->null_key_name, sizeof(name))) {
  848. *null_key = tmp_null_key;
  849. return 0;
  850. }
  851. /* Deduce from the name change TPM interference: */
  852. dev_err(&chip->dev, "null key integrity check failed\n");
  853. tpm2_flush_context(chip, tmp_null_key);
  854. err:
  855. if (rc) {
  856. chip->flags |= TPM_CHIP_FLAG_DISABLE;
  857. rc = -ENODEV;
  858. }
  859. return rc;
  860. }
  861. /**
  862. * tpm2_start_auth_session() - Create an a HMAC authentication session
  863. * @chip: A TPM chip
  864. *
  865. * Loads the ephemeral key (null seed), and starts an HMAC authenticated
  866. * session. The null seed is flushed before the return.
  867. *
  868. * Returns zero on success, or a POSIX error code.
  869. */
  870. int tpm2_start_auth_session(struct tpm_chip *chip)
  871. {
  872. struct tpm2_auth *auth;
  873. struct tpm_buf buf;
  874. u32 null_key;
  875. int rc;
  876. if (chip->auth) {
  877. dev_dbg_once(&chip->dev, "auth session is active\n");
  878. return 0;
  879. }
  880. auth = kzalloc(sizeof(*auth), GFP_KERNEL);
  881. if (!auth)
  882. return -ENOMEM;
  883. rc = tpm2_load_null(chip, &null_key);
  884. if (rc)
  885. goto out;
  886. auth->session = TPM_HEADER_SIZE;
  887. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_START_AUTH_SESS);
  888. if (rc)
  889. goto out;
  890. /* salt key handle */
  891. tpm_buf_append_u32(&buf, null_key);
  892. /* bind key handle */
  893. tpm_buf_append_u32(&buf, TPM2_RH_NULL);
  894. /* nonce caller */
  895. get_random_bytes(auth->our_nonce, sizeof(auth->our_nonce));
  896. tpm_buf_append_u16(&buf, sizeof(auth->our_nonce));
  897. tpm_buf_append(&buf, auth->our_nonce, sizeof(auth->our_nonce));
  898. /* append encrypted salt and squirrel away unencrypted in auth */
  899. tpm_buf_append_salt(&buf, chip, auth);
  900. /* session type (HMAC, audit or policy) */
  901. tpm_buf_append_u8(&buf, TPM2_SE_HMAC);
  902. /* symmetric encryption parameters */
  903. /* symmetric algorithm */
  904. tpm_buf_append_u16(&buf, TPM_ALG_AES);
  905. /* bits for symmetric algorithm */
  906. tpm_buf_append_u16(&buf, AES_KEY_BITS);
  907. /* symmetric algorithm mode (must be CFB) */
  908. tpm_buf_append_u16(&buf, TPM_ALG_CFB);
  909. /* hash algorithm for session */
  910. tpm_buf_append_u16(&buf, TPM_ALG_SHA256);
  911. rc = tpm_ret_to_err(tpm_transmit_cmd(chip, &buf, 0, "StartAuthSession"));
  912. tpm2_flush_context(chip, null_key);
  913. if (rc == TPM2_RC_SUCCESS)
  914. rc = tpm2_parse_start_auth_session(auth, &buf);
  915. tpm_buf_destroy(&buf);
  916. if (rc == TPM2_RC_SUCCESS) {
  917. chip->auth = auth;
  918. return 0;
  919. }
  920. out:
  921. kfree_sensitive(auth);
  922. return rc;
  923. }
  924. EXPORT_SYMBOL(tpm2_start_auth_session);
  925. /*
  926. * A mask containing the object attributes for the kernel held null primary key
  927. * used in HMAC encryption. For more information on specific attributes look up
  928. * to "8.3 TPMA_OBJECT (Object Attributes)".
  929. */
  930. #define TPM2_OA_NULL_KEY ( \
  931. TPM2_OA_NO_DA | \
  932. TPM2_OA_FIXED_TPM | \
  933. TPM2_OA_FIXED_PARENT | \
  934. TPM2_OA_SENSITIVE_DATA_ORIGIN | \
  935. TPM2_OA_USER_WITH_AUTH | \
  936. TPM2_OA_DECRYPT | \
  937. TPM2_OA_RESTRICTED)
  938. /**
  939. * tpm2_parse_create_primary() - parse the data returned from TPM_CC_CREATE_PRIMARY
  940. *
  941. * @chip: The TPM the primary was created under
  942. * @buf: The response buffer from the chip
  943. * @handle: pointer to be filled in with the return handle of the primary
  944. * @hierarchy: The hierarchy the primary was created for
  945. * @name: pointer to be filled in with the primary key name
  946. *
  947. * Return:
  948. * * 0 - OK
  949. * * -errno - A system error
  950. * * TPM_RC - A TPM error
  951. */
  952. static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
  953. u32 *handle, u32 hierarchy, u8 *name)
  954. {
  955. struct tpm_header *head = (struct tpm_header *)buf->data;
  956. off_t offset_r = TPM_HEADER_SIZE, offset_t;
  957. u16 len = TPM_HEADER_SIZE;
  958. u32 total_len = be32_to_cpu(head->length);
  959. u32 val, param_len, keyhandle;
  960. keyhandle = tpm_buf_read_u32(buf, &offset_r);
  961. if (handle)
  962. *handle = keyhandle;
  963. else
  964. tpm2_flush_context(chip, keyhandle);
  965. param_len = tpm_buf_read_u32(buf, &offset_r);
  966. /*
  967. * param_len doesn't include the header, but all the other
  968. * lengths and offsets do, so add it to parm len to make
  969. * the comparisons easier
  970. */
  971. param_len += TPM_HEADER_SIZE;
  972. if (param_len + 8 > total_len)
  973. return -EINVAL;
  974. len = tpm_buf_read_u16(buf, &offset_r);
  975. offset_t = offset_r;
  976. if (name) {
  977. /*
  978. * now we have the public area, compute the name of
  979. * the object
  980. */
  981. put_unaligned_be16(TPM_ALG_SHA256, name);
  982. sha256(&buf->data[offset_r], len, name + 2);
  983. }
  984. /* validate the public key */
  985. val = tpm_buf_read_u16(buf, &offset_t);
  986. /* key type (must be what we asked for) */
  987. if (val != TPM_ALG_ECC)
  988. return -EINVAL;
  989. val = tpm_buf_read_u16(buf, &offset_t);
  990. /* name algorithm */
  991. if (val != TPM_ALG_SHA256)
  992. return -EINVAL;
  993. val = tpm_buf_read_u32(buf, &offset_t);
  994. /* object properties */
  995. if (val != TPM2_OA_NULL_KEY)
  996. return -EINVAL;
  997. /* auth policy (empty) */
  998. val = tpm_buf_read_u16(buf, &offset_t);
  999. if (val != 0)
  1000. return -EINVAL;
  1001. /* symmetric key parameters */
  1002. val = tpm_buf_read_u16(buf, &offset_t);
  1003. if (val != TPM_ALG_AES)
  1004. return -EINVAL;
  1005. /* symmetric key length */
  1006. val = tpm_buf_read_u16(buf, &offset_t);
  1007. if (val != AES_KEY_BITS)
  1008. return -EINVAL;
  1009. /* symmetric encryption scheme */
  1010. val = tpm_buf_read_u16(buf, &offset_t);
  1011. if (val != TPM_ALG_CFB)
  1012. return -EINVAL;
  1013. /* signing scheme */
  1014. val = tpm_buf_read_u16(buf, &offset_t);
  1015. if (val != TPM_ALG_NULL)
  1016. return -EINVAL;
  1017. /* ECC Curve */
  1018. val = tpm_buf_read_u16(buf, &offset_t);
  1019. if (val != TPM2_ECC_NIST_P256)
  1020. return -EINVAL;
  1021. /* KDF Scheme */
  1022. val = tpm_buf_read_u16(buf, &offset_t);
  1023. if (val != TPM_ALG_NULL)
  1024. return -EINVAL;
  1025. /* extract public key (x and y points) */
  1026. val = tpm_buf_read_u16(buf, &offset_t);
  1027. if (val != EC_PT_SZ)
  1028. return -EINVAL;
  1029. memcpy(chip->null_ec_key_x, &buf->data[offset_t], val);
  1030. offset_t += val;
  1031. val = tpm_buf_read_u16(buf, &offset_t);
  1032. if (val != EC_PT_SZ)
  1033. return -EINVAL;
  1034. memcpy(chip->null_ec_key_y, &buf->data[offset_t], val);
  1035. offset_t += val;
  1036. /* original length of the whole TPM2B */
  1037. offset_r += len;
  1038. /* should have exactly consumed the TPM2B public structure */
  1039. if (offset_t != offset_r)
  1040. return -EINVAL;
  1041. if (offset_r > param_len)
  1042. return -EINVAL;
  1043. /* creation data (skip) */
  1044. len = tpm_buf_read_u16(buf, &offset_r);
  1045. offset_r += len;
  1046. if (offset_r > param_len)
  1047. return -EINVAL;
  1048. /* creation digest (must be sha256) */
  1049. len = tpm_buf_read_u16(buf, &offset_r);
  1050. offset_r += len;
  1051. if (len != SHA256_DIGEST_SIZE || offset_r > param_len)
  1052. return -EINVAL;
  1053. /* TPMT_TK_CREATION follows */
  1054. /* tag, must be TPM_ST_CREATION (0x8021) */
  1055. val = tpm_buf_read_u16(buf, &offset_r);
  1056. if (val != TPM2_ST_CREATION || offset_r > param_len)
  1057. return -EINVAL;
  1058. /* hierarchy */
  1059. val = tpm_buf_read_u32(buf, &offset_r);
  1060. if (val != hierarchy || offset_r > param_len)
  1061. return -EINVAL;
  1062. /* the ticket digest HMAC (might not be sha256) */
  1063. len = tpm_buf_read_u16(buf, &offset_r);
  1064. offset_r += len;
  1065. if (offset_r > param_len)
  1066. return -EINVAL;
  1067. /*
  1068. * finally we have the name, which is a sha256 digest plus a 2
  1069. * byte algorithm type
  1070. */
  1071. len = tpm_buf_read_u16(buf, &offset_r);
  1072. if (offset_r + len != param_len + 8)
  1073. return -EINVAL;
  1074. if (len != SHA256_DIGEST_SIZE + 2)
  1075. return -EINVAL;
  1076. if (memcmp(chip->null_key_name, &buf->data[offset_r],
  1077. SHA256_DIGEST_SIZE + 2) != 0) {
  1078. dev_err(&chip->dev, "NULL Seed name comparison failed\n");
  1079. return -EINVAL;
  1080. }
  1081. return 0;
  1082. }
  1083. /**
  1084. * tpm2_create_primary() - create a primary key using a fixed P-256 template
  1085. *
  1086. * @chip: the TPM chip to create under
  1087. * @hierarchy: The hierarchy handle to create under
  1088. * @handle: The returned volatile handle on success
  1089. * @name: The name of the returned key
  1090. *
  1091. * For platforms that might not have a persistent primary, this can be
  1092. * used to create one quickly on the fly (it uses Elliptic Curve not
  1093. * RSA, so even slow TPMs can create one fast). The template uses the
  1094. * TCG mandated H one for non-endorsement ECC primaries, i.e. P-256
  1095. * elliptic curve (the only current one all TPM2s are required to
  1096. * have) a sha256 name hash and no policy.
  1097. *
  1098. * Return:
  1099. * * 0 - OK
  1100. * * -errno - A system error
  1101. * * TPM_RC - A TPM error
  1102. */
  1103. static int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy,
  1104. u32 *handle, u8 *name)
  1105. {
  1106. int rc;
  1107. struct tpm_buf buf;
  1108. struct tpm_buf template;
  1109. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE_PRIMARY);
  1110. if (rc)
  1111. return rc;
  1112. rc = tpm_buf_init_sized(&template);
  1113. if (rc) {
  1114. tpm_buf_destroy(&buf);
  1115. return rc;
  1116. }
  1117. /*
  1118. * create the template. Note: in order for userspace to
  1119. * verify the security of the system, it will have to create
  1120. * and certify this NULL primary, meaning all the template
  1121. * parameters will have to be identical, so conform exactly to
  1122. * the TCG TPM v2.0 Provisioning Guidance for the SRK ECC
  1123. * key H template (H has zero size unique points)
  1124. */
  1125. /* key type */
  1126. tpm_buf_append_u16(&template, TPM_ALG_ECC);
  1127. /* name algorithm */
  1128. tpm_buf_append_u16(&template, TPM_ALG_SHA256);
  1129. /* object properties */
  1130. tpm_buf_append_u32(&template, TPM2_OA_NULL_KEY);
  1131. /* sauth policy (empty) */
  1132. tpm_buf_append_u16(&template, 0);
  1133. /* BEGIN parameters: key specific; for ECC*/
  1134. /* symmetric algorithm */
  1135. tpm_buf_append_u16(&template, TPM_ALG_AES);
  1136. /* bits for symmetric algorithm */
  1137. tpm_buf_append_u16(&template, AES_KEY_BITS);
  1138. /* algorithm mode (must be CFB) */
  1139. tpm_buf_append_u16(&template, TPM_ALG_CFB);
  1140. /* scheme (NULL means any scheme) */
  1141. tpm_buf_append_u16(&template, TPM_ALG_NULL);
  1142. /* ECC Curve ID */
  1143. tpm_buf_append_u16(&template, TPM2_ECC_NIST_P256);
  1144. /* KDF Scheme */
  1145. tpm_buf_append_u16(&template, TPM_ALG_NULL);
  1146. /* unique: key specific; for ECC it is two zero size points */
  1147. tpm_buf_append_u16(&template, 0);
  1148. tpm_buf_append_u16(&template, 0);
  1149. /* END parameters */
  1150. /* primary handle */
  1151. tpm_buf_append_u32(&buf, hierarchy);
  1152. tpm_buf_append_empty_auth(&buf, TPM2_RS_PW);
  1153. /* sensitive create size is 4 for two empty buffers */
  1154. tpm_buf_append_u16(&buf, 4);
  1155. /* sensitive create auth data (empty) */
  1156. tpm_buf_append_u16(&buf, 0);
  1157. /* sensitive create sensitive data (empty) */
  1158. tpm_buf_append_u16(&buf, 0);
  1159. /* the public template */
  1160. tpm_buf_append(&buf, template.data, template.length);
  1161. tpm_buf_destroy(&template);
  1162. /* outside info (empty) */
  1163. tpm_buf_append_u16(&buf, 0);
  1164. /* creation PCR (none) */
  1165. tpm_buf_append_u32(&buf, 0);
  1166. rc = tpm_transmit_cmd(chip, &buf, 0,
  1167. "attempting to create NULL primary");
  1168. if (rc == TPM2_RC_SUCCESS)
  1169. rc = tpm2_parse_create_primary(chip, &buf, handle, hierarchy,
  1170. name);
  1171. tpm_buf_destroy(&buf);
  1172. return rc;
  1173. }
  1174. static int tpm2_create_null_primary(struct tpm_chip *chip)
  1175. {
  1176. u32 null_key;
  1177. int rc;
  1178. rc = tpm2_create_primary(chip, TPM2_RH_NULL, &null_key,
  1179. chip->null_key_name);
  1180. if (rc == TPM2_RC_SUCCESS) {
  1181. unsigned int offset = 0; /* dummy offset for null key context */
  1182. rc = tpm2_save_context(chip, null_key, chip->null_key_context,
  1183. sizeof(chip->null_key_context), &offset);
  1184. tpm2_flush_context(chip, null_key);
  1185. }
  1186. return rc;
  1187. }
  1188. /**
  1189. * tpm2_sessions_init() - start of day initialization for the sessions code
  1190. * @chip: TPM chip
  1191. *
  1192. * Derive and context save the null primary and allocate memory in the
  1193. * struct tpm_chip for the authorizations.
  1194. *
  1195. * Return:
  1196. * * 0 - OK
  1197. * * -errno - A system error
  1198. * * TPM_RC - A TPM error
  1199. */
  1200. int tpm2_sessions_init(struct tpm_chip *chip)
  1201. {
  1202. int rc;
  1203. rc = tpm2_create_null_primary(chip);
  1204. if (rc) {
  1205. dev_err(&chip->dev, "null key creation failed with %d\n", rc);
  1206. return rc;
  1207. }
  1208. return rc;
  1209. }
  1210. EXPORT_SYMBOL(tpm2_sessions_init);
  1211. #endif /* CONFIG_TCG_TPM2_HMAC */