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
25namespace keymaster {
26
27class RsaKeyFactory : public AsymmetricKeyFactory {
28  public:
29    RsaKeyFactory(const KeymasterContext* context) : AsymmetricKeyFactory(context) {}
30
31    keymaster_error_t GenerateKey(const AuthorizationSet& key_description,
32                                  KeymasterKeyBlob* key_blob, AuthorizationSet* hw_enforced,
33                                  AuthorizationSet* sw_enforced) const override;
34    keymaster_error_t ImportKey(const AuthorizationSet& key_description,
35                                keymaster_key_format_t input_key_material_format,
36                                const KeymasterKeyBlob& input_key_material,
37                                KeymasterKeyBlob* output_key_blob, AuthorizationSet* hw_enforced,
38                                AuthorizationSet* sw_enforced) const override;
39
40    keymaster_error_t CreateEmptyKey(const AuthorizationSet& hw_enforced,
41                                     const AuthorizationSet& sw_enforced,
42                                     UniquePtr<AsymmetricKey>* key) const override;
43
44    OperationFactory* GetOperationFactory(keymaster_purpose_t purpose) const override;
45
46    keymaster_algorithm_t keymaster_key_type() const override { return KM_ALGORITHM_RSA; }
47    int evp_key_type() const override { return EVP_PKEY_RSA; }
48
49  protected:
50    keymaster_error_t UpdateImportKeyDescription(const AuthorizationSet& key_description,
51                                                 keymaster_key_format_t import_key_format,
52                                                 const KeymasterKeyBlob& import_key_material,
53                                                 AuthorizationSet* updated_description,
54                                                 uint64_t* public_exponent,
55                                                 uint32_t* key_size) const;
56};
57
58}  // namespace keymaster
59
60#endif  // SYSTEM_KEYMASTER_RSA_KEY_FACTORY_H_
61