Keymaster.h 6.0 KB

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