blob: d16d893bd1959ca7d69eab2b011faa13c8e410c9 [file] [log] [blame]
Denis Kenzior903be6b2018-10-09 17:48:02 +01001// SPDX-License-Identifier: GPL-2.0
2#define pr_fmt(fmt) "ASYM-TPM: "fmt
3#include <linux/slab.h>
4#include <linux/module.h>
5#include <linux/export.h>
6#include <linux/kernel.h>
7#include <linux/seq_file.h>
8#include <linux/scatterlist.h>
9#include <linux/tpm.h>
Denis Kenzior0c362642018-10-09 17:48:58 +010010#include <linux/tpm_command.h>
Denis Kenziordff5a612018-10-09 17:48:25 +010011#include <crypto/akcipher.h>
Denis Kenzior0c362642018-10-09 17:48:58 +010012#include <crypto/hash.h>
13#include <crypto/sha.h>
Denis Kenziorf8c54e12018-10-09 17:48:10 +010014#include <asm/unaligned.h>
Denis Kenzior903be6b2018-10-09 17:48:02 +010015#include <keys/asymmetric-subtype.h>
Sumit Garg47f9c272019-10-16 10:44:54 +053016#include <keys/trusted_tpm.h>
Denis Kenzior903be6b2018-10-09 17:48:02 +010017#include <crypto/asym_tpm_subtype.h>
Denis Kenziore08e6892018-10-09 17:49:20 +010018#include <crypto/public_key.h>
Denis Kenzior903be6b2018-10-09 17:48:02 +010019
Denis Kenzior0c362642018-10-09 17:48:58 +010020#define TPM_ORD_FLUSHSPECIFIC 186
21#define TPM_ORD_LOADKEY2 65
Denis Kenziorf884fe52018-10-09 17:49:05 +010022#define TPM_ORD_UNBIND 30
Denis Kenziore73d1702018-10-09 17:49:28 +010023#define TPM_ORD_SIGN 60
Denis Kenzior0c362642018-10-09 17:48:58 +010024
25#define TPM_RT_KEY 0x00000001
26
27/*
28 * Load a TPM key from the blob provided by userspace
29 */
Sumit Gargc6f61e52019-10-16 10:44:53 +053030static int tpm_loadkey2(struct tpm_buf *tb,
Denis Kenzior0c362642018-10-09 17:48:58 +010031 uint32_t keyhandle, unsigned char *keyauth,
32 const unsigned char *keyblob, int keybloblen,
33 uint32_t *newhandle)
34{
35 unsigned char nonceodd[TPM_NONCE_SIZE];
36 unsigned char enonce[TPM_NONCE_SIZE];
37 unsigned char authdata[SHA1_DIGEST_SIZE];
38 uint32_t authhandle = 0;
39 unsigned char cont = 0;
40 uint32_t ordinal;
41 int ret;
42
43 ordinal = htonl(TPM_ORD_LOADKEY2);
44
45 /* session for loading the key */
46 ret = oiap(tb, &authhandle, enonce);
47 if (ret < 0) {
48 pr_info("oiap failed (%d)\n", ret);
49 return ret;
50 }
51
52 /* generate odd nonce */
53 ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
54 if (ret < 0) {
55 pr_info("tpm_get_random failed (%d)\n", ret);
56 return ret;
57 }
58
59 /* calculate authorization HMAC value */
60 ret = TSS_authhmac(authdata, keyauth, SHA1_DIGEST_SIZE, enonce,
61 nonceodd, cont, sizeof(uint32_t), &ordinal,
62 keybloblen, keyblob, 0, 0);
63 if (ret < 0)
64 return ret;
65
66 /* build the request buffer */
Sumit Gargc6f61e52019-10-16 10:44:53 +053067 tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_LOADKEY2);
68 tpm_buf_append_u32(tb, keyhandle);
69 tpm_buf_append(tb, keyblob, keybloblen);
70 tpm_buf_append_u32(tb, authhandle);
71 tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
72 tpm_buf_append_u8(tb, cont);
73 tpm_buf_append(tb, authdata, SHA1_DIGEST_SIZE);
Denis Kenzior0c362642018-10-09 17:48:58 +010074
75 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
76 if (ret < 0) {
77 pr_info("authhmac failed (%d)\n", ret);
78 return ret;
79 }
80
81 ret = TSS_checkhmac1(tb->data, ordinal, nonceodd, keyauth,
82 SHA1_DIGEST_SIZE, 0, 0);
83 if (ret < 0) {
84 pr_info("TSS_checkhmac1 failed (%d)\n", ret);
85 return ret;
86 }
87
88 *newhandle = LOAD32(tb->data, TPM_DATA_OFFSET);
89 return 0;
90}
91
92/*
93 * Execute the FlushSpecific TPM command
94 */
Sumit Gargc6f61e52019-10-16 10:44:53 +053095static int tpm_flushspecific(struct tpm_buf *tb, uint32_t handle)
Denis Kenzior0c362642018-10-09 17:48:58 +010096{
Sumit Gargc6f61e52019-10-16 10:44:53 +053097 tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_FLUSHSPECIFIC);
98 tpm_buf_append_u32(tb, handle);
99 tpm_buf_append_u32(tb, TPM_RT_KEY);
Denis Kenzior0c362642018-10-09 17:48:58 +0100100
101 return trusted_tpm_send(tb->data, MAX_BUF_SIZE);
102}
103
Denis Kenzior903be6b2018-10-09 17:48:02 +0100104/*
Denis Kenziorf884fe52018-10-09 17:49:05 +0100105 * Decrypt a blob provided by userspace using a specific key handle.
106 * The handle is a well known handle or previously loaded by e.g. LoadKey2
107 */
Sumit Gargc6f61e52019-10-16 10:44:53 +0530108static int tpm_unbind(struct tpm_buf *tb,
Denis Kenziorf884fe52018-10-09 17:49:05 +0100109 uint32_t keyhandle, unsigned char *keyauth,
110 const unsigned char *blob, uint32_t bloblen,
111 void *out, uint32_t outlen)
112{
113 unsigned char nonceodd[TPM_NONCE_SIZE];
114 unsigned char enonce[TPM_NONCE_SIZE];
115 unsigned char authdata[SHA1_DIGEST_SIZE];
116 uint32_t authhandle = 0;
117 unsigned char cont = 0;
118 uint32_t ordinal;
119 uint32_t datalen;
120 int ret;
121
122 ordinal = htonl(TPM_ORD_UNBIND);
123 datalen = htonl(bloblen);
124
125 /* session for loading the key */
126 ret = oiap(tb, &authhandle, enonce);
127 if (ret < 0) {
128 pr_info("oiap failed (%d)\n", ret);
129 return ret;
130 }
131
132 /* generate odd nonce */
133 ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
134 if (ret < 0) {
135 pr_info("tpm_get_random failed (%d)\n", ret);
136 return ret;
137 }
138
139 /* calculate authorization HMAC value */
140 ret = TSS_authhmac(authdata, keyauth, SHA1_DIGEST_SIZE, enonce,
141 nonceodd, cont, sizeof(uint32_t), &ordinal,
142 sizeof(uint32_t), &datalen,
143 bloblen, blob, 0, 0);
144 if (ret < 0)
145 return ret;
146
147 /* build the request buffer */
Sumit Gargc6f61e52019-10-16 10:44:53 +0530148 tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_UNBIND);
149 tpm_buf_append_u32(tb, keyhandle);
150 tpm_buf_append_u32(tb, bloblen);
151 tpm_buf_append(tb, blob, bloblen);
152 tpm_buf_append_u32(tb, authhandle);
153 tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
154 tpm_buf_append_u8(tb, cont);
155 tpm_buf_append(tb, authdata, SHA1_DIGEST_SIZE);
Denis Kenziorf884fe52018-10-09 17:49:05 +0100156
157 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
158 if (ret < 0) {
159 pr_info("authhmac failed (%d)\n", ret);
160 return ret;
161 }
162
163 datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
164
165 ret = TSS_checkhmac1(tb->data, ordinal, nonceodd,
166 keyauth, SHA1_DIGEST_SIZE,
167 sizeof(uint32_t), TPM_DATA_OFFSET,
168 datalen, TPM_DATA_OFFSET + sizeof(uint32_t),
169 0, 0);
170 if (ret < 0) {
171 pr_info("TSS_checkhmac1 failed (%d)\n", ret);
172 return ret;
173 }
174
175 memcpy(out, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t),
176 min(outlen, datalen));
177
178 return datalen;
179}
180
181/*
Denis Kenziore73d1702018-10-09 17:49:28 +0100182 * Sign a blob provided by userspace (that has had the hash function applied)
183 * using a specific key handle. The handle is assumed to have been previously
184 * loaded by e.g. LoadKey2.
185 *
186 * Note that the key signature scheme of the used key should be set to
187 * TPM_SS_RSASSAPKCS1v15_DER. This allows the hashed input to be of any size
188 * up to key_length_in_bytes - 11 and not be limited to size 20 like the
189 * TPM_SS_RSASSAPKCS1v15_SHA1 signature scheme.
190 */
Sumit Gargc6f61e52019-10-16 10:44:53 +0530191static int tpm_sign(struct tpm_buf *tb,
Denis Kenziore73d1702018-10-09 17:49:28 +0100192 uint32_t keyhandle, unsigned char *keyauth,
193 const unsigned char *blob, uint32_t bloblen,
194 void *out, uint32_t outlen)
195{
196 unsigned char nonceodd[TPM_NONCE_SIZE];
197 unsigned char enonce[TPM_NONCE_SIZE];
198 unsigned char authdata[SHA1_DIGEST_SIZE];
199 uint32_t authhandle = 0;
200 unsigned char cont = 0;
201 uint32_t ordinal;
202 uint32_t datalen;
203 int ret;
204
205 ordinal = htonl(TPM_ORD_SIGN);
206 datalen = htonl(bloblen);
207
208 /* session for loading the key */
209 ret = oiap(tb, &authhandle, enonce);
210 if (ret < 0) {
211 pr_info("oiap failed (%d)\n", ret);
212 return ret;
213 }
214
215 /* generate odd nonce */
216 ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
217 if (ret < 0) {
218 pr_info("tpm_get_random failed (%d)\n", ret);
219 return ret;
220 }
221
222 /* calculate authorization HMAC value */
223 ret = TSS_authhmac(authdata, keyauth, SHA1_DIGEST_SIZE, enonce,
224 nonceodd, cont, sizeof(uint32_t), &ordinal,
225 sizeof(uint32_t), &datalen,
226 bloblen, blob, 0, 0);
227 if (ret < 0)
228 return ret;
229
230 /* build the request buffer */
Sumit Gargc6f61e52019-10-16 10:44:53 +0530231 tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_SIGN);
232 tpm_buf_append_u32(tb, keyhandle);
233 tpm_buf_append_u32(tb, bloblen);
234 tpm_buf_append(tb, blob, bloblen);
235 tpm_buf_append_u32(tb, authhandle);
236 tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
237 tpm_buf_append_u8(tb, cont);
238 tpm_buf_append(tb, authdata, SHA1_DIGEST_SIZE);
Denis Kenziore73d1702018-10-09 17:49:28 +0100239
240 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
241 if (ret < 0) {
242 pr_info("authhmac failed (%d)\n", ret);
243 return ret;
244 }
245
246 datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
247
248 ret = TSS_checkhmac1(tb->data, ordinal, nonceodd,
249 keyauth, SHA1_DIGEST_SIZE,
250 sizeof(uint32_t), TPM_DATA_OFFSET,
251 datalen, TPM_DATA_OFFSET + sizeof(uint32_t),
252 0, 0);
253 if (ret < 0) {
254 pr_info("TSS_checkhmac1 failed (%d)\n", ret);
255 return ret;
256 }
257
258 memcpy(out, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t),
259 min(datalen, outlen));
260
261 return datalen;
262}
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300263
264/* Room to fit two u32 zeros for algo id and parameters length. */
265#define SETKEY_PARAMS_SIZE (sizeof(u32) * 2)
266
Denis Kenziore73d1702018-10-09 17:49:28 +0100267/*
Denis Kenziordff5a612018-10-09 17:48:25 +0100268 * Maximum buffer size for the BER/DER encoded public key. The public key
269 * is of the form SEQUENCE { INTEGER n, INTEGER e } where n is a maximum 2048
270 * bit key and e is usually 65537
271 * The encoding overhead is:
272 * - max 4 bytes for SEQUENCE
273 * - max 4 bytes for INTEGER n type/length
274 * - 257 bytes of n
275 * - max 2 bytes for INTEGER e type/length
276 * - 3 bytes of e
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300277 * - 4+4 of zeros for set_pub_key parameters (SETKEY_PARAMS_SIZE)
Denis Kenziordff5a612018-10-09 17:48:25 +0100278 */
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300279#define PUB_KEY_BUF_SIZE (4 + 4 + 257 + 2 + 3 + SETKEY_PARAMS_SIZE)
Denis Kenziordff5a612018-10-09 17:48:25 +0100280
281/*
Denis Kenzior903be6b2018-10-09 17:48:02 +0100282 * Provide a part of a description of the key for /proc/keys.
283 */
284static void asym_tpm_describe(const struct key *asymmetric_key,
285 struct seq_file *m)
286{
287 struct tpm_key *tk = asymmetric_key->payload.data[asym_crypto];
288
289 if (!tk)
290 return;
291
292 seq_printf(m, "TPM1.2/Blob");
293}
294
295static void asym_tpm_destroy(void *payload0, void *payload3)
296{
297 struct tpm_key *tk = payload0;
298
299 if (!tk)
300 return;
301
302 kfree(tk->blob);
303 tk->blob_len = 0;
304
305 kfree(tk);
306}
307
Denis Kenziordff5a612018-10-09 17:48:25 +0100308/* How many bytes will it take to encode the length */
309static inline uint32_t definite_length(uint32_t len)
310{
311 if (len <= 127)
312 return 1;
313 if (len <= 255)
314 return 2;
315 return 3;
316}
317
318static inline uint8_t *encode_tag_length(uint8_t *buf, uint8_t tag,
319 uint32_t len)
320{
321 *buf++ = tag;
322
323 if (len <= 127) {
324 buf[0] = len;
325 return buf + 1;
326 }
327
328 if (len <= 255) {
329 buf[0] = 0x81;
330 buf[1] = len;
331 return buf + 2;
332 }
333
334 buf[0] = 0x82;
335 put_unaligned_be16(len, buf + 1);
336 return buf + 3;
337}
338
339static uint32_t derive_pub_key(const void *pub_key, uint32_t len, uint8_t *buf)
340{
341 uint8_t *cur = buf;
342 uint32_t n_len = definite_length(len) + 1 + len + 1;
343 uint32_t e_len = definite_length(3) + 1 + 3;
344 uint8_t e[3] = { 0x01, 0x00, 0x01 };
345
346 /* SEQUENCE */
347 cur = encode_tag_length(cur, 0x30, n_len + e_len);
348 /* INTEGER n */
349 cur = encode_tag_length(cur, 0x02, len + 1);
350 cur[0] = 0x00;
351 memcpy(cur + 1, pub_key, len);
352 cur += len + 1;
353 cur = encode_tag_length(cur, 0x02, sizeof(e));
354 memcpy(cur, e, sizeof(e));
355 cur += sizeof(e);
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300356 /* Zero parameters to satisfy set_pub_key ABI. */
357 memset(cur, 0, SETKEY_PARAMS_SIZE);
Denis Kenziordff5a612018-10-09 17:48:25 +0100358
359 return cur - buf;
360}
361
362/*
363 * Determine the crypto algorithm name.
364 */
365static int determine_akcipher(const char *encoding, const char *hash_algo,
366 char alg_name[CRYPTO_MAX_ALG_NAME])
367{
Denis Kenziordff5a612018-10-09 17:48:25 +0100368 if (strcmp(encoding, "pkcs1") == 0) {
Denis Kenziore08e6892018-10-09 17:49:20 +0100369 if (!hash_algo) {
370 strcpy(alg_name, "pkcs1pad(rsa)");
371 return 0;
372 }
373
374 if (snprintf(alg_name, CRYPTO_MAX_ALG_NAME, "pkcs1pad(rsa,%s)",
375 hash_algo) >= CRYPTO_MAX_ALG_NAME)
376 return -EINVAL;
377
Denis Kenziordff5a612018-10-09 17:48:25 +0100378 return 0;
379 }
380
381 if (strcmp(encoding, "raw") == 0) {
382 strcpy(alg_name, "rsa");
383 return 0;
384 }
385
386 return -ENOPKG;
387}
388
389/*
390 * Query information about a key.
391 */
392static int tpm_key_query(const struct kernel_pkey_params *params,
393 struct kernel_pkey_query *info)
394{
395 struct tpm_key *tk = params->key->payload.data[asym_crypto];
396 int ret;
397 char alg_name[CRYPTO_MAX_ALG_NAME];
398 struct crypto_akcipher *tfm;
399 uint8_t der_pub_key[PUB_KEY_BUF_SIZE];
400 uint32_t der_pub_key_len;
401 int len;
402
403 /* TPM only works on private keys, public keys still done in software */
404 ret = determine_akcipher(params->encoding, params->hash_algo, alg_name);
405 if (ret < 0)
406 return ret;
407
408 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
409 if (IS_ERR(tfm))
410 return PTR_ERR(tfm);
411
412 der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len,
413 der_pub_key);
414
415 ret = crypto_akcipher_set_pub_key(tfm, der_pub_key, der_pub_key_len);
416 if (ret < 0)
417 goto error_free_tfm;
418
419 len = crypto_akcipher_maxsize(tfm);
420
421 info->key_size = tk->key_len;
422 info->max_data_size = tk->key_len / 8;
423 info->max_sig_size = len;
424 info->max_enc_size = len;
425 info->max_dec_size = tk->key_len / 8;
426
Denis Kenziora3359742018-10-09 17:49:13 +0100427 info->supported_ops = KEYCTL_SUPPORTS_ENCRYPT |
Denis Kenziore08e6892018-10-09 17:49:20 +0100428 KEYCTL_SUPPORTS_DECRYPT |
Denis Kenzior64ae16d2018-10-09 17:49:35 +0100429 KEYCTL_SUPPORTS_VERIFY |
430 KEYCTL_SUPPORTS_SIGN;
Denis Kenziorad4b1eb2018-10-09 17:48:33 +0100431
Denis Kenziordff5a612018-10-09 17:48:25 +0100432 ret = 0;
433error_free_tfm:
434 crypto_free_akcipher(tfm);
435 pr_devel("<==%s() = %d\n", __func__, ret);
436 return ret;
437}
438
Denis Kenziorf8c54e12018-10-09 17:48:10 +0100439/*
Denis Kenziorad4b1eb2018-10-09 17:48:33 +0100440 * Encryption operation is performed with the public key. Hence it is done
441 * in software
442 */
443static int tpm_key_encrypt(struct tpm_key *tk,
444 struct kernel_pkey_params *params,
445 const void *in, void *out)
446{
447 char alg_name[CRYPTO_MAX_ALG_NAME];
448 struct crypto_akcipher *tfm;
449 struct akcipher_request *req;
450 struct crypto_wait cwait;
451 struct scatterlist in_sg, out_sg;
452 uint8_t der_pub_key[PUB_KEY_BUF_SIZE];
453 uint32_t der_pub_key_len;
454 int ret;
455
456 pr_devel("==>%s()\n", __func__);
457
458 ret = determine_akcipher(params->encoding, params->hash_algo, alg_name);
459 if (ret < 0)
460 return ret;
461
462 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
463 if (IS_ERR(tfm))
464 return PTR_ERR(tfm);
465
466 der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len,
467 der_pub_key);
468
469 ret = crypto_akcipher_set_pub_key(tfm, der_pub_key, der_pub_key_len);
470 if (ret < 0)
471 goto error_free_tfm;
472
473 req = akcipher_request_alloc(tfm, GFP_KERNEL);
474 if (!req)
475 goto error_free_tfm;
476
477 sg_init_one(&in_sg, in, params->in_len);
478 sg_init_one(&out_sg, out, params->out_len);
479 akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len,
480 params->out_len);
481 crypto_init_wait(&cwait);
482 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
483 CRYPTO_TFM_REQ_MAY_SLEEP,
484 crypto_req_done, &cwait);
485
486 ret = crypto_akcipher_encrypt(req);
487 ret = crypto_wait_req(ret, &cwait);
488
489 if (ret == 0)
490 ret = req->dst_len;
491
492 akcipher_request_free(req);
493error_free_tfm:
494 crypto_free_akcipher(tfm);
495 pr_devel("<==%s() = %d\n", __func__, ret);
496 return ret;
497}
498
499/*
Denis Kenziora3359742018-10-09 17:49:13 +0100500 * Decryption operation is performed with the private key in the TPM.
501 */
502static int tpm_key_decrypt(struct tpm_key *tk,
503 struct kernel_pkey_params *params,
504 const void *in, void *out)
505{
Sumit Gargc6f61e52019-10-16 10:44:53 +0530506 struct tpm_buf tb;
Denis Kenziora3359742018-10-09 17:49:13 +0100507 uint32_t keyhandle;
508 uint8_t srkauth[SHA1_DIGEST_SIZE];
509 uint8_t keyauth[SHA1_DIGEST_SIZE];
510 int r;
511
512 pr_devel("==>%s()\n", __func__);
513
514 if (params->hash_algo)
515 return -ENOPKG;
516
517 if (strcmp(params->encoding, "pkcs1"))
518 return -ENOPKG;
519
Sumit Gargc6f61e52019-10-16 10:44:53 +0530520 r = tpm_buf_init(&tb, 0, 0);
521 if (r)
522 return r;
Denis Kenziora3359742018-10-09 17:49:13 +0100523
524 /* TODO: Handle a non-all zero SRK authorization */
525 memset(srkauth, 0, sizeof(srkauth));
526
Sumit Gargc6f61e52019-10-16 10:44:53 +0530527 r = tpm_loadkey2(&tb, SRKHANDLE, srkauth,
Denis Kenziora3359742018-10-09 17:49:13 +0100528 tk->blob, tk->blob_len, &keyhandle);
529 if (r < 0) {
530 pr_devel("loadkey2 failed (%d)\n", r);
531 goto error;
532 }
533
534 /* TODO: Handle a non-all zero key authorization */
535 memset(keyauth, 0, sizeof(keyauth));
536
Sumit Gargc6f61e52019-10-16 10:44:53 +0530537 r = tpm_unbind(&tb, keyhandle, keyauth,
Denis Kenziora3359742018-10-09 17:49:13 +0100538 in, params->in_len, out, params->out_len);
539 if (r < 0)
540 pr_devel("tpm_unbind failed (%d)\n", r);
541
Sumit Gargc6f61e52019-10-16 10:44:53 +0530542 if (tpm_flushspecific(&tb, keyhandle) < 0)
Denis Kenziora3359742018-10-09 17:49:13 +0100543 pr_devel("flushspecific failed (%d)\n", r);
544
545error:
Sumit Gargc6f61e52019-10-16 10:44:53 +0530546 tpm_buf_destroy(&tb);
Denis Kenziora3359742018-10-09 17:49:13 +0100547 pr_devel("<==%s() = %d\n", __func__, r);
548 return r;
549}
550
551/*
Denis Kenzior64ae16d2018-10-09 17:49:35 +0100552 * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2].
553 */
554static const u8 digest_info_md5[] = {
555 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08,
556 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, /* OID */
557 0x05, 0x00, 0x04, 0x10
558};
559
560static const u8 digest_info_sha1[] = {
561 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
562 0x2b, 0x0e, 0x03, 0x02, 0x1a,
563 0x05, 0x00, 0x04, 0x14
564};
565
566static const u8 digest_info_rmd160[] = {
567 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
568 0x2b, 0x24, 0x03, 0x02, 0x01,
569 0x05, 0x00, 0x04, 0x14
570};
571
572static const u8 digest_info_sha224[] = {
573 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09,
574 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
575 0x05, 0x00, 0x04, 0x1c
576};
577
578static const u8 digest_info_sha256[] = {
579 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09,
580 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
581 0x05, 0x00, 0x04, 0x20
582};
583
584static const u8 digest_info_sha384[] = {
585 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
586 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
587 0x05, 0x00, 0x04, 0x30
588};
589
590static const u8 digest_info_sha512[] = {
591 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09,
592 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
593 0x05, 0x00, 0x04, 0x40
594};
595
596static const struct asn1_template {
597 const char *name;
598 const u8 *data;
599 size_t size;
600} asn1_templates[] = {
601#define _(X) { #X, digest_info_##X, sizeof(digest_info_##X) }
602 _(md5),
603 _(sha1),
604 _(rmd160),
605 _(sha256),
606 _(sha384),
607 _(sha512),
608 _(sha224),
609 { NULL }
610#undef _
611};
612
613static const struct asn1_template *lookup_asn1(const char *name)
614{
615 const struct asn1_template *p;
616
617 for (p = asn1_templates; p->name; p++)
618 if (strcmp(name, p->name) == 0)
619 return p;
620 return NULL;
621}
622
623/*
624 * Sign operation is performed with the private key in the TPM.
625 */
626static int tpm_key_sign(struct tpm_key *tk,
627 struct kernel_pkey_params *params,
628 const void *in, void *out)
629{
Sumit Gargc6f61e52019-10-16 10:44:53 +0530630 struct tpm_buf tb;
Denis Kenzior64ae16d2018-10-09 17:49:35 +0100631 uint32_t keyhandle;
632 uint8_t srkauth[SHA1_DIGEST_SIZE];
633 uint8_t keyauth[SHA1_DIGEST_SIZE];
634 void *asn1_wrapped = NULL;
635 uint32_t in_len = params->in_len;
636 int r;
637
638 pr_devel("==>%s()\n", __func__);
639
640 if (strcmp(params->encoding, "pkcs1"))
641 return -ENOPKG;
642
643 if (params->hash_algo) {
644 const struct asn1_template *asn1 =
645 lookup_asn1(params->hash_algo);
646
647 if (!asn1)
648 return -ENOPKG;
649
650 /* request enough space for the ASN.1 template + input hash */
651 asn1_wrapped = kzalloc(in_len + asn1->size, GFP_KERNEL);
652 if (!asn1_wrapped)
653 return -ENOMEM;
654
655 /* Copy ASN.1 template, then the input */
656 memcpy(asn1_wrapped, asn1->data, asn1->size);
657 memcpy(asn1_wrapped + asn1->size, in, in_len);
658
659 in = asn1_wrapped;
660 in_len += asn1->size;
661 }
662
663 if (in_len > tk->key_len / 8 - 11) {
664 r = -EOVERFLOW;
665 goto error_free_asn1_wrapped;
666 }
667
Sumit Gargc6f61e52019-10-16 10:44:53 +0530668 r = tpm_buf_init(&tb, 0, 0);
669 if (r)
Denis Kenzior64ae16d2018-10-09 17:49:35 +0100670 goto error_free_asn1_wrapped;
671
672 /* TODO: Handle a non-all zero SRK authorization */
673 memset(srkauth, 0, sizeof(srkauth));
674
Sumit Gargc6f61e52019-10-16 10:44:53 +0530675 r = tpm_loadkey2(&tb, SRKHANDLE, srkauth,
Denis Kenzior64ae16d2018-10-09 17:49:35 +0100676 tk->blob, tk->blob_len, &keyhandle);
677 if (r < 0) {
678 pr_devel("loadkey2 failed (%d)\n", r);
679 goto error_free_tb;
680 }
681
682 /* TODO: Handle a non-all zero key authorization */
683 memset(keyauth, 0, sizeof(keyauth));
684
Sumit Gargc6f61e52019-10-16 10:44:53 +0530685 r = tpm_sign(&tb, keyhandle, keyauth, in, in_len, out, params->out_len);
Denis Kenzior64ae16d2018-10-09 17:49:35 +0100686 if (r < 0)
687 pr_devel("tpm_sign failed (%d)\n", r);
688
Sumit Gargc6f61e52019-10-16 10:44:53 +0530689 if (tpm_flushspecific(&tb, keyhandle) < 0)
Denis Kenzior64ae16d2018-10-09 17:49:35 +0100690 pr_devel("flushspecific failed (%d)\n", r);
691
692error_free_tb:
Sumit Gargc6f61e52019-10-16 10:44:53 +0530693 tpm_buf_destroy(&tb);
Denis Kenzior64ae16d2018-10-09 17:49:35 +0100694error_free_asn1_wrapped:
695 kfree(asn1_wrapped);
696 pr_devel("<==%s() = %d\n", __func__, r);
697 return r;
698}
699
700/*
Denis Kenziorad4b1eb2018-10-09 17:48:33 +0100701 * Do encryption, decryption and signing ops.
702 */
703static int tpm_key_eds_op(struct kernel_pkey_params *params,
704 const void *in, void *out)
705{
706 struct tpm_key *tk = params->key->payload.data[asym_crypto];
707 int ret = -EOPNOTSUPP;
708
709 /* Perform the encryption calculation. */
710 switch (params->op) {
711 case kernel_pkey_encrypt:
712 ret = tpm_key_encrypt(tk, params, in, out);
713 break;
Denis Kenziora3359742018-10-09 17:49:13 +0100714 case kernel_pkey_decrypt:
715 ret = tpm_key_decrypt(tk, params, in, out);
716 break;
Denis Kenzior64ae16d2018-10-09 17:49:35 +0100717 case kernel_pkey_sign:
718 ret = tpm_key_sign(tk, params, in, out);
719 break;
Denis Kenziorad4b1eb2018-10-09 17:48:33 +0100720 default:
721 BUG();
722 }
723
724 return ret;
725}
726
727/*
Denis Kenziore08e6892018-10-09 17:49:20 +0100728 * Verify a signature using a public key.
729 */
730static int tpm_key_verify_signature(const struct key *key,
731 const struct public_key_signature *sig)
732{
733 const struct tpm_key *tk = key->payload.data[asym_crypto];
734 struct crypto_wait cwait;
735 struct crypto_akcipher *tfm;
736 struct akcipher_request *req;
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300737 struct scatterlist src_sg[2];
Denis Kenziore08e6892018-10-09 17:49:20 +0100738 char alg_name[CRYPTO_MAX_ALG_NAME];
739 uint8_t der_pub_key[PUB_KEY_BUF_SIZE];
740 uint32_t der_pub_key_len;
Denis Kenziore08e6892018-10-09 17:49:20 +0100741 int ret;
742
743 pr_devel("==>%s()\n", __func__);
744
745 BUG_ON(!tk);
746 BUG_ON(!sig);
747 BUG_ON(!sig->s);
748
749 if (!sig->digest)
750 return -ENOPKG;
751
752 ret = determine_akcipher(sig->encoding, sig->hash_algo, alg_name);
753 if (ret < 0)
754 return ret;
755
756 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
757 if (IS_ERR(tfm))
758 return PTR_ERR(tfm);
759
760 der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len,
761 der_pub_key);
762
763 ret = crypto_akcipher_set_pub_key(tfm, der_pub_key, der_pub_key_len);
764 if (ret < 0)
765 goto error_free_tfm;
766
767 ret = -ENOMEM;
768 req = akcipher_request_alloc(tfm, GFP_KERNEL);
769 if (!req)
770 goto error_free_tfm;
771
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300772 sg_init_table(src_sg, 2);
773 sg_set_buf(&src_sg[0], sig->s, sig->s_size);
Vitaly Chikunov83bc0292019-04-11 18:51:16 +0300774 sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300775 akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size,
776 sig->digest_size);
Denis Kenziore08e6892018-10-09 17:49:20 +0100777 crypto_init_wait(&cwait);
778 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
779 CRYPTO_TFM_REQ_MAY_SLEEP,
780 crypto_req_done, &cwait);
Denis Kenziore08e6892018-10-09 17:49:20 +0100781 ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
Denis Kenziore08e6892018-10-09 17:49:20 +0100782
Denis Kenziore08e6892018-10-09 17:49:20 +0100783 akcipher_request_free(req);
784error_free_tfm:
785 crypto_free_akcipher(tfm);
786 pr_devel("<==%s() = %d\n", __func__, ret);
787 if (WARN_ON_ONCE(ret > 0))
788 ret = -EINVAL;
789 return ret;
790}
791
792/*
Denis Kenziorf8c54e12018-10-09 17:48:10 +0100793 * Parse enough information out of TPM_KEY structure:
794 * TPM_STRUCT_VER -> 4 bytes
795 * TPM_KEY_USAGE -> 2 bytes
796 * TPM_KEY_FLAGS -> 4 bytes
797 * TPM_AUTH_DATA_USAGE -> 1 byte
798 * TPM_KEY_PARMS -> variable
799 * UINT32 PCRInfoSize -> 4 bytes
800 * BYTE* -> PCRInfoSize bytes
801 * TPM_STORE_PUBKEY
802 * UINT32 encDataSize;
803 * BYTE* -> encDataSize;
804 *
805 * TPM_KEY_PARMS:
806 * TPM_ALGORITHM_ID -> 4 bytes
807 * TPM_ENC_SCHEME -> 2 bytes
808 * TPM_SIG_SCHEME -> 2 bytes
809 * UINT32 parmSize -> 4 bytes
810 * BYTE* -> variable
811 */
812static int extract_key_parameters(struct tpm_key *tk)
813{
814 const void *cur = tk->blob;
815 uint32_t len = tk->blob_len;
816 const void *pub_key;
817 uint32_t sz;
818 uint32_t key_len;
819
820 if (len < 11)
821 return -EBADMSG;
822
823 /* Ensure this is a legacy key */
824 if (get_unaligned_be16(cur + 4) != 0x0015)
825 return -EBADMSG;
826
827 /* Skip to TPM_KEY_PARMS */
828 cur += 11;
829 len -= 11;
830
831 if (len < 12)
832 return -EBADMSG;
833
834 /* Make sure this is an RSA key */
835 if (get_unaligned_be32(cur) != 0x00000001)
836 return -EBADMSG;
837
838 /* Make sure this is TPM_ES_RSAESPKCSv15 encoding scheme */
839 if (get_unaligned_be16(cur + 4) != 0x0002)
840 return -EBADMSG;
841
842 /* Make sure this is TPM_SS_RSASSAPKCS1v15_DER signature scheme */
843 if (get_unaligned_be16(cur + 6) != 0x0003)
844 return -EBADMSG;
845
846 sz = get_unaligned_be32(cur + 8);
847 if (len < sz + 12)
848 return -EBADMSG;
849
850 /* Move to TPM_RSA_KEY_PARMS */
851 len -= 12;
852 cur += 12;
853
854 /* Grab the RSA key length */
855 key_len = get_unaligned_be32(cur);
856
857 switch (key_len) {
858 case 512:
859 case 1024:
860 case 1536:
861 case 2048:
862 break;
863 default:
864 return -EINVAL;
865 }
866
867 /* Move just past TPM_KEY_PARMS */
868 cur += sz;
869 len -= sz;
870
871 if (len < 4)
872 return -EBADMSG;
873
874 sz = get_unaligned_be32(cur);
875 if (len < 4 + sz)
876 return -EBADMSG;
877
878 /* Move to TPM_STORE_PUBKEY */
879 cur += 4 + sz;
880 len -= 4 + sz;
881
882 /* Grab the size of the public key, it should jive with the key size */
883 sz = get_unaligned_be32(cur);
884 if (sz > 256)
885 return -EINVAL;
886
887 pub_key = cur + 4;
888
889 tk->key_len = key_len;
890 tk->pub_key = pub_key;
891 tk->pub_key_len = sz;
892
893 return 0;
894}
895
Denis Kenzior903be6b2018-10-09 17:48:02 +0100896/* Given the blob, parse it and load it into the TPM */
897struct tpm_key *tpm_key_create(const void *blob, uint32_t blob_len)
898{
899 int r;
900 struct tpm_key *tk;
901
902 r = tpm_is_tpm2(NULL);
903 if (r < 0)
904 goto error;
905
906 /* We don't support TPM2 yet */
907 if (r > 0) {
908 r = -ENODEV;
909 goto error;
910 }
911
912 r = -ENOMEM;
913 tk = kzalloc(sizeof(struct tpm_key), GFP_KERNEL);
914 if (!tk)
915 goto error;
916
917 tk->blob = kmemdup(blob, blob_len, GFP_KERNEL);
918 if (!tk->blob)
919 goto error_memdup;
920
921 tk->blob_len = blob_len;
922
Denis Kenziorf8c54e12018-10-09 17:48:10 +0100923 r = extract_key_parameters(tk);
924 if (r < 0)
925 goto error_extract;
926
Denis Kenzior903be6b2018-10-09 17:48:02 +0100927 return tk;
928
Denis Kenziorf8c54e12018-10-09 17:48:10 +0100929error_extract:
930 kfree(tk->blob);
931 tk->blob_len = 0;
Denis Kenzior903be6b2018-10-09 17:48:02 +0100932error_memdup:
933 kfree(tk);
934error:
935 return ERR_PTR(r);
936}
937EXPORT_SYMBOL_GPL(tpm_key_create);
938
939/*
940 * TPM-based asymmetric key subtype
941 */
942struct asymmetric_key_subtype asym_tpm_subtype = {
943 .owner = THIS_MODULE,
944 .name = "asym_tpm",
945 .name_len = sizeof("asym_tpm") - 1,
946 .describe = asym_tpm_describe,
947 .destroy = asym_tpm_destroy,
Denis Kenziordff5a612018-10-09 17:48:25 +0100948 .query = tpm_key_query,
Denis Kenziorad4b1eb2018-10-09 17:48:33 +0100949 .eds_op = tpm_key_eds_op,
Denis Kenziore08e6892018-10-09 17:49:20 +0100950 .verify_signature = tpm_key_verify_signature,
Denis Kenzior903be6b2018-10-09 17:48:02 +0100951};
952EXPORT_SYMBOL_GPL(asym_tpm_subtype);
953
954MODULE_DESCRIPTION("TPM based asymmetric key subtype");
955MODULE_AUTHOR("Intel Corporation");
956MODULE_LICENSE("GPL v2");