keymaster_passthrough_operation.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. **
  3. ** Copyright 2017, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #ifndef SYSTEM_KEYMASTER_KEYMASTER_PASSTHROUGH_OPERATION_H_
  18. #define SYSTEM_KEYMASTER_KEYMASTER_PASSTHROUGH_OPERATION_H_
  19. #include <hardware/keymaster1.h>
  20. #include <hardware/keymaster2.h>
  21. #include <keymaster/legacy_support/keymaster_passthrough_key.h>
  22. #include <keymaster/operation.h>
  23. namespace keymaster {
  24. class AuthorizationSet;
  25. class Key;
  26. class Operation;
  27. /**
  28. * Template implementation for KM1 and KM2 operations
  29. */
  30. template <typename KeymasterDeviceType> class KeymasterPassthroughOperation : public Operation {
  31. public:
  32. explicit KeymasterPassthroughOperation(keymaster_purpose_t purpose,
  33. const KeymasterDeviceType* km_device, Key&& key)
  34. : Operation(purpose, key.hw_enforced_move(), key.sw_enforced_move()),
  35. key_blob_(key.key_material_move()), km_device_(km_device) {
  36. operation_handle_ = 0;
  37. }
  38. virtual ~KeymasterPassthroughOperation() {}
  39. keymaster_error_t Begin(const AuthorizationSet& input_params,
  40. AuthorizationSet* output_params) override {
  41. keymaster_key_param_set_t out_params = {};
  42. keymaster_error_t rc;
  43. rc = km_device_->begin(km_device_, purpose(), &key_blob_, &input_params, &out_params,
  44. &operation_handle_);
  45. if (rc == KM_ERROR_OK && output_params) output_params->Reinitialize(out_params);
  46. keymaster_free_param_set(&out_params);
  47. return rc;
  48. }
  49. keymaster_error_t Update(const AuthorizationSet& input_params, const Buffer& input,
  50. AuthorizationSet* output_params, Buffer* output,
  51. size_t* input_consumed) override {
  52. keymaster_key_param_set_t out_params = {};
  53. keymaster_blob_t in{input.peek_read(), input.available_read()};
  54. keymaster_blob_t out = {};
  55. keymaster_error_t rc;
  56. rc = km_device_->update(km_device_, operation_handle_, &input_params, &in, input_consumed,
  57. &out_params, &out);
  58. if (rc == KM_ERROR_OK) {
  59. if (output) output->Reinitialize(out.data, out.data_length);
  60. if (output_params) output_params->Reinitialize(out_params);
  61. }
  62. keymaster_free_param_set(&out_params);
  63. free(const_cast<uint8_t*>(out.data));
  64. return rc;
  65. }
  66. keymaster_error_t Finish(const AuthorizationSet& input_params, const Buffer& input,
  67. const Buffer& signature, AuthorizationSet* output_params,
  68. Buffer* output) override;
  69. keymaster_error_t Abort() { return km_device_->abort(km_device_, operation_handle_); }
  70. private:
  71. KeymasterKeyBlob key_blob_;
  72. const KeymasterDeviceType* km_device_;
  73. };
  74. template <>
  75. keymaster_error_t KeymasterPassthroughOperation<keymaster1_device_t>::Finish(
  76. const AuthorizationSet& input_params, const Buffer& input, const Buffer& signature,
  77. AuthorizationSet* output_params, Buffer* output);
  78. template <>
  79. keymaster_error_t KeymasterPassthroughOperation<keymaster2_device_t>::Finish(
  80. const AuthorizationSet& input_params, const Buffer& input, const Buffer& signature,
  81. AuthorizationSet* output_params, Buffer* output);
  82. template <typename KeymasterDeviceType>
  83. class KeymasterPassthroughOperationFactory : public OperationFactory {
  84. public:
  85. KeymasterPassthroughOperationFactory(keymaster_algorithm_t algorithm,
  86. keymaster_purpose_t purpose,
  87. const KeymasterDeviceType* km_device)
  88. : key_type_(algorithm, purpose), km_device_(km_device) {}
  89. virtual ~KeymasterPassthroughOperationFactory() {}
  90. KeyType registry_key() const override { return key_type_; }
  91. // Factory methods
  92. OperationPtr CreateOperation(Key&& key, const AuthorizationSet& /*begin_params*/,
  93. keymaster_error_t* error) const override {
  94. if (!error) return nullptr;
  95. *error = KM_ERROR_OK;
  96. OperationPtr op(new (std::nothrow) KeymasterPassthroughOperation<KeymasterDeviceType>(
  97. key_type_.purpose, km_device_, std::move(key)));
  98. if (!op) {
  99. *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
  100. }
  101. return op;
  102. }
  103. // Informational methods. The returned arrays reference static memory and must not be
  104. // deallocated or modified.
  105. const keymaster_padding_t* SupportedPaddingModes(size_t* padding_count) const override {
  106. *padding_count = 0;
  107. return nullptr;
  108. }
  109. const keymaster_block_mode_t* SupportedBlockModes(size_t* block_mode_count) const override {
  110. *block_mode_count = 0;
  111. return nullptr;
  112. }
  113. const keymaster_digest_t* SupportedDigests(size_t* digest_count) const override {
  114. *digest_count = 0;
  115. return nullptr;
  116. }
  117. private:
  118. KeyType key_type_;
  119. const KeymasterDeviceType* km_device_;
  120. };
  121. } // namespace keymaster
  122. #endif // SYSTEM_KEYMASTER_KEYMASTER_PASSTHROUGH_OPERATION_H_