triple_des_key.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2018 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. #include <keymaster/km_openssl/triple_des_key.h>
  17. #include <assert.h>
  18. #include <keymaster/new>
  19. #include <openssl/err.h>
  20. #include <openssl/rand.h>
  21. #include "triple_des_operation.h"
  22. namespace keymaster {
  23. static TripleDesOperationFactory encrypt_factory(KM_PURPOSE_ENCRYPT);
  24. static TripleDesOperationFactory decrypt_factory(KM_PURPOSE_DECRYPT);
  25. OperationFactory* TripleDesKeyFactory::GetOperationFactory(keymaster_purpose_t purpose) const {
  26. switch (purpose) {
  27. case KM_PURPOSE_ENCRYPT:
  28. return &encrypt_factory;
  29. case KM_PURPOSE_DECRYPT:
  30. return &decrypt_factory;
  31. default:
  32. return nullptr;
  33. }
  34. }
  35. keymaster_error_t TripleDesKeyFactory::LoadKey(KeymasterKeyBlob&& key_material,
  36. const AuthorizationSet& /* additional_params */,
  37. AuthorizationSet&& hw_enforced,
  38. AuthorizationSet&& sw_enforced,
  39. UniquePtr<Key>* key) const {
  40. if (!key) return KM_ERROR_OUTPUT_PARAMETER_NULL;
  41. keymaster_error_t error = KM_ERROR_OK;
  42. key->reset(new (std::nothrow)
  43. TripleDesKey(move(key_material), move(hw_enforced), move(sw_enforced), this));
  44. if (!key->get()) error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
  45. return error;
  46. }
  47. keymaster_error_t TripleDesKeyFactory::validate_algorithm_specific_new_key_params(
  48. const AuthorizationSet& key_description) const {
  49. if (key_description.Contains(TAG_MIN_MAC_LENGTH)) return KM_ERROR_INVALID_TAG;
  50. return KM_ERROR_OK;
  51. }
  52. } // namespace keymaster