blob: 109f3552eed057a962c89ec7765e191bd87c279e [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;
53 return copy_uint32_array_from_buf(buf_ptr, end, &algorithms, &algorithms_length);
54}
55
56GenerateKeyResponse::~GenerateKeyResponse() {
57 delete[] key_blob.key_material;
58}
59
60size_t GenerateKeyResponse::NonErrorSerializedSize() const {
61 return sizeof(uint32_t) /* key size */ + key_blob.key_material_size +
62 sizeof(uint32_t) /* enforced size */ + enforced.SerializedSize() +
63 sizeof(uint32_t) /* unenforced size */ + unenforced.SerializedSize();
64}
65
66uint8_t* GenerateKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
67 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
68 buf = append_uint32_to_buf(buf, end, enforced.SerializedSize());
69 buf = enforced.Serialize(buf, end);
70 buf = append_uint32_to_buf(buf, end, unenforced.SerializedSize());
71 return unenforced.Serialize(buf, end);
72}
73
74bool GenerateKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willden128ffe02014-08-06 12:31:33 -060075 delete[] key_blob.key_material;
76
Shawn Willden128ffe02014-08-06 12:31:33 -060077 uint32_t enforced_size;
Shawn Willden128ffe02014-08-06 12:31:33 -060078 uint32_t unenforced_size;
Shawn Willdenda8485e2014-08-17 08:00:01 -060079 return copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
80 &key_blob.key_material) &&
81 copy_uint32_from_buf(buf_ptr, end, &enforced_size) &&
82 enforced.Deserialize(buf_ptr, *buf_ptr + enforced_size) &&
83 copy_uint32_from_buf(buf_ptr, end, &unenforced_size) &&
84 unenforced.Deserialize(buf_ptr, *buf_ptr + unenforced_size);
85}
Shawn Willden128ffe02014-08-06 12:31:33 -060086
Shawn Willdenda8485e2014-08-17 08:00:01 -060087GetKeyCharacteristicsRequest::~GetKeyCharacteristicsRequest() {
88 delete[] key_blob.key_material;
89}
90
91void GetKeyCharacteristicsRequest::SetKeyMaterial(const void* key_material, size_t length) {
92 delete[] key_blob.key_material;
93 key_blob.key_material = dup_buffer(key_material, length);
94 key_blob.key_material_size = length;
95}
96
97size_t GetKeyCharacteristicsRequest::SerializedSize() const {
98 return sizeof(uint32_t) /* key blob size */ + key_blob.key_material_size +
99 sizeof(uint32_t) /* size of additional_params */ + additional_params.SerializedSize();
100}
101
102uint8_t* GetKeyCharacteristicsRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
103 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
104 buf = append_uint32_to_buf(buf, end, additional_params.SerializedSize());
105 return additional_params.Serialize(buf, end);
106}
107
108bool GetKeyCharacteristicsRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
109 uint8_t additional_params_size;
110 return copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
111 &key_blob.key_material) &&
112 copy_uint32_from_buf(buf_ptr, end, &additional_params_size) &&
113 additional_params.Deserialize(buf_ptr, *buf_ptr + additional_params_size);
114}
115
116size_t GetKeyCharacteristicsResponse::NonErrorSerializedSize() const {
117 return sizeof(uint32_t) /* enforced size */ + enforced.SerializedSize() +
118 sizeof(uint32_t) /* unenforced size */ + unenforced.SerializedSize();
119}
120
121uint8_t* GetKeyCharacteristicsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
122 buf = append_uint32_to_buf(buf, end, enforced.SerializedSize());
123 buf = enforced.Serialize(buf, end);
124 buf = append_uint32_to_buf(buf, end, unenforced.SerializedSize());
125 return unenforced.Serialize(buf, end);
126}
127
128bool GetKeyCharacteristicsResponse::NonErrorDeserialize(const uint8_t** buf_ptr,
129 const uint8_t* end) {
130 uint32_t enforced_size;
131 uint32_t unenforced_size;
132 return copy_uint32_from_buf(buf_ptr, end, &enforced_size) &&
133 enforced.Deserialize(buf_ptr, *buf_ptr + enforced_size) &&
134 copy_uint32_from_buf(buf_ptr, end, &unenforced_size) &&
135 unenforced.Deserialize(buf_ptr, *buf_ptr + unenforced_size);
136}
137
138void BeginOperationRequest::SetKeyMaterial(const void* key_material, size_t length) {
139 delete[] key_blob.key_material;
140 key_blob.key_material = dup_buffer(key_material, length);
141 key_blob.key_material_size = length;
142}
143
144size_t BeginOperationRequest::SerializedSize() const {
145 return sizeof(uint32_t) /* purpose */ + sizeof(uint32_t) /* key length */ +
146 key_blob.key_material_size + additional_params.SerializedSize();
147}
148
149uint8_t* BeginOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
150 buf = append_uint32_to_buf(buf, end, purpose);
151 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
152 return additional_params.Serialize(buf, end);
153}
154
155bool BeginOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
156 return copy_uint32_from_buf(buf_ptr, end, &purpose) &&
157 copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
158 &key_blob.key_material) &&
159 additional_params.Deserialize(buf_ptr, end);
160}
161
162size_t BeginOperationResponse::NonErrorSerializedSize() const {
163 return sizeof(op_handle);
164}
165
166uint8_t* BeginOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
167 return append_uint64_to_buf(buf, end, op_handle);
168}
169
170bool BeginOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
171 return copy_uint64_from_buf(buf_ptr, end, &op_handle);
172}
173
174size_t UpdateOperationRequest::SerializedSize() const {
175 return sizeof(op_handle) + input.SerializedSize();
176}
177
178uint8_t* UpdateOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
179 buf = append_uint64_to_buf(buf, end, op_handle);
180 return input.Serialize(buf, end);
181}
182
183bool UpdateOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
184 return copy_uint64_from_buf(buf_ptr, end, &op_handle) && input.Deserialize(buf_ptr, end);
185}
186
187size_t UpdateOperationResponse::NonErrorSerializedSize() const {
188 return output.SerializedSize();
189}
190
191uint8_t* UpdateOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
192 return output.Serialize(buf, end);
193}
194
195bool UpdateOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
196 return output.Deserialize(buf_ptr, end);
197}
198
199size_t FinishOperationRequest::SerializedSize() const {
200 return sizeof(op_handle) + signature.SerializedSize();
201}
202
203uint8_t* FinishOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
204 buf = append_uint64_to_buf(buf, end, op_handle);
205 return signature.Serialize(buf, end);
206}
207
208bool FinishOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
209 return copy_uint64_from_buf(buf_ptr, end, &op_handle) && signature.Deserialize(buf_ptr, end);
210}
211
212size_t FinishOperationResponse::NonErrorSerializedSize() const {
213 return output.SerializedSize();
214}
215
216uint8_t* FinishOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
217 return output.Serialize(buf, end);
218}
219
220bool FinishOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
221 return output.Deserialize(buf_ptr, end);
222}
223
224void ImportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
225 delete[] key_data;
226 key_data = dup_buffer(key_material, length);
227 key_data_length = length;
228}
229
230size_t ImportKeyRequest::SerializedSize() const {
Shawn Willden437fbd12014-08-20 11:59:49 -0600231 return sizeof(uint32_t) /* additional_params size */ + key_description.SerializedSize() +
Shawn Willdenda8485e2014-08-17 08:00:01 -0600232 sizeof(uint32_t) /* key_format */ + sizeof(uint32_t) /* key_data_length */ +
233 key_data_length;
234}
235
236uint8_t* ImportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willden437fbd12014-08-20 11:59:49 -0600237 buf = append_uint32_to_buf(buf, end, key_description.SerializedSize());
238 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) {
244 uint32_t additional_params_size;
245 return copy_uint32_from_buf(buf_ptr, end, &additional_params_size) &&
Shawn Willden437fbd12014-08-20 11:59:49 -0600246 key_description.Deserialize(buf_ptr, *buf_ptr + additional_params_size) &&
Shawn Willdenda8485e2014-08-17 08:00:01 -0600247 copy_uint32_from_buf(buf_ptr, end, &key_format) &&
248 copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &key_data);
249}
250
251void ImportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
252 delete[] key_blob.key_material;
253 key_blob.key_material = dup_buffer(key_material, length);
254 key_blob.key_material_size = length;
255}
256
257size_t ImportKeyResponse::NonErrorSerializedSize() const {
258 return sizeof(uint32_t) /* key_material length */ + key_blob.key_material_size +
259 sizeof(uint32_t) /* enforced length */ + enforced.SerializedSize() +
260 sizeof(uint32_t) /* unenforced length */ + unenforced.SerializedSize();
261}
262
263uint8_t* ImportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
264 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
265 buf = append_uint32_to_buf(buf, end, enforced.SerializedSize());
266 buf = enforced.Serialize(buf, end);
267 buf = append_uint32_to_buf(buf, end, unenforced.SerializedSize());
268 return unenforced.Serialize(buf, end);
269}
270
271bool ImportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
272 uint8_t enforced_size;
273 uint8_t unenforced_size;
274 return copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
275 &key_blob.key_material) &&
276 copy_uint32_from_buf(buf_ptr, end, &enforced_size) &&
277 enforced.Deserialize(buf_ptr, *buf_ptr + enforced_size) &&
278 copy_uint32_from_buf(buf_ptr, end, &unenforced_size) &&
279 unenforced.Deserialize(buf_ptr, *buf_ptr + enforced_size);
280}
281
282void ExportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
283 delete[] key_blob.key_material;
284 key_blob.key_material = dup_buffer(key_material, length);
285 key_blob.key_material_size = length;
286}
287
288size_t ExportKeyRequest::SerializedSize() const {
289 return sizeof(uint32_t) /* aditional_data length */ + additional_params.SerializedSize() +
290 sizeof(uint32_t) /* key_format */ + sizeof(uint32_t) /* key_material_size */ +
291 key_blob.key_material_size;
292}
293
294uint8_t* ExportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
295 buf = append_uint32_to_buf(buf, end, additional_params.SerializedSize());
296 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) {
302 size_t additional_params_size;
303 return copy_uint32_from_buf(buf_ptr, end, &additional_params_size) &&
304 additional_params.Deserialize(buf_ptr, *buf_ptr + additional_params_size) &&
305 copy_uint32_from_buf(buf_ptr, end, &key_format) &&
306 copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
307 &key_blob.key_material);
308}
309
310void ExportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
311 delete[] key_data;
312 key_data = dup_buffer(key_material, length);
313 key_data_length = length;
314}
315
316size_t ExportKeyResponse::NonErrorSerializedSize() const {
317 return sizeof(uint32_t) /* key_data_length */ + key_data_length;
318}
319
320uint8_t* ExportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
321 return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
322}
323
324bool ExportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
325 return copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &key_data);
Shawn Willden128ffe02014-08-06 12:31:33 -0600326}
327
328} // namespace keymaster