tpm2-sessions.c 41 KB

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