Home | History | Annotate | Download | only in functional
      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 #ifndef HARDWARE_INTERFACES_KEYMASTER_40_VTS_FUNCTIONAL_KEYMASTER_HIDL_TEST_H_
     18 #define HARDWARE_INTERFACES_KEYMASTER_40_VTS_FUNCTIONAL_KEYMASTER_HIDL_TEST_H_
     19 
     20 #include <android/hardware/keymaster/4.0/IKeymasterDevice.h>
     21 #include <android/hardware/keymaster/4.0/types.h>
     22 
     23 #include <VtsHalHidlTargetTestBase.h>
     24 
     25 #include <keymaster/keymaster_configuration.h>
     26 
     27 #include <keymasterV4_0/authorization_set.h>
     28 
     29 namespace android {
     30 namespace hardware {
     31 namespace keymaster {
     32 namespace V4_0 {
     33 
     34 ::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set);
     35 
     36 namespace test {
     37 
     38 using ::android::sp;
     39 using ::std::string;
     40 
     41 class HidlBuf : public hidl_vec<uint8_t> {
     42     typedef hidl_vec<uint8_t> super;
     43 
     44    public:
     45     HidlBuf() {}
     46     HidlBuf(const super& other) : super(other) {}
     47     HidlBuf(super&& other) : super(std::move(other)) {}
     48     explicit HidlBuf(const std::string& other) : HidlBuf() { *this = other; }
     49 
     50     HidlBuf& operator=(const super& other) {
     51         super::operator=(other);
     52         return *this;
     53     }
     54 
     55     HidlBuf& operator=(super&& other) {
     56         super::operator=(std::move(other));
     57         return *this;
     58     }
     59 
     60     HidlBuf& operator=(const string& other) {
     61         resize(other.size());
     62         std::copy(other.begin(), other.end(), begin());
     63         return *this;
     64     }
     65 
     66     string to_string() const { return string(reinterpret_cast<const char*>(data()), size()); }
     67 };
     68 
     69 constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF;
     70 
     71 class KeymasterHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
     72    public:
     73     // get the test environment singleton
     74     static KeymasterHidlEnvironment* Instance() {
     75         static KeymasterHidlEnvironment* instance = new KeymasterHidlEnvironment;
     76         return instance;
     77     }
     78 
     79     void registerTestServices() override { registerTestService<IKeymasterDevice>(); }
     80 
     81    private:
     82     KeymasterHidlEnvironment(){};
     83 
     84     GTEST_DISALLOW_COPY_AND_ASSIGN_(KeymasterHidlEnvironment);
     85 };
     86 
     87 class KeymasterHidlTest : public ::testing::VtsHalHidlTargetTestBase {
     88    public:
     89     void TearDown() override {
     90         if (key_blob_.size()) {
     91             CheckedDeleteKey();
     92         }
     93         AbortIfNeeded();
     94     }
     95 
     96     // SetUpTestCase runs only once per test case, not once per test.
     97     static void SetUpTestCase();
     98     static void TearDownTestCase() {
     99         keymaster_.clear();
    100         all_keymasters_.clear();
    101     }
    102 
    103     static IKeymasterDevice& keymaster() { return *keymaster_; }
    104     static const std::vector<sp<IKeymasterDevice>>& all_keymasters() { return all_keymasters_; }
    105     static uint32_t os_version() { return os_version_; }
    106     static uint32_t os_patch_level() { return os_patch_level_; }
    107 
    108     ErrorCode GenerateKey(const AuthorizationSet& key_desc, HidlBuf* key_blob,
    109                           KeyCharacteristics* key_characteristics);
    110     ErrorCode GenerateKey(const AuthorizationSet& key_desc);
    111 
    112     ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
    113                         const string& key_material, HidlBuf* key_blob,
    114                         KeyCharacteristics* key_characteristics);
    115     ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
    116                         const string& key_material);
    117 
    118     ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
    119                                const AuthorizationSet& wrapping_key_desc, string masking_key,
    120                                const AuthorizationSet& unwrapping_params);
    121 
    122     ErrorCode ExportKey(KeyFormat format, const HidlBuf& key_blob, const HidlBuf& client_id,
    123                         const HidlBuf& app_data, HidlBuf* key_material);
    124     ErrorCode ExportKey(KeyFormat format, HidlBuf* key_material);
    125 
    126     ErrorCode DeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
    127     ErrorCode DeleteKey(bool keep_key_blob = false);
    128 
    129     ErrorCode DeleteAllKeys();
    130 
    131     void CheckedDeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
    132     void CheckedDeleteKey();
    133 
    134     static void CheckCreationDateTime(const AuthorizationSet& sw_enforced,
    135                                       std::chrono::time_point<std::chrono::system_clock> creation);
    136 
    137     void CheckGetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
    138                                  const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
    139     ErrorCode GetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
    140                                  const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
    141     ErrorCode GetCharacteristics(const HidlBuf& key_blob, KeyCharacteristics* key_characteristics);
    142 
    143     ErrorCode Begin(KeyPurpose purpose, const HidlBuf& key_blob, const AuthorizationSet& in_params,
    144                     AuthorizationSet* out_params, OperationHandle* op_handle);
    145     ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
    146                     AuthorizationSet* out_params);
    147     ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
    148 
    149     ErrorCode Update(OperationHandle op_handle, const AuthorizationSet& in_params,
    150                      const string& input, AuthorizationSet* out_params, string* output,
    151                      size_t* input_consumed);
    152     ErrorCode Update(const string& input, string* out, size_t* input_consumed);
    153 
    154     ErrorCode Finish(OperationHandle op_handle, const AuthorizationSet& in_params,
    155                      const string& input, const string& signature, AuthorizationSet* out_params,
    156                      string* output);
    157     ErrorCode Finish(const string& message, string* output);
    158     ErrorCode Finish(const string& message, const string& signature, string* output);
    159     ErrorCode Finish(string* output) { return Finish(string(), output); }
    160 
    161     ErrorCode Abort(OperationHandle op_handle);
    162 
    163     void AbortIfNeeded();
    164 
    165     ErrorCode AttestKey(const HidlBuf& key_blob, const AuthorizationSet& attest_params,
    166                         hidl_vec<hidl_vec<uint8_t>>* cert_chain);
    167     ErrorCode AttestKey(const AuthorizationSet& attest_params,
    168                         hidl_vec<hidl_vec<uint8_t>>* cert_chain);
    169 
    170     string ProcessMessage(const HidlBuf& key_blob, KeyPurpose operation, const string& message,
    171                           const AuthorizationSet& in_params, AuthorizationSet* out_params);
    172 
    173     string SignMessage(const HidlBuf& key_blob, const string& message,
    174                        const AuthorizationSet& params);
    175     string SignMessage(const string& message, const AuthorizationSet& params);
    176 
    177     string MacMessage(const string& message, Digest digest, size_t mac_length);
    178 
    179     void CheckHmacTestVector(const string& key, const string& message, Digest digest,
    180                              const string& expected_mac);
    181 
    182     void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
    183                                const string& expected_ciphertext);
    184 
    185     void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
    186                                   PaddingMode padding_mode, const string& key, const string& iv,
    187                                   const string& input, const string& expected_output);
    188 
    189     void VerifyMessage(const HidlBuf& key_blob, const string& message, const string& signature,
    190                        const AuthorizationSet& params);
    191     void VerifyMessage(const string& message, const string& signature,
    192                        const AuthorizationSet& params);
    193 
    194     string EncryptMessage(const HidlBuf& key_blob, const string& message,
    195                           const AuthorizationSet& in_params, AuthorizationSet* out_params);
    196     string EncryptMessage(const string& message, const AuthorizationSet& params,
    197                           AuthorizationSet* out_params);
    198     string EncryptMessage(const string& message, const AuthorizationSet& params);
    199     string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
    200     string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
    201                           HidlBuf* iv_out);
    202     string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
    203                           const HidlBuf& iv_in);
    204 
    205     string DecryptMessage(const HidlBuf& key_blob, const string& ciphertext,
    206                           const AuthorizationSet& params);
    207     string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
    208     string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
    209                           const HidlBuf& iv);
    210 
    211     std::pair<ErrorCode, HidlBuf> UpgradeKey(const HidlBuf& key_blob);
    212 
    213     static bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; }
    214     static SecurityLevel SecLevel() { return securityLevel_; }
    215 
    216     std::vector<uint32_t> ValidKeySizes(Algorithm algorithm);
    217     std::vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
    218 
    219     std::vector<EcCurve> ValidCurves();
    220     std::vector<EcCurve> InvalidCurves();
    221 
    222     std::vector<Digest> ValidDigests(bool withNone, bool withMD5);
    223     std::vector<Digest> InvalidDigests();
    224 
    225     HidlBuf key_blob_;
    226     KeyCharacteristics key_characteristics_;
    227     OperationHandle op_handle_ = kOpHandleSentinel;
    228 
    229    private:
    230     static sp<IKeymasterDevice> keymaster_;
    231     static std::vector<sp<IKeymasterDevice>> all_keymasters_;
    232     static uint32_t os_version_;
    233     static uint32_t os_patch_level_;
    234 
    235     static SecurityLevel securityLevel_;
    236     static hidl_string name_;
    237     static hidl_string author_;
    238 };
    239 
    240 }  // namespace test
    241 }  // namespace V4_0
    242 }  // namespace keymaster
    243 }  // namespace hardware
    244 }  // namespace android
    245 
    246 #endif  // HARDWARE_INTERFACES_KEYMASTER_40_VTS_FUNCTIONAL_KEYMASTER_HIDL_TEST_H_
    247