Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 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 | |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 17 | #include "rsa_key.h" |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 18 | |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 19 | #include <keymaster/keymaster_context.h> |
| 20 | |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 21 | #include "openssl_err.h" |
| 22 | #include "openssl_utils.h" |
Shawn Willden | 0629810 | 2015-05-25 23:12:48 -0600 | [diff] [blame] | 23 | #include "rsa_operation.h" |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 24 | |
| 25 | namespace keymaster { |
| 26 | |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 27 | bool RsaKey::EvpToInternal(const EVP_PKEY* pkey) { |
| 28 | rsa_key_.reset(EVP_PKEY_get1_RSA(const_cast<EVP_PKEY*>(pkey))); |
| 29 | return rsa_key_.get() != NULL; |
| 30 | } |
| 31 | |
| 32 | bool RsaKey::InternalToEvp(EVP_PKEY* pkey) const { |
| 33 | return EVP_PKEY_set1_RSA(pkey, rsa_key_.get()) == 1; |
| 34 | } |
| 35 | |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 36 | bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_padding_t padding) { |
| 37 | switch (purpose) { |
| 38 | case KM_PURPOSE_SIGN: |
| 39 | case KM_PURPOSE_VERIFY: |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 40 | return padding == KM_PAD_NONE || padding == KM_PAD_RSA_PSS || |
| 41 | padding == KM_PAD_RSA_PKCS1_1_5_SIGN; |
Shawn Willden | d4c83db | 2015-12-22 14:40:15 -0700 | [diff] [blame] | 42 | |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 43 | case KM_PURPOSE_ENCRYPT: |
| 44 | case KM_PURPOSE_DECRYPT: |
| 45 | return padding == KM_PAD_RSA_OAEP || padding == KM_PAD_RSA_PKCS1_1_5_ENCRYPT; |
Shawn Willden | d4c83db | 2015-12-22 14:40:15 -0700 | [diff] [blame] | 46 | |
| 47 | case KM_PURPOSE_DERIVE_KEY: |
| 48 | return false; |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 49 | }; |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_digest_t digest) { |
| 54 | switch (purpose) { |
| 55 | case KM_PURPOSE_SIGN: |
| 56 | case KM_PURPOSE_VERIFY: |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 57 | return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256; |
Shawn Willden | d4c83db | 2015-12-22 14:40:15 -0700 | [diff] [blame] | 58 | |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 59 | case KM_PURPOSE_ENCRYPT: |
| 60 | case KM_PURPOSE_DECRYPT: |
| 61 | /* Don't care */ |
| 62 | break; |
Shawn Willden | d4c83db | 2015-12-22 14:40:15 -0700 | [diff] [blame] | 63 | |
| 64 | case KM_PURPOSE_DERIVE_KEY: |
| 65 | return false; |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 66 | }; |
| 67 | return true; |
| 68 | } |
| 69 | |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 70 | } // namespace keymaster |