Add GoogleKeymaster. Very incomplete.
Change-Id: I53542c7132bd1a04afee93f3247b88ed7ed0bedc
diff --git a/google_keymaster_messages.cpp b/google_keymaster_messages.cpp
new file mode 100644
index 0000000..a8730b6
--- /dev/null
+++ b/google_keymaster_messages.cpp
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "google_keymaster_messages.h"
+
+namespace keymaster {
+
+GenerateKeyResponse::~GenerateKeyResponse() { delete[] key_blob.key_material; }
+
+size_t GenerateKeyResponse::SerializedSize() const {
+ if (error == KM_ERROR_OK) {
+ return sizeof(int32_t) /* error */ + sizeof(uint32_t) /* key size */ +
+ key_blob.key_material_size + sizeof(uint32_t) /* enforced size */ +
+ enforced.SerializedSize() + sizeof(uint32_t) /* unenforced size */ +
+ unenforced.SerializedSize();
+ } else {
+ return sizeof(error);
+ }
+}
+
+uint8_t* GenerateKeyResponse::Serialize(uint8_t* buf) const {
+ buf = append_to_buf(buf, static_cast<int32_t>(error));
+ if (error == KM_ERROR_OK) {
+ buf = append_size_and_data_to_buf(buf, key_blob.key_material, key_blob.key_material_size);
+ buf = append_to_buf(buf, static_cast<uint32_t>(enforced.SerializedSize()));
+ buf = enforced.Serialize(buf);
+ buf = append_to_buf(buf, static_cast<uint32_t>(unenforced.SerializedSize()));
+ buf = unenforced.Serialize(buf);
+ };
+ return buf;
+}
+
+bool GenerateKeyResponse::DeserializeToCopy(const uint8_t** buf, const uint8_t* end) {
+ delete[] key_blob.key_material;
+
+ if (!copy_from_buf(buf, end, &error))
+ return false;
+
+ if (end == *buf)
+ // Nothing but an error
+ return true;
+
+ uint32_t key_material_size;
+ if (!copy_from_buf(buf, end, &key_material_size) || end - *buf < key_material_size)
+ return false;
+
+ key_blob.key_material = new uint8_t[key_material_size];
+ key_blob.key_material_size = key_material_size;
+
+ if (key_blob.key_material == NULL ||
+ !copy_from_buf(buf, end, key_blob.key_material, key_blob.key_material_size))
+ return false;
+
+ uint32_t enforced_size;
+ if (!copy_from_buf(buf, end, &enforced_size) || end - *buf < enforced_size ||
+ !enforced.DeserializeToCopy(buf, *buf + enforced_size))
+ return false;
+
+ uint32_t unenforced_size;
+ if (!copy_from_buf(buf, end, &unenforced_size) || end - *buf < unenforced_size ||
+ !unenforced.DeserializeToCopy(buf, *buf + unenforced_size))
+ return false;
+
+ return true;
+}
+
+} // namespace keymaster