rsa_key.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 <keymaster/km_openssl/rsa_key.h>
  17. #include <keymaster/keymaster_context.h>
  18. #include <keymaster/km_openssl/openssl_err.h>
  19. #include <keymaster/km_openssl/openssl_utils.h>
  20. #include <keymaster/km_openssl/rsa_operation.h>
  21. namespace keymaster {
  22. bool RsaKey::EvpToInternal(const EVP_PKEY* pkey) {
  23. rsa_key_.reset(EVP_PKEY_get1_RSA(const_cast<EVP_PKEY*>(pkey)));
  24. return rsa_key_.get() != nullptr;
  25. }
  26. bool RsaKey::InternalToEvp(EVP_PKEY* pkey) const {
  27. return EVP_PKEY_set1_RSA(pkey, rsa_key_.get()) == 1;
  28. }
  29. bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_padding_t padding) {
  30. switch (purpose) {
  31. case KM_PURPOSE_SIGN:
  32. case KM_PURPOSE_VERIFY:
  33. return padding == KM_PAD_NONE || padding == KM_PAD_RSA_PSS ||
  34. padding == KM_PAD_RSA_PKCS1_1_5_SIGN;
  35. case KM_PURPOSE_ENCRYPT:
  36. case KM_PURPOSE_DECRYPT:
  37. case KM_PURPOSE_WRAP:
  38. return padding == KM_PAD_RSA_OAEP || padding == KM_PAD_RSA_PKCS1_1_5_ENCRYPT;
  39. case KM_PURPOSE_DERIVE_KEY:
  40. return false;
  41. };
  42. return false;
  43. }
  44. bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_digest_t digest) {
  45. switch (purpose) {
  46. case KM_PURPOSE_SIGN:
  47. case KM_PURPOSE_VERIFY:
  48. return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256;
  49. case KM_PURPOSE_ENCRYPT:
  50. case KM_PURPOSE_DECRYPT:
  51. case KM_PURPOSE_WRAP:
  52. /* Don't care */
  53. break;
  54. case KM_PURPOSE_DERIVE_KEY:
  55. return false;
  56. };
  57. return true;
  58. }
  59. } // namespace keymaster