blob: e3e15f6944efc756ec8280a2450cf4019973ded0 [file] [log] [blame]
Thai Duongf862a762015-03-18 14:10:56 -07001/*
2 * Copyright 2014 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_H_
18#define SYSTEM_KEYMASTER_EC_KEY_H_
19
20#include <openssl/ec.h>
21
22#include "asymmetric_key.h"
23
24namespace keymaster {
25
26class EcKeyFactory : public AsymmetricKeyFactory {
27 public:
Shawn Willden0cb69422015-05-26 08:31:37 -060028 EcKeyFactory(const KeymasterContext* context) : AsymmetricKeyFactory(context) {}
29
30 keymaster_error_t GenerateKey(const AuthorizationSet& key_description,
31 KeymasterKeyBlob* key_blob, AuthorizationSet* hw_enforced,
32 AuthorizationSet* sw_enforced) override;
33 keymaster_error_t ImportKey(const AuthorizationSet& key_description,
34 keymaster_key_format_t input_key_material_format,
35 const KeymasterKeyBlob& input_key_material,
36 KeymasterKeyBlob* output_key_blob, AuthorizationSet* hw_enforced,
37 AuthorizationSet* sw_enforced) override;
38
39 keymaster_error_t CreateEmptyKey(const AuthorizationSet& hw_enforced,
40 const AuthorizationSet& sw_enforced,
41 UniquePtr<AsymmetricKey>* key) override;
Thai Duongf862a762015-03-18 14:10:56 -070042
43 private:
44 static EC_GROUP* choose_group(size_t key_size_bits);
45 static keymaster_error_t get_group_size(const EC_GROUP& group, size_t* key_size_bits);
46
47 struct EC_GROUP_Delete {
48 void operator()(EC_GROUP* p) { EC_GROUP_free(p); }
49 };
50};
51
52class EcdsaKeyFactory : public EcKeyFactory {
53 public:
Shawn Willden0cb69422015-05-26 08:31:37 -060054 EcdsaKeyFactory(const KeymasterContext* context) : EcKeyFactory(context) {}
55
56 keymaster_algorithm_t registry_key() const override { return KM_ALGORITHM_EC; }
57 int evp_key_type() override { return EVP_PKEY_EC; }
Thai Duongf862a762015-03-18 14:10:56 -070058};
59
Shawn Willden13e29e02015-05-08 11:02:46 -060060class EcdsaOperationFactory;
Thai Duongf862a762015-03-18 14:10:56 -070061
62class EcKey : public AsymmetricKey {
Shawn Willden0cb69422015-05-26 08:31:37 -060063 public:
64 EcKey(const AuthorizationSet& hw_enforced, const AuthorizationSet& sw_enforced,
65 keymaster_error_t* error)
66 : AsymmetricKey(hw_enforced, sw_enforced, error) {}
Thai Duongf862a762015-03-18 14:10:56 -070067
Shawn Willden0cb69422015-05-26 08:31:37 -060068 bool InternalToEvp(EVP_PKEY* pkey) const override;
69 bool EvpToInternal(const EVP_PKEY* pkey) override;
Thai Duongf862a762015-03-18 14:10:56 -070070
71 struct EC_Delete {
72 void operator()(EC_KEY* p) { EC_KEY_free(p); }
73 };
74
75 EC_KEY* key() const { return EC_KEY_dup(ec_key_.get()); }
76
77 UniquePtr<EC_KEY, EC_Delete> ec_key_;
78};
79
80} // namespace keymaster
81
82#endif // SYSTEM_KEYMASTER_EC_KEY_H_