Home | History | Annotate | Download | only in vold
      1 /*
      2  * Copyright (C) 2016 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 ANDROID_VOLD_KEYMASTER_H
     18 #define ANDROID_VOLD_KEYMASTER_H
     19 
     20 #ifdef __cplusplus
     21 
     22 #include <memory>
     23 #include <string>
     24 #include <utility>
     25 
     26 #include <android/hardware/keymaster/3.0/IKeymasterDevice.h>
     27 #include <keystore/authorization_set.h>
     28 
     29 namespace android {
     30 namespace vold {
     31 using ::android::hardware::keymaster::V3_0::IKeymasterDevice;
     32 using ::keystore::ErrorCode;
     33 using ::keystore::KeyPurpose;
     34 using ::keystore::AuthorizationSet;
     35 
     36 // C++ wrappers to the Keymaster hidl interface.
     37 // This is tailored to the needs of KeyStorage, but could be extended to be
     38 // a more general interface.
     39 
     40 // Wrapper for a Keymaster operation handle representing an
     41 // ongoing Keymaster operation.  Aborts the operation
     42 // in the destructor if it is unfinished. Methods log failures
     43 // to LOG(ERROR).
     44 class KeymasterOperation {
     45   public:
     46     ~KeymasterOperation();
     47     // Is this instance valid? This is false if creation fails, and becomes
     48     // false on finish or if an update fails.
     49     explicit operator bool() { return mError == ErrorCode::OK; }
     50     ErrorCode errorCode() { return mError; }
     51     // Call "update" repeatedly until all of the input is consumed, and
     52     // concatenate the output. Return true on success.
     53     bool updateCompletely(const std::string& input, std::string* output);
     54     // Finish and write the output to this string, unless pointer is null.
     55     bool finish(std::string* output);
     56     // Move constructor
     57     KeymasterOperation(KeymasterOperation&& rhs) {
     58         mDevice = std::move(rhs.mDevice);
     59         mOpHandle = std::move(rhs.mOpHandle);
     60         mError = std::move(rhs.mError);
     61     }
     62     // Construct an object in an error state for error returns
     63     KeymasterOperation()
     64         : mDevice{nullptr}, mOpHandle{0},
     65           mError {ErrorCode::UNKNOWN_ERROR} {}
     66     // Move Assignment
     67     KeymasterOperation& operator= (KeymasterOperation&& rhs) {
     68         mDevice = std::move(rhs.mDevice);
     69         mOpHandle = std::move(rhs.mOpHandle);
     70         mError = std::move(rhs.mError);
     71         rhs.mError = ErrorCode::UNKNOWN_ERROR;
     72         rhs.mOpHandle = 0;
     73         return *this;
     74     }
     75 
     76   private:
     77     KeymasterOperation(const sp<IKeymasterDevice>& d, uint64_t h)
     78         : mDevice{d}, mOpHandle{h}, mError {ErrorCode::OK} {}
     79     KeymasterOperation(ErrorCode error)
     80         : mDevice{nullptr}, mOpHandle{0},
     81           mError {error} {}
     82     sp<IKeymasterDevice> mDevice;
     83     uint64_t mOpHandle;
     84     ErrorCode mError;
     85     DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
     86     friend class Keymaster;
     87 };
     88 
     89 // Wrapper for a Keymaster device for methods that start a KeymasterOperation or are not
     90 // part of one.
     91 class Keymaster {
     92   public:
     93     Keymaster();
     94     // false if we failed to open the keymaster device.
     95     explicit operator bool() { return mDevice.get() != nullptr; }
     96     // Generate a key in the keymaster from the given params.
     97     bool generateKey(const AuthorizationSet& inParams, std::string* key);
     98     // If the keymaster supports it, permanently delete a key.
     99     bool deleteKey(const std::string& key);
    100     // Replace stored key blob in response to KM_ERROR_KEY_REQUIRES_UPGRADE.
    101     bool upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams,
    102                     std::string* newKey);
    103     // Begin a new cryptographic operation, collecting output parameters if pointer is non-null
    104     KeymasterOperation begin(KeyPurpose purpose, const std::string& key,
    105                              const AuthorizationSet& inParams, AuthorizationSet* outParams);
    106     bool isSecure();
    107 
    108   private:
    109     sp<hardware::keymaster::V3_0::IKeymasterDevice> mDevice;
    110     DISALLOW_COPY_AND_ASSIGN(Keymaster);
    111 };
    112 
    113 }  // namespace vold
    114 }  // namespace android
    115 
    116 #endif // __cplusplus
    117 
    118 
    119 /*
    120  * The following functions provide C bindings to keymaster services
    121  * needed by cryptfs scrypt. The compatibility check checks whether
    122  * the keymaster implementation is considered secure, i.e., TEE backed.
    123  * The create_key function generates an RSA key for signing.
    124  * The sign_object function signes an object with the given keymaster
    125  * key.
    126  */
    127 __BEGIN_DECLS
    128 
    129 int keymaster_compatibility_cryptfs_scrypt();
    130 int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size,
    131                                             uint64_t rsa_exponent,
    132                                             uint32_t ratelimit,
    133                                             uint8_t* key_buffer,
    134                                             uint32_t key_buffer_size,
    135                                             uint32_t* key_out_size);
    136 
    137 int keymaster_sign_object_for_cryptfs_scrypt(const uint8_t* key_blob,
    138                                              size_t key_blob_size,
    139                                              uint32_t ratelimit,
    140                                              const uint8_t* object,
    141                                              const size_t object_size,
    142                                              uint8_t** signature_buffer,
    143                                              size_t* signature_buffer_size);
    144 
    145 __END_DECLS
    146 
    147 #endif
    148