Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | // SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2004 IBM Corporation * Copyright (C) 2014 Intel Corporation */ #include <linux/string.h> #include <linux/err.h> #include <linux/tpm.h> #include <linux/tpm_command.h> #include <keys/trusted-type.h> #include <keys/trusted_tpm.h> static struct tpm2_hash tpm2_hash_map[] = { {HASH_ALGO_SHA1, TPM_ALG_SHA1}, {HASH_ALGO_SHA256, TPM_ALG_SHA256}, {HASH_ALGO_SHA384, TPM_ALG_SHA384}, {HASH_ALGO_SHA512, TPM_ALG_SHA512}, {HASH_ALGO_SM3_256, TPM_ALG_SM3_256}, }; /** * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer. * * @buf: an allocated tpm_buf instance * @session_handle: session handle * @nonce: the session nonce, may be NULL if not used * @nonce_len: the session nonce length, may be 0 if not used * @attributes: the session attributes * @hmac: the session HMAC or password, may be NULL if not used * @hmac_len: the session HMAC or password length, maybe 0 if not used */ static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle, const u8 *nonce, u16 nonce_len, u8 attributes, const u8 *hmac, u16 hmac_len) { tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len); tpm_buf_append_u32(buf, session_handle); tpm_buf_append_u16(buf, nonce_len); if (nonce && nonce_len) tpm_buf_append(buf, nonce, nonce_len); tpm_buf_append_u8(buf, attributes); tpm_buf_append_u16(buf, hmac_len); if (hmac && hmac_len) tpm_buf_append(buf, hmac, hmac_len); } /** * tpm2_seal_trusted() - seal the payload of a trusted key * * @chip: TPM chip to use * @payload: the key data in clear and encrypted form * @options: authentication values and other options * * Return: < 0 on error and 0 on success. */ int tpm2_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload, struct trusted_key_options *options) { unsigned int blob_len; struct tpm_buf buf; u32 hash; int i; int rc; for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) { if (options->hash == tpm2_hash_map[i].crypto_id) { hash = tpm2_hash_map[i].tpm_id; break; } } if (i == ARRAY_SIZE(tpm2_hash_map)) return -EINVAL; rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE); if (rc) return rc; tpm_buf_append_u32(&buf, options->keyhandle); tpm2_buf_append_auth(&buf, TPM2_RS_PW, NULL /* nonce */, 0, 0 /* session_attributes */, options->keyauth /* hmac */, TPM_DIGEST_SIZE); /* sensitive */ tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1); tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE); tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE); tpm_buf_append_u16(&buf, payload->key_len + 1); tpm_buf_append(&buf, payload->key, payload->key_len); tpm_buf_append_u8(&buf, payload->migratable); /* public */ tpm_buf_append_u16(&buf, 14 + options->policydigest_len); tpm_buf_append_u16(&buf, TPM_ALG_KEYEDHASH); tpm_buf_append_u16(&buf, hash); /* policy */ if (options->policydigest_len) { tpm_buf_append_u32(&buf, 0); tpm_buf_append_u16(&buf, options->policydigest_len); tpm_buf_append(&buf, options->policydigest, options->policydigest_len); } else { tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH); tpm_buf_append_u16(&buf, 0); } /* public parameters */ tpm_buf_append_u16(&buf, TPM_ALG_NULL); tpm_buf_append_u16(&buf, 0); /* outside info */ tpm_buf_append_u16(&buf, 0); /* creation PCR */ tpm_buf_append_u32(&buf, 0); if (buf.flags & TPM_BUF_OVERFLOW) { rc = -E2BIG; goto out; } rc = tpm_send(chip, buf.data, tpm_buf_length(&buf)); if (rc) goto out; blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]); if (blob_len > MAX_BLOB_SIZE) { rc = -E2BIG; goto out; } if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 4 + blob_len) { rc = -EFAULT; goto out; } memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len); payload->blob_len = blob_len; out: tpm_buf_destroy(&buf); if (rc > 0) { if (tpm2_rc_value(rc) == TPM2_RC_HASH) rc = -EINVAL; else rc = -EPERM; } return rc; } /** * tpm2_load_cmd() - execute a TPM2_Load command * * @chip: TPM chip to use * @payload: the key data in clear and encrypted form * @options: authentication values and other options * @blob_handle: returned blob handle * * Return: 0 on success. * -E2BIG on wrong payload size. * -EPERM on tpm error status. * < 0 error from tpm_send. */ static int tpm2_load_cmd(struct tpm_chip *chip, struct trusted_key_payload *payload, struct trusted_key_options *options, u32 *blob_handle) { struct tpm_buf buf; unsigned int private_len; unsigned int public_len; unsigned int blob_len; int rc; private_len = be16_to_cpup((__be16 *) &payload->blob[0]); if (private_len > (payload->blob_len - 2)) return -E2BIG; public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]); blob_len = private_len + public_len + 4; if (blob_len > payload->blob_len) return -E2BIG; rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD); if (rc) return rc; tpm_buf_append_u32(&buf, options->keyhandle); tpm2_buf_append_auth(&buf, TPM2_RS_PW, NULL /* nonce */, 0, 0 /* session_attributes */, options->keyauth /* hmac */, TPM_DIGEST_SIZE); tpm_buf_append(&buf, payload->blob, blob_len); if (buf.flags & TPM_BUF_OVERFLOW) { rc = -E2BIG; goto out; } rc = tpm_send(chip, buf.data, tpm_buf_length(&buf)); if (!rc) *blob_handle = be32_to_cpup( (__be32 *) &buf.data[TPM_HEADER_SIZE]); out: tpm_buf_destroy(&buf); if (rc > 0) rc = -EPERM; return rc; } /** * tpm2_unseal_cmd() - execute a TPM2_Unload command * * @chip: TPM chip to use * @payload: the key data in clear and encrypted form * @options: authentication values and other options * @blob_handle: blob handle * * Return: 0 on success * -EPERM on tpm error status * < 0 error from tpm_send */ static int tpm2_unseal_cmd(struct tpm_chip *chip, struct trusted_key_payload *payload, struct trusted_key_options *options, u32 blob_handle) { struct tpm_buf buf; u16 data_len; u8 *data; int rc; rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL); if (rc) return rc; tpm_buf_append_u32(&buf, blob_handle); tpm2_buf_append_auth(&buf, options->policyhandle ? options->policyhandle : TPM2_RS_PW, NULL /* nonce */, 0, TPM2_SA_CONTINUE_SESSION, options->blobauth /* hmac */, TPM_DIGEST_SIZE); rc = tpm_send(chip, buf.data, tpm_buf_length(&buf)); if (rc > 0) rc = -EPERM; if (!rc) { data_len = be16_to_cpup( (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]); if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE + 1) { rc = -EFAULT; goto out; } if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len) { rc = -EFAULT; goto out; } data = &buf.data[TPM_HEADER_SIZE + 6]; memcpy(payload->key, data, data_len - 1); payload->key_len = data_len - 1; payload->migratable = data[data_len - 1]; } out: tpm_buf_destroy(&buf); return rc; } /** * tpm2_unseal_trusted() - unseal the payload of a trusted key * * @chip: TPM chip to use * @payload: the key data in clear and encrypted form * @options: authentication values and other options * * Return: Same as with tpm_send. */ int tpm2_unseal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload, struct trusted_key_options *options) { u32 blob_handle; int rc; rc = tpm2_load_cmd(chip, payload, options, &blob_handle); if (rc) return rc; rc = tpm2_unseal_cmd(chip, payload, options, blob_handle); tpm2_flush_context(chip, blob_handle); return rc; } |