|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (C) 2018 [email protected] |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include "tpm.h" |
| 9 | +#include <asm/unaligned.h> |
| 10 | + |
| 11 | +/** |
| 12 | + * tpm2_parse_create_primary() - parse the data returned from TPM_CC_CREATE_PRIMARY |
| 13 | + * |
| 14 | + * @chip: The TPM the primary was created under |
| 15 | + * @buf: The response buffer from the chip |
| 16 | + * @handle: pointer to be filled in with the return handle of the primary |
| 17 | + * @hierarchy: The hierarchy the primary was created for |
| 18 | + * |
| 19 | + * Return: |
| 20 | + * * 0 - OK |
| 21 | + * * -errno - A system error |
| 22 | + * * TPM_RC - A TPM error |
| 23 | + */ |
| 24 | +static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf, |
| 25 | + u32 *handle, u32 hierarchy) |
| 26 | +{ |
| 27 | + struct tpm_header *head = (struct tpm_header *)buf->data; |
| 28 | + off_t offset_r = TPM_HEADER_SIZE, offset_t; |
| 29 | + u16 len = TPM_HEADER_SIZE; |
| 30 | + u32 total_len = be32_to_cpu(head->length); |
| 31 | + u32 val, param_len; |
| 32 | + |
| 33 | + *handle = tpm_buf_read_u32(buf, &offset_r); |
| 34 | + param_len = tpm_buf_read_u32(buf, &offset_r); |
| 35 | + /* |
| 36 | + * param_len doesn't include the header, but all the other |
| 37 | + * lengths and offsets do, so add it to parm len to make |
| 38 | + * the comparisons easier |
| 39 | + */ |
| 40 | + param_len += TPM_HEADER_SIZE; |
| 41 | + |
| 42 | + if (param_len + 8 > total_len) |
| 43 | + return -EINVAL; |
| 44 | + len = tpm_buf_read_u16(buf, &offset_r); |
| 45 | + offset_t = offset_r; |
| 46 | + /* now we have the public area, compute the name of the object */ |
| 47 | + put_unaligned_be16(TPM_ALG_SHA256, chip->null_key_name); |
| 48 | + sha256(&buf->data[offset_r], len, chip->null_key_name + 2); |
| 49 | + |
| 50 | + /* validate the public key */ |
| 51 | + val = tpm_buf_read_u16(buf, &offset_t); |
| 52 | + |
| 53 | + /* key type (must be what we asked for) */ |
| 54 | + if (val != TPM_ALG_ECC) |
| 55 | + return -EINVAL; |
| 56 | + val = tpm_buf_read_u16(buf, &offset_t); |
| 57 | + |
| 58 | + /* name algorithm */ |
| 59 | + if (val != TPM_ALG_SHA256) |
| 60 | + return -EINVAL; |
| 61 | + val = tpm_buf_read_u32(buf, &offset_t); |
| 62 | + |
| 63 | + /* object properties */ |
| 64 | + if (val != TPM2_OA_TMPL) |
| 65 | + return -EINVAL; |
| 66 | + |
| 67 | + /* auth policy (empty) */ |
| 68 | + val = tpm_buf_read_u16(buf, &offset_t); |
| 69 | + if (val != 0) |
| 70 | + return -EINVAL; |
| 71 | + |
| 72 | + /* symmetric key parameters */ |
| 73 | + val = tpm_buf_read_u16(buf, &offset_t); |
| 74 | + if (val != TPM_ALG_AES) |
| 75 | + return -EINVAL; |
| 76 | + |
| 77 | + /* symmetric key length */ |
| 78 | + val = tpm_buf_read_u16(buf, &offset_t); |
| 79 | + if (val != AES_KEY_BITS) |
| 80 | + return -EINVAL; |
| 81 | + |
| 82 | + /* symmetric encryption scheme */ |
| 83 | + val = tpm_buf_read_u16(buf, &offset_t); |
| 84 | + if (val != TPM_ALG_CFB) |
| 85 | + return -EINVAL; |
| 86 | + |
| 87 | + /* signing scheme */ |
| 88 | + val = tpm_buf_read_u16(buf, &offset_t); |
| 89 | + if (val != TPM_ALG_NULL) |
| 90 | + return -EINVAL; |
| 91 | + |
| 92 | + /* ECC Curve */ |
| 93 | + val = tpm_buf_read_u16(buf, &offset_t); |
| 94 | + if (val != TPM2_ECC_NIST_P256) |
| 95 | + return -EINVAL; |
| 96 | + |
| 97 | + /* KDF Scheme */ |
| 98 | + val = tpm_buf_read_u16(buf, &offset_t); |
| 99 | + if (val != TPM_ALG_NULL) |
| 100 | + return -EINVAL; |
| 101 | + |
| 102 | + /* extract public key (x and y points) */ |
| 103 | + val = tpm_buf_read_u16(buf, &offset_t); |
| 104 | + if (val != EC_PT_SZ) |
| 105 | + return -EINVAL; |
| 106 | + memcpy(chip->null_ec_key_x, &buf->data[offset_t], val); |
| 107 | + offset_t += val; |
| 108 | + val = tpm_buf_read_u16(buf, &offset_t); |
| 109 | + if (val != EC_PT_SZ) |
| 110 | + return -EINVAL; |
| 111 | + memcpy(chip->null_ec_key_y, &buf->data[offset_t], val); |
| 112 | + offset_t += val; |
| 113 | + |
| 114 | + /* original length of the whole TPM2B */ |
| 115 | + offset_r += len; |
| 116 | + |
| 117 | + /* should have exactly consumed the TPM2B public structure */ |
| 118 | + if (offset_t != offset_r) |
| 119 | + return -EINVAL; |
| 120 | + if (offset_r > param_len) |
| 121 | + return -EINVAL; |
| 122 | + |
| 123 | + /* creation data (skip) */ |
| 124 | + len = tpm_buf_read_u16(buf, &offset_r); |
| 125 | + offset_r += len; |
| 126 | + if (offset_r > param_len) |
| 127 | + return -EINVAL; |
| 128 | + |
| 129 | + /* creation digest (must be sha256) */ |
| 130 | + len = tpm_buf_read_u16(buf, &offset_r); |
| 131 | + offset_r += len; |
| 132 | + if (len != SHA256_DIGEST_SIZE || offset_r > param_len) |
| 133 | + return -EINVAL; |
| 134 | + |
| 135 | + /* TPMT_TK_CREATION follows */ |
| 136 | + /* tag, must be TPM_ST_CREATION (0x8021) */ |
| 137 | + val = tpm_buf_read_u16(buf, &offset_r); |
| 138 | + if (val != TPM2_ST_CREATION || offset_r > param_len) |
| 139 | + return -EINVAL; |
| 140 | + |
| 141 | + /* hierarchy */ |
| 142 | + val = tpm_buf_read_u32(buf, &offset_r); |
| 143 | + if (val != hierarchy || offset_r > param_len) |
| 144 | + return -EINVAL; |
| 145 | + |
| 146 | + /* the ticket digest HMAC (might not be sha256) */ |
| 147 | + len = tpm_buf_read_u16(buf, &offset_r); |
| 148 | + offset_r += len; |
| 149 | + if (offset_r > param_len) |
| 150 | + return -EINVAL; |
| 151 | + |
| 152 | + /* |
| 153 | + * finally we have the name, which is a sha256 digest plus a 2 |
| 154 | + * byte algorithm type |
| 155 | + */ |
| 156 | + len = tpm_buf_read_u16(buf, &offset_r); |
| 157 | + if (offset_r + len != param_len + 8) |
| 158 | + return -EINVAL; |
| 159 | + if (len != SHA256_DIGEST_SIZE + 2) |
| 160 | + return -EINVAL; |
| 161 | + |
| 162 | + if (memcmp(chip->null_key_name, &buf->data[offset_r], |
| 163 | + SHA256_DIGEST_SIZE + 2) != 0) { |
| 164 | + dev_err(&chip->dev, "NULL Seed name comparison failed\n"); |
| 165 | + return -EINVAL; |
| 166 | + } |
| 167 | + |
| 168 | + return 0; |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * tpm2_create_primary() - create a primary key using a fixed P-256 template |
| 173 | + * |
| 174 | + * @chip: the TPM chip to create under |
| 175 | + * @hierarchy: The hierarchy handle to create under |
| 176 | + * @handle: The returned volatile handle on success |
| 177 | + * |
| 178 | + * For platforms that might not have a persistent primary, this can be |
| 179 | + * used to create one quickly on the fly (it uses Elliptic Curve not |
| 180 | + * RSA, so even slow TPMs can create one fast). The template uses the |
| 181 | + * TCG mandated H one for non-endorsement ECC primaries, i.e. P-256 |
| 182 | + * elliptic curve (the only current one all TPM2s are required to |
| 183 | + * have) a sha256 name hash and no policy. |
| 184 | + * |
| 185 | + * Return: |
| 186 | + * * 0 - OK |
| 187 | + * * -errno - A system error |
| 188 | + * * TPM_RC - A TPM error |
| 189 | + */ |
| 190 | +static int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy, |
| 191 | + u32 *handle) |
| 192 | +{ |
| 193 | + int rc; |
| 194 | + struct tpm_buf buf; |
| 195 | + struct tpm_buf template; |
| 196 | + |
| 197 | + rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE_PRIMARY); |
| 198 | + if (rc) |
| 199 | + return rc; |
| 200 | + |
| 201 | + rc = tpm_buf_init_sized(&template); |
| 202 | + if (rc) { |
| 203 | + tpm_buf_destroy(&buf); |
| 204 | + return rc; |
| 205 | + } |
| 206 | + |
| 207 | + /* |
| 208 | + * create the template. Note: in order for userspace to |
| 209 | + * verify the security of the system, it will have to create |
| 210 | + * and certify this NULL primary, meaning all the template |
| 211 | + * parameters will have to be identical, so conform exactly to |
| 212 | + * the TCG TPM v2.0 Provisioning Guidance for the SRK ECC |
| 213 | + * key H template (H has zero size unique points) |
| 214 | + */ |
| 215 | + |
| 216 | + /* key type */ |
| 217 | + tpm_buf_append_u16(&template, TPM_ALG_ECC); |
| 218 | + |
| 219 | + /* name algorithm */ |
| 220 | + tpm_buf_append_u16(&template, TPM_ALG_SHA256); |
| 221 | + |
| 222 | + /* object properties */ |
| 223 | + tpm_buf_append_u32(&template, TPM2_OA_TMPL); |
| 224 | + |
| 225 | + /* sauth policy (empty) */ |
| 226 | + tpm_buf_append_u16(&template, 0); |
| 227 | + |
| 228 | + /* BEGIN parameters: key specific; for ECC*/ |
| 229 | + |
| 230 | + /* symmetric algorithm */ |
| 231 | + tpm_buf_append_u16(&template, TPM_ALG_AES); |
| 232 | + |
| 233 | + /* bits for symmetric algorithm */ |
| 234 | + tpm_buf_append_u16(&template, AES_KEY_BITS); |
| 235 | + |
| 236 | + /* algorithm mode (must be CFB) */ |
| 237 | + tpm_buf_append_u16(&template, TPM_ALG_CFB); |
| 238 | + |
| 239 | + /* scheme (NULL means any scheme) */ |
| 240 | + tpm_buf_append_u16(&template, TPM_ALG_NULL); |
| 241 | + |
| 242 | + /* ECC Curve ID */ |
| 243 | + tpm_buf_append_u16(&template, TPM2_ECC_NIST_P256); |
| 244 | + |
| 245 | + /* KDF Scheme */ |
| 246 | + tpm_buf_append_u16(&template, TPM_ALG_NULL); |
| 247 | + |
| 248 | + /* unique: key specific; for ECC it is two zero size points */ |
| 249 | + tpm_buf_append_u16(&template, 0); |
| 250 | + tpm_buf_append_u16(&template, 0); |
| 251 | + |
| 252 | + /* END parameters */ |
| 253 | + |
| 254 | + /* primary handle */ |
| 255 | + tpm_buf_append_u32(&buf, hierarchy); |
| 256 | + tpm_buf_append_empty_auth(&buf, TPM2_RS_PW); |
| 257 | + |
| 258 | + /* sensitive create size is 4 for two empty buffers */ |
| 259 | + tpm_buf_append_u16(&buf, 4); |
| 260 | + |
| 261 | + /* sensitive create auth data (empty) */ |
| 262 | + tpm_buf_append_u16(&buf, 0); |
| 263 | + |
| 264 | + /* sensitive create sensitive data (empty) */ |
| 265 | + tpm_buf_append_u16(&buf, 0); |
| 266 | + |
| 267 | + /* the public template */ |
| 268 | + tpm_buf_append(&buf, template.data, template.length); |
| 269 | + tpm_buf_destroy(&template); |
| 270 | + |
| 271 | + /* outside info (empty) */ |
| 272 | + tpm_buf_append_u16(&buf, 0); |
| 273 | + |
| 274 | + /* creation PCR (none) */ |
| 275 | + tpm_buf_append_u32(&buf, 0); |
| 276 | + |
| 277 | + rc = tpm_transmit_cmd(chip, &buf, 0, |
| 278 | + "attempting to create NULL primary"); |
| 279 | + |
| 280 | + if (rc == TPM2_RC_SUCCESS) |
| 281 | + rc = tpm2_parse_create_primary(chip, &buf, handle, hierarchy); |
| 282 | + |
| 283 | + tpm_buf_destroy(&buf); |
| 284 | + |
| 285 | + return rc; |
| 286 | +} |
| 287 | + |
| 288 | +static int tpm2_create_null_primary(struct tpm_chip *chip) |
| 289 | +{ |
| 290 | + u32 null_key; |
| 291 | + int rc; |
| 292 | + |
| 293 | + rc = tpm2_create_primary(chip, TPM2_RH_NULL, &null_key); |
| 294 | + |
| 295 | + if (rc == TPM2_RC_SUCCESS) { |
| 296 | + unsigned int offset = 0; /* dummy offset for null key context */ |
| 297 | + |
| 298 | + rc = tpm2_save_context(chip, null_key, chip->null_key_context, |
| 299 | + sizeof(chip->null_key_context), &offset); |
| 300 | + tpm2_flush_context(chip, null_key); |
| 301 | + } |
| 302 | + |
| 303 | + return rc; |
| 304 | +} |
| 305 | + |
| 306 | +/** |
| 307 | + * tpm2_sessions_init() - start of day initialization for the sessions code |
| 308 | + * @chip: TPM chip |
| 309 | + * |
| 310 | + * Derive and context save the null primary and allocate memory in the |
| 311 | + * struct tpm_chip for the authorizations. |
| 312 | + */ |
| 313 | +int tpm2_sessions_init(struct tpm_chip *chip) |
| 314 | +{ |
| 315 | + int rc; |
| 316 | + |
| 317 | + rc = tpm2_create_null_primary(chip); |
| 318 | + if (rc) |
| 319 | + dev_err(&chip->dev, "TPM: security failed (NULL seed derivation): %d\n", rc); |
| 320 | + |
| 321 | + return rc; |
| 322 | +} |
0 commit comments