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_RSA_KEY_FACTORY_H_
18#define SYSTEM_KEYMASTER_RSA_KEY_FACTORY_H_
19
20#include <openssl/evp.h>
21#include <openssl/rsa.h>
22
23#include <keymaster/asymmetric_key_factory.h>
24#include <keymaster/soft_key_factory.h>
25#include <keymaster/attestation_record.h>
26
27namespace keymaster {
28
29class RsaKeyFactory : public AsymmetricKeyFactory, public SoftKeyFactoryMixin {
30  public:
31    explicit RsaKeyFactory(const SoftwareKeyBlobMaker* blob_maker) :
32            SoftKeyFactoryMixin(blob_maker) {}
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(AuthorizationSet&& hw_enforced,
44                                     AuthorizationSet&& sw_enforced,
45                                     UniquePtr<AsymmetricKey>* key) const override;
46
47    OperationFactory* GetOperationFactory(keymaster_purpose_t purpose) const override;
48
49    keymaster_algorithm_t keymaster_key_type() const override { return KM_ALGORITHM_RSA; }
50    int evp_key_type() const override { return EVP_PKEY_RSA; }
51
52  protected:
53    keymaster_error_t UpdateImportKeyDescription(const AuthorizationSet& key_description,
54                                                 keymaster_key_format_t import_key_format,
55                                                 const KeymasterKeyBlob& import_key_material,
56                                                 AuthorizationSet* updated_description,
57                                                 uint64_t* public_exponent,
58                                                 uint32_t* key_size) const;
59};
60
61}  // namespace keymaster
62
63#endif  // SYSTEM_KEYMASTER_RSA_KEY_FACTORY_H_
64