triple_des_operation.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2014 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 "triple_des_operation.h"
  17. namespace keymaster {
  18. static const keymaster_block_mode_t supported_block_modes[] = {KM_MODE_ECB, KM_MODE_CBC};
  19. const keymaster_block_mode_t*
  20. TripleDesEvpCipherDescription::SupportedBlockModes(size_t* block_mode_count) const {
  21. *block_mode_count = array_length(supported_block_modes);
  22. return supported_block_modes;
  23. }
  24. const EVP_CIPHER*
  25. TripleDesEvpCipherDescription::GetCipherInstance(size_t key_size, keymaster_block_mode_t block_mode,
  26. keymaster_error_t* error) const {
  27. *error = KM_ERROR_OK;
  28. switch (block_mode) {
  29. case KM_MODE_ECB:
  30. switch (key_size) {
  31. case 16:
  32. return EVP_des_ede(); // Note: OpenSSL 1.1.0 renamed this to EVP_des_ede_ecb
  33. case 24:
  34. return EVP_des_ede3(); // Note: OpenSSL 1.1.0 renamed this to EVP_des_ede3_ecb
  35. default:
  36. *error = KM_ERROR_UNSUPPORTED_KEY_SIZE;
  37. break;
  38. }
  39. break;
  40. case KM_MODE_CBC:
  41. switch (key_size) {
  42. case 16:
  43. return EVP_des_ede_cbc();
  44. case 24:
  45. return EVP_des_ede3_cbc();
  46. default:
  47. *error = KM_ERROR_UNSUPPORTED_KEY_SIZE;
  48. break;
  49. }
  50. break;
  51. default:
  52. *error = KM_ERROR_UNSUPPORTED_BLOCK_MODE;
  53. break;
  54. }
  55. assert(*error != KM_ERROR_OK);
  56. return nullptr;
  57. }
  58. static TripleDesEvpCipherDescription description;
  59. const EvpCipherDescription& TripleDesOperationFactory::GetCipherDescription() const {
  60. return description;
  61. }
  62. } // namespace keymaster