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: |
| 28 | virtual Key* GenerateKey(const AuthorizationSet& key_description, keymaster_error_t* error); |
| 29 | virtual Key* ImportKey(const AuthorizationSet& key_description, |
| 30 | keymaster_key_format_t key_format, const uint8_t* key_data, |
| 31 | size_t key_data_length, keymaster_error_t* error); |
| 32 | virtual Key* LoadKey(const UnencryptedKeyBlob& blob, keymaster_error_t* error); |
| 33 | virtual Key* RescopeKey(const UnencryptedKeyBlob& blob, |
| 34 | const AuthorizationSet& new_authorizations, keymaster_error_t* error); |
| 35 | |
| 36 | private: |
| 37 | static EC_GROUP* choose_group(size_t key_size_bits); |
| 38 | static keymaster_error_t get_group_size(const EC_GROUP& group, size_t* key_size_bits); |
| 39 | |
| 40 | struct EC_GROUP_Delete { |
| 41 | void operator()(EC_GROUP* p) { EC_GROUP_free(p); } |
| 42 | }; |
| 43 | }; |
| 44 | |
| 45 | class EcdsaKeyFactory : public EcKeyFactory { |
| 46 | public: |
Shawn Willden | 9c65b2b | 2015-04-07 17:01:10 -0600 | [diff] [blame^] | 47 | virtual keymaster_algorithm_t registry_key() const { return KM_ALGORITHM_EC; } |
Thai Duong | f862a76 | 2015-03-18 14:10:56 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | class EcdsaSignOperationFactory; |
| 51 | class EcdsaVerifyOperationFactory; |
| 52 | |
| 53 | class EcKey : public AsymmetricKey { |
| 54 | private: |
| 55 | friend EcKeyFactory; |
| 56 | friend EcdsaKeyFactory; |
| 57 | friend EcdsaSignOperationFactory; |
| 58 | friend EcdsaVerifyOperationFactory; |
| 59 | |
| 60 | EcKey(const UnencryptedKeyBlob& blob, keymaster_error_t* error); |
| 61 | EcKey(EC_KEY* ec_key, const AuthorizationSet auths) : AsymmetricKey(auths), ec_key_(ec_key) {} |
| 62 | |
| 63 | virtual int evp_key_type() { return EVP_PKEY_EC; } |
| 64 | virtual bool InternalToEvp(EVP_PKEY* pkey) const; |
| 65 | virtual bool EvpToInternal(const EVP_PKEY* pkey); |
| 66 | |
| 67 | struct EC_Delete { |
| 68 | void operator()(EC_KEY* p) { EC_KEY_free(p); } |
| 69 | }; |
| 70 | |
| 71 | EC_KEY* key() const { return EC_KEY_dup(ec_key_.get()); } |
| 72 | |
| 73 | UniquePtr<EC_KEY, EC_Delete> ec_key_; |
| 74 | }; |
| 75 | |
| 76 | } // namespace keymaster |
| 77 | |
| 78 | #endif // SYSTEM_KEYMASTER_EC_KEY_H_ |