2 * Copyright (c) 2016-2019 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Kyungwook Tak <k.tak@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
19 * @author Kyungwook Tak (k.tak@samsung.com)
28 #include <boost/test/unit_test.hpp>
30 #include <SecCryptoSvc.h>
32 BOOST_AUTO_TEST_SUITE(CRYPTO_SERVICE_TEST)
34 BOOST_AUTO_TEST_CASE(PLATFORM_UNIQUE_KEY)
36 constexpr unsigned int KeyLen = 16;
37 unsigned char cek[KeyLen] = {0};
39 BOOST_REQUIRE_MESSAGE(SecFrameGeneratePlatformUniqueKey(KeyLen, cek),
40 "Failed to SecFrameGeneratePlatformUniqueKey.");
42 std::unique_ptr<char, void(*)(void *)> encoded_cek(
43 Base64Encoding(reinterpret_cast<char *>(cek), static_cast<int>(KeyLen)),
45 BOOST_REQUIRE_MESSAGE(!!encoded_cek, "Failed to base64 encoding.");
47 std::cout << "base64 encoded platform unique key(with len 16): "
48 << encoded_cek.get() << std::endl;
51 BOOST_AUTO_TEST_CASE(GETDUID_16)
53 std::unique_ptr<char, void(*)(void *)> duid(GetDuid(16), free);
54 BOOST_REQUIRE_MESSAGE(!!duid, "returned duid shouldn't be null");
55 std::cout << "duid: " << duid.get() << std::endl;
58 BOOST_AUTO_TEST_CASE(GETDUID_20)
60 std::unique_ptr<char, void(*)(void *)> duid(GetDuid(20), free);
61 BOOST_REQUIRE_MESSAGE(!!duid, "returned duid shouldn't be null");
62 std::cout << "duid: " << duid.get() << std::endl;
65 static void derive_key_with_pass(const char *pass, int passlen)
67 int retval = CS_ERROR_NONE;
69 constexpr unsigned int KeyLen = 20;
70 unsigned char *key = nullptr;
71 retval = cs_derive_key_with_pass(pass, passlen, KeyLen, &key);
72 BOOST_REQUIRE_MESSAGE(retval == CS_ERROR_NONE,
73 "Failed to cs_derive_key_with_pass with retval: " << retval);
75 std::unique_ptr<unsigned char, void(*)(void *)> pKey(key, free);
77 std::unique_ptr<char, void(*)(void *)> encoded_key(
78 Base64Encoding(reinterpret_cast<char *>(key), static_cast<int>(KeyLen)),
80 BOOST_REQUIRE_MESSAGE(!!encoded_key, "Failed to base64 encoding.");
82 std::cout << "base64 encoded key derived from pass(len " << KeyLen
83 << "): " << encoded_key.get() << std::endl;
86 BOOST_AUTO_TEST_CASE(DERIVE_KEY_WITH_PASS)
88 const char *test_pass = "test-password";
89 derive_key_with_pass(test_pass, 5);
90 derive_key_with_pass(test_pass, 10);
91 derive_key_with_pass(test_pass, strlen(test_pass));
93 const char empty_pass[30] = {0, };
94 derive_key_with_pass(empty_pass, strlen(empty_pass));
97 BOOST_AUTO_TEST_SUITE_END()