Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 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 <gtest/gtest.h>
     18 
     19 #include <endian.h>
     20 #include <hidl/HidlSupport.h>
     21 #include <keymaster/logger.h>
     22 #include <keymasterV4_0/keymaster_utils.h>
     23 
     24 #include <keystore/keymaster_types.h>
     25 #include <keystore/keystore_hidl_support.h>
     26 
     27 #include "../auth_token_table.h"
     28 
     29 using std::vector;
     30 
     31 namespace keystore {
     32 
     33 using android::hardware::hidl_array;
     34 using android::hardware::hidl_vec;
     35 
     36 namespace test {
     37 
     38 namespace {
     39 
     40 class StdoutLogger : public ::keymaster::Logger {
     41   public:
     42     StdoutLogger() { set_instance(this); }
     43 
     44     int log_msg(LogLevel level, const char* fmt, va_list args) const {
     45         int output_len = 0;
     46         switch (level) {
     47         case DEBUG_LVL:
     48             output_len = printf("DEBUG: ");
     49             break;
     50         case INFO_LVL:
     51             output_len = printf("INFO: ");
     52             break;
     53         case WARNING_LVL:
     54             output_len = printf("WARNING: ");
     55             break;
     56         case ERROR_LVL:
     57             output_len = printf("ERROR: ");
     58             break;
     59         case SEVERE_LVL:
     60             output_len = printf("SEVERE: ");
     61             break;
     62         }
     63 
     64         output_len += vprintf(fmt, args);
     65         output_len += printf("\n");
     66         return output_len;
     67     }
     68 };
     69 
     70 StdoutLogger logger;
     71 
     72 }  // namespace
     73 
     74 constexpr const uint8_t test_token[69] = {
     75     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
     76     0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
     77     0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
     78     0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
     79     0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44};
     80 
     81 constexpr const uint8_t test_hmac_data[] = {
     82     0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34,
     83     0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44};
     84 
     85 static const Km3HardwareAuthToken km3_hidl_test_token_little_endian = {
     86     UINT64_C(0x0807060504030201), UINT64_C(0x100f0e0d0c0b0a09),
     87     UINT64_C(0x1817161514131211), UINT32_C(0x1c1b1a19),
     88     UINT64_C(0x24232221201f1e1d), hidl_array<uint8_t, 32>(test_hmac_data)};
     89 
     90 static const HardwareAuthToken km4_hidl_test_token = {
     91     UINT64_C(0x0807060504030201), UINT64_C(0x100f0e0d0c0b0a09),
     92     UINT64_C(0x1817161514131211), static_cast<HardwareAuthenticatorType>(UINT32_C(0x191a1b1c)),
     93     UINT64_C(0x1d1e1f2021222324), hidl_vec<uint8_t>(test_hmac_data, test_hmac_data + 32)};
     94 
     95 TEST(AuthenticationTokenFormattingTest, hidlVec2Km3AuthToken) {
     96     static_assert(sizeof(hw_auth_token_t) == sizeof(test_token), "test_token has wrong size");
     97     hidl_vec<uint8_t> hidl_test_token;
     98     hidl_test_token.setToExternal(const_cast<unsigned char*>(test_token), sizeof(test_token));
     99     ASSERT_EQ(km3_hidl_test_token_little_endian, hidlVec2Km3AuthToken(hidl_test_token));
    100 }
    101 
    102 TEST(AuthenticationTokenFormattingTest, hidlVec2Km4AuthToken) {
    103     static_assert(sizeof(hw_auth_token_t) == sizeof(test_token), "test_token has wrong size");
    104     hidl_vec<uint8_t> hidl_test_token;
    105     hidl_test_token.setToExternal(const_cast<unsigned char*>(test_token), sizeof(test_token));
    106     ASSERT_EQ(km4_hidl_test_token, hidlVec2AuthToken(hidl_test_token));
    107 }
    108 
    109 TEST(AuthenticationTokenFormattingTest, km3AuthToken2HidlVec) {
    110     static_assert(sizeof(hw_auth_token_t) == sizeof(test_token), "test_token has wrong size");
    111     hidl_vec<uint8_t> hidl_test_token;
    112     hidl_test_token.setToExternal(const_cast<unsigned char*>(test_token), sizeof(test_token));
    113     ASSERT_EQ(hidl_test_token, authToken2HidlVec(km3_hidl_test_token_little_endian));
    114 }
    115 
    116 TEST(AuthenticationTokenFormattingTest, km4AuthToken2HidlVec) {
    117     static_assert(sizeof(hw_auth_token_t) == sizeof(test_token), "test_token has wrong size");
    118     hidl_vec<uint8_t> hidl_test_token;
    119     hidl_test_token.setToExternal(const_cast<unsigned char*>(test_token), sizeof(test_token));
    120     ASSERT_EQ(hidl_test_token, authToken2HidlVec(km4_hidl_test_token));
    121 }
    122 
    123 TEST(AuthenticationTokenFormattingTest, backAndForth) {
    124     static_assert(sizeof(hw_auth_token_t) == sizeof(test_token), "test_token has wrong size");
    125     hidl_vec<uint8_t> hidl_test_token;
    126     hidl_test_token.setToExternal(const_cast<unsigned char*>(test_token), sizeof(test_token));
    127     ASSERT_EQ(km3_hidl_test_token_little_endian,
    128               hidlVec2Km3AuthToken(authToken2HidlVec(km3_hidl_test_token_little_endian)));
    129     ASSERT_EQ(km4_hidl_test_token, hidlVec2AuthToken(authToken2HidlVec(km4_hidl_test_token)));
    130 }
    131 
    132 TEST(AuthenticationTokenFormattingTest, forthAndBack) {
    133     static_assert(sizeof(hw_auth_token_t) == sizeof(test_token), "test_token has wrong size");
    134     hidl_vec<uint8_t> hidl_test_token;
    135     hidl_test_token.setToExternal(const_cast<unsigned char*>(test_token), sizeof(test_token));
    136     ASSERT_EQ(hidl_test_token, authToken2HidlVec(hidlVec2Km3AuthToken(hidl_test_token)));
    137     ASSERT_EQ(hidl_test_token, authToken2HidlVec(hidlVec2Km3AuthToken(hidl_test_token)));
    138 }
    139 
    140 TEST(AuthenticationTokenFormattingTest, roundAndRound) {
    141     static_assert(sizeof(hw_auth_token_t) == sizeof(test_token), "test_token has wrong size");
    142     hidl_vec<uint8_t> hidl_test_token;
    143     hidl_test_token.setToExternal(const_cast<unsigned char*>(test_token), sizeof(test_token));
    144     HardwareAuthToken km4_from_hidl = hidlVec2AuthToken(hidl_test_token);
    145     hidl_vec<uint8_t> hidl_from_km4 = authToken2HidlVec(km4_from_hidl);
    146     Km3HardwareAuthToken km3_from_hidl = hidlVec2Km3AuthToken(hidl_from_km4);
    147     hidl_vec<uint8_t> hidl_from_km3 = authToken2HidlVec(km3_from_hidl);
    148 
    149     ASSERT_EQ(hidl_from_km4, hidl_test_token);
    150     ASSERT_EQ(hidl_from_km3, hidl_test_token);
    151     ASSERT_NE(km4_from_hidl.timestamp, km3_from_hidl.timestamp);
    152     ASSERT_NE(static_cast<uint32_t>(km4_from_hidl.authenticatorType),
    153               km3_from_hidl.authenticatorType);
    154 }
    155 
    156 }  // namespace test
    157 }  // namespace keystore
    158