Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [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 | |
| 17 | #ifndef SYSTEM_KEYMASTER_EC_KEY_H_ |
| 18 | #define SYSTEM_KEYMASTER_EC_KEY_H_ |
| 19 | |
| 20 | #include <openssl/ec.h> |
| 21 | |
| 22 | #include "asymmetric_key.h" |
| 23 | |
| 24 | namespace keymaster { |
| 25 | |
| 26 | class EcKeyFactory : public AsymmetricKeyFactory { |
| 27 | public: |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 28 | EcKeyFactory(const KeymasterContext* context) : AsymmetricKeyFactory(context) {} |
| 29 | |
| 30 | keymaster_error_t GenerateKey(const AuthorizationSet& key_description, |
| 31 | KeymasterKeyBlob* key_blob, AuthorizationSet* hw_enforced, |
| 32 | AuthorizationSet* sw_enforced) override; |
| 33 | keymaster_error_t ImportKey(const AuthorizationSet& key_description, |
| 34 | keymaster_key_format_t input_key_material_format, |
| 35 | const KeymasterKeyBlob& input_key_material, |
| 36 | KeymasterKeyBlob* output_key_blob, AuthorizationSet* hw_enforced, |
| 37 | AuthorizationSet* sw_enforced) override; |
| 38 | |
| 39 | keymaster_error_t CreateEmptyKey(const AuthorizationSet& hw_enforced, |
| 40 | const AuthorizationSet& sw_enforced, |
| 41 | UniquePtr<AsymmetricKey>* key) override; |
Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [diff] [blame] | 42 | |
Shawn Willden | 6270aca | 2015-05-26 13:12:24 -0600 | [diff] [blame^] | 43 | keymaster_error_t UpdateImportKeyDescription(const AuthorizationSet& key_description, |
| 44 | keymaster_key_format_t key_format, |
| 45 | const KeymasterKeyBlob& key_material, |
| 46 | AuthorizationSet* updated_description, |
| 47 | uint32_t* key_size); |
| 48 | |
Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [diff] [blame] | 49 | private: |
| 50 | static EC_GROUP* choose_group(size_t key_size_bits); |
| 51 | static keymaster_error_t get_group_size(const EC_GROUP& group, size_t* key_size_bits); |
Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | class EcdsaKeyFactory : public EcKeyFactory { |
| 55 | public: |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 56 | EcdsaKeyFactory(const KeymasterContext* context) : EcKeyFactory(context) {} |
| 57 | |
| 58 | keymaster_algorithm_t registry_key() const override { return KM_ALGORITHM_EC; } |
| 59 | int evp_key_type() override { return EVP_PKEY_EC; } |
Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Shawn Willden | 13e29e0 | 2015-05-08 11:02:46 -0600 | [diff] [blame] | 62 | class EcdsaOperationFactory; |
Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [diff] [blame] | 63 | |
| 64 | class EcKey : public AsymmetricKey { |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 65 | public: |
| 66 | EcKey(const AuthorizationSet& hw_enforced, const AuthorizationSet& sw_enforced, |
| 67 | keymaster_error_t* error) |
| 68 | : AsymmetricKey(hw_enforced, sw_enforced, error) {} |
Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [diff] [blame] | 69 | |
Shawn Willden | 0cb6942 | 2015-05-26 08:31:37 -0600 | [diff] [blame] | 70 | bool InternalToEvp(EVP_PKEY* pkey) const override; |
| 71 | bool EvpToInternal(const EVP_PKEY* pkey) override; |
Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [diff] [blame] | 72 | |
Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [diff] [blame] | 73 | EC_KEY* key() const { return EC_KEY_dup(ec_key_.get()); } |
| 74 | |
Shawn Willden | 6270aca | 2015-05-26 13:12:24 -0600 | [diff] [blame^] | 75 | protected: |
| 76 | EcKey(EC_KEY* ec_key, const AuthorizationSet& hw_enforced, const AuthorizationSet& sw_enforced, |
| 77 | keymaster_error_t* error) |
| 78 | : AsymmetricKey(hw_enforced, sw_enforced, error), ec_key_(ec_key) {} |
| 79 | |
| 80 | private: |
Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [diff] [blame] | 81 | UniquePtr<EC_KEY, EC_Delete> ec_key_; |
| 82 | }; |
| 83 | |
| 84 | } // namespace keymaster |
| 85 | |
| 86 | #endif // SYSTEM_KEYMASTER_EC_KEY_H_ |