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 17 #include <keymaster/km_openssl/rsa_key.h> 18 19 #include <keymaster/keymaster_context.h> 20 #include <keymaster/km_openssl/openssl_err.h> 21 #include <keymaster/km_openssl/openssl_utils.h> 22 #include <keymaster/km_openssl/rsa_operation.h> 23 24 namespace keymaster { 25 EvpToInternal(const EVP_PKEY * pkey)26bool RsaKey::EvpToInternal(const EVP_PKEY* pkey) { 27 rsa_key_.reset(EVP_PKEY_get1_RSA(const_cast<EVP_PKEY*>(pkey))); 28 return rsa_key_.get() != nullptr; 29 } 30 InternalToEvp() const31EVP_PKEY_Ptr RsaKey::InternalToEvp() const { 32 EVP_PKEY_Ptr pkey(EVP_PKEY_new()); 33 if (pkey.get() != nullptr) { 34 if (EVP_PKEY_set1_RSA(pkey.get(), rsa_key_.get()) != 1) { 35 return {}; 36 } 37 } 38 return pkey; 39 } 40 SupportedMode(keymaster_purpose_t purpose,keymaster_padding_t padding)41bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_padding_t padding) { 42 switch (purpose) { 43 case KM_PURPOSE_ATTEST_KEY: 44 return true; 45 46 case KM_PURPOSE_SIGN: 47 case KM_PURPOSE_VERIFY: 48 return padding == KM_PAD_NONE || padding == KM_PAD_RSA_PSS || 49 padding == KM_PAD_RSA_PKCS1_1_5_SIGN; 50 51 case KM_PURPOSE_ENCRYPT: 52 case KM_PURPOSE_DECRYPT: 53 case KM_PURPOSE_WRAP: 54 return padding == KM_PAD_RSA_OAEP || padding == KM_PAD_RSA_PKCS1_1_5_ENCRYPT; 55 56 case KM_PURPOSE_DERIVE_KEY: 57 case KM_PURPOSE_AGREE_KEY: 58 return false; 59 }; 60 return false; 61 } 62 SupportedMode(keymaster_purpose_t purpose,keymaster_digest_t digest)63bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_digest_t digest) { 64 switch (purpose) { 65 case KM_PURPOSE_ATTEST_KEY: 66 return true; 67 68 case KM_PURPOSE_SIGN: 69 case KM_PURPOSE_VERIFY: 70 return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256; 71 72 case KM_PURPOSE_ENCRYPT: 73 case KM_PURPOSE_DECRYPT: 74 case KM_PURPOSE_WRAP: 75 /* Don't care */ 76 break; 77 78 case KM_PURPOSE_DERIVE_KEY: 79 case KM_PURPOSE_AGREE_KEY: 80 return false; 81 }; 82 return true; 83 } 84 85 } // namespace keymaster 86