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