1 /*
2 * Copyright 2017 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 <keymaster/km_openssl/ckdf.h>
18
19 #include <gtest/gtest.h>
20 #include <string.h>
21
22 #include "android_keymaster_test_utils.h"
23
24 using std::string;
25
26 namespace keymaster {
27 namespace test {
28
29 struct CkdfTest {
30 const char* key;
31 const char* label;
32 const char* context;
33 const char* output;
34 };
35
36 static const CkdfTest kCkdfTests[] = {{"80583f389dd797a3d18abd7b9399da02"
37 "6fecb1eade7bc2f0ef091ad39e613c35",
38 "c16e6e02c5a3dcc8d78b9ac1306877761310455b4e41469951d9e6",
39 "c2245a064b33fd8c3b01203a7824485bf0a64060c4648b707d260793",
40 "5d42a1941670917d746c7278e75f4879"
41 "750469dcb59c129e42edb7a3273f38d4"
42 "ea6fbcba9f422f735fc2db23603c63e5"
43 "86ff39cc048f4bc18690e478dd1108fa"
44 "fc635b29acb6b29784fdf8184296fa7f"
45 "772b62cdd1a8bd1a2d073830fac0409b"
46 "45acedf53a70676de96d7cb7e337cec4"
47 "08d5e3d626ac6c775baf71368b1d5851"
48 "47585f06b305ad5f547cb40644d2e048"
49 "7a9ded9778ddbfac15a6a7aee399fc7d"
50 "92610b028c624fd68cb573b830d842c2"
51 "ceb34da13efd50db13165a4f19d38cea"
52 "3293a073ba2d1bb31642297764b0fc17"
53 "e941ba73d703ba77455f30f9293a41fe"
54 "2915358c99f95a55075811d57ddff3d3"
55 "67d0a59e5b2f4e1c697b1e9955aa972c"
56 "f43d5b81c242a2b8eda917b25dc689be"
57 "f514b39979b7181eb5db62eb39cd0c3a"
58 "3dcb8013b19bdb262a890fce3360a351"
59 "cb3ddf76c13606177479b6e1345a2705"
60 "eaf97715af161b17b715ab6ef006e697"
61 "a1a779ea879a10c258069c4d9522d411"
62 "70aa69132d6e5cecb7ada5d16973d77f"
63 "3d7cc647175604d7151480473e61e73f"
64 "36227324058f38f578198a19e083db2b"
65 "8454ee2a00b527a99e3ec9addbfbd3a6"
66 "8c51cab16a720b7f47fe6fbfb4ca541c"
67 "2ec4683588ce2106fc907d987620ee48"
68 "aa506b8a246a18e2fa156d66b5add15c"
69 "2305615bb1c7c76d95aa679545eac38b"
70 "806cd02e5ef89897e278a536c25553f4"
71 "05d12474"}};
72
hex2span(const char * hex)73 template <class Blob> Blob hex2span(const char* hex) {
74 string bytes = hex2str(hex);
75 Blob retval(reinterpret_cast<const uint8_t*>(bytes.data()), bytes.size());
76 return std::move(retval);
77 }
78
hex2key(const char * hex)79 KeymasterKeyBlob hex2key(const char* hex) {
80 return hex2span<KeymasterKeyBlob>(hex);
81 }
82
hex2blob(const char * hex)83 KeymasterBlob hex2blob(const char* hex) {
84 return hex2span<KeymasterBlob>(hex);
85 }
86
TEST(CkdfTest,Ckdf)87 TEST(CkdfTest, Ckdf) {
88 for (auto& test : kCkdfTests) {
89 auto key = hex2key(test.key);
90 auto label = hex2blob(test.label);
91 auto context = hex2blob(test.context);
92 auto expected = hex2blob(test.output);
93
94 KeymasterKeyBlob output;
95 output.Reset(expected.data_length);
96
97 ASSERT_EQ(KM_ERROR_OK, ckdf(key, label, context, &output));
98 EXPECT_TRUE(std::equal(output.begin(), output.end(), expected.begin()));
99 }
100 }
101
102 } // namespace test
103 } // namespace keymaster
104