-
Notifications
You must be signed in to change notification settings - Fork 4
/
tpm2_cmds.c
158 lines (129 loc) · 2.99 KB
/
tpm2_cmds.c
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
/*
* Copyright (c) 2019 Apertus Solutions, LLC
*
* Author(s):
* Daniel P. Smith <[email protected]>
*
*/
#ifdef LINUX_KERNEL
#include <linux/types.h>
#include <linux/const.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <asm/byteorder.h>
#elif defined LINUX_USERSPACE
#include <string.h>
#include <endian.h>
#include <errno.h>
#define cpu_to_be16 htobe16
#define cpu_to_be32 htobe32
#endif
#include "tpm.h"
#include "tpmbuff.h"
#include "tpm_common.h"
#include "tpm2.h"
#include "tpm2_auth.h"
#include "tis.h"
#include "crb.h"
static int tpm2_alloc_cmd(struct tpmbuff *b, struct tpm2_cmd *c, u16 tag,
u32 code)
{
/* ensure buffer is free for use */
tpmb_free(b);
c->header = (struct tpm_header *)tpmb_reserve(b);
if (!c->header)
return -ENOMEM;
c->header->tag = cpu_to_be16(tag);
c->header->code = cpu_to_be32(code);
return 0;
}
static u16 convert_digest_list(struct tpml_digest_values *digests)
{
int i;
u16 size = sizeof(digests->count);
struct tpmt_ha *h = digests->digests;
for (i = 0; i < digests->count; i++) {
switch (h->alg) {
case TPM_ALG_SHA1:
h->alg = cpu_to_be16(h->alg);
h = (struct tpmt_ha *)((u8 *)h + SHA1_SIZE);
size += sizeof(u16) + SHA1_SIZE;
break;
case TPM_ALG_SHA256:
h->alg = cpu_to_be16(h->alg);
h = (struct tpmt_ha *)((u8 *)h + SHA256_SIZE);
size += sizeof(u16) + SHA256_SIZE;
break;
case TPM_ALG_SHA384:
h->alg = cpu_to_be16(h->alg);
h = (struct tpmt_ha *)((u8 *)h + SHA384_SIZE);
size += sizeof(u16) + SHA384_SIZE;
break;
case TPM_ALG_SHA512:
h->alg = cpu_to_be16(h->alg);
h = (struct tpmt_ha *)((u8 *)h + SHA512_SIZE);
size += sizeof(u16) + SHA512_SIZE;
break;
case TPM_ALG_SM3_256:
h->alg = cpu_to_be16(h->alg);
h = (struct tpmt_ha *)((u8 *)h + SM3256_SIZE);
size += sizeof(u16) + SHA1_SIZE;
break;
default:
return 0;
}
}
digests->count = cpu_to_be32(digests->count);
return size;
}
int tpm2_extend_pcr(struct tpm *t, u32 pcr,
struct tpml_digest_values *digests)
{
struct tpmbuff *b = t->buff;
struct tpm2_cmd cmd;
u16 size;
int ret = 0;
if (b == NULL) {
ret = -EINVAL;
goto out;
}
ret = tpm2_alloc_cmd(b, &cmd, TPM_ST_SESSIONS, TPM_CC_PCR_EXTEND);
if (ret < 0)
goto out;
cmd.handles = (u32 *)tpmb_put(b, sizeof(u32));
if (cmd.handles == NULL) {
ret = -ENOMEM;
goto free;
}
cmd.handles[0] = cpu_to_be32(pcr);
cmd.auth_size = (u32 *)tpmb_put(b, sizeof(u32));
if (cmd.auth_size == NULL) {
ret = -ENOMEM;
goto free;
}
cmd.auth = tpm2_null_auth(b);
if (cmd.auth == NULL) {
ret = -ENOMEM;
goto free;
}
*cmd.auth_size = cpu_to_be32(tpm2_null_auth_size());
size = convert_digest_list(digests);
if (size == 0) {
ret = -ENOMEM;
goto free;
}
cmd.params = (u8 *)tpmb_put(b, size);
if (cmd.params == NULL) {
ret = -ENOMEM;
goto free;
}
memcpy(cmd.params, digests, size);
cmd.header->size = cpu_to_be32(tpmb_size(b));
size = t->ops.send(b);
if (tpmb_size(b) != size)
ret = -EAGAIN;
free:
tpmb_free(b);
out:
return ret;
}