1 /*
2 * Copyright (C) 2021 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 "host/commands/secure_env/encrypted_serializable.h"
18
19 #include <gtest/gtest.h>
20 #include <keymaster/serializable.h>
21 #include <string.h>
22
23 #include "host/commands/secure_env/primary_key_builder.h"
24 #include "host/commands/secure_env/test_tpm.h"
25 #include "host/commands/secure_env/tpm_resource_manager.h"
26
27 namespace cuttlefish {
28
TEST(TpmEncryptedSerializable,BinaryData)29 TEST(TpmEncryptedSerializable, BinaryData) {
30 TestTpm tpm;
31 TpmResourceManager resource_manager(tpm.Esys());
32
33 uint8_t input_data[] = {1, 2, 3, 4, 5};
34 keymaster::Buffer input(input_data, sizeof(input_data));
35 EncryptedSerializable encrypt_input(resource_manager,
36 ParentKeyCreator("test"), input);
37
38 std::vector<uint8_t> encrypted_data(encrypt_input.SerializedSize());
39 auto encrypt_return = encrypt_input.Serialize(
40 encrypted_data.data(), encrypted_data.data() + encrypted_data.size());
41
42 keymaster::Buffer output(sizeof(input_data));
43 EncryptedSerializable decrypt_intermediate(resource_manager,
44 ParentKeyCreator("test"), output);
45 const uint8_t* encrypted_data_ptr = encrypted_data.data();
46 auto decrypt_return = decrypt_intermediate.Deserialize(
47 &encrypted_data_ptr, encrypted_data_ptr + encrypted_data.size());
48
49 ASSERT_EQ(encrypt_return, encrypted_data.data() + encrypted_data.size());
50 ASSERT_TRUE(decrypt_return);
51 ASSERT_EQ(encrypted_data_ptr, encrypted_data.data() + encrypted_data.size());
52 ASSERT_EQ(0, memcmp(input_data, output.begin(), sizeof(input_data)));
53 }
54
55 } // namespace cuttlefish
56