blob: 46e368d407454605fd1a4acfc99b8c31ccff4585 [file] [log] [blame]
Shawn Willden128ffe02014-08-06 12:31:33 -06001/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "google_keymaster_messages.h"
18
19namespace keymaster {
20
Shawn Willdenda8485e2014-08-17 08:00:01 -060021size_t KeymasterResponse::SerializedSize() const {
22 if (error != KM_ERROR_OK)
23 return sizeof(int32_t);
24 else
25 return sizeof(int32_t) + NonErrorSerializedSize();
Shawn Willden128ffe02014-08-06 12:31:33 -060026}
27
Shawn Willdenda8485e2014-08-17 08:00:01 -060028uint8_t* KeymasterResponse::Serialize(uint8_t* buf, const uint8_t* end) const {
29 buf = append_uint32_to_buf(buf, end, static_cast<uint32_t>(error));
30 if (error == KM_ERROR_OK)
31 buf = NonErrorSerialize(buf, end);
Shawn Willden128ffe02014-08-06 12:31:33 -060032 return buf;
33}
34
Shawn Willdenda8485e2014-08-17 08:00:01 -060035bool KeymasterResponse::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
36 if (!copy_uint32_from_buf(buf_ptr, end, &error))
37 return false;
38 if (error != KM_ERROR_OK)
39 return true;
40 return NonErrorDeserialize(buf_ptr, end);
41}
42
43size_t SupportedAlgorithmsResponse::NonErrorSerializedSize() const {
44 return sizeof(uint32_t) + sizeof(uint32_t) * algorithms_length;
45}
46
47uint8_t* SupportedAlgorithmsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
48 return append_uint32_array_to_buf(buf, end, algorithms, algorithms_length);
49}
50
51bool SupportedAlgorithmsResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
52 delete[] algorithms;
Shawn Willdenf2282b32014-08-25 06:49:54 -060053 algorithms = NULL;
54 UniquePtr<keymaster_algorithm_t[]> deserialized_algorithms;
55 if (!copy_uint32_array_from_buf(buf_ptr, end, &deserialized_algorithms, &algorithms_length))
56 return false;
57 algorithms = deserialized_algorithms.release();
58 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -060059}
60
61GenerateKeyResponse::~GenerateKeyResponse() {
62 delete[] key_blob.key_material;
63}
64
65size_t GenerateKeyResponse::NonErrorSerializedSize() const {
66 return sizeof(uint32_t) /* key size */ + key_blob.key_material_size +
Shawn Willdenf2282b32014-08-25 06:49:54 -060067 enforced.SerializedSize() + unenforced.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -060068}
69
70uint8_t* GenerateKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
71 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
Shawn Willdenda8485e2014-08-17 08:00:01 -060072 buf = enforced.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -060073 return unenforced.Serialize(buf, end);
74}
75
76bool GenerateKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willden128ffe02014-08-06 12:31:33 -060077 delete[] key_blob.key_material;
Shawn Willdenf2282b32014-08-25 06:49:54 -060078 key_blob.key_material = NULL;
79 UniquePtr<uint8_t[]> deserialized_key_material;
80 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
81 &deserialized_key_material) ||
82 !enforced.Deserialize(buf_ptr, end) || !unenforced.Deserialize(buf_ptr, end))
83 return false;
84 key_blob.key_material = deserialized_key_material.release();
85 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -060086}
Shawn Willden128ffe02014-08-06 12:31:33 -060087
Shawn Willdenda8485e2014-08-17 08:00:01 -060088GetKeyCharacteristicsRequest::~GetKeyCharacteristicsRequest() {
89 delete[] key_blob.key_material;
90}
91
92void GetKeyCharacteristicsRequest::SetKeyMaterial(const void* key_material, size_t length) {
93 delete[] key_blob.key_material;
94 key_blob.key_material = dup_buffer(key_material, length);
95 key_blob.key_material_size = length;
96}
97
98size_t GetKeyCharacteristicsRequest::SerializedSize() const {
99 return sizeof(uint32_t) /* key blob size */ + key_blob.key_material_size +
Shawn Willdenf2282b32014-08-25 06:49:54 -0600100 additional_params.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600101}
102
103uint8_t* GetKeyCharacteristicsRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
104 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600105 return additional_params.Serialize(buf, end);
106}
107
108bool GetKeyCharacteristicsRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600109 delete[] key_blob.key_material;
110 key_blob.key_material = NULL;
111 UniquePtr<uint8_t[]> deserialized_key_material;
112 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
113 &deserialized_key_material) ||
114 !additional_params.Deserialize(buf_ptr, end))
115 return false;
116 key_blob.key_material = deserialized_key_material.release();
117 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600118}
119
120size_t GetKeyCharacteristicsResponse::NonErrorSerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600121 return enforced.SerializedSize() + unenforced.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600122}
123
124uint8_t* GetKeyCharacteristicsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willdenda8485e2014-08-17 08:00:01 -0600125 buf = enforced.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600126 return unenforced.Serialize(buf, end);
127}
128
129bool GetKeyCharacteristicsResponse::NonErrorDeserialize(const uint8_t** buf_ptr,
130 const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600131 return enforced.Deserialize(buf_ptr, end) && unenforced.Deserialize(buf_ptr, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600132}
133
134void BeginOperationRequest::SetKeyMaterial(const void* key_material, size_t length) {
135 delete[] key_blob.key_material;
136 key_blob.key_material = dup_buffer(key_material, length);
137 key_blob.key_material_size = length;
138}
139
140size_t BeginOperationRequest::SerializedSize() const {
141 return sizeof(uint32_t) /* purpose */ + sizeof(uint32_t) /* key length */ +
142 key_blob.key_material_size + additional_params.SerializedSize();
143}
144
145uint8_t* BeginOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
146 buf = append_uint32_to_buf(buf, end, purpose);
147 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
148 return additional_params.Serialize(buf, end);
149}
150
151bool BeginOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600152 delete[] key_blob.key_material;
153 key_blob.key_material = 0;
154 UniquePtr<uint8_t[]> deserialized_key_material;
155 if (!copy_uint32_from_buf(buf_ptr, end, &purpose) ||
156 !copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
157 &deserialized_key_material) ||
158 !additional_params.Deserialize(buf_ptr, end))
159 return false;
160 key_blob.key_material = deserialized_key_material.release();
161 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600162}
163
164size_t BeginOperationResponse::NonErrorSerializedSize() const {
165 return sizeof(op_handle);
166}
167
168uint8_t* BeginOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
169 return append_uint64_to_buf(buf, end, op_handle);
170}
171
172bool BeginOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
173 return copy_uint64_from_buf(buf_ptr, end, &op_handle);
174}
175
176size_t UpdateOperationRequest::SerializedSize() const {
177 return sizeof(op_handle) + input.SerializedSize();
178}
179
180uint8_t* UpdateOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
181 buf = append_uint64_to_buf(buf, end, op_handle);
182 return input.Serialize(buf, end);
183}
184
185bool UpdateOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
186 return copy_uint64_from_buf(buf_ptr, end, &op_handle) && input.Deserialize(buf_ptr, end);
187}
188
189size_t UpdateOperationResponse::NonErrorSerializedSize() const {
190 return output.SerializedSize();
191}
192
193uint8_t* UpdateOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
194 return output.Serialize(buf, end);
195}
196
197bool UpdateOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
198 return output.Deserialize(buf_ptr, end);
199}
200
201size_t FinishOperationRequest::SerializedSize() const {
202 return sizeof(op_handle) + signature.SerializedSize();
203}
204
205uint8_t* FinishOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
206 buf = append_uint64_to_buf(buf, end, op_handle);
207 return signature.Serialize(buf, end);
208}
209
210bool FinishOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
211 return copy_uint64_from_buf(buf_ptr, end, &op_handle) && signature.Deserialize(buf_ptr, end);
212}
213
214size_t FinishOperationResponse::NonErrorSerializedSize() const {
215 return output.SerializedSize();
216}
217
218uint8_t* FinishOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
219 return output.Serialize(buf, end);
220}
221
222bool FinishOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
223 return output.Deserialize(buf_ptr, end);
224}
225
226void ImportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
227 delete[] key_data;
228 key_data = dup_buffer(key_material, length);
229 key_data_length = length;
230}
231
232size_t ImportKeyRequest::SerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600233 return key_description.SerializedSize() + sizeof(uint32_t) /* key_format */ +
234 sizeof(uint32_t) /* key_data_length */ + key_data_length;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600235}
236
237uint8_t* ImportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willden437fbd12014-08-20 11:59:49 -0600238 buf = key_description.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600239 buf = append_uint32_to_buf(buf, end, key_format);
240 return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
241}
242
243bool ImportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600244 delete[] key_data;
245 key_data = NULL;
246 UniquePtr<uint8_t[]> deserialized_key_material;
247 if (!key_description.Deserialize(buf_ptr, end) ||
248 !copy_uint32_from_buf(buf_ptr, end, &key_format) ||
249 !copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
250 return false;
251 key_data = deserialized_key_material.release();
252 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600253}
254
255void ImportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
256 delete[] key_blob.key_material;
257 key_blob.key_material = dup_buffer(key_material, length);
258 key_blob.key_material_size = length;
259}
260
261size_t ImportKeyResponse::NonErrorSerializedSize() const {
262 return sizeof(uint32_t) /* key_material length */ + key_blob.key_material_size +
Shawn Willdenf2282b32014-08-25 06:49:54 -0600263 enforced.SerializedSize() + unenforced.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600264}
265
266uint8_t* ImportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
267 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600268 buf = enforced.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600269 return unenforced.Serialize(buf, end);
270}
271
272bool ImportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600273 delete[] key_blob.key_material;
274 key_blob.key_material = NULL;
275 UniquePtr<uint8_t[]> deserialized_key_material;
276 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
277 &deserialized_key_material) ||
278 !enforced.Deserialize(buf_ptr, end) || !unenforced.Deserialize(buf_ptr, end))
279 return false;
280 key_blob.key_material = deserialized_key_material.release();
281 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600282}
283
284void ExportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
285 delete[] key_blob.key_material;
286 key_blob.key_material = dup_buffer(key_material, length);
287 key_blob.key_material_size = length;
288}
289
290size_t ExportKeyRequest::SerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600291 return additional_params.SerializedSize() + sizeof(uint32_t) /* key_format */ +
292 sizeof(uint32_t) /* key_material_size */ + key_blob.key_material_size;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600293}
294
295uint8_t* ExportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willdenda8485e2014-08-17 08:00:01 -0600296 buf = additional_params.Serialize(buf, end);
297 buf = append_uint32_to_buf(buf, end, key_format);
298 return append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
299}
300
301bool ExportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600302 delete[] key_blob.key_material;
303 key_blob.key_material = NULL;
304 UniquePtr<uint8_t[]> deserialized_key_material;
305 if (!additional_params.Deserialize(buf_ptr, end) ||
306 !copy_uint32_from_buf(buf_ptr, end, &key_format) ||
307 !copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
308 &deserialized_key_material))
309 return false;
310 key_blob.key_material = deserialized_key_material.release();
311 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600312}
313
314void ExportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
315 delete[] key_data;
316 key_data = dup_buffer(key_material, length);
317 key_data_length = length;
318}
319
320size_t ExportKeyResponse::NonErrorSerializedSize() const {
321 return sizeof(uint32_t) /* key_data_length */ + key_data_length;
322}
323
324uint8_t* ExportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
325 return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
326}
327
328bool ExportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600329 delete[] key_data;
330 key_data = NULL;
331 UniquePtr<uint8_t[]> deserialized_key_material;
332 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
333 return false;
334 key_data = deserialized_key_material.release();
335 return true;
Shawn Willden128ffe02014-08-06 12:31:33 -0600336}
337
338} // namespace keymaster