• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <openssl/ec.h>
20 #include <openssl/evp.h>
21 
22 #include <keymaster/asymmetric_key_factory.h>
23 #include <keymaster/soft_key_factory.h>
24 
25 namespace keymaster {
26 
27 class EcKeyFactory : public AsymmetricKeyFactory, public SoftKeyFactoryMixin {
28   public:
EcKeyFactory(const SoftwareKeyBlobMaker & blob_maker,const KeymasterContext & context)29     explicit EcKeyFactory(const SoftwareKeyBlobMaker& blob_maker, const KeymasterContext& context)
30         : AsymmetricKeyFactory(context), SoftKeyFactoryMixin(blob_maker) {}
31 
keymaster_key_type()32     keymaster_algorithm_t keymaster_key_type() const override { return KM_ALGORITHM_EC; }
33 
34     keymaster_error_t GenerateKey(const AuthorizationSet& key_description,
35                                   UniquePtr<Key> attest_key,            //
36                                   const KeymasterBlob& issuer_subject,  //
37                                   KeymasterKeyBlob* key_blob,
38                                   AuthorizationSet* hw_enforced,  //
39                                   AuthorizationSet* sw_enforced,
40                                   CertificateChain* cert_chain) const override;
41     keymaster_error_t ImportKey(const AuthorizationSet& key_description,
42                                 keymaster_key_format_t input_key_material_format,
43                                 const KeymasterKeyBlob& input_key_material,
44                                 UniquePtr<Key> attest_key,  //
45                                 const KeymasterBlob& issuer_subject,
46                                 KeymasterKeyBlob* output_key_blob,  //
47                                 AuthorizationSet* hw_enforced,      //
48                                 AuthorizationSet* sw_enforced,
49                                 CertificateChain* cert_chain) const override;
50 
51     keymaster_error_t ImportRawKey(const AuthorizationSet& key_description,
52                                    const KeymasterKeyBlob& input_key_material,
53                                    UniquePtr<Key> attest_key,  //
54                                    const KeymasterBlob& issuer_subject,
55                                    KeymasterKeyBlob* output_key_blob,  //
56                                    AuthorizationSet* hw_enforced,      //
57                                    AuthorizationSet* sw_enforced,
58                                    CertificateChain* cert_chain) const;
59 
60     keymaster_error_t CreateEmptyKey(AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced,
61                                      UniquePtr<AsymmetricKey>* key) const override;
62 
63     keymaster_error_t UpdateImportKeyDescription(const AuthorizationSet& key_description,
64                                                  keymaster_key_format_t key_format,
65                                                  const KeymasterKeyBlob& key_material,
66                                                  AuthorizationSet* updated_description,
67                                                  uint32_t* key_size) const;
68 
69     OperationFactory* GetOperationFactory(keymaster_purpose_t purpose) const override;
70 
71   protected:
72     static EC_GROUP* ChooseGroup(size_t key_size_bits);
73     static EC_GROUP* ChooseGroup(keymaster_ec_curve_t ec_curve);
74 
75     static keymaster_error_t GetCurveAndSize(const AuthorizationSet& key_description,
76                                              keymaster_ec_curve_t* curve, uint32_t* key_size_bits);
77 };
78 
79 }  // namespace keymaster
80