keymaster_passthrough_operation.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "keymaster_passthrough_operation.h"
  18. #include <vector>
  19. #include <keymaster/android_keymaster_utils.h>
  20. namespace keymaster {
  21. template<>
  22. keymaster_error_t
  23. KeymasterPassthroughOperation<keymaster1_device_t>::Finish(const AuthorizationSet& input_params,
  24. const Buffer& input,
  25. const Buffer& signature, AuthorizationSet* output_params,
  26. Buffer* output) {
  27. keymaster_key_param_set_t out_params = {};
  28. keymaster_blob_t in{ input.peek_read(), input.available_read() };
  29. keymaster_blob_t out = {};
  30. keymaster_error_t rc;
  31. std::vector<KeymasterBlob> accumulate_output;
  32. size_t accumulated_output_size = 0;
  33. AuthorizationSet accumulated_out_params;
  34. AuthorizationSet mutable_input_params = input_params;
  35. while (in.data_length != 0) {
  36. size_t consumed = 0;
  37. rc = km_device_->update(km_device_, operation_handle_, &mutable_input_params, &in, &consumed, &out_params, &out);
  38. if (rc == KM_ERROR_OK) {
  39. accumulate_output.push_back(KeymasterBlob(out));
  40. accumulated_output_size += out.data_length;
  41. free(const_cast<uint8_t*>(out.data));
  42. out = {};
  43. accumulated_out_params.push_back(out_params);
  44. keymaster_free_param_set(&out_params);
  45. } else {
  46. return rc;
  47. }
  48. int aad_pos = mutable_input_params.find(TAG_ASSOCIATED_DATA);
  49. if (aad_pos != -1) {
  50. mutable_input_params.erase(aad_pos);
  51. }
  52. if (consumed == 0) {
  53. km_device_->abort(km_device_, operation_handle_);
  54. return KM_ERROR_INVALID_INPUT_LENGTH;
  55. } else {
  56. in.data += consumed;
  57. if (consumed > in.data_length) return KM_ERROR_UNKNOWN_ERROR;
  58. in.data_length -= consumed;
  59. }
  60. }
  61. keymaster_blob_t sig{ signature.peek_read(), signature.available_read() };
  62. rc = km_device_->finish(km_device_, operation_handle_, &mutable_input_params, &sig, &out_params, &out);
  63. if (rc != KM_ERROR_OK) return rc;
  64. accumulate_output.push_back(KeymasterBlob(out));
  65. accumulated_output_size += out.data_length;
  66. free(const_cast<uint8_t*>(out.data));
  67. out = {};
  68. accumulated_out_params.push_back(out_params);
  69. keymaster_free_param_set(&out_params);
  70. if (output && accumulated_output_size) {
  71. if (!output->reserve(accumulated_output_size)) {
  72. return KM_ERROR_MEMORY_ALLOCATION_FAILED;
  73. }
  74. for (auto& outBlob: accumulate_output) {
  75. output->write(outBlob.data, outBlob.data_length);
  76. }
  77. }
  78. accumulated_out_params.Deduplicate();
  79. if (output_params) *output_params = move(accumulated_out_params);
  80. return KM_ERROR_OK;
  81. }
  82. template<>
  83. keymaster_error_t
  84. KeymasterPassthroughOperation<keymaster2_device_t>::Finish(const AuthorizationSet& input_params, const Buffer& input,
  85. const Buffer& signature, AuthorizationSet* output_params,
  86. Buffer* output) {
  87. keymaster_key_param_set_t out_params = {};
  88. keymaster_blob_t sig{ signature.peek_read(), signature.available_read() };
  89. keymaster_blob_t in{ input.peek_read(), input.available_read() };
  90. keymaster_blob_t out = {};
  91. keymaster_error_t rc;
  92. rc = km_device_->finish(km_device_, operation_handle_, &input_params, &in, &sig, &out_params, &out);
  93. if (rc == KM_ERROR_OK) {
  94. if (output) output->Reinitialize(out.data, out.data_length);
  95. if (output_params) output_params->Reinitialize(out_params);
  96. }
  97. keymaster_free_param_set(&out_params);
  98. free(const_cast<uint8_t*>(out.data));
  99. return rc;
  100. }
  101. } // namespace keymaster