blob: 403f87ab7e9eeb0e350c25c800ca2e5ed927f0be [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
21GenerateKeyResponse::~GenerateKeyResponse() { delete[] key_blob.key_material; }
22
23size_t GenerateKeyResponse::SerializedSize() const {
24 if (error == KM_ERROR_OK) {
25 return sizeof(int32_t) /* error */ + sizeof(uint32_t) /* key size */ +
26 key_blob.key_material_size + sizeof(uint32_t) /* enforced size */ +
27 enforced.SerializedSize() + sizeof(uint32_t) /* unenforced size */ +
28 unenforced.SerializedSize();
29 } else {
30 return sizeof(error);
31 }
32}
33
Shawn Willden58e1a542014-08-08 21:58:29 -060034uint8_t* GenerateKeyResponse::Serialize(uint8_t* buf, const uint8_t* end) const {
35 buf = append_to_buf(buf, end, static_cast<int32_t>(error));
Shawn Willden128ffe02014-08-06 12:31:33 -060036 if (error == KM_ERROR_OK) {
Shawn Willden58e1a542014-08-08 21:58:29 -060037 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material,
38 key_blob.key_material_size);
39 buf = append_to_buf(buf, end, static_cast<uint32_t>(enforced.SerializedSize()));
40 buf = enforced.Serialize(buf, end);
41 buf = append_to_buf(buf, end, static_cast<uint32_t>(unenforced.SerializedSize()));
42 buf = unenforced.Serialize(buf, end);
Shawn Willden128ffe02014-08-06 12:31:33 -060043 };
44 return buf;
45}
46
Shawn Willden58e1a542014-08-08 21:58:29 -060047bool GenerateKeyResponse::Deserialize(const uint8_t** buf, const uint8_t* end) {
Shawn Willden128ffe02014-08-06 12:31:33 -060048 delete[] key_blob.key_material;
49
50 if (!copy_from_buf(buf, end, &error))
51 return false;
52
53 if (end == *buf)
54 // Nothing but an error
55 return true;
56
57 uint32_t key_material_size;
58 if (!copy_from_buf(buf, end, &key_material_size) || end - *buf < key_material_size)
59 return false;
60
61 key_blob.key_material = new uint8_t[key_material_size];
62 key_blob.key_material_size = key_material_size;
63
64 if (key_blob.key_material == NULL ||
65 !copy_from_buf(buf, end, key_blob.key_material, key_blob.key_material_size))
66 return false;
67
68 uint32_t enforced_size;
69 if (!copy_from_buf(buf, end, &enforced_size) || end - *buf < enforced_size ||
Shawn Willden58e1a542014-08-08 21:58:29 -060070 !enforced.Deserialize(buf, *buf + enforced_size))
Shawn Willden128ffe02014-08-06 12:31:33 -060071 return false;
72
73 uint32_t unenforced_size;
74 if (!copy_from_buf(buf, end, &unenforced_size) || end - *buf < unenforced_size ||
Shawn Willden58e1a542014-08-08 21:58:29 -060075 !unenforced.Deserialize(buf, *buf + unenforced_size))
Shawn Willden128ffe02014-08-06 12:31:33 -060076 return false;
77
78 return true;
79}
80
81} // namespace keymaster