Mimi Zohar | d00a1c7 | 2010-11-23 17:50:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 IBM Corporation |
| 3 | * |
| 4 | * Author: |
| 5 | * David Safford <safford@us.ibm.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation, version 2 of the License. |
| 10 | * |
| 11 | * See Documentation/keys-trusted-encrypted.txt |
| 12 | */ |
| 13 | |
| 14 | #include <linux/uaccess.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/parser.h> |
| 19 | #include <linux/string.h> |
Mimi Zohar | 93ae86e | 2010-11-29 16:20:04 -0500 | [diff] [blame^] | 20 | #include <linux/err.h> |
Mimi Zohar | d00a1c7 | 2010-11-23 17:50:34 -0500 | [diff] [blame] | 21 | #include <keys/user-type.h> |
| 22 | #include <keys/trusted-type.h> |
| 23 | #include <linux/key-type.h> |
| 24 | #include <linux/rcupdate.h> |
| 25 | #include <linux/crypto.h> |
| 26 | #include <crypto/hash.h> |
| 27 | #include <crypto/sha.h> |
| 28 | #include <linux/capability.h> |
| 29 | #include <linux/tpm.h> |
| 30 | #include <linux/tpm_command.h> |
| 31 | |
| 32 | #include "trusted_defined.h" |
| 33 | |
| 34 | static const char hmac_alg[] = "hmac(sha1)"; |
| 35 | static const char hash_alg[] = "sha1"; |
| 36 | |
| 37 | struct sdesc { |
| 38 | struct shash_desc shash; |
| 39 | char ctx[]; |
| 40 | }; |
| 41 | |
| 42 | static struct crypto_shash *hashalg; |
| 43 | static struct crypto_shash *hmacalg; |
| 44 | |
| 45 | static struct sdesc *init_sdesc(struct crypto_shash *alg) |
| 46 | { |
| 47 | struct sdesc *sdesc; |
| 48 | int size; |
| 49 | |
| 50 | size = sizeof(struct shash_desc) + crypto_shash_descsize(alg); |
| 51 | sdesc = kmalloc(size, GFP_KERNEL); |
| 52 | if (!sdesc) |
| 53 | return ERR_PTR(-ENOMEM); |
| 54 | sdesc->shash.tfm = alg; |
| 55 | sdesc->shash.flags = 0x0; |
| 56 | return sdesc; |
| 57 | } |
| 58 | |
| 59 | static int TSS_sha1(const unsigned char *data, const unsigned int datalen, |
| 60 | unsigned char *digest) |
| 61 | { |
| 62 | struct sdesc *sdesc; |
| 63 | int ret; |
| 64 | |
| 65 | sdesc = init_sdesc(hashalg); |
| 66 | if (IS_ERR(sdesc)) { |
| 67 | pr_info("trusted_key: can't alloc %s\n", hash_alg); |
| 68 | return PTR_ERR(sdesc); |
| 69 | } |
| 70 | |
| 71 | ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest); |
| 72 | kfree(sdesc); |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | static int TSS_rawhmac(unsigned char *digest, const unsigned char *key, |
| 77 | const unsigned int keylen, ...) |
| 78 | { |
| 79 | struct sdesc *sdesc; |
| 80 | va_list argp; |
| 81 | unsigned int dlen; |
| 82 | unsigned char *data; |
| 83 | int ret; |
| 84 | |
| 85 | sdesc = init_sdesc(hmacalg); |
| 86 | if (IS_ERR(sdesc)) { |
| 87 | pr_info("trusted_key: can't alloc %s\n", hmac_alg); |
| 88 | return PTR_ERR(sdesc); |
| 89 | } |
| 90 | |
| 91 | ret = crypto_shash_setkey(hmacalg, key, keylen); |
| 92 | if (ret < 0) |
| 93 | goto out; |
| 94 | ret = crypto_shash_init(&sdesc->shash); |
| 95 | if (ret < 0) |
| 96 | goto out; |
| 97 | |
| 98 | va_start(argp, keylen); |
| 99 | for (;;) { |
| 100 | dlen = va_arg(argp, unsigned int); |
| 101 | if (dlen == 0) |
| 102 | break; |
| 103 | data = va_arg(argp, unsigned char *); |
| 104 | if (data == NULL) |
| 105 | return -EINVAL; |
| 106 | ret = crypto_shash_update(&sdesc->shash, data, dlen); |
| 107 | if (ret < 0) |
| 108 | goto out; |
| 109 | } |
| 110 | va_end(argp); |
| 111 | ret = crypto_shash_final(&sdesc->shash, digest); |
| 112 | out: |
| 113 | kfree(sdesc); |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * calculate authorization info fields to send to TPM |
| 119 | */ |
| 120 | static uint32_t TSS_authhmac(unsigned char *digest, const unsigned char *key, |
| 121 | const unsigned int keylen, unsigned char *h1, |
| 122 | unsigned char *h2, unsigned char h3, ...) |
| 123 | { |
| 124 | unsigned char paramdigest[SHA1_DIGEST_SIZE]; |
| 125 | struct sdesc *sdesc; |
| 126 | unsigned int dlen; |
| 127 | unsigned char *data; |
| 128 | unsigned char c; |
| 129 | int ret; |
| 130 | va_list argp; |
| 131 | |
| 132 | sdesc = init_sdesc(hashalg); |
| 133 | if (IS_ERR(sdesc)) { |
| 134 | pr_info("trusted_key: can't alloc %s\n", hash_alg); |
| 135 | return PTR_ERR(sdesc); |
| 136 | } |
| 137 | |
| 138 | c = h3; |
| 139 | ret = crypto_shash_init(&sdesc->shash); |
| 140 | if (ret < 0) |
| 141 | goto out; |
| 142 | va_start(argp, h3); |
| 143 | for (;;) { |
| 144 | dlen = va_arg(argp, unsigned int); |
| 145 | if (dlen == 0) |
| 146 | break; |
| 147 | data = va_arg(argp, unsigned char *); |
| 148 | ret = crypto_shash_update(&sdesc->shash, data, dlen); |
| 149 | if (ret < 0) |
| 150 | goto out; |
| 151 | } |
| 152 | va_end(argp); |
| 153 | ret = crypto_shash_final(&sdesc->shash, paramdigest); |
| 154 | if (!ret) |
| 155 | TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE, |
| 156 | paramdigest, TPM_NONCE_SIZE, h1, |
| 157 | TPM_NONCE_SIZE, h2, 1, &c, 0, 0); |
| 158 | out: |
| 159 | kfree(sdesc); |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * verify the AUTH1_COMMAND (Seal) result from TPM |
| 165 | */ |
| 166 | static uint32_t TSS_checkhmac1(unsigned char *buffer, |
| 167 | const uint32_t command, |
| 168 | const unsigned char *ononce, |
| 169 | const unsigned char *key, |
| 170 | const unsigned int keylen, ...) |
| 171 | { |
| 172 | uint32_t bufsize; |
| 173 | uint16_t tag; |
| 174 | uint32_t ordinal; |
| 175 | uint32_t result; |
| 176 | unsigned char *enonce; |
| 177 | unsigned char *continueflag; |
| 178 | unsigned char *authdata; |
| 179 | unsigned char testhmac[SHA1_DIGEST_SIZE]; |
| 180 | unsigned char paramdigest[SHA1_DIGEST_SIZE]; |
| 181 | struct sdesc *sdesc; |
| 182 | unsigned int dlen; |
| 183 | unsigned int dpos; |
| 184 | va_list argp; |
| 185 | int ret; |
| 186 | |
| 187 | bufsize = LOAD32(buffer, TPM_SIZE_OFFSET); |
| 188 | tag = LOAD16(buffer, 0); |
| 189 | ordinal = command; |
| 190 | result = LOAD32N(buffer, TPM_RETURN_OFFSET); |
| 191 | if (tag == TPM_TAG_RSP_COMMAND) |
| 192 | return 0; |
| 193 | if (tag != TPM_TAG_RSP_AUTH1_COMMAND) |
| 194 | return -EINVAL; |
| 195 | authdata = buffer + bufsize - SHA1_DIGEST_SIZE; |
| 196 | continueflag = authdata - 1; |
| 197 | enonce = continueflag - TPM_NONCE_SIZE; |
| 198 | |
| 199 | sdesc = init_sdesc(hashalg); |
| 200 | if (IS_ERR(sdesc)) { |
| 201 | pr_info("trusted_key: can't alloc %s\n", hash_alg); |
| 202 | return PTR_ERR(sdesc); |
| 203 | } |
| 204 | ret = crypto_shash_init(&sdesc->shash); |
| 205 | if (ret < 0) |
| 206 | goto out; |
| 207 | ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result, |
| 208 | sizeof result); |
| 209 | if (ret < 0) |
| 210 | goto out; |
| 211 | ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal, |
| 212 | sizeof ordinal); |
| 213 | if (ret < 0) |
| 214 | goto out; |
| 215 | va_start(argp, keylen); |
| 216 | for (;;) { |
| 217 | dlen = va_arg(argp, unsigned int); |
| 218 | if (dlen == 0) |
| 219 | break; |
| 220 | dpos = va_arg(argp, unsigned int); |
| 221 | ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen); |
| 222 | if (ret < 0) |
| 223 | goto out; |
| 224 | } |
| 225 | va_end(argp); |
| 226 | ret = crypto_shash_final(&sdesc->shash, paramdigest); |
| 227 | if (ret < 0) |
| 228 | goto out; |
| 229 | ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest, |
| 230 | TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce, |
| 231 | 1, continueflag, 0, 0); |
| 232 | if (ret < 0) |
| 233 | goto out; |
| 234 | if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE)) |
| 235 | ret = -EINVAL; |
| 236 | out: |
| 237 | kfree(sdesc); |
| 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * verify the AUTH2_COMMAND (unseal) result from TPM |
| 243 | */ |
| 244 | static uint32_t TSS_checkhmac2(unsigned char *buffer, |
| 245 | const uint32_t command, |
| 246 | const unsigned char *ononce, |
| 247 | const unsigned char *key1, |
| 248 | const unsigned int keylen1, |
| 249 | const unsigned char *key2, |
| 250 | const unsigned int keylen2, ...) |
| 251 | { |
| 252 | uint32_t bufsize; |
| 253 | uint16_t tag; |
| 254 | uint32_t ordinal; |
| 255 | uint32_t result; |
| 256 | unsigned char *enonce1; |
| 257 | unsigned char *continueflag1; |
| 258 | unsigned char *authdata1; |
| 259 | unsigned char *enonce2; |
| 260 | unsigned char *continueflag2; |
| 261 | unsigned char *authdata2; |
| 262 | unsigned char testhmac1[SHA1_DIGEST_SIZE]; |
| 263 | unsigned char testhmac2[SHA1_DIGEST_SIZE]; |
| 264 | unsigned char paramdigest[SHA1_DIGEST_SIZE]; |
| 265 | struct sdesc *sdesc; |
| 266 | unsigned int dlen; |
| 267 | unsigned int dpos; |
| 268 | va_list argp; |
| 269 | int ret; |
| 270 | |
| 271 | bufsize = LOAD32(buffer, TPM_SIZE_OFFSET); |
| 272 | tag = LOAD16(buffer, 0); |
| 273 | ordinal = command; |
| 274 | result = LOAD32N(buffer, TPM_RETURN_OFFSET); |
| 275 | |
| 276 | if (tag == TPM_TAG_RSP_COMMAND) |
| 277 | return 0; |
| 278 | if (tag != TPM_TAG_RSP_AUTH2_COMMAND) |
| 279 | return -EINVAL; |
| 280 | authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1 |
| 281 | + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE); |
| 282 | authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE); |
| 283 | continueflag1 = authdata1 - 1; |
| 284 | continueflag2 = authdata2 - 1; |
| 285 | enonce1 = continueflag1 - TPM_NONCE_SIZE; |
| 286 | enonce2 = continueflag2 - TPM_NONCE_SIZE; |
| 287 | |
| 288 | sdesc = init_sdesc(hashalg); |
| 289 | if (IS_ERR(sdesc)) { |
| 290 | pr_info("trusted_key: can't alloc %s\n", hash_alg); |
| 291 | return PTR_ERR(sdesc); |
| 292 | } |
| 293 | ret = crypto_shash_init(&sdesc->shash); |
| 294 | if (ret < 0) |
| 295 | goto out; |
| 296 | ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result, |
| 297 | sizeof result); |
| 298 | if (ret < 0) |
| 299 | goto out; |
| 300 | ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal, |
| 301 | sizeof ordinal); |
| 302 | if (ret < 0) |
| 303 | goto out; |
| 304 | |
| 305 | va_start(argp, keylen2); |
| 306 | for (;;) { |
| 307 | dlen = va_arg(argp, unsigned int); |
| 308 | if (dlen == 0) |
| 309 | break; |
| 310 | dpos = va_arg(argp, unsigned int); |
| 311 | ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen); |
| 312 | if (ret < 0) |
| 313 | goto out; |
| 314 | } |
| 315 | ret = crypto_shash_final(&sdesc->shash, paramdigest); |
| 316 | if (ret < 0) |
| 317 | goto out; |
| 318 | |
| 319 | ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE, |
| 320 | paramdigest, TPM_NONCE_SIZE, enonce1, |
| 321 | TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0); |
| 322 | if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) { |
| 323 | ret = -EINVAL; |
| 324 | goto out; |
| 325 | } |
| 326 | ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE, |
| 327 | paramdigest, TPM_NONCE_SIZE, enonce2, |
| 328 | TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0); |
| 329 | if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE)) |
| 330 | ret = -EINVAL; |
| 331 | out: |
| 332 | kfree(sdesc); |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * For key specific tpm requests, we will generate and send our |
| 338 | * own TPM command packets using the drivers send function. |
| 339 | */ |
| 340 | static int trusted_tpm_send(const u32 chip_num, unsigned char *cmd, |
| 341 | size_t buflen) |
| 342 | { |
| 343 | int rc; |
| 344 | |
| 345 | dump_tpm_buf(cmd); |
| 346 | rc = tpm_send(chip_num, cmd, buflen); |
| 347 | dump_tpm_buf(cmd); |
| 348 | if (rc > 0) |
| 349 | /* Can't return positive return codes values to keyctl */ |
| 350 | rc = -EPERM; |
| 351 | return rc; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * get a random value from TPM |
| 356 | */ |
| 357 | static int tpm_get_random(struct tpm_buf *tb, unsigned char *buf, uint32_t len) |
| 358 | { |
| 359 | int ret; |
| 360 | |
| 361 | INIT_BUF(tb); |
| 362 | store16(tb, TPM_TAG_RQU_COMMAND); |
| 363 | store32(tb, TPM_GETRANDOM_SIZE); |
| 364 | store32(tb, TPM_ORD_GETRANDOM); |
| 365 | store32(tb, len); |
| 366 | ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, sizeof tb->data); |
| 367 | memcpy(buf, tb->data + TPM_GETRANDOM_SIZE, len); |
| 368 | |
| 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | static int my_get_random(unsigned char *buf, int len) |
| 373 | { |
| 374 | struct tpm_buf *tb; |
| 375 | int ret; |
| 376 | |
| 377 | tb = kzalloc(sizeof *tb, GFP_KERNEL); |
| 378 | if (!tb) |
| 379 | return -ENOMEM; |
| 380 | ret = tpm_get_random(tb, buf, len); |
| 381 | |
| 382 | kfree(tb); |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Lock a trusted key, by extending a selected PCR. |
| 388 | * |
| 389 | * Prevents a trusted key that is sealed to PCRs from being accessed. |
| 390 | * This uses the tpm driver's extend function. |
| 391 | */ |
| 392 | static int pcrlock(const int pcrnum) |
| 393 | { |
| 394 | unsigned char hash[SHA1_DIGEST_SIZE]; |
| 395 | |
| 396 | if (!capable(CAP_SYS_ADMIN)) |
| 397 | return -EPERM; |
| 398 | my_get_random(hash, SHA1_DIGEST_SIZE); |
| 399 | return tpm_pcr_extend(TPM_ANY_NUM, pcrnum, hash) ? -EINVAL : 0; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * Create an object specific authorisation protocol (OSAP) session |
| 404 | */ |
| 405 | static int osap(struct tpm_buf *tb, struct osapsess *s, |
| 406 | const unsigned char *key, const uint16_t type, |
| 407 | const uint32_t handle) |
| 408 | { |
| 409 | unsigned char enonce[TPM_NONCE_SIZE]; |
| 410 | unsigned char ononce[TPM_NONCE_SIZE]; |
| 411 | int ret; |
| 412 | |
| 413 | ret = tpm_get_random(tb, ononce, TPM_NONCE_SIZE); |
| 414 | if (ret < 0) |
| 415 | return ret; |
| 416 | |
| 417 | INIT_BUF(tb); |
| 418 | store16(tb, TPM_TAG_RQU_COMMAND); |
| 419 | store32(tb, TPM_OSAP_SIZE); |
| 420 | store32(tb, TPM_ORD_OSAP); |
| 421 | store16(tb, type); |
| 422 | store32(tb, handle); |
| 423 | storebytes(tb, ononce, TPM_NONCE_SIZE); |
| 424 | |
| 425 | ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE); |
| 426 | if (ret < 0) |
| 427 | return ret; |
| 428 | |
| 429 | s->handle = LOAD32(tb->data, TPM_DATA_OFFSET); |
| 430 | memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]), |
| 431 | TPM_NONCE_SIZE); |
| 432 | memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) + |
| 433 | TPM_NONCE_SIZE]), TPM_NONCE_SIZE); |
| 434 | ret = TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE, |
| 435 | enonce, TPM_NONCE_SIZE, ononce, 0, 0); |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Create an object independent authorisation protocol (oiap) session |
| 441 | */ |
| 442 | static int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce) |
| 443 | { |
| 444 | int ret; |
| 445 | |
| 446 | INIT_BUF(tb); |
| 447 | store16(tb, TPM_TAG_RQU_COMMAND); |
| 448 | store32(tb, TPM_OIAP_SIZE); |
| 449 | store32(tb, TPM_ORD_OIAP); |
| 450 | ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE); |
| 451 | if (ret < 0) |
| 452 | return ret; |
| 453 | |
| 454 | *handle = LOAD32(tb->data, TPM_DATA_OFFSET); |
| 455 | memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)], |
| 456 | TPM_NONCE_SIZE); |
| 457 | return ret; |
| 458 | } |
| 459 | |
| 460 | struct tpm_digests { |
| 461 | unsigned char encauth[SHA1_DIGEST_SIZE]; |
| 462 | unsigned char pubauth[SHA1_DIGEST_SIZE]; |
| 463 | unsigned char xorwork[SHA1_DIGEST_SIZE * 2]; |
| 464 | unsigned char xorhash[SHA1_DIGEST_SIZE]; |
| 465 | unsigned char nonceodd[TPM_NONCE_SIZE]; |
| 466 | }; |
| 467 | |
| 468 | /* |
| 469 | * Have the TPM seal(encrypt) the trusted key, possibly based on |
| 470 | * Platform Configuration Registers (PCRs). AUTH1 for sealing key. |
| 471 | */ |
| 472 | static int tpm_seal(struct tpm_buf *tb, const uint16_t keytype, |
| 473 | const uint32_t keyhandle, const unsigned char *keyauth, |
| 474 | const unsigned char *data, const uint32_t datalen, |
| 475 | unsigned char *blob, uint32_t *bloblen, |
| 476 | const unsigned char *blobauth, |
| 477 | const unsigned char *pcrinfo, const uint32_t pcrinfosize) |
| 478 | { |
| 479 | struct osapsess sess; |
| 480 | struct tpm_digests *td; |
| 481 | unsigned char cont; |
| 482 | uint32_t ordinal; |
| 483 | uint32_t pcrsize; |
| 484 | uint32_t datsize; |
| 485 | int sealinfosize; |
| 486 | int encdatasize; |
| 487 | int storedsize; |
| 488 | int ret; |
| 489 | int i; |
| 490 | |
| 491 | /* alloc some work space for all the hashes */ |
| 492 | td = kmalloc(sizeof *td, GFP_KERNEL); |
| 493 | if (!td) |
| 494 | return -ENOMEM; |
| 495 | |
| 496 | /* get session for sealing key */ |
| 497 | ret = osap(tb, &sess, keyauth, keytype, keyhandle); |
| 498 | if (ret < 0) |
| 499 | return ret; |
| 500 | dump_sess(&sess); |
| 501 | |
| 502 | /* calculate encrypted authorization value */ |
| 503 | memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE); |
| 504 | memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE); |
| 505 | ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash); |
| 506 | if (ret < 0) |
| 507 | return ret; |
| 508 | |
| 509 | ret = tpm_get_random(tb, td->nonceodd, TPM_NONCE_SIZE); |
| 510 | if (ret < 0) |
| 511 | return ret; |
| 512 | ordinal = htonl(TPM_ORD_SEAL); |
| 513 | datsize = htonl(datalen); |
| 514 | pcrsize = htonl(pcrinfosize); |
| 515 | cont = 0; |
| 516 | |
| 517 | /* encrypt data authorization key */ |
| 518 | for (i = 0; i < SHA1_DIGEST_SIZE; ++i) |
| 519 | td->encauth[i] = td->xorhash[i] ^ blobauth[i]; |
| 520 | |
| 521 | /* calculate authorization HMAC value */ |
| 522 | if (pcrinfosize == 0) { |
| 523 | /* no pcr info specified */ |
| 524 | TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE, |
| 525 | sess.enonce, td->nonceodd, cont, sizeof(uint32_t), |
| 526 | &ordinal, SHA1_DIGEST_SIZE, td->encauth, |
| 527 | sizeof(uint32_t), &pcrsize, sizeof(uint32_t), |
| 528 | &datsize, datalen, data, 0, 0); |
| 529 | } else { |
| 530 | /* pcr info specified */ |
| 531 | TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE, |
| 532 | sess.enonce, td->nonceodd, cont, sizeof(uint32_t), |
| 533 | &ordinal, SHA1_DIGEST_SIZE, td->encauth, |
| 534 | sizeof(uint32_t), &pcrsize, pcrinfosize, |
| 535 | pcrinfo, sizeof(uint32_t), &datsize, datalen, |
| 536 | data, 0, 0); |
| 537 | } |
| 538 | |
| 539 | /* build and send the TPM request packet */ |
| 540 | INIT_BUF(tb); |
| 541 | store16(tb, TPM_TAG_RQU_AUTH1_COMMAND); |
| 542 | store32(tb, TPM_SEAL_SIZE + pcrinfosize + datalen); |
| 543 | store32(tb, TPM_ORD_SEAL); |
| 544 | store32(tb, keyhandle); |
| 545 | storebytes(tb, td->encauth, SHA1_DIGEST_SIZE); |
| 546 | store32(tb, pcrinfosize); |
| 547 | storebytes(tb, pcrinfo, pcrinfosize); |
| 548 | store32(tb, datalen); |
| 549 | storebytes(tb, data, datalen); |
| 550 | store32(tb, sess.handle); |
| 551 | storebytes(tb, td->nonceodd, TPM_NONCE_SIZE); |
| 552 | store8(tb, cont); |
| 553 | storebytes(tb, td->pubauth, SHA1_DIGEST_SIZE); |
| 554 | |
| 555 | ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE); |
| 556 | if (ret < 0) |
| 557 | return ret; |
| 558 | |
| 559 | /* calculate the size of the returned Blob */ |
| 560 | sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t)); |
| 561 | encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) + |
| 562 | sizeof(uint32_t) + sealinfosize); |
| 563 | storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize + |
| 564 | sizeof(uint32_t) + encdatasize; |
| 565 | |
| 566 | /* check the HMAC in the response */ |
| 567 | ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret, |
| 568 | SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0, |
| 569 | 0); |
| 570 | |
| 571 | /* copy the returned blob to caller */ |
| 572 | memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize); |
| 573 | *bloblen = storedsize; |
| 574 | return ret; |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * use the AUTH2_COMMAND form of unseal, to authorize both key and blob |
| 579 | */ |
| 580 | static int tpm_unseal(struct tpm_buf *tb, |
| 581 | const uint32_t keyhandle, const unsigned char *keyauth, |
| 582 | const unsigned char *blob, const int bloblen, |
| 583 | const unsigned char *blobauth, |
| 584 | unsigned char *data, unsigned int *datalen) |
| 585 | { |
| 586 | unsigned char nonceodd[TPM_NONCE_SIZE]; |
| 587 | unsigned char enonce1[TPM_NONCE_SIZE]; |
| 588 | unsigned char enonce2[TPM_NONCE_SIZE]; |
| 589 | unsigned char authdata1[SHA1_DIGEST_SIZE]; |
| 590 | unsigned char authdata2[SHA1_DIGEST_SIZE]; |
| 591 | uint32_t authhandle1 = 0; |
| 592 | uint32_t authhandle2 = 0; |
| 593 | unsigned char cont = 0; |
| 594 | uint32_t ordinal; |
| 595 | uint32_t keyhndl; |
| 596 | int ret; |
| 597 | |
| 598 | /* sessions for unsealing key and data */ |
| 599 | ret = oiap(tb, &authhandle1, enonce1); |
| 600 | if (ret < 0) { |
| 601 | pr_info("trusted_key: oiap failed (%d)\n", ret); |
| 602 | return ret; |
| 603 | } |
| 604 | ret = oiap(tb, &authhandle2, enonce2); |
| 605 | if (ret < 0) { |
| 606 | pr_info("trusted_key: oiap failed (%d)\n", ret); |
| 607 | return ret; |
| 608 | } |
| 609 | |
| 610 | ordinal = htonl(TPM_ORD_UNSEAL); |
| 611 | keyhndl = htonl(SRKHANDLE); |
| 612 | ret = tpm_get_random(tb, nonceodd, TPM_NONCE_SIZE); |
| 613 | if (ret < 0) { |
| 614 | pr_info("trusted_key: tpm_get_random failed (%d)\n", ret); |
| 615 | return ret; |
| 616 | } |
| 617 | TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE, |
| 618 | enonce1, nonceodd, cont, sizeof(uint32_t), |
| 619 | &ordinal, bloblen, blob, 0, 0); |
| 620 | TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE, |
| 621 | enonce2, nonceodd, cont, sizeof(uint32_t), |
| 622 | &ordinal, bloblen, blob, 0, 0); |
| 623 | |
| 624 | /* build and send TPM request packet */ |
| 625 | INIT_BUF(tb); |
| 626 | store16(tb, TPM_TAG_RQU_AUTH2_COMMAND); |
| 627 | store32(tb, TPM_UNSEAL_SIZE + bloblen); |
| 628 | store32(tb, TPM_ORD_UNSEAL); |
| 629 | store32(tb, keyhandle); |
| 630 | storebytes(tb, blob, bloblen); |
| 631 | store32(tb, authhandle1); |
| 632 | storebytes(tb, nonceodd, TPM_NONCE_SIZE); |
| 633 | store8(tb, cont); |
| 634 | storebytes(tb, authdata1, SHA1_DIGEST_SIZE); |
| 635 | store32(tb, authhandle2); |
| 636 | storebytes(tb, nonceodd, TPM_NONCE_SIZE); |
| 637 | store8(tb, cont); |
| 638 | storebytes(tb, authdata2, SHA1_DIGEST_SIZE); |
| 639 | |
| 640 | ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE); |
| 641 | if (ret < 0) { |
| 642 | pr_info("trusted_key: authhmac failed (%d)\n", ret); |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | *datalen = LOAD32(tb->data, TPM_DATA_OFFSET); |
| 647 | ret = TSS_checkhmac2(tb->data, ordinal, nonceodd, |
| 648 | keyauth, SHA1_DIGEST_SIZE, |
| 649 | blobauth, SHA1_DIGEST_SIZE, |
| 650 | sizeof(uint32_t), TPM_DATA_OFFSET, |
| 651 | *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0, |
| 652 | 0); |
| 653 | if (ret < 0) |
| 654 | pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret); |
| 655 | memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen); |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | * Have the TPM seal(encrypt) the symmetric key |
| 661 | */ |
| 662 | static int key_seal(struct trusted_key_payload *p, |
| 663 | struct trusted_key_options *o) |
| 664 | { |
| 665 | struct tpm_buf *tb; |
| 666 | int ret; |
| 667 | |
| 668 | tb = kzalloc(sizeof *tb, GFP_KERNEL); |
| 669 | if (!tb) |
| 670 | return -ENOMEM; |
| 671 | |
| 672 | /* include migratable flag at end of sealed key */ |
| 673 | p->key[p->key_len] = p->migratable; |
| 674 | |
| 675 | ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth, |
| 676 | p->key, p->key_len + 1, p->blob, &p->blob_len, |
| 677 | o->blobauth, o->pcrinfo, o->pcrinfo_len); |
| 678 | if (ret < 0) |
| 679 | pr_info("trusted_key: srkseal failed (%d)\n", ret); |
| 680 | |
| 681 | kfree(tb); |
| 682 | return ret; |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Have the TPM unseal(decrypt) the symmetric key |
| 687 | */ |
| 688 | static int key_unseal(struct trusted_key_payload *p, |
| 689 | struct trusted_key_options *o) |
| 690 | { |
| 691 | struct tpm_buf *tb; |
| 692 | int ret; |
| 693 | |
| 694 | tb = kzalloc(sizeof *tb, GFP_KERNEL); |
| 695 | if (!tb) |
| 696 | return -ENOMEM; |
| 697 | |
| 698 | ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len, |
| 699 | o->blobauth, p->key, &p->key_len); |
| 700 | /* pull migratable flag out of sealed key */ |
| 701 | p->migratable = p->key[--p->key_len]; |
| 702 | |
| 703 | if (ret < 0) |
| 704 | pr_info("trusted_key: srkunseal failed (%d)\n", ret); |
| 705 | |
| 706 | kfree(tb); |
| 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | enum { |
| 711 | Opt_err = -1, |
| 712 | Opt_new, Opt_load, Opt_update, |
| 713 | Opt_keyhandle, Opt_keyauth, Opt_blobauth, |
| 714 | Opt_pcrinfo, Opt_pcrlock, Opt_migratable |
| 715 | }; |
| 716 | |
| 717 | static const match_table_t key_tokens = { |
| 718 | {Opt_new, "new"}, |
| 719 | {Opt_load, "load"}, |
| 720 | {Opt_update, "update"}, |
| 721 | {Opt_keyhandle, "keyhandle=%s"}, |
| 722 | {Opt_keyauth, "keyauth=%s"}, |
| 723 | {Opt_blobauth, "blobauth=%s"}, |
| 724 | {Opt_pcrinfo, "pcrinfo=%s"}, |
| 725 | {Opt_pcrlock, "pcrlock=%s"}, |
| 726 | {Opt_migratable, "migratable=%s"}, |
| 727 | {Opt_err, NULL} |
| 728 | }; |
| 729 | |
| 730 | /* can have zero or more token= options */ |
| 731 | static int getoptions(char *c, struct trusted_key_payload *pay, |
| 732 | struct trusted_key_options *opt) |
| 733 | { |
| 734 | substring_t args[MAX_OPT_ARGS]; |
| 735 | char *p = c; |
| 736 | int token; |
| 737 | int res; |
| 738 | unsigned long handle; |
| 739 | unsigned long lock; |
| 740 | |
| 741 | while ((p = strsep(&c, " \t"))) { |
| 742 | if (*p == '\0' || *p == ' ' || *p == '\t') |
| 743 | continue; |
| 744 | token = match_token(p, key_tokens, args); |
| 745 | |
| 746 | switch (token) { |
| 747 | case Opt_pcrinfo: |
| 748 | opt->pcrinfo_len = strlen(args[0].from) / 2; |
| 749 | if (opt->pcrinfo_len > MAX_PCRINFO_SIZE) |
| 750 | return -EINVAL; |
| 751 | hex2bin(opt->pcrinfo, args[0].from, opt->pcrinfo_len); |
| 752 | break; |
| 753 | case Opt_keyhandle: |
| 754 | res = strict_strtoul(args[0].from, 16, &handle); |
| 755 | if (res < 0) |
| 756 | return -EINVAL; |
| 757 | opt->keytype = SEAL_keytype; |
| 758 | opt->keyhandle = handle; |
| 759 | break; |
| 760 | case Opt_keyauth: |
| 761 | if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE) |
| 762 | return -EINVAL; |
| 763 | hex2bin(opt->keyauth, args[0].from, SHA1_DIGEST_SIZE); |
| 764 | break; |
| 765 | case Opt_blobauth: |
| 766 | if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE) |
| 767 | return -EINVAL; |
| 768 | hex2bin(opt->blobauth, args[0].from, SHA1_DIGEST_SIZE); |
| 769 | break; |
| 770 | case Opt_migratable: |
| 771 | if (*args[0].from == '0') |
| 772 | pay->migratable = 0; |
| 773 | else |
| 774 | return -EINVAL; |
| 775 | break; |
| 776 | case Opt_pcrlock: |
| 777 | res = strict_strtoul(args[0].from, 10, &lock); |
| 778 | if (res < 0) |
| 779 | return -EINVAL; |
| 780 | opt->pcrlock = lock; |
| 781 | break; |
| 782 | default: |
| 783 | return -EINVAL; |
| 784 | } |
| 785 | } |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | /* |
| 790 | * datablob_parse - parse the keyctl data and fill in the |
| 791 | * payload and options structures |
| 792 | * |
| 793 | * On success returns 0, otherwise -EINVAL. |
| 794 | */ |
| 795 | static int datablob_parse(char *datablob, struct trusted_key_payload *p, |
| 796 | struct trusted_key_options *o) |
| 797 | { |
| 798 | substring_t args[MAX_OPT_ARGS]; |
| 799 | long keylen; |
| 800 | int ret = -EINVAL; |
| 801 | int key_cmd; |
| 802 | char *c; |
| 803 | |
| 804 | /* main command */ |
| 805 | c = strsep(&datablob, " \t"); |
| 806 | if (!c) |
| 807 | return -EINVAL; |
| 808 | key_cmd = match_token(c, key_tokens, args); |
| 809 | switch (key_cmd) { |
| 810 | case Opt_new: |
| 811 | /* first argument is key size */ |
| 812 | c = strsep(&datablob, " \t"); |
| 813 | if (!c) |
| 814 | return -EINVAL; |
| 815 | ret = strict_strtol(c, 10, &keylen); |
| 816 | if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE) |
| 817 | return -EINVAL; |
| 818 | p->key_len = keylen; |
| 819 | ret = getoptions(datablob, p, o); |
| 820 | if (ret < 0) |
| 821 | return ret; |
| 822 | ret = Opt_new; |
| 823 | break; |
| 824 | case Opt_load: |
| 825 | /* first argument is sealed blob */ |
| 826 | c = strsep(&datablob, " \t"); |
| 827 | if (!c) |
| 828 | return -EINVAL; |
| 829 | p->blob_len = strlen(c) / 2; |
| 830 | if (p->blob_len > MAX_BLOB_SIZE) |
| 831 | return -EINVAL; |
| 832 | hex2bin(p->blob, c, p->blob_len); |
| 833 | ret = getoptions(datablob, p, o); |
| 834 | if (ret < 0) |
| 835 | return ret; |
| 836 | ret = Opt_load; |
| 837 | break; |
| 838 | case Opt_update: |
| 839 | /* all arguments are options */ |
| 840 | ret = getoptions(datablob, p, o); |
| 841 | if (ret < 0) |
| 842 | return ret; |
| 843 | ret = Opt_update; |
| 844 | break; |
| 845 | case Opt_err: |
| 846 | return -EINVAL; |
| 847 | break; |
| 848 | } |
| 849 | return ret; |
| 850 | } |
| 851 | |
| 852 | static struct trusted_key_options *trusted_options_alloc(void) |
| 853 | { |
| 854 | struct trusted_key_options *options; |
| 855 | |
| 856 | options = kzalloc(sizeof *options, GFP_KERNEL); |
| 857 | if (!options) |
| 858 | return options; |
| 859 | |
| 860 | /* set any non-zero defaults */ |
| 861 | options->keytype = SRK_keytype; |
| 862 | options->keyhandle = SRKHANDLE; |
| 863 | return options; |
| 864 | } |
| 865 | |
| 866 | static struct trusted_key_payload *trusted_payload_alloc(struct key *key) |
| 867 | { |
| 868 | struct trusted_key_payload *p = NULL; |
| 869 | int ret; |
| 870 | |
| 871 | ret = key_payload_reserve(key, sizeof *p); |
| 872 | if (ret < 0) |
| 873 | return p; |
| 874 | p = kzalloc(sizeof *p, GFP_KERNEL); |
| 875 | |
| 876 | /* migratable by default */ |
| 877 | p->migratable = 1; |
| 878 | return p; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * trusted_instantiate - create a new trusted key |
| 883 | * |
| 884 | * Unseal an existing trusted blob or, for a new key, get a |
| 885 | * random key, then seal and create a trusted key-type key, |
| 886 | * adding it to the specified keyring. |
| 887 | * |
| 888 | * On success, return 0. Otherwise return errno. |
| 889 | */ |
| 890 | static int trusted_instantiate(struct key *key, const void *data, |
| 891 | const size_t datalen) |
| 892 | { |
| 893 | struct trusted_key_payload *payload = NULL; |
| 894 | struct trusted_key_options *options = NULL; |
| 895 | char *datablob; |
| 896 | int ret = 0; |
| 897 | int key_cmd; |
| 898 | |
| 899 | if (datalen <= 0 || datalen > 32767 || !data) |
| 900 | return -EINVAL; |
| 901 | |
| 902 | datablob = kmalloc(datalen + 1, GFP_KERNEL); |
| 903 | if (!datablob) |
| 904 | return -ENOMEM; |
| 905 | memcpy(datablob, data, datalen); |
| 906 | datablob[datalen] = '\0'; |
| 907 | |
| 908 | options = trusted_options_alloc(); |
| 909 | if (!options) { |
| 910 | ret = -ENOMEM; |
| 911 | goto out; |
| 912 | } |
| 913 | payload = trusted_payload_alloc(key); |
| 914 | if (!payload) { |
| 915 | ret = -ENOMEM; |
| 916 | goto out; |
| 917 | } |
| 918 | |
| 919 | key_cmd = datablob_parse(datablob, payload, options); |
| 920 | if (key_cmd < 0) { |
| 921 | ret = key_cmd; |
| 922 | goto out; |
| 923 | } |
| 924 | |
| 925 | dump_payload(payload); |
| 926 | dump_options(options); |
| 927 | |
| 928 | switch (key_cmd) { |
| 929 | case Opt_load: |
| 930 | ret = key_unseal(payload, options); |
| 931 | dump_payload(payload); |
| 932 | dump_options(options); |
| 933 | if (ret < 0) |
| 934 | pr_info("trusted_key: key_unseal failed (%d)\n", ret); |
| 935 | break; |
| 936 | case Opt_new: |
| 937 | ret = my_get_random(payload->key, payload->key_len); |
| 938 | if (ret < 0) { |
| 939 | pr_info("trusted_key: key_create failed (%d)\n", ret); |
| 940 | goto out; |
| 941 | } |
| 942 | ret = key_seal(payload, options); |
| 943 | if (ret < 0) |
| 944 | pr_info("trusted_key: key_seal failed (%d)\n", ret); |
| 945 | break; |
| 946 | default: |
| 947 | ret = -EINVAL; |
| 948 | goto out; |
| 949 | } |
| 950 | if (!ret && options->pcrlock) |
| 951 | ret = pcrlock(options->pcrlock); |
| 952 | out: |
| 953 | kfree(datablob); |
| 954 | kfree(options); |
| 955 | if (!ret) |
| 956 | rcu_assign_pointer(key->payload.data, payload); |
| 957 | else |
| 958 | kfree(payload); |
| 959 | return ret; |
| 960 | } |
| 961 | |
| 962 | static void trusted_rcu_free(struct rcu_head *rcu) |
| 963 | { |
| 964 | struct trusted_key_payload *p; |
| 965 | |
| 966 | p = container_of(rcu, struct trusted_key_payload, rcu); |
| 967 | memset(p->key, 0, p->key_len); |
| 968 | kfree(p); |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | * trusted_update - reseal an existing key with new PCR values |
| 973 | */ |
| 974 | static int trusted_update(struct key *key, const void *data, |
| 975 | const size_t datalen) |
| 976 | { |
| 977 | struct trusted_key_payload *p = key->payload.data; |
| 978 | struct trusted_key_payload *new_p; |
| 979 | struct trusted_key_options *new_o; |
| 980 | char *datablob; |
| 981 | int ret = 0; |
| 982 | |
| 983 | if (!p->migratable) |
| 984 | return -EPERM; |
| 985 | if (datalen <= 0 || datalen > 32767 || !data) |
| 986 | return -EINVAL; |
| 987 | |
| 988 | datablob = kmalloc(datalen + 1, GFP_KERNEL); |
| 989 | if (!datablob) |
| 990 | return -ENOMEM; |
| 991 | new_o = trusted_options_alloc(); |
| 992 | if (!new_o) { |
| 993 | ret = -ENOMEM; |
| 994 | goto out; |
| 995 | } |
| 996 | new_p = trusted_payload_alloc(key); |
| 997 | if (!new_p) { |
| 998 | ret = -ENOMEM; |
| 999 | goto out; |
| 1000 | } |
| 1001 | |
| 1002 | memcpy(datablob, data, datalen); |
| 1003 | datablob[datalen] = '\0'; |
| 1004 | ret = datablob_parse(datablob, new_p, new_o); |
| 1005 | if (ret != Opt_update) { |
| 1006 | ret = -EINVAL; |
| 1007 | goto out; |
| 1008 | } |
| 1009 | /* copy old key values, and reseal with new pcrs */ |
| 1010 | new_p->migratable = p->migratable; |
| 1011 | new_p->key_len = p->key_len; |
| 1012 | memcpy(new_p->key, p->key, p->key_len); |
| 1013 | dump_payload(p); |
| 1014 | dump_payload(new_p); |
| 1015 | |
| 1016 | ret = key_seal(new_p, new_o); |
| 1017 | if (ret < 0) { |
| 1018 | pr_info("trusted_key: key_seal failed (%d)\n", ret); |
| 1019 | kfree(new_p); |
| 1020 | goto out; |
| 1021 | } |
| 1022 | if (new_o->pcrlock) { |
| 1023 | ret = pcrlock(new_o->pcrlock); |
| 1024 | if (ret < 0) { |
| 1025 | pr_info("trusted_key: pcrlock failed (%d)\n", ret); |
| 1026 | kfree(new_p); |
| 1027 | goto out; |
| 1028 | } |
| 1029 | } |
| 1030 | rcu_assign_pointer(key->payload.data, new_p); |
| 1031 | call_rcu(&p->rcu, trusted_rcu_free); |
| 1032 | out: |
| 1033 | kfree(datablob); |
| 1034 | kfree(new_o); |
| 1035 | return ret; |
| 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * trusted_read - copy the sealed blob data to userspace in hex. |
| 1040 | * On success, return to userspace the trusted key datablob size. |
| 1041 | */ |
| 1042 | static long trusted_read(const struct key *key, char __user *buffer, |
| 1043 | size_t buflen) |
| 1044 | { |
| 1045 | struct trusted_key_payload *p; |
| 1046 | char *ascii_buf; |
| 1047 | char *bufp; |
| 1048 | int i; |
| 1049 | |
| 1050 | p = rcu_dereference_protected(key->payload.data, |
| 1051 | rwsem_is_locked(&((struct key *)key)->sem)); |
| 1052 | if (!p) |
| 1053 | return -EINVAL; |
| 1054 | if (!buffer || buflen <= 0) |
| 1055 | return 2 * p->blob_len; |
| 1056 | ascii_buf = kmalloc(2 * p->blob_len, GFP_KERNEL); |
| 1057 | if (!ascii_buf) |
| 1058 | return -ENOMEM; |
| 1059 | |
| 1060 | bufp = ascii_buf; |
| 1061 | for (i = 0; i < p->blob_len; i++) |
| 1062 | bufp = pack_hex_byte(bufp, p->blob[i]); |
| 1063 | if ((copy_to_user(buffer, ascii_buf, 2 * p->blob_len)) != 0) { |
| 1064 | kfree(ascii_buf); |
| 1065 | return -EFAULT; |
| 1066 | } |
| 1067 | kfree(ascii_buf); |
| 1068 | return 2 * p->blob_len; |
| 1069 | } |
| 1070 | |
| 1071 | /* |
| 1072 | * trusted_destroy - before freeing the key, clear the decrypted data |
| 1073 | */ |
| 1074 | static void trusted_destroy(struct key *key) |
| 1075 | { |
| 1076 | struct trusted_key_payload *p = key->payload.data; |
| 1077 | |
| 1078 | if (!p) |
| 1079 | return; |
| 1080 | memset(p->key, 0, p->key_len); |
| 1081 | kfree(key->payload.data); |
| 1082 | } |
| 1083 | |
| 1084 | struct key_type key_type_trusted = { |
| 1085 | .name = "trusted", |
| 1086 | .instantiate = trusted_instantiate, |
| 1087 | .update = trusted_update, |
| 1088 | .match = user_match, |
| 1089 | .destroy = trusted_destroy, |
| 1090 | .describe = user_describe, |
| 1091 | .read = trusted_read, |
| 1092 | }; |
| 1093 | |
| 1094 | EXPORT_SYMBOL_GPL(key_type_trusted); |
| 1095 | |
| 1096 | static void trusted_shash_release(void) |
| 1097 | { |
| 1098 | if (hashalg) |
| 1099 | crypto_free_shash(hashalg); |
| 1100 | if (hmacalg) |
| 1101 | crypto_free_shash(hmacalg); |
| 1102 | } |
| 1103 | |
| 1104 | static int __init trusted_shash_alloc(void) |
| 1105 | { |
| 1106 | int ret; |
| 1107 | |
| 1108 | hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC); |
| 1109 | if (IS_ERR(hmacalg)) { |
| 1110 | pr_info("trusted_key: could not allocate crypto %s\n", |
| 1111 | hmac_alg); |
| 1112 | return PTR_ERR(hmacalg); |
| 1113 | } |
| 1114 | |
| 1115 | hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC); |
| 1116 | if (IS_ERR(hashalg)) { |
| 1117 | pr_info("trusted_key: could not allocate crypto %s\n", |
| 1118 | hash_alg); |
| 1119 | ret = PTR_ERR(hashalg); |
| 1120 | goto hashalg_fail; |
| 1121 | } |
| 1122 | |
| 1123 | return 0; |
| 1124 | |
| 1125 | hashalg_fail: |
| 1126 | crypto_free_shash(hmacalg); |
| 1127 | return ret; |
| 1128 | } |
| 1129 | |
| 1130 | static int __init init_trusted(void) |
| 1131 | { |
| 1132 | int ret; |
| 1133 | |
| 1134 | ret = trusted_shash_alloc(); |
| 1135 | if (ret < 0) |
| 1136 | return ret; |
| 1137 | ret = register_key_type(&key_type_trusted); |
| 1138 | if (ret < 0) |
| 1139 | trusted_shash_release(); |
| 1140 | return ret; |
| 1141 | } |
| 1142 | |
| 1143 | static void __exit cleanup_trusted(void) |
| 1144 | { |
| 1145 | trusted_shash_release(); |
| 1146 | unregister_key_type(&key_type_trusted); |
| 1147 | } |
| 1148 | |
| 1149 | late_initcall(init_trusted); |
| 1150 | module_exit(cleanup_trusted); |
| 1151 | |
| 1152 | MODULE_LICENSE("GPL"); |