blob: 2bc6f7302e67250e20f723838eccded579d496a3 [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
Shawn Willden98d9b922014-08-26 08:14:10 -060017#include <keymaster/google_keymaster_messages.h>
18#include <keymaster/google_keymaster_utils.h>
Shawn Willden128ffe02014-08-06 12:31:33 -060019
20namespace keymaster {
21
Shawn Willdenda8485e2014-08-17 08:00:01 -060022size_t KeymasterResponse::SerializedSize() const {
23 if (error != KM_ERROR_OK)
24 return sizeof(int32_t);
25 else
26 return sizeof(int32_t) + NonErrorSerializedSize();
Shawn Willden128ffe02014-08-06 12:31:33 -060027}
28
Shawn Willdenda8485e2014-08-17 08:00:01 -060029uint8_t* KeymasterResponse::Serialize(uint8_t* buf, const uint8_t* end) const {
30 buf = append_uint32_to_buf(buf, end, static_cast<uint32_t>(error));
31 if (error == KM_ERROR_OK)
32 buf = NonErrorSerialize(buf, end);
Shawn Willden128ffe02014-08-06 12:31:33 -060033 return buf;
34}
35
Shawn Willdenda8485e2014-08-17 08:00:01 -060036bool KeymasterResponse::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
37 if (!copy_uint32_from_buf(buf_ptr, end, &error))
38 return false;
39 if (error != KM_ERROR_OK)
40 return true;
41 return NonErrorDeserialize(buf_ptr, end);
42}
43
44size_t SupportedAlgorithmsResponse::NonErrorSerializedSize() const {
45 return sizeof(uint32_t) + sizeof(uint32_t) * algorithms_length;
46}
47
48uint8_t* SupportedAlgorithmsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
49 return append_uint32_array_to_buf(buf, end, algorithms, algorithms_length);
50}
51
52bool SupportedAlgorithmsResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
53 delete[] algorithms;
Shawn Willdenf2282b32014-08-25 06:49:54 -060054 algorithms = NULL;
55 UniquePtr<keymaster_algorithm_t[]> deserialized_algorithms;
56 if (!copy_uint32_array_from_buf(buf_ptr, end, &deserialized_algorithms, &algorithms_length))
57 return false;
58 algorithms = deserialized_algorithms.release();
59 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -060060}
61
62GenerateKeyResponse::~GenerateKeyResponse() {
63 delete[] key_blob.key_material;
64}
65
66size_t GenerateKeyResponse::NonErrorSerializedSize() const {
67 return sizeof(uint32_t) /* key size */ + key_blob.key_material_size +
Shawn Willdenf2282b32014-08-25 06:49:54 -060068 enforced.SerializedSize() + unenforced.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -060069}
70
71uint8_t* GenerateKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
72 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 -060073 buf = enforced.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -060074 return unenforced.Serialize(buf, end);
75}
76
77bool GenerateKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willden128ffe02014-08-06 12:31:33 -060078 delete[] key_blob.key_material;
Shawn Willdenf2282b32014-08-25 06:49:54 -060079 key_blob.key_material = NULL;
80 UniquePtr<uint8_t[]> deserialized_key_material;
81 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
82 &deserialized_key_material) ||
83 !enforced.Deserialize(buf_ptr, end) || !unenforced.Deserialize(buf_ptr, end))
84 return false;
85 key_blob.key_material = deserialized_key_material.release();
86 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -060087}
Shawn Willden128ffe02014-08-06 12:31:33 -060088
Shawn Willdenda8485e2014-08-17 08:00:01 -060089GetKeyCharacteristicsRequest::~GetKeyCharacteristicsRequest() {
90 delete[] key_blob.key_material;
91}
92
93void GetKeyCharacteristicsRequest::SetKeyMaterial(const void* key_material, size_t length) {
94 delete[] key_blob.key_material;
95 key_blob.key_material = dup_buffer(key_material, length);
96 key_blob.key_material_size = length;
97}
98
99size_t GetKeyCharacteristicsRequest::SerializedSize() const {
100 return sizeof(uint32_t) /* key blob size */ + key_blob.key_material_size +
Shawn Willdenf2282b32014-08-25 06:49:54 -0600101 additional_params.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600102}
103
104uint8_t* GetKeyCharacteristicsRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
105 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 -0600106 return additional_params.Serialize(buf, end);
107}
108
109bool GetKeyCharacteristicsRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600110 delete[] key_blob.key_material;
111 key_blob.key_material = NULL;
112 UniquePtr<uint8_t[]> deserialized_key_material;
113 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
114 &deserialized_key_material) ||
115 !additional_params.Deserialize(buf_ptr, end))
116 return false;
117 key_blob.key_material = deserialized_key_material.release();
118 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600119}
120
121size_t GetKeyCharacteristicsResponse::NonErrorSerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600122 return enforced.SerializedSize() + unenforced.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600123}
124
125uint8_t* GetKeyCharacteristicsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willdenda8485e2014-08-17 08:00:01 -0600126 buf = enforced.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600127 return unenforced.Serialize(buf, end);
128}
129
130bool GetKeyCharacteristicsResponse::NonErrorDeserialize(const uint8_t** buf_ptr,
131 const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600132 return enforced.Deserialize(buf_ptr, end) && unenforced.Deserialize(buf_ptr, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600133}
134
135void BeginOperationRequest::SetKeyMaterial(const void* key_material, size_t length) {
136 delete[] key_blob.key_material;
137 key_blob.key_material = dup_buffer(key_material, length);
138 key_blob.key_material_size = length;
139}
140
141size_t BeginOperationRequest::SerializedSize() const {
142 return sizeof(uint32_t) /* purpose */ + sizeof(uint32_t) /* key length */ +
143 key_blob.key_material_size + additional_params.SerializedSize();
144}
145
146uint8_t* BeginOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
147 buf = append_uint32_to_buf(buf, end, purpose);
148 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
149 return additional_params.Serialize(buf, end);
150}
151
152bool BeginOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600153 delete[] key_blob.key_material;
154 key_blob.key_material = 0;
155 UniquePtr<uint8_t[]> deserialized_key_material;
156 if (!copy_uint32_from_buf(buf_ptr, end, &purpose) ||
157 !copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
158 &deserialized_key_material) ||
159 !additional_params.Deserialize(buf_ptr, end))
160 return false;
161 key_blob.key_material = deserialized_key_material.release();
162 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600163}
164
165size_t BeginOperationResponse::NonErrorSerializedSize() const {
Shawn Willden7b38f442015-01-22 13:07:34 -0700166 if (message_version == 0)
167 return sizeof(op_handle);
168 else
169 return sizeof(op_handle) + output_params.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600170}
171
172uint8_t* BeginOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willden7b38f442015-01-22 13:07:34 -0700173 buf = append_uint64_to_buf(buf, end, op_handle);
174 if (message_version > 0)
175 buf = output_params.Serialize(buf, end);
176 return buf;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600177}
178
179bool BeginOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willden7b38f442015-01-22 13:07:34 -0700180 bool retval = copy_uint64_from_buf(buf_ptr, end, &op_handle);
181 if (retval && message_version > 0)
182 retval = output_params.Deserialize(buf_ptr, end);
183 return retval;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600184}
185
186size_t UpdateOperationRequest::SerializedSize() const {
Shawn Willdenba5e94d2015-02-06 17:09:23 -0700187 if (message_version == 0)
188 return sizeof(op_handle) + input.SerializedSize();
189 else
190 return sizeof(op_handle) + input.SerializedSize() + additional_params.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600191}
192
193uint8_t* UpdateOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
194 buf = append_uint64_to_buf(buf, end, op_handle);
Shawn Willdenba5e94d2015-02-06 17:09:23 -0700195 buf = input.Serialize(buf, end);
196 if (message_version > 0)
197 buf = additional_params.Serialize(buf, end);
198 return buf;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600199}
200
201bool UpdateOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenba5e94d2015-02-06 17:09:23 -0700202 bool retval = copy_uint64_from_buf(buf_ptr, end, &op_handle) && input.Deserialize(buf_ptr, end);
203 if (retval && message_version > 0)
204 retval = additional_params.Deserialize(buf_ptr, end);
205 return retval;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600206}
207
208size_t UpdateOperationResponse::NonErrorSerializedSize() const {
Shawn Willdenb7361132014-12-08 08:15:14 -0700209 if (message_version == 0)
210 return output.SerializedSize();
211 else
212 return output.SerializedSize() + sizeof(uint32_t);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600213}
214
215uint8_t* UpdateOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willdenb7361132014-12-08 08:15:14 -0700216 buf = output.Serialize(buf, end);
217 if (message_version > 0)
218 buf = append_uint32_to_buf(buf, end, input_consumed);
219 return buf;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600220}
221
222bool UpdateOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenb7361132014-12-08 08:15:14 -0700223 bool retval = output.Deserialize(buf_ptr, end);
224 if (retval && message_version > 0)
225 retval = copy_uint32_from_buf(buf_ptr, end, &input_consumed);
226 return retval;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600227}
228
229size_t FinishOperationRequest::SerializedSize() const {
Shawn Willdenba5e94d2015-02-06 17:09:23 -0700230 if (message_version == 0)
231 return sizeof(op_handle) + signature.SerializedSize();
232 else
233 return sizeof(op_handle) + signature.SerializedSize() + additional_params.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600234}
235
236uint8_t* FinishOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
237 buf = append_uint64_to_buf(buf, end, op_handle);
Shawn Willdenba5e94d2015-02-06 17:09:23 -0700238 buf = signature.Serialize(buf, end);
239 if (message_version > 0)
240 buf = additional_params.Serialize(buf, end);
241 return buf;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600242}
243
244bool FinishOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenba5e94d2015-02-06 17:09:23 -0700245 bool retval =
246 copy_uint64_from_buf(buf_ptr, end, &op_handle) && signature.Deserialize(buf_ptr, end);
247 if (retval && message_version > 0)
248 retval = additional_params.Deserialize(buf_ptr, end);
249 return retval;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600250}
251
252size_t FinishOperationResponse::NonErrorSerializedSize() const {
253 return output.SerializedSize();
254}
255
256uint8_t* FinishOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
257 return output.Serialize(buf, end);
258}
259
260bool FinishOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
261 return output.Deserialize(buf_ptr, end);
262}
263
Shawn Willdencd695822015-01-26 14:06:32 -0700264size_t AddEntropyRequest::SerializedSize() const {
265 return random_data.SerializedSize();
266}
267
268uint8_t* AddEntropyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
269 return random_data.Serialize(buf, end);
270}
271
272bool AddEntropyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
273 return random_data.Deserialize(buf_ptr, end);
274}
275
Shawn Willdenda8485e2014-08-17 08:00:01 -0600276void ImportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
277 delete[] key_data;
278 key_data = dup_buffer(key_material, length);
279 key_data_length = length;
280}
281
282size_t ImportKeyRequest::SerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600283 return key_description.SerializedSize() + sizeof(uint32_t) /* key_format */ +
284 sizeof(uint32_t) /* key_data_length */ + key_data_length;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600285}
286
287uint8_t* ImportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willden437fbd12014-08-20 11:59:49 -0600288 buf = key_description.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600289 buf = append_uint32_to_buf(buf, end, key_format);
290 return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
291}
292
293bool ImportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600294 delete[] key_data;
295 key_data = NULL;
296 UniquePtr<uint8_t[]> deserialized_key_material;
297 if (!key_description.Deserialize(buf_ptr, end) ||
298 !copy_uint32_from_buf(buf_ptr, end, &key_format) ||
299 !copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
300 return false;
301 key_data = deserialized_key_material.release();
302 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600303}
304
305void ImportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
306 delete[] key_blob.key_material;
307 key_blob.key_material = dup_buffer(key_material, length);
308 key_blob.key_material_size = length;
309}
310
311size_t ImportKeyResponse::NonErrorSerializedSize() const {
312 return sizeof(uint32_t) /* key_material length */ + key_blob.key_material_size +
Shawn Willdenf2282b32014-08-25 06:49:54 -0600313 enforced.SerializedSize() + unenforced.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600314}
315
316uint8_t* ImportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
317 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 -0600318 buf = enforced.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600319 return unenforced.Serialize(buf, end);
320}
321
322bool ImportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600323 delete[] key_blob.key_material;
324 key_blob.key_material = NULL;
325 UniquePtr<uint8_t[]> deserialized_key_material;
326 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
327 &deserialized_key_material) ||
328 !enforced.Deserialize(buf_ptr, end) || !unenforced.Deserialize(buf_ptr, end))
329 return false;
330 key_blob.key_material = deserialized_key_material.release();
331 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600332}
333
334void ExportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
335 delete[] key_blob.key_material;
336 key_blob.key_material = dup_buffer(key_material, length);
337 key_blob.key_material_size = length;
338}
339
340size_t ExportKeyRequest::SerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600341 return additional_params.SerializedSize() + sizeof(uint32_t) /* key_format */ +
342 sizeof(uint32_t) /* key_material_size */ + key_blob.key_material_size;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600343}
344
345uint8_t* ExportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willdenda8485e2014-08-17 08:00:01 -0600346 buf = additional_params.Serialize(buf, end);
347 buf = append_uint32_to_buf(buf, end, key_format);
348 return append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
349}
350
351bool ExportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600352 delete[] key_blob.key_material;
353 key_blob.key_material = NULL;
354 UniquePtr<uint8_t[]> deserialized_key_material;
355 if (!additional_params.Deserialize(buf_ptr, end) ||
356 !copy_uint32_from_buf(buf_ptr, end, &key_format) ||
357 !copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
358 &deserialized_key_material))
359 return false;
360 key_blob.key_material = deserialized_key_material.release();
361 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600362}
363
364void ExportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
365 delete[] key_data;
366 key_data = dup_buffer(key_material, length);
367 key_data_length = length;
368}
369
370size_t ExportKeyResponse::NonErrorSerializedSize() const {
371 return sizeof(uint32_t) /* key_data_length */ + key_data_length;
372}
373
374uint8_t* ExportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
375 return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
376}
377
378bool ExportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600379 delete[] key_data;
380 key_data = NULL;
381 UniquePtr<uint8_t[]> deserialized_key_material;
382 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
383 return false;
384 key_data = deserialized_key_material.release();
385 return true;
Shawn Willden128ffe02014-08-06 12:31:33 -0600386}
387
Shawn Willden2665e862014-11-24 14:46:21 -0700388size_t GetVersionResponse::NonErrorSerializedSize() const {
389 return sizeof(major_ver) + sizeof(minor_ver) + sizeof(subminor_ver);
390}
391
392uint8_t* GetVersionResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
393 if (buf + NonErrorSerializedSize() <= end) {
394 *buf++ = major_ver;
395 *buf++ = minor_ver;
396 *buf++ = subminor_ver;
397 } else {
398 buf += NonErrorSerializedSize();
399 }
400 return buf;
401}
402
403bool GetVersionResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
404 if (*buf_ptr + NonErrorSerializedSize() > end)
405 return false;
406 const uint8_t* tmp = *buf_ptr;
407 major_ver = *tmp++;
408 minor_ver = *tmp++;
409 subminor_ver = *tmp++;
410 *buf_ptr = tmp;
411 return true;
412}
413
Shawn Willden128ffe02014-08-06 12:31:33 -0600414} // namespace keymaster