xref: /linux/samples/tsm-mr/tsm_mr_sample.c (revision ae5ec8adb8ec9c2aa916f853737c101faa87e5ba)
1f6953f1fSCedric Xing // SPDX-License-Identifier: GPL-2.0-only
2f6953f1fSCedric Xing /* Copyright(c) 2024-2005 Intel Corporation. All rights reserved. */
3f6953f1fSCedric Xing 
4f6953f1fSCedric Xing #define pr_fmt(x) KBUILD_MODNAME ": " x
5f6953f1fSCedric Xing 
6f6953f1fSCedric Xing #include <linux/module.h>
7f6953f1fSCedric Xing #include <linux/tsm-mr.h>
8f6953f1fSCedric Xing #include <linux/miscdevice.h>
9f6953f1fSCedric Xing #include <crypto/hash.h>
10f6953f1fSCedric Xing 
11*1f450730SCedric Xing static struct {
12f6953f1fSCedric Xing 	u8 static_mr[SHA384_DIGEST_SIZE];
13f6953f1fSCedric Xing 	u8 config_mr[SHA512_DIGEST_SIZE];
14f6953f1fSCedric Xing 	u8 rtmr0[SHA256_DIGEST_SIZE];
15f6953f1fSCedric Xing 	u8 rtmr1[SHA384_DIGEST_SIZE];
16f6953f1fSCedric Xing 	u8 report_digest[SHA512_DIGEST_SIZE];
17f6953f1fSCedric Xing } sample_report = {
18f6953f1fSCedric Xing 	.static_mr = "static_mr",
19f6953f1fSCedric Xing 	.config_mr = "config_mr",
20f6953f1fSCedric Xing 	.rtmr0 = "rtmr0",
21f6953f1fSCedric Xing 	.rtmr1 = "rtmr1",
22f6953f1fSCedric Xing };
23f6953f1fSCedric Xing 
sample_report_refresh(const struct tsm_measurements * tm)24f6953f1fSCedric Xing static int sample_report_refresh(const struct tsm_measurements *tm)
25f6953f1fSCedric Xing {
26f6953f1fSCedric Xing 	struct crypto_shash *tfm;
27f6953f1fSCedric Xing 	int rc;
28f6953f1fSCedric Xing 
29f6953f1fSCedric Xing 	tfm = crypto_alloc_shash(hash_algo_name[HASH_ALGO_SHA512], 0, 0);
30f6953f1fSCedric Xing 	if (IS_ERR(tfm)) {
31f6953f1fSCedric Xing 		pr_err("crypto_alloc_shash failed: %ld\n", PTR_ERR(tfm));
32f6953f1fSCedric Xing 		return PTR_ERR(tfm);
33f6953f1fSCedric Xing 	}
34f6953f1fSCedric Xing 
35f6953f1fSCedric Xing 	rc = crypto_shash_tfm_digest(tfm, (u8 *)&sample_report,
36f6953f1fSCedric Xing 				     offsetof(typeof(sample_report),
37f6953f1fSCedric Xing 					      report_digest),
38f6953f1fSCedric Xing 				     sample_report.report_digest);
39f6953f1fSCedric Xing 	crypto_free_shash(tfm);
40f6953f1fSCedric Xing 	if (rc)
41f6953f1fSCedric Xing 		pr_err("crypto_shash_tfm_digest failed: %d\n", rc);
42f6953f1fSCedric Xing 	return rc;
43f6953f1fSCedric Xing }
44f6953f1fSCedric Xing 
sample_report_extend_mr(const struct tsm_measurements * tm,const struct tsm_measurement_register * mr,const u8 * data)45f6953f1fSCedric Xing static int sample_report_extend_mr(const struct tsm_measurements *tm,
46f6953f1fSCedric Xing 				   const struct tsm_measurement_register *mr,
47f6953f1fSCedric Xing 				   const u8 *data)
48f6953f1fSCedric Xing {
49f6953f1fSCedric Xing 	SHASH_DESC_ON_STACK(desc, 0);
50f6953f1fSCedric Xing 	int rc;
51f6953f1fSCedric Xing 
52f6953f1fSCedric Xing 	desc->tfm = crypto_alloc_shash(hash_algo_name[mr->mr_hash], 0, 0);
53f6953f1fSCedric Xing 	if (IS_ERR(desc->tfm)) {
54f6953f1fSCedric Xing 		pr_err("crypto_alloc_shash failed: %ld\n", PTR_ERR(desc->tfm));
55f6953f1fSCedric Xing 		return PTR_ERR(desc->tfm);
56f6953f1fSCedric Xing 	}
57f6953f1fSCedric Xing 
58f6953f1fSCedric Xing 	rc = crypto_shash_init(desc);
59f6953f1fSCedric Xing 	if (!rc)
60f6953f1fSCedric Xing 		rc = crypto_shash_update(desc, mr->mr_value, mr->mr_size);
61f6953f1fSCedric Xing 	if (!rc)
62f6953f1fSCedric Xing 		rc = crypto_shash_finup(desc, data, mr->mr_size, mr->mr_value);
63f6953f1fSCedric Xing 	crypto_free_shash(desc->tfm);
64f6953f1fSCedric Xing 	if (rc)
65f6953f1fSCedric Xing 		pr_err("SHA calculation failed: %d\n", rc);
66f6953f1fSCedric Xing 	return rc;
67f6953f1fSCedric Xing }
68f6953f1fSCedric Xing 
69f6953f1fSCedric Xing #define MR_(mr, hash) .mr_value = &sample_report.mr, TSM_MR_(mr, hash)
70f6953f1fSCedric Xing static const struct tsm_measurement_register sample_mrs[] = {
71f6953f1fSCedric Xing 	/* static MR, read-only */
72f6953f1fSCedric Xing 	{ MR_(static_mr, SHA384) },
73f6953f1fSCedric Xing 	/* config MR, read-only */
74f6953f1fSCedric Xing 	{ MR_(config_mr, SHA512) | TSM_MR_F_NOHASH },
75f6953f1fSCedric Xing 	/* RTMR, direct extension prohibited */
76f6953f1fSCedric Xing 	{ MR_(rtmr0, SHA256) | TSM_MR_F_LIVE },
77f6953f1fSCedric Xing 	/* RTMR, direct extension allowed */
78f6953f1fSCedric Xing 	{ MR_(rtmr1, SHA384) | TSM_MR_F_RTMR },
79f6953f1fSCedric Xing 	/* RTMR, crypto agile, alaised to rtmr0 and rtmr1, respectively */
80f6953f1fSCedric Xing 	{ .mr_value = &sample_report.rtmr0,
81f6953f1fSCedric Xing 	  TSM_MR_(rtmr_crypto_agile, SHA256) | TSM_MR_F_RTMR },
82f6953f1fSCedric Xing 	{ .mr_value = &sample_report.rtmr1,
83f6953f1fSCedric Xing 	  TSM_MR_(rtmr_crypto_agile, SHA384) | TSM_MR_F_RTMR },
84f6953f1fSCedric Xing 	/* sha512 digest of the whole structure */
85f6953f1fSCedric Xing 	{ MR_(report_digest, SHA512) | TSM_MR_F_LIVE },
86f6953f1fSCedric Xing };
87f6953f1fSCedric Xing #undef MR_
88f6953f1fSCedric Xing 
89f6953f1fSCedric Xing static struct tsm_measurements sample_tm = {
90f6953f1fSCedric Xing 	.mrs = sample_mrs,
91f6953f1fSCedric Xing 	.nr_mrs = ARRAY_SIZE(sample_mrs),
92f6953f1fSCedric Xing 	.refresh = sample_report_refresh,
93f6953f1fSCedric Xing 	.write = sample_report_extend_mr,
94f6953f1fSCedric Xing };
95f6953f1fSCedric Xing 
96f6953f1fSCedric Xing static const struct attribute_group *sample_groups[] = {
97f6953f1fSCedric Xing 	NULL,
98f6953f1fSCedric Xing 	NULL,
99f6953f1fSCedric Xing };
100f6953f1fSCedric Xing 
101f6953f1fSCedric Xing static struct miscdevice sample_misc_dev = {
102f6953f1fSCedric Xing 	.name = KBUILD_MODNAME,
103f6953f1fSCedric Xing 	.minor = MISC_DYNAMIC_MINOR,
104f6953f1fSCedric Xing 	.groups = sample_groups,
105f6953f1fSCedric Xing };
106f6953f1fSCedric Xing 
tsm_mr_sample_init(void)107f6953f1fSCedric Xing static int __init tsm_mr_sample_init(void)
108f6953f1fSCedric Xing {
109f6953f1fSCedric Xing 	int rc;
110f6953f1fSCedric Xing 
111f6953f1fSCedric Xing 	sample_groups[0] = tsm_mr_create_attribute_group(&sample_tm);
112f6953f1fSCedric Xing 	if (IS_ERR(sample_groups[0]))
113f6953f1fSCedric Xing 		return PTR_ERR(sample_groups[0]);
114f6953f1fSCedric Xing 
115f6953f1fSCedric Xing 	rc = misc_register(&sample_misc_dev);
116f6953f1fSCedric Xing 	if (rc)
117f6953f1fSCedric Xing 		tsm_mr_free_attribute_group(sample_groups[0]);
118f6953f1fSCedric Xing 	return rc;
119f6953f1fSCedric Xing }
120f6953f1fSCedric Xing 
tsm_mr_sample_exit(void)121f6953f1fSCedric Xing static void __exit tsm_mr_sample_exit(void)
122f6953f1fSCedric Xing {
123f6953f1fSCedric Xing 	misc_deregister(&sample_misc_dev);
124f6953f1fSCedric Xing 	tsm_mr_free_attribute_group(sample_groups[0]);
125f6953f1fSCedric Xing }
126f6953f1fSCedric Xing 
127f6953f1fSCedric Xing module_init(tsm_mr_sample_init);
128f6953f1fSCedric Xing module_exit(tsm_mr_sample_exit);
129f6953f1fSCedric Xing 
130f6953f1fSCedric Xing MODULE_LICENSE("GPL");
131f6953f1fSCedric Xing MODULE_DESCRIPTION("Sample module using tsm-mr to expose emulated MRs");
132