1 /* 2 * Copyright 2015 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/kdf2.h> 18 #include <gtest/gtest.h> 19 #include <string.h> 20 21 #include "android_keymaster_test_utils.h" 22 23 using std::string; 24 25 namespace keymaster { 26 27 namespace test { 28 29 struct Kdf2Test { 30 const char* key_hex; 31 const char* info_hex; 32 const char* expected_output_hex; 33 keymaster_digest_t digest_type; 34 }; 35 36 static const Kdf2Test kKdf2Tests[] = { 37 {"032e45326fa859a72ec235acff929b15d1372e30b207255f0611b8f785d764374152e0ac009e509e7ba30cd2f1778" 38 "e113b64e135cf4e2292c75efe5288edfda4", 39 "", 40 "10a2403db42a8743cb989de86e668d168cbe6046e23ff26f741e87949a3bba1311ac179f819a3d18412e9eb45668f" 41 "2923c087c1299005f8d5fd42ca257bc93e8fee0c5a0d2a8aa70185401fbbd99379ec76c663e9a29d0b70f3fe261a5" 42 "9cdc24875a60b4aacb1319fa11c3365a8b79a44669f26fba933d012db213d7e3b16349", 43 KM_DIGEST_SHA_2_256}, 44 {"032e45326fa859a72ec235acff929b15d1372e30b207255f0611b8f785d764374152e0ac009e509e7ba30cd2f1778" 45 "e113b64e135cf4e2292c75efe5288edfda4", 46 "", 47 "0e6a26eb7b956ccb8b3bdc1ca975bc57c3989e8fbad31a224655d800c46954840ff32052cdf0d640562bdfadfa263" 48 "cfccf3c52b29f2af4a1869959bc77f854cf15bd7a25192985a842dbff8e13efee5b7e7e55bbe4d389647c686a9a9a" 49 "b3fb889b2d7767d3837eea4e0a2f04b53ca8f50fb31225c1be2d0126c8c7a4753b0807", 50 KM_DIGEST_SHA1}, 51 {"CA7C0F8C3FFA87A96E1B74AC8E6AF594347BB40A", "", "744AB703F5BC082E59185F6D049D2D367DB245C2", 52 KM_DIGEST_SHA1}, 53 {"0499B502FC8B5BAFB0F4047E731D1F9FD8CD0D8881", "", 54 "03C62280C894E103C680B13CD4B4AE740A5EF0C72547292F82DC6B1777F47D63BA9D1EA73" 55 "2DBF386", 56 KM_DIGEST_SHA1}, 57 {"5E10B967A95606853E528F04262AD18A4767C761163971391E17CB05A21668D4CE2B9F151617408042CE091958382" 58 "3FD346D1751FBE2341AF2EE0461B62F100FFAD4F723F70C18B38238ED183E9398C8CA517EE0CBBEFFF9C59471FE27" 59 "8093924089480DBC5A38E9A1A97D23038106847D0D22ECF85F49A861821199BAFCB0D74E6ACFFD7D142765EBF4C71" 60 "2414FE4B6AB957F4CB466B46601289BB82060428272842EE28F113CD11F39431CBFFD823254CE472E2105E49B3D7F" 61 "113B825076E6264585807BC46454665F27C5E4E1A4BD03470486322981FDC894CCA1E2930987C92C15A38BC42EB38" 62 "810E867C4432F07259EC00CDBBB0FB99E1727C706DA58DD", 63 "484D4143204B6579", "BC98EB018CB00EE26D1F97A15AE166912A7AC4C5", KM_DIGEST_SHA1}, 64 65 }; 66 67 TEST(Kdf2Test, Kdf2) { 68 for (auto& test : kKdf2Tests) { 69 const string key = hex2str(test.key_hex); 70 const string info = hex2str(test.info_hex); 71 const string expected_output = hex2str(test.expected_output_hex); 72 size_t output_len = expected_output.size(); 73 uint8_t output[output_len]; 74 75 Kdf2 kdf2; 76 ASSERT_TRUE( 77 kdf2.Init(test.digest_type, reinterpret_cast<const uint8_t*>(key.data()), key.size())); 78 ASSERT_TRUE(kdf2.GenerateKey(reinterpret_cast<const uint8_t*>(info.data()), info.size(), 79 output, output_len)); 80 EXPECT_EQ(0, memcmp(output, expected_output.data(), output_len)); 81 } 82 } 83 84 } // namespace test 85 86 } // namespace keymaster 87