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