1 /*
2  * Copyright 2015 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_FACTORY_H_
18 #define SYSTEM_KEYMASTER_EC_KEY_FACTORY_H_
19 
20 #include <openssl/ec.h>
21 #include <openssl/evp.h>
22 
23 #include <keymaster/asymmetric_key_factory.h>
24 
25 namespace keymaster {
26 
27 class EcKeyFactory : public AsymmetricKeyFactory {
28   public:
EcKeyFactory(const KeymasterContext * context)29     explicit EcKeyFactory(const KeymasterContext* context) : AsymmetricKeyFactory(context) {}
30 
keymaster_key_type()31     keymaster_algorithm_t keymaster_key_type() const override { return KM_ALGORITHM_EC; }
evp_key_type()32     int evp_key_type() const override { return EVP_PKEY_EC; }
33 
34     keymaster_error_t GenerateKey(const AuthorizationSet& key_description,
35                                   KeymasterKeyBlob* key_blob, AuthorizationSet* hw_enforced,
36                                   AuthorizationSet* sw_enforced) const override;
37     keymaster_error_t ImportKey(const AuthorizationSet& key_description,
38                                 keymaster_key_format_t input_key_material_format,
39                                 const KeymasterKeyBlob& input_key_material,
40                                 KeymasterKeyBlob* output_key_blob, AuthorizationSet* hw_enforced,
41                                 AuthorizationSet* sw_enforced) const override;
42 
43     keymaster_error_t CreateEmptyKey(const AuthorizationSet& hw_enforced,
44                                      const AuthorizationSet& sw_enforced,
45                                      UniquePtr<AsymmetricKey>* key) const override;
46 
47     keymaster_error_t UpdateImportKeyDescription(const AuthorizationSet& key_description,
48                                                  keymaster_key_format_t key_format,
49                                                  const KeymasterKeyBlob& key_material,
50                                                  AuthorizationSet* updated_description,
51                                                  uint32_t* key_size) const;
52 
53     OperationFactory* GetOperationFactory(keymaster_purpose_t purpose) const override;
54 
55   protected:
56     static EC_GROUP* ChooseGroup(size_t key_size_bits);
57     static EC_GROUP* ChooseGroup(keymaster_ec_curve_t ec_curve);
58 
59     static keymaster_error_t GetCurveAndSize(const AuthorizationSet& key_description,
60                                              keymaster_ec_curve_t* curve, uint32_t* key_size_bits);
61 };
62 
63 }  // namespace keymaster
64 
65 #endif  // SYSTEM_KEYMASTER_EC_KEY_FACTORY_H_
66