xref: /linux/drivers/char/tpm/tpm-buf.c (revision 40ccd6aa3e2e05be93394e3cd560c718dedfcc77)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Handling of TPM command and other buffers.
4  */
5 
6 #include <linux/tpm_command.h>
7 #include <linux/module.h>
8 #include <linux/tpm.h>
9 
10 /**
11  * tpm_buf_init() - Allocate and initialize a TPM command
12  * @buf:	A &tpm_buf
13  * @tag:	TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS
14  * @ordinal:	A command ordinal
15  *
16  * Return: 0 or -ENOMEM
17  */
18 int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
19 {
20 	buf->data = (u8 *)__get_free_page(GFP_KERNEL);
21 	if (!buf->data)
22 		return -ENOMEM;
23 
24 	tpm_buf_reset(buf, tag, ordinal);
25 	return 0;
26 }
27 EXPORT_SYMBOL_GPL(tpm_buf_init);
28 
29 /**
30  * tpm_buf_reset() - Initialize a TPM command
31  * @buf:	A &tpm_buf
32  * @tag:	TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS
33  * @ordinal:	A command ordinal
34  */
35 void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
36 {
37 	struct tpm_header *head = (struct tpm_header *)buf->data;
38 
39 	WARN_ON(tag != TPM_TAG_RQU_COMMAND && tag != TPM2_ST_NO_SESSIONS &&
40 		tag != TPM2_ST_SESSIONS && tag != 0);
41 
42 	buf->flags = 0;
43 	buf->length = sizeof(*head);
44 	head->tag = cpu_to_be16(tag);
45 	head->length = cpu_to_be32(sizeof(*head));
46 	head->ordinal = cpu_to_be32(ordinal);
47 	buf->handles = 0;
48 }
49 EXPORT_SYMBOL_GPL(tpm_buf_reset);
50 
51 /**
52  * tpm_buf_init_sized() - Allocate and initialize a sized (TPM2B) buffer
53  * @buf:	A @tpm_buf
54  *
55  * Return: 0 or -ENOMEM
56  */
57 int tpm_buf_init_sized(struct tpm_buf *buf)
58 {
59 	buf->data = (u8 *)__get_free_page(GFP_KERNEL);
60 	if (!buf->data)
61 		return -ENOMEM;
62 
63 	tpm_buf_reset_sized(buf);
64 	return 0;
65 }
66 EXPORT_SYMBOL_GPL(tpm_buf_init_sized);
67 
68 /**
69  * tpm_buf_reset_sized() - Initialize a sized buffer
70  * @buf:	A &tpm_buf
71  */
72 void tpm_buf_reset_sized(struct tpm_buf *buf)
73 {
74 	buf->flags = TPM_BUF_TPM2B;
75 	buf->length = 2;
76 	buf->data[0] = 0;
77 	buf->data[1] = 0;
78 }
79 EXPORT_SYMBOL_GPL(tpm_buf_reset_sized);
80 
81 void tpm_buf_destroy(struct tpm_buf *buf)
82 {
83 	free_page((unsigned long)buf->data);
84 }
85 EXPORT_SYMBOL_GPL(tpm_buf_destroy);
86 
87 /**
88  * tpm_buf_length() - Return the number of bytes consumed by the data
89  * @buf:	A &tpm_buf
90  *
91  * Return: The number of bytes consumed by the buffer
92  */
93 u32 tpm_buf_length(struct tpm_buf *buf)
94 {
95 	return buf->length;
96 }
97 EXPORT_SYMBOL_GPL(tpm_buf_length);
98 
99 /**
100  * tpm_buf_append() - Append data to an initialized buffer
101  * @buf:	A &tpm_buf
102  * @new_data:	A data blob
103  * @new_length:	Size of the appended data
104  */
105 void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length)
106 {
107 	/* Return silently if overflow has already happened. */
108 	if (buf->flags & TPM_BUF_OVERFLOW)
109 		return;
110 
111 	if ((buf->length + new_length) > PAGE_SIZE) {
112 		WARN(1, "tpm_buf: write overflow\n");
113 		buf->flags |= TPM_BUF_OVERFLOW;
114 		return;
115 	}
116 
117 	memcpy(&buf->data[buf->length], new_data, new_length);
118 	buf->length += new_length;
119 
120 	if (buf->flags & TPM_BUF_TPM2B)
121 		((__be16 *)buf->data)[0] = cpu_to_be16(buf->length - 2);
122 	else
123 		((struct tpm_header *)buf->data)->length = cpu_to_be32(buf->length);
124 }
125 EXPORT_SYMBOL_GPL(tpm_buf_append);
126 
127 void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
128 {
129 	tpm_buf_append(buf, &value, 1);
130 }
131 EXPORT_SYMBOL_GPL(tpm_buf_append_u8);
132 
133 void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value)
134 {
135 	__be16 value2 = cpu_to_be16(value);
136 
137 	tpm_buf_append(buf, (u8 *)&value2, 2);
138 }
139 EXPORT_SYMBOL_GPL(tpm_buf_append_u16);
140 
141 void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
142 {
143 	__be32 value2 = cpu_to_be32(value);
144 
145 	tpm_buf_append(buf, (u8 *)&value2, 4);
146 }
147 EXPORT_SYMBOL_GPL(tpm_buf_append_u32);
148 
149 /**
150  * tpm_buf_read() - Read from a TPM buffer
151  * @buf:	&tpm_buf instance
152  * @offset:	offset within the buffer
153  * @count:	the number of bytes to read
154  * @output:	the output buffer
155  */
156 static void tpm_buf_read(struct tpm_buf *buf, off_t *offset, size_t count, void *output)
157 {
158 	off_t next_offset;
159 
160 	/* Return silently if overflow has already happened. */
161 	if (buf->flags & TPM_BUF_BOUNDARY_ERROR)
162 		return;
163 
164 	next_offset = *offset + count;
165 	if (next_offset > buf->length) {
166 		WARN(1, "tpm_buf: read out of boundary\n");
167 		buf->flags |= TPM_BUF_BOUNDARY_ERROR;
168 		return;
169 	}
170 
171 	memcpy(output, &buf->data[*offset], count);
172 	*offset = next_offset;
173 }
174 
175 /**
176  * tpm_buf_read_u8() - Read 8-bit word from a TPM buffer
177  * @buf:	&tpm_buf instance
178  * @offset:	offset within the buffer
179  *
180  * Return: next 8-bit word
181  */
182 u8 tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset)
183 {
184 	u8 value;
185 
186 	tpm_buf_read(buf, offset, sizeof(value), &value);
187 
188 	return value;
189 }
190 EXPORT_SYMBOL_GPL(tpm_buf_read_u8);
191 
192 /**
193  * tpm_buf_read_u16() - Read 16-bit word from a TPM buffer
194  * @buf:	&tpm_buf instance
195  * @offset:	offset within the buffer
196  *
197  * Return: next 16-bit word
198  */
199 u16 tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset)
200 {
201 	u16 value;
202 
203 	tpm_buf_read(buf, offset, sizeof(value), &value);
204 
205 	return be16_to_cpu(value);
206 }
207 EXPORT_SYMBOL_GPL(tpm_buf_read_u16);
208 
209 /**
210  * tpm_buf_read_u32() - Read 32-bit word from a TPM buffer
211  * @buf:	&tpm_buf instance
212  * @offset:	offset within the buffer
213  *
214  * Return: next 32-bit word
215  */
216 u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset)
217 {
218 	u32 value;
219 
220 	tpm_buf_read(buf, offset, sizeof(value), &value);
221 
222 	return be32_to_cpu(value);
223 }
224 EXPORT_SYMBOL_GPL(tpm_buf_read_u32);
225 
226 static u16 tpm_buf_tag(struct tpm_buf *buf)
227 {
228 	struct tpm_header *head = (struct tpm_header *)buf->data;
229 
230 	return be16_to_cpu(head->tag);
231 }
232 
233 /**
234  * tpm_buf_parameters - return the TPM response parameters area of the tpm_buf
235  * @buf: tpm_buf to use
236  *
237  * Where the parameters are located depends on the tag of a TPM
238  * command (it's immediately after the header for TPM_ST_NO_SESSIONS
239  * or 4 bytes after for TPM_ST_SESSIONS). Evaluate this and return a
240  * pointer to the first byte of the parameters area.
241  *
242  * @return: pointer to parameters area
243  */
244 u8 *tpm_buf_parameters(struct tpm_buf *buf)
245 {
246 	int offset = TPM_HEADER_SIZE;
247 
248 	if (tpm_buf_tag(buf) == TPM2_ST_SESSIONS)
249 		offset += 4;
250 
251 	return &buf->data[offset];
252 }
253