1 /*
2 * Copyright (C) 2023 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 <aidl/Gtest.h>
18 #include <aidl/Vintf.h>
19 #include <aidl/android/hardware/macsec/IMacsecPskPlugin.h>
20 #include <android-base/logging.h>
21 #include <android/binder_manager.h>
22 #include <gtest/gtest.h>
23
24 #include <chrono>
25 #include <thread>
26
27 using aidl::android::hardware::macsec::IMacsecPskPlugin;
28 using namespace std::chrono_literals;
29 using namespace std::string_literals;
30
31 const std::vector<uint8_t> CAK_ID_1 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01};
33 const std::vector<uint8_t> CAK_KEY_1 = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
34 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
35 const std::vector<uint8_t> CKN_1 = {0x31, 0x32, 0x33, 0x34}; // maximum 16 bytes
36 const std::vector<uint8_t> SAK_DATA_1 = {0x31, 0x32, 0x33, 0x34, 0x11, 0x12, 0x12, 0x14,
37 0x31, 0x32, 0x33, 0x34, 0x11, 0x12, 0x12, 0x14};
38 const std::vector<uint8_t> SAK_1 = {0x13, 0xD9, 0xEE, 0x5B, 0x26, 0x8B, 0x44, 0xFB,
39 0x37, 0x63, 0x3D, 0x41, 0xC8, 0xE7, 0x0D, 0x93};
40 const std::vector<uint8_t> WRAPPED_SAK_1 = {0x3B, 0x39, 0xAB, 0x4C, 0xD8, 0xDA, 0x2E, 0xC5,
41 0xD1, 0x38, 0x6A, 0x13, 0x9D, 0xE3, 0x78, 0xD9,
42 0x93, 0xD2, 0xA0, 0x70, 0x88, 0xCB, 0xF5, 0xEC};
43 const std::vector<uint8_t> DATA_1 = {0x31, 0x32, 0x33, 0x34, 0x31, 0x32, 0x34, 0x29,
44 0x51, 0x52, 0x53, 0x54, 0x51, 0x35, 0x54, 0x59};
45 const std::vector<uint8_t> ICV_1 = {0xDF, 0x54, 0xFF, 0xCD, 0xE0, 0xA9, 0x78, 0x10,
46 0x6B, 0x7B, 0xD2, 0xBF, 0xEF, 0xD9, 0x0C, 0x81};
47
48 const std::vector<uint8_t> CAK_ID_2 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02};
52 const std::vector<uint8_t> CAK_KEY_2 = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
53 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
54 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
55 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
56 const std::vector<uint8_t> CKN_2 = {0x35, 0x36, 0x37, 0x38}; // maximum 16 bytes
57 const std::vector<uint8_t> SAK_DATA_2 = {0x31, 0x32, 0x33, 0x34, 0x31, 0x32, 0x33, 0x34,
58 0x31, 0x32, 0x33, 0x34, 0x31, 0x32, 0x33, 0x34,
59 0x31, 0x32, 0x33, 0x34, 0x31, 0x32, 0x33, 0x34,
60 0x31, 0x32, 0x33, 0x34, 0x31, 0x32, 0x33, 0x34};
61 const std::vector<uint8_t> SAK_2 = {0x39, 0x09, 0x36, 0x60, 0x18, 0x07, 0x2B, 0x5D,
62 0xF0, 0x81, 0x81, 0x45, 0xCD, 0x71, 0xC6, 0xBA,
63 0x1D, 0x2B, 0x87, 0xC4, 0xEF, 0x79, 0x68, 0x82,
64 0x28, 0xD0, 0x25, 0x86, 0xD3, 0x63, 0xFF, 0x89};
65 const std::vector<uint8_t> WRAPPED_SAK_2 = {
66 0x2f, 0x6a, 0x22, 0x29, 0x68, 0x0e, 0x6e, 0x35, 0x91, 0x64, 0x05, 0x4a, 0x31, 0x8d,
67 0x35, 0xea, 0x95, 0x85, 0x40, 0xc6, 0xea, 0x55, 0xe5, 0xc5, 0x68, 0x40, 0xae, 0x4d,
68 0x6f, 0xeb, 0x73, 0xcd, 0x4e, 0x2a, 0x43, 0xb1, 0xda, 0x49, 0x4f, 0x0a};
69 const std::vector<uint8_t> DATA_2 = {0x71, 0x82, 0x13, 0x24, 0x31, 0x82, 0xA4, 0x2F,
70 0x51, 0x52, 0x53, 0x44, 0x21, 0x35, 0x54, 0x59};
71 const std::vector<uint8_t> ICV_2 = {0x8D, 0xF1, 0x1D, 0x6E, 0xAC, 0x62, 0xC1, 0x2A,
72 0xE8, 0xF8, 0x4E, 0xB1, 0x00, 0x45, 0x9A, 0xAD};
73
74 class MacsecAidlTest : public ::testing::TestWithParam<std::string> {
75 public:
SetUp()76 virtual void SetUp() override {
77 android::base::SetDefaultTag("MACSEC_HAL_VTS");
78 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
79 const auto instance = IMacsecPskPlugin::descriptor + "/default"s;
80 mMacsecPskPluginService = IMacsecPskPlugin::fromBinder(
81 ndk::SpAIBinder(AServiceManager_waitForService(instance.c_str())));
82
83 ASSERT_NE(mMacsecPskPluginService, nullptr);
84 auto aidlStatus = mMacsecPskPluginService->addTestKey(CAK_ID_1, CAK_KEY_1, CKN_1);
85 ASSERT_TRUE(aidlStatus.isOk());
86 aidlStatus = mMacsecPskPluginService->addTestKey(CAK_ID_2, CAK_KEY_2, CKN_2);
87 ASSERT_TRUE(aidlStatus.isOk());
88 }
TearDown()89 virtual void TearDown() override {}
90
91 std::shared_ptr<IMacsecPskPlugin> mMacsecPskPluginService;
92 };
93
TEST_P(MacsecAidlTest,calcIcv)94 TEST_P(MacsecAidlTest, calcIcv) {
95 std::vector<uint8_t> out;
96 auto aidlStatus = mMacsecPskPluginService->calcIcv(CAK_ID_1, DATA_1, &out);
97 ASSERT_TRUE(aidlStatus.isOk()) << "calcIcv KEY 1 failed: " << aidlStatus.getMessage();
98 EXPECT_EQ(out, ICV_1);
99
100 aidlStatus = mMacsecPskPluginService->calcIcv(CAK_ID_2, DATA_2, &out);
101 ASSERT_TRUE(aidlStatus.isOk()) << "calcIcv KEY 2 failed: " << aidlStatus.getMessage();
102 EXPECT_EQ(out, ICV_2);
103 }
104
TEST_P(MacsecAidlTest,generateSak)105 TEST_P(MacsecAidlTest, generateSak) {
106 std::vector<uint8_t> out;
107 auto aidlStatus = mMacsecPskPluginService->generateSak(CAK_ID_1, SAK_DATA_1, 16, &out);
108 ASSERT_TRUE(aidlStatus.isOk()) << "generateSak KEY 1 failed: " << aidlStatus.getMessage();
109 EXPECT_EQ(out, SAK_1);
110
111 aidlStatus = mMacsecPskPluginService->generateSak(CAK_ID_2, SAK_DATA_2, 32, &out);
112 ASSERT_TRUE(aidlStatus.isOk()) << "generateSak KEY 2 failed: " << aidlStatus.getMessage();
113 EXPECT_EQ(out, SAK_2);
114 }
115
TEST_P(MacsecAidlTest,wrapSak)116 TEST_P(MacsecAidlTest, wrapSak) {
117 std::vector<uint8_t> out;
118 auto aidlStatus = mMacsecPskPluginService->wrapSak(CAK_ID_1, SAK_1, &out);
119 ASSERT_TRUE(aidlStatus.isOk()) << "wrapSak KEY 1 failed: " << aidlStatus.getMessage();
120 EXPECT_EQ(out, WRAPPED_SAK_1);
121
122 aidlStatus = mMacsecPskPluginService->wrapSak(CAK_ID_2, SAK_2, &out);
123 ASSERT_TRUE(aidlStatus.isOk()) << "wrapSak KEY 2 failed: " << aidlStatus.getMessage();
124 EXPECT_EQ(out, WRAPPED_SAK_2);
125 }
126
TEST_P(MacsecAidlTest,unwrapSak)127 TEST_P(MacsecAidlTest, unwrapSak) {
128 std::vector<uint8_t> out;
129 auto aidlStatus = mMacsecPskPluginService->unwrapSak(CAK_ID_1, WRAPPED_SAK_1, &out);
130 ASSERT_TRUE(aidlStatus.isOk()) << "unwrapSak KEY 1 failed: " << aidlStatus.getMessage();
131 EXPECT_EQ(out, SAK_1);
132
133 aidlStatus = mMacsecPskPluginService->unwrapSak(CAK_ID_2, WRAPPED_SAK_2, &out);
134 ASSERT_TRUE(aidlStatus.isOk()) << "unwrapSak KEY 2 failed: " << aidlStatus.getMessage();
135 EXPECT_EQ(out, SAK_2);
136 }
137
138 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MacsecAidlTest);
139 INSTANTIATE_TEST_SUITE_P(
140 PerInstance, MacsecAidlTest,
141 testing::ValuesIn(android::getAidlHalInstanceNames(IMacsecPskPlugin::descriptor)),
142 android::PrintInstanceNameToString);
143